@@ -69,6 +69,11 @@ pub mut:
6969 vals []? string
7070}
7171
72+ pub struct RowNoNull {
73+ pub mut :
74+ vals []string
75+ }
76+
7277pub struct Result {
7378pub :
7479 cols map [string ]int
@@ -259,6 +264,22 @@ fn res_to_rows(res voidptr) []Row {
259264 return rows
260265}
261266
267+ fn res_to_rows_no_null (res voidptr ) []RowNoNull {
268+ nr_rows := C.PQntuples (res)
269+ nr_cols := C.PQnfields (res)
270+ mut rows := []RowNoNull{}
271+ for i in 0 .. nr_rows {
272+ mut row := RowNoNull{}
273+ for j in 0 .. nr_cols {
274+ val := C.PQgetvalue (res, i, j)
275+ row.vals << unsafe { cstring_to_vstring (val) }
276+ }
277+ rows << row
278+ }
279+ C.PQclear (res)
280+ return rows
281+ }
282+
262283// res_to_result creates a `Result` struct out of a `C.PGresult` pointer
263284fn res_to_result (res voidptr ) Result {
264285 nr_rows := C.PQntuples (res)
@@ -338,6 +359,12 @@ pub fn (db &DB) exec(query string) ![]Row {
338359 return db.handle_error_or_rows (res, 'exec' )
339360}
340361
362+ // exec_no_null works like exec, but the fields can't be NULL, no optionals
363+ pub fn (db &DB) exec_no_null (query string ) ! []RowNoNull {
364+ res := C.PQexec (db.conn, & char (query.str))
365+ return db.handle_error_or_rows_no_null (res, 'exec' )
366+ }
367+
341368// exec_result submits a command to the database server and wait for the result, returning an error on failure and a `Result` set on success
342369pub fn (db &DB) exec_result (query string ) ! Result {
343370 res := C.PQexec (db.conn, & char (query.str))
@@ -440,6 +467,7 @@ fn (db &DB) handle_error_or_rows(res voidptr, elabel string) ![]Row {
440467 e := unsafe { C.PQerrorMessage (db.conn).vstring () }
441468 if e != '' {
442469 C.PQclear (res)
470+ // TODO make it default
443471 $if trace_pg_error ? {
444472 eprintln ('pg error: ${e }' )
445473 }
@@ -448,6 +476,19 @@ fn (db &DB) handle_error_or_rows(res voidptr, elabel string) ![]Row {
448476 return res_to_rows (res)
449477}
450478
479+ fn (db &DB) handle_error_or_rows_no_null (res voidptr , elabel string ) ! []RowNoNull {
480+ // TODO copypasta
481+ e := unsafe { C.PQerrorMessage (db.conn).vstring () }
482+ if e != '' {
483+ C.PQclear (res)
484+ $if trace_pg_error ? {
485+ eprintln ('pg error: ${e }' )
486+ }
487+ return error ('pg ${elabel } error:\n ${e }' )
488+ }
489+ return res_to_rows_no_null (res)
490+ }
491+
451492// hande_error_or_result is an internal function similar to handle_error_or_rows that returns `Result` instead of `[]Row`
452493fn (db &DB) handle_error_or_result (res voidptr , elabel string ) ! Result {
453494 e := unsafe { C.PQerrorMessage (db.conn).vstring () }
0 commit comments