Skip to content

Commit fc0c881

Browse files
committed
Little fixes
1 parent 90d17e8 commit fc0c881

File tree

8 files changed

+77
-12
lines changed

8 files changed

+77
-12
lines changed

gradle.properties

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
kotlin.code.style=official
22

33
GROUP=co.touchlab
4-
VERSION_NAME=1.0.0-a2
4+
VERSION_NAME=1.0.0-b1
55

66
KOTLIN_VERSION=1.4.21
77
kotlin.native.ignoreDisabledTargets=true

sqliter-driver/src/appleMain/kotlin/co/touchlab/sqliter/interop/Platform.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,4 @@ import kotlinx.cinterop.*
44
import platform.Foundation.NSString
55
import platform.Foundation.create
66

7-
actual inline fun bytesToString(bv:CPointer<ByteVar>):String = NSString.create(bv).toString()
7+
actual inline fun bytesToString(bv:CPointer<ByteVar>):String = NSString.create(uTF8String = bv).toString()

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

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ class ActualSqliteStatement(private val db: SqliteDatabase, internal val stmtPoi
5353
return false;
5454
} else if (err == SQLITE_LOCKED || err == SQLITE_BUSY) {
5555
// The table is locked, retry
56-
trace_log("Database locked, retrying")
56+
traceLogCallback("Database locked, retrying")
5757
usleep(1000);
5858
} else {
5959
throw sqlException(db.logger, db.config, "sqlite3_step failed", err)
@@ -142,4 +142,8 @@ class ActualSqliteStatement(private val db: SqliteDatabase, internal val stmtPoi
142142
}
143143
return err
144144
}
145+
146+
override fun traceLogCallback(message: String) {
147+
//No logging
148+
}
145149
}

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

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,6 @@ package co.touchlab.sqliter.interop
22

33
internal const val LOG_TRACE = true
44

5-
internal inline fun trace_log(s: String) {
6-
if (LOG_TRACE) {
7-
print("trace - ")
8-
println(s)
9-
}
10-
}
11-
125
interface Logger {
136
fun trace(message: String)
147
val vActive:Boolean
@@ -17,7 +10,6 @@ interface Logger {
1710
fun eWrite(message: String, exception: Throwable? = null)
1811
}
1912

20-
2113
inline fun Logger.v(block:()->String){
2214
if(vActive)
2315
vWrite(block())

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,4 +26,6 @@ interface SqliteStatement {
2626
fun bindString(index: Int, value: String)
2727
fun bindBlob(index: Int, value: ByteArray)
2828
fun executeNonQuery(): Int
29+
30+
fun traceLogCallback(message:String)
2931
}

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,4 +29,8 @@ class TracingSqliteStatement(private val logger: Logger, private val delegate:Sq
2929
override fun bindString(index: Int, value: String) = logWrapper("bindString", listOf(index, value)) {delegate.bindString(index, value)}
3030
override fun bindBlob(index: Int, value: ByteArray) = logWrapper("bindBlob", listOf(index, value)) {delegate.bindBlob(index, value)}
3131
override fun executeNonQuery(): Int = logWrapper("executeNonQuery", emptyList()) {delegate.executeNonQuery()}
32+
override fun traceLogCallback(message: String) {
33+
logger.vWrite(message)
34+
delegate.traceLogCallback(message)
35+
}
3236
}

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

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ val TEST_DB_NAME = "testdb"
2525
fun createTestDb(
2626
name:String = TEST_DB_NAME,
2727
version:Int = 1,
28+
timeout:Int? = null,
2829
update:(DatabaseConnection, Int, Int)->Unit = {_,_,_->},
2930
onCreateConnection: (DatabaseConnection) -> Unit = { _ -> },
3031
onCloseConnection: (DatabaseConnection) -> Unit = { _ -> },
@@ -34,6 +35,10 @@ fun createTestDb(
3435
deleteDatabase(name)
3536
} catch (e: Exception) {
3637
}
38+
var extended = DatabaseConfiguration.Extended()
39+
if(timeout != null)
40+
extended = extended.copy(busyTimeout = timeout)
41+
3742
return createDatabaseManager(DatabaseConfiguration(
3843
name,
3944
version,
@@ -42,7 +47,8 @@ fun createTestDb(
4247
lifecycleConfig = DatabaseConfiguration.Lifecycle(
4348
onCreateConnection = onCreateConnection,
4449
onCloseConnection = onCloseConnection
45-
)
50+
),
51+
extendedConfig = extended
4652
))
4753
}
4854

@@ -65,12 +71,14 @@ val FOUR_COL = "CREATE TABLE test (num INTEGER NOT NULL, " +
6571

6672
fun basicTestDb(
6773
createSql: String = FOUR_COL,
74+
timeout: Int? = null,
6875
onCreateConnection: (DatabaseConnection) -> Unit = { _ -> },
6976
onCloseConnection: (DatabaseConnection) -> Unit = { _ -> },
7077
block: (DatabaseManager) -> Unit
7178
) {
7279
val dbname = TEST_DB_NAME
7380
val dbManager = createTestDb(
81+
timeout = timeout,
7482
onCreateConnection = onCreateConnection,
7583
onCloseConnection = onCloseConnection
7684
) { db ->
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
package co.touchlab.sqliter.concurrency
2+
3+
import co.touchlab.sqliter.*
4+
import kotlin.test.Test
5+
import kotlin.test.assertEquals
6+
7+
class ConcurrentPlaygroundTest {
8+
/**
9+
* Playing with how connections and transactions interact
10+
*/
11+
@Test
12+
fun testLongRead() {
13+
basicTestDb(TWO_COL, timeout = 1000) { dbman ->
14+
val conn = dbman.createMultiThreadedConnection()
15+
val conn2 = dbman.createMultiThreadedConnection()
16+
17+
insert10(conn)
18+
19+
val queryStatement = conn.createStatement("select * from test")
20+
val cursor = queryStatement.query()
21+
cursor.next()
22+
23+
try {
24+
assertEquals(10, countRows(conn2))
25+
insert10(conn2)
26+
assertEquals(10, countRows(conn))
27+
queryStatement.finalizeStatement()
28+
assertEquals(20, countRows(conn))
29+
} finally {
30+
31+
conn.close()
32+
conn2.close()
33+
}
34+
}
35+
}
36+
37+
private fun countRows(conn: DatabaseConnection): Long = conn.withStatement("select count(*) from test"){
38+
val c = query()
39+
c.next()
40+
c.getLong(0)
41+
}
42+
43+
private fun insert10(conn: DatabaseConnection) {
44+
conn.withTransaction {
45+
repeat(10) { i ->
46+
it.withStatement("insert into test(num, str)values(?,?)") {
47+
bindLong(1, i.toLong())
48+
bindString(2, "arst $i")
49+
executeInsert()
50+
}
51+
}
52+
}
53+
}
54+
55+
}

0 commit comments

Comments
 (0)