pg: minor fixes

pull/2856/head
Alexander Medvednikov 2019-11-23 19:45:35 +03:00
parent e724792a67
commit 0fb0c43c0a
4 changed files with 28 additions and 25 deletions

View File

@ -41,5 +41,6 @@
+ fast.vlang.io
+ bare metal support
+ inline assembly
+ x64 machine code generation (ELF)

View File

@ -1625,7 +1625,9 @@ fn (p mut Parser) var_expr(v Var) string {
p.next()
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.insert_query(fn_ph)
return 'void'

View File

@ -5,19 +5,19 @@ module pg
#flag darwin -I/opt/local/include/postgresql11
#include <libpq-fe.h>
struct DB {
pub struct DB {
mut:
conn &C.PGconn
}
struct Row {
pub struct Row {
pub mut:
vals []string
}
struct C.PGResult { }
struct Config {
pub struct Config {
pub:
host string
user string
@ -26,30 +26,30 @@ pub:
}
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.PQstatus(voidptr) int
fn C.PQstatus(voidptr) int
pub fn connect(config pg.Config) DB {
conninfo := 'host=$config.host user=$config.user dbname=$config.dbname'
conn:=C.PQconnectdb(conninfo.str)
status := C.PQstatus(conn)
if status != C.CONNECTION_OK {
error_msg := C.PQerrorMessage(conn)
eprintln('Connection to a PG database failed: ' + string(error_msg))
exit(1)
if status != C.CONNECTION_OK {
error_msg := C.PQerrorMessage(conn)
eprintln('Connection to a PG database failed: ' + string(error_msg))
exit(1)
}
return DB {conn: conn}
return DB {conn: conn}
}
fn res_to_rows(res voidptr) []pg.Row {
nr_rows := C.PQntuples(res)
nr_cols := C.PQnfields(res)
nr_rows := C.PQntuples(res)
nr_cols := C.PQnfields(res)
mut rows := []pg.Row
for i := 0; i < nr_rows; i++ {
mut row := Row{}
for j := 0; j < nr_cols; j++ {
val := C.PQgetvalue(res, i, j)
val := C.PQgetvalue(res, i, j)
row.vals << string(val)
}
rows << row
@ -68,7 +68,7 @@ pub fn (db DB) q_int(query string) int {
return 0
}
val := row.vals[0]
return val.int()
return val.int()
}
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 {
if rows.len == 0 {
return error('no row')
}
}
return rows[0]
}
pub fn (db DB) exec_one(query string) ?pg.Row {
res := C.PQexec(db.conn, query.str)
e := string(C.PQerrorMessage(db.conn))
@ -117,12 +117,12 @@ pub fn (db DB) exec_one(query string) ?pg.Row {
return row
}
//
//
pub fn (db DB) exec_param2(query string, param, param2 string) []pg.Row {
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)
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)
e := string(C.PQerrorMessage(db.conn))
if e != '' {
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 {
mut param_vals := [1]byteptr
param_vals[0] = param.str
res := C.PQexecParams(db.conn, query.str, 1, 0, param_vals, 0, 0, 0)
mut param_vals := [1]byteptr
param_vals[0] = param.str
res := C.PQexecParams(db.conn, query.str, 1, 0, param_vals, 0, 0, 0)
return res_to_rows(res)
}