pg: add db.exec_param_many()

pull/3321/head
Delyan Angelov 2020-01-03 23:07:28 +02:00 committed by Alexander Medvednikov
parent 67f397f888
commit ddd83c2108
1 changed files with 21 additions and 9 deletions

View File

@ -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 { pub fn (db DB) exec_param2(query string, param, param2 string) []pg.Row {
mut param_vals := [2]byteptr mut param_vals := [2]byteptr
param_vals[0] = param.str param_vals[0] = param.str
param_vals[1] = param2.str param_vals[1] = param2.str
res := C.PQexecParams(db.conn, query.str, 2, 0, param_vals, 0, 0, 0) res := C.PQexecParams(db.conn, query.str, 2, 0, param_vals, 0, 0, 0)
e := string(C.PQerrorMessage(db.conn)) return db.handle_error_or_result(res, 'exec_param2')
if e != '' {
println('pg exec2 error:')
println(e)
return res_to_rows(res)
}
return res_to_rows(res)
} }
pub fn (db DB) exec_param(query string, param string) []pg.Row { pub fn (db DB) exec_param(query string, param string) []pg.Row {
mut param_vals := [1]byteptr mut param_vals := [1]byteptr
param_vals[0] = param.str param_vals[0] = param.str
res := C.PQexecParams(db.conn, query.str, 1, 0, param_vals, 0, 0, 0) 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)
}