Skip to content

Commit 2804384

Browse files
committed
Added rawExecSql and a little warning cleanup
1 parent 40d744b commit 2804384

File tree

10 files changed

+86
-38
lines changed

10 files changed

+86
-38
lines changed

sqliter-driver/src/appleMain/kotlin/co/touchlab/sqliter/internal/File.kt

Lines changed: 3 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -44,38 +44,15 @@ internal actual class File(dirPath:String?=null, name:String){
4444
* This field is initialized from the system property "file.separator".
4545
* Later changes to that property will have no effect on this field or this class.
4646
*/
47-
var separatorChar: Char = '/'
47+
val separatorChar: Char = '/'
4848

4949
/**
5050
* The system-dependent string used to separate components in filenames ('/').
5151
* See [.separatorChar].
5252
*/
53-
var separator: String = "/"
53+
val separator: String = "/"
5454

55-
/**
56-
* The system-dependent character used to separate components in search paths (':').
57-
* This is used to split such things as the PATH environment variable and classpath
58-
* system properties into lists of directories to be searched.
59-
*
60-
*
61-
* This field is initialized from the system property "path.separator".
62-
* Later changes to that property will have no effect on this field or this class.
63-
*/
64-
var pathSeparatorChar: Char = ':'
65-
66-
/**
67-
* The system-dependent string used to separate components in search paths (":").
68-
* See [.pathSeparatorChar].
69-
*/
70-
var pathSeparator: String = ":"
71-
72-
private var caseSensitive: Boolean = true
73-
74-
// separatorChar = System.getProperty("file.separator", "/").charAt(0);
75-
// pathSeparatorChar = System.getProperty("path.separator", ":").charAt(0);
76-
// separator = String.valueOf(separatorChar);
77-
// pathSeparator = String.valueOf(pathSeparatorChar);
78-
// caseSensitive = isCaseSensitiveImpl();
55+
private val caseSensitive: Boolean = true
7956
}
8057

