1
0
mirror of https://github.com/vlang/v.git synced 2023-08-10 21:13:21 +03:00

pg: add db.exec_param_many()

This commit is contained in:
Delyan Angelov 2020-01-03 23:07:28 +02:00 committed by Alexander Medvednikov
parent 67f397f888
commit ddd83c2108

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 {
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)
}