expo-sqlite 12.2.0 → 13.0.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.
- package/CHANGELOG.md +22 -0
- package/android/CMakeLists.txt +1 -1
- package/android/build.gradle +2 -2
- package/android/src/main/cpp/NativeStatementBinding.cpp +16 -15
- package/android/src/main/java/expo/modules/sqlite/NativeDatabase.kt +2 -0
- package/android/src/main/java/expo/modules/sqlite/NativeStatement.kt +2 -0
- package/android/src/main/java/expo/modules/sqlite/SQLExceptions.kt +6 -0
- package/android/src/main/java/expo/modules/sqlite/SQLiteModuleNext.kt +98 -95
- package/build/next/NativeStatement.d.ts +9 -13
- package/build/next/NativeStatement.d.ts.map +1 -1
- package/build/next/NativeStatement.js.map +1 -1
- package/build/next/Statement.d.ts +4 -6
- package/build/next/Statement.d.ts.map +1 -1
- package/build/next/Statement.js +36 -47
- package/build/next/Statement.js.map +1 -1
- package/ios/Exceptions.swift +12 -0
- package/ios/NativeDatabase.swift +1 -0
- package/ios/NativeStatement.swift +1 -0
- package/ios/SQLiteModule.swift +3 -4
- package/ios/SQLiteModuleNext.swift +108 -92
- package/ios/crsqlite.xcframework/Info.plist +4 -0
- package/ios/crsqlite.xcframework/ios-arm64/crsqlite.framework/Info.plist +4 -0
- package/package.json +2 -2
- package/src/next/NativeStatement.ts +36 -22
- package/src/next/Statement.ts +54 -49
package/build/next/Statement.js
CHANGED
|
@@ -9,42 +9,28 @@ export class Statement {
|
|
|
9
9
|
this.nativeStatement = nativeStatement;
|
|
10
10
|
}
|
|
11
11
|
async runAsync(...params) {
|
|
12
|
-
|
|
13
|
-
if (shouldPassAsObject) {
|
|
14
|
-
return await this.nativeStatement.objectRunAsync(this.nativeDatabase, bindParams);
|
|
15
|
-
}
|
|
16
|
-
else {
|
|
17
|
-
return await this.nativeStatement.arrayRunAsync(this.nativeDatabase, bindParams);
|
|
18
|
-
}
|
|
12
|
+
return await this.nativeStatement.runAsync(this.nativeDatabase, ...normalizeParams(...params));
|
|
19
13
|
}
|
|
20
14
|
async *eachAsync(...params) {
|
|
21
|
-
const
|
|
22
|
-
const func =
|
|
23
|
-
? this.nativeStatement.objectGetAsync.bind(this.nativeStatement)
|
|
24
|
-
: this.nativeStatement.arrayGetAsync.bind(this.nativeStatement);
|
|
15
|
+
const paramTuple = normalizeParams(...params);
|
|
16
|
+
const func = this.nativeStatement.getAsync.bind(this.nativeStatement);
|
|
25
17
|
const columnNames = await this.getColumnNamesAsync();
|
|
26
18
|
let result = null;
|
|
27
19
|
do {
|
|
28
|
-
result = await func(this.nativeDatabase,
|
|
20
|
+
result = await func(this.nativeDatabase, ...paramTuple);
|
|
29
21
|
if (result != null) {
|
|
30
22
|
yield composeRow(columnNames, result);
|
|
31
23
|
}
|
|
32
24
|
} while (result != null);
|
|
33
25
|
}
|
|
34
26
|
async getAsync(...params) {
|
|
35
|
-
const { params: bindParams, shouldPassAsObject } = normalizeParams(...params);
|
|
36
27
|
const columnNames = await this.getColumnNamesAsync();
|
|
37
|
-
const columnValues =
|
|
38
|
-
? await this.nativeStatement.objectGetAsync(this.nativeDatabase, bindParams)
|
|
39
|
-
: await this.nativeStatement.arrayGetAsync(this.nativeDatabase, bindParams);
|
|
28
|
+
const columnValues = await this.nativeStatement.getAsync(this.nativeDatabase, ...normalizeParams(...params));
|
|
40
29
|
return columnValues != null ? composeRow(columnNames, columnValues) : null;
|
|
41
30
|
}
|
|
42
31
|
async allAsync(...params) {
|
|
43
|
-
const { params: bindParams, shouldPassAsObject } = normalizeParams(...params);
|
|
44
32
|
const columnNames = await this.getColumnNamesAsync();
|
|
45
|
-
const columnValuesList =
|
|
46
|
-
? await this.nativeStatement.objectGetAllAsync(this.nativeDatabase, bindParams)
|
|
47
|
-
: await this.nativeStatement.arrayGetAllAsync(this.nativeDatabase, bindParams);
|
|
33
|
+
const columnValuesList = await this.nativeStatement.getAllAsync(this.nativeDatabase, ...normalizeParams(...params));
|
|
48
34
|
return composeRows(columnNames, columnValuesList);
|
|
49
35
|
}
|
|
50
36
|
/**
|
|
@@ -67,42 +53,28 @@ export class Statement {
|
|
|
67
53
|
await this.nativeStatement.finalizeAsync(this.nativeDatabase);
|
|
68
54
|
}
|
|
69
55
|
runSync(...params) {
|
|
70
|
-
|
|
71
|
-
if (shouldPassAsObject) {
|
|
72
|
-
return this.nativeStatement.objectRunSync(this.nativeDatabase, bindParams);
|
|
73
|
-
}
|
|
74
|
-
else {
|
|
75
|
-
return this.nativeStatement.arrayRunSync(this.nativeDatabase, bindParams);
|
|
76
|
-
}
|
|
56
|
+
return this.nativeStatement.runSync(this.nativeDatabase, ...normalizeParams(...params));
|
|
77
57
|
}
|
|
78
58
|
*eachSync(...params) {
|
|
79
|
-
const
|
|
80
|
-
const func =
|
|
81
|
-
? this.nativeStatement.objectGetSync.bind(this.nativeStatement)
|
|
82
|
-
: this.nativeStatement.arrayGetSync.bind(this.nativeStatement);
|
|
59
|
+
const paramTuple = normalizeParams(...params);
|
|
60
|
+
const func = this.nativeStatement.getSync.bind(this.nativeStatement);
|
|
83
61
|
const columnNames = this.getColumnNamesSync();
|
|
84
62
|
let result = null;
|
|
85
63
|
do {
|
|
86
|
-
result = func(this.nativeDatabase,
|
|
64
|
+
result = func(this.nativeDatabase, ...paramTuple);
|
|
87
65
|
if (result != null) {
|
|
88
66
|
yield composeRow(columnNames, result);
|
|
89
67
|
}
|
|
90
68
|
} while (result != null);
|
|
91
69
|
}
|
|
92
70
|
getSync(...params) {
|
|
93
|
-
const { params: bindParams, shouldPassAsObject } = normalizeParams(...params);
|
|
94
71
|
const columnNames = this.getColumnNamesSync();
|
|
95
|
-
const columnValues =
|
|
96
|
-
? this.nativeStatement.objectGetSync(this.nativeDatabase, bindParams)
|
|
97
|
-
: this.nativeStatement.arrayGetSync(this.nativeDatabase, bindParams);
|
|
72
|
+
const columnValues = this.nativeStatement.getSync(this.nativeDatabase, ...normalizeParams(...params));
|
|
98
73
|
return columnValues != null ? composeRow(columnNames, columnValues) : null;
|
|
99
74
|
}
|
|
100
75
|
allSync(...params) {
|
|
101
|
-
const { params: bindParams, shouldPassAsObject } = normalizeParams(...params);
|
|
102
76
|
const columnNames = this.getColumnNamesSync();
|
|
103
|
-
const columnValuesList =
|
|
104
|
-
? this.nativeStatement.objectGetAllSync(this.nativeDatabase, bindParams)
|
|
105
|
-
: this.nativeStatement.arrayGetAllSync(this.nativeDatabase, bindParams);
|
|
77
|
+
const columnValuesList = this.nativeStatement.getAllSync(this.nativeDatabase, ...normalizeParams(...params));
|
|
106
78
|
return composeRows(columnNames, columnValuesList);
|
|
107
79
|
}
|
|
108
80
|
/**
|
|
@@ -128,7 +100,8 @@ export class Statement {
|
|
|
128
100
|
}
|
|
129
101
|
}
|
|
130
102
|
/**
|
|
131
|
-
* Normalize the bind params to
|
|
103
|
+
* Normalize the bind params to data structure that can be passed to native module.
|
|
104
|
+
* The data structure is a tuple of [primitiveParams, blobParams, shouldPassAsArray].
|
|
132
105
|
* @hidden
|
|
133
106
|
*/
|
|
134
107
|
export function normalizeParams(...params) {
|
|
@@ -136,14 +109,30 @@ export function normalizeParams(...params) {
|
|
|
136
109
|
if (bindParams == null) {
|
|
137
110
|
bindParams = [];
|
|
138
111
|
}
|
|
139
|
-
if (typeof bindParams !== 'object'
|
|
112
|
+
if (typeof bindParams !== 'object' ||
|
|
113
|
+
bindParams instanceof ArrayBuffer ||
|
|
114
|
+
ArrayBuffer.isView(bindParams)) {
|
|
140
115
|
bindParams = [bindParams];
|
|
141
116
|
}
|
|
142
|
-
const
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
117
|
+
const shouldPassAsArray = Array.isArray(bindParams);
|
|
118
|
+
if (Array.isArray(bindParams)) {
|
|
119
|
+
bindParams = bindParams.reduce((acc, value, index) => {
|
|
120
|
+
acc[index] = value;
|
|
121
|
+
return acc;
|
|
122
|
+
}, {});
|
|
123
|
+
}
|
|
124
|
+
const primitiveParams = {};
|
|
125
|
+
const blobParams = {};
|
|
126
|
+
for (const key in bindParams) {
|
|
127
|
+
const value = bindParams[key];
|
|
128
|
+
if (value instanceof Uint8Array) {
|
|
129
|
+
blobParams[key] = value;
|
|
130
|
+
}
|
|
131
|
+
else {
|
|
132
|
+
primitiveParams[key] = value;
|
|
133
|
+
}
|
|
134
|
+
}
|
|
135
|
+
return [primitiveParams, blobParams, shouldPassAsArray];
|
|
147
136
|
}
|
|
148
137
|
/**
|
|
149
138
|
* Compose `columnNames` and `columnValues` to an row object.
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"Statement.js","sourceRoot":"","sources":["../../src/next/Statement.ts"],"names":[],"mappings":"AAaA;;GAEG;AACH,MAAM,OAAO,SAAS;IAED;IACA;IAFnB,YACmB,cAA8B,EAC9B,eAAgC;QADhC,mBAAc,GAAd,cAAc,CAAgB;QAC9B,oBAAe,GAAf,eAAe,CAAiB;IAChD,CAAC;IAaG,KAAK,CAAC,QAAQ,CAAC,GAAG,MAAiB;QACxC,MAAM,EAAE,MAAM,EAAE,UAAU,EAAE,kBAAkB,EAAE,GAAG,eAAe,CAAC,GAAG,MAAM,CAAC,CAAC;QAC9E,IAAI,kBAAkB,EAAE;YACtB,OAAO,MAAM,IAAI,CAAC,eAAe,CAAC,cAAc,CAAC,IAAI,CAAC,cAAc,EAAE,UAAU,CAAC,CAAC;SACnF;aAAM;YACL,OAAO,MAAM,IAAI,CAAC,eAAe,CAAC,aAAa,CAAC,IAAI,CAAC,cAAc,EAAE,UAAU,CAAC,CAAC;SAClF;IACH,CAAC;IAmBM,KAAK,CAAC,CAAC,SAAS,CAAI,GAAG,MAAiB;QAC7C,MAAM,EAAE,MAAM,EAAE,UAAU,EAAE,kBAAkB,EAAE,GAAG,eAAe,CAAC,GAAG,MAAM,CAAC,CAAC;QAC9E,MAAM,IAAI,GAAG,kBAAkB;YAC7B,CAAC,CAAC,IAAI,CAAC,eAAe,CAAC,cAAc,CAAC,IAAI,CAAC,IAAI,CAAC,eAAe,CAAC;YACh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,kDAAkD,WAAW,CAAC,MAAM,aAAa,YAAY,CAAC,MAAM,EAAE,CACvG,CAAC;KACH;IACD,KAAK,IAAI,CAAC,GAAG,CAAC,EAAE,CAAC,GAAG,WAAW,CAAC,MAAM,EAAE,CAAC,EAAE,EAAE;QAC3C,GAAG,CAAC,WAAW,CAAC,CAAC,CAAC,CAAC,GAAG,YAAY,CAAC,CAAC,CAAC,CAAC;KACvC;IACD,OAAO,GAAQ,CAAC;AAClB,CAAC;AAED;;;GAGG;AACH,MAAM,UAAU,WAAW,CAAI,WAAwB,EAAE,gBAAgC;IACvF,IAAI,gBAAgB,CAAC,MAAM,KAAK,CAAC,EAAE;QACjC,OAAO,EAAE,CAAC;KACX;IACD,IAAI,WAAW,CAAC,MAAM,KAAK,gBAAgB,CAAC,CAAC,CAAC,CAAC,MAAM,EAAE;QACrD,yFAAyF;QACzF,MAAM,IAAI,KAAK,CACb,kDAAkD,WAAW,CAAC,MAAM,aAAa,gBAAgB,CAAC,CAAC,CAAC,CAAC,MAAM,EAAE,CAC9G,CAAC;KACH;IACD,MAAM,OAAO,GAAQ,EAAE,CAAC;IACxB,KAAK,MAAM,YAAY,IAAI,gBAAgB,EAAE;QAC3C,MAAM,GAAG,GAAG,EAAE,CAAC;QACf,KAAK,IAAI,CAAC,GAAG,CAAC,EAAE,CAAC,GAAG,WAAW,CAAC,MAAM,EAAE,CAAC,EAAE,EAAE;YAC3C,GAAG,CAAC,WAAW,CAAC,CAAC,CAAC,CAAC,GAAG,YAAY,CAAC,CAAC,CAAC,CAAC;SACvC;QACD,OAAO,CAAC,IAAI,CAAC,GAAQ,CAAC,CAAC;KACxB;IACD,OAAO,OAAO,CAAC;AACjB,CAAC","sourcesContent":["import { NativeDatabase } from './NativeDatabase';\nimport {\n BindParams,\n BindValue,\n NativeStatement,\n RunResult,\n VariadicBindParams,\n type ColumnNames,\n type ColumnValues,\n} from './NativeStatement';\n\nexport { BindParams, BindValue, RunResult, VariadicBindParams };\n\n/**\n * A prepared statement returned by [`Database.prepareAsync()`](#prepareasyncsource) or [`Database.prepareSync()`](#preparesyncsource) that can be binded with parameters and executed.\n */\nexport class Statement {\n constructor(\n private readonly nativeDatabase: NativeDatabase,\n private readonly nativeStatement: NativeStatement\n ) {}\n\n //#region Asynchronous API\n\n /**\n * Run the prepared statement and return the result.\n * @param params The parameters to bind to the prepared statement. You can pass values in array, object, or variadic arguments. See [`BindValue`](#bindvalue) for more information about binding values.\n */\n public runAsync(params: BindParams): Promise<RunResult>;\n /**\n * @hidden\n */\n public runAsync(...params: VariadicBindParams): Promise<RunResult>;\n public async runAsync(...params: unknown[]): Promise<RunResult> {\n const { params: bindParams, shouldPassAsObject } = normalizeParams(...params);\n if (shouldPassAsObject) {\n return await this.nativeStatement.objectRunAsync(this.nativeDatabase, bindParams);\n } else {\n return await this.nativeStatement.arrayRunAsync(this.nativeDatabase, bindParams);\n }\n }\n\n /**\n * Iterate the prepared statement and return results as an async iterable.\n * @param params The parameters to bind to the prepared statement. You can pass values in array, object, or variadic arguments. See [`BindValue`](#bindvalue) for more information about binding values.\n * @example\n * ```ts\n * const statement = await db.prepareAsync('SELECT * FROM test');\n * for await (const row of statement.eachAsync<any>()) {\n * console.log(row);\n * }\n * await statement.finalizeAsync();\n * ```\n */\n public eachAsync<T>(params: BindParams): AsyncIterableIterator<T>;\n /**\n * @hidden\n */\n public eachAsync<T>(...params: VariadicBindParams): AsyncIterableIterator<T>;\n public async *eachAsync<T>(...params: unknown[]): AsyncIterableIterator<T> {\n const { params: bindParams, shouldPassAsObject } = normalizeParams(...params);\n const func = shouldPassAsObject\n ? this.nativeStatement.objectGetAsync.bind(this.nativeStatement)\n : this.nativeStatement.arrayGetAsync.bind(this.nativeStatement);\n\n const columnNames = await this.getColumnNamesAsync();\n let result = null;\n do {\n result = await func(this.nativeDatabase, bindParams);\n if (result != null) {\n yield composeRow<T>(columnNames, result);\n }\n } while (result != null);\n }\n\n /**\n * Get one row from the prepared statement.\n * @param params The parameters to bind to the prepared statement. You can pass values in array, object, or variadic arguments. See [`BindValue`](#bindvalue) for more information about binding values.\n */\n public getAsync<T>(params: BindParams): Promise<T | null>;\n /**\n * @hidden\n */\n public getAsync<T>(...params: VariadicBindParams): Promise<T | null>;\n public async getAsync<T>(...params: unknown[]): Promise<T | null> {\n const { params: bindParams, shouldPassAsObject } = normalizeParams(...params);\n const columnNames = await this.getColumnNamesAsync();\n const columnValues = shouldPassAsObject\n ? await this.nativeStatement.objectGetAsync(this.nativeDatabase, bindParams)\n : await this.nativeStatement.arrayGetAsync(this.nativeDatabase, bindParams);\n return columnValues != null ? composeRow<T>(columnNames, columnValues) : null;\n }\n\n /**\n * Get all rows from the prepared statement.\n * @param params The parameters to bind to the prepared statement. You can pass values in array, object, or variadic arguments. See [`BindValue`](#bindvalue) for more information about binding values.\n */\n public allAsync<T>(params: BindParams): Promise<T[]>;\n /**\n * @hidden\n */\n public allAsync<T>(...params: VariadicBindParams): Promise<T[]>;\n public async allAsync<T>(...params: unknown[]): Promise<T[]> {\n const { params: bindParams, shouldPassAsObject } = normalizeParams(...params);\n const columnNames = await this.getColumnNamesAsync();\n const columnValuesList = shouldPassAsObject\n ? await this.nativeStatement.objectGetAllAsync(this.nativeDatabase, bindParams)\n : await this.nativeStatement.arrayGetAllAsync(this.nativeDatabase, bindParams);\n return composeRows<T>(columnNames, columnValuesList);\n }\n\n /**\n * Get the column names of the prepared statement.\n */\n public getColumnNamesAsync(): Promise<string[]> {\n return this.nativeStatement.getColumnNamesAsync();\n }\n\n /**\n * Reset the prepared statement cursor.\n */\n public async resetAsync(): Promise<void> {\n await this.nativeStatement.resetAsync(this.nativeDatabase);\n }\n\n /**\n * Finalize the prepared statement.\n * > **Note:** Remember to finalize the prepared statement whenever you call `prepareAsync()` to avoid resource leaks.\n */\n public async finalizeAsync(): Promise<void> {\n await this.nativeStatement.finalizeAsync(this.nativeDatabase);\n }\n\n //#endregion\n\n //#region Synchronous API\n\n /**\n * Run the prepared statement and return the result.\n * > **Note:** Running heavy tasks with this function can block the JavaScript thread and affect performance.\n * @param params The parameters to bind to the prepared statement. You can pass values in array, object, or variadic arguments. See [`BindValue`](#bindvalue) for more information about binding values.\n */\n public runSync(params: BindParams): RunResult;\n /**\n * @hidden\n */\n public runSync(...params: VariadicBindParams): RunResult;\n public runSync(...params: unknown[]): RunResult {\n const { params: bindParams, shouldPassAsObject } = normalizeParams(...params);\n if (shouldPassAsObject) {\n return this.nativeStatement.objectRunSync(this.nativeDatabase, bindParams);\n } else {\n return this.nativeStatement.arrayRunSync(this.nativeDatabase, bindParams);\n }\n }\n\n /**\n * Iterate the prepared statement and return results as an iterable.\n * > **Note:** Running heavy tasks with this function can block the JavaScript thread and affect performance.\n * @param params The parameters to bind to the prepared statement. You can pass values in array, object, or variadic arguments. See [`BindValue`](#bindvalue) for more information about binding values.\n */\n public eachSync<T>(params: BindParams): IterableIterator<T>;\n /**\n * @hidden\n */\n public eachSync<T>(...params: VariadicBindParams): IterableIterator<T>;\n public *eachSync<T>(...params: unknown[]): IterableIterator<T> {\n const { params: bindParams, shouldPassAsObject } = normalizeParams(...params);\n const func = shouldPassAsObject\n ? this.nativeStatement.objectGetSync.bind(this.nativeStatement)\n : this.nativeStatement.arrayGetSync.bind(this.nativeStatement);\n\n const columnNames = this.getColumnNamesSync();\n let result = null;\n do {\n result = func(this.nativeDatabase, bindParams);\n if (result != null) {\n yield composeRow<T>(columnNames, result);\n }\n } while (result != null);\n }\n\n /**\n * Get one row from the prepared statement.\n * > **Note:** Running heavy tasks with this function can block the JavaScript thread and affect performance.\n * @param params The parameters to bind to the prepared statement. You can pass values in array, object, or variadic arguments. See [`BindValue`](#bindvalue) for more information about binding values.\n */\n public getSync<T>(params: BindParams): T | null;\n /**\n * @hidden\n */\n public getSync<T>(...params: VariadicBindParams): T | null;\n public getSync<T>(...params: unknown[]): T | null {\n const { params: bindParams, shouldPassAsObject } = normalizeParams(...params);\n const columnNames = this.getColumnNamesSync();\n const columnValues = shouldPassAsObject\n ? this.nativeStatement.objectGetSync(this.nativeDatabase, bindParams)\n : this.nativeStatement.arrayGetSync(this.nativeDatabase, bindParams);\n return columnValues != null ? composeRow<T>(columnNames, columnValues) : null;\n }\n\n /**\n * Get all rows from the prepared statement.\n * > **Note:** Running heavy tasks with this function can block the JavaScript thread and affect performance.\n * @param params The parameters to bind to the prepared statement. You can pass values in array, object, or variadic arguments. See [`BindValue`](#bindvalue) for more information about binding values.\n */\n public allSync<T>(params: BindParams): T[];\n /**\n * @hidden\n */\n public allSync<T>(...params: VariadicBindParams): T[];\n public allSync<T>(...params: unknown[]): T[] {\n const { params: bindParams, shouldPassAsObject } = normalizeParams(...params);\n const columnNames = this.getColumnNamesSync();\n const columnValuesList = shouldPassAsObject\n ? this.nativeStatement.objectGetAllSync(this.nativeDatabase, bindParams)\n : this.nativeStatement.arrayGetAllSync(this.nativeDatabase, bindParams);\n return composeRows<T>(columnNames, columnValuesList);\n }\n\n /**\n * Get the column names of the prepared statement.\n */\n public getColumnNamesSync(): string[] {\n return this.nativeStatement.getColumnNamesSync();\n }\n\n /**\n * Reset the prepared statement cursor.\n */\n public resetSync(): void {\n this.nativeStatement.resetSync(this.nativeDatabase);\n }\n\n /**\n * Finalize the prepared statement.\n *\n * > **Note:** Remember to finalize the prepared statement whenever you call `prepareSync()` to avoid resource leaks.\n *\n */\n public finalizeSync(): void {\n this.nativeStatement.finalizeSync(this.nativeDatabase);\n }\n\n //#endregion\n}\n\n/**\n * Normalize the bind params to an array or object.\n * @hidden\n */\nexport function normalizeParams(...params: any[]): {\n params: BindParams;\n shouldPassAsObject: boolean;\n} {\n let bindParams = params.length > 1 ? params : (params[0] as BindParams);\n if (bindParams == null) {\n bindParams = [];\n }\n if (typeof bindParams !== 'object') {\n bindParams = [bindParams];\n }\n const shouldPassAsObject = !Array.isArray(bindParams);\n return {\n params: bindParams,\n shouldPassAsObject,\n };\n}\n\n/**\n * Compose `columnNames` and `columnValues` to an row object.\n * @hidden\n */\nexport function composeRow<T>(columnNames: ColumnNames, columnValues: ColumnValues): T {\n const row = {};\n if (columnNames.length !== columnValues.length) {\n throw new Error(\n `Column names and values count mismatch. Names: ${columnNames.length}, Values: ${columnValues.length}`\n );\n }\n for (let i = 0; i < columnNames.length; i++) {\n row[columnNames[i]] = columnValues[i];\n }\n return row as T;\n}\n\n/**\n * Compose `columnNames` and `columnValuesList` to an array of row objects.\n * @hidden\n */\nexport function composeRows<T>(columnNames: ColumnNames, columnValuesList: ColumnValues[]): T[] {\n if (columnValuesList.length === 0) {\n return [];\n }\n if (columnNames.length !== columnValuesList[0].length) {\n // We only check the first row because SQLite returns the same column count for all rows.\n throw new Error(\n `Column names and values count mismatch. Names: ${columnNames.length}, Values: ${columnValuesList[0].length}`\n );\n }\n const results: T[] = [];\n for (const columnValues of columnValuesList) {\n const row = {};\n for (let i = 0; i < columnNames.length; i++) {\n row[columnNames[i]] = columnValues[i];\n }\n results.push(row as T);\n }\n return results;\n}\n"]}
|
|
1
|
+
{"version":3,"file":"Statement.js","sourceRoot":"","sources":["../../src/next/Statement.ts"],"names":[],"mappings":"AAeA;;GAEG;AACH,MAAM,OAAO,SAAS;IAED;IACA;IAFnB,YACmB,cAA8B,EAC9B,eAAgC;QADhC,mBAAc,GAAd,cAAc,CAAgB;QAC9B,oBAAe,GAAf,eAAe,CAAiB;IAChD,CAAC;IAaG,KAAK,CAAC,QAAQ,CAAC,GAAG,MAAiB;QACxC,OAAO,MAAM,IAAI,CAAC,eAAe,CAAC,QAAQ,CAAC,IAAI,CAAC,cAAc,EAAE,GAAG,eAAe,CAAC,GAAG,MAAM,CAAC,CAAC,CAAC;IACjG,CAAC;IAmBM,KAAK,CAAC,CAAC,SAAS,CAAI,GAAG,MAAiB;QAC7C,MAAM,UAAU,GAAG,eAAe,CAAC,GAAG,MAAM,CAAC,CAAC;QAC9C,MAAM,IAAI,GAAG,IAAI,CAAC,eAAe,CAAC,QAAQ,CAAC,IAAI,CAAC,IAAI,CAAC,eAAe,CAAC,CAAC;QAEtE,MAA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D,WAAW,CAAC,MAAM,aAAa,gBAAgB,CAAC,CAAC,CAAC,CAAC,MAAM,EAAE,CAC9G,CAAC;KACH;IACD,MAAM,OAAO,GAAQ,EAAE,CAAC;IACxB,KAAK,MAAM,YAAY,IAAI,gBAAgB,EAAE;QAC3C,MAAM,GAAG,GAAG,EAAE,CAAC;QACf,KAAK,IAAI,CAAC,GAAG,CAAC,EAAE,CAAC,GAAG,WAAW,CAAC,MAAM,EAAE,CAAC,EAAE,EAAE;YAC3C,GAAG,CAAC,WAAW,CAAC,CAAC,CAAC,CAAC,GAAG,YAAY,CAAC,CAAC,CAAC,CAAC;SACvC;QACD,OAAO,CAAC,IAAI,CAAC,GAAQ,CAAC,CAAC;KACxB;IACD,OAAO,OAAO,CAAC;AACjB,CAAC","sourcesContent":["import { NativeDatabase } from './NativeDatabase';\nimport {\n BindBlobParams,\n BindParams,\n BindPrimitiveParams,\n BindValue,\n NativeStatement,\n RunResult,\n VariadicBindParams,\n type ColumnNames,\n type ColumnValues,\n} from './NativeStatement';\n\nexport { BindParams, BindValue, RunResult, VariadicBindParams };\n\n/**\n * A prepared statement returned by [`Database.prepareAsync()`](#prepareasyncsource) or [`Database.prepareSync()`](#preparesyncsource) that can be binded with parameters and executed.\n */\nexport class Statement {\n constructor(\n private readonly nativeDatabase: NativeDatabase,\n private readonly nativeStatement: NativeStatement\n ) {}\n\n //#region Asynchronous API\n\n /**\n * Run the prepared statement and return the result.\n * @param params The parameters to bind to the prepared statement. You can pass values in array, object, or variadic arguments. See [`BindValue`](#bindvalue) for more information about binding values.\n */\n public runAsync(params: BindParams): Promise<RunResult>;\n /**\n * @hidden\n */\n public runAsync(...params: VariadicBindParams): Promise<RunResult>;\n public async runAsync(...params: unknown[]): Promise<RunResult> {\n return await this.nativeStatement.runAsync(this.nativeDatabase, ...normalizeParams(...params));\n }\n\n /**\n * Iterate the prepared statement and return results as an async iterable.\n * @param params The parameters to bind to the prepared statement. You can pass values in array, object, or variadic arguments. See [`BindValue`](#bindvalue) for more information about binding values.\n * @example\n * ```ts\n * const statement = await db.prepareAsync('SELECT * FROM test');\n * for await (const row of statement.eachAsync<any>()) {\n * console.log(row);\n * }\n * await statement.finalizeAsync();\n * ```\n */\n public eachAsync<T>(params: BindParams): AsyncIterableIterator<T>;\n /**\n * @hidden\n */\n public eachAsync<T>(...params: VariadicBindParams): AsyncIterableIterator<T>;\n public async *eachAsync<T>(...params: unknown[]): AsyncIterableIterator<T> {\n const paramTuple = normalizeParams(...params);\n const func = this.nativeStatement.getAsync.bind(this.nativeStatement);\n\n const columnNames = await this.getColumnNamesAsync();\n let result = null;\n do {\n result = await func(this.nativeDatabase, ...paramTuple);\n if (result != null) {\n yield composeRow<T>(columnNames, result);\n }\n } while (result != null);\n }\n\n /**\n * Get one row from the prepared statement.\n * @param params The parameters to bind to the prepared statement. You can pass values in array, object, or variadic arguments. See [`BindValue`](#bindvalue) for more information about binding values.\n */\n public getAsync<T>(params: BindParams): Promise<T | null>;\n /**\n * @hidden\n */\n public getAsync<T>(...params: VariadicBindParams): Promise<T | null>;\n public async getAsync<T>(...params: unknown[]): Promise<T | null> {\n const columnNames = await this.getColumnNamesAsync();\n const columnValues = await this.nativeStatement.getAsync(\n this.nativeDatabase,\n ...normalizeParams(...params)\n );\n return columnValues != null ? composeRow<T>(columnNames, columnValues) : null;\n }\n\n /**\n * Get all rows from the prepared statement.\n * @param params The parameters to bind to the prepared statement. You can pass values in array, object, or variadic arguments. See [`BindValue`](#bindvalue) for more information about binding values.\n */\n public allAsync<T>(params: BindParams): Promise<T[]>;\n /**\n * @hidden\n */\n public allAsync<T>(...params: VariadicBindParams): Promise<T[]>;\n public async allAsync<T>(...params: unknown[]): Promise<T[]> {\n const columnNames = await this.getColumnNamesAsync();\n const columnValuesList = await this.nativeStatement.getAllAsync(\n this.nativeDatabase,\n ...normalizeParams(...params)\n );\n return composeRows<T>(columnNames, columnValuesList);\n }\n\n /**\n * Get the column names of the prepared statement.\n */\n public getColumnNamesAsync(): Promise<string[]> {\n return this.nativeStatement.getColumnNamesAsync();\n }\n\n /**\n * Reset the prepared statement cursor.\n */\n public async resetAsync(): Promise<void> {\n await this.nativeStatement.resetAsync(this.nativeDatabase);\n }\n\n /**\n * Finalize the prepared statement.\n * > **Note:** Remember to finalize the prepared statement whenever you call `prepareAsync()` to avoid resource leaks.\n */\n public async finalizeAsync(): Promise<void> {\n await this.nativeStatement.finalizeAsync(this.nativeDatabase);\n }\n\n //#endregion\n\n //#region Synchronous API\n\n /**\n * Run the prepared statement and return the result.\n * > **Note:** Running heavy tasks with this function can block the JavaScript thread and affect performance.\n * @param params The parameters to bind to the prepared statement. You can pass values in array, object, or variadic arguments. See [`BindValue`](#bindvalue) for more information about binding values.\n */\n public runSync(params: BindParams): RunResult;\n /**\n * @hidden\n */\n public runSync(...params: VariadicBindParams): RunResult;\n public runSync(...params: unknown[]): RunResult {\n return this.nativeStatement.runSync(this.nativeDatabase, ...normalizeParams(...params));\n }\n\n /**\n * Iterate the prepared statement and return results as an iterable.\n * > **Note:** Running heavy tasks with this function can block the JavaScript thread and affect performance.\n * @param params The parameters to bind to the prepared statement. You can pass values in array, object, or variadic arguments. See [`BindValue`](#bindvalue) for more information about binding values.\n */\n public eachSync<T>(params: BindParams): IterableIterator<T>;\n /**\n * @hidden\n */\n public eachSync<T>(...params: VariadicBindParams): IterableIterator<T>;\n public *eachSync<T>(...params: unknown[]): IterableIterator<T> {\n const paramTuple = normalizeParams(...params);\n const func = this.nativeStatement.getSync.bind(this.nativeStatement);\n\n const columnNames = this.getColumnNamesSync();\n let result = null;\n do {\n result = func(this.nativeDatabase, ...paramTuple);\n if (result != null) {\n yield composeRow<T>(columnNames, result);\n }\n } while (result != null);\n }\n\n /**\n * Get one row from the prepared statement.\n * > **Note:** Running heavy tasks with this function can block the JavaScript thread and affect performance.\n * @param params The parameters to bind to the prepared statement. You can pass values in array, object, or variadic arguments. See [`BindValue`](#bindvalue) for more information about binding values.\n */\n public getSync<T>(params: BindParams): T | null;\n /**\n * @hidden\n */\n public getSync<T>(...params: VariadicBindParams): T | null;\n public getSync<T>(...params: unknown[]): T | null {\n const columnNames = this.getColumnNamesSync();\n const columnValues = this.nativeStatement.getSync(\n this.nativeDatabase,\n ...normalizeParams(...params)\n );\n return columnValues != null ? composeRow<T>(columnNames, columnValues) : null;\n }\n\n /**\n * Get all rows from the prepared statement.\n * > **Note:** Running heavy tasks with this function can block the JavaScript thread and affect performance.\n * @param params The parameters to bind to the prepared statement. You can pass values in array, object, or variadic arguments. See [`BindValue`](#bindvalue) for more information about binding values.\n */\n public allSync<T>(params: BindParams): T[];\n /**\n * @hidden\n */\n public allSync<T>(...params: VariadicBindParams): T[];\n public allSync<T>(...params: unknown[]): T[] {\n const columnNames = this.getColumnNamesSync();\n const columnValuesList = this.nativeStatement.getAllSync(\n this.nativeDatabase,\n ...normalizeParams(...params)\n );\n return composeRows<T>(columnNames, columnValuesList);\n }\n\n /**\n * Get the column names of the prepared statement.\n */\n public getColumnNamesSync(): string[] {\n return this.nativeStatement.getColumnNamesSync();\n }\n\n /**\n * Reset the prepared statement cursor.\n */\n public resetSync(): void {\n this.nativeStatement.resetSync(this.nativeDatabase);\n }\n\n /**\n * Finalize the prepared statement.\n *\n * > **Note:** Remember to finalize the prepared statement whenever you call `prepareSync()` to avoid resource leaks.\n *\n */\n public finalizeSync(): void {\n this.nativeStatement.finalizeSync(this.nativeDatabase);\n }\n\n //#endregion\n}\n\n/**\n * Normalize the bind params to data structure that can be passed to native module.\n * The data structure is a tuple of [primitiveParams, blobParams, shouldPassAsArray].\n * @hidden\n */\nexport function normalizeParams(...params: any[]): [BindPrimitiveParams, BindBlobParams, boolean] {\n let bindParams = params.length > 1 ? params : (params[0] as BindParams);\n if (bindParams == null) {\n bindParams = [];\n }\n if (\n typeof bindParams !== 'object' ||\n bindParams instanceof ArrayBuffer ||\n ArrayBuffer.isView(bindParams)\n ) {\n bindParams = [bindParams];\n }\n const shouldPassAsArray = Array.isArray(bindParams);\n if (Array.isArray(bindParams)) {\n bindParams = bindParams.reduce<Record<string, BindValue>>((acc, value, index) => {\n acc[index] = value;\n return acc;\n }, {});\n }\n\n const primitiveParams: BindPrimitiveParams = {};\n const blobParams: BindBlobParams = {};\n for (const key in bindParams) {\n const value = bindParams[key];\n if (value instanceof Uint8Array) {\n blobParams[key] = value;\n } else {\n primitiveParams[key] = value;\n }\n }\n\n return [primitiveParams, blobParams, shouldPassAsArray];\n}\n\n/**\n * Compose `columnNames` and `columnValues` to an row object.\n * @hidden\n */\nexport function composeRow<T>(columnNames: ColumnNames, columnValues: ColumnValues): T {\n const row = {};\n if (columnNames.length !== columnValues.length) {\n throw new Error(\n `Column names and values count mismatch. Names: ${columnNames.length}, Values: ${columnValues.length}`\n );\n }\n for (let i = 0; i < columnNames.length; i++) {\n row[columnNames[i]] = columnValues[i];\n }\n return row as T;\n}\n\n/**\n * Compose `columnNames` and `columnValuesList` to an array of row objects.\n * @hidden\n */\nexport function composeRows<T>(columnNames: ColumnNames, columnValuesList: ColumnValues[]): T[] {\n if (columnValuesList.length === 0) {\n return [];\n }\n if (columnNames.length !== columnValuesList[0].length) {\n // We only check the first row because SQLite returns the same column count for all rows.\n throw new Error(\n `Column names and values count mismatch. Names: ${columnNames.length}, Values: ${columnValuesList[0].length}`\n );\n }\n const results: T[] = [];\n for (const columnValues of columnValuesList) {\n const row = {};\n for (let i = 0; i < columnNames.length; i++) {\n row[columnNames[i]] = columnValues[i];\n }\n results.push(row as T);\n }\n return results;\n}\n"]}
|
package/ios/Exceptions.swift
CHANGED
|
@@ -52,6 +52,18 @@ internal class InvalidArgumentsException: Exception {
|
|
|
52
52
|
}
|
|
53
53
|
}
|
|
54
54
|
|
|
55
|
+
internal class InvalidBindParameterException: Exception {
|
|
56
|
+
override var reason: String {
|
|
57
|
+
"Invalid bind parameter"
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
internal class AccessClosedResourceException: Exception {
|
|
62
|
+
override var reason: String {
|
|
63
|
+
"Access to closed resource"
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
|
|
55
67
|
internal class SQLiteErrorException: GenericException<String> {
|
|
56
68
|
override var code: String {
|
|
57
69
|
"ERR_INTERNAL_SQLITE_ERROR"
|
package/ios/NativeDatabase.swift
CHANGED
|
@@ -5,6 +5,7 @@ import ExpoModulesCore
|
|
|
5
5
|
final class NativeDatabase: SharedRef<OpaquePointer?>, Equatable, Hashable {
|
|
6
6
|
let dbName: String
|
|
7
7
|
let openOptions: OpenDatabaseOptions
|
|
8
|
+
var isClosed = false
|
|
8
9
|
|
|
9
10
|
init(_ pointer: OpaquePointer?, dbName: String, openOptions: OpenDatabaseOptions) {
|
|
10
11
|
self.dbName = dbName
|
package/ios/SQLiteModule.swift
CHANGED
|
@@ -63,12 +63,11 @@ public final class SQLiteModule: Module {
|
|
|
63
63
|
}
|
|
64
64
|
|
|
65
65
|
private func pathForDatabaseName(name: String) -> URL? {
|
|
66
|
-
guard let
|
|
66
|
+
guard let path = appContext?.config.documentDirectory?.path else {
|
|
67
67
|
return nil
|
|
68
68
|
}
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
fileSystem.ensureDirExists(withPath: directory?.absoluteString)
|
|
69
|
+
let directory = URL(string: path)?.appendingPathComponent("SQLite")
|
|
70
|
+
FileSystemUtilities.ensureDirExists(at: directory)
|
|
72
71
|
|
|
73
72
|
return directory?.appendingPathComponent(name)
|
|
74
73
|
}
|