mirror of
https://github.com/vlang/v.git
synced 2025-09-13 14:32:26 +03:00
db: modify mysql/pg/sqlite interface for pool working (#24780)
This commit is contained in:
parent
f62b5fd7f2
commit
7039081d66
15 changed files with 214 additions and 21 deletions
|
@ -330,12 +330,10 @@ pub fn (mut db DB) refresh(options u32) !bool {
|
|||
}
|
||||
|
||||
// reset resets the connection, and clear the session state.
|
||||
pub fn (mut db DB) reset() !bool {
|
||||
pub fn (mut db DB) reset() ! {
|
||||
if C.mysql_reset_connection(db.conn) != 0 {
|
||||
db.throw_mysql_error()!
|
||||
}
|
||||
|
||||
return true
|
||||
}
|
||||
|
||||
// ping pings a server connection, or tries to reconnect if the connection
|
||||
|
@ -348,8 +346,14 @@ pub fn (mut db DB) ping() !bool {
|
|||
return true
|
||||
}
|
||||
|
||||
// validate pings a server connection, or tries to reconnect if the connection
|
||||
// has gone down.
|
||||
pub fn (mut db DB) validate() !bool {
|
||||
return db.ping()!
|
||||
}
|
||||
|
||||
// close closes the connection.
|
||||
pub fn (mut db DB) close() {
|
||||
pub fn (mut db DB) close() ! {
|
||||
C.mysql_close(db.conn)
|
||||
}
|
||||
|
||||
|
|
|
@ -58,7 +58,7 @@ fn test_mysql_orm() {
|
|||
dbname: 'mysql'
|
||||
)!
|
||||
defer {
|
||||
db.close()
|
||||
db.close() or {}
|
||||
}
|
||||
table := orm.Table{
|
||||
name: 'Test'
|
||||
|
|
|
@ -16,6 +16,11 @@ fn test_mysql() {
|
|||
}
|
||||
|
||||
mut db := mysql.connect(config)!
|
||||
defer {
|
||||
db.close() or {}
|
||||
}
|
||||
|
||||
assert db.validate()!
|
||||
|
||||
mut response := db.exec('drop table if exists users')!
|
||||
assert response == []mysql.Row{}
|
||||
|
|
|
@ -33,6 +33,6 @@ pub fn (mut pool ConnectionPool) release(conn DB) {
|
|||
pub fn (mut pool ConnectionPool) close() {
|
||||
for _ in 0 .. pool.connections.len {
|
||||
mut conn := <-pool.connections or { break }
|
||||
conn.close()
|
||||
conn.close() or { break }
|
||||
}
|
||||
}
|
||||
|
|
|
@ -224,7 +224,7 @@ fn res_to_rows(res voidptr) []Row {
|
|||
}
|
||||
|
||||
// close frees the underlying resource allocated by the database connection
|
||||
pub fn (db &DB) close() {
|
||||
pub fn (db &DB) close() ! {
|
||||
C.PQfinish(db.conn)
|
||||
}
|
||||
|
||||
|
@ -510,3 +510,13 @@ pub fn (db &DB) savepoint(savepoint string) ! {
|
|||
return error('pg exec error: "${e}"')
|
||||
}
|
||||
}
|
||||
|
||||
// validate checks if the connection is still usable
|
||||
pub fn (db &DB) validate() !bool {
|
||||
db.exec_one('SELECT 1')!
|
||||
return true
|
||||
}
|
||||
|
||||
// reset returns the connection to initial state for reuse
|
||||
pub fn (db &DB) reset() ! {
|
||||
}
|
||||
|
|
|
@ -19,7 +19,7 @@ fn test_float_field() {
|
|||
conn := 'host=localhost user=postgres password=12345678' // insert own connection string
|
||||
db := pg.connect_with_conninfo(conn)!
|
||||
defer {
|
||||
db.close()
|
||||
db.close() or {}
|
||||
}
|
||||
|
||||
sql db {
|
||||
|
|
|
@ -57,7 +57,7 @@ fn test_pg_orm() {
|
|||
) or { panic(err) }
|
||||
|
||||
defer {
|
||||
db.close()
|
||||
db.close() or {}
|
||||
}
|
||||
table := orm.Table{
|
||||
name: 'Test'
|
||||
|
|
|
@ -12,9 +12,11 @@ fn test_large_exec() {
|
|||
|
||||
db := pg.connect(pg.Config{ user: 'postgres', password: '12345678', dbname: 'postgres' })!
|
||||
defer {
|
||||
db.close()
|
||||
db.close() or {}
|
||||
}
|
||||
|
||||
assert db.validate()!
|
||||
|
||||
rows := db.exec('
|
||||
SELECT ischema.table_schema, c.relname, a.attname, t.typname, t.typalign, t.typlen
|
||||
FROM pg_class c
|
||||
|
@ -38,7 +40,7 @@ fn test_prepared() {
|
|||
}
|
||||
db := pg.connect(pg.Config{ user: 'postgres', password: '12345678', dbname: 'postgres' })!
|
||||
defer {
|
||||
db.close()
|
||||
db.close() or {}
|
||||
}
|
||||
|
||||
db.prepare('test_prepared', 'SELECT NOW(), $1 AS NAME', 1) or { panic(err) }
|
||||
|
@ -57,7 +59,7 @@ fn test_transaction() {
|
|||
|
||||
db := pg.connect(pg.Config{ user: 'postgres', password: '12345678', dbname: 'postgres' })!
|
||||
defer {
|
||||
db.close()
|
||||
db.close() or {}
|
||||
}
|
||||
db.exec('drop table if exists users')!
|
||||
db.exec('create table if not exists users (
|
||||
|
|
|
@ -33,6 +33,6 @@ pub fn (mut pool ConnectionPool) release(conn DB) {
|
|||
pub fn (mut pool ConnectionPool) close() {
|
||||
for _ in 0 .. pool.connections.len {
|
||||
conn := <-pool.connections or { break }
|
||||
conn.close()
|
||||
conn.close() or { break }
|
||||
}
|
||||
}
|
||||
|
|
|
@ -150,7 +150,7 @@ pub fn connect(path string) !DB {
|
|||
// close Closes the DB.
|
||||
// TODO: For all functions, determine whether the connection is
|
||||
// closed first, and determine what to do if it is
|
||||
pub fn (mut db DB) close() !bool {
|
||||
pub fn (mut db DB) close() ! {
|
||||
code := C.sqlite3_close(db.conn)
|
||||
if code == 0 {
|
||||
db.is_open = false
|
||||
|
@ -160,7 +160,6 @@ pub fn (mut db DB) close() !bool {
|
|||
code: code
|
||||
}
|
||||
}
|
||||
return true // successfully closed
|
||||
}
|
||||
|
||||
// Only for V ORM
|
||||
|
@ -500,3 +499,12 @@ pub fn (mut db DB) rollback_to(savepoint string) ! {
|
|||
}
|
||||
db.exec('ROLLBACK TO ${savepoint};')!
|
||||
}
|
||||
|
||||
// reset returns the connection to initial state for reuse
|
||||
pub fn (mut db DB) reset() ! {
|
||||
}
|
||||
|
||||
// validate checks if the connection is still usable
|
||||
pub fn (mut db DB) validate() !bool {
|
||||
return db.exec_none('SELECT 1') == 100
|
||||
}
|
||||
|
|
|
@ -37,6 +37,7 @@ fn test_sqlite() {
|
|||
}
|
||||
mut db := sqlite.connect(':memory:') or { panic(err) }
|
||||
assert db.is_open
|
||||
assert db.validate()!
|
||||
db.exec('drop table if exists users')!
|
||||
db.exec("create table users (id integer primary key, name text default '', last_name text null default null);")!
|
||||
db.exec("insert into users (name) values ('Sam')")!
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue