1
0
mirror of https://github.com/vlang/v.git synced 2023-08-10 21:13:21 +03:00
v/vlib/sqlite/sqlite.v

237 lines
5.0 KiB
V
Raw Normal View History

2019-10-27 23:32:15 +03:00
module sqlite
$if freebsd || openbsd {
#flag -I/usr/local/include
#flag -L/usr/local/lib
}
$if windows {
#flag windows -I@VEXEROOT/thirdparty/sqlite
#flag windows -L@VEXEROOT/thirdparty/sqlite
#flag windows @VEXEROOT/thirdparty/sqlite/sqlite3.o
} $else {
#flag -lsqlite3
}
2019-10-27 23:32:15 +03:00
#include "sqlite3.h"
2021-11-17 05:45:50 +03:00
pub const (
sqlite_ok = 0
sqlite_error = 1
sqlite_row = 100
sqlite_done = 101
)
2020-10-19 21:11:04 +03:00
struct C.sqlite3 {
}
struct C.sqlite3_stmt {
}
struct Stmt {
stmt &C.sqlite3_stmt
db &DB
}
struct SQLError {
msg string
code int
}
//
pub struct DB {
pub mut:
is_open bool
2019-10-27 23:32:15 +03:00
mut:
2021-01-20 11:30:26 +03:00
conn &C.sqlite3
2019-10-27 23:32:15 +03:00
}
pub fn (db DB) str() string {
return 'sqlite.DB{ conn: ' + ptr_str(db.conn) + ' }'
}
pub struct Row {
2019-10-27 23:32:15 +03:00
pub mut:
vals []string
}
2020-10-19 21:11:04 +03:00
//
2021-04-06 22:15:14 +03:00
fn C.sqlite3_open(&char, &&C.sqlite3) int
2020-10-19 21:11:04 +03:00
fn C.sqlite3_close(&C.sqlite3) int
2020-10-19 21:11:04 +03:00
fn C.sqlite3_last_insert_rowid(&C.sqlite3) i64
//
2021-04-06 22:15:14 +03:00
fn C.sqlite3_prepare_v2(&C.sqlite3, &char, int, &&C.sqlite3_stmt, &&char) int
2020-10-19 21:11:04 +03:00
fn C.sqlite3_step(&C.sqlite3_stmt) int
2020-10-19 21:11:04 +03:00
fn C.sqlite3_finalize(&C.sqlite3_stmt) int
2020-10-19 21:11:04 +03:00
//
2021-04-06 22:15:14 +03:00
fn C.sqlite3_column_name(&C.sqlite3_stmt, int) &char
2020-10-19 21:11:04 +03:00
2021-04-06 22:15:14 +03:00
fn C.sqlite3_column_text(&C.sqlite3_stmt, int) &byte
2020-10-19 21:11:04 +03:00
fn C.sqlite3_column_int(&C.sqlite3_stmt, int) int
2020-10-19 21:11:04 +03:00
fn C.sqlite3_column_int64(&C.sqlite3_stmt, int) i64
2020-10-19 21:11:04 +03:00
fn C.sqlite3_column_double(&C.sqlite3_stmt, int) f64
2020-10-19 21:11:04 +03:00
fn C.sqlite3_column_count(&C.sqlite3_stmt) int
2020-10-19 21:11:04 +03:00
//
2021-04-06 22:15:14 +03:00
fn C.sqlite3_errstr(int) &char
2020-10-19 21:11:04 +03:00
fn C.sqlite3_errmsg(&C.sqlite3) &char
fn C.sqlite3_free(voidptr)
// connect Opens the connection with a database.
2020-06-16 13:14:22 +03:00
pub fn connect(path string) ?DB {
2019-12-04 13:08:28 +03:00
db := &C.sqlite3(0)
code := C.sqlite3_open(&char(path.str), &db)
if code != 0 {
return IError(&SQLError{
msg: unsafe { cstring_to_vstring(&char(C.sqlite3_errstr(code))) }
code: code
})
2020-06-16 13:14:22 +03:00
}
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 IError(&SQLError{
msg: unsafe { cstring_to_vstring(&char(C.sqlite3_errstr(code))) }
code: code
})
}
return true // successfully closed
2019-10-27 23:32:15 +03:00
}
2020-06-17 01:59:33 +03:00
// Only for V ORM
fn get_int_from_stmt(stmt &C.sqlite3_stmt) int {
x := C.sqlite3_step(stmt)
if x != C.SQLITE_OK && x != C.SQLITE_DONE {
2020-07-16 01:48:10 +03:00
C.puts(C.sqlite3_errstr(x))
}
2020-06-17 01:59:33 +03:00
res := C.sqlite3_column_int(stmt, 0)
C.sqlite3_finalize(stmt)
return res
}
// Returns last insert rowid
// https://www.sqlite.org/c3ref/last_insert_rowid.html
pub fn (db DB) last_insert_rowid() i64 {
return C.sqlite3_last_insert_rowid(db.conn)
}
// Returns a single cell with value int.
2019-10-27 23:32:15 +03:00
pub fn (db DB) q_int(query string) int {
2019-12-04 13:08:28 +03:00
stmt := &C.sqlite3_stmt(0)
C.sqlite3_prepare_v2(db.conn, &char(query.str), query.len, &stmt, 0)
2019-10-27 23:32:15 +03:00
C.sqlite3_step(stmt)
2019-10-27 23:32:15 +03:00
res := C.sqlite3_column_int(stmt, 0)
C.sqlite3_finalize(stmt)
return res
}
// Returns a single cell with value string.
2019-10-27 23:32:15 +03:00
pub fn (db DB) q_string(query string) string {
2019-12-04 13:08:28 +03:00
stmt := &C.sqlite3_stmt(0)
defer {
C.sqlite3_finalize(stmt)
}
C.sqlite3_prepare_v2(db.conn, &char(query.str), query.len, &stmt, 0)
2019-10-27 23:32:15 +03:00
C.sqlite3_step(stmt)
val := unsafe { &byte(C.sqlite3_column_text(stmt, 0)) }
return if val != &byte(0) { unsafe { tos_clone(val) } } else { '' }
2019-10-27 23:32:15 +03:00
}
// Execute the query on db, return an array of all the results, alongside any result code.
// Result codes: https://www.sqlite.org/rescode.html
2020-10-19 21:11:04 +03:00
pub fn (db DB) exec(query string) ([]Row, int) {
2019-12-04 13:08:28 +03:00
stmt := &C.sqlite3_stmt(0)
C.sqlite3_prepare_v2(db.conn, &char(query.str), query.len, &stmt, 0)
2019-10-27 23:32:15 +03:00
nr_cols := C.sqlite3_column_count(stmt)
mut res := 0
2020-04-26 17:25:54 +03:00
mut rows := []Row{}
2019-10-27 23:32:15 +03:00
for {
res = C.sqlite3_step(stmt)
2020-04-26 17:25:54 +03:00
// Result Code SQLITE_ROW; Another row is available
if res != 100 {
2020-10-19 21:11:04 +03:00
// C.puts(C.sqlite3_errstr(res))
2019-10-27 23:32:15 +03:00
break
2019-12-04 13:08:28 +03:00
}
2019-10-27 23:32:15 +03:00
mut row := Row{}
for i in 0 .. nr_cols {
val := unsafe { &byte(C.sqlite3_column_text(stmt, i)) }
if val == &byte(0) {
row.vals << ''
} else {
row.vals << unsafe { tos_clone(val) }
}
2019-10-27 23:32:15 +03:00
}
rows << row
}
2020-11-09 10:22:16 +03:00
C.sqlite3_finalize(stmt)
2020-10-19 21:11:04 +03:00
return rows, res
2019-10-27 23:32:15 +03:00
}
// Execute a query, handle error code
// Return the first row from the resulting table
2019-10-27 23:32:15 +03:00
pub fn (db DB) exec_one(query string) ?Row {
2020-10-19 21:11:04 +03:00
rows, code := db.exec(query)
if rows.len == 0 {
return IError(&SQLError{
msg: 'No rows'
code: code
})
} else if code != 101 {
return IError(&SQLError{
msg: unsafe { cstring_to_vstring(&char(C.sqlite3_errstr(code))) }
code: code
})
}
2019-10-27 23:32:15 +03:00
return rows[0]
}
pub fn (db DB) error_message(code int, query string) IError {
msg := unsafe { cstring_to_vstring(&char(C.sqlite3_errmsg(db.conn))) }
return IError(&SQLError{
msg: '$msg ($code) ($query)'
code: code
})
}
// In case you don't expect any result, but still want an error code
// e.g. INSERT INTO ... VALUES (...)
pub fn (db DB) exec_none(query string) int {
2020-10-19 21:11:04 +03:00
_, code := db.exec(query)
return code
}
2020-10-19 21:11:04 +03:00
/*
TODO
2019-10-27 23:32:15 +03:00
pub fn (db DB) exec_param(query string, param string) []Row {
}
*/
2021-01-20 11:30:26 +03:00
pub fn (db DB) create_table(table_name string, columns []string) {
2021-04-06 22:15:14 +03:00
db.exec('create table if not exists $table_name (' + columns.join(',\n') + ')')
2021-01-20 11:30:26 +03:00
}