diff --git a/vlib/pg/pg.v b/vlib/pg/pg.v index ee7d34361e..c43a273137 100644 --- a/vlib/pg/pg.v +++ b/vlib/pg/pg.v @@ -122,25 +122,37 @@ pub fn (db DB) exec_one(query string) ?pg.Row { } // +pub fn (db DB) exec_param_many(query string, params []string) []pg.Row { + mut param_vals := &byteptr( malloc( params.len * sizeof(byteptr) ) ) + for i := 0; i < params.len; i++ { + param_vals[i] = params[i].str + } + res := C.PQexecParams(db.conn, query.str, params.len, 0, param_vals, 0, 0, 0) + unsafe{ free(param_vals) } + return db.handle_error_or_result(res, 'exec_param_many') +} + pub fn (db DB) exec_param2(query string, param, param2 string) []pg.Row { mut param_vals := [2]byteptr param_vals[0] = param.str param_vals[1] = param2.str res := C.PQexecParams(db.conn, query.str, 2, 0, param_vals, 0, 0, 0) - e := string(C.PQerrorMessage(db.conn)) - if e != '' { - println('pg exec2 error:') - println(e) - return res_to_rows(res) - } - return res_to_rows(res) + return db.handle_error_or_result(res, 'exec_param2') } pub fn (db DB) exec_param(query string, param string) []pg.Row { mut param_vals := [1]byteptr param_vals[0] = param.str res := C.PQexecParams(db.conn, query.str, 1, 0, param_vals, 0, 0, 0) - return res_to_rows(res) + return db.handle_error_or_result(res, 'exec_param') } - +fn (db DB) handle_error_or_result(res voidptr, elabel string) []pg.Row { + e := string(C.PQerrorMessage(db.conn)) + if e != '' { + println('pg $elabel error:') + println(e) + return res_to_rows(res) + } + return res_to_rows(res) +}