2019-08-03 22:26:12 +03:00
|
|
|
module pg
|
|
|
|
|
2021-03-13 09:06:53 +03:00
|
|
|
import io
|
|
|
|
|
2019-08-03 22:26:12 +03:00
|
|
|
#flag -lpq
|
|
|
|
#flag linux -I/usr/include/postgresql
|
|
|
|
#flag darwin -I/opt/local/include/postgresql11
|
2021-09-30 20:00:23 +03:00
|
|
|
#flag darwin -I/opt/homebrew/include
|
|
|
|
#flag darwin -L/opt/homebrew/lib
|
2021-04-19 19:01:47 +03:00
|
|
|
#flag windows -I @VEXEROOT/thirdparty/pg/include
|
|
|
|
#flag windows -L @VEXEROOT/thirdparty/pg/win64
|
2021-03-13 09:06:53 +03:00
|
|
|
|
|
|
|
// PostgreSQL Source Code
|
|
|
|
// https://doxygen.postgresql.org/libpq-fe_8h.html
|
2019-08-03 22:26:12 +03:00
|
|
|
#include <libpq-fe.h>
|
2022-12-14 19:26:39 +03:00
|
|
|
|
|
|
|
// for PG_VERSION_NUM, which is defined everywhere at least since PG 9.5
|
|
|
|
#include <pg_config.h>
|
|
|
|
|
2021-04-25 18:57:55 +03:00
|
|
|
// for orm
|
|
|
|
#include <arpa/inet.h>
|
2021-03-13 09:06:53 +03:00
|
|
|
|
2022-12-14 19:26:39 +03:00
|
|
|
#include "@VMODROOT/vlib/pg/compatibility.h"
|
|
|
|
|
2019-11-23 19:45:35 +03:00
|
|
|
pub struct DB {
|
2019-08-03 22:26:12 +03:00
|
|
|
mut:
|
2022-09-15 07:59:31 +03:00
|
|
|
conn &C.PGconn = unsafe { nil }
|
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
|
|
|
|
}
|
|
|
|
|
2019-11-23 19:45:35 +03:00
|
|
|
pub struct Config {
|
2019-08-20 11:08:06 +03:00
|
|
|
pub:
|
2022-11-14 17:25:39 +03:00
|
|
|
host string = 'localhost'
|
|
|
|
port int = 5432
|
2020-10-03 08:03:11 +03:00
|
|
|
user string
|
2019-12-05 22:31:56 +03:00
|
|
|
password string
|
2020-10-03 08:03:11 +03:00
|
|
|
dbname string
|
2019-08-20 11:08:06 +03:00
|
|
|
}
|
|
|
|
|
2022-12-14 14:10:23 +03:00
|
|
|
//
|
|
|
|
|
|
|
|
struct C.pg_result {}
|
|
|
|
|
|
|
|
struct C.pg_conn {}
|
|
|
|
|
|
|
|
[typedef]
|
|
|
|
pub struct C.PGresult {}
|
|
|
|
|
|
|
|
[typedef]
|
|
|
|
pub struct C.PGconn {}
|
|
|
|
|
|
|
|
pub enum ConnStatusType {
|
|
|
|
ok = C.CONNECTION_OK
|
|
|
|
bad = C.CONNECTION_BAD
|
|
|
|
// Non-blocking mode only below here
|
|
|
|
// The existence of these should never be relied upon - they should only be used for user feedback or similar purposes.
|
|
|
|
started = C.CONNECTION_STARTED // Waiting for connection to be made.
|
|
|
|
made = C.CONNECTION_MADE // Connection OK; waiting to send.
|
|
|
|
awaiting_response = C.CONNECTION_AWAITING_RESPONSE // Waiting for a response from the postmaster.
|
|
|
|
auth_ok = C.CONNECTION_AUTH_OK // Received authentication; waiting for backend startup.
|
|
|
|
setenv = C.CONNECTION_SETENV // Negotiating environment.
|
|
|
|
ssl_startup = C.CONNECTION_SSL_STARTUP // Negotiating SSL.
|
2022-12-14 19:26:39 +03:00
|
|
|
needed = C.CONNECTION_NEEDED // Internal state: connect() needed . Available in PG 8
|
|
|
|
check_writable = C.CONNECTION_CHECK_WRITABLE // Check if we could make a writable connection. Available since PG 10
|
|
|
|
consume = C.CONNECTION_CONSUME // Wait for any pending message and consume them. Available since PG 10
|
|
|
|
gss_startup = C.CONNECTION_GSS_STARTUP // Negotiating GSSAPI; available since PG 12
|
2022-12-14 14:10:23 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
[typedef]
|
|
|
|
pub enum ExecStatusType {
|
|
|
|
empty_query = C.PGRES_EMPTY_QUERY // empty query string was executed
|
|
|
|
command_ok = C.PGRES_COMMAND_OK // a query command that doesn't return anything was executed properly by the backend
|
|
|
|
tuples_ok = C.PGRES_TUPLES_OK // a query command that returns tuples was executed properly by the backend, PGresult contains the result tuples
|
|
|
|
copy_out = C.PGRES_COPY_OUT // Copy Out data transfer in progress
|
|
|
|
copy_in = C.PGRES_COPY_IN // Copy In data transfer in progress
|
|
|
|
bad_response = C.PGRES_BAD_RESPONSE // an unexpected response was recv'd from the backend
|
|
|
|
nonfatal_error = C.PGRES_NONFATAL_ERROR // notice or warning message
|
|
|
|
fatal_error = C.PGRES_FATAL_ERROR // query failed
|
|
|
|
copy_both = C.PGRES_COPY_BOTH // Copy In/Out data transfer in progress
|
|
|
|
single_tuple = C.PGRES_SINGLE_TUPLE // single tuple from larger resultset
|
|
|
|
}
|
|
|
|
|
|
|
|
//
|
|
|
|
|
|
|
|
fn C.PQconnectdb(const_conninfo &char) &C.PGconn
|
|
|
|
|
|
|
|
fn C.PQstatus(const_conn &C.PGconn) int
|
2019-08-03 22:26:12 +03:00
|
|
|
|
2022-12-14 14:10:23 +03:00
|
|
|
fn C.PQerrorMessage(const_conn &C.PGconn) &char
|
2020-10-03 08:03:11 +03:00
|
|
|
|
2022-12-14 14:10:23 +03:00
|
|
|
fn C.PQexec(res &C.PGconn, const_query &char) &C.PGresult
|
2020-10-03 08:03:11 +03:00
|
|
|
|
2022-12-14 14:10:23 +03:00
|
|
|
//
|
2020-10-03 08:03:11 +03:00
|
|
|
|
2022-12-14 14:10:23 +03:00
|
|
|
fn C.PQgetvalue(const_res &C.PGresult, int, int) &char
|
2020-11-09 10:22:16 +03:00
|
|
|
|
2022-12-14 14:10:23 +03:00
|
|
|
fn C.PQresultStatus(const_res &C.PGresult) int
|
2020-10-03 08:03:11 +03:00
|
|
|
|
2022-12-14 14:10:23 +03:00
|
|
|
fn C.PQntuples(const_res &C.PGresult) int
|
2020-10-03 08:03:11 +03:00
|
|
|
|
2022-12-14 14:10:23 +03:00
|
|
|
fn C.PQnfields(const_res &C.PGresult) int
|
2021-03-13 09:06:53 +03:00
|
|
|
|
|
|
|
// Params:
|
|
|
|
// const Oid *paramTypes
|
|
|
|
// const char *const *paramValues
|
|
|
|
// const int *paramLengths
|
|
|
|
// const int *paramFormats
|
2022-12-14 14:10:23 +03:00
|
|
|
fn C.PQexecParams(conn &C.PGconn, const_command &char, nParams int, const_paramTypes &int, const_paramValues &char, const_paramLengths &int, const_paramFormats &int, resultFormat int) &C.PGresult
|
2021-03-13 09:06:53 +03:00
|
|
|
|
2022-12-14 14:10:23 +03:00
|
|
|
fn C.PQputCopyData(conn &C.PGconn, const_buffer &char, nbytes int) int
|
2021-03-13 09:06:53 +03:00
|
|
|
|
2022-12-14 14:10:23 +03:00
|
|
|
fn C.PQputCopyEnd(conn &C.PGconn, const_errmsg &char) int
|
2020-10-03 08:03:11 +03:00
|
|
|
|
2022-12-14 14:10:23 +03:00
|
|
|
fn C.PQgetCopyData(conn &C.PGconn, buffer &&char, async int) int
|
2020-10-03 08:03:11 +03:00
|
|
|
|
2022-12-14 14:10:23 +03:00
|
|
|
// cleanup
|
2020-10-03 08:03:11 +03:00
|
|
|
|
2022-12-14 14:10:23 +03:00
|
|
|
fn C.PQclear(res &C.PGresult)
|
2021-03-13 09:06:53 +03:00
|
|
|
|
2022-12-14 14:10:23 +03:00
|
|
|
fn C.PQfreemem(ptr voidptr)
|
|
|
|
|
|
|
|
fn C.PQfinish(conn &C.PGconn)
|
2020-10-03 08:03:11 +03:00
|
|
|
|
2020-10-04 07:31:47 +03:00
|
|
|
// connect makes a new connection to the database server using
|
2020-10-03 08:03:11 +03:00
|
|
|
// the parameters from the `Config` structure, returning
|
|
|
|
// a connection error when something goes wrong
|
2022-10-26 11:26:28 +03:00
|
|
|
pub fn connect(config Config) !DB {
|
2022-11-15 16:53:13 +03:00
|
|
|
conninfo := 'host=${config.host} port=${config.port} user=${config.user} dbname=${config.dbname} password=${config.password}'
|
2022-12-14 14:10:23 +03:00
|
|
|
conn := C.PQconnectdb(&char(conninfo.str))
|
2020-10-03 08:03:11 +03:00
|
|
|
if conn == 0 {
|
|
|
|
return error('libpq memory allocation error')
|
|
|
|
}
|
2022-12-14 14:10:23 +03:00
|
|
|
status := unsafe { ConnStatusType(C.PQstatus(conn)) }
|
|
|
|
if status != .ok {
|
2020-10-03 08:03:11 +03:00
|
|
|
// We force the construction of a new string as the
|
|
|
|
// error message will be freed by the next `PQfinish`
|
|
|
|
// call
|
2021-03-13 09:06:53 +03:00
|
|
|
c_error_msg := unsafe { C.PQerrorMessage(conn).vstring() }
|
2022-11-15 16:53:13 +03:00
|
|
|
error_msg := '${c_error_msg}'
|
2020-10-03 08:03:11 +03:00
|
|
|
C.PQfinish(conn)
|
2022-11-15 16:53:13 +03:00
|
|
|
return error('Connection to a PG database failed: ${error_msg}')
|
2020-10-03 08:03:11 +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-11-09 10:22:16 +03:00
|
|
|
|
2020-05-10 22:16:03 +03:00
|
|
|
mut rows := []Row{}
|
2020-10-03 08:03:11 +03:00
|
|
|
for i in 0 .. nr_rows {
|
2019-08-03 22:26:12 +03:00
|
|
|
mut row := Row{}
|
2020-10-03 08:03:11 +03:00
|
|
|
for j in 0 .. nr_cols {
|
2019-11-23 19:45:35 +03:00
|
|
|
val := C.PQgetvalue(res, i, j)
|
2021-03-13 09:06:53 +03:00
|
|
|
sval := unsafe { val.vstring() }
|
2020-08-10 19:05:26 +03:00
|
|
|
row.vals << sval
|
2019-08-03 22:26:12 +03:00
|
|
|
}
|
|
|
|
rows << row
|
|
|
|
}
|
2020-11-09 10:22:16 +03:00
|
|
|
|
2020-06-26 12:55:59 +03:00
|
|
|
C.PQclear(res)
|
2019-08-03 22:26:12 +03:00
|
|
|
return rows
|
|
|
|
}
|
|
|
|
|
2021-02-15 18:53:38 +03:00
|
|
|
// close frees the underlying resource allocated by the database connection
|
2020-09-21 02:47:37 +03:00
|
|
|
pub fn (db DB) close() {
|
|
|
|
C.PQfinish(db.conn)
|
|
|
|
}
|
|
|
|
|
2020-10-04 07:31:47 +03: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
|
2022-10-26 11:26:28 +03:00
|
|
|
pub fn (db DB) q_int(query string) !int {
|
|
|
|
rows := db.exec(query)!
|
2019-08-03 22:26:12 +03:00
|
|
|
if rows.len == 0 {
|
2022-11-15 16:53:13 +03:00
|
|
|
return error('q_int "${query}" not found')
|
2019-08-03 22:26:12 +03:00
|
|
|
}
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2021-03-13 09:06:53 +03:00
|
|
|
// q_string submit a command to the database server and
|
2020-10-04 07:31:47 +03: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
|
2022-10-26 11:26:28 +03:00
|
|
|
pub fn (db DB) q_string(query string) !string {
|
|
|
|
rows := db.exec(query)!
|
2019-08-03 22:26:12 +03:00
|
|
|
if rows.len == 0 {
|
2022-11-15 16:53:13 +03:00
|
|
|
return error('q_string "${query}" not found')
|
2019-08-03 22:26:12 +03:00
|
|
|
}
|
|
|
|
row := rows[0]
|
|
|
|
if row.vals.len == 0 {
|
|
|
|
return ''
|
|
|
|
}
|
|
|
|
val := row.vals[0]
|
|
|
|
return val
|
|
|
|
}
|
|
|
|
|
2020-10-04 07:31:47 +03:00
|
|
|
// q_strings submit a command to the database server and
|
|
|
|
// returns the resulting row set. Alias of `exec`
|
2022-10-26 11:26:28 +03:00
|
|
|
pub fn (db DB) q_strings(query string) ![]Row {
|
2019-08-03 22:26:12 +03:00
|
|
|
return db.exec(query)
|
|
|
|
}
|
|
|
|
|
2020-10-04 07:31:47 +03: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
|
2022-10-26 11:26:28 +03:00
|
|
|
pub fn (db DB) exec(query string) ![]Row {
|
2022-12-14 14:10:23 +03:00
|
|
|
res := C.PQexec(db.conn, &char(query.str))
|
2020-11-09 10:22:16 +03:00
|
|
|
return db.handle_error_or_result(res, 'exec')
|
2019-08-03 22:26:12 +03:00
|
|
|
}
|
|
|
|
|
2022-10-26 11:26:28 +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
|
|
|
|
2022-10-26 11:26:28 +03:00
|
|
|
pub fn (db DB) exec_one(query string) !Row {
|
2022-12-14 14:10:23 +03:00
|
|
|
res := C.PQexec(db.conn, &char(query.str))
|
2021-03-13 09:06:53 +03:00
|
|
|
e := unsafe { C.PQerrorMessage(db.conn).vstring() }
|
2019-08-09 19:10:59 +03:00
|
|
|
if e != '' {
|
2022-11-15 16:53:13 +03:00
|
|
|
return error('pg exec error: "${e}"')
|
2019-08-09 19:10:59 +03:00
|
|
|
}
|
2022-10-26 11:26:28 +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-11-09 10:22:16 +03:00
|
|
|
// exec_param_many executes a query with the provided parameters
|
2022-10-26 11:26:28 +03:00
|
|
|
pub fn (db DB) exec_param_many(query string, params []string) ![]Row {
|
2022-07-06 10:43:36 +03:00
|
|
|
unsafe {
|
|
|
|
mut param_vals := []&char{len: params.len}
|
|
|
|
for i in 0 .. params.len {
|
2022-12-14 14:10:23 +03:00
|
|
|
param_vals[i] = &char(params[i].str)
|
2022-07-06 10:43:36 +03:00
|
|
|
}
|
2020-11-09 10:22:16 +03:00
|
|
|
|
2022-12-14 14:10:23 +03:00
|
|
|
res := C.PQexecParams(db.conn, &char(query.str), params.len, 0, param_vals.data,
|
|
|
|
0, 0, 0)
|
2022-07-06 10:43:36 +03:00
|
|
|
return db.handle_error_or_result(res, 'exec_param_many')
|
|
|
|
}
|
2020-02-27 01:17:56 +03:00
|
|
|
}
|
2020-01-04 00:07:28 +03:00
|
|
|
|
2022-10-26 11:26:28 +03:00
|
|
|
pub fn (db DB) exec_param2(query string, param string, param2 string) ![]Row {
|
2020-11-09 10:22:16 +03:00
|
|
|
return db.exec_param_many(query, [param, param2])
|
2019-08-03 22:26:12 +03:00
|
|
|
}
|
|
|
|
|
2022-10-26 11:26:28 +03:00
|
|
|
pub fn (db DB) exec_param(query string, param string) ![]Row {
|
2020-11-09 10:22:16 +03:00
|
|
|
return db.exec_param_many(query, [param])
|
2019-08-03 22:26:12 +03:00
|
|
|
}
|
|
|
|
|
2022-10-26 11:26:28 +03:00
|
|
|
fn (db DB) handle_error_or_result(res voidptr, elabel string) ![]Row {
|
2021-03-13 09:06:53 +03:00
|
|
|
e := unsafe { C.PQerrorMessage(db.conn).vstring() }
|
2020-01-04 00:07:28 +03:00
|
|
|
if e != '' {
|
2020-11-09 10:22:16 +03:00
|
|
|
C.PQclear(res)
|
2022-11-15 16:53:13 +03:00
|
|
|
return error('pg ${elabel} error:\n${e}')
|
2020-01-04 00:07:28 +03:00
|
|
|
}
|
|
|
|
return res_to_rows(res)
|
|
|
|
}
|
2021-03-13 09:06:53 +03:00
|
|
|
|
|
|
|
// copy_expert execute COPY commands
|
|
|
|
// https://www.postgresql.org/docs/9.5/libpq-copy.html
|
2022-10-26 11:26:28 +03:00
|
|
|
pub fn (db DB) copy_expert(query string, mut file io.ReaderWriter) !int {
|
2022-12-14 14:10:23 +03:00
|
|
|
mut res := C.PQexec(db.conn, &char(query.str))
|
|
|
|
status := unsafe { ExecStatusType(C.PQresultStatus(res)) }
|
2021-03-13 09:06:53 +03:00
|
|
|
defer {
|
|
|
|
C.PQclear(res)
|
|
|
|
}
|
|
|
|
|
|
|
|
e := unsafe { C.PQerrorMessage(db.conn).vstring() }
|
|
|
|
if e != '' {
|
2022-11-15 16:53:13 +03:00
|
|
|
return error('pg copy error:\n${e}')
|
2021-03-13 09:06:53 +03:00
|
|
|
}
|
|
|
|
|
2022-12-14 14:10:23 +03:00
|
|
|
if status == .copy_in {
|
2022-04-15 15:35:35 +03:00
|
|
|
mut buf := []u8{len: 4 * 1024}
|
2021-03-13 09:06:53 +03:00
|
|
|
for {
|
|
|
|
n := file.read(mut buf) or {
|
|
|
|
msg := 'pg copy error: Failed to read from input'
|
2022-12-14 14:10:23 +03:00
|
|
|
C.PQputCopyEnd(db.conn, &char(msg.str))
|
2021-03-13 09:06:53 +03:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
if n <= 0 {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
|
|
|
|
code := C.PQputCopyData(db.conn, buf.data, n)
|
|
|
|
if code == -1 {
|
2022-11-15 16:53:13 +03:00
|
|
|
return error('pg copy error: Failed to send data, code=${code}')
|
2021-03-13 09:06:53 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-12-14 14:10:23 +03:00
|
|
|
code := C.PQputCopyEnd(db.conn, &char(0))
|
2021-03-13 09:06:53 +03:00
|
|
|
|
|
|
|
if code != 1 {
|
2022-11-15 16:53:13 +03:00
|
|
|
return error('pg copy error: Failed to finish copy command, code: ${code}')
|
2021-03-13 09:06:53 +03:00
|
|
|
}
|
2022-12-14 14:10:23 +03:00
|
|
|
} else if status == .copy_out {
|
2021-03-13 09:06:53 +03:00
|
|
|
for {
|
2022-12-14 14:10:23 +03:00
|
|
|
address := &char(0)
|
2021-03-13 09:06:53 +03:00
|
|
|
n_bytes := C.PQgetCopyData(db.conn, &address, 0)
|
|
|
|
if n_bytes > 0 {
|
2022-04-15 15:35:35 +03:00
|
|
|
mut local_buf := []u8{len: n_bytes}
|
2022-04-15 14:58:56 +03:00
|
|
|
unsafe { C.memcpy(&u8(local_buf.data), address, n_bytes) }
|
2021-03-13 09:06:53 +03:00
|
|
|
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
|
|
|
|
}
|