2019-08-03 22:26:12 +03:00
|
|
|
module pg
|
|
|
|
|
|
|
|
#flag -lpq
|
|
|
|
#flag linux -I/usr/include/postgresql
|
|
|
|
#flag darwin -I/opt/local/include/postgresql11
|
2020-03-26 16:17:13 +03:00
|
|
|
#flag windows -I @VROOT/thirdparty/pg/include
|
|
|
|
#flag windows -L @VROOT/thirdparty/pg/win64
|
2019-08-03 22:26:12 +03:00
|
|
|
#include <libpq-fe.h>
|
|
|
|
|
2019-11-23 19:45:35 +03:00
|
|
|
pub struct DB {
|
2019-08-03 22:26:12 +03:00
|
|
|
mut:
|
2019-09-12 04:53:14 +03:00
|
|
|
conn &C.PGconn
|
2019-08-03 22:26:12 +03:00
|
|
|
}
|
|
|
|
|
2019-11-23 19:45:35 +03:00
|
|
|
pub struct Row {
|
2019-09-12 04:53:14 +03:00
|
|
|
pub mut:
|
2019-08-03 22:26:12 +03:00
|
|
|
vals []string
|
|
|
|
}
|
|
|
|
|
|
|
|
struct C.PGResult { }
|
|
|
|
|
2019-11-23 19:45:35 +03:00
|
|
|
pub struct Config {
|
2019-08-20 11:08:06 +03:00
|
|
|
pub:
|
2019-12-05 22:31:56 +03:00
|
|
|
host string
|
2020-03-06 20:40:24 +03:00
|
|
|
port int = 5432
|
2019-12-05 22:31:56 +03:00
|
|
|
user string
|
|
|
|
password string
|
|
|
|
dbname string
|
2019-08-20 11:08:06 +03:00
|
|
|
}
|
|
|
|
|
2019-09-12 04:53:14 +03:00
|
|
|
fn C.PQconnectdb(a byteptr) &C.PGconn
|
2019-11-23 19:45:35 +03:00
|
|
|
fn C.PQerrorMessage(voidptr) byteptr
|
2019-08-03 22:26:12 +03:00
|
|
|
fn C.PQgetvalue(voidptr, int, int) byteptr
|
2019-11-23 19:45:35 +03:00
|
|
|
fn C.PQstatus(voidptr) int
|
2019-11-25 06:48:48 +03:00
|
|
|
fn C.PQntuples(voidptr) int
|
|
|
|
fn C.PQnfields(voidptr) int
|
2019-12-05 22:31:56 +03:00
|
|
|
fn C.PQexec(voidptr) voidptr
|
|
|
|
fn C.PQexecParams(voidptr) voidptr
|
2020-06-26 12:55:59 +03:00
|
|
|
fn C.PQclear(voidptr) voidptr
|
2019-08-03 22:26:12 +03:00
|
|
|
|
2020-05-10 22:16:03 +03:00
|
|
|
pub fn connect(config Config) ?DB {
|
2020-03-06 04:01:53 +03:00
|
|
|
conninfo := 'host=$config.host port=$config.port user=$config.user dbname=$config.dbname password=$config.password'
|
2019-12-05 22:31:56 +03:00
|
|
|
conn := C.PQconnectdb(conninfo.str)
|
2019-08-03 22:26:12 +03:00
|
|
|
status := C.PQstatus(conn)
|
2019-12-05 22:31:56 +03:00
|
|
|
println("status=$status")
|
2019-11-23 19:45:35 +03:00
|
|
|
if status != C.CONNECTION_OK {
|
|
|
|
error_msg := C.PQerrorMessage(conn)
|
2020-08-10 19:05:26 +03:00
|
|
|
return error ('Connection to a PG database failed: ' + unsafe { error_msg.vstring() } )
|
2019-08-03 22:26:12 +03:00
|
|
|
}
|
2019-11-23 19:45:35 +03:00
|
|
|
return DB {conn: conn}
|
2019-08-03 22:26:12 +03:00
|
|
|
}
|
|
|
|
|
2020-05-10 22:16:03 +03:00
|
|
|
fn res_to_rows(res voidptr) []Row {
|
2019-11-23 19:45:35 +03:00
|
|
|
nr_rows := C.PQntuples(res)
|
|
|
|
nr_cols := C.PQnfields(res)
|
2020-05-10 22:16:03 +03:00
|
|
|
mut rows := []Row{}
|
2020-02-24 19:55:16 +03:00
|
|
|
for i in 0..nr_rows {
|
2019-08-03 22:26:12 +03:00
|
|
|
mut row := Row{}
|
2020-02-24 19:55:16 +03:00
|
|
|
for j in 0..nr_cols {
|
2019-11-23 19:45:35 +03:00
|
|
|
val := C.PQgetvalue(res, i, j)
|
2020-08-10 19:05:26 +03:00
|
|
|
sval := unsafe { val.vstring() }
|
|
|
|
row.vals << sval
|
2019-08-03 22:26:12 +03:00
|
|
|
}
|
|
|
|
rows << row
|
|
|
|
}
|
2020-06-26 12:55:59 +03:00
|
|
|
C.PQclear(res)
|
2019-08-03 22:26:12 +03:00
|
|
|
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 19:45:35 +03:00
|
|
|
return val.int()
|
2019-08-03 22:26:12 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2020-05-10 22:16:03 +03:00
|
|
|
pub fn (db DB) q_strings(query string) []Row {
|
2019-08-03 22:26:12 +03:00
|
|
|
return db.exec(query)
|
|
|
|
}
|
|
|
|
|
2020-05-10 22:16:03 +03:00
|
|
|
pub fn (db DB) exec(query string) []Row {
|
2019-08-03 22:26:12 +03:00
|
|
|
res := C.PQexec(db.conn, query.str)
|
2020-08-10 19:05:26 +03:00
|
|
|
e := unsafe { C.PQerrorMessage(db.conn).vstring() }
|
2019-08-03 22:26:12 +03:00
|
|
|
if e != '' {
|
|
|
|
println('pg exec error:')
|
|
|
|
println(e)
|
|
|
|
return res_to_rows(res)
|
|
|
|
}
|
|
|
|
return res_to_rows(res)
|
|
|
|
}
|
|
|
|
|
2020-05-10 22:16:03 +03:00
|
|
|
fn rows_first_or_empty(rows []Row) ?Row {
|
2019-08-20 15:34:34 +03:00
|
|
|
if rows.len == 0 {
|
|
|
|
return error('no row')
|
2019-11-23 19:45:35 +03:00
|
|
|
}
|
2019-08-20 15:34:34 +03:00
|
|
|
return rows[0]
|
|
|
|
}
|
2019-11-23 19:45:35 +03:00
|
|
|
|
2020-05-10 22:16:03 +03:00
|
|
|
pub fn (db DB) exec_one(query string) ?Row {
|
2019-08-09 19:10:59 +03:00
|
|
|
res := C.PQexec(db.conn, query.str)
|
2020-08-10 19:05:26 +03:00
|
|
|
e := unsafe { C.PQerrorMessage(db.conn).vstring() }
|
2019-08-09 19:10:59 +03:00
|
|
|
if e != '' {
|
2019-08-20 15:34:34 +03:00
|
|
|
return error('pg exec error: "$e"')
|
2019-08-09 19:10:59 +03:00
|
|
|
}
|
2020-08-29 02:58:03 +03:00
|
|
|
row := rows_first_or_empty( res_to_rows(res) )?
|
2019-08-20 15:34:34 +03:00
|
|
|
return row
|
2019-08-09 19:10:59 +03:00
|
|
|
}
|
|
|
|
|
2020-02-27 01:17:56 +03: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.
|
2020-05-10 22:16:03 +03:00
|
|
|
pub fn (db DB) exec_param_many(query string, params []string) []Row {
|
2020-02-27 01:17:56 +03:00
|
|
|
unsafe {
|
2020-06-07 17:05:44 +03:00
|
|
|
mut param_vals := &byteptr(malloc(params.len * 8))
|
2020-02-27 01:17:56 +03:00
|
|
|
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-04 00:07:28 +03:00
|
|
|
}
|
2020-02-27 01:17:56 +03:00
|
|
|
}
|
2020-01-04 00:07:28 +03:00
|
|
|
|
2020-05-10 22:16:03 +03:00
|
|
|
pub fn (db DB) exec_param2(query string, param, param2 string) []Row {
|
2020-08-19 08:10:09 +03:00
|
|
|
mut param_vals := [2]byteptr{}
|
2019-11-23 19:45:35 +03:00
|
|
|
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-04 00:07:28 +03:00
|
|
|
return db.handle_error_or_result(res, 'exec_param2')
|
2019-08-03 22:26:12 +03:00
|
|
|
}
|
|
|
|
|
2020-05-10 22:16:03 +03:00
|
|
|
pub fn (db DB) exec_param(query string, param string) []Row {
|
2020-08-19 08:10:09 +03:00
|
|
|
mut param_vals := [1]byteptr{}
|
2019-11-23 19:45:35 +03:00
|
|
|
param_vals[0] = param.str
|
|
|
|
res := C.PQexecParams(db.conn, query.str, 1, 0, param_vals, 0, 0, 0)
|
2020-01-04 00:07:28 +03:00
|
|
|
return db.handle_error_or_result(res, 'exec_param')
|
2019-08-03 22:26:12 +03:00
|
|
|
}
|
|
|
|
|
2020-05-10 22:16:03 +03:00
|
|
|
fn (db DB) handle_error_or_result(res voidptr, elabel string) []Row {
|
2020-08-10 19:05:26 +03:00
|
|
|
e := unsafe { C.PQerrorMessage(db.conn).vstring() }
|
2020-01-04 00:07:28 +03:00
|
|
|
if e != '' {
|
|
|
|
println('pg $elabel error:')
|
|
|
|
println(e)
|
|
|
|
return res_to_rows(res)
|
|
|
|
}
|
|
|
|
return res_to_rows(res)
|
|
|
|
}
|