@capawesome/capacitor-libsql 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (53) hide show
  1. package/Package.swift +30 -0
  2. package/README.md +301 -0
  3. package/android/build.gradle +66 -0
  4. package/android/src/main/AndroidManifest.xml +2 -0
  5. package/android/src/main/java/io/capawesome/capacitorjs/plugins/libsql/Libsql.kt +188 -0
  6. package/android/src/main/java/io/capawesome/capacitorjs/plugins/libsql/LibsqlPlugin.java +217 -0
  7. package/android/src/main/java/io/capawesome/capacitorjs/plugins/libsql/classes/options/BeginTransactionOptions.java +28 -0
  8. package/android/src/main/java/io/capawesome/capacitorjs/plugins/libsql/classes/options/CommitTransactionOptions.java +46 -0
  9. package/android/src/main/java/io/capawesome/capacitorjs/plugins/libsql/classes/options/ConnectOptions.java +37 -0
  10. package/android/src/main/java/io/capawesome/capacitorjs/plugins/libsql/classes/options/ExecuteBatchOptions.java +103 -0
  11. package/android/src/main/java/io/capawesome/capacitorjs/plugins/libsql/classes/options/ExecuteOptions.java +82 -0
  12. package/android/src/main/java/io/capawesome/capacitorjs/plugins/libsql/classes/options/QueryOptions.java +82 -0
  13. package/android/src/main/java/io/capawesome/capacitorjs/plugins/libsql/classes/options/RollbackTransactionOptions.java +46 -0
  14. package/android/src/main/java/io/capawesome/capacitorjs/plugins/libsql/classes/options/SyncOptions.java +28 -0
  15. package/android/src/main/java/io/capawesome/capacitorjs/plugins/libsql/classes/results/BeginTransactionResult.java +23 -0
  16. package/android/src/main/java/io/capawesome/capacitorjs/plugins/libsql/classes/results/ConnectResult.java +23 -0
  17. package/android/src/main/java/io/capawesome/capacitorjs/plugins/libsql/classes/results/QueryResult.java +47 -0
  18. package/android/src/main/java/io/capawesome/capacitorjs/plugins/libsql/interfaces/Callback.java +5 -0
  19. package/android/src/main/java/io/capawesome/capacitorjs/plugins/libsql/interfaces/EmptyCallback.java +5 -0
  20. package/android/src/main/java/io/capawesome/capacitorjs/plugins/libsql/interfaces/NonEmptyCallback.java +7 -0
  21. package/android/src/main/java/io/capawesome/capacitorjs/plugins/libsql/interfaces/NonEmptyResultCallback.java +7 -0
  22. package/android/src/main/java/io/capawesome/capacitorjs/plugins/libsql/interfaces/Result.java +7 -0
  23. package/android/src/main/res/.gitkeep +0 -0
  24. package/dist/docs.json +684 -0
  25. package/dist/esm/definitions.d.ts +287 -0
  26. package/dist/esm/definitions.js +2 -0
  27. package/dist/esm/definitions.js.map +1 -0
  28. package/dist/esm/index.d.ts +4 -0
  29. package/dist/esm/index.js +7 -0
  30. package/dist/esm/index.js.map +1 -0
  31. package/dist/esm/web.d.ts +12 -0
  32. package/dist/esm/web.js +36 -0
  33. package/dist/esm/web.js.map +1 -0
  34. package/dist/plugin.cjs.js +50 -0
  35. package/dist/plugin.cjs.js.map +1 -0
  36. package/dist/plugin.js +53 -0
  37. package/dist/plugin.js.map +1 -0
  38. package/ios/Plugin/Classes/Options/BeginTransactionOptions.swift +14 -0
  39. package/ios/Plugin/Classes/Options/CommitTransactionOptions.swift +19 -0
  40. package/ios/Plugin/Classes/Options/ConnectOptions.swift +24 -0
  41. package/ios/Plugin/Classes/Options/ExecuteBatchOptions.swift +41 -0
  42. package/ios/Plugin/Classes/Options/ExecuteOptions.swift +23 -0
  43. package/ios/Plugin/Classes/Options/QueryOptions.swift +23 -0
  44. package/ios/Plugin/Classes/Options/RollbackTransactionOptions.swift +19 -0
  45. package/ios/Plugin/Classes/Options/SyncOptions.swift +14 -0
  46. package/ios/Plugin/Classes/Results/BeginTransactionResult.swift +16 -0
  47. package/ios/Plugin/Classes/Results/ConnectResult.swift +16 -0
  48. package/ios/Plugin/Classes/Results/QueryResult.swift +36 -0
  49. package/ios/Plugin/Enums/LibsqlError.swift +61 -0
  50. package/ios/Plugin/Libsql.swift +219 -0
  51. package/ios/Plugin/LibsqlPlugin.swift +166 -0
  52. package/ios/Plugin/Protocols/Result.swift +6 -0
  53. package/package.json +91 -0
