mirror of
https://github.com/vlang/v.git
synced 2023-08-10 21:13:21 +03:00
Revert "db: increase db module level in vlib "
This reverts commit cd6d175180
.
This commit is contained in:
162
vlib/pg/pg.v
Normal file
162
vlib/pg/pg.v
Normal file
@ -0,0 +1,162 @@
|
||||
module pg
|
||||
|
||||
#flag -lpq
|
||||
#flag linux -I/usr/include/postgresql
|
||||
#flag darwin -I/opt/local/include/postgresql11
|
||||
#include <libpq-fe.h>
|
||||
|
||||
pub struct DB {
|
||||
mut:
|
||||
conn &C.PGconn
|
||||
}
|
||||
|
||||
pub struct Row {
|
||||
pub mut:
|
||||
vals []string
|
||||
}
|
||||
|
||||
struct C.PGResult { }
|
||||
|
||||
pub struct Config {
|
||||
pub:
|
||||
host string
|
||||
port int = 5432
|
||||
user string
|
||||
password string
|
||||
dbname string
|
||||
}
|
||||
|
||||
fn C.PQconnectdb(a byteptr) &C.PGconn
|
||||
fn C.PQerrorMessage(voidptr) byteptr
|
||||
fn C.PQgetvalue(voidptr, int, int) byteptr
|
||||
fn C.PQstatus(voidptr) int
|
||||
fn C.PQntuples(voidptr) int
|
||||
fn C.PQnfields(voidptr) int
|
||||
fn C.PQexec(voidptr) voidptr
|
||||
fn C.PQexecParams(voidptr) voidptr
|
||||
|
||||
pub fn connect(config pg.Config) ?DB {
|
||||
conninfo := 'host=$config.host port=$config.port user=$config.user dbname=$config.dbname password=$config.password'
|
||||
conn := C.PQconnectdb(conninfo.str)
|
||||
status := C.PQstatus(conn)
|
||||
println("status=$status")
|
||||
if status != C.CONNECTION_OK {
|
||||
error_msg := C.PQerrorMessage(conn)
|
||||
return error ('Connection to a PG database failed: ' + string(error_msg))
|
||||
}
|
||||
return DB {conn: conn}
|
||||
}
|
||||
|
||||
fn res_to_rows(res voidptr) []pg.Row {
|
||||
nr_rows := C.PQntuples(res)
|
||||
nr_cols := C.PQnfields(res)
|
||||
mut rows := []pg.Row
|
||||
for i in 0..nr_rows {
|
||||
mut row := Row{}
|
||||
for j in 0..nr_cols {
|
||||
val := C.PQgetvalue(res, i, j)
|
||||
row.vals << string(val)
|
||||
}
|
||||
rows << row
|
||||
}
|
||||
return rows
|
||||
}
|
||||
|
||||
pub fn (db DB) q_int(query string) int {
|
||||
rows := db.exec(query)
|
||||
if rows.len == 0 {
|
||||
println('q_int "$query" not found')
|
||||
return 0
|
||||
}
|
||||
row := rows[0]
|
||||
if row.vals.len == 0 {
|
||||
return 0
|
||||
}
|
||||
val := row.vals[0]
|
||||
return val.int()
|
||||
}
|
||||
|
||||
pub fn (db DB) q_string(query string) string {
|
||||
rows := db.exec(query)
|
||||
if rows.len == 0 {
|
||||
println('q_string "$query" not found')
|
||||
return ''
|
||||
}
|
||||
row := rows[0]
|
||||
if row.vals.len == 0 {
|
||||
return ''
|
||||
}
|
||||
val := row.vals[0]
|
||||
return val
|
||||
}
|
||||
|
||||
pub fn (db DB) q_strings(query string) []pg.Row {
|
||||
return db.exec(query)
|
||||
}
|
||||
|
||||
pub fn (db DB) exec(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 res_to_rows(res)
|
||||
}
|
||||
return res_to_rows(res)
|
||||
}
|
||||
|
||||
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))
|
||||
if e != '' {
|
||||
return error('pg exec error: "$e"')
|
||||
}
|
||||
row := rows_first_or_empty( res_to_rows(res) )
|
||||
return row
|
||||
}
|
||||
|
||||
// The entire function can be considered unsafe because of the malloc and the
|
||||
// free. This prevents warnings and doesn't seem to affect behavior.
|
||||
pub fn (db DB) exec_param_many(query string, params []string) []pg.Row {
|
||||
unsafe {
|
||||
mut param_vals := &byteptr( malloc( params.len * sizeof(byteptr) ) )
|
||||
for i in 0..params.len {
|
||||
param_vals[i] = params[i].str
|
||||
}
|
||||
res := C.PQexecParams(db.conn, query.str, params.len, 0, param_vals, 0, 0, 0)
|
||||
free(param_vals)
|
||||
return db.handle_error_or_result(res, 'exec_param_many')
|
||||
}
|
||||
}
|
||||
|
||||
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)
|
||||
return db.handle_error_or_result(res, 'exec_param2')
|
||||
}
|
||||
|
||||
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)
|
||||
return db.handle_error_or_result(res, 'exec_param')
|
||||
}
|
||||
|
||||
fn (db DB) handle_error_or_result(res voidptr, elabel string) []pg.Row {
|
||||
e := string(C.PQerrorMessage(db.conn))
|
||||
if e != '' {
|
||||
println('pg $elabel error:')
|
||||
println(e)
|
||||
return res_to_rows(res)
|
||||
}
|
||||
return res_to_rows(res)
|
||||
}
|
25
vlib/pg/readme.md
Normal file
25
vlib/pg/readme.md
Normal file
@ -0,0 +1,25 @@
|
||||
Before you can use this module, you must first have PostgreSQL installed on
|
||||
your system. To do this, find your OS and perform the actions listed.
|
||||
|
||||
**NOTE**: These instructions are meant only as a convenience. If your OS is not
|
||||
listed or you need extra help, [go here](https://www.postgresql.org/download/).
|
||||
|
||||
### Fedora 31
|
||||
```
|
||||
sudo dnf install postgresql-server postgresql-contrib
|
||||
sudo systemctl enable postgresql # to autostart on startup
|
||||
sudo systemctl start postgresql
|
||||
```
|
||||
|
||||
### Debian 10/11
|
||||
```
|
||||
sudo apt-get install postgresql postgresql-client
|
||||
sudo systemctl enable postgresql # to autostart on startup
|
||||
sudo systemctl start postgresql
|
||||
```
|
||||
|
||||
### MacOSX (Homebrew)
|
||||
```
|
||||
brew install postgresql
|
||||
brew services start postgresql
|
||||
```
|
Reference in New Issue
Block a user