From ec4be80bcc0a727f24dc1b80d70a36e675696881 Mon Sep 17 00:00:00 2001 From: Alexander Medvednikov Date: Thu, 26 Mar 2020 09:23:54 +0100 Subject: [PATCH] Revert "db: increase db module level in vlib " This reverts commit cd6d17518096a53cc8dbcba276117ed47d95ea41. --- examples/database/mysql.v | 6 +++--- examples/database/pg/customer.v | 4 +++- examples/database/sqlite.v | 14 +++++++------- vlib/compiler/aparser.v | 2 +- vlib/compiler/query.v | 13 +++++++------ vlib/{db => }/mysql/_cdefs.v | 0 vlib/{db => }/mysql/consts.v | 0 vlib/{db => }/mysql/enums.v | 0 vlib/{db => }/mysql/mysql.v | 0 vlib/{db => }/mysql/result.v | 0 vlib/{db => }/mysql/utils.v | 0 vlib/{db => }/orm/orm_test.v | 0 vlib/{db => }/pg/pg.v | 22 +++++++++++----------- vlib/{db => }/pg/readme.md | 0 vlib/{db => }/sqlite/readme.md | 0 vlib/{db => }/sqlite/sqlite.v | 0 vlib/{db => }/sqlite/sqlite_test.v | 10 +++++----- 17 files changed, 37 insertions(+), 34 deletions(-) rename vlib/{db => }/mysql/_cdefs.v (100%) rename vlib/{db => }/mysql/consts.v (100%) rename vlib/{db => }/mysql/enums.v (100%) rename vlib/{db => }/mysql/mysql.v (100%) rename vlib/{db => }/mysql/result.v (100%) rename vlib/{db => }/mysql/utils.v (100%) rename vlib/{db => }/orm/orm_test.v (100%) rename vlib/{db => }/pg/pg.v (89%) rename vlib/{db => }/pg/readme.md (100%) rename vlib/{db => }/sqlite/readme.md (100%) rename vlib/{db => }/sqlite/sqlite.v (100%) rename vlib/{db => }/sqlite/sqlite_test.v (97%) diff --git a/examples/database/mysql.v b/examples/database/mysql.v index 4609294b5e..bb1d7ce84b 100644 --- a/examples/database/mysql.v +++ b/examples/database/mysql.v @@ -1,4 +1,4 @@ -// import db.mysql +// import mysql // pub fn main() { // conn := mysql.connect('localhost', 'root', '', 'mysql') @@ -11,5 +11,5 @@ // } fn main() { - -} + +} diff --git a/examples/database/pg/customer.v b/examples/database/pg/customer.v index 1360820c60..05dd074697 100644 --- a/examples/database/pg/customer.v +++ b/examples/database/pg/customer.v @@ -1,6 +1,6 @@ module main -import db.pg +import pg struct Customer { id int @@ -58,3 +58,5 @@ fn main() { } db.insert(nc) } + + diff --git a/examples/database/sqlite.v b/examples/database/sqlite.v index 6288ee947e..311a0c064b 100644 --- a/examples/database/sqlite.v +++ b/examples/database/sqlite.v @@ -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) - } -} + } +} diff --git a/vlib/compiler/aparser.v b/vlib/compiler/aparser.v index 27b823941a..4dd6428210 100644 --- a/vlib/compiler/aparser.v +++ b/vlib/compiler/aparser.v @@ -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() diff --git a/vlib/compiler/query.v b/vlib/compiler/query.v index 1878f6d827..ceec14fa26 100644 --- a/vlib/compiler/query.v +++ b/vlib/compiler/query.v @@ -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)') } + diff --git a/vlib/db/mysql/_cdefs.v b/vlib/mysql/_cdefs.v similarity index 100% rename from vlib/db/mysql/_cdefs.v rename to vlib/mysql/_cdefs.v diff --git a/vlib/db/mysql/consts.v b/vlib/mysql/consts.v similarity index 100% rename from vlib/db/mysql/consts.v rename to vlib/mysql/consts.v diff --git a/vlib/db/mysql/enums.v b/vlib/mysql/enums.v similarity index 100% rename from vlib/db/mysql/enums.v rename to vlib/mysql/enums.v diff --git a/vlib/db/mysql/mysql.v b/vlib/mysql/mysql.v similarity index 100% rename from vlib/db/mysql/mysql.v rename to vlib/mysql/mysql.v diff --git a/vlib/db/mysql/result.v b/vlib/mysql/result.v similarity index 100% rename from vlib/db/mysql/result.v rename to vlib/mysql/result.v diff --git a/vlib/db/mysql/utils.v b/vlib/mysql/utils.v similarity index 100% rename from vlib/db/mysql/utils.v rename to vlib/mysql/utils.v diff --git a/vlib/db/orm/orm_test.v b/vlib/orm/orm_test.v similarity index 100% rename from vlib/db/orm/orm_test.v rename to vlib/orm/orm_test.v diff --git a/vlib/db/pg/pg.v b/vlib/pg/pg.v similarity index 89% rename from vlib/db/pg/pg.v rename to vlib/pg/pg.v index f3ec0ccd4b..0372ec3cce 100644 --- a/vlib/db/pg/pg.v +++ b/vlib/pg/pg.v @@ -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:') diff --git a/vlib/db/pg/readme.md b/vlib/pg/readme.md similarity index 100% rename from vlib/db/pg/readme.md rename to vlib/pg/readme.md diff --git a/vlib/db/sqlite/readme.md b/vlib/sqlite/readme.md similarity index 100% rename from vlib/db/sqlite/readme.md rename to vlib/sqlite/readme.md diff --git a/vlib/db/sqlite/sqlite.v b/vlib/sqlite/sqlite.v similarity index 100% rename from vlib/db/sqlite/sqlite.v rename to vlib/sqlite/sqlite.v diff --git a/vlib/db/sqlite/sqlite_test.v b/vlib/sqlite/sqlite_test.v similarity index 97% rename from vlib/db/sqlite/sqlite_test.v rename to vlib/sqlite/sqlite_test.v index a02b574cd2..d43c3b0456 100644 --- a/vlib/db/sqlite/sqlite_test.v +++ b/vlib/sqlite/sqlite_test.v @@ -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