pg: minor fixes
parent
e724792a67
commit
0fb0c43c0a
|
@ -41,5 +41,6 @@
|
||||||
+ fast.vlang.io
|
+ fast.vlang.io
|
||||||
+ bare metal support
|
+ bare metal support
|
||||||
+ inline assembly
|
+ inline assembly
|
||||||
|
+ x64 machine code generation (ELF)
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -1625,7 +1625,9 @@ fn (p mut Parser) var_expr(v Var) string {
|
||||||
p.next()
|
p.next()
|
||||||
return p.select_query(fn_ph)
|
return p.select_query(fn_ph)
|
||||||
}
|
}
|
||||||
if typ == 'pg__DB' && !p.fileis('pg.v') && p.peek() == .name {
|
if typ == 'pg__DB' && !p.fileis('pg.v') && p.peek() == .name &&
|
||||||
|
!p.tokens[p.token_idx].lit.contains('exec')
|
||||||
|
{
|
||||||
p.next()
|
p.next()
|
||||||
p.insert_query(fn_ph)
|
p.insert_query(fn_ph)
|
||||||
return 'void'
|
return 'void'
|
||||||
|
|
48
vlib/pg/pg.v
48
vlib/pg/pg.v
|
@ -5,19 +5,19 @@ module pg
|
||||||
#flag darwin -I/opt/local/include/postgresql11
|
#flag darwin -I/opt/local/include/postgresql11
|
||||||
#include <libpq-fe.h>
|
#include <libpq-fe.h>
|
||||||
|
|
||||||
struct DB {
|
pub struct DB {
|
||||||
mut:
|
mut:
|
||||||
conn &C.PGconn
|
conn &C.PGconn
|
||||||
}
|
}
|
||||||
|
|
||||||
struct Row {
|
pub struct Row {
|
||||||
pub mut:
|
pub mut:
|
||||||
vals []string
|
vals []string
|
||||||
}
|
}
|
||||||
|
|
||||||
struct C.PGResult { }
|
struct C.PGResult { }
|
||||||
|
|
||||||
struct Config {
|
pub struct Config {
|
||||||
pub:
|
pub:
|
||||||
host string
|
host string
|
||||||
user string
|
user string
|
||||||
|
@ -26,30 +26,30 @@ pub:
|
||||||
}
|
}
|
||||||
|
|
||||||
fn C.PQconnectdb(a byteptr) &C.PGconn
|
fn C.PQconnectdb(a byteptr) &C.PGconn
|
||||||
fn C.PQerrorMessage(voidptr) byteptr
|
fn C.PQerrorMessage(voidptr) byteptr
|
||||||
fn C.PQgetvalue(voidptr, int, int) byteptr
|
fn C.PQgetvalue(voidptr, int, int) byteptr
|
||||||
fn C.PQstatus(voidptr) int
|
fn C.PQstatus(voidptr) int
|
||||||
|
|
||||||
pub fn connect(config pg.Config) DB {
|
pub fn connect(config pg.Config) DB {
|
||||||
conninfo := 'host=$config.host user=$config.user dbname=$config.dbname'
|
conninfo := 'host=$config.host user=$config.user dbname=$config.dbname'
|
||||||
conn:=C.PQconnectdb(conninfo.str)
|
conn:=C.PQconnectdb(conninfo.str)
|
||||||
status := C.PQstatus(conn)
|
status := C.PQstatus(conn)
|
||||||
if status != C.CONNECTION_OK {
|
if status != C.CONNECTION_OK {
|
||||||
error_msg := C.PQerrorMessage(conn)
|
error_msg := C.PQerrorMessage(conn)
|
||||||
eprintln('Connection to a PG database failed: ' + string(error_msg))
|
eprintln('Connection to a PG database failed: ' + string(error_msg))
|
||||||
exit(1)
|
exit(1)
|
||||||
}
|
}
|
||||||
return DB {conn: conn}
|
return DB {conn: conn}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn res_to_rows(res voidptr) []pg.Row {
|
fn res_to_rows(res voidptr) []pg.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 := []pg.Row
|
||||||
for i := 0; i < nr_rows; i++ {
|
for i := 0; i < nr_rows; i++ {
|
||||||
mut row := Row{}
|
mut row := Row{}
|
||||||
for j := 0; j < nr_cols; j++ {
|
for j := 0; j < nr_cols; j++ {
|
||||||
val := C.PQgetvalue(res, i, j)
|
val := C.PQgetvalue(res, i, j)
|
||||||
row.vals << string(val)
|
row.vals << string(val)
|
||||||
}
|
}
|
||||||
rows << row
|
rows << row
|
||||||
|
@ -68,7 +68,7 @@ pub fn (db DB) q_int(query string) int {
|
||||||
return 0
|
return 0
|
||||||
}
|
}
|
||||||
val := row.vals[0]
|
val := row.vals[0]
|
||||||
return val.int()
|
return val.int()
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn (db DB) q_string(query string) string {
|
pub fn (db DB) q_string(query string) string {
|
||||||
|
@ -103,10 +103,10 @@ pub fn (db DB) exec(query string) []pg.Row {
|
||||||
fn rows_first_or_empty(rows []pg.Row) ?pg.Row {
|
fn rows_first_or_empty(rows []pg.Row) ?pg.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) ?pg.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))
|
||||||
|
@ -117,12 +117,12 @@ pub fn (db DB) exec_one(query string) ?pg.Row {
|
||||||
return row
|
return 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) []pg.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
|
||||||
res := C.PQexecParams(db.conn, query.str, 2, 0, param_vals, 0, 0, 0)
|
res := C.PQexecParams(db.conn, query.str, 2, 0, param_vals, 0, 0, 0)
|
||||||
e := string(C.PQerrorMessage(db.conn))
|
e := string(C.PQerrorMessage(db.conn))
|
||||||
if e != '' {
|
if e != '' {
|
||||||
println('pg exec2 error:')
|
println('pg exec2 error:')
|
||||||
|
@ -133,9 +133,9 @@ pub fn (db DB) exec_param2(query string, param, param2 string) []pg.Row {
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn (db DB) exec_param(query string, param string) []pg.Row {
|
pub fn (db DB) exec_param(query string, param string) []pg.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 res_to_rows(res)
|
return res_to_rows(res)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue