2019-08-03 21:26:12 +02:00
|
|
|
module pg
|
|
|
|
|
2021-03-13 07:06:53 +01:00
|
|
|
import io
|
|
|
|
|
2019-08-03 21:26:12 +02:00
|
|
|
#flag -lpq
|
|
|
|
#flag linux -I/usr/include/postgresql
|
|
|
|
#flag darwin -I/opt/local/include/postgresql11
|
2021-04-19 18:01:47 +02:00
|
|
|
#flag windows -I @VEXEROOT/thirdparty/pg/include
|
|
|
|
#flag windows -L @VEXEROOT/thirdparty/pg/win64
|
2021-03-13 07:06:53 +01:00
|
|
|
|
|
|
|
// PostgreSQL Source Code
|
|
|
|
// https://doxygen.postgresql.org/libpq-fe_8h.html
|
2019-08-03 21:26:12 +02:00
|
|
|
#include <libpq-fe.h>
|
2021-04-25 17:57:55 +02:00
|
|
|
// for orm
|
|
|
|
#include <arpa/inet.h>
|
2021-03-13 07:06:53 +01:00
|
|
|
|
2019-11-23 17:45:35 +01:00
|
|
|
pub struct DB {
|
2019-08-03 21:26:12 +02:00
|
|
|
mut:
|
2019-09-12 03:53:14 +02:00
|
|
|
conn &C.PGconn
|
2019-08-03 21:26:12 +02:00
|
|
|
}
|
|
|
|
|
2019-11-23 17:45:35 +01:00
|
|
|
pub struct Row {
|
2019-09-12 03:53:14 +02:00
|
|
|
pub mut:
|
2019-08-03 21:26:12 +02:00
|
|
|
vals []string
|
|
|
|
}
|
|
|
|
|
2021-03-13 07:06:53 +01:00
|
|
|
struct C.PGResult {
|
|
|
|
}
|
2019-08-03 21:26:12 +02:00
|
|
|
|
2019-11-23 17:45:35 +01:00
|
|
|
pub struct Config {
|
2019-08-20 10:08:06 +02:00
|
|
|
pub:
|
2020-10-03 07:03:11 +02:00
|
|
|
host string
|
|
|
|
port int = 5432
|
|
|
|
user string
|
2019-12-05 20:31:56 +01:00
|
|
|
password string
|
2020-10-03 07:03:11 +02:00
|
|
|
dbname string
|
2019-08-20 10:08:06 +02:00
|
|
|
}
|
|
|
|
|
2019-09-12 03:53:14 +02:00
|
|
|
fn C.PQconnectdb(a byteptr) &C.PGconn
|
2019-08-03 21:26:12 +02:00
|
|
|
|
2020-10-04 06:32:47 +02:00
|
|
|
fn C.PQerrorMessage(voidptr) byteptr
|
2020-10-03 07:03:11 +02:00
|
|
|
|
2020-11-09 08:22:16 +01:00
|
|
|
fn C.PQgetvalue(&C.PGResult, int, int) byteptr
|
2020-10-03 07:03:11 +02:00
|
|
|
|
2020-10-04 06:32:47 +02:00
|
|
|
fn C.PQstatus(voidptr) int
|
2020-10-03 07:03:11 +02:00
|
|
|
|
2020-11-09 08:22:16 +01:00
|
|
|
fn C.PQresultStatus(voidptr) int
|
|
|
|
|
|
|
|
fn C.PQntuples(&C.PGResult) int
|
2020-10-03 07:03:11 +02:00
|
|
|
|
2020-11-09 08:22:16 +01:00
|
|
|
fn C.PQnfields(&C.PGResult) int
|
2020-10-03 07:03:11 +02:00
|
|
|
|
2021-03-13 07:06:53 +01:00
|
|
|
fn C.PQexec(voidptr, byteptr) &C.PGResult
|
|
|
|
|
|
|
|
// Params:
|
|
|
|
// const Oid *paramTypes
|
|
|
|
// const char *const *paramValues
|
|
|
|
// const int *paramLengths
|
|
|
|
// const int *paramFormats
|
|
|
|
fn C.PQexecParams(conn voidptr, command byteptr, nParams int, paramTypes int, paramValues byteptr, paramLengths int, paramFormats int, resultFormat int) &C.PGResult
|
|
|
|
|
|
|
|
fn C.PQputCopyData(conn voidptr, buffer byteptr, nbytes int) int
|
|
|
|
|
2021-04-25 17:57:55 +02:00
|
|
|
fn C.PQputCopyEnd(voidptr, &byte) int
|
2020-10-03 07:03:11 +02:00
|
|
|
|
2021-03-13 07:06:53 +01:00
|
|
|
fn C.PQgetCopyData(conn voidptr, buffer &byteptr, async int) int
|
2020-10-03 07:03:11 +02:00
|
|
|
|
2020-11-09 08:22:16 +01:00
|
|
|
fn C.PQclear(&C.PGResult) voidptr
|
2020-10-03 07:03:11 +02:00
|
|
|
|
2021-03-13 07:06:53 +01:00
|
|
|
fn C.PQfreemem(voidptr)
|
|
|
|
|
2020-10-04 06:32:47 +02:00
|
|
|
fn C.PQfinish(voidptr)
|
2020-10-03 07:03:11 +02:00
|
|
|
|
2020-10-04 06:31:47 +02:00
|
|
|
// connect makes a new connection to the database server using
|
2020-10-03 07:03:11 +02:00
|
|
|
// the parameters from the `Config` structure, returning
|
|
|
|
// a connection error when something goes wrong
|
2020-05-10 21:16:03 +02:00
|
|
|
pub fn connect(config 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)
|
2020-10-03 07:03:11 +02:00
|
|
|
if conn == 0 {
|
|
|
|
return error('libpq memory allocation error')
|
|
|
|
}
|
2019-08-03 21:26:12 +02:00
|
|
|
status := C.PQstatus(conn)
|
2019-11-23 17:45:35 +01:00
|
|
|
if status != C.CONNECTION_OK {
|
2020-10-03 07:03:11 +02:00
|
|
|
// We force the construction of a new string as the
|
|
|
|
// error message will be freed by the next `PQfinish`
|
|
|
|
// call
|
2021-03-13 07:06:53 +01:00
|
|
|
c_error_msg := unsafe { C.PQerrorMessage(conn).vstring() }
|
2020-10-03 07:03:11 +02:00
|
|
|
error_msg := '$c_error_msg'
|
|
|
|
C.PQfinish(conn)
|
|
|
|
return error('Connection to a PG database failed: $error_msg')
|
|
|
|
}
|
|
|
|
return DB{
|
|
|
|
conn: conn
|
2019-08-03 21:26:12 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-10 21:16:03 +02:00
|
|
|
fn res_to_rows(res voidptr) []Row {
|
2019-11-23 17:45:35 +01:00
|
|
|
nr_rows := C.PQntuples(res)
|
|
|
|
nr_cols := C.PQnfields(res)
|
2020-11-09 08:22:16 +01:00
|
|
|
|
2020-05-10 21:16:03 +02:00
|
|
|
mut rows := []Row{}
|
2020-10-03 07:03:11 +02:00
|
|
|
for i in 0 .. nr_rows {
|
2019-08-03 21:26:12 +02:00
|
|
|
mut row := Row{}
|
2020-10-03 07:03:11 +02:00
|
|
|
for j in 0 .. nr_cols {
|
2019-11-23 17:45:35 +01:00
|
|
|
val := C.PQgetvalue(res, i, j)
|
2021-03-13 07:06:53 +01:00
|
|
|
sval := unsafe { val.vstring() }
|
2020-08-10 18:05:26 +02:00
|
|
|
row.vals << sval
|
2019-08-03 21:26:12 +02:00
|
|
|
}
|
|
|
|
rows << row
|
|
|
|
}
|
2020-11-09 08:22:16 +01:00
|
|
|
|
2020-06-26 11:55:59 +02:00
|
|
|
C.PQclear(res)
|
2019-08-03 21:26:12 +02:00
|
|
|
return rows
|
|
|
|
}
|
|
|
|
|
2021-02-15 16:53:38 +01:00
|
|
|
// close frees the underlying resource allocated by the database connection
|
2020-09-21 01:47:37 +02:00
|
|
|
pub fn (db DB) close() {
|
|
|
|
C.PQfinish(db.conn)
|
|
|
|
}
|
|
|
|
|
2020-10-04 06:31:47 +02:00
|
|
|
// q_int submit a command to the database server and
|
|
|
|
// returns an the first field in the first tuple
|
|
|
|
// converted to an int. If no row is found or on
|
|
|
|
// command failure, an error is returned
|
|
|
|
pub fn (db DB) q_int(query string) ?int {
|
|
|
|
rows := db.exec(query) ?
|
2019-08-03 21:26:12 +02:00
|
|
|
if rows.len == 0 {
|
2020-10-04 06:31:47 +02:00
|
|
|
return error('q_int "$query" not found')
|
2019-08-03 21:26:12 +02:00
|
|
|
}
|
|
|
|
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()
|
2019-08-03 21:26:12 +02:00
|
|
|
}
|
|
|
|
|
2021-03-13 07:06:53 +01:00
|
|
|
// q_string submit a command to the database server and
|
2020-10-04 06:31:47 +02:00
|
|
|
// returns an the first field in the first tuple
|
|
|
|
// as a string. If no row is found or on
|
|
|
|
// command failure, an error is returned
|
|
|
|
pub fn (db DB) q_string(query string) ?string {
|
|
|
|
rows := db.exec(query) ?
|
2019-08-03 21:26:12 +02:00
|
|
|
if rows.len == 0 {
|
2020-10-04 06:31:47 +02:00
|
|
|
return error('q_string "$query" not found')
|
2019-08-03 21:26:12 +02:00
|
|
|
}
|
|
|
|
row := rows[0]
|
|
|
|
if row.vals.len == 0 {
|
|
|
|
return ''
|
|
|
|
}
|
|
|
|
val := row.vals[0]
|
|
|
|
return val
|
|
|
|
}
|
|
|
|
|
2020-10-04 06:31:47 +02:00
|
|
|
// q_strings submit a command to the database server and
|
|
|
|
// returns the resulting row set. Alias of `exec`
|
|
|
|
pub fn (db DB) q_strings(query string) ?[]Row {
|
2019-08-03 21:26:12 +02:00
|
|
|
return db.exec(query)
|
|
|
|
}
|
|
|
|
|
2020-10-04 06:31:47 +02:00
|
|
|
// exec submit a command to the database server and wait
|
|
|
|
// for the result, returning an error on failure and a
|
|
|
|
// row set on success
|
|
|
|
pub fn (db DB) exec(query string) ?[]Row {
|
2019-08-03 21:26:12 +02:00
|
|
|
res := C.PQexec(db.conn, query.str)
|
2020-11-09 08:22:16 +01:00
|
|
|
return db.handle_error_or_result(res, 'exec')
|
2019-08-03 21:26:12 +02:00
|
|
|
}
|
|
|
|
|
2020-05-10 21:16:03 +02:00
|
|
|
fn rows_first_or_empty(rows []Row) ?Row {
|
2019-08-20 14:34:34 +02:00
|
|
|
if rows.len == 0 {
|
|
|
|
return error('no row')
|
2019-11-23 17:45:35 +01:00
|
|
|
}
|
2019-08-20 14:34:34 +02:00
|
|
|
return rows[0]
|
|
|
|
}
|
2019-11-23 17:45:35 +01:00
|
|
|
|
2020-05-10 21:16:03 +02:00
|
|
|
pub fn (db DB) exec_one(query string) ?Row {
|
2019-08-09 18:10:59 +02:00
|
|
|
res := C.PQexec(db.conn, query.str)
|
2021-03-13 07:06:53 +01:00
|
|
|
e := unsafe { C.PQerrorMessage(db.conn).vstring() }
|
2019-08-09 18:10:59 +02:00
|
|
|
if e != '' {
|
2019-08-20 14:34:34 +02:00
|
|
|
return error('pg exec error: "$e"')
|
2019-08-09 18:10:59 +02:00
|
|
|
}
|
2020-10-03 07:03:11 +02:00
|
|
|
row := rows_first_or_empty(res_to_rows(res)) ?
|
2019-08-20 14:34:34 +02:00
|
|
|
return row
|
2019-08-09 18:10:59 +02:00
|
|
|
}
|
|
|
|
|
2020-11-09 08:22:16 +01:00
|
|
|
// exec_param_many executes a query with the provided parameters
|
|
|
|
pub fn (db DB) exec_param_many(query string, params []string) ?[]Row {
|
2021-03-13 07:06:53 +01:00
|
|
|
mut param_vals := []charptr{len: params.len}
|
2020-11-09 08:22:16 +01:00
|
|
|
for i in 0 .. params.len {
|
|
|
|
param_vals[i] = params[i].str
|
2020-01-03 22:07:28 +01:00
|
|
|
}
|
2020-11-09 08:22:16 +01:00
|
|
|
|
|
|
|
res := C.PQexecParams(db.conn, query.str, params.len, 0, param_vals.data, 0, 0, 0)
|
|
|
|
return db.handle_error_or_result(res, 'exec_param_many')
|
2020-02-26 23:17:56 +01:00
|
|
|
}
|
2020-01-03 22:07:28 +01:00
|
|
|
|
2020-11-09 08:22:16 +01:00
|
|
|
pub fn (db DB) exec_param2(query string, param string, param2 string) ?[]Row {
|
|
|
|
return db.exec_param_many(query, [param, param2])
|
2019-08-03 21:26:12 +02:00
|
|
|
}
|
|
|
|
|
2020-11-09 08:22:16 +01:00
|
|
|
pub fn (db DB) exec_param(query string, param string) ?[]Row {
|
|
|
|
return db.exec_param_many(query, [param])
|
2019-08-03 21:26:12 +02:00
|
|
|
}
|
|
|
|
|
2020-11-09 08:22:16 +01:00
|
|
|
fn (db DB) handle_error_or_result(res voidptr, elabel string) ?[]Row {
|
2021-03-13 07:06:53 +01:00
|
|
|
e := unsafe { C.PQerrorMessage(db.conn).vstring() }
|
2020-01-03 22:07:28 +01:00
|
|
|
if e != '' {
|
2020-11-09 08:22:16 +01:00
|
|
|
C.PQclear(res)
|
|
|
|
return error('pg $elabel error:\n$e')
|
2020-01-03 22:07:28 +01:00
|
|
|
}
|
|
|
|
return res_to_rows(res)
|
|
|
|
}
|
2021-03-13 07:06:53 +01:00
|
|
|
|
|
|
|
// copy_expert execute COPY commands
|
|
|
|
// https://www.postgresql.org/docs/9.5/libpq-copy.html
|
|
|
|
pub fn (db DB) copy_expert(query string, file io.ReaderWriter) ?int {
|
|
|
|
res := C.PQexec(db.conn, query.str)
|
|
|
|
status := C.PQresultStatus(res)
|
|
|
|
|
|
|
|
defer {
|
|
|
|
C.PQclear(res)
|
|
|
|
}
|
|
|
|
|
|
|
|
e := unsafe { C.PQerrorMessage(db.conn).vstring() }
|
|
|
|
if e != '' {
|
|
|
|
return error('pg copy error:\n$e')
|
|
|
|
}
|
|
|
|
|
|
|
|
if status == C.PGRES_COPY_IN {
|
|
|
|
mut buf := []byte{len: 4 * 1024}
|
|
|
|
for {
|
|
|
|
n := file.read(mut buf) or {
|
|
|
|
msg := 'pg copy error: Failed to read from input'
|
|
|
|
C.PQputCopyEnd(db.conn, msg.str)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if n <= 0 {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
|
|
|
|
code := C.PQputCopyData(db.conn, buf.data, n)
|
|
|
|
if code == -1 {
|
|
|
|
return error('pg copy error: Failed to send data, code=$code')
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
code := C.PQputCopyEnd(db.conn, 0)
|
|
|
|
|
|
|
|
if code != 1 {
|
|
|
|
return error('pg copy error: Failed to finish copy command, code: $code')
|
|
|
|
}
|
|
|
|
} else if status == C.PGRES_COPY_OUT {
|
|
|
|
for {
|
|
|
|
address := byteptr(0)
|
|
|
|
n_bytes := C.PQgetCopyData(db.conn, &address, 0)
|
|
|
|
if n_bytes > 0 {
|
|
|
|
mut local_buf := []byte{len: n_bytes}
|
|
|
|
unsafe { C.memcpy(byteptr(local_buf.data), address, n_bytes) }
|
|
|
|
file.write(local_buf) or {
|
|
|
|
C.PQfreemem(address)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
} else if n_bytes == -1 {
|
|
|
|
break
|
|
|
|
} else if n_bytes == -2 {
|
|
|
|
// consult PQerrorMessage for the reason
|
|
|
|
return error('pg copy error: read error')
|
|
|
|
}
|
|
|
|
if address != 0 {
|
|
|
|
C.PQfreemem(address)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0
|
|
|
|
}
|