Skip to content

Commit

Permalink
Added support for SQLite.
Browse files Browse the repository at this point in the history
  • Loading branch information
smyrgeorge committed Aug 23, 2024
1 parent 0684f2a commit 621a73f
Show file tree
Hide file tree
Showing 19 changed files with 1,533 additions and 2 deletions.
4 changes: 2 additions & 2 deletions settings.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,5 @@ include("sqlx4k-postgres")
include("sqlx4k-postgres-examples")
include("sqlx4k-mysql")
include("sqlx4k-mysql-examples")
//include("sqlx4k-sqlite")
//include("sqlx4k-sqlite-examples")
include("sqlx4k-sqlite")
include("sqlx4k-sqlite-examples")
28 changes: 28 additions & 0 deletions sqlx4k-sqlite-examples/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
plugins {
kotlin("multiplatform")
}

repositories {
mavenCentral()
}

group = rootProject.group
version = rootProject.version

kotlin {
macosArm64 { binaries { executable() } }
// macosX64 { binaries { executable() } }
// linuxArm64 { binaries { executable() } }
// linuxX64 { binaries { executable() } }

applyDefaultHierarchyTemplate()
sourceSets {
val nativeMain by getting {
dependencies {
implementation(project(":sqlx4k-sqlite"))
// https://github.com/Kotlin/kotlinx.coroutines
implementation("org.jetbrains.kotlinx:kotlinx-coroutines-core:1.8.1")
}
}
}
}
146 changes: 146 additions & 0 deletions sqlx4k-sqlite-examples/src/nativeMain/kotlin/Main.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,146 @@
import io.github.smyrgeorge.sqlx4k.sqlite.Sqlx4k
import io.github.smyrgeorge.sqlx4k.sqlite.Transaction
import io.github.smyrgeorge.sqlx4k.sqlite.impl.SQLite
import io.github.smyrgeorge.sqlx4k.sqlite.impl.errorOrNull
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.IO
import kotlinx.coroutines.async
import kotlinx.coroutines.awaitAll
import kotlinx.coroutines.runBlocking
import kotlinx.coroutines.withContext
import kotlin.coroutines.CoroutineContext
import kotlin.time.measureTime

fun main() {
runBlocking {
suspend fun <A> Iterable<A>.forEachParallel(
context: CoroutineContext = Dispatchers.IO,
f: suspend (A) -> Unit
): Unit = withContext(context) { map { async { f(it) } }.awaitAll() }

val db = SQLite(
database = "test.db",
maxConnections = 10
)

db.query("drop table if exists :table;", mapOf("table" to "sqlx4k")).getOrThrow()
db.query("drop table if exists :table;", mapOf("table" to "sqlx4k")) { _: Any? ->
// Map the value here.
"MAPPED_VALUE"
}.getOrThrow()
val error = db.query("select * from sqlx4kk").errorOrNull()
println(error)

db.query("create table if not exists sqlx4k(id integer);").getOrThrow()
db.query("insert into sqlx4k (id) values (65);").getOrThrow()
db.query("insert into sqlx4k (id) values (66);").getOrThrow()

data class Test(val id: Int)

val r1 = db.fetchAll("select * from sqlx4k;") {
val id: Sqlx4k.Row.Column = get("id")
Test(id = id.value.toInt())
}
println(r1)

val r2 = db.fetchAll("select * from sqlx4k;") {
val id: Sqlx4k.Row.Column = get(0)
Test(id = id.value.toInt())
}
println(r2)
val r3 = db.fetchAll("select * from sqlx4k;") {
val id: Sqlx4k.Row.Column = get(1)
Test(id = id.value.toInt())
}
println(r3)

db.fetchAll("select * from :table;", mapOf("table" to "sqlx4k")) {
val id: Sqlx4k.Row.Column = get("id")
Test(id = id.value.toInt())
}

db.fetchAll("select 1;") {
println(debug())
}

db.fetchAll("select now();") {
println(debug())
}

db.fetchAll("select 'testtest', 'test1';") {
println(debug())
}

println("Connections: ${db.poolSize()}, Idle: ${db.poolIdleSize()}")

println("\n\n\n::: TX :::")
val tx1: Transaction = db.begin().getOrThrow()
println(tx1)
tx1.query("delete from sqlx4k;").getOrThrow()
tx1.fetchAll("select * from sqlx4k;") {
println(debug())
}
db.fetchAll("select * from sqlx4k;") {
println(debug())
}
tx1.commit().getOrThrow()
db.fetchAll("select * from sqlx4k;") {
println(debug())
}

db.query("insert into sqlx4k (id) values (65);").getOrThrow()
db.query("insert into sqlx4k (id) values (66);").getOrThrow()

val test = db.fetchAll("select * from sqlx4k;") {
val id: Sqlx4k.Row.Column = get("id")
Test(id = id.value.toInt())
}
println(test)

val t1 = measureTime {
runBlocking {
(1..20).forEachParallel {
repeat(1_000) {
db.fetchAll("select * from sqlx4k limit 1000;") {
val id: Sqlx4k.Row.Column = get("id")
Test(id = id.value.toInt())
}
db.fetchAll("select * from sqlx4k;") {
val id: Sqlx4k.Row.Column = get("id")
Test(id = id.value.toInt())
}
}
}
}
}
// 4.740002541s
// 4.732109584s
println(t1)

val t2 = measureTime {
runBlocking {
(1..20).forEachParallel {
repeat(1_000) {
val tx2 = db.begin().getOrThrow()
tx2.query("insert into sqlx4k (id) values (65);").getOrThrow()
tx2.query("insert into sqlx4k (id) values (66);").getOrThrow()
tx2.fetchAll("select * from sqlx4k;") {
val id: Sqlx4k.Row.Column = get("id")
Test(id = id.value.toInt())
}
tx2.rollback().getOrThrow()
db.fetchAll("select * from sqlx4k;") {
val id: Sqlx4k.Row.Column = get("id")
Test(id = id.value.toInt())
}
}
}
}
}
// 9.385897375s
// 9.351138833s
println(t2)

println("Connections: ${db.poolSize()}, Idle: ${db.poolIdleSize()}")
}
}
137 changes: 137 additions & 0 deletions sqlx4k-sqlite/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -0,0 +1,137 @@
import com.vanniktech.maven.publish.SonatypeHost
import org.gradle.nativeplatform.platform.internal.DefaultNativePlatform
import org.jetbrains.kotlin.gradle.plugin.mpp.KotlinNativeTarget
import java.lang.System.getenv

