forked from osmandapp/OsmAnd
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added OsmAnd Shared Library (kotlin multiplatform)
- Loading branch information
Showing
8 changed files
with
195 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
|
||
plugins { | ||
id("org.jetbrains.kotlin.multiplatform") | ||
id("com.android.library") | ||
} | ||
|
||
kotlin { | ||
androidTarget { | ||
compilations.all { | ||
kotlinOptions { | ||
jvmTarget = "11" | ||
} | ||
} | ||
} | ||
|
||
listOf( | ||
iosX64(), | ||
iosArm64(), | ||
iosSimulatorArm64() | ||
).forEach { iosTarget -> | ||
iosTarget.binaries.framework { | ||
baseName = "OsmAndShared" | ||
isStatic = true | ||
} | ||
} | ||
|
||
val sqliteVersion = "2.3.1" | ||
val sqlDelightVersion = "1.5.4" | ||
|
||
sourceSets { | ||
commonMain.dependencies { | ||
implementation("com.squareup.sqldelight:runtime:$sqlDelightVersion") | ||
} | ||
androidMain.dependencies { | ||
implementation("com.squareup.sqldelight:android-driver:$sqlDelightVersion") | ||
implementation("androidx.sqlite:sqlite-framework:$sqliteVersion") | ||
} | ||
iosMain.dependencies { | ||
implementation("com.squareup.sqldelight:native-driver:$sqlDelightVersion") | ||
} | ||
} | ||
} | ||
|
||
android { | ||
namespace = "net.osmand.shared" | ||
compileSdk = 33 | ||
compileOptions { | ||
sourceCompatibility = JavaVersion.VERSION_11 | ||
targetCompatibility = JavaVersion.VERSION_11 | ||
} | ||
defaultConfig { | ||
minSdk = 23 | ||
} | ||
} |
34 changes: 34 additions & 0 deletions
34
OsmAnd-shared/src/androidMain/kotlin/net/osmand/shared/db/DatabaseDriverFactory.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
package net.osmand.shared.db | ||
|
||
import android.content.Context | ||
import androidx.sqlite.db.SupportSQLiteDatabase | ||
import androidx.sqlite.db.SupportSQLiteOpenHelper.Callback | ||
import androidx.sqlite.db.SupportSQLiteOpenHelper.Configuration | ||
import androidx.sqlite.db.framework.FrameworkSQLiteOpenHelperFactory | ||
import com.squareup.sqldelight.android.AndroidSqliteDriver | ||
import com.squareup.sqldelight.db.SqlDriver | ||
|
||
actual class DatabaseDriverFactory( | ||
private val context: Context, | ||
private val name: String, | ||
private val version: Int | ||
) { | ||
actual fun createDriver(): SqlDriver { | ||
val callback = object: Callback(version) { | ||
override fun onCreate(db: SupportSQLiteDatabase) { | ||
} | ||
|
||
override fun onUpgrade(db: SupportSQLiteDatabase, oldVersion: Int, newVersion: Int) { | ||
} | ||
} | ||
return AndroidSqliteDriver( | ||
FrameworkSQLiteOpenHelperFactory().create( | ||
Configuration.builder(context) | ||
.callback(callback) | ||
.name(name) | ||
.noBackupDirectory(false) | ||
.build() | ||
) | ||
) | ||
} | ||
} |
74 changes: 74 additions & 0 deletions
74
OsmAnd-shared/src/commonMain/kotlin/net.osmand.shared/db/Database.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
package net.osmand.shared.db | ||
|
||
import com.squareup.sqldelight.db.SqlCursor | ||
|
||
class Database @Throws(Exception::class) constructor(databaseDriverFactory: DatabaseDriverFactory) { | ||
private val driver = databaseDriverFactory.createDriver() | ||
|
||
@Throws(Exception::class) | ||
fun execSQL(sql: String) { | ||
try { | ||
driver.execute(null, sql, 0) | ||
} catch (e: Exception) { | ||
throw e | ||
} | ||
} | ||
|
||
@Throws(Exception::class) | ||
fun execSQL(sql: String, parameters: List<Any>) { | ||
try { | ||
driver.execute(null, sql, parameters.size) { | ||
for ((index, param) in parameters.withIndex()) { | ||
val i = index + 1 | ||
when (param) { | ||
is Long -> bindLong(i, param) | ||
is String -> bindString(i, param) | ||
is Double -> bindDouble(i, param) | ||
is ByteArray -> bindBytes(i, param) | ||
else -> throw IllegalArgumentException("Non supported parameter: $param") | ||
} | ||
} | ||
} | ||
} catch (e: Exception) { | ||
throw e | ||
} | ||
} | ||
|
||
@Throws(Exception::class) | ||
fun execSQLQuery(sql: String): SqlCursor { | ||
try { | ||
return driver.executeQuery(null, sql, 0) | ||
} catch (e: Exception) { | ||
throw e | ||
} | ||
} | ||
|
||
@Throws(Exception::class) | ||
fun execSQLQuery(sql: String, parameters: List<Any>): SqlCursor { | ||
try { | ||
return driver.executeQuery(null, sql, parameters.size) { | ||
for ((index, param) in parameters.withIndex()) { | ||
val i = index + 1 | ||
when (param) { | ||
is Long -> bindLong(i, param) | ||
is String -> bindString(i, param) | ||
is Double -> bindDouble(i, param) | ||
is ByteArray -> bindBytes(i, param) | ||
else -> throw IllegalArgumentException("Non supported parameter: $param") | ||
} | ||
} | ||
} | ||
} catch (e: Exception) { | ||
throw e | ||
} | ||
} | ||
|
||
@Throws(Exception::class) | ||
fun close() { | ||
try { | ||
driver.close() | ||
} catch (e: Exception) { | ||
throw e | ||
} | ||
} | ||
} |
7 changes: 7 additions & 0 deletions
7
OsmAnd-shared/src/commonMain/kotlin/net.osmand.shared/db/DatabaseDriverFactory.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
package net.osmand.shared.db | ||
|
||
import com.squareup.sqldelight.db.SqlDriver | ||
|
||
expect class DatabaseDriverFactory { | ||
fun createDriver(): SqlDriver | ||
} |
19 changes: 19 additions & 0 deletions
19
OsmAnd-shared/src/iosMain/kotlin/net/osmand/shared/db/DatabaseDriverFactory.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
package net.osmand.shared.db | ||
|
||
import co.touchlab.sqliter.DatabaseConfiguration | ||
import com.squareup.sqldelight.db.SqlDriver | ||
import com.squareup.sqldelight.drivers.native.NativeSqliteDriver | ||
|
||
actual class DatabaseDriverFactory(private val dbName: String, | ||
private val dbPath: String, | ||
private val version: Int) { | ||
actual fun createDriver(): SqlDriver { | ||
return NativeSqliteDriver( | ||
DatabaseConfiguration( | ||
name = dbName, | ||
version = version, | ||
create = {}, | ||
extendedConfig = DatabaseConfiguration.Extended(basePath = dbPath)) | ||
) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters