pg: remove `pg.`
parent
7c5905164e
commit
40aad27a67
24
vlib/pg/pg.v
24
vlib/pg/pg.v
|
@ -37,7 +37,7 @@ fn C.PQnfields(voidptr) int
|
||||||
fn C.PQexec(voidptr) voidptr
|
fn C.PQexec(voidptr) voidptr
|
||||||
fn C.PQexecParams(voidptr) voidptr
|
fn C.PQexecParams(voidptr) voidptr
|
||||||
|
|
||||||
pub fn connect(config pg.Config) ?DB {
|
pub fn connect(config Config) ?DB {
|
||||||
conninfo := 'host=$config.host port=$config.port user=$config.user dbname=$config.dbname password=$config.password'
|
conninfo := 'host=$config.host port=$config.port user=$config.user dbname=$config.dbname password=$config.password'
|
||||||
conn := C.PQconnectdb(conninfo.str)
|
conn := C.PQconnectdb(conninfo.str)
|
||||||
status := C.PQstatus(conn)
|
status := C.PQstatus(conn)
|
||||||
|
@ -49,10 +49,10 @@ pub fn connect(config pg.Config) ?DB {
|
||||||
return DB {conn: conn}
|
return DB {conn: conn}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn res_to_rows(res voidptr) []pg.Row {
|
fn res_to_rows(res voidptr) []Row {
|
||||||
nr_rows := C.PQntuples(res)
|
nr_rows := C.PQntuples(res)
|
||||||
nr_cols := C.PQnfields(res)
|
nr_cols := C.PQnfields(res)
|
||||||
mut rows := []pg.Row{}
|
mut rows := []Row{}
|
||||||
for i in 0..nr_rows {
|
for i in 0..nr_rows {
|
||||||
mut row := Row{}
|
mut row := Row{}
|
||||||
for j in 0..nr_cols {
|
for j in 0..nr_cols {
|
||||||
|
@ -92,11 +92,11 @@ pub fn (db DB) q_string(query string) string {
|
||||||
return val
|
return val
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn (db DB) q_strings(query string) []pg.Row {
|
pub fn (db DB) q_strings(query string) []Row {
|
||||||
return db.exec(query)
|
return db.exec(query)
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn (db DB) exec(query string) []pg.Row {
|
pub fn (db DB) exec(query string) []Row {
|
||||||
res := C.PQexec(db.conn, query.str)
|
res := C.PQexec(db.conn, query.str)
|
||||||
e := string(C.PQerrorMessage(db.conn))
|
e := string(C.PQerrorMessage(db.conn))
|
||||||
if e != '' {
|
if e != '' {
|
||||||
|
@ -107,26 +107,26 @@ pub fn (db DB) exec(query string) []pg.Row {
|
||||||
return res_to_rows(res)
|
return res_to_rows(res)
|
||||||
}
|
}
|
||||||
|
|
||||||
fn rows_first_or_empty(rows []pg.Row) ?pg.Row {
|
fn rows_first_or_empty(rows []Row) ?Row {
|
||||||
if rows.len == 0 {
|
if rows.len == 0 {
|
||||||
return error('no row')
|
return error('no row')
|
||||||
}
|
}
|
||||||
return rows[0]
|
return rows[0]
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn (db DB) exec_one(query string) ?pg.Row {
|
pub fn (db DB) exec_one(query string) ?Row {
|
||||||
res := C.PQexec(db.conn, query.str)
|
res := C.PQexec(db.conn, query.str)
|
||||||
e := string(C.PQerrorMessage(db.conn))
|
e := string(C.PQerrorMessage(db.conn))
|
||||||
if e != '' {
|
if e != '' {
|
||||||
return error('pg exec error: "$e"')
|
return error('pg exec error: "$e"')
|
||||||
}
|
}
|
||||||
row := rows_first_or_empty( res_to_rows(res) )
|
row := rows_first_or_empty( res_to_rows(res) ) or { return error(err) }
|
||||||
return row
|
return row
|
||||||
}
|
}
|
||||||
|
|
||||||
// The entire function can be considered unsafe because of the malloc and the
|
// The entire function can be considered unsafe because of the malloc and the
|
||||||
// free. This prevents warnings and doesn't seem to affect behavior.
|
// free. This prevents warnings and doesn't seem to affect behavior.
|
||||||
pub fn (db DB) exec_param_many(query string, params []string) []pg.Row {
|
pub fn (db DB) exec_param_many(query string, params []string) []Row {
|
||||||
unsafe {
|
unsafe {
|
||||||
mut param_vals := &byteptr( malloc( params.len * sizeof(byteptr) ) )
|
mut param_vals := &byteptr( malloc( params.len * sizeof(byteptr) ) )
|
||||||
for i in 0..params.len {
|
for i in 0..params.len {
|
||||||
|
@ -138,7 +138,7 @@ pub fn (db DB) exec_param_many(query string, params []string) []pg.Row {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn (db DB) exec_param2(query string, param, param2 string) []pg.Row {
|
pub fn (db DB) exec_param2(query string, param, param2 string) []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
|
||||||
|
@ -146,14 +146,14 @@ pub fn (db DB) exec_param2(query string, param, param2 string) []pg.Row {
|
||||||
return db.handle_error_or_result(res, 'exec_param2')
|
return db.handle_error_or_result(res, 'exec_param2')
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn (db DB) exec_param(query string, param string) []pg.Row {
|
pub fn (db DB) exec_param(query string, param string) []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 db.handle_error_or_result(res, 'exec_param')
|
return db.handle_error_or_result(res, 'exec_param')
|
||||||
}
|
}
|
||||||
|
|
||||||
fn (db DB) handle_error_or_result(res voidptr, elabel string) []pg.Row {
|
fn (db DB) handle_error_or_result(res voidptr, elabel string) []Row {
|
||||||
e := string(C.PQerrorMessage(db.conn))
|
e := string(C.PQerrorMessage(db.conn))
|
||||||
if e != '' {
|
if e != '' {
|
||||||
println('pg $elabel error:')
|
println('pg $elabel error:')
|
||||||
|
|
Loading…
Reference in New Issue