| 1 | module sqlite |
| 2 | |
| 3 | fn C.sqlite3_bind_null(&C.sqlite3_stmt, i32) i32 |
| 4 | fn C.sqlite3_bind_double(&C.sqlite3_stmt, i32, f64) i32 |
| 5 | fn C.sqlite3_bind_int(&C.sqlite3_stmt, i32, i32) i32 |
| 6 | fn C.sqlite3_bind_int64(&C.sqlite3_stmt, i32, i64) i32 |
| 7 | fn C.sqlite3_bind_text(&C.sqlite3_stmt, i32, &char, i32, voidptr) i32 |
| 8 | |
| 9 | // Only for V ORM |
| 10 | fn (db &DB) init_stmt(query string) (&C.sqlite3_stmt, int) { |
| 11 | // println('init_stmt("${query}")') |
| 12 | stmt := &C.sqlite3_stmt(unsafe { nil }) |
| 13 | err := C.sqlite3_prepare_v2(db.conn, &char(query.str), query.len, &stmt, 0) |
| 14 | return stmt, err |
| 15 | } |
| 16 | |
| 17 | fn (db &DB) new_init_stmt(query string) !Stmt { |
| 18 | stmt, err := db.init_stmt(query) |
| 19 | if err != sqlite_ok { |
| 20 | return db.error_message(err, query) |
| 21 | } |
| 22 | return Stmt{stmt, db} |
| 23 | } |
| 24 | |
| 25 | fn (stmt &Stmt) bind_null(idx int) int { |
| 26 | return C.sqlite3_bind_null(stmt.stmt, idx) |
| 27 | } |
| 28 | |
| 29 | fn (stmt &Stmt) bind_int(idx int, v int) int { |
| 30 | return C.sqlite3_bind_int(stmt.stmt, idx, v) |
| 31 | } |
| 32 | |
| 33 | fn (stmt &Stmt) bind_i64(idx int, v i64) int { |
| 34 | return C.sqlite3_bind_int64(stmt.stmt, idx, v) |
| 35 | } |
| 36 | |
| 37 | fn (stmt &Stmt) bind_f64(idx int, v f64) int { |
| 38 | return C.sqlite3_bind_double(stmt.stmt, idx, v) |
| 39 | } |
| 40 | |
| 41 | fn (stmt &Stmt) bind_text(idx int, s string) int { |
| 42 | return C.sqlite3_bind_text(stmt.stmt, idx, voidptr(s.str), s.len, 0) |
| 43 | } |
| 44 | |
| 45 | fn (stmt &Stmt) get_int(idx int) ?int { |
| 46 | if C.sqlite3_column_type(stmt.stmt, idx) == C.SQLITE_NULL { |
| 47 | return none |
| 48 | } else { |
| 49 | return C.sqlite3_column_int(stmt.stmt, idx) |
| 50 | } |
| 51 | } |
| 52 | |
| 53 | fn (stmt &Stmt) get_i64(idx int) ?i64 { |
| 54 | if C.sqlite3_column_type(stmt.stmt, idx) == C.SQLITE_NULL { |
| 55 | return none |
| 56 | } else { |
| 57 | return C.sqlite3_column_int64(stmt.stmt, idx) |
| 58 | } |
| 59 | } |
| 60 | |
| 61 | fn (stmt &Stmt) get_f64(idx int) ?f64 { |
| 62 | if C.sqlite3_column_type(stmt.stmt, idx) == C.SQLITE_NULL { |
| 63 | return none |
| 64 | } else { |
| 65 | return C.sqlite3_column_double(stmt.stmt, idx) |
| 66 | } |
| 67 | } |
| 68 | |
| 69 | fn (stmt &Stmt) get_text(idx int) ?string { |
| 70 | if C.sqlite3_column_type(stmt.stmt, idx) == C.SQLITE_NULL { |
| 71 | return none |
| 72 | } else { |
| 73 | b := &char(C.sqlite3_column_text(stmt.stmt, idx)) |
| 74 | if b == &char(unsafe { nil }) { |
| 75 | return '' |
| 76 | } |
| 77 | l := C.sqlite3_column_bytes(stmt.stmt, idx) |
| 78 | return unsafe { b.vstring_with_len(l) } |
| 79 | } |
| 80 | } |
| 81 | |
| 82 | fn (stmt &Stmt) get_count() int { |
| 83 | return C.sqlite3_column_count(stmt.stmt) |
| 84 | } |
| 85 | |
| 86 | fn (stmt &Stmt) step() int { |
| 87 | return C.sqlite3_step(stmt.stmt) |
| 88 | } |
| 89 | |
| 90 | fn (stmt &Stmt) orm_step(query string) ! { |
| 91 | res := stmt.step() |
| 92 | if res != sqlite_ok && res != sqlite_done && res != sqlite_row { |
| 93 | return stmt.db.error_message(res, query) |
| 94 | } |
| 95 | } |
| 96 | |
| 97 | fn (stmt &Stmt) finalize() { |
| 98 | C.sqlite3_finalize(stmt.stmt) |
| 99 | } |
| 100 | |