8158
/**

sqliter-driver/src/nativeCommonMain/kotlin/co/touchlab/sqliter/DatabaseConnection.kt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
package co.touchlab.sqliter
1818

1919
interface DatabaseConnection {
20+
fun rawExecSql(sql: String)
2021
fun createStatement(sql: String): Statement
2122
fun beginTransaction()
2223
fun setTransactionSuccessful()

sqliter-driver/src/nativeCommonMain/kotlin/co/touchlab/sqliter/concurrency/ConcurrentDatabaseConnection.kt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@ import co.touchlab.sqliter.Statement
2424
class ConcurrentDatabaseConnection(private val delegateConnection:DatabaseConnection):DatabaseConnection{
2525
private val accessLock = Lock()
2626

27+
override fun rawExecSql(sql: String) = accessLock.withLock { delegateConnection.rawExecSql(sql) }
28+
2729
override fun createStatement(sql: String): Statement = accessLock.withLock { ConcurrentStatement(delegateConnection.createStatement(sql)) }
2830

2931
override fun beginTransaction() = accessLock.withLock { delegateConnection.beginTransaction() }

sqliter-driver/src/nativeCommonMain/kotlin/co/touchlab/sqliter/interop/SqliteDatabase.kt

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,13 @@ class SqliteDatabase(path:String, label:String, val logger: Logger, private val
3535
}
3636
}
3737

38+
fun rawExecSql(sqlString: String){
39+
val err = sqlite3_exec(dbPointer, sqlString, null, null, null)
40+
if (err != SQLITE_OK) {
41+
throw sqlException(logger, config, "error rawExecSql: $sqlString", err)
42+
}
43+
}
44+
3845
fun close(){
3946
logger.v { "close $config" }
4047

sqliter-driver/src/nativeCommonMain/kotlin/co/touchlab/sqliter/native/NativeDatabaseConnection.kt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,10 @@ class NativeDatabaseConnection(
3636

3737
data class Transaction(val successful: Boolean)
3838

39+
override fun rawExecSql(sql: String) {
40+
sqliteDatabase.rawExecSql(sql)
41+
}
42+
3943
override fun createStatement(sql: String): Statement {
4044
val statementPtr = sqliteDatabase.prepareStatement(sql)
4145
val statement = NativeStatement(this, statementPtr, sql)

sqliter-driver/src/nativeCommonTest/kotlin/co/touchlab/sqliter/DatabaseConfigurationTest.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -171,8 +171,8 @@ class DatabaseConfigurationTest : BaseDatabaseTest(){
171171
conn.withStatement("insert into book(id, name, author_id)values(${bookId++}, 'Hello Book', 5)") { executeInsert() }
172172

173173
fun makeBook(conn: DatabaseConnection) {
174-
conn.withTransaction { conn ->
175-
makeBookWithoutTransaction(conn)
174+
conn.withTransaction { conn2 ->
175+
makeBookWithoutTransaction(conn2)
176176
}
177177
}
178178
fun checkAB(conn:DatabaseConnection, expectBooks: Int, expectAuthors: Int){

sqliter-driver/src/nativeCommonTest/kotlin/co/touchlab/sqliter/DatabaseConnectionTest.kt

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -351,6 +351,16 @@ class DatabaseConnectionTest {
351351
}
352352
}
353353

354+
@Test
355+
fun rawSqlInsert() {
356+
basicTestDb(TWO_COL) { databaseManager ->
357+
databaseManager.withConnection {
358+
it.rawExecSql("INSERT INTO test(num, str)values(3,'abc')")
359+
assertEquals(1, it.longForQuery("select count(*) from test"))
360+
}
361+
}
362+
}
363+
354364
private fun checkDbIsFile(memoryName: String?, mem:Boolean): Boolean {
355365
var dbFileExists = false
356366
val checkName = memoryName ?: ":memory:"

sqliter-driver/src/nativeCommonTest/kotlin/co/touchlab/sqliter/DatabaseManagerTest.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -179,7 +179,7 @@ class DatabaseManagerTest : BaseDatabaseTest(){
179179

180180
//Upgrade to v2 fails
181181
assertFails { createDatabaseManager(configUpgradeFails).withConnection { } }
182-
val configUpgrade = configUpgradeFails.copy(upgrade = {conn, from, to ->
182+
val configUpgrade = configUpgradeFails.copy(upgrade = {conn, _,_ ->
183183
conn.withStatement("CREATE TABLE test2 (num INTEGER NOT NULL, " +
184184
"str TEXT NOT NULL)"){execute()}
185185
conn.withStatement("insert into test2(num, str)values(?,?)"){

sqliter-driver/src/nativeCommonTest/kotlin/co/touchlab/sqliter/NativeDatabaseConnectionTest.kt

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -348,7 +348,7 @@ class NativeDatabaseConnectionTest : BaseDatabaseTest(){
348348

349349
val conn = manager.createMultiThreadedConnection()
350350
val stmt = conn.createStatement("select * from test")
351-
val cursor = stmt.query()
351+
stmt.query()
352352

353353
//After connection close, we should still be able to finalize statements
354354
conn.close()
@@ -380,7 +380,7 @@ class NativeDatabaseConnectionTest : BaseDatabaseTest(){
380380
execute()
381381
}
382382
},
383-
upgrade = {dc, oldv, newv ->
383+
upgrade = {_,_,_ ->
384384
throw IllegalStateException("This shouldn't happen")
385385
},
386386
extendedConfig = DatabaseConfiguration.Extended(busyTimeout = 3000)
@@ -391,21 +391,23 @@ class NativeDatabaseConnectionTest : BaseDatabaseTest(){
391391

392392
val config2 = config1.copy(
393393
version = 2,
394-
create = { db ->
394+
create = {
395395
throw IllegalStateException("This shouldn't happen")
396396
},
397-
upgrade = {dc, oldv, newv ->
397+
upgrade = {_,_,_ ->
398398
if(!upgradeCalled.compareAndSet(0, 1))
399399
throw IllegalStateException("Multiple upgrade calls")
400400
}
401401
)
402402

403403
val workers = (0 until 20).map { Worker.start(errorReporting = true, name = "Test Worker $it") }
404-
val futures = workers.map { it.execute(TransferMode.SAFE, {config2.freeze()}){
405-
val manager = createDatabaseManager(it)
406-
val conn = manager.createMultiThreadedConnection()
407-
conn.close()
408-
} }
404+
val futures = workers.map { worker ->
405+
worker.execute(TransferMode.SAFE, { config2.freeze() }) {
406+
val managerInner = createDatabaseManager(it)
407+
val conn = managerInner.createMultiThreadedConnection()
408+
conn.close()
409+
}
410+
}
409411

410412
futures.forEach {
411413
it.result

sqliter-driver/src/nativeCommonTest/kotlin/co/touchlab/sqliter/NativeStatementTest.kt

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -217,5 +217,50 @@ class NativeStatementTest : BaseDatabaseTest(){
217217
}
218218
}
219219
}
220+
221+
val TWO_COL_WITH_BLOB = "CREATE TABLE test (num INTEGER NOT NULL, " +
222+
"blb BLOB NOT NULL)"
223+
224+
// @Test
225+
// Need to review what other drivers do here. It's not acting as expected
226+
// https://github.com/touchlab/SQLiter/issues/62
227+
fun bindEmptyBlob() {
228+
basicTestDb(TWO_COL_WITH_BLOB) {
229+
it.withConnection {
230+
it.withStatement("insert into test(num, blb)values(?,?)") {
231+
bindLong(1, 22)
232+
bindBlob(2, ByteArray(0){it.toByte()})
233+
executeInsert()
234+
}
235+
236+
it.withStatement("select blb from test") {
237+
val query = query()
238+
query.next()
239+
assertEquals(query.getBytes(query.columnNames["blb"]!!).size, 0)
240+
}
241+
}
242+
}
243+
}
244+
245+
@Test
246+
fun bindBlob() {
247+
basicTestDb(TWO_COL_WITH_BLOB) {
248+
it.withConnection {
249+
it.withTransaction {
250+
it.withStatement("insert into test(num, blb)values(?,?)") {
251+
bindLong(1, 22)
252+
bindBlob(2, ByteArray(10){it.toByte()})
253+
executeInsert()
254+
}
255+
}
256+
257+
it.withStatement("select blb from test") {
258+
val query = query()
259+
query.next()
260+
assertEquals(query.getBytes(query.columnNames["blb"]!!).size, 10)
261+
}
262+
}
263+
}
264+
}
220265
}
221266

0 commit comments

Comments
 (0)