@capgo/capacitor-fast-sql 8.0.24 → 8.0.25
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.
- package/README.md +54 -0
- package/android/build.gradle +1 -1
- package/android/src/main/java/app/capgo/capacitor/fastsql/CapgoCapacitorFastSqlPlugin.java +7 -1
- package/android/src/main/java/app/capgo/capacitor/fastsql/EncryptedSQLDatabase.java +7 -8
- package/android/src/main/java/app/capgo/capacitor/fastsql/SQLDatabase.java +7 -8
- package/dist/esm/definitions.d.ts +40 -0
- package/dist/esm/definitions.d.ts.map +1 -1
- package/dist/esm/fast-sql.d.ts +1 -1
- package/dist/esm/fast-sql.d.ts.map +1 -1
- package/dist/esm/fast-sql.js +12 -3
- package/dist/esm/helpers.js +0 -1
- package/dist/esm/index.d.ts +3 -1
- package/dist/esm/index.d.ts.map +1 -1
- package/dist/esm/index.js +2 -1
- package/dist/esm/native-sql-connection.d.ts +95 -0
- package/dist/esm/native-sql-connection.d.ts.map +1 -0
- package/dist/esm/native-sql-connection.js +244 -0
- package/dist/esm/sql-connection.d.ts +4 -78
- package/dist/esm/sql-connection.d.ts.map +1 -1
- package/dist/esm/sql-connection.js +1 -244
- package/dist/esm/web-sql-connection.d.ts +72 -0
- package/dist/esm/web-sql-connection.d.ts.map +1 -0
- package/dist/esm/web-sql-connection.js +122 -0
- package/dist/esm/web.d.ts +9 -2
- package/dist/esm/web.d.ts.map +1 -1
- package/dist/esm/web.js +41 -25
- package/dist/plugin.cjs.js +176 -29
- package/dist/plugin.cjs.js.map +1 -1
- package/dist/plugin.js +176 -29
- package/dist/plugin.js.map +1 -1
- package/ios/Sources/CapgoCapacitorFastSqlPlugin/CapgoCapacitorFastSqlPlugin.swift +8 -2
- package/package.json +1 -1
|
@@ -0,0 +1,244 @@
|
|
|
1
|
+
import { IsolationLevel } from './definitions';
|
|
2
|
+
/**
|
|
3
|
+
* SQL Connection class that uses HTTP protocol for efficient communication
|
|
4
|
+
* with native SQLite databases, bypassing Capacitor's standard bridge.
|
|
5
|
+
*
|
|
6
|
+
* Inspired by capacitor-blob-writer's approach to avoid serialization overhead.
|
|
7
|
+
*/
|
|
8
|
+
export class NativeSQLConnection {
|
|
9
|
+
constructor(database, port, token) {
|
|
10
|
+
this.inTransaction = false;
|
|
11
|
+
this.database = database;
|
|
12
|
+
this.port = port;
|
|
13
|
+
this.token = token;
|
|
14
|
+
this.baseUrl = `http://localhost:${port}`;
|
|
15
|
+
}
|
|
16
|
+
/**
|
|
17
|
+
* Get the database name
|
|
18
|
+
*/
|
|
19
|
+
getDatabaseName() {
|
|
20
|
+
return this.database;
|
|
21
|
+
}
|
|
22
|
+
/**
|
|
23
|
+
* Execute a SQL query via HTTP protocol for optimal performance
|
|
24
|
+
*
|
|
25
|
+
* @param statement - SQL statement to execute
|
|
26
|
+
* @param params - Parameters to bind to the statement
|
|
27
|
+
* @returns Query results
|
|
28
|
+
*/
|
|
29
|
+
async execute(statement, params) {
|
|
30
|
+
const response = await fetch(`${this.baseUrl}/execute`, {
|
|
31
|
+
method: 'POST',
|
|
32
|
+
headers: {
|
|
33
|
+
'Content-Type': 'application/json',
|
|
34
|
+
Authorization: `Bearer ${this.token}`,
|
|
35
|
+
'X-Database': this.database,
|
|
36
|
+
},
|
|
37
|
+
body: JSON.stringify({
|
|
38
|
+
statement,
|
|
39
|
+
params: params ? this.serializeParams(params) : [],
|
|
40
|
+
}),
|
|
41
|
+
});
|
|
42
|
+
if (!response.ok) {
|
|
43
|
+
const error = await response.text();
|
|
44
|
+
throw new Error(`SQL execution failed: ${error}`);
|
|
45
|
+
}
|
|
46
|
+
const result = await response.json();
|
|
47
|
+
return this.deserializeResult(result);
|
|
48
|
+
}
|
|
49
|
+
/**
|
|
50
|
+
* Execute multiple SQL statements in a batch for better performance
|
|
51
|
+
*
|
|
52
|
+
* @param operations - Array of SQL operations to execute
|
|
53
|
+
* @returns Array of results for each operation
|
|
54
|
+
*/
|
|
55
|
+
async executeBatch(operations) {
|
|
56
|
+
const response = await fetch(`${this.baseUrl}/batch`, {
|
|
57
|
+
method: 'POST',
|
|
58
|
+
headers: {
|
|
59
|
+
'Content-Type': 'application/json',
|
|
60
|
+
Authorization: `Bearer ${this.token}`,
|
|
61
|
+
'X-Database': this.database,
|
|
62
|
+
},
|
|
63
|
+
body: JSON.stringify({
|
|
64
|
+
operations: operations.map((op) => ({
|
|
65
|
+
statement: op.statement,
|
|
66
|
+
params: op.params ? this.serializeParams(op.params) : [],
|
|
67
|
+
})),
|
|
68
|
+
}),
|
|
69
|
+
});
|
|
70
|
+
if (!response.ok) {
|
|
71
|
+
const error = await response.text();
|
|
72
|
+
throw new Error(`SQL batch execution failed: ${error}`);
|
|
73
|
+
}
|
|
74
|
+
const results = await response.json();
|
|
75
|
+
return results.map((r) => this.deserializeResult(r));
|
|
76
|
+
}
|
|
77
|
+
/**
|
|
78
|
+
* Begin a transaction
|
|
79
|
+
*
|
|
80
|
+
* @param isolationLevel - Optional isolation level
|
|
81
|
+
*/
|
|
82
|
+
async beginTransaction(isolationLevel) {
|
|
83
|
+
if (this.inTransaction) {
|
|
84
|
+
throw new Error('Transaction already in progress');
|
|
85
|
+
}
|
|
86
|
+
const response = await fetch(`${this.baseUrl}/transaction/begin`, {
|
|
87
|
+
method: 'POST',
|
|
88
|
+
headers: {
|
|
89
|
+
'Content-Type': 'application/json',
|
|
90
|
+
Authorization: `Bearer ${this.token}`,
|
|
91
|
+
'X-Database': this.database,
|
|
92
|
+
},
|
|
93
|
+
body: JSON.stringify({
|
|
94
|
+
isolationLevel: isolationLevel || IsolationLevel.Serializable,
|
|
95
|
+
}),
|
|
96
|
+
});
|
|
97
|
+
if (!response.ok) {
|
|
98
|
+
const error = await response.text();
|
|
99
|
+
throw new Error(`Failed to begin transaction: ${error}`);
|
|
100
|
+
}
|
|
101
|
+
this.inTransaction = true;
|
|
102
|
+
}
|
|
103
|
+
/**
|
|
104
|
+
* Commit the current transaction
|
|
105
|
+
*/
|
|
106
|
+
async commit() {
|
|
107
|
+
if (!this.inTransaction) {
|
|
108
|
+
throw new Error('No transaction in progress');
|
|
109
|
+
}
|
|
110
|
+
const response = await fetch(`${this.baseUrl}/transaction/commit`, {
|
|
111
|
+
method: 'POST',
|
|
112
|
+
headers: {
|
|
113
|
+
Authorization: `Bearer ${this.token}`,
|
|
114
|
+
'X-Database': this.database,
|
|
115
|
+
},
|
|
116
|
+
});
|
|
117
|
+
if (!response.ok) {
|
|
118
|
+
const error = await response.text();
|
|
119
|
+
throw new Error(`Failed to commit transaction: ${error}`);
|
|
120
|
+
}
|
|
121
|
+
this.inTransaction = false;
|
|
122
|
+
}
|
|
123
|
+
/**
|
|
124
|
+
* Rollback the current transaction
|
|
125
|
+
*/
|
|
126
|
+
async rollback() {
|
|
127
|
+
if (!this.inTransaction) {
|
|
128
|
+
throw new Error('No transaction in progress');
|
|
129
|
+
}
|
|
130
|
+
const response = await fetch(`${this.baseUrl}/transaction/rollback`, {
|
|
131
|
+
method: 'POST',
|
|
132
|
+
headers: {
|
|
133
|
+
Authorization: `Bearer ${this.token}`,
|
|
134
|
+
'X-Database': this.database,
|
|
135
|
+
},
|
|
136
|
+
});
|
|
137
|
+
if (!response.ok) {
|
|
138
|
+
const error = await response.text();
|
|
139
|
+
throw new Error(`Failed to rollback transaction: ${error}`);
|
|
140
|
+
}
|
|
141
|
+
this.inTransaction = false;
|
|
142
|
+
}
|
|
143
|
+
/**
|
|
144
|
+
* Execute operations within a transaction automatically
|
|
145
|
+
*
|
|
146
|
+
* @param callback - Function containing operations to execute
|
|
147
|
+
* @param isolationLevel - Optional isolation level
|
|
148
|
+
*/
|
|
149
|
+
async transaction(callback, isolationLevel) {
|
|
150
|
+
await this.beginTransaction(isolationLevel);
|
|
151
|
+
try {
|
|
152
|
+
const result = await callback(this);
|
|
153
|
+
await this.commit();
|
|
154
|
+
return result;
|
|
155
|
+
}
|
|
156
|
+
catch (error) {
|
|
157
|
+
await this.rollback();
|
|
158
|
+
throw error;
|
|
159
|
+
}
|
|
160
|
+
}
|
|
161
|
+
/**
|
|
162
|
+
* Query helper for SELECT statements
|
|
163
|
+
*
|
|
164
|
+
* @param statement - SELECT statement
|
|
165
|
+
* @param params - Query parameters
|
|
166
|
+
* @returns Array of rows
|
|
167
|
+
*/
|
|
168
|
+
async query(statement, params) {
|
|
169
|
+
const result = await this.execute(statement, params);
|
|
170
|
+
return result.rows;
|
|
171
|
+
}
|
|
172
|
+
/**
|
|
173
|
+
* Execute helper for INSERT/UPDATE/DELETE statements
|
|
174
|
+
*
|
|
175
|
+
* @param statement - SQL statement
|
|
176
|
+
* @param params - Statement parameters
|
|
177
|
+
* @returns Number of affected rows and insert ID if applicable
|
|
178
|
+
*/
|
|
179
|
+
async run(statement, params) {
|
|
180
|
+
const result = await this.execute(statement, params);
|
|
181
|
+
return {
|
|
182
|
+
rowsAffected: result.rowsAffected,
|
|
183
|
+
insertId: result.insertId,
|
|
184
|
+
};
|
|
185
|
+
}
|
|
186
|
+
/**
|
|
187
|
+
* Serialize parameters for transmission
|
|
188
|
+
* Binary data (Uint8Array) is converted to base64 for JSON transport
|
|
189
|
+
*/
|
|
190
|
+
serializeParams(params) {
|
|
191
|
+
return params.map((param) => {
|
|
192
|
+
if (param instanceof Uint8Array) {
|
|
193
|
+
return {
|
|
194
|
+
_type: 'binary',
|
|
195
|
+
_data: this.uint8ArrayToBase64(param),
|
|
196
|
+
};
|
|
197
|
+
}
|
|
198
|
+
return param;
|
|
199
|
+
});
|
|
200
|
+
}
|
|
201
|
+
/**
|
|
202
|
+
* Deserialize result from server
|
|
203
|
+
* Base64-encoded binary data is converted back to Uint8Array
|
|
204
|
+
*/
|
|
205
|
+
deserializeResult(result) {
|
|
206
|
+
return {
|
|
207
|
+
rows: result.rows.map((row) => {
|
|
208
|
+
const deserializedRow = {};
|
|
209
|
+
for (const [key, value] of Object.entries(row)) {
|
|
210
|
+
if (value && typeof value === 'object' && value._type === 'binary') {
|
|
211
|
+
deserializedRow[key] = this.base64ToUint8Array(value._data);
|
|
212
|
+
}
|
|
213
|
+
else {
|
|
214
|
+
deserializedRow[key] = value;
|
|
215
|
+
}
|
|
216
|
+
}
|
|
217
|
+
return deserializedRow;
|
|
218
|
+
}),
|
|
219
|
+
rowsAffected: result.rowsAffected || 0,
|
|
220
|
+
insertId: result.insertId,
|
|
221
|
+
};
|
|
222
|
+
}
|
|
223
|
+
/**
|
|
224
|
+
* Convert Uint8Array to base64 string
|
|
225
|
+
*/
|
|
226
|
+
uint8ArrayToBase64(bytes) {
|
|
227
|
+
let binary = '';
|
|
228
|
+
for (let i = 0; i < bytes.byteLength; i++) {
|
|
229
|
+
binary += String.fromCharCode(bytes[i]);
|
|
230
|
+
}
|
|
231
|
+
return btoa(binary);
|
|
232
|
+
}
|
|
233
|
+
/**
|
|
234
|
+
* Convert base64 string to Uint8Array
|
|
235
|
+
*/
|
|
236
|
+
base64ToUint8Array(base64) {
|
|
237
|
+
const binary = atob(base64);
|
|
238
|
+
const bytes = new Uint8Array(binary.length);
|
|
239
|
+
for (let i = 0; i < binary.length; i++) {
|
|
240
|
+
bytes[i] = binary.charCodeAt(i);
|
|
241
|
+
}
|
|
242
|
+
return bytes;
|
|
243
|
+
}
|
|
244
|
+
}
|
|
@@ -1,94 +1,20 @@
|
|
|
1
|
-
import type { SQLValue, SQLRow, SQLResult, SQLBatchOperation } from './definitions';
|
|
2
|
-
import { IsolationLevel } from './definitions';
|
|
1
|
+
import type { SQLValue, SQLRow, SQLResult, SQLBatchOperation, IsolationLevel } from './definitions';
|
|
3
2
|
/**
|
|
4
|
-
*
|
|
5
|
-
*
|
|
6
|
-
*
|
|
7
|
-
* Inspired by capacitor-blob-writer's approach to avoid serialization overhead.
|
|
3
|
+
* Common interface for SQL database connections.
|
|
4
|
+
* Implemented by NativeSQLConnection (HTTP protocol) and WebSQLConnection (direct plugin calls).
|
|
8
5
|
*/
|
|
9
|
-
export
|
|
10
|
-
private port;
|
|
11
|
-
private token;
|
|
12
|
-
private database;
|
|
13
|
-
private baseUrl;
|
|
14
|
-
private inTransaction;
|
|
15
|
-
constructor(database: string, port: number, token: string);
|
|
16
|
-
/**
|
|
17
|
-
* Get the database name
|
|
18
|
-
*/
|
|
6
|
+
export interface SQLConnection {
|
|
19
7
|
getDatabaseName(): string;
|
|
20
|
-
/**
|
|
21
|
-
* Execute a SQL query via HTTP protocol for optimal performance
|
|
22
|
-
*
|
|
23
|
-
* @param statement - SQL statement to execute
|
|
24
|
-
* @param params - Parameters to bind to the statement
|
|
25
|
-
* @returns Query results
|
|
26
|
-
*/
|
|
27
8
|
execute(statement: string, params?: SQLValue[]): Promise<SQLResult>;
|
|
28
|
-
/**
|
|
29
|
-
* Execute multiple SQL statements in a batch for better performance
|
|
30
|
-
*
|
|
31
|
-
* @param operations - Array of SQL operations to execute
|
|
32
|
-
* @returns Array of results for each operation
|
|
33
|
-
*/
|
|
34
9
|
executeBatch(operations: SQLBatchOperation[]): Promise<SQLResult[]>;
|
|
35
|
-
/**
|
|
36
|
-
* Begin a transaction
|
|
37
|
-
*
|
|
38
|
-
* @param isolationLevel - Optional isolation level
|
|
39
|
-
*/
|
|
40
10
|
beginTransaction(isolationLevel?: IsolationLevel): Promise<void>;
|
|
41
|
-
/**
|
|
42
|
-
* Commit the current transaction
|
|
43
|
-
*/
|
|
44
11
|
commit(): Promise<void>;
|
|
45
|
-
/**
|
|
46
|
-
* Rollback the current transaction
|
|
47
|
-
*/
|
|
48
12
|
rollback(): Promise<void>;
|
|
49
|
-
/**
|
|
50
|
-
* Execute operations within a transaction automatically
|
|
51
|
-
*
|
|
52
|
-
* @param callback - Function containing operations to execute
|
|
53
|
-
* @param isolationLevel - Optional isolation level
|
|
54
|
-
*/
|
|
55
13
|
transaction<T>(callback: (conn: SQLConnection) => Promise<T>, isolationLevel?: IsolationLevel): Promise<T>;
|
|
56
|
-
/**
|
|
57
|
-
* Query helper for SELECT statements
|
|
58
|
-
*
|
|
59
|
-
* @param statement - SELECT statement
|
|
60
|
-
* @param params - Query parameters
|
|
61
|
-
* @returns Array of rows
|
|
62
|
-
*/
|
|
63
14
|
query(statement: string, params?: SQLValue[]): Promise<SQLRow[]>;
|
|
64
|
-
/**
|
|
65
|
-
* Execute helper for INSERT/UPDATE/DELETE statements
|
|
66
|
-
*
|
|
67
|
-
* @param statement - SQL statement
|
|
68
|
-
* @param params - Statement parameters
|
|
69
|
-
* @returns Number of affected rows and insert ID if applicable
|
|
70
|
-
*/
|
|
71
15
|
run(statement: string, params?: SQLValue[]): Promise<{
|
|
72
16
|
rowsAffected: number;
|
|
73
17
|
insertId?: number;
|
|
74
18
|
}>;
|
|
75
|
-
/**
|
|
76
|
-
* Serialize parameters for transmission
|
|
77
|
-
* Binary data (Uint8Array) is converted to base64 for JSON transport
|
|
78
|
-
*/
|
|
79
|
-
private serializeParams;
|
|
80
|
-
/**
|
|
81
|
-
* Deserialize result from server
|
|
82
|
-
* Base64-encoded binary data is converted back to Uint8Array
|
|
83
|
-
*/
|
|
84
|
-
private deserializeResult;
|
|
85
|
-
/**
|
|
86
|
-
* Convert Uint8Array to base64 string
|
|
87
|
-
*/
|
|
88
|
-
private uint8ArrayToBase64;
|
|
89
|
-
/**
|
|
90
|
-
* Convert base64 string to Uint8Array
|
|
91
|
-
*/
|
|
92
|
-
private base64ToUint8Array;
|
|
93
19
|
}
|
|
94
20
|
//# sourceMappingURL=sql-connection.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"sql-connection.d.ts","sourceRoot":"","sources":["../../src/sql-connection.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,QAAQ,EAAE,MAAM,EAAE,SAAS,EAAE,iBAAiB,EAAE,
|
|
1
|
+
{"version":3,"file":"sql-connection.d.ts","sourceRoot":"","sources":["../../src/sql-connection.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,QAAQ,EAAE,MAAM,EAAE,SAAS,EAAE,iBAAiB,EAAE,cAAc,EAAE,MAAM,eAAe,CAAC;AAEpG;;;GAGG;AACH,MAAM,WAAW,aAAa;IAC5B,eAAe,IAAI,MAAM,CAAC;IAC1B,OAAO,CAAC,SAAS,EAAE,MAAM,EAAE,MAAM,CAAC,EAAE,QAAQ,EAAE,GAAG,OAAO,CAAC,SAAS,CAAC,CAAC;IACpE,YAAY,CAAC,UAAU,EAAE,iBAAiB,EAAE,GAAG,OAAO,CAAC,SAAS,EAAE,CAAC,CAAC;IACpE,gBAAgB,CAAC,cAAc,CAAC,EAAE,cAAc,GAAG,OAAO,CAAC,IAAI,CAAC,CAAC;IACjE,MAAM,IAAI,OAAO,CAAC,IAAI,CAAC,CAAC;IACxB,QAAQ,IAAI,OAAO,CAAC,IAAI,CAAC,CAAC;IAC1B,WAAW,CAAC,CAAC,EAAE,QAAQ,EAAE,CAAC,IAAI,EAAE,aAAa,KAAK,OAAO,CAAC,CAAC,CAAC,EAAE,cAAc,CAAC,EAAE,cAAc,GAAG,OAAO,CAAC,CAAC,CAAC,CAAC;IAC3G,KAAK,CAAC,SAAS,EAAE,MAAM,EAAE,MAAM,CAAC,EAAE,QAAQ,EAAE,GAAG,OAAO,CAAC,MAAM,EAAE,CAAC,CAAC;IACjE,GAAG,CAAC,SAAS,EAAE,MAAM,EAAE,MAAM,CAAC,EAAE,QAAQ,EAAE,GAAG,OAAO,CAAC;QAAE,YAAY,EAAE,MAAM,CAAC;QAAC,QAAQ,CAAC,EAAE,MAAM,CAAA;KAAE,CAAC,CAAC;CACnG"}
|
|
@@ -1,244 +1 @@
|
|
|
1
|
-
|
|
2
|
-
/**
|
|
3
|
-
* SQL Connection class that uses HTTP protocol for efficient communication
|
|
4
|
-
* with native SQLite databases, bypassing Capacitor's standard bridge.
|
|
5
|
-
*
|
|
6
|
-
* Inspired by capacitor-blob-writer's approach to avoid serialization overhead.
|
|
7
|
-
*/
|
|
8
|
-
export class SQLConnection {
|
|
9
|
-
constructor(database, port, token) {
|
|
10
|
-
this.inTransaction = false;
|
|
11
|
-
this.database = database;
|
|
12
|
-
this.port = port;
|
|
13
|
-
this.token = token;
|
|
14
|
-
this.baseUrl = `http://localhost:${port}`;
|
|
15
|
-
}
|
|
16
|
-
/**
|
|
17
|
-
* Get the database name
|
|
18
|
-
*/
|
|
19
|
-
getDatabaseName() {
|
|
20
|
-
return this.database;
|
|
21
|
-
}
|
|
22
|
-
/**
|
|
23
|
-
* Execute a SQL query via HTTP protocol for optimal performance
|
|
24
|
-
*
|
|
25
|
-
* @param statement - SQL statement to execute
|
|
26
|
-
* @param params - Parameters to bind to the statement
|
|
27
|
-
* @returns Query results
|
|
28
|
-
*/
|
|
29
|
-
async execute(statement, params) {
|
|
30
|
-
const response = await fetch(`${this.baseUrl}/execute`, {
|
|
31
|
-
method: 'POST',
|
|
32
|
-
headers: {
|
|
33
|
-
'Content-Type': 'application/json',
|
|
34
|
-
Authorization: `Bearer ${this.token}`,
|
|
35
|
-
'X-Database': this.database,
|
|
36
|
-
},
|
|
37
|
-
body: JSON.stringify({
|
|
38
|
-
statement,
|
|
39
|
-
params: params ? this.serializeParams(params) : [],
|
|
40
|
-
}),
|
|
41
|
-
});
|
|
42
|
-
if (!response.ok) {
|
|
43
|
-
const error = await response.text();
|
|
44
|
-
throw new Error(`SQL execution failed: ${error}`);
|
|
45
|
-
}
|
|
46
|
-
const result = await response.json();
|
|
47
|
-
return this.deserializeResult(result);
|
|
48
|
-
}
|
|
49
|
-
/**
|
|
50
|
-
* Execute multiple SQL statements in a batch for better performance
|
|
51
|
-
*
|
|
52
|
-
* @param operations - Array of SQL operations to execute
|
|
53
|
-
* @returns Array of results for each operation
|
|
54
|
-
*/
|
|
55
|
-
async executeBatch(operations) {
|
|
56
|
-
const response = await fetch(`${this.baseUrl}/batch`, {
|
|
57
|
-
method: 'POST',
|
|
58
|
-
headers: {
|
|
59
|
-
'Content-Type': 'application/json',
|
|
60
|
-
Authorization: `Bearer ${this.token}`,
|
|
61
|
-
'X-Database': this.database,
|
|
62
|
-
},
|
|
63
|
-
body: JSON.stringify({
|
|
64
|
-
operations: operations.map((op) => ({
|
|
65
|
-
statement: op.statement,
|
|
66
|
-
params: op.params ? this.serializeParams(op.params) : [],
|
|
67
|
-
})),
|
|
68
|
-
}),
|
|
69
|
-
});
|
|
70
|
-
if (!response.ok) {
|
|
71
|
-
const error = await response.text();
|
|
72
|
-
throw new Error(`SQL batch execution failed: ${error}`);
|
|
73
|
-
}
|
|
74
|
-
const results = await response.json();
|
|
75
|
-
return results.map((r) => this.deserializeResult(r));
|
|
76
|
-
}
|
|
77
|
-
/**
|
|
78
|
-
* Begin a transaction
|
|
79
|
-
*
|
|
80
|
-
* @param isolationLevel - Optional isolation level
|
|
81
|
-
*/
|
|
82
|
-
async beginTransaction(isolationLevel) {
|
|
83
|
-
if (this.inTransaction) {
|
|
84
|
-
throw new Error('Transaction already in progress');
|
|
85
|
-
}
|
|
86
|
-
const response = await fetch(`${this.baseUrl}/transaction/begin`, {
|
|
87
|
-
method: 'POST',
|
|
88
|
-
headers: {
|
|
89
|
-
'Content-Type': 'application/json',
|
|
90
|
-
Authorization: `Bearer ${this.token}`,
|
|
91
|
-
'X-Database': this.database,
|
|
92
|
-
},
|
|
93
|
-
body: JSON.stringify({
|
|
94
|
-
isolationLevel: isolationLevel || IsolationLevel.Serializable,
|
|
95
|
-
}),
|
|
96
|
-
});
|
|
97
|
-
if (!response.ok) {
|
|
98
|
-
const error = await response.text();
|
|
99
|
-
throw new Error(`Failed to begin transaction: ${error}`);
|
|
100
|
-
}
|
|
101
|
-
this.inTransaction = true;
|
|
102
|
-
}
|
|
103
|
-
/**
|
|
104
|
-
* Commit the current transaction
|
|
105
|
-
*/
|
|
106
|
-
async commit() {
|
|
107
|
-
if (!this.inTransaction) {
|
|
108
|
-
throw new Error('No transaction in progress');
|
|
109
|
-
}
|
|
110
|
-
const response = await fetch(`${this.baseUrl}/transaction/commit`, {
|
|
111
|
-
method: 'POST',
|
|
112
|
-
headers: {
|
|
113
|
-
Authorization: `Bearer ${this.token}`,
|
|
114
|
-
'X-Database': this.database,
|
|
115
|
-
},
|
|
116
|
-
});
|
|
117
|
-
if (!response.ok) {
|
|
118
|
-
const error = await response.text();
|
|
119
|
-
throw new Error(`Failed to commit transaction: ${error}`);
|
|
120
|
-
}
|
|
121
|
-
this.inTransaction = false;
|
|
122
|
-
}
|
|
123
|
-
/**
|
|
124
|
-
* Rollback the current transaction
|
|
125
|
-
*/
|
|
126
|
-
async rollback() {
|
|
127
|
-
if (!this.inTransaction) {
|
|
128
|
-
throw new Error('No transaction in progress');
|
|
129
|
-
}
|
|
130
|
-
const response = await fetch(`${this.baseUrl}/transaction/rollback`, {
|
|
131
|
-
method: 'POST',
|
|
132
|
-
headers: {
|
|
133
|
-
Authorization: `Bearer ${this.token}`,
|
|
134
|
-
'X-Database': this.database,
|
|
135
|
-
},
|
|
136
|
-
});
|
|
137
|
-
if (!response.ok) {
|
|
138
|
-
const error = await response.text();
|
|
139
|
-
throw new Error(`Failed to rollback transaction: ${error}`);
|
|
140
|
-
}
|
|
141
|
-
this.inTransaction = false;
|
|
142
|
-
}
|
|
143
|
-
/**
|
|
144
|
-
* Execute operations within a transaction automatically
|
|
145
|
-
*
|
|
146
|
-
* @param callback - Function containing operations to execute
|
|
147
|
-
* @param isolationLevel - Optional isolation level
|
|
148
|
-
*/
|
|
149
|
-
async transaction(callback, isolationLevel) {
|
|
150
|
-
await this.beginTransaction(isolationLevel);
|
|
151
|
-
try {
|
|
152
|
-
const result = await callback(this);
|
|
153
|
-
await this.commit();
|
|
154
|
-
return result;
|
|
155
|
-
}
|
|
156
|
-
catch (error) {
|
|
157
|
-
await this.rollback();
|
|
158
|
-
throw error;
|
|
159
|
-
}
|
|
160
|
-
}
|
|
161
|
-
/**
|
|
162
|
-
* Query helper for SELECT statements
|
|
163
|
-
*
|
|
164
|
-
* @param statement - SELECT statement
|
|
165
|
-
* @param params - Query parameters
|
|
166
|
-
* @returns Array of rows
|
|
167
|
-
*/
|
|
168
|
-
async query(statement, params) {
|
|
169
|
-
const result = await this.execute(statement, params);
|
|
170
|
-
return result.rows;
|
|
171
|
-
}
|
|
172
|
-
/**
|
|
173
|
-
* Execute helper for INSERT/UPDATE/DELETE statements
|
|
174
|
-
*
|
|
175
|
-
* @param statement - SQL statement
|
|
176
|
-
* @param params - Statement parameters
|
|
177
|
-
* @returns Number of affected rows and insert ID if applicable
|
|
178
|
-
*/
|
|
179
|
-
async run(statement, params) {
|
|
180
|
-
const result = await this.execute(statement, params);
|
|
181
|
-
return {
|
|
182
|
-
rowsAffected: result.rowsAffected,
|
|
183
|
-
insertId: result.insertId,
|
|
184
|
-
};
|
|
185
|
-
}
|
|
186
|
-
/**
|
|
187
|
-
* Serialize parameters for transmission
|
|
188
|
-
* Binary data (Uint8Array) is converted to base64 for JSON transport
|
|
189
|
-
*/
|
|
190
|
-
serializeParams(params) {
|
|
191
|
-
return params.map((param) => {
|
|
192
|
-
if (param instanceof Uint8Array) {
|
|
193
|
-
return {
|
|
194
|
-
_type: 'binary',
|
|
195
|
-
_data: this.uint8ArrayToBase64(param),
|
|
196
|
-
};
|
|
197
|
-
}
|
|
198
|
-
return param;
|
|
199
|
-
});
|
|
200
|
-
}
|
|
201
|
-
/**
|
|
202
|
-
* Deserialize result from server
|
|
203
|
-
* Base64-encoded binary data is converted back to Uint8Array
|
|
204
|
-
*/
|
|
205
|
-
deserializeResult(result) {
|
|
206
|
-
return {
|
|
207
|
-
rows: result.rows.map((row) => {
|
|
208
|
-
const deserializedRow = {};
|
|
209
|
-
for (const [key, value] of Object.entries(row)) {
|
|
210
|
-
if (value && typeof value === 'object' && value._type === 'binary') {
|
|
211
|
-
deserializedRow[key] = this.base64ToUint8Array(value._data);
|
|
212
|
-
}
|
|
213
|
-
else {
|
|
214
|
-
deserializedRow[key] = value;
|
|
215
|
-
}
|
|
216
|
-
}
|
|
217
|
-
return deserializedRow;
|
|
218
|
-
}),
|
|
219
|
-
rowsAffected: result.rowsAffected || 0,
|
|
220
|
-
insertId: result.insertId,
|
|
221
|
-
};
|
|
222
|
-
}
|
|
223
|
-
/**
|
|
224
|
-
* Convert Uint8Array to base64 string
|
|
225
|
-
*/
|
|
226
|
-
uint8ArrayToBase64(bytes) {
|
|
227
|
-
let binary = '';
|
|
228
|
-
for (let i = 0; i < bytes.byteLength; i++) {
|
|
229
|
-
binary += String.fromCharCode(bytes[i]);
|
|
230
|
-
}
|
|
231
|
-
return btoa(binary);
|
|
232
|
-
}
|
|
233
|
-
/**
|
|
234
|
-
* Convert base64 string to Uint8Array
|
|
235
|
-
*/
|
|
236
|
-
base64ToUint8Array(base64) {
|
|
237
|
-
const binary = atob(base64);
|
|
238
|
-
const bytes = new Uint8Array(binary.length);
|
|
239
|
-
for (let i = 0; i < binary.length; i++) {
|
|
240
|
-
bytes[i] = binary.charCodeAt(i);
|
|
241
|
-
}
|
|
242
|
-
return bytes;
|
|
243
|
-
}
|
|
244
|
-
}
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,72 @@
|
|
|
1
|
+
import type { CapgoCapacitorFastSqlPlugin, SQLValue, SQLRow, SQLResult, SQLBatchOperation, IsolationLevel } from './definitions';
|
|
2
|
+
import type { SQLConnection } from './sql-connection';
|
|
3
|
+
/**
|
|
4
|
+
* Web implementation of SQLConnection that calls the Capacitor plugin directly,
|
|
5
|
+
* avoiding HTTP network requests which are not available on the web platform.
|
|
6
|
+
*/
|
|
7
|
+
export declare class WebSQLConnection implements SQLConnection {
|
|
8
|
+
private database;
|
|
9
|
+
private plugin;
|
|
10
|
+
private inTransaction;
|
|
11
|
+
constructor(database: string, plugin: CapgoCapacitorFastSqlPlugin);
|
|
12
|
+
/**
|
|
13
|
+
* Get the database name
|
|
14
|
+
*/
|
|
15
|
+
getDatabaseName(): string;
|
|
16
|
+
/**
|
|
17
|
+
* Execute a SQL query via the Capacitor plugin
|
|
18
|
+
*
|
|
19
|
+
* @param statement - SQL statement to execute
|
|
20
|
+
* @param params - Parameters to bind to the statement
|
|
21
|
+
* @returns Query results
|
|
22
|
+
*/
|
|
23
|
+
execute(statement: string, params?: SQLValue[]): Promise<SQLResult>;
|
|
24
|
+
/**
|
|
25
|
+
* Execute multiple SQL statements in a batch, sequentially
|
|
26
|
+
*
|
|
27
|
+
* @param operations - Array of SQL operations to execute
|
|
28
|
+
* @returns Array of results for each operation
|
|
29
|
+
*/
|
|
30
|
+
executeBatch(operations: SQLBatchOperation[]): Promise<SQLResult[]>;
|
|
31
|
+
/**
|
|
32
|
+
* Begin a transaction
|
|
33
|
+
*
|
|
34
|
+
* @param isolationLevel - Optional isolation level
|
|
35
|
+
*/
|
|
36
|
+
beginTransaction(isolationLevel?: IsolationLevel): Promise<void>;
|
|
37
|
+
/**
|
|
38
|
+
* Commit the current transaction
|
|
39
|
+
*/
|
|
40
|
+
commit(): Promise<void>;
|
|
41
|
+
/**
|
|
42
|
+
* Rollback the current transaction
|
|
43
|
+
*/
|
|
44
|
+
rollback(): Promise<void>;
|
|
45
|
+
/**
|
|
46
|
+
* Execute operations within a transaction automatically
|
|
47
|
+
*
|
|
48
|
+
* @param callback - Function containing operations to execute
|
|
49
|
+
* @param isolationLevel - Optional isolation level
|
|
50
|
+
*/
|
|
51
|
+
transaction<T>(callback: (conn: SQLConnection) => Promise<T>, isolationLevel?: IsolationLevel): Promise<T>;
|
|
52
|
+
/**
|
|
53
|
+
* Query helper for SELECT statements
|
|
54
|
+
*
|
|
55
|
+
* @param statement - SELECT statement
|
|
56
|
+
* @param params - Query parameters
|
|
57
|
+
* @returns Array of rows
|
|
58
|
+
*/
|
|
59
|
+
query(statement: string, params?: SQLValue[]): Promise<SQLRow[]>;
|
|
60
|
+
/**
|
|
61
|
+
* Execute helper for INSERT/UPDATE/DELETE statements
|
|
62
|
+
*
|
|
63
|
+
* @param statement - SQL statement
|
|
64
|
+
* @param params - Statement parameters
|
|
65
|
+
* @returns Number of affected rows and insert ID if applicable
|
|
66
|
+
*/
|
|
67
|
+
run(statement: string, params?: SQLValue[]): Promise<{
|
|
68
|
+
rowsAffected: number;
|
|
69
|
+
insertId?: number;
|
|
70
|
+
}>;
|
|
71
|
+
}
|
|
72
|
+
//# sourceMappingURL=web-sql-connection.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"web-sql-connection.d.ts","sourceRoot":"","sources":["../../src/web-sql-connection.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EACV,2BAA2B,EAC3B,QAAQ,EACR,MAAM,EACN,SAAS,EACT,iBAAiB,EACjB,cAAc,EACf,MAAM,eAAe,CAAC;AACvB,OAAO,KAAK,EAAE,aAAa,EAAE,MAAM,kBAAkB,CAAC;AAEtD;;;GAGG;AACH,qBAAa,gBAAiB,YAAW,aAAa;IACpD,OAAO,CAAC,QAAQ,CAAS;IACzB,OAAO,CAAC,MAAM,CAA8B;IAC5C,OAAO,CAAC,aAAa,CAAS;gBAElB,QAAQ,EAAE,MAAM,EAAE,MAAM,EAAE,2BAA2B;IAKjE;;OAEG;IACH,eAAe,IAAI,MAAM;IAIzB;;;;;;OAMG;IACG,OAAO,CAAC,SAAS,EAAE,MAAM,EAAE,MAAM,CAAC,EAAE,QAAQ,EAAE,GAAG,OAAO,CAAC,SAAS,CAAC;IAIzE;;;;;OAKG;IACG,YAAY,CAAC,UAAU,EAAE,iBAAiB,EAAE,GAAG,OAAO,CAAC,SAAS,EAAE,CAAC;IAQzE;;;;OAIG;IACG,gBAAgB,CAAC,cAAc,CAAC,EAAE,cAAc,GAAG,OAAO,CAAC,IAAI,CAAC;IAStE;;OAEG;IACG,MAAM,IAAI,OAAO,CAAC,IAAI,CAAC;IAS7B;;OAEG;IACG,QAAQ,IAAI,OAAO,CAAC,IAAI,CAAC;IAS/B;;;;;OAKG;IACG,WAAW,CAAC,CAAC,EAAE,QAAQ,EAAE,CAAC,IAAI,EAAE,aAAa,KAAK,OAAO,CAAC,CAAC,CAAC,EAAE,cAAc,CAAC,EAAE,cAAc,GAAG,OAAO,CAAC,CAAC,CAAC;IAoBhH;;;;;;OAMG;IACG,KAAK,CAAC,SAAS,EAAE,MAAM,EAAE,MAAM,CAAC,EAAE,QAAQ,EAAE,GAAG,OAAO,CAAC,MAAM,EAAE,CAAC;IAKtE;;;;;;OAMG;IACG,GAAG,CAAC,SAAS,EAAE,MAAM,EAAE,MAAM,CAAC,EAAE,QAAQ,EAAE,GAAG,OAAO,CAAC;QAAE,YAAY,EAAE,MAAM,CAAC;QAAC,QAAQ,CAAC,EAAE,MAAM,CAAA;KAAE,CAAC;CAOxG"}
|