Skip to content

Commit 7d8009f

Browse files
authored
Merge pull request #109 from touchlab/05nelsonm-issue/104-create-callback
05nelsonm issue/104 create callback
2 parents 7bbf0ea + 725bb34 commit 7d8009f

File tree

4 files changed

+94
-18
lines changed

4 files changed

+94
-18
lines changed

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

+12-1
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,18 @@ class NativeDatabaseManager(private val path:String,
9797
conn.close()
9898
throw e
9999
}
100-
newConnection.increment()
100+
101+
// "Temporary" and "purely in-memory" databases live only as long
102+
// as the connection. Subsequent connections (even if open at
103+
// the same time) are completely separate databases.
104+
//
105+
// If this is the case, do not increment newConnection so that
106+
// this if block executes on every new connection (i.e. every new
107+
// ephemeral database).
108+
when (path) {
109+
"", ":memory:" -> {}
110+
else -> newConnection.increment()
111+
}
101112
}
102113

103114
conn

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

+13
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,19 @@ class DatabaseConfigurationTest : BaseDatabaseTest(){
6060
assertEquals(":memory:", dbPathString)
6161
}
6262

63+
@Test
64+
fun temporaryOnlyTest(){
65+
val conf = DatabaseConfiguration(
66+
name = "",
67+
inMemory = false,
68+
version = 1,
69+
create = {},
70+
extendedConfig = DatabaseConfiguration.Extended(basePath = ""),
71+
)
72+
val dbPathString = diskOrMemoryPath(conf)
73+
assertEquals("", dbPathString)
74+
}
75+
6376
@Test
6477
fun memoryPathTest(){
6578
val conf = DatabaseConfiguration(

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

+9-17
Original file line numberDiff line numberDiff line change
@@ -257,17 +257,6 @@ class DatabaseConnectionTest {
257257

258258
assertEquals(1, conn1.longForQuery("select count(*) from test"))
259259

260-
assertFails {
261-
val connFail = man.surpriseMeConnection()
262-
connFail.withTransaction {
263-
it.withStatement("insert into test(num, str)values(?,?)") {
264-
bindLong(1, 232)
265-
bindString(2, "asdf")
266-
executeInsert()
267-
}
268-
}
269-
}
270-
271260
val man2 = createDatabaseManager(
272261
conf
273262
)
@@ -278,20 +267,23 @@ class DatabaseConnectionTest {
278267
bindString(2, "asdf")
279268
executeInsert()
280269
}
270+
it.withStatement("insert into test(num, str)values(?,?)") {
271+
bindLong(1, 233)
272+
bindString(2, "asdfg")
273+
executeInsert()
274+
}
281275
}
282276

283-
assertEquals(1, conn2.longForQuery("select count(*) from test"))
277+
assertEquals(1, conn1.longForQuery("select count(*) from test"))
284278

285279
conn1.close()
286280

287-
assertEquals(1, conn2.longForQuery("select count(*) from test"))
281+
assertEquals(2, conn2.longForQuery("select count(*) from test"))
288282

289283
conn2.close()
290284

291-
assertFails {
292-
man.withConnection {
293-
assertEquals(0, it.longForQuery("select count(*) from test"))
294-
}
285+
man.withConnection {
286+
assertEquals(0, it.longForQuery("select count(*) from test"))
295287
}
296288
}
297289

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

+60
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,66 @@ class DatabaseManagerTest : BaseDatabaseTest(){
8888
assertEquals(1, updateCalled.value)
8989
}
9090

91+
@Test
92+
fun createCalledEachNewConnectionWhenInMemory() {
93+
var creationCount = 0
94+
95+
val conf = DatabaseConfiguration(
96+
name = null,
97+
inMemory = true,
98+
version = 1, create = { db ->
99+
creationCount++
100+
db.withStatement(TWO_COL) {
101+
execute()
102+
}
103+
},
104+
)
105+
106+
val mgr = createDatabaseManager(conf)
107+
mgr.withConnection {
108+
it.rawExecSql("INSERT INTO test(num, str)values(3,'abc')")
109+
assertEquals(1, it.longForQuery("select count(*) from test"))
110+
111+
mgr.withConnection {
112+
assertEquals(0, it.longForQuery("select count(*) from test"))
113+
}
114+
}
115+
116+
assertEquals(2, creationCount)
117+
}
118+
119+
@Test
120+
fun createCalledEachNewConnectionWhenTemporary() {
121+
var creationCount = 0
122+
123+
val conf = DatabaseConfiguration(
124+
name = "",
125+
inMemory = false,
126+
version = 1,
127+
create = { db ->
128+
creationCount++
129+
db.withStatement(TWO_COL) {
130+
execute()
131+
}
132+
},
133+
extendedConfig = DatabaseConfiguration.Extended(
134+
basePath = ""
135+
),
136+
)
137+
138+
val mgr = createDatabaseManager(conf)
139+
mgr.withConnection { conn1 ->
140+
conn1.rawExecSql("INSERT INTO test(num, str)values(3,'abc')")
141+
assertEquals(1, conn1.longForQuery("select count(*) from test"))
142+
143+
mgr.withConnection { conn2 ->
144+
assertEquals(0, conn2.longForQuery("select count(*) from test"))
145+
}
146+
}
147+
148+
assertEquals(2, creationCount)
149+
}
150+
91151
@Test
92152
fun downgradeNotAllowed(){
93153
val upgradeCalled = AtomicInt(0)

0 commit comments

Comments
 (0)