plugins {
kotlin("multiplatform")
// https://github.com/vanniktech/gradle-maven-publish-plugin
id("com.vanniktech.maven.publish") version "0.28.0"
}

repositories {
mavenCentral()
}

group = rootProject.group
version = rootProject.version

private val os = DefaultNativePlatform.getCurrentOperatingSystem()
private val arch = DefaultNativePlatform.getCurrentArchitecture()

private val exeExt: String
get() = when {
os.isWindows -> ".exe"
else -> ""
}

private val cargo: String
get() = when {
os.isWindows -> getenv("USERPROFILE")
else -> getenv("HOME")
}?.let(::File)
?.resolve(".cargo/bin/cargo$exeExt")
?.takeIf { it.exists() }
?.absolutePath
?: throw GradleException("Rust cargo binary is required to build project but it wasn't found.")

//val chosenTargets = (properties["targets"] as? String)?.split(",")
// ?: listOf("iosArm64", "androidNativeX64", "macosArm64", "macosX64", "linuxArm64", "linuxX64")

val chosenTargets = (properties["targets"] as? String)?.split(",") ?: listOf("macosArm64")

kotlin {
fun KotlinNativeTarget.rust(target: String) {
compilations.getByName("main").cinterops {
create("librust_lib") {
val cargo = tasks.create("cargo-$target") {
exec {
executable = cargo
args(
"build",
"--manifest-path", projectDir.resolve("rust_lib/Cargo.toml").absolutePath,
"--package", "rust_lib",
"--lib",
"--target=$target",
"--release"
)
}
}

tasks.getByName(interopProcessingTaskName) {
dependsOn(cargo)
}

definitionFile.set(projectDir.resolve("src/nativeInterop/cinterop/$target.def"))
}
}
}

val availableTargets = mapOf(
Pair("iosArm64") { iosArm64 { rust("aarch64-apple-ios") } },
Pair("androidNativeX64") { androidNativeX64 { rust("aarch64-linux-android") } },
Pair("macosArm64") { macosArm64 { rust("aarch64-apple-darwin") } },
Pair("linuxArm64") { linuxArm64 { rust("aarch64-unknown-linux-gnu") } },
Pair("macosX64") { macosX64 { rust("x86_64-apple-darwin") } },
Pair("linuxX64") { linuxX64 { rust("x86_64-unknown-linux-gnu") } },
)
chosenTargets.forEach {
println("Enabling target $it")
availableTargets[it]?.invoke()
}

applyDefaultHierarchyTemplate()
sourceSets {
configureEach {
languageSettings.progressiveMode = true
}
val nativeMain by getting {
dependencies {
// https://github.com/Kotlin/kotlinx.coroutines
implementation("org.jetbrains.kotlinx:kotlinx-coroutines-core:1.8.1")
}
}
}
}

mavenPublishing {
coordinates(
groupId = group as String,
artifactId = name,
version = version as String
)

pom {
name = "sqlx4k-mysql"
description = "A non-blocking MySQL database driver written in Kotlin for the Native platform."
url = "https://github.com/smyrgeorge/sqlx4k"

licenses {
license {
name = "MIT License"
url = "https://github.com/smyrgeorge/sqlx4k/blob/main/LICENSE"
}
}

developers {
developer {
id = "smyrgeorge"
name = "Yorgos S."
email = "smyrgoerge@gmail.com"
url = "https://smyrgeorge.github.io/"
}
}

scm {
url = "https://github.com/smyrgeorge/sqlx4k"
connection = "scm:git:https://github.com/smyrgeorge/sqlx4k.git"
developerConnection = "scm:git:git@github.com:smyrgeorge/sqlx4k.git"
}
}

// Configure publishing to Maven Central
publishToMavenCentral(SonatypeHost.CENTRAL_PORTAL)

// Enable GPG signing for all publications
signAllPublications()
}
Loading

0 comments on commit 621a73f

Please sign in to comment.