also looking at this
feat(android): dbExecute and dbQuery in Zig — ratchet 84 → 82
#160
7 files
+541
-3
| @@ -2,6 +2,7 @@ package com.craft.runtime | ||
| 2 | 2 | |
| 3 | 3 | import android.app.Activity |
| 4 | 4 | import android.content.SharedPreferences |
| 5 | import android.database.sqlite.SQLiteDatabase | |
| 5 | 6 | import org.json.JSONObject |
| 6 | 7 | |
| 7 | 8 | /** |
| @@ -110,6 +111,8 @@ object CraftNative { | ||
| 110 | 111 | private external fun nativeDeleteCalendarEvent(activity: Activity, eventId: String): Boolean |
| 111 | 112 | private external fun nativeGetCalendarEvents(activity: Activity, startDateMs: Long, endDateMs: Long): Boolean |
| 112 | 113 | private external fun nativeCreateCalendarEvent(activity: Activity, eventJson: String): Boolean |
| 114 | private external fun nativeDbExecute(database: SQLiteDatabase, sql: String, paramsJson: String): Boolean | |
| 115 | private external fun nativeDbQuery(database: SQLiteDatabase, sql: String, paramsJson: String): Boolean | |
| 113 | 116 | |
| 114 | 117 | /** |
| 115 | 118 | * `getDeviceInfo`, or null to use the Kotlin implementation. |
| @@ -385,4 +388,31 @@ object CraftNative { | ||
| 385 | 388 | false |
| 386 | 389 | } |
| 387 | 390 | } |
| 391 | ||
| 392 | /** | |
| 393 | * Returns whether Zig took the statement. | |
| 394 | * | |
| 395 | * The connection is passed in rather than opened: the Kotlin caches one | |
| 396 | * `SQLiteDatabase` so that a BEGIN in one call and a COMMIT in the next | |
| 397 | * reach the same connection, and a second one opened by Zig would take | |
| 398 | * locks against the first for no visible reason. | |
| 399 | */ | |
| 400 | fun dbExecute(database: SQLiteDatabase, sql: String, paramsJson: String): Boolean { | |
| 401 | if (!isAvailable) return false | |
| 402 | return try { | |
| 403 | nativeDbExecute(database, sql, paramsJson) | |
| 404 | } catch (e: UnsatisfiedLinkError) { | |
| 405 | false | |
| 406 | } | |
| 407 | } | |
| 408 | ||
| 409 | /** The same, for a statement that returns rows. */ | |
| 410 | fun dbQuery(database: SQLiteDatabase, sql: String, paramsJson: String): Boolean { | |
| 411 | if (!isAvailable) return false | |
| 412 | return try { | |
| 413 | nativeDbQuery(database, sql, paramsJson) | |
| 414 | } catch (e: UnsatisfiedLinkError) { | |
| 415 | false | |
| 416 | } | |
| 417 | } | |
| 388 | 418 | } |