package/Package.swift ADDED
@@ -0,0 +1,30 @@
1
+ // swift-tools-version: 5.9
2
+ import PackageDescription
3
+
4
+ let package = Package(
5
+ name: "CapawesomeCapacitorLibsql",
6
+ platforms: [.iOS(.v14)],
7
+ products: [
8
+ .library(
9
+ name: "CapawesomeCapacitorLibsql",
10
+ targets: ["LibsqlPlugin"])
11
+ ],
12
+ dependencies: [
13
+ .package(url: "https://github.com/ionic-team/capacitor-swift-pm.git", from: "7.0.0"),
14
+ .package(url: "https://github.com/tursodatabase/libsql-swift", from: "0.1.1")
15
+ ],
16
+ targets: [
17
+ .target(
18
+ name: "LibsqlPlugin",
19
+ dependencies: [
20
+ .product(name: "Capacitor", package: "capacitor-swift-pm"),
21
+ .product(name: "Cordova", package: "capacitor-swift-pm"),
22
+ .product(name: "Libsql", package: "libsql-swift")
23
+ ],
24
+ path: "ios/Plugin"),
25
+ .testTarget(
26
+ name: "LibsqlPluginTests",
27
+ dependencies: ["LibsqlPlugin"],
28
+ path: "ios/PluginTests")
29
+ ]
30
+ )
package/README.md ADDED
@@ -0,0 +1,301 @@
1
+ # @capawesome/capacitor-libsql
2
+
3
+ Capacitor plugin for libSQL databases.
4
+
5
+ ## Installation
6
+
7
+ ```bash
8
+ npm install @capawesome/capacitor-libsql
9
+ npx cap sync
10
+ ```
11
+
12
+ ## Usage
13
+
14
+ ```typescript
15
+ import { Libsql } from '@capawesome/capacitor-libsql';
16
+
17
+ const echo = async () => {
18
+ await Libsql.echo();
19
+ };
20
+ ```
21
+
22
+ ## API
23
+
24
+ <docgen-index>
25
+
26
+ * [`beginTransaction(...)`](#begintransaction)
27
+ * [`commitTransaction(...)`](#committransaction)
28
+ * [`connect(...)`](#connect)
29
+ * [`execute(...)`](#execute)
30
+ * [`executeBatch(...)`](#executebatch)
31
+ * [`query(...)`](#query)
32
+ * [`rollbackTransaction(...)`](#rollbacktransaction)
33
+ * [`sync(...)`](#sync)
34
+ * [Interfaces](#interfaces)
35
+ * [Type Aliases](#type-aliases)
36
+
37
+ </docgen-index>
38
+
39
+ <docgen-api>
40
+ <!--Update the source file JSDoc comments and rerun docgen to update the docs below-->
41
+
42
+ ### beginTransaction(...)
43
+
44
+ ```typescript
45
+ beginTransaction(options: BeginTransactionOptions) => Promise<BeginTransactionResult>
46
+ ```
47
+
48
+ Begin a transaction on the specified database connection.
49
+
50
+ Only available on Android.
51
+
52
+ | Param | Type |
53
+ | ------------- | --------------------------------------------------------------------------- |
54
+ | **`options`** | <code><a href="#begintransactionoptions">BeginTransactionOptions</a></code> |
55
+
56
+ **Returns:** <code>Promise&lt;<a href="#begintransactionresult">BeginTransactionResult</a>&gt;</code>
57
+
58
+ **Since:** 0.0.0
59
+
60
+ --------------------
61
+
62
+
63
+ ### commitTransaction(...)
64
+
65
+ ```typescript
66
+ commitTransaction(options: CommitTransactionOptions) => Promise<void>
67
+ ```
68
+
69
+ Commit the current transaction on the specified database connection.
70
+
71
+ Only available on Android.
72
+
73
+ | Param | Type |
74
+ | ------------- | ----------------------------------------------------------------------------- |
75
+ | **`options`** | <code><a href="#committransactionoptions">CommitTransactionOptions</a></code> |
76
+
77
+ **Since:** 0.0.0
78
+
79
+ --------------------
80
+
81
+
82
+ ### connect(...)
83
+
84
+ ```typescript
85
+ connect(options: ConnectOptions) => Promise<ConnectResult>
86
+ ```
87
+
88
+ Connect to a database.
89
+
90
+ This method must be called before any other methods that interact with the database.
91
+
92
+ | Param | Type |
93
+ | ------------- | --------------------------------------------------------- |
94
+ | **`options`** | <code><a href="#connectoptions">ConnectOptions</a></code> |
95
+
96
+ **Returns:** <code>Promise&lt;<a href="#connectresult">ConnectResult</a>&gt;</code>
97
+
98
+ **Since:** 0.0.0
99
+
100
+ --------------------
101
+
102
+
103
+ ### execute(...)
104
+
105
+ ```typescript
106
+ execute(options: ExecuteOptions) => Promise<void>
107
+ ```
108
+
109
+ Execute a single SQL statement on the specified database connection.
110
+
111
+ This method can be used to execute any SQL statement, including `INSERT`, `UPDATE`, `DELETE`, and `CREATE TABLE`.
112
+
113
+ | Param | Type |
114
+ | ------------- | --------------------------------------------------------- |
115
+ | **`options`** | <code><a href="#executeoptions">ExecuteOptions</a></code> |
116
+
117
+ **Since:** 0.0.0
118
+
119
+ --------------------
120
+
121
+
122
+ ### executeBatch(...)
123
+
124
+ ```typescript
125
+ executeBatch(options: ExecuteBatchOptions) => Promise<void>
126
+ ```
127
+
128
+ Execute a batch of SQL statements on the specified database connection.
129
+
130
+ This method can be used to execute multiple SQL statements in a single call.
131
+
132
+ | Param | Type |
133
+ | ------------- | ------------------------------------------------------------------- |
134
+ | **`options`** | <code><a href="#executebatchoptions">ExecuteBatchOptions</a></code> |
135
+
136
+ **Since:** 0.0.0
137
+
138
+ --------------------
139
+
140
+
141
+ ### query(...)
142
+
143
+ ```typescript
144
+ query(options: QueryOptions) => Promise<QueryResult>
145
+ ```
146
+
147
+ Query the database and return the result set.
148
+
149
+ This method can be used to execute `SELECT` statements and retrieve the result set.
150
+
151
+ | Param | Type |
152
+ | ------------- | ----------------------------------------------------- |
153
+ | **`options`** | <code><a href="#queryoptions">QueryOptions</a></code> |
154
+
155
+ **Returns:** <code>Promise&lt;<a href="#queryresult">QueryResult</a>&gt;</code>
156
+
157
+ **Since:** 0.0.0
158
+
159
+ --------------------
160
+
161
+
162
+ ### rollbackTransaction(...)
163
+
164
+ ```typescript
165
+ rollbackTransaction(options: RollbackTransactionOptions) => Promise<void>
166
+ ```
167
+
168
+ Rollback the current transaction on the specified database connection.
169
+
170
+ This method will undo all changes made in the current transaction.
171
+
172
+ Only available on Android.
173
+
174
+ | Param | Type |
175
+ | ------------- | --------------------------------------------------------------------------------- |
176
+ | **`options`** | <code><a href="#rollbacktransactionoptions">RollbackTransactionOptions</a></code> |
177
+
178
+ **Since:** 0.0.0
179
+
180
+ --------------------
181
+
182
+
183
+ ### sync(...)
184
+
185
+ ```typescript
186
+ sync(options: SyncOptions) => Promise<void>
187
+ ```
188
+
189
+ Synchronize the database with the remote server.
190
+
191
+ Only available on iOS.
192
+
193
+ | Param | Type |
194
+ | ------------- | --------------------------------------------------- |
195
+ | **`options`** | <code><a href="#syncoptions">SyncOptions</a></code> |
196
+
197
+ **Since:** 0.0.0
198
+
199
+ --------------------
200
+
201
+
202
+ ### Interfaces
203
+
204
+
205
+ #### BeginTransactionResult
206
+
207
+ | Prop | Type | Description | Since |
208
+ | ------------------- | ------------------- | ------------------------------------------- | ----- |
209
+ | **`transactionId`** | <code>string</code> | The ID of the transaction that was started. | 0.0.0 |
210
+
211
+
212
+ #### BeginTransactionOptions
213
+
214
+ | Prop | Type | Description | Since |
215
+ | ------------------ | ------------------- | ----------------------------------------------------- | ----- |
216
+ | **`connectionId`** | <code>string</code> | The ID of the connection to begin the transaction on. | 0.0.0 |
217
+
218
+
219
+ #### CommitTransactionOptions
220
+
221
+ | Prop | Type | Description | Since |
222
+ | ------------------- | ------------------- | ------------------------------------------------------ | ----- |
223
+ | **`connectionId`** | <code>string</code> | The ID of the connection to commit the transaction on. | 0.0.0 |
224
+ | **`transactionId`** | <code>string</code> | The ID of the transaction to commit. | 0.0.0 |
225
+
226
+
227
+ #### ConnectResult
228
+
229
+ | Prop | Type | Description | Since |
230
+ | ------------------ | ------------------- | ------------------------- | ----- |
231
+ | **`connectionId`** | <code>string</code> | The ID of the connection. | 0.0.0 |
232
+
233
+
234
+ #### ConnectOptions
235
+
236
+ | Prop | Type | Description | Since |
237
+ | --------------- | ------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | ----- |
238
+ | **`authToken`** | <code>string</code> | The authentication token for the database. This is required for connecting to a remote database. If the database is local (i.e., a file on the device), this can be omitted. | 0.0.0 |
239
+ | **`path`** | <code>string</code> | The path to the database file. If no path or URL is provided, the plugin will create a new in-memory database. If no file exists at the specified path, a new file will be created. | 0.0.0 |
240
+ | **`url`** | <code>string</code> | The URL of the database. This can be used to connect to a remote database. If the URL is provided, the `authToken` must also be provided. If no path or URL is provided, the plugin will create a new in-memory database. | 0.0.0 |
241
+
242
+
243
+ #### ExecuteOptions
244
+
245
+ | Prop | Type | Description | Since |
246
+ | ------------------- | -------------------- | --------------------------------------------------------------------------- | ----- |
247
+ | **`connectionId`** | <code>string</code> | The ID of the connection to execute the SQL statement on. | 0.0.0 |
248
+ | **`statement`** | <code>string</code> | The SQL statement to execute. | 0.0.0 |
249
+ | **`transactionId`** | <code>string</code> | The transaction ID to use for the SQL statement. Only available on Android. | 0.0.0 |
250
+ | **`values`** | <code>Value[]</code> | The values to bind to the SQL statement. | 0.0.0 |
251
+
252
+
253
+ #### ExecuteBatchOptions
254
+
255
+ | Prop | Type | Description | Since |
256
+ | ------------------ | ---------------------- | ------------------------------------------------------------------- | ----- |
257
+ | **`connectionId`** | <code>string</code> | The ID of the connection to execute the batch on. | 0.0.0 |
258
+ | **`statement`** | <code>string[]</code> | The SQL statements to execute in the batch. | 0.0.0 |
259
+ | **`values`** | <code>Value[][]</code> | The transaction ID to use for the batch. Only available on Android. | 0.0.0 |
260
+
261
+
262
+ #### QueryResult
263
+
264
+ | Prop | Type | Description | Since |
265
+ | ---------- | ---------------------- | --------------------------------- | ----- |
266
+ | **`rows`** | <code>Value[][]</code> | The values returned by the query. | 0.0.0 |
267
+
268
+
269
+ #### QueryOptions
270
+
271
+ | Prop | Type | Description | Since |
272
+ | ------------------- | -------------------- | ------------------------------------------------------------------- | ----- |
273
+ | **`connectionId`** | <code>string</code> | The ID of the connection to query. | 0.0.0 |
274
+ | **`statement`** | <code>string</code> | The SQL statement to execute. | 0.0.0 |
275
+ | **`transactionId`** | <code>string</code> | The transaction ID to use for the query. Only available on Android. | 0.0.0 |
276
+ | **`values`** | <code>Value[]</code> | The values to bind to the SQL statement. | 0.0.0 |
277
+
278
+
279
+ #### RollbackTransactionOptions
280
+
281
+ | Prop | Type | Description | Since |
282
+ | ------------------- | ------------------- | -------------------------------------------------------- | ----- |
283
+ | **`connectionId`** | <code>string</code> | The ID of the connection to rollback the transaction on. | 0.0.0 |
284
+ | **`transactionId`** | <code>string</code> | The ID of the transaction to rollback. | 0.0.0 |
285
+
286
+
287
+ #### SyncOptions
288
+
289
+ | Prop | Type | Description | Since |
290
+ | ------------------ | ------------------- | --------------------------------- | ----- |
291
+ | **`connectionId`** | <code>string</code> | The ID of the connection to sync. | 0.0.0 |
292
+
293
+
294
+ ### Type Aliases
295
+
296
+
297
+ #### Value
298
+
299
+ <code>string | number | null</code>
300
+
301
+ </docgen-api>
@@ -0,0 +1,66 @@
1
+ ext {
2
+ junitVersion = project.hasProperty('junitVersion') ? rootProject.ext.junitVersion : '4.13.2'
3
+ androidxAppCompatVersion = project.hasProperty('androidxAppCompatVersion') ? rootProject.ext.androidxAppCompatVersion : '1.7.0'
4
+ androidxJunitVersion = project.hasProperty('androidxJunitVersion') ? rootProject.ext.androidxJunitVersion : '1.2.1'
5
+ androidxEspressoCoreVersion = project.hasProperty('androidxEspressoCoreVersion') ? rootProject.ext.androidxEspressoCoreVersion : '3.6.1'
6
+ libsqlVersion = project.hasProperty('libsqlVersion') ? rootProject.ext.libsqlVersion : '0.1.0'
7
+ }
8
+
9
+ buildscript {
10
+ ext.kotlin_version = project.hasProperty("kotlin_version") ? rootProject.ext.kotlin_version : '1.9.25'
11
+ repositories {
12
+ google()
13
+ mavenCentral()
14
+ }
15
+ dependencies {
16
+ classpath 'com.android.tools.build:gradle:8.7.2'
17
+ classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
18
+ }
19
+ }
20
+
21
+ apply plugin: 'com.android.library'
22
+ apply plugin: 'org.jetbrains.kotlin.android'
23
+
24
+ android {
25
+ namespace "io.capawesome.capacitorjs.plugins.libsql"
26
+ compileSdk project.hasProperty('compileSdkVersion') ? rootProject.ext.compileSdkVersion : 35
27
+ defaultConfig {
28
+ minSdkVersion project.hasProperty('minSdkVersion') ? rootProject.ext.minSdkVersion : 23
29
+ targetSdkVersion project.hasProperty('targetSdkVersion') ? rootProject.ext.targetSdkVersion : 35
30
+ versionCode 1
31
+ versionName "1.0"
32
+ testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
33
+ }
34
+ buildTypes {
35
+ release {
36
+ minifyEnabled false
37
+ proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
38
+ }
39
+ }
40
+ lintOptions {
41
+ abortOnError false
42
+ }
43
+ compileOptions {
44
+ sourceCompatibility JavaVersion.VERSION_21
45
+ targetCompatibility JavaVersion.VERSION_21
46
+ }
47
+ kotlinOptions {
48
+ jvmTarget = '21'
49
+ }
50
+ }
51
+
52
+ repositories {
53
+ google()
54
+ mavenCentral()
55
+ }
56
+
57
+
58
+ dependencies {
59
+ implementation fileTree(dir: 'libs', include: ['*.jar'])
60
+ implementation project(':capacitor-android')
61
+ implementation "androidx.appcompat:appcompat:$androidxAppCompatVersion"
62
+ implementation "tech.turso.libsql:libsql:$libsqlVersion"
63
+ testImplementation "junit:junit:$junitVersion"
64
+ androidTestImplementation "androidx.test.ext:junit:$androidxJunitVersion"
65
+ androidTestImplementation "androidx.test.espresso:espresso-core:$androidxEspressoCoreVersion"
66
+ }
@@ -0,0 +1,2 @@
1
+ <manifest xmlns:android="http://schemas.android.com/apk/res/android">
2
+ </manifest>
@@ -0,0 +1,188 @@
1
+ package io.capawesome.capacitorjs.plugins.libsql
2
+
3
+ import io.capawesome.capacitorjs.plugins.libsql.classes.options.*
4
+ import io.capawesome.capacitorjs.plugins.libsql.classes.results.*
5
+ import io.capawesome.capacitorjs.plugins.libsql.interfaces.*
6
+ import tech.turso.libsql.Database
7
+ import java.io.File
8
+ import tech.turso.libsql.Connection as LibsqlConnection
9
+ import java.util.concurrent.ConcurrentHashMap
10
+ import java.util.UUID
11
+
12
+ class Libsql(private val plugin: LibsqlPlugin) {
13
+
14
+ private val databases = ConcurrentHashMap<String, Database>()
15
+ private val connections = ConcurrentHashMap<String, LibsqlConnection>()
16
+ private val transactions = ConcurrentHashMap<String, String>()
17
+
18
+ @Throws(Exception::class)
19
+ fun connect(options: ConnectOptions, callback: NonEmptyResultCallback<ConnectResult>) {
20
+ try {
21
+ val connectionId = UUID.randomUUID().toString()
22
+
23
+ val database = when {
24
+ options.url != null && options.path != null -> {
25
+ // Embedded replica mode
26
+ tech.turso.libsql.Libsql.open(
27
+ url = options.url!!,
28
+ authToken = options.authToken!!,
29
+ path = resolvePath(options.path!!)
30
+ )
31
+ }
32
+ options.url != null -> {
33
+ // Remote-only mode
34
+ tech.turso.libsql.Libsql.open(
35
+ url = options.url!!,
36
+ authToken = options.authToken!!
37
+ )
38
+ }
39
+ options.path != null -> {
40
+ // Local-only mode
41
+ tech.turso.libsql.Libsql.open(path = resolvePath(options.path!!))
42
+ }
43
+ else -> {
44
+ // In-memory database
45
+ tech.turso.libsql.Libsql.open(path = ":memory:")
46
+ }
47
+ }
48
+
49
+ val connection = database.connect()
50
+
51
+ databases[connectionId] = database
52
+ connections[connectionId] = connection
53
+
54
+ val result = ConnectResult(connectionId)
55
+ callback.success(result)
56
+ } catch (exception: Exception) {
57
+ callback.error(exception)
58
+ }
59
+ }
60
+
61
+ @Throws(Exception::class)
62
+ fun execute(options: ExecuteOptions, callback: EmptyCallback) {
63
+ try {
64
+ val connection = connections[options.connectionId]
65
+ ?: throw Exception("Connection not found: ${options.connectionId}")
66
+
67
+ val values = options.values?.toTypedArray()
68
+
69
+ if (values != null && values.isNotEmpty()) {
70
+ connection.execute(options.statement, *values)
71
+ } else {
72
+ connection.execute(options.statement)
73
+ }
74
+
75
+ callback.success()
76
+ } catch (exception: Exception) {
77
+ callback.error(exception)
78
+ }
79
+ }
80
+
81
+ @Throws(Exception::class)
82
+ fun executeBatch(options: ExecuteBatchOptions, callback: EmptyCallback) {
83
+ try {
84
+ val connection = connections[options.connectionId]
85
+ ?: throw Exception("Connection not found: ${options.connectionId}")
86
+
87
+ for (i in options.statement.indices) {
88
+ val statement = options.statement[i]
89
+ val values = options.values?.getOrNull(i)?.toTypedArray()
90
+
91
+ if (!values.isNullOrEmpty()) {
92
+ connection.execute(statement, *values)
93
+ } else {
94
+ connection.execute(statement)
95
+ }
96
+ }
97
+
98
+ callback.success()
99
+ } catch (exception: Exception) {
100
+ callback.error(exception)
101
+ }
102
+ }
103
+
104
+ @Throws(Exception::class)
105
+ fun query(options: QueryOptions, callback: NonEmptyResultCallback<QueryResult>) {
106
+ try {
107
+ val connection = connections[options.connectionId]
108
+ ?: throw Exception("Connection not found: ${options.connectionId}")
109
+
110
+ val values = options.values?.toTypedArray()
111
+
112
+ val rows = if (values != null && values.isNotEmpty()) {
113
+ connection.query(options.statement, *values)
114
+ } else {
115
+ connection.query(options.statement)
116
+ }
117
+
118
+ val result = QueryResult(rows)
119
+ callback.success(result)
120
+ } catch (exception: Exception) {
121
+ callback.error(exception)
122
+ }
123
+ }
124
+
125
+ @Throws(Exception::class)
126
+ fun beginTransaction(options: BeginTransactionOptions, callback: NonEmptyResultCallback<BeginTransactionResult>) {
127
+ try {
128
+ val connection = connections[options.connectionId]
129
+ ?: throw Exception("Connection not found: ${options.connectionId}")
130
+
131
+ connection.execute("BEGIN TRANSACTION")
132
+
133
+ val transactionId = UUID.randomUUID().toString()
134
+ transactions[transactionId] = options.connectionId
135
+
136
+ val result = BeginTransactionResult(transactionId)
137
+ callback.success(result)
138
+ } catch (exception: Exception) {
139
+ callback.error(exception)
140
+ }
141
+ }
142
+
143
+ @Throws(Exception::class)
144
+ fun commitTransaction(options: CommitTransactionOptions, callback: EmptyCallback) {
145
+ try {
146
+ val connection = connections[options.connectionId]
147
+ ?: throw Exception("Connection not found: ${options.connectionId}")
148
+
149
+ if (!transactions.containsKey(options.transactionId)) {
150
+ throw Exception("Transaction not found: ${options.transactionId}")
151
+ }
152
+
153
+ connection.execute("COMMIT")
154
+ transactions.remove(options.transactionId)
155
+
156
+ callback.success()
157
+ } catch (exception: Exception) {
158
+ callback.error(exception)
159
+ }
160
+ }
161
+
162
+ @Throws(Exception::class)
163
+ fun rollbackTransaction(options: RollbackTransactionOptions, callback: EmptyCallback) {
164
+ try {
165
+ val connection = connections[options.connectionId]
166
+ ?: throw Exception("Connection not found: ${options.connectionId}")
167
+
168
+ if (!transactions.containsKey(options.transactionId)) {
169
+ throw Exception("Transaction not found: ${options.transactionId}")
170
+ }
171
+
172
+ connection.execute("ROLLBACK")
173
+ transactions.remove(options.transactionId)
174
+
175
+ callback.success()
176
+ } catch (exception: Exception) {
177
+ callback.error(exception)
178
+ }
179
+ }
180
+
181
+ private fun resolvePath(path: String): String {
182
+ var file = File(path)
183
+ if (!file.isAbsolute) {
184
+ file = File(plugin.context.getDatabasePath(path).absolutePath)
185
+ }
186
+ return file.absolutePath
187
+ }
188
+ }