From ddd83c210800c91bd489de29b8ecc1fc145f71fb Mon Sep 17 00:00:00 2001 From: Delyan Angelov Date: Fri, 3 Jan 2020 23:07:28 +0200 Subject: [PATCH] pg: add db.exec_param_many() --- vlib/pg/pg.v | 30 +++++++++++++++++++++--------- 1 file changed, 21 insertions(+), 9 deletions(-) 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) +}