v/vlib/pg/pg.v

165 lines
3.7 KiB
V
Raw Normal View History

module pg
#flag -lpq
#flag linux -I/usr/include/postgresql
#flag darwin -I/opt/local/include/postgresql11
2020-03-26 14:17:13 +01:00
#flag windows -I @VROOT/thirdparty/pg/include
#flag windows -L @VROOT/thirdparty/pg/win64
#include <libpq-fe.h>
2019-11-23 17:45:35 +01:00
pub struct DB {
mut:
conn &C.PGconn
}
2019-11-23 17:45:35 +01:00
pub struct Row {
pub mut:
vals []string
}
struct C.PGResult { }
2019-11-23 17:45:35 +01:00
pub struct Config {
2019-08-20 10:08:06 +02:00
pub:
2019-12-05 20:31:56 +01:00
host string
port int = 5432
2019-12-05 20:31:56 +01:00
user string
password string
dbname string
2019-08-20 10:08:06 +02:00
}
fn C.PQconnectdb(a byteptr) &C.PGconn
2019-11-23 17:45:35 +01:00
fn C.PQerrorMessage(voidptr) byteptr
fn C.PQgetvalue(voidptr, int, int) byteptr
2019-11-23 17:45:35 +01:00
fn C.PQstatus(voidptr) int
2019-11-25 04:48:48 +01:00
fn C.PQntuples(voidptr) int
fn C.PQnfields(voidptr) int
2019-12-05 20:31:56 +01:00
fn C.PQexec(voidptr) voidptr
fn C.PQexecParams(voidptr) voidptr
pub fn connect(config pg.Config) ?DB {
2020-03-06 02:01:53 +01:00
conninfo := 'host=$config.host port=$config.port user=$config.user dbname=$config.dbname password=$config.password'
2019-12-05 20:31:56 +01:00
conn := C.PQconnectdb(conninfo.str)
status := C.PQstatus(conn)
2019-12-05 20:31:56 +01:00
println("status=$status")
2019-11-23 17:45:35 +01:00
if status != C.CONNECTION_OK {
error_msg := C.PQerrorMessage(conn)
2019-12-05 20:31:56 +01:00
return error ('Connection to a PG database failed: ' + string(error_msg))
}
2019-11-23 17:45:35 +01:00
return DB {conn: conn}
}
fn res_to_rows(res voidptr) []pg.Row {
2019-11-23 17:45:35 +01:00
nr_rows := C.PQntuples(res)
nr_cols := C.PQnfields(res)
2020-04-26 16:25:54 +02:00
mut rows := []pg.Row{}
for i in 0..nr_rows {
mut row := Row{}
for j in 0..nr_cols {
2019-11-23 17:45:35 +01:00
val := C.PQgetvalue(res, i, j)
row.vals << string(val)
}
rows << row
}
return rows
}
pub fn (db DB) q_int(query string) int {
rows := db.exec(query)
if rows.len == 0 {
println('q_int "$query" not found')
return 0
}
row := rows[0]
if row.vals.len == 0 {
return 0
}
val := row.vals[0]
2019-11-23 17:45:35 +01:00
return val.int()
}
pub fn (db DB) q_string(query string) string {
rows := db.exec(query)
if rows.len == 0 {
println('q_string "$query" not found')
return ''
}
row := rows[0]
if row.vals.len == 0 {
return ''
}
val := row.vals[0]
return val
}
pub fn (db DB) q_strings(query string) []pg.Row {
return db.exec(query)
}
pub fn (db DB) exec(query string) []pg.Row {
res := C.PQexec(db.conn, query.str)
e := string(C.PQerrorMessage(db.conn))
if e != '' {
println('pg exec error:')
println(e)
return res_to_rows(res)
}
return res_to_rows(res)
}
fn rows_first_or_empty(rows []pg.Row) ?pg.Row {
if rows.len == 0 {
return error('no row')
2019-11-23 17:45:35 +01:00
}
return rows[0]
}
2019-11-23 17:45:35 +01:00
pub fn (db DB) exec_one(query string) ?pg.Row {
2019-08-09 18:10:59 +02:00
res := C.PQexec(db.conn, query.str)
e := string(C.PQerrorMessage(db.conn))
if e != '' {
return error('pg exec error: "$e"')
2019-08-09 18:10:59 +02:00
}
row := rows_first_or_empty( res_to_rows(res) )
return row
2019-08-09 18:10:59 +02:00
}
2020-02-26 23:17:56 +01:00
// The entire function can be considered unsafe because of the malloc and the
// free. This prevents warnings and doesn't seem to affect behavior.
pub fn (db DB) exec_param_many(query string, params []string) []pg.Row {
2020-02-26 23:17:56 +01:00
unsafe {
mut param_vals := &byteptr( malloc( params.len * sizeof(byteptr) ) )
for i in 0..params.len {
param_vals[i] = params[i].str
}
res := C.PQexecParams(db.conn, query.str, params.len, 0, param_vals, 0, 0, 0)
free(param_vals)
return db.handle_error_or_result(res, 'exec_param_many')
2020-01-03 22:07:28 +01:00
}
2020-02-26 23:17:56 +01:00
}
2020-01-03 22:07:28 +01:00
pub fn (db DB) exec_param2(query string, param, param2 string) []pg.Row {
2019-11-23 17:45:35 +01:00
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)
2020-01-03 22:07:28 +01:00
return db.handle_error_or_result(res, 'exec_param2')
}
pub fn (db DB) exec_param(query string, param string) []pg.Row {
2019-11-23 17:45:35 +01:00
mut param_vals := [1]byteptr
param_vals[0] = param.str
res := C.PQexecParams(db.conn, query.str, 1, 0, param_vals, 0, 0, 0)
2020-01-03 22:07:28 +01:00
return db.handle_error_or_result(res, 'exec_param')
}
fn (db DB) handle_error_or_result(res voidptr, elabel string) []pg.Row {
2020-01-03 22:07:28 +01:00
e := string(C.PQerrorMessage(db.conn))
if e != '' {
println('pg $elabel error:')
println(e)
return res_to_rows(res)
}
return res_to_rows(res)
}