1
0
mirror of https://github.com/vlang/v.git synced 2023-08-10 21:13:21 +03:00
This commit is contained in:
Alexander Medvednikov
2019-08-09 18:10:59 +02:00
parent c67783bcd1
commit 8f8e0dfad7
8 changed files with 256 additions and 44 deletions

View File

@@ -30,7 +30,8 @@ fn C.PQgetvalue(voidptr, int, int) byteptr
fn C.PQstatus(voidptr) int
pub fn connect(dbname, user string) DB {
conninfo := 'host=localhost user=$user dbname=$dbname'
//conninfo := 'host=localhost user=$user dbname=$dbname'
conninfo := 'host=127.0.0.1 user=$user dbname=$dbname'
conn:=C.PQconnectdb(conninfo.str)
status := C.PQstatus(conn)
if status != CONNECTION_OK {
@@ -99,6 +100,21 @@ pub fn (db DB) exec(query string) []pg.Row {
return res_to_rows(res)
}
pub fn (db DB) exec_one(query string) pg.Row {
res := C.PQexec(db.conn, query.str)
e := string(C.PQerrorMessage(db.conn))
if e != '' {
println('pg exec error:')
println(e)
return Row{}
}
rows := res_to_rows(res)
if rows.len == 0 {
return Row{}
}
return rows[0]
}
//
pub fn (db DB) exec_param2(query string, param, param2 string) []pg.Row {
@@ -122,3 +138,4 @@ pub fn (db DB) exec_param(query string, param string) []pg.Row {
return res_to_rows(res)
}