1
0
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:
Alexander Medvednikov 2020-03-26 09:23:54 +01:00
parent cd6d175180
commit ec4be80bcc
17 changed files with 37 additions and 34 deletions

View File

@ -1,4 +1,4 @@
// import db.mysql
// import mysql
// pub fn main() {
// conn := mysql.connect('localhost', 'root', '', 'mysql')
@ -11,5 +11,5 @@
// }
fn main() {
}
}

View File

@ -1,6 +1,6 @@
module main
import db.pg
import pg
struct Customer {
id int
@ -58,3 +58,5 @@ fn main() {
}
db.insert(nc)
}

View File

@ -1,22 +1,22 @@
import db.sqlite
import sqlite
fn main() {
db := sqlite.connect(':memory:')
db.exec("create table users (id integer primary key, name text default '');")
db.exec("insert into users (name) values ('Sam')")
db.exec("insert into users (name) values ('Peter')")
db.exec("insert into users (name) values ('Kate')")
nr_users := db.q_int('select count(*) from users')
println('nr users = $nr_users')
name := db.q_string('select name from users where id = 1')
assert name == 'Sam'
users, code := db.exec('select * from users')
println("SQL Result code: $code")
for row in users {
println(row.vals)
}
}
}
}

View File

@ -2036,7 +2036,7 @@ fn (p mut Parser) var_expr(v Var) string {
p.next()
return p.select_query(fn_ph)
}
if typ == 'db_dot_pg__DB' && !p.fileis('pg.v') && p.peek() == .name {
if typ == 'pg__DB' && !p.fileis('pg.v') && p.peek() == .name {
name := p.tokens[p.token_idx].lit
if !name.contains('exec') && !name.starts_with('q_') {
p.next()

View File

@ -134,7 +134,7 @@ fn (p mut Parser) select_query(fn_ph int) string {
println('sql query="$q"')
p.cgen.insert_before('// DEBUG_SQL prefix: $qprefix | fn_ph: $fn_ph | query: "$q" ')
if n == 'count' {
p.cgen.set_placeholder(fn_ph, 'db_dot_pg__DB_q_int(')
p.cgen.set_placeholder(fn_ph, 'pg__DB_q_int(')
p.gen(', tos2("$q"))')
}
else {
@ -161,13 +161,13 @@ $params_gen
Option_${table_name} opt_${qprefix}$tmp;
void* ${qprefix}res = PQexecParams(db.conn, "$q", $p.sql_i, 0, ${qprefix}params, 0, 0, 0) ;
array_db_dot_pg__Row ${qprefix}rows = db_dot_pg__res_to_rows ( ${qprefix}res ) ;
Option_db_dot_pg__Row opt_${qprefix}row = db_dot_pg__rows_first_or_empty( ${qprefix}rows );
array_pg__Row ${qprefix}rows = pg__res_to_rows ( ${qprefix}res ) ;
Option_pg__Row opt_${qprefix}row = pg__rows_first_or_empty( ${qprefix}rows );
if (! opt_${qprefix}row . ok ) {
opt_${qprefix}$tmp = v_error( opt_${qprefix}row . error );
}else{
$table_name ${qprefix}$tmp;
db_dot_pg__Row ${qprefix}row = *(db_dot_pg__Row*) opt_${qprefix}row . data;
pg__Row ${qprefix}row = *(pg__Row*) opt_${qprefix}row . data;
${obj_gen.str()}
opt_${qprefix}$tmp = opt_ok( & ${qprefix}$tmp, sizeof($table_name) );
}
@ -183,12 +183,12 @@ ${obj_gen.str()}
$params_gen
void* ${qprefix}res = PQexecParams(db.conn, "$q", $p.sql_i, 0, ${qprefix}params, 0, 0, 0) ;
array_db_dot_pg__Row ${qprefix}rows = db_dot_pg__res_to_rows(${qprefix}res);
array_pg__Row ${qprefix}rows = pg__res_to_rows(${qprefix}res);
// TODO preallocate
array ${qprefix}arr_$tmp = new_array(0, 0, sizeof($table_name));
for (int i = 0; i < ${qprefix}rows.len; i++) {
db_dot_pg__Row ${qprefix}row = *(db_dot_pg__Row*)array_get(${qprefix}rows, i);
pg__Row ${qprefix}row = *(pg__Row*)array_get(${qprefix}rows, i);
$table_name ${qprefix}$tmp;
${obj_gen.str()}
_PUSH(&${qprefix}arr_$tmp, ${qprefix}$tmp, ${tmp}2, $table_name);
@ -337,3 +337,4 @@ fn (p mut Parser) update_query(fn_ph int) {
println('update q="$q"')
p.genln('.conn, "$q", $nr_vals, 0, params, 0, 0, 0)')
}

View File

@ -35,7 +35,7 @@ fn C.PQnfields(voidptr) int
fn C.PQexec(voidptr) voidptr
fn C.PQexecParams(voidptr) voidptr
pub fn connect(config Config) ?DB {
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)
@ -47,10 +47,10 @@ pub fn connect(config Config) ?DB {
return DB {conn: conn}
}
fn res_to_rows(res voidptr) []Row {
fn res_to_rows(res voidptr) []pg.Row {
nr_rows := C.PQntuples(res)
nr_cols := C.PQnfields(res)
mut rows := []Row
mut rows := []pg.Row
for i in 0..nr_rows {
mut row := Row{}
for j in 0..nr_cols {
@ -90,11 +90,11 @@ pub fn (db DB) q_string(query string) string {
return val
}
pub fn (db DB) q_strings(query string) []Row {
pub fn (db DB) q_strings(query string) []pg.Row {
return db.exec(query)
}
pub fn (db DB) exec(query string) []Row {
pub fn (db DB) exec(query string) []pg.Row {
res := C.PQexec(db.conn, query.str)
e := string(C.PQerrorMessage(db.conn))
if e != '' {
@ -105,14 +105,14 @@ pub fn (db DB) exec(query string) []Row {
return res_to_rows(res)
}
fn rows_first_or_empty(rows []Row) ?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) ?Row {
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 != '' {
@ -124,7 +124,7 @@ pub fn (db DB) exec_one(query string) ?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) []Row {
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 {
@ -136,7 +136,7 @@ pub fn (db DB) exec_param_many(query string, params []string) []Row {
}
}
pub fn (db DB) exec_param2(query string, param, param2 string) []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
@ -144,14 +144,14 @@ pub fn (db DB) exec_param2(query string, param, param2 string) []Row {
return db.handle_error_or_result(res, 'exec_param2')
}
pub fn (db DB) exec_param(query string, param string) []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)
return db.handle_error_or_result(res, 'exec_param')
}
fn (db DB) handle_error_or_result(res voidptr, elabel string) []Row {
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:')

View File

@ -1,20 +1,20 @@
import db.sqlite
import sqlite
fn test_sqlite() {
db := sqlite.connect(':memory:')
db.exec("drop table if exists users")
db.exec("create table users (id integer primary key, name text default '');")
db.exec("insert into users (name) values ('Sam')")
db.exec("insert into users (name) values ('Peter')")
db.exec("insert into users (name) values ('Kate')")
nr_users := db.q_int('select count(*) from users')
assert nr_users == 3
name := db.q_string('select name from users where id = 1')
assert name == 'Sam'
users, mut code := db.exec('select * from users')
assert users.len == 3
assert code == 101