From aabc78bd45d8209052ddb159e49544d5bcdb0c8a Mon Sep 17 00:00:00 2001 From: Nicholas Bransby-Williams Date: Tue, 17 Oct 2023 07:22:02 +0100 Subject: [PATCH 1/9] Update NativeDatabase.kt --- .../com/ctrip/sqllin/driver/cinterop/NativeDatabase.kt | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/sqllin-driver/src/nativeMain/kotlin/com/ctrip/sqllin/driver/cinterop/NativeDatabase.kt b/sqllin-driver/src/nativeMain/kotlin/com/ctrip/sqllin/driver/cinterop/NativeDatabase.kt index 1fa92d0..6bf17d8 100644 --- a/sqllin-driver/src/nativeMain/kotlin/com/ctrip/sqllin/driver/cinterop/NativeDatabase.kt +++ b/sqllin-driver/src/nativeMain/kotlin/com/ctrip/sqllin/driver/cinterop/NativeDatabase.kt @@ -24,6 +24,7 @@ import com.ctrip.sqllin.sqlite3.SQLITE_DBCONFIG_LOOKASIDE import com.ctrip.sqllin.sqlite3.SQLITE_OK import com.ctrip.sqllin.sqlite3.SQLITE_OPEN_CREATE import com.ctrip.sqllin.sqlite3.SQLITE_OPEN_READWRITE +import com.ctrip.sqllin.sqlite3.SQLITE_OPEN_READONLY import com.ctrip.sqllin.sqlite3.SQLITE_OPEN_URI import com.ctrip.sqllin.sqlite3.sqlite3_busy_timeout import com.ctrip.sqllin.sqlite3.sqlite3_close_v2 @@ -45,11 +46,11 @@ internal class NativeDatabase private constructor(val dbPointer: CPointer>() - val openResult = sqlite3_open_v2(realPath, dbPtr.ptr, sqliteFlags, null) + val openResult = sqlite3_open_v2(realPath, dbPtr.ptr, SQLITE_OPEN_URI or sqliteFlags, null) if (openResult != SQLITE_OK) { throw sqliteException(sqlite3_errmsg(dbPtr.value)?.toKString() ?: "", openResult) } From 3f6d35b2c262add529bc365ba3876b5bfd2f44d8 Mon Sep 17 00:00:00 2001 From: Nicholas Bransby-Williams Date: Tue, 17 Oct 2023 07:48:04 +0100 Subject: [PATCH 2/9] Update NativeDatabase.kt --- .../com/ctrip/sqllin/driver/cinterop/NativeDatabase.kt | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/sqllin-driver/src/nativeMain/kotlin/com/ctrip/sqllin/driver/cinterop/NativeDatabase.kt b/sqllin-driver/src/nativeMain/kotlin/com/ctrip/sqllin/driver/cinterop/NativeDatabase.kt index 6bf17d8..ef5cfb3 100644 --- a/sqllin-driver/src/nativeMain/kotlin/com/ctrip/sqllin/driver/cinterop/NativeDatabase.kt +++ b/sqllin-driver/src/nativeMain/kotlin/com/ctrip/sqllin/driver/cinterop/NativeDatabase.kt @@ -46,13 +46,16 @@ internal class NativeDatabase private constructor(val dbPointer: CPointer>() - val openResult = sqlite3_open_v2(realPath, dbPtr.ptr, SQLITE_OPEN_URI or sqliteFlags, null) + val openResult = sqlite3_open_v2(realPath, dbPtr.ptr, sqliteFlags, null) if (openResult != SQLITE_OK) { - throw sqliteException(sqlite3_errmsg(dbPtr.value)?.toKString() ?: "", openResult) + val openCreateResult = sqlite3_open_v2(realPath, dbPtr.ptr, sqliteFlags or SQLITE_OPEN_CREATE, null) + if (openCreateResult != SQLITE_OK) { + throw sqliteException(sqlite3_errmsg(dbPtr.value)?.toKString() ?: "", openCreateResult) + } } dbPtr.value!! } From 9c5ae439fde08037960cf154fd74f4f55995f30d Mon Sep 17 00:00:00 2001 From: Nicholas Bransby-Williams Date: Tue, 17 Oct 2023 07:58:50 +0100 Subject: [PATCH 3/9] Update NativeDatabase.kt --- .../com/ctrip/sqllin/driver/cinterop/NativeDatabase.kt | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/sqllin-driver/src/nativeMain/kotlin/com/ctrip/sqllin/driver/cinterop/NativeDatabase.kt b/sqllin-driver/src/nativeMain/kotlin/com/ctrip/sqllin/driver/cinterop/NativeDatabase.kt index ef5cfb3..92df542 100644 --- a/sqllin-driver/src/nativeMain/kotlin/com/ctrip/sqllin/driver/cinterop/NativeDatabase.kt +++ b/sqllin-driver/src/nativeMain/kotlin/com/ctrip/sqllin/driver/cinterop/NativeDatabase.kt @@ -46,13 +46,14 @@ internal class NativeDatabase private constructor(val dbPointer: CPointer>() + val sqliteCreateFlags = SQLITE_OPEN_READWRITE or SQLITE_OPEN_CREATE or SQLITE_OPEN_URI + val sqliteOpenFlags = if(configuration.isReadOnly) SQLITE_OPEN_READONLY or SQLITE_OPEN_URI else sqliteCreateFlags val openResult = sqlite3_open_v2(realPath, dbPtr.ptr, sqliteFlags, null) if (openResult != SQLITE_OK) { - val openCreateResult = sqlite3_open_v2(realPath, dbPtr.ptr, sqliteFlags or SQLITE_OPEN_CREATE, null) + val openCreateResult = sqlite3_open_v2(realPath, dbPtr.ptr, sqliteCreateFlags, null) if (openCreateResult != SQLITE_OK) { throw sqliteException(sqlite3_errmsg(dbPtr.value)?.toKString() ?: "", openCreateResult) } From cf0cff65248a98e611a7d296bcdaf784875630ac Mon Sep 17 00:00:00 2001 From: Nicholas Bransby-Williams Date: Tue, 17 Oct 2023 08:02:21 +0100 Subject: [PATCH 4/9] Update NativeDatabase.kt --- .../kotlin/com/ctrip/sqllin/driver/cinterop/NativeDatabase.kt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/sqllin-driver/src/nativeMain/kotlin/com/ctrip/sqllin/driver/cinterop/NativeDatabase.kt b/sqllin-driver/src/nativeMain/kotlin/com/ctrip/sqllin/driver/cinterop/NativeDatabase.kt index 92df542..5f96d79 100644 --- a/sqllin-driver/src/nativeMain/kotlin/com/ctrip/sqllin/driver/cinterop/NativeDatabase.kt +++ b/sqllin-driver/src/nativeMain/kotlin/com/ctrip/sqllin/driver/cinterop/NativeDatabase.kt @@ -50,7 +50,7 @@ internal class NativeDatabase private constructor(val dbPointer: CPointer>() val sqliteCreateFlags = SQLITE_OPEN_READWRITE or SQLITE_OPEN_CREATE or SQLITE_OPEN_URI - val sqliteOpenFlags = if(configuration.isReadOnly) SQLITE_OPEN_READONLY or SQLITE_OPEN_URI else sqliteCreateFlags + val sqliteFlags = if(configuration.isReadOnly) SQLITE_OPEN_READONLY or SQLITE_OPEN_URI else sqliteCreateFlags val openResult = sqlite3_open_v2(realPath, dbPtr.ptr, sqliteFlags, null) if (openResult != SQLITE_OK) { val openCreateResult = sqlite3_open_v2(realPath, dbPtr.ptr, sqliteCreateFlags, null) From 629b6f777aacc5a62aa6b4b6d196590d802716fb Mon Sep 17 00:00:00 2001 From: Nicholas Bransby-Williams Date: Tue, 17 Oct 2023 08:04:03 +0100 Subject: [PATCH 5/9] Update NativeDatabase.kt --- .../kotlin/com/ctrip/sqllin/driver/cinterop/NativeDatabase.kt | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/sqllin-driver/src/nativeMain/kotlin/com/ctrip/sqllin/driver/cinterop/NativeDatabase.kt b/sqllin-driver/src/nativeMain/kotlin/com/ctrip/sqllin/driver/cinterop/NativeDatabase.kt index 5f96d79..cf6aa9c 100644 --- a/sqllin-driver/src/nativeMain/kotlin/com/ctrip/sqllin/driver/cinterop/NativeDatabase.kt +++ b/sqllin-driver/src/nativeMain/kotlin/com/ctrip/sqllin/driver/cinterop/NativeDatabase.kt @@ -46,11 +46,11 @@ internal class NativeDatabase private constructor(val dbPointer: CPointer>() - val sqliteCreateFlags = SQLITE_OPEN_READWRITE or SQLITE_OPEN_CREATE or SQLITE_OPEN_URI - val sqliteFlags = if(configuration.isReadOnly) SQLITE_OPEN_READONLY or SQLITE_OPEN_URI else sqliteCreateFlags val openResult = sqlite3_open_v2(realPath, dbPtr.ptr, sqliteFlags, null) if (openResult != SQLITE_OK) { val openCreateResult = sqlite3_open_v2(realPath, dbPtr.ptr, sqliteCreateFlags, null) From 34514b4af9b86812fd80622f86592c126817d8f4 Mon Sep 17 00:00:00 2001 From: Nicholas Bransby-Williams Date: Tue, 17 Oct 2023 12:07:08 +0100 Subject: [PATCH 6/9] Update NativeDatabase.kt --- .../ctrip/sqllin/driver/cinterop/NativeDatabase.kt | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/sqllin-driver/src/nativeMain/kotlin/com/ctrip/sqllin/driver/cinterop/NativeDatabase.kt b/sqllin-driver/src/nativeMain/kotlin/com/ctrip/sqllin/driver/cinterop/NativeDatabase.kt index cf6aa9c..87ad350 100644 --- a/sqllin-driver/src/nativeMain/kotlin/com/ctrip/sqllin/driver/cinterop/NativeDatabase.kt +++ b/sqllin-driver/src/nativeMain/kotlin/com/ctrip/sqllin/driver/cinterop/NativeDatabase.kt @@ -46,17 +46,18 @@ internal class NativeDatabase private constructor(val dbPointer: CPointer>() + if(configuration.isReadOnly) { + //from SQLITE_OPEN_READWRITE docs: if opening in read-write mode fails due to OS-level permissions, an attempt is made to open it in read-only mode + val openResult = sqlite3_open_v2(realPath, dbPtr.ptr, SQLITE_OPEN_READWRITE or SQLITE_OPEN_URI, null) + if (openResult == SQLITE_OK) return@memScoped dbPtr.value!! + } val openResult = sqlite3_open_v2(realPath, dbPtr.ptr, sqliteFlags, null) if (openResult != SQLITE_OK) { - val openCreateResult = sqlite3_open_v2(realPath, dbPtr.ptr, sqliteCreateFlags, null) - if (openCreateResult != SQLITE_OK) { - throw sqliteException(sqlite3_errmsg(dbPtr.value)?.toKString() ?: "", openCreateResult) - } + throw sqliteException(sqlite3_errmsg(dbPtr.value)?.toKString() ?: "", openResult) } dbPtr.value!! } From e68478b934dadf0920c6f80d0356e74ebb54e327 Mon Sep 17 00:00:00 2001 From: Nicholas Bransby-Williams Date: Tue, 17 Oct 2023 12:07:32 +0100 Subject: [PATCH 7/9] Update NativeDatabase.kt --- .../kotlin/com/ctrip/sqllin/driver/cinterop/NativeDatabase.kt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/sqllin-driver/src/nativeMain/kotlin/com/ctrip/sqllin/driver/cinterop/NativeDatabase.kt b/sqllin-driver/src/nativeMain/kotlin/com/ctrip/sqllin/driver/cinterop/NativeDatabase.kt index 87ad350..997b61e 100644 --- a/sqllin-driver/src/nativeMain/kotlin/com/ctrip/sqllin/driver/cinterop/NativeDatabase.kt +++ b/sqllin-driver/src/nativeMain/kotlin/com/ctrip/sqllin/driver/cinterop/NativeDatabase.kt @@ -57,7 +57,7 @@ internal class NativeDatabase private constructor(val dbPointer: CPointer Date: Tue, 17 Oct 2023 12:10:40 +0100 Subject: [PATCH 8/9] Update NativeDatabase.kt --- .../kotlin/com/ctrip/sqllin/driver/cinterop/NativeDatabase.kt | 1 - 1 file changed, 1 deletion(-) diff --git a/sqllin-driver/src/nativeMain/kotlin/com/ctrip/sqllin/driver/cinterop/NativeDatabase.kt b/sqllin-driver/src/nativeMain/kotlin/com/ctrip/sqllin/driver/cinterop/NativeDatabase.kt index 997b61e..e6e7990 100644 --- a/sqllin-driver/src/nativeMain/kotlin/com/ctrip/sqllin/driver/cinterop/NativeDatabase.kt +++ b/sqllin-driver/src/nativeMain/kotlin/com/ctrip/sqllin/driver/cinterop/NativeDatabase.kt @@ -24,7 +24,6 @@ import com.ctrip.sqllin.sqlite3.SQLITE_DBCONFIG_LOOKASIDE import com.ctrip.sqllin.sqlite3.SQLITE_OK import com.ctrip.sqllin.sqlite3.SQLITE_OPEN_CREATE import com.ctrip.sqllin.sqlite3.SQLITE_OPEN_READWRITE -import com.ctrip.sqllin.sqlite3.SQLITE_OPEN_READONLY import com.ctrip.sqllin.sqlite3.SQLITE_OPEN_URI import com.ctrip.sqllin.sqlite3.sqlite3_busy_timeout import com.ctrip.sqllin.sqlite3.sqlite3_close_v2 From 3013a608350ae0a93d4a7e7c31e9eb6646c69e1c Mon Sep 17 00:00:00 2001 From: Nicholas Bransby-Williams Date: Tue, 17 Oct 2023 12:13:12 +0100 Subject: [PATCH 9/9] Update NativeDatabase.kt --- .../kotlin/com/ctrip/sqllin/driver/cinterop/NativeDatabase.kt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/sqllin-driver/src/nativeMain/kotlin/com/ctrip/sqllin/driver/cinterop/NativeDatabase.kt b/sqllin-driver/src/nativeMain/kotlin/com/ctrip/sqllin/driver/cinterop/NativeDatabase.kt index e6e7990..9f18baa 100644 --- a/sqllin-driver/src/nativeMain/kotlin/com/ctrip/sqllin/driver/cinterop/NativeDatabase.kt +++ b/sqllin-driver/src/nativeMain/kotlin/com/ctrip/sqllin/driver/cinterop/NativeDatabase.kt @@ -50,7 +50,7 @@ internal class NativeDatabase private constructor(val dbPointer: CPointer>() if(configuration.isReadOnly) { - //from SQLITE_OPEN_READWRITE docs: if opening in read-write mode fails due to OS-level permissions, an attempt is made to open it in read-only mode + //from sqlite3_open_v2 docs: if opening in read-write mode fails due to OS-level permissions, an attempt is made to open it in read-only mode val openResult = sqlite3_open_v2(realPath, dbPtr.ptr, SQLITE_OPEN_READWRITE or SQLITE_OPEN_URI, null) if (openResult == SQLITE_OK) return@memScoped dbPtr.value!! }