mirror of
https://github.com/vlang/v.git
synced 2025-09-14 23:12:33 +03:00
sqlite: add close
method, and an is_open
field (#7382)
This commit is contained in:
parent
6c341a77f5
commit
c831711a0e
2 changed files with 24 additions and 3 deletions
|
@ -20,8 +20,10 @@ struct C.sqlite3_stmt {
|
|||
|
||||
//
|
||||
pub struct DB {
|
||||
pub mut:
|
||||
is_open bool
|
||||
mut:
|
||||
conn &C.sqlite3
|
||||
conn &C.sqlite3
|
||||
}
|
||||
|
||||
pub fn (db DB) str() string {
|
||||
|
@ -63,7 +65,7 @@ fn C.sqlite3_errstr(int) charptr
|
|||
|
||||
fn C.sqlite3_free(voidptr)
|
||||
|
||||
// Opens the connection with a database.
|
||||
// connect Opens the connection with a database.
|
||||
pub fn connect(path string) ?DB {
|
||||
db := &C.sqlite3(0)
|
||||
if C.sqlite3_open(path.str, &db) != 0 {
|
||||
|
@ -71,9 +73,23 @@ pub fn connect(path string) ?DB {
|
|||
}
|
||||
return DB{
|
||||
conn: db
|
||||
is_open: true
|
||||
}
|
||||
}
|
||||
|
||||
// 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 {
|
||||
code := C.sqlite3_close(db.conn)
|
||||
if code == 0 {
|
||||
db.is_open = false
|
||||
} else {
|
||||
return error('sqlite db error: failed to close with code: $code')
|
||||
}
|
||||
return true // successfully closed
|
||||
}
|
||||
|
||||
// Only for V ORM
|
||||
fn (db DB) init_stmt(query string) &C.sqlite3_stmt {
|
||||
stmt := &C.sqlite3_stmt(0)
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue