Skip to content

Commit ea89605

Browse files
committed
SQLiter version 0.5.10
1 parent 2900643 commit ea89605

File tree

10 files changed

+77
-42
lines changed

10 files changed

+77
-42
lines changed

SQLager/build.gradle

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@ kotlin {
6464
dependencies {
6565
implementation 'org.jetbrains.kotlin:kotlin-test-common'
6666
implementation 'org.jetbrains.kotlin:kotlin-test-annotations-common'
67+
implementation "co.touchlab:testhelp"
6768
}
6869
}
6970
/*jvmMain {

SQLager/gradle.properties

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,11 @@
1717
kotlin.code.style=official
1818

1919
GROUP=co.touchlab
20-
VERSION_NAME=0.1.1-SNAPSHOT
20+
VERSION_NAME=0.1.2-SNAPSHOT
2121

2222
STATELY_VERSION=0.5.1
23-
SQLITER_VERSION=0.5.7-SNAPSHOT
23+
TESTHELP_VERSION=0.1.0
24+
SQLITER_VERSION=0.5.9
2425
KOTLIN_VERSION=1.3.10
2526

2627
POM_URL=https://github.com/touchlab/SQLiter

SQLiter/gradle.properties

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
kotlin.code.style=official
1818

1919
GROUP=co.touchlab
20-
VERSION_NAME=0.5.9
20+
VERSION_NAME=0.5.10
2121

2222
STATELY_VERSION=0.5.1
2323
KOTLIN_VERSION=1.3.11
@@ -40,4 +40,7 @@ POM_DEVELOPER_NAME=Kevin Galligan
4040
POM_DEVELOPER_ORG=Kevin Galligan
4141
POM_DEVELOPER_URL=https://touchlab.co/
4242

43-
BINTRAY_VERSION=1.8.3-kotlin-a1
43+
BINTRAY_VERSION=1.8.3-kotlin-a1
44+
45+
org.gradle.internal.http.socketTimeout=120000
46+
org.gradle.internal.http.connectionTimeout=120000

SQLiter/src/commonMain/kotlin/co/touchlab/sqliter/DatabaseConfiguration.kt

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -16,27 +16,29 @@
1616

1717
package co.touchlab.sqliter
1818

19-
import co.touchlab.sqliter.Constants.validDatabaseName
20-
2119
data class DatabaseConfiguration(
2220

23-
val name:String,
24-
val version:Int,
25-
val create:(DatabaseConnection)->Unit,
26-
val upgrade:(DatabaseConnection, Int, Int)->Unit = {_,_,_->},
21+
val name: String,
22+
val version: Int,
23+
val create: (DatabaseConnection) -> Unit,
24+
val upgrade: (DatabaseConnection, Int, Int) -> Unit = { _, _, _ -> },
2725
val journalMode: JournalMode = JournalMode.WAL,
28-
val busyTimeout:Int = 2500,
29-
val pageSize:Int? = null,
30-
val inMemory:Boolean = false
31-
){
26+
val busyTimeout: Int = 2500,
27+
val pageSize: Int? = null,
28+
val inMemory: Boolean = false,
29+
val basePath: String? = null
30+
) {
3231
init {
33-
if(!validDatabaseName.matches(name))
34-
throw IllegalArgumentException("Database name $name not valid. Only letters, numbers, dash and underscore allowed")
32+
checkFilename(name)
3533
}
3634
}
3735

38-
object Constants{
39-
val validDatabaseName = "[A-Za-z0-9\\-_.]+".toRegex()
36+
private fun checkFilename(name: String) {
37+
if (name.contains("/")) {
38+
throw IllegalArgumentException(
39+
"File $name contains a path separator"
40+
)
41+
}
4042
}
4143

4244
enum class JournalMode {

SQLiter/src/commonMain/kotlin/co/touchlab/sqliter/DatabaseFileContext.kt

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,5 +17,9 @@
1717
package co.touchlab.sqliter
1818

1919
expect object DatabaseFileContext{
20-
fun deleteDatabase(name: String)
20+
fun deleteDatabase(name: String, basePath:String? = null)
21+
}
22+
23+
fun DatabaseFileContext.deleteDatabase(configuration: DatabaseConfiguration){
24+
deleteDatabase(configuration.name, configuration.basePath)
2125
}

SQLiter/src/jvmMain/kotlin/co/touchlab/sqliter/DatabaseFileContext.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,5 +17,5 @@
1717
package co.touchlab.sqliter
1818

1919
actual object DatabaseFileContext {
20-
actual fun deleteDatabase(name: String) {}
20+
actual fun deleteDatabase(name: String, basePath:String?) {}
2121
}

SQLiter/src/nativeCommonMain/kotlin/co/touchlab/sqliter/NativeFileContext.kt

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -24,15 +24,15 @@ import platform.Foundation.NSSearchPathForDirectoriesInDomains
2424
import platform.Foundation.NSUserDomainMask
2525

2626
actual object DatabaseFileContext{
27-
actual fun deleteDatabase(name: String) {
28-
deleteDatabaseFile(databaseFile(name))
27+
actual fun deleteDatabase(name: String, basePath:String?) {
28+
deleteDatabaseFile(databaseFile(name, basePath))
2929
}
3030

31-
internal fun databasePath(databaseName:String, inMemory:Boolean):String{
31+
internal fun databasePath(databaseName:String, inMemory:Boolean, datapathPath:String?):String{
3232
return if(inMemory){
3333
"file:$databaseName?mode=memory&cache=shared"
3434
}else{
35-
File(databaseDirPath(), databaseName).path
35+
databaseFile(databaseName, datapathPath).path
3636
}
3737
}
3838

@@ -52,7 +52,7 @@ actual object DatabaseFileContext{
5252
return databaseDirectory
5353
}
5454

55-
internal fun databaseFile(databaseName:String):File = File(databaseDirPath(), databaseName)
55+
internal fun databaseFile(databaseName:String, datapathPath:String?):File = File(datapathPath?:databaseDirPath(), databaseName)
5656

5757
internal fun deleteDatabaseFile(file:File):Boolean {
5858
var deleted = false

SQLiter/src/nativeCommonMain/kotlin/co/touchlab/sqliter/createDatabaseManager.kt

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

1919
fun createDatabaseManager(configuration: DatabaseConfiguration): DatabaseManager {
20-
val databasePath = DatabaseFileContext.databasePath(configuration.name, configuration.inMemory)
20+
val databasePath = DatabaseFileContext.databasePath(configuration.name, configuration.inMemory, configuration.basePath)
2121
return NativeDatabaseManager(databasePath, configuration)
2222
}

SQLiter/src/nativeCommonTest/kotlin/co/touchlab/sqliter/DatabaseConfigurationTest.kt

Lines changed: 39 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -16,42 +16,66 @@
1616

1717
package co.touchlab.sqliter
1818

19+
import platform.Foundation.NSFileManager
1920
import kotlin.test.*
20-
import co.touchlab.sqliter.Constants.validDatabaseName
2121

2222
class DatabaseConfigurationTest : BaseDatabaseTest(){
2323

2424
@Test
2525
fun pathTest(){
26-
val dbPathString = DatabaseFileContext.databasePath(TEST_DB_NAME, false)
26+
val dbPathString = DatabaseFileContext.databasePath(TEST_DB_NAME, false, null)
2727
assertTrue(dbPathString.endsWith(TEST_DB_NAME))
2828
}
2929

3030
@Test
3131
fun memoryPathTest(){
32-
val dbPathString = DatabaseFileContext.databasePath(TEST_DB_NAME, true)
32+
val dbPathString = DatabaseFileContext.databasePath(TEST_DB_NAME, true, null)
3333
assertEquals("file:$TEST_DB_NAME?mode=memory&cache=shared", dbPathString)
3434
}
3535

36+
fun checkFilePath(name: String, path: String?) {
37+
var conn: DatabaseConnection? = null
38+
val config = DatabaseConfiguration(name = name, basePath = path, version = 1, create = { db ->
39+
db.withStatement(TWO_COL) {
40+
execute()
41+
}
42+
})
43+
44+
try {
45+
val expectedPath = DatabaseFileContext.databaseFile(name, path)
46+
val manager = createDatabaseManager(config)
47+
48+
conn = manager.createMultiThreadedConnection()
49+
50+
assertTrue(expectedPath.exists())
51+
} finally {
52+
conn?.close()
53+
DatabaseFileContext.deleteDatabase(config)
54+
}
55+
}
56+
3657
@Test
37-
fun validDatabaseName(){
38-
assertTrue(validDatabaseName.matches("absdf"))
39-
assertTrue(validDatabaseName.matches("abs4f"))
40-
assertTrue(validDatabaseName.matches("abWWs4f"))
41-
assertTrue(validDatabaseName.matches("_-abWWs4f"))
42-
assertTrue(validDatabaseName.matches("_-ab.WWs4f"))
43-
assertFalse(validDatabaseName.matches("_-a bWWs4f"))
44-
assertFalse(validDatabaseName.matches("_-a ~bWWs4f"))
45-
assertFalse(validDatabaseName.matches("_-a ~bWWs4f"))
58+
fun noSlashInName(){
59+
assertFails {
60+
checkFilePath("arst/qwfp", null)
61+
}
4662
}
4763

4864
@Test
49-
fun invalidDatabaseNameFailsConfig() {
50-
DatabaseConfiguration("asdf", 1, {})
51-
assertFails { DatabaseConfiguration("as df", 1, {}) }
65+
fun basicDbNameWorks(){
66+
checkFilePath("arst", null)
5267
}
5368

69+
@Test
70+
fun nameWithSpace(){
71+
checkFilePath("ar st", null)
72+
}
5473

74+
@Test
75+
fun dbWithBasePath(){
76+
val basePath = DatabaseFileContext.iosDirPath("notdatabases")
77+
checkFilePath("arst", basePath)
78+
}
5579

5680
@Test
5781
fun journalModeSetting()

SQLiter/src/nativeCommonTest/kotlin/co/touchlab/sqliter/DatabaseConnectionTest.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -323,7 +323,7 @@ class DatabaseConnectionTest {
323323
}
324324
}
325325

326-
dbFileExists = DatabaseFileContext.databaseFile(memoryName).exists()
326+
dbFileExists = DatabaseFileContext.databaseFile(memoryName, null).exists()
327327
}
328328
} finally {
329329
DatabaseFileContext.deleteDatabase(memoryName)

0 commit comments

Comments
 (0)