From c784f40c5559661aae97c08ede4d737126d6e4b8 Mon Sep 17 00:00:00 2001 From: Alinson Xavier Date: Sun, 17 May 2020 13:27:51 -0500 Subject: [PATCH] Remove custom iosTest task; fix IosDatabase --- core/build.gradle | 27 +++++-------------- .../ios/org/isoron/platform/io/IosDatabase.kt | 23 +++++++++------- 2 files changed, 21 insertions(+), 29 deletions(-) diff --git a/core/build.gradle b/core/build.gradle index 8794edcab..0db23b98a 100644 --- a/core/build.gradle +++ b/core/build.gradle @@ -122,24 +122,11 @@ kotlin { } } -// task iosTestCopyResources(type: Copy) { -// dependsOn 'linkTestIos' -// from 'assets/test/' -// from 'assets/main/' -// into 'build/bin/ios/testDebugExecutable' -// } -// -// task iosTestCustom { -// dependsOn 'linkTestIos', 'iosTestCopyResources' -// group = JavaBasePlugin.VERIFICATION_GROUP -// description = "Runs tests on iOS simulator" -// -// doLast { -// def emulatorName = "iPhone 8 Plus" -// def binary = kotlin.targets.ios.compilations.test.getBinary('EXECUTABLE', 'DEBUG') -// exec { -// commandLine 'xcrun', 'simctl', 'spawn', emulatorName, binary.absolutePath -// } -// } -// } + task iosTestCopyResources(type: Copy) { + from 'assets/test/' + from 'assets/main/' + into 'build/bin/ios/debugTest' + } + + iosTest.dependsOn(iosTestCopyResources) } \ No newline at end of file diff --git a/core/src/main/ios/org/isoron/platform/io/IosDatabase.kt b/core/src/main/ios/org/isoron/platform/io/IosDatabase.kt index 2f3501bc1..34a12376f 100644 --- a/core/src/main/ios/org/isoron/platform/io/IosDatabase.kt +++ b/core/src/main/ios/org/isoron/platform/io/IosDatabase.kt @@ -20,19 +20,25 @@ package org.isoron.platform.io import kotlinx.cinterop.* +import platform.Foundation.* import sqlite3.* fun sqlite3_errstr(db: CPointer): String { return "SQLite3 error: " + sqlite3_errmsg(db).toString() } +@Suppress("CAST_NEVER_SUCCEEDS") class IosDatabaseOpener : DatabaseOpener { override fun open(file: UserFile): Database = memScoped { - val db = alloc>() val path = (file as IosFile).path - if (sqlite3_open(path, db.ptr) != SQLITE_OK) { - throw Exception(sqlite3_errstr(db.value!!)) - } + val dirname = (path as NSString).stringByDeletingLastPathComponent + NSFileManager.defaultManager.createDirectoryAtPath(dirname, true, null, null) + + val db = alloc>() + val result = sqlite3_open(path, db.ptr) + if (result != SQLITE_OK) + throw Exception("sqlite3_open failed (code $result)") + return IosDatabase(db.value!!) } } @@ -41,9 +47,9 @@ class IosDatabase(val db: CPointer) : Database { override fun prepareStatement(sql: String): PreparedStatement = memScoped { if (sql.isEmpty()) throw Exception("empty SQL query") val stmt = alloc>() - if (sqlite3_prepare_v2(db, sql.cstr, -1, stmt.ptr, null) != SQLITE_OK) { - throw Exception(sqlite3_errstr(db)) - } + val result = sqlite3_prepare_v2(db, sql.cstr, -1, stmt.ptr, null) + if (result != SQLITE_OK) + throw Exception("sqlite3_prepare_v2 failed (code $result)") return IosPreparedStatement(db, stmt.value!!) } override fun close() { @@ -54,8 +60,7 @@ class IosDatabase(val db: CPointer) : Database { class IosPreparedStatement(val db: CPointer, val stmt: CPointer) : PreparedStatement { override fun step(): StepResult { - val result = sqlite3_step(stmt) - when (result) { + when (sqlite3_step(stmt)) { SQLITE_ROW -> return StepResult.ROW SQLITE_DONE -> return StepResult.DONE else -> throw Exception(sqlite3_errstr(db))