Skip to content

Commit

Permalink
Fix issues 45 and 46
Browse files Browse the repository at this point in the history
  • Loading branch information
kpgalligan committed May 11, 2021
1 parent 4cda0af commit 24ce67c
Show file tree
Hide file tree
Showing 6 changed files with 39 additions and 41 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -59,9 +59,7 @@ fun DatabaseConnection.stringForQuery(sql: String): String = withStatement(sql)
* @param cipherKey the database cipher key
*/
fun DatabaseConnection.setCipherKey(cipherKey: String) {
withStatement("PRAGMA key = ?;"){
bindString(1, cipherKey)
}
stringForQuery("PRAGMA key = '${cipherKey.escapeSql()}';")
}

/**
Expand All @@ -74,11 +72,11 @@ fun DatabaseConnection.setCipherKey(cipherKey: String) {
//TODO: Maybe figure out key suppress in log?
fun DatabaseConnection.resetCipherKey(oldKey: String, newKey: String) {
setCipherKey(oldKey)
withStatement("PRAGMA rekey = ?;"){
bindString(1, newKey)
}
stringForQuery("PRAGMA rekey = '${newKey.escapeSql()}';")
}

private fun String.escapeSql() = this.replace(oldValue = "'", newValue = "''")

/**
* Gets the database version.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package co.touchlab.sqliter.interop

import kotlinx.cinterop.*
import platform.posix.usleep
import sqlite3.*
import co.touchlab.sqliter.sqlite3.*

expect inline fun bytesToString(bv:CPointer<ByteVar>):String

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,37 +23,37 @@ internal inline fun sqlException(logging: Logger, config: SqliteDatabaseConfig,
}

enum class SqliteErrorType(val code: Int) {
SQLITE_OK(sqlite3.SQLITE_OK), /* Successful result */
SQLITE_OK(co.touchlab.sqliter.sqlite3.SQLITE_OK), /* Successful result */

/* beginning-of-error-codes */
SQLITE_ERROR(sqlite3.SQLITE_ERROR), /* Generic error */
SQLITE_INTERNAL(sqlite3.SQLITE_INTERNAL), /* Internal logic error in SQLite */
SQLITE_PERM(sqlite3.SQLITE_PERM), /* Access permission denied */
SQLITE_ABORT(sqlite3.SQLITE_ABORT), /* Callback routine requested an abort */
SQLITE_BUSY(sqlite3.SQLITE_BUSY), /* The database file is locked */
SQLITE_LOCKED(sqlite3.SQLITE_LOCKED), /* A table in the database is locked */
SQLITE_NOMEM(sqlite3.SQLITE_NOMEM), /* A malloc() failed */
SQLITE_READONLY(sqlite3.SQLITE_READONLY), /* Attempt to write a readonly database */
SQLITE_INTERRUPT(sqlite3.SQLITE_INTERRUPT), /* Operation terminated by sqlite3_interrupt()*/
SQLITE_IOERR(sqlite3.SQLITE_IOERR), /* Some kind of disk I/O error occurred */
SQLITE_CORRUPT(sqlite3.SQLITE_CORRUPT), /* The database disk image is malformed */
SQLITE_NOTFOUND(sqlite3.SQLITE_NOTFOUND), /* Unknown opcode in sqlite3_file_control() */
SQLITE_FULL(sqlite3.SQLITE_FULL), /* Insertion failed because database is full */
SQLITE_CANTOPEN(sqlite3.SQLITE_CANTOPEN), /* Unable to open the database file */
SQLITE_PROTOCOL(sqlite3.SQLITE_PROTOCOL), /* Database lock protocol error */
SQLITE_EMPTY(sqlite3.SQLITE_EMPTY), /* Internal use only */
SQLITE_SCHEMA(sqlite3.SQLITE_SCHEMA), /* The database schema changed */
SQLITE_TOOBIG(sqlite3.SQLITE_TOOBIG), /* String or BLOB exceeds size limit */
SQLITE_CONSTRAINT(sqlite3.SQLITE_CONSTRAINT), /* Abort due to constraint violation */
SQLITE_MISMATCH(sqlite3.SQLITE_MISMATCH), /* Data type mismatch */
SQLITE_MISUSE(sqlite3.SQLITE_MISUSE), /* Library used incorrectly */
SQLITE_NOLFS(sqlite3.SQLITE_NOLFS), /* Uses OS features not supported on host */
SQLITE_AUTH(sqlite3.SQLITE_AUTH), /* Authorization denied */
SQLITE_FORMAT(sqlite3.SQLITE_FORMAT), /* Not used */
SQLITE_RANGE(sqlite3.SQLITE_RANGE), /* 2nd parameter to sqlite3_bind out of range */
SQLITE_NOTADB(sqlite3.SQLITE_NOTADB), /* File opened that is not a database file */
SQLITE_NOTICE(sqlite3.SQLITE_NOTICE), /* Notifications from sqlite3_log() */
SQLITE_WARNING(sqlite3.SQLITE_WARNING), /* Warnings from sqlite3_log() */
SQLITE_ROW(sqlite3.SQLITE_ROW), /* sqlite3_step() has another row ready */
SQLITE_DONE(sqlite3.SQLITE_DONE), /* sqlite3_step() has finished executing */
SQLITE_ERROR(co.touchlab.sqliter.sqlite3.SQLITE_ERROR), /* Generic error */
SQLITE_INTERNAL(co.touchlab.sqliter.sqlite3.SQLITE_INTERNAL), /* Internal logic error in SQLite */
SQLITE_PERM(co.touchlab.sqliter.sqlite3.SQLITE_PERM), /* Access permission denied */
SQLITE_ABORT(co.touchlab.sqliter.sqlite3.SQLITE_ABORT), /* Callback routine requested an abort */
SQLITE_BUSY(co.touchlab.sqliter.sqlite3.SQLITE_BUSY), /* The database file is locked */
SQLITE_LOCKED(co.touchlab.sqliter.sqlite3.SQLITE_LOCKED), /* A table in the database is locked */
SQLITE_NOMEM(co.touchlab.sqliter.sqlite3.SQLITE_NOMEM), /* A malloc() failed */
SQLITE_READONLY(co.touchlab.sqliter.sqlite3.SQLITE_READONLY), /* Attempt to write a readonly database */
SQLITE_INTERRUPT(co.touchlab.sqliter.sqlite3.SQLITE_INTERRUPT), /* Operation terminated by sqlite3_interrupt()*/
SQLITE_IOERR(co.touchlab.sqliter.sqlite3.SQLITE_IOERR), /* Some kind of disk I/O error occurred */
SQLITE_CORRUPT(co.touchlab.sqliter.sqlite3.SQLITE_CORRUPT), /* The database disk image is malformed */
SQLITE_NOTFOUND(co.touchlab.sqliter.sqlite3.SQLITE_NOTFOUND), /* Unknown opcode in sqlite3_file_control() */
SQLITE_FULL(co.touchlab.sqliter.sqlite3.SQLITE_FULL), /* Insertion failed because database is full */
SQLITE_CANTOPEN(co.touchlab.sqliter.sqlite3.SQLITE_CANTOPEN), /* Unable to open the database file */
SQLITE_PROTOCOL(co.touchlab.sqliter.sqlite3.SQLITE_PROTOCOL), /* Database lock protocol error */
SQLITE_EMPTY(co.touchlab.sqliter.sqlite3.SQLITE_EMPTY), /* Internal use only */
SQLITE_SCHEMA(co.touchlab.sqliter.sqlite3.SQLITE_SCHEMA), /* The database schema changed */
SQLITE_TOOBIG(co.touchlab.sqliter.sqlite3.SQLITE_TOOBIG), /* String or BLOB exceeds size limit */
SQLITE_CONSTRAINT(co.touchlab.sqliter.sqlite3.SQLITE_CONSTRAINT), /* Abort due to constraint violation */
SQLITE_MISMATCH(co.touchlab.sqliter.sqlite3.SQLITE_MISMATCH), /* Data type mismatch */
SQLITE_MISUSE(co.touchlab.sqliter.sqlite3.SQLITE_MISUSE), /* Library used incorrectly */
SQLITE_NOLFS(co.touchlab.sqliter.sqlite3.SQLITE_NOLFS), /* Uses OS features not supported on host */
SQLITE_AUTH(co.touchlab.sqliter.sqlite3.SQLITE_AUTH), /* Authorization denied */
SQLITE_FORMAT(co.touchlab.sqliter.sqlite3.SQLITE_FORMAT), /* Not used */
SQLITE_RANGE(co.touchlab.sqliter.sqlite3.SQLITE_RANGE), /* 2nd parameter to sqlite3_bind out of range */
SQLITE_NOTADB(co.touchlab.sqliter.sqlite3.SQLITE_NOTADB), /* File opened that is not a database file */
SQLITE_NOTICE(co.touchlab.sqliter.sqlite3.SQLITE_NOTICE), /* Notifications from sqlite3_log() */
SQLITE_WARNING(co.touchlab.sqliter.sqlite3.SQLITE_WARNING), /* Warnings from sqlite3_log() */
SQLITE_ROW(co.touchlab.sqliter.sqlite3.SQLITE_ROW), /* sqlite3_step() has another row ready */
SQLITE_DONE(co.touchlab.sqliter.sqlite3.SQLITE_DONE), /* sqlite3_step() has finished executing */
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package co.touchlab.sqliter.interop
import cnames.structs.sqlite3
import cnames.structs.sqlite3_stmt
import kotlinx.cinterop.*
import sqlite3.*
import co.touchlab.sqliter.sqlite3.*

class SqliteDatabase(path:String, label:String, val logger: Logger, private val verboseDataCalls: Boolean, val dbPointer:SqliteDatabasePointer) {
val config = SqliteDatabaseConfig(path, label)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,5 @@ package co.touchlab.sqliter.interop
import cnames.structs.sqlite3_stmt
import kotlinx.cinterop.CPointer

typealias SqliteDatabasePointer = CPointer<sqlite3.sqlite3>
typealias SqliteDatabasePointer = CPointer<co.touchlab.sqliter.sqlite3.sqlite3>
typealias SqliteStatementPointer = CPointer<sqlite3_stmt>
2 changes: 1 addition & 1 deletion sqliter-driver/src/nativeInterop/cinterop/sqlite3.def
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package = sqlite3
package = co.touchlab.sqliter.sqlite3
headers = sqlite3.h
headerFilter = sqlite3*.h

Expand Down

0 comments on commit 24ce67c

Please sign in to comment.