From cd6d17518096a53cc8dbcba276117ed47d95ea41 Mon Sep 17 00:00:00 2001 From: yuyi Date: Thu, 26 Mar 2020 16:21:34 +0800 Subject: [PATCH] db: increase db module level in vlib --- 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, 34 insertions(+), 37 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 bb1d7ce84b..4609294b5e 100644 --- a/examples/database/mysql.v +++ b/examples/database/mysql.v @@ -1,4 +1,4 @@ -// import mysql +// import db.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 05dd074697..1360820c60 100644 --- a/examples/database/pg/customer.v +++ b/examples/database/pg/customer.v @@ -1,6 +1,6 @@ module main -import pg +import db.pg struct Customer { id int @@ -58,5 +58,3 @@ fn main() { } db.insert(nc) } - - diff --git a/examples/database/sqlite.v b/examples/database/sqlite.v index 311a0c064b..6288ee947e 100644 --- a/examples/database/sqlite.v +++ b/examples/database/sqlite.v @@ -1,22 +1,22 @@ -import sqlite +import db.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 4dd6428210..27b823941a 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 == 'pg__DB' && !p.fileis('pg.v') && p.peek() == .name { + if typ == 'db_dot_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 ceec14fa26..1878f6d827 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, 'pg__DB_q_int(') + p.cgen.set_placeholder(fn_ph, 'db_dot_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_pg__Row ${qprefix}rows = pg__res_to_rows ( ${qprefix}res ) ; -Option_pg__Row opt_${qprefix}row = pg__rows_first_or_empty( ${qprefix}rows ); +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 ); if (! opt_${qprefix}row . ok ) { opt_${qprefix}$tmp = v_error( opt_${qprefix}row . error ); }else{ $table_name ${qprefix}$tmp; - pg__Row ${qprefix}row = *(pg__Row*) opt_${qprefix}row . data; + db_dot_pg__Row ${qprefix}row = *(db_dot_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_pg__Row ${qprefix}rows = pg__res_to_rows(${qprefix}res); +array_db_dot_pg__Row ${qprefix}rows = db_dot_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++) { - pg__Row ${qprefix}row = *(pg__Row*)array_get(${qprefix}rows, i); + db_dot_pg__Row ${qprefix}row = *(db_dot_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,4 +337,3 @@ 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/mysql/_cdefs.v b/vlib/db/mysql/_cdefs.v similarity index 100% rename from vlib/mysql/_cdefs.v rename to vlib/db/mysql/_cdefs.v diff --git a/vlib/mysql/consts.v b/vlib/db/mysql/consts.v similarity index 100% rename from vlib/mysql/consts.v rename to vlib/db/mysql/consts.v diff --git a/vlib/mysql/enums.v b/vlib/db/mysql/enums.v similarity index 100% rename from vlib/mysql/enums.v rename to vlib/db/mysql/enums.v diff --git a/vlib/mysql/mysql.v b/vlib/db/mysql/mysql.v similarity index 100% rename from vlib/mysql/mysql.v rename to vlib/db/mysql/mysql.v diff --git a/vlib/mysql/result.v b/vlib/db/mysql/result.v similarity index 100% rename from vlib/mysql/result.v rename to vlib/db/mysql/result.v diff --git a/vlib/mysql/utils.v b/vlib/db/mysql/utils.v similarity index 100% rename from vlib/mysql/utils.v rename to vlib/db/mysql/utils.v diff --git a/vlib/orm/orm_test.v b/vlib/db/orm/orm_test.v similarity index 100% rename from vlib/orm/orm_test.v rename to vlib/db/orm/orm_test.v diff --git a/vlib/pg/pg.v b/vlib/db/pg/pg.v similarity index 89% rename from vlib/pg/pg.v rename to vlib/db/pg/pg.v index 0372ec3cce..f3ec0ccd4b 100644 --- a/vlib/pg/pg.v +++ b/vlib/db/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 pg.Config) ?DB { +pub fn connect(config 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 pg.Config) ?DB { return DB {conn: conn} } -fn res_to_rows(res voidptr) []pg.Row { +fn res_to_rows(res voidptr) []Row { nr_rows := C.PQntuples(res) nr_cols := C.PQnfields(res) - mut rows := []pg.Row + mut rows := []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) []pg.Row { +pub fn (db DB) q_strings(query string) []Row { return db.exec(query) } -pub fn (db DB) exec(query string) []pg.Row { +pub fn (db DB) exec(query string) []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) []pg.Row { return res_to_rows(res) } -fn rows_first_or_empty(rows []pg.Row) ?pg.Row { +fn rows_first_or_empty(rows []Row) ?Row { if rows.len == 0 { return error('no row') } return rows[0] } -pub fn (db DB) exec_one(query string) ?pg.Row { +pub fn (db DB) exec_one(query string) ?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) ?pg.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 { +pub fn (db DB) exec_param_many(query string, params []string) []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) []pg.Row { } } -pub fn (db DB) exec_param2(query string, param, param2 string) []pg.Row { +pub fn (db DB) exec_param2(query string, param, param2 string) []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) []pg.Row { return db.handle_error_or_result(res, 'exec_param2') } -pub fn (db DB) exec_param(query string, param string) []pg.Row { +pub fn (db DB) exec_param(query string, param string) []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 { +fn (db DB) handle_error_or_result(res voidptr, elabel string) []Row { e := string(C.PQerrorMessage(db.conn)) if e != '' { println('pg $elabel error:') diff --git a/vlib/pg/readme.md b/vlib/db/pg/readme.md similarity index 100% rename from vlib/pg/readme.md rename to vlib/db/pg/readme.md diff --git a/vlib/sqlite/readme.md b/vlib/db/sqlite/readme.md similarity index 100% rename from vlib/sqlite/readme.md rename to vlib/db/sqlite/readme.md diff --git a/vlib/sqlite/sqlite.v b/vlib/db/sqlite/sqlite.v similarity index 100% rename from vlib/sqlite/sqlite.v rename to vlib/db/sqlite/sqlite.v diff --git a/vlib/sqlite/sqlite_test.v b/vlib/db/sqlite/sqlite_test.v similarity index 97% rename from vlib/sqlite/sqlite_test.v rename to vlib/db/sqlite/sqlite_test.v index d43c3b0456..a02b574cd2 100644 --- a/vlib/sqlite/sqlite_test.v +++ b/vlib/db/sqlite/sqlite_test.v @@ -1,20 +1,20 @@ -import sqlite +import db.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