SQLCipher experiment (#17)

This commit is contained in:
RichΛrd 2020-05-11 16:35:58 -04:00 committed by GitHub
parent 5c3b240cef
commit 9ad0d2a8a7
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 804 additions and 0 deletions

7
sqlite/.gitignore vendored Normal file
View File

@ -0,0 +1,7 @@
libcrypto.a
openssl-1.1.1g.tar.gz
openssl-1.1.1g
sqlite3.a
sqlcipher
main
myDatabase

4
sqlite/Makefile Normal file
View File

@ -0,0 +1,4 @@
SHELL := bash
build:
nim c -l:lib/sqlite3.a -L:-lm -l:lib/libcrypto.a --threads --outdir:. main.nim

110
sqlite/README.md Normal file
View File

@ -0,0 +1,110 @@
SQLCipher - Nim / NOTES
===
**Notes:**
This is a experimental project to test how to use SQLCipher with Nim. I tried to use [c2nim](https://github.com/nim-lang/c2nim) to generate a small wrapper for SQLite using the header file generated during the SQLCipher compilation process but I wasn't successful. Someone please try using that software to see if it works for them
I ended up using, [Tiny_SQLite](https://github.com/GULPF/tiny_sqlite/blob/master/src/tiny_sqlite/sqlite_wrapper.nim) because it already provides a wrapper for SQLite. It assumes SQLite is dynamically linked so I changed it to static linking so I can use it with the compiled SQLCipher and added new functions.
```nim
# In sqlite_wrapper.nim
proc key*(db: PSqlite3, pKey: cstring, nKey: int32): int32{.cdecl, importc: "sqlite3_key".}
proc rekey*(db: PSqlite3, pKey: cstring, nKey: int32): int32{.cdecl, importc: "sqlite3_rekey".}
# In tiny_sqlite.nim
proc key*(db: DbConn, password: string) =
let rc = sqlite.key(db, password, int32(password.len))
db.checkRc(rc)
proc rekey*(db: DbConn, password: string) =
let rc = sqlite.rekey(db, password, int32(password.len))
db.checkRc(rc)
```
We can either fork this library, or create a new .nim file with the required functions, and use Tiny_SQLite along with the SQLCipher specific functions. Docs for Tiny_SQLite are available here: https://gulpf.github.io/tiny_sqlite/tiny_sqlite.html
### Statically Linked OpenSSL
There are some implications: gclib should be installed in the OS that will run the static linked. This is probably an non-issue. Ubuntu is providing this lib since 2013. It might be the same for other operative systems
```
rm -rf lib
mkdir lib
# Compiling libcrypto.o
wget https://www.openssl.org/source/openssl-1.1.1g.tar.gz
tar -zxvf openssl-1.1.1g.tar.gz
rm openssl-1.1.1g.tar.gz
cd openssl-1.1.1g/
./config -shared
make -j`nproc`
cp libcrypto.a ../lib/.
cd ..
rm -rf openssl-1.1.1g/
# Generating sqlite3.c
git clone https://github.com/sqlcipher/sqlcipher.git
cd sqlcipher/
./configure --enable-tempstore=yes CFLAGS="-DSQLITE_HAS_CODEC" LDFLAGS="../lib/libcrypto.a"
make sqlite3.c
cp sqlite3.c ../lib/.
cd ..
rm -rf sqlcipher
# Compiling sqlite3.c
gcc -lpthread -DSQLITE_HAS_CODEC -L./lib/libcrypto.a -c ./lib/sqlite3.c -o ./lib/sqlite3.a
rm ./lib/sqlite3.c
```
## Compile / Run
```
make build
./main
```
This will ask for a passwd to encrypt/decrypt the DB. and then insert a timestamp in a table, and select all records from that table.
## Dynamic linking
Depends on the requirements / security considerations. It assumes `libssl-dev` is installed in Ubuntu, and will do a dynamic linking: the final user will have to install openssl before running the executable.
```
sudo apt install libssl-dev
```
```
rm -rf lib
mkdir lib
# Generating sqlite3.c
git clone https://github.com/sqlcipher/sqlcipher.git
cd sqlcipher/
./configure --enable-tempstore=yes CFLAGS="-DSQLITE_HAS_CODEC" LDFLAGS="-lcrypto"
make sqlite3.c
cp sqlite3.c ../lib/.
cd ..
rm -rf sqlcipher
#Compiling sqlite3.c**
gcc -lpthread -DSQLITE_HAS_CODEC -lcrypto -c ./lib/sqlite3.c -o ./lib/sqlite3.a
rm ./lib/sqlite3.c
```
The build command changes:
```
nim c -d:release -L:./lib/sqlite3.a -L:-lm -L:"-lcrypto" --threads --outdir:. main.nim
./main
```

22
sqlite/main.nim Normal file
View File

@ -0,0 +1,22 @@
import tiny_sqlite
import times
import strformat
when isMainModule:
let db: DbConn = openDatabase("./myDatabase")
write(stdout, "Enter the db password> ")
let passwd = readLine(stdin)
key(db, passwd)
execScript(db, "create table if not exists Log (theTime text primary key)")
let date = getDateStr(now())
let time = getClockStr(now())
execScript(db, &"""insert into Log values("{date}:{time}")""")
echo rows(db, "select * from Log")

377
sqlite/sqlite_wrapper.nim Normal file
View File

@ -0,0 +1,377 @@
const
SQLITE_INTEGER* = 1
SQLITE_FLOAT* = 2
SQLITE_BLOB* = 4
SQLITE_NULL* = 5
SQLITE_TEXT* = 3
SQLITE_UTF8* = 1
SQLITE_UTF16LE* = 2
SQLITE_UTF16BE* = 3 # Use native byte order
SQLITE_UTF16* = 4 # sqlite3_create_function only
SQLITE_ANY* = 5 #sqlite_exec return values
SQLITE_OK* = 0
SQLITE_ERROR* = 1 # SQL error or missing database
SQLITE_INTERNAL* = 2 # An internal logic error in SQLite
SQLITE_PERM* = 3 # Access permission denied
SQLITE_ABORT* = 4 # Callback routine requested an abort
SQLITE_BUSY* = 5 # The database file is locked
SQLITE_LOCKED* = 6 # A table in the database is locked
SQLITE_NOMEM* = 7 # A malloc() failed
SQLITE_READONLY* = 8 # Attempt to write a readonly database
SQLITE_INTERRUPT* = 9 # Operation terminated by sqlite3_interrupt()
SQLITE_IOERR* = 10 # Some kind of disk I/O error occurred
SQLITE_CORRUPT* = 11 # The database disk image is malformed
SQLITE_NOTFOUND* = 12 # (Internal Only) Table or record not found
SQLITE_FULL* = 13 # Insertion failed because database is full
SQLITE_CANTOPEN* = 14 # Unable to open the database file
SQLITE_PROTOCOL* = 15 # Database lock protocol error
SQLITE_EMPTY* = 16 # Database is empty
SQLITE_SCHEMA* = 17 # The database schema changed
SQLITE_TOOBIG* = 18 # Too much data for one row of a table
SQLITE_CONSTRAINT* = 19 # Abort due to contraint violation
SQLITE_MISMATCH* = 20 # Data type mismatch
SQLITE_MISUSE* = 21 # Library used incorrectly
SQLITE_NOLFS* = 22 # Uses OS features not supported on host
SQLITE_AUTH* = 23 # Authorization denied
SQLITE_FORMAT* = 24 # Auxiliary database format error
SQLITE_RANGE* = 25 # 2nd parameter to sqlite3_bind out of range
SQLITE_NOTADB* = 26 # File opened that is not a database file
SQLITE_ROW* = 100 # sqlite3_step() has another row ready
SQLITE_DONE* = 101 # sqlite3_step() has finished executing
SQLITE_COPY* = 0
SQLITE_CREATE_INDEX* = 1
SQLITE_CREATE_TABLE* = 2
SQLITE_CREATE_TEMP_INDEX* = 3
SQLITE_CREATE_TEMP_TABLE* = 4
SQLITE_CREATE_TEMP_TRIGGER* = 5
SQLITE_CREATE_TEMP_VIEW* = 6
SQLITE_CREATE_TRIGGER* = 7
SQLITE_CREATE_VIEW* = 8
SQLITE_DELETE* = 9
SQLITE_DROP_INDEX* = 10
SQLITE_DROP_TABLE* = 11
SQLITE_DROP_TEMP_INDEX* = 12
SQLITE_DROP_TEMP_TABLE* = 13
SQLITE_DROP_TEMP_TRIGGER* = 14
SQLITE_DROP_TEMP_VIEW* = 15
SQLITE_DROP_TRIGGER* = 16
SQLITE_DROP_VIEW* = 17
SQLITE_INSERT* = 18
SQLITE_PRAGMA* = 19
SQLITE_READ* = 20
SQLITE_SELECT* = 21
SQLITE_TRANSACTION* = 22
SQLITE_UPDATE* = 23
SQLITE_ATTACH* = 24
SQLITE_DETACH* = 25
SQLITE_ALTER_TABLE* = 26
SQLITE_REINDEX* = 27
SQLITE_DENY* = 1
SQLITE_IGNORE* = 2 # Original from sqlite3.h:
#define SQLITE_STATIC ((void(*)(void *))0)
#define SQLITE_TRANSIENT ((void(*)(void *))-1)
SQLITE_DETERMINISTIC* = 0x800
type
Sqlite3 {.pure, final.} = object
PSqlite3* = ptr Sqlite3
PPSqlite3* = ptr PSqlite3
Context{.pure, final.} = object
Pcontext* = ptr Context
Tstmt{.pure, final.} = object
Pstmt* = ptr Tstmt
Value{.pure, final.} = object
Pvalue* = ptr Value
PValueArg* = array[0..127, Pvalue]
Callback* = proc (para1: pointer, para2: int32, para3,
para4: cstringArray): int32{.cdecl, raises: [].}
Tbind_destructor_func* = proc (para1: pointer){.cdecl, locks: 0, tags: [], raises: [], gcsafe.}
Create_function_step_func* = proc (para1: Pcontext, para2: int32,
para3: PValueArg){.cdecl.}
Create_function_func_func* = proc (para1: Pcontext, para2: int32,
para3: PValueArg){.cdecl.}
Create_function_final_func* = proc (para1: Pcontext){.cdecl.}
Result_func* = proc (para1: pointer){.cdecl.}
Create_collation_func* = proc (para1: pointer, para2: int32, para3: pointer,
para4: int32, para5: pointer): int32{.cdecl.}
Collation_needed_func* = proc (para1: pointer, para2: PSqlite3, eTextRep: int32,
para4: cstring){.cdecl.}
const
SQLITE_STATIC* = nil
SQLITE_TRANSIENT* = cast[Tbind_destructor_func](-1)
proc close*(para1: PSqlite3): int32{.cdecl, importc: "sqlite3_close".}
proc exec*(para1: PSqlite3, sql: cstring, para3: Callback, para4: pointer,
errmsg: var cstring): int32{.cdecl,
importc: "sqlite3_exec".}
proc last_insert_rowid*(para1: PSqlite3): int64{.cdecl,
importc: "sqlite3_last_insert_rowid".}
proc changes*(para1: PSqlite3): int32{.cdecl, importc: "sqlite3_changes".}
proc total_changes*(para1: PSqlite3): int32{.cdecl,
importc: "sqlite3_total_changes".}
proc interrupt*(para1: PSqlite3){.cdecl, importc: "sqlite3_interrupt".}
proc complete*(sql: cstring): int32{.cdecl,
importc: "sqlite3_complete".}
proc complete16*(sql: pointer): int32{.cdecl,
importc: "sqlite3_complete16".}
proc busy_handler*(para1: PSqlite3,
para2: proc (para1: pointer, para2: int32): int32{.cdecl.},
para3: pointer): int32{.cdecl,
importc: "sqlite3_busy_handler".}
proc busy_timeout*(para1: PSqlite3, ms: int32): int32{.cdecl,
importc: "sqlite3_busy_timeout".}
proc get_table*(para1: PSqlite3, sql: cstring, resultp: var cstringArray,
nrow, ncolumn: var cint, errmsg: ptr cstring): int32{.cdecl,
importc: "sqlite3_get_table".}
proc free_table*(result: cstringArray){.cdecl,
importc: "sqlite3_free_table".}
# Todo: see how translate sqlite3_mprintf, sqlite3_vmprintf, sqlite3_snprintf
# function sqlite3_mprintf(_para1:Pchar; args:array of const):Pchar;cdecl; external Sqlite3Lib name 'sqlite3_mprintf';
proc mprintf*(para1: cstring): cstring{.cdecl, varargs,
importc: "sqlite3_mprintf".}
#function sqlite3_vmprintf(_para1:Pchar; _para2:va_list):Pchar;cdecl; external Sqlite3Lib name 'sqlite3_vmprintf';
proc free*(z: cstring){.cdecl, importc: "sqlite3_free".}
#function sqlite3_snprintf(_para1:longint; _para2:Pchar; _para3:Pchar; args:array of const):Pchar;cdecl; external Sqlite3Lib name 'sqlite3_snprintf';
proc snprintf*(para1: int32, para2: cstring, para3: cstring): cstring{.cdecl,
varargs, importc: "sqlite3_snprintf".}
proc set_authorizer*(para1: PSqlite3, xAuth: proc (para1: pointer, para2: int32,
para3: cstring, para4: cstring, para5: cstring, para6: cstring): int32{.
cdecl.}, pUserData: pointer): int32{.cdecl,
importc: "sqlite3_set_authorizer".}
proc trace*(para1: PSqlite3, xTrace: proc (para1: pointer, para2: cstring){.cdecl.},
para3: pointer): pointer{.cdecl,
importc: "sqlite3_trace".}
proc progress_handler*(para1: PSqlite3, para2: int32,
para3: proc (para1: pointer): int32{.cdecl.},
para4: pointer){.cdecl,
importc: "sqlite3_progress_handler".}
proc commit_hook*(para1: PSqlite3, para2: proc (para1: pointer): int32{.cdecl.},
para3: pointer): pointer{.cdecl,
importc: "sqlite3_commit_hook".}
proc open*(filename: cstring, ppDb: var PSqlite3): int32{.cdecl,
importc: "sqlite3_open".}
proc open16*(filename: pointer, ppDb: var PSqlite3): int32{.cdecl,
importc: "sqlite3_open16".}
proc errcode*(db: PSqlite3): int32{.cdecl, importc: "sqlite3_errcode".}
proc errmsg*(para1: PSqlite3): cstring{.cdecl, importc: "sqlite3_errmsg".}
proc errmsg16*(para1: PSqlite3): pointer{.cdecl,
importc: "sqlite3_errmsg16".}
proc prepare*(db: PSqlite3, zSql: cstring, nBytes: int32, ppStmt: var Pstmt,
pzTail: var cstring): int32{.cdecl,
importc: "sqlite3_prepare".}
proc prepare_v2*(db: PSqlite3, zSql: cstring, nByte: cint, ppStmt: var Pstmt,
pzTail: var cstring): cint {.
importc: "sqlite3_prepare_v2", cdecl.}
proc prepare16*(db: PSqlite3, zSql: pointer, nBytes: int32, ppStmt: var Pstmt,
pzTail: var pointer): int32{.cdecl,
importc: "sqlite3_prepare16".}
proc bind_blob*(para1: Pstmt, para2: int32, para3: pointer, n: int32,
para5: Tbind_destructor_func): int32{.cdecl,
importc: "sqlite3_bind_blob".}
proc bind_double*(para1: Pstmt, para2: int32, para3: float64): int32{.cdecl,
importc: "sqlite3_bind_double".}
proc bind_int*(para1: Pstmt, para2: int32, para3: int32): int32{.cdecl,
importc: "sqlite3_bind_int".}
proc bind_int64*(para1: Pstmt, para2: int32, para3: int64): int32{.cdecl,
importc: "sqlite3_bind_int64".}
proc bind_null*(para1: Pstmt, para2: int32): int32{.cdecl,
importc: "sqlite3_bind_null".}
proc bind_text*(para1: Pstmt, para2: int32, para3: cstring, n: int32,
para5: Tbind_destructor_func): int32{.cdecl,
importc: "sqlite3_bind_text".}
proc bind_text16*(para1: Pstmt, para2: int32, para3: pointer, para4: int32,
para5: Tbind_destructor_func): int32{.cdecl,
importc: "sqlite3_bind_text16".}
#function sqlite3_bind_value(_para1:Psqlite3_stmt; _para2:longint; _para3:Psqlite3_value):longint;cdecl; external Sqlite3Lib name 'sqlite3_bind_value';
#These overloaded functions were introduced to allow the use of SQLITE_STATIC and SQLITE_TRANSIENT
#It's the c world man ;-)
proc bind_blob*(para1: Pstmt, para2: int32, para3: pointer, n: int32,
para5: int32): int32{.cdecl,
importc: "sqlite3_bind_blob".}
proc bind_text*(para1: Pstmt, para2: int32, para3: cstring, n: int32,
para5: int32): int32{.cdecl,
importc: "sqlite3_bind_text".}
proc bind_text16*(para1: Pstmt, para2: int32, para3: pointer, para4: int32,
para5: int32): int32{.cdecl,
importc: "sqlite3_bind_text16".}
proc bind_parameter_count*(para1: Pstmt): int32{.cdecl,
importc: "sqlite3_bind_parameter_count".}
proc bind_parameter_name*(para1: Pstmt, para2: int32): cstring{.cdecl,
importc: "sqlite3_bind_parameter_name".}
proc bind_parameter_index*(para1: Pstmt, zName: cstring): int32{.cdecl,
importc: "sqlite3_bind_parameter_index".}
proc clear_bindings*(para1: Pstmt): int32 {.cdecl,
importc: "sqlite3_clear_bindings".}
proc column_count*(pStmt: Pstmt): int32{.cdecl,
importc: "sqlite3_column_count".}
proc column_name*(para1: Pstmt, para2: int32): cstring{.cdecl,
importc: "sqlite3_column_name".}
proc column_table_name*(para1: Pstmt; para2: int32): cstring{.cdecl,
importc: "sqlite3_column_table_name".}
proc column_name16*(para1: Pstmt, para2: int32): pointer{.cdecl,
importc: "sqlite3_column_name16".}
proc column_decltype*(para1: Pstmt, i: int32): cstring{.cdecl,
importc: "sqlite3_column_decltype".}
proc column_decltype16*(para1: Pstmt, para2: int32): pointer{.cdecl,
importc: "sqlite3_column_decltype16".}
proc step*(para1: Pstmt): int32{.cdecl, importc: "sqlite3_step".}
proc data_count*(pStmt: Pstmt): int32{.cdecl,
importc: "sqlite3_data_count".}
proc column_blob*(para1: Pstmt, iCol: int32): pointer{.cdecl,
importc: "sqlite3_column_blob".}
proc column_bytes*(para1: Pstmt, iCol: int32): int32{.cdecl,
importc: "sqlite3_column_bytes".}
proc column_bytes16*(para1: Pstmt, iCol: int32): int32{.cdecl,
importc: "sqlite3_column_bytes16".}
proc column_double*(para1: Pstmt, iCol: int32): float64{.cdecl,
importc: "sqlite3_column_double".}
proc column_int*(para1: Pstmt, iCol: int32): int32{.cdecl,
importc: "sqlite3_column_int".}
proc column_int64*(para1: Pstmt, iCol: int32): int64{.cdecl,
importc: "sqlite3_column_int64".}
proc column_text*(para1: Pstmt, iCol: int32): cstring{.cdecl,
importc: "sqlite3_column_text".}
proc column_text16*(para1: Pstmt, iCol: int32): pointer{.cdecl,
importc: "sqlite3_column_text16".}
proc column_type*(para1: Pstmt, iCol: int32): int32{.cdecl,
importc: "sqlite3_column_type".}
proc finalize*(pStmt: Pstmt): int32{.cdecl,
importc: "sqlite3_finalize".}
proc reset*(pStmt: Pstmt): int32{.cdecl, importc: "sqlite3_reset".}
proc create_function*(para1: PSqlite3, zFunctionName: cstring, nArg: int32,
eTextRep: int32, para5: pointer,
xFunc: Create_function_func_func,
xStep: Create_function_step_func,
xFinal: Create_function_final_func): int32{.cdecl,
importc: "sqlite3_create_function".}
proc create_function16*(para1: PSqlite3, zFunctionName: pointer, nArg: int32,
eTextRep: int32, para5: pointer,
xFunc: Create_function_func_func,
xStep: Create_function_step_func,
xFinal: Create_function_final_func): int32{.cdecl,
importc: "sqlite3_create_function16".}
proc aggregate_count*(para1: Pcontext): int32{.cdecl,
importc: "sqlite3_aggregate_count".}
proc value_blob*(para1: Pvalue): pointer{.cdecl,
importc: "sqlite3_value_blob".}
proc value_bytes*(para1: Pvalue): int32{.cdecl,
importc: "sqlite3_value_bytes".}
proc value_bytes16*(para1: Pvalue): int32{.cdecl,
importc: "sqlite3_value_bytes16".}
proc value_double*(para1: Pvalue): float64{.cdecl,
importc: "sqlite3_value_double".}
proc value_int*(para1: Pvalue): int32{.cdecl,
importc: "sqlite3_value_int".}
proc value_int64*(para1: Pvalue): int64{.cdecl,
importc: "sqlite3_value_int64".}
proc value_text*(para1: Pvalue): cstring{.cdecl,
importc: "sqlite3_value_text".}
proc value_text16*(para1: Pvalue): pointer{.cdecl,
importc: "sqlite3_value_text16".}
proc value_text16le*(para1: Pvalue): pointer{.cdecl,
importc: "sqlite3_value_text16le".}
proc value_text16be*(para1: Pvalue): pointer{.cdecl,
importc: "sqlite3_value_text16be".}
proc value_type*(para1: Pvalue): int32{.cdecl,
importc: "sqlite3_value_type".}
proc aggregate_context*(para1: Pcontext, nBytes: int32): pointer{.cdecl,
importc: "sqlite3_aggregate_context".}
proc user_data*(para1: Pcontext): pointer{.cdecl,
importc: "sqlite3_user_data".}
proc get_auxdata*(para1: Pcontext, para2: int32): pointer{.cdecl,
importc: "sqlite3_get_auxdata".}
proc set_auxdata*(para1: Pcontext, para2: int32, para3: pointer,
para4: proc (para1: pointer){.cdecl.}){.cdecl,
importc: "sqlite3_set_auxdata".}
proc result_blob*(para1: Pcontext, para2: pointer, para3: int32,
para4: Result_func){.cdecl,
importc: "sqlite3_result_blob".}
proc result_double*(para1: Pcontext, para2: float64){.cdecl,
importc: "sqlite3_result_double".}
proc result_error*(para1: Pcontext, para2: cstring, para3: int32){.cdecl,
importc: "sqlite3_result_error".}
proc result_error16*(para1: Pcontext, para2: pointer, para3: int32){.cdecl,
importc: "sqlite3_result_error16".}
proc result_int*(para1: Pcontext, para2: int32){.cdecl,
importc: "sqlite3_result_int".}
proc result_int64*(para1: Pcontext, para2: int64){.cdecl,
importc: "sqlite3_result_int64".}
proc result_null*(para1: Pcontext){.cdecl,
importc: "sqlite3_result_null".}
proc result_text*(para1: Pcontext, para2: cstring, para3: int32,
para4: Result_func){.cdecl,
importc: "sqlite3_result_text".}
proc result_text16*(para1: Pcontext, para2: pointer, para3: int32,
para4: Result_func){.cdecl,
importc: "sqlite3_result_text16".}
proc result_text16le*(para1: Pcontext, para2: pointer, para3: int32,
para4: Result_func){.cdecl,
importc: "sqlite3_result_text16le".}
proc result_text16be*(para1: Pcontext, para2: pointer, para3: int32,
para4: Result_func){.cdecl,
importc: "sqlite3_result_text16be".}
proc result_value*(para1: Pcontext, para2: Pvalue){.cdecl,
importc: "sqlite3_result_value".}
proc create_collation*(para1: PSqlite3, zName: cstring, eTextRep: int32,
para4: pointer, xCompare: Create_collation_func): int32{.
cdecl, importc: "sqlite3_create_collation".}
proc create_collation16*(para1: PSqlite3, zName: cstring, eTextRep: int32,
para4: pointer, xCompare: Create_collation_func): int32{.
cdecl, importc: "sqlite3_create_collation16".}
proc collation_needed*(para1: PSqlite3, para2: pointer, para3: Collation_needed_func): int32{.
cdecl, importc: "sqlite3_collation_needed".}
proc collation_needed16*(para1: PSqlite3, para2: pointer, para3: Collation_needed_func): int32{.
cdecl, importc: "sqlite3_collation_needed16".}
proc libversion*(): cstring{.cdecl, importc: "sqlite3_libversion".}
#Alias for allowing better code portability (win32 is not working with external variables)
proc version*(): cstring{.cdecl, importc: "sqlite3_libversion".}
# Not published functions
proc libversion_number*(): int32{.cdecl,
importc: "sqlite3_libversion_number".}
proc key*(db: PSqlite3, pKey: cstring, nKey: int32): int32{.cdecl, importc: "sqlite3_key".}
proc rekey*(db: PSqlite3, pKey: cstring, nKey: int32): int32{.cdecl, importc: "sqlite3_rekey".}
#function sqlite3_sleep(_para1:longint):longint;cdecl; external Sqlite3Lib name 'sqlite3_sleep';
#function sqlite3_expired(_para1:Psqlite3_stmt):longint;cdecl; external Sqlite3Lib name 'sqlite3_expired';
#function sqlite3_global_recover:longint;cdecl; external Sqlite3Lib name 'sqlite3_global_recover';
# implementation
proc db_handle*(para1: Pstmt): PSqlite3
{.cdecl, importc: "sqlite3_db_handle".}
proc get_autocommit*(db: PSqlite3): cint
{.cdecl, importc: "sqlite3_get_autocommit".}
const
SQLITE_OPEN_READONLY* = 0x00000001 #/* Ok for sqlite3_open_v2() */
SQLITE_OPEN_READWRITE* = 0x00000002 #/* Ok for sqlite3_open_v2() */
SQLITE_OPEN_CREATE* = 0x00000004 #/* Ok for sqlite3_open_v2() */
SQLITE_OPEN_DELETEONCLOSE* = 0x00000008 #/* VFS only */
SQLITE_OPEN_EXCLUSIVE* = 0x00000010 #/* VFS only */
SQLITE_OPEN_AUTOPROXY* = 0x00000020 #/* VFS only */
SQLITE_OPEN_URI* = 0x00000040 #/* Ok for sqlite3_open_v2() */
SQLITE_OPEN_MEMORY* = 0x00000080 #/* Ok for sqlite3_open_v2() */
SQLITE_OPEN_MAIN_DB* = 0x00000100 #/* VFS only */
SQLITE_OPEN_TEMP_DB* = 0x00000200 #/* VFS only */
SQLITE_OPEN_TRANSIENT_DB* = 0x00000400 #/* VFS only */
SQLITE_OPEN_MAIN_JOURNAL* = 0x00000800 #/* VFS only */
SQLITE_OPEN_TEMP_JOURNAL* = 0x00001000 #/* VFS only */
SQLITE_OPEN_SUBJOURNAL* = 0x00002000 #/* VFS only */
SQLITE_OPEN_MASTER_JOURNAL* = 0x00004000 #/* VFS only */
SQLITE_OPEN_NOMUTEX* = 0x00008000 #/* Ok for sqlite3_open_v2() */
SQLITE_OPEN_FULLMUTEX* = 0x00010000 #/* Ok for sqlite3_open_v2() */
SQLITE_OPEN_SHAREDCACHE* = 0x00020000 #/* Ok for sqlite3_open_v2() */
SQLITE_OPEN_PRIVATECACHE* = 0x00040000 #/* Ok for sqlite3_open_v2() */
SQLITE_OPEN_WAL* = 0x00080000 #/* VFS only */
proc open_v2*(filename: cstring, ppDb : var PSqlite3,
flags : int32 , zVfsName : cstring ): int32 {.
cdecl,importc: "sqlite3_open_v2".}
proc db_readonly*(ppDb: PSqlite3, dbname : cstring): int32 {.
cdecl,importc: "sqlite3_db_readonly".}

284
sqlite/tiny_sqlite.nim Normal file
View File

@ -0,0 +1,284 @@
import std / [strutils, options, macros, typetraits]
from sqlite_wrapper as sqlite import nil
type
DbConn* = sqlite.PSqlite3 ## \
## Encapsulates a database connection.
## Note that this is just an alias for `sqlite_wrapper.PSqlite3`.
PreparedSql = sqlite.Pstmt
DbMode* = enum
dbRead,
dbReadWrite
SqliteError* = object of CatchableError ## \
## Raised when an error in the underlying SQLite library
## occurs.
errorCode*: int32 ## \
## This is the error code that was returned by the underlying
## SQLite library. Constants for the different possible
## values of this field exists in the
## ``tiny_sqlite/sqlite_wrapper`` module.
DbValueKind* = enum ## \
## Enum of all possible value types in a Sqlite database.
sqliteNull,
sqliteInteger,
sqliteReal,
sqliteText,
sqliteBlob
DbValue* = object ## \
## Represents a value in a SQLite database.
case kind*: DbValueKind
of sqliteInteger:
intVal*: int64
of sqliteReal:
floatVal*: float64
of sqliteText:
strVal*: string
of sqliteBlob:
blobVal*: seq[byte]
of sqliteNull:
discard
proc newSqliteError(db: DbConn, errorCode: int32): ref SqliteError =
## Raises a SqliteError exception.
(ref SqliteError)(
msg: $sqlite.errmsg(db),
errorCode: errorCode
)
template checkRc(db: DbConn, rc: int32) =
if rc != sqlite.SQLITE_OK:
raise newSqliteError(db, rc)
proc prepareSql(db: DbConn, sql: string, params: seq[DbValue]): PreparedSql
{.raises: [SqliteError].} =
var tail: cstring
let rc = sqlite.prepare_v2(db, sql.cstring, sql.len.cint, result, tail)
assert tail.len == 0,
"`exec` and `execMany` can only be used with a single SQL statement. " &
"To execute several SQL statements, use `execScript`"
db.checkRc(rc)
var idx = 1'i32
for value in params:
let rc =
case value.kind
of sqliteNull: sqlite.bind_null(result, idx)
of sqliteInteger: sqlite.bind_int64(result, idx, value.intval)
of sqliteReal: sqlite.bind_double(result, idx, value.floatVal)
of sqliteText: sqlite.bind_text(result, idx, value.strVal.cstring,
value.strVal.len.int32, sqlite.SQLITE_TRANSIENT)
of sqliteBlob: sqlite.bind_blob(result, idx.int32,
cast[string](value.blobVal).cstring,
value.blobVal.len.int32, sqlite.SQLITE_TRANSIENT)
sqlite.db_handle(result).checkRc(rc)
idx.inc
proc next(prepared: PreparedSql): bool =
## Advance cursor by one row.
## Return ``true`` if there are more rows.
let rc = sqlite.step(prepared)
if rc == sqlite.SQLITE_ROW:
result = true
elif rc == sqlite.SQLITE_DONE:
result = false
else:
raise newSqliteError(sqlite.db_handle(prepared), rc)
proc finalize(prepared: PreparedSql) =
## Finalize statement or raise SqliteError if not successful.
let rc = sqlite.finalize(prepared)
sqlite.db_handle(prepared).checkRc(rc)
proc toDbValue*[T: Ordinal](val: T): DbValue =
DbValue(kind: sqliteInteger, intVal: val.int64)
proc toDbValue*[T: SomeFloat](val: T): DbValue =
DbValue(kind: sqliteReal, floatVal: val)
proc toDbValue*[T: string](val: T): DbValue =
DbValue(kind: sqliteText, strVal: val)
proc toDbValue*[T: seq[byte]](val: T): DbValue =
DbValue(kind: sqliteBlob, blobVal: val)
proc toDbValue*[T: Option](val: T): DbValue =
if val.isNone:
DbValue(kind: sqliteNull)
else:
toDbValue(val.get)
when (NimMajor, NimMinor, NimPatch) > (0, 19, 9):
proc toDbValue*[T: type(nil)](val: T): DbValue =
DbValue(kind: sqliteNull)
proc nilDbValue(): DbValue =
## Since above isn't available for older versions,
## we use this internally.
DbValue(kind: sqliteNull)
proc fromDbValue*(val: DbValue, T: typedesc[Ordinal]): T = val.intval.T
proc fromDbValue*(val: DbValue, T: typedesc[SomeFloat]): float64 = val.floatVal
proc fromDbValue*(val: DbValue, T: typedesc[string]): string = val.strVal
proc fromDbValue*(val: DbValue, T: typedesc[seq[byte]]): seq[byte] = val.blobVal
proc fromDbValue*(val: DbValue, T: typedesc[DbValue]): T = val
proc fromDbValue*[T](val: DbValue, _: typedesc[Option[T]]): Option[T] =
if val.kind == sqliteNull:
none(T)
else:
some(val.fromDbValue(T))
proc unpack*[T: tuple](row: openArray[DbValue], _: typedesc[T]): T =
## Call ``fromDbValue`` on each element of ``row`` and return it
## as a tuple.
var idx = 0
for value in result.fields:
value = row[idx].fromDbValue(type(value))
idx.inc
proc `$`*(dbVal: DbValue): string =
result.add "DbValue["
case dbVal.kind
of sqliteInteger: result.add $dbVal.intVal
of sqliteReal: result.add $dbVal.floatVal
of sqliteText: result.addQuoted dbVal.strVal
of sqliteBlob: result.add "<blob>"
of sqliteNull: result.add "nil"
result.add "]"
proc exec*(db: DbConn, sql: string, params: varargs[DbValue, toDbValue]) =
## Executes ``sql`` and raises SqliteError if not successful.
assert (not db.isNil), "Database is nil"
let prepared = db.prepareSql(sql, @params)
defer: prepared.finalize()
discard prepared.next
proc execMany*(db: DbConn, sql: string, params: seq[seq[DbValue]]) =
## Executes ``sql`` repeatedly using each element of ``params`` as parameters.
assert (not db.isNil), "Database is nil"
for p in params:
db.exec(sql, p)
proc execScript*(db: DbConn, sql: string) =
## Executes the query and raises SqliteError if not successful.
assert (not db.isNil), "Database is nil"
let rc = sqlite.exec(db, sql.cstring, cast[sqlite.Callback](nil), nil,
cast[var cstring](nil))
db.checkRc(rc)
template transaction*(db: DbConn, body: untyped) =
db.exec("BEGIN")
var ok = true
try:
try:
body
except Exception as ex:
ok = false
db.exec("ROLLBACK")
raise ex
finally:
if ok:
db.exec("COMMIT")
proc readColumn(prepared: PreparedSql, col: int32): DbValue =
let columnType = sqlite.column_type(prepared, col)
case columnType
of sqlite.SQLITE_INTEGER:
result = toDbValue(sqlite.column_int64(prepared, col))
of sqlite.SQLITE_FLOAT:
result = toDbValue(sqlite.column_double(prepared, col))
of sqlite.SQLITE_TEXT:
result = toDbValue($sqlite.column_text(prepared, col))
of sqlite.SQLITE_BLOB:
let blob = sqlite.column_blob(prepared, col)
let bytes = sqlite.column_bytes(prepared, col)
var s = newSeq[byte](bytes)
if bytes != 0:
copyMem(addr(s[0]), blob, bytes)
result = toDbValue(s)
of sqlite.SQLITE_NULL:
result = nilDbValue()
else:
raiseAssert "Unexpected column type: " & $columnType
iterator rows*(db: DbConn, sql: string,
params: varargs[DbValue, toDbValue]): seq[DbValue] =
## Executes the query and iterates over the result dataset.
assert (not db.isNil), "Database is nil"
let prepared = db.prepareSql(sql, @params)
defer: prepared.finalize()
var row = newSeq[DbValue](sqlite.column_count(prepared))
while prepared.next:
for col, _ in row:
row[col] = readColumn(prepared, col.int32)
yield row
proc rows*(db: DbConn, sql: string,
params: varargs[DbValue, toDbValue]): seq[seq[DbValue]] =
## Executes the query and returns the resulting rows.
for row in db.rows(sql, params):
result.add row
proc openDatabase*(path: string, mode = dbReadWrite): DbConn =
## Open a new database connection to a database file. To create a
## in-memory database the special path `":memory:"` can be used.
## If the database doesn't already exist and ``mode`` is ``dbReadWrite``,
## the database will be created. If the database doesn't exist and ``mode``
## is ``dbRead``, a ``SqliteError`` exception will be raised.
##
## NOTE: To avoid memory leaks, ``db.close`` must be called when the
## database connection is no longer needed.
runnableExamples:
let memDb = openDatabase(":memory:")
case mode
of dbReadWrite:
let rc = sqlite.open(path, result)
result.checkRc(rc)
of dbRead:
let rc = sqlite.open_v2(path, result, sqlite.SQLITE_OPEN_READONLY, nil)
result.checkRc(rc)
proc key*(db: DbConn, password: string) =
let rc = sqlite.key(db, password, int32(password.len))
db.checkRc(rc)
proc rekey*(db: DbConn, password: string) =
let rc = sqlite.rekey(db, password, int32(password.len))
db.checkRc(rc)
proc close*(db: DbConn) =
## Closes the database connection.
let rc = sqlite.close(db)
db.checkRc(rc)
proc lastInsertRowId*(db: DbConn): int64 =
## Get the row id of the last inserted row.
## For tables with an integer primary key,
## the row id will be the primary key.
##
## For more information, refer to the SQLite documentation
## (https://www.sqlite.org/c3ref/last_insert_rowid.html).
sqlite.last_insert_rowid(db)
proc changes*(db: DbConn): int32 =
## Get the number of changes triggered by the most recent INSERT, UPDATE or
## DELETE statement.
##
## For more information, refer to the SQLite documentation
## (https://www.sqlite.org/c3ref/changes.html).
sqlite.changes(db)
proc isReadonly*(db: DbConn): bool =
## Returns true if ``db`` is in readonly mode.
sqlite.db_readonly(db, "main") == 1