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.
@@ -1,6 +1,8 @@
1
1
  import { NativeDatabase } from './NativeDatabase';
2
2
  import {
3
+ BindBlobParams,
3
4
  BindParams,
5
+ BindPrimitiveParams,
4
6
  BindValue,
5
7
  NativeStatement,
6
8
  RunResult,
@@ -32,12 +34,7 @@ export class Statement {
32
34
  */
33
35
  public runAsync(...params: VariadicBindParams): Promise<RunResult>;
34
36
  public async runAsync(...params: unknown[]): Promise<RunResult> {
35
- const { params: bindParams, shouldPassAsObject } = normalizeParams(...params);
36
- if (shouldPassAsObject) {
37
- return await this.nativeStatement.objectRunAsync(this.nativeDatabase, bindParams);
38
- } else {
39
- return await this.nativeStatement.arrayRunAsync(this.nativeDatabase, bindParams);
40
- }
37
+ return await this.nativeStatement.runAsync(this.nativeDatabase, ...normalizeParams(...params));
41
38
  }
42
39
 
43
40
  /**
@@ -58,15 +55,13 @@ export class Statement {
58
55
  */
59
56
  public eachAsync<T>(...params: VariadicBindParams): AsyncIterableIterator<T>;
60
57
  public async *eachAsync<T>(...params: unknown[]): AsyncIterableIterator<T> {
61
- const { params: bindParams, shouldPassAsObject } = normalizeParams(...params);
62
- const func = shouldPassAsObject
63
- ? this.nativeStatement.objectGetAsync.bind(this.nativeStatement)
64
- : this.nativeStatement.arrayGetAsync.bind(this.nativeStatement);
58
+ const paramTuple = normalizeParams(...params);
59
+ const func = this.nativeStatement.getAsync.bind(this.nativeStatement);
65
60
 
66
61
  const columnNames = await this.getColumnNamesAsync();
67
62
  let result = null;
68
63
  do {
69
- result = await func(this.nativeDatabase, bindParams);
64
+ result = await func(this.nativeDatabase, ...paramTuple);
70
65
  if (result != null) {
71
66
  yield composeRow<T>(columnNames, result);
72
67
  }
@@ -83,11 +78,11 @@ export class Statement {
83
78
  */
84
79
  public getAsync<T>(...params: VariadicBindParams): Promise<T | null>;
85
80
  public async getAsync<T>(...params: unknown[]): Promise<T | null> {
86
- const { params: bindParams, shouldPassAsObject } = normalizeParams(...params);
87
81
  const columnNames = await this.getColumnNamesAsync();
88
- const columnValues = shouldPassAsObject
89
- ? await this.nativeStatement.objectGetAsync(this.nativeDatabase, bindParams)
90
- : await this.nativeStatement.arrayGetAsync(this.nativeDatabase, bindParams);
82
+ const columnValues = await this.nativeStatement.getAsync(
83
+ this.nativeDatabase,
84
+ ...normalizeParams(...params)
85
+ );
91
86
  return columnValues != null ? composeRow<T>(columnNames, columnValues) : null;
92
87
  }
93
88
 
@@ -101,11 +96,11 @@ export class Statement {
101
96
  */
102
97
  public allAsync<T>(...params: VariadicBindParams): Promise<T[]>;
103
98
  public async allAsync<T>(...params: unknown[]): Promise<T[]> {
104
- const { params: bindParams, shouldPassAsObject } = normalizeParams(...params);
105
99
  const columnNames = await this.getColumnNamesAsync();
106
- const columnValuesList = shouldPassAsObject
107
- ? await this.nativeStatement.objectGetAllAsync(this.nativeDatabase, bindParams)
108
- : await this.nativeStatement.arrayGetAllAsync(this.nativeDatabase, bindParams);
100
+ const columnValuesList = await this.nativeStatement.getAllAsync(
101
+ this.nativeDatabase,
102
+ ...normalizeParams(...params)
103
+ );
109
104
  return composeRows<T>(columnNames, columnValuesList);
110
105
  }
111
106
 
@@ -146,12 +141,7 @@ export class Statement {
146
141
  */
147
142
  public runSync(...params: VariadicBindParams): RunResult;
148
143
  public runSync(...params: unknown[]): RunResult {
149
- const { params: bindParams, shouldPassAsObject } = normalizeParams(...params);
150
- if (shouldPassAsObject) {
151
- return this.nativeStatement.objectRunSync(this.nativeDatabase, bindParams);
152
- } else {
153
- return this.nativeStatement.arrayRunSync(this.nativeDatabase, bindParams);
154
- }
144
+ return this.nativeStatement.runSync(this.nativeDatabase, ...normalizeParams(...params));
155
145
  }
156
146
 
157
147
  /**
@@ -165,15 +155,13 @@ export class Statement {
165
155
  */
166
156
  public eachSync<T>(...params: VariadicBindParams): IterableIterator<T>;
167
157
  public *eachSync<T>(...params: unknown[]): IterableIterator<T> {
168
- const { params: bindParams, shouldPassAsObject } = normalizeParams(...params);
169
- const func = shouldPassAsObject
170
- ? this.nativeStatement.objectGetSync.bind(this.nativeStatement)
171
- : this.nativeStatement.arrayGetSync.bind(this.nativeStatement);
158
+ const paramTuple = normalizeParams(...params);
159
+ const func = this.nativeStatement.getSync.bind(this.nativeStatement);
172
160
 
173
161
  const columnNames = this.getColumnNamesSync();
174
162
  let result = null;
175
163
  do {
176
- result = func(this.nativeDatabase, bindParams);
164
+ result = func(this.nativeDatabase, ...paramTuple);
177
165
  if (result != null) {
178
166
  yield composeRow<T>(columnNames, result);
179
167
  }
@@ -191,11 +179,11 @@ export class Statement {
191
179
  */
192
180
  public getSync<T>(...params: VariadicBindParams): T | null;
193
181
  public getSync<T>(...params: unknown[]): T | null {
194
- const { params: bindParams, shouldPassAsObject } = normalizeParams(...params);
195
182
  const columnNames = this.getColumnNamesSync();
196
- const columnValues = shouldPassAsObject
197
- ? this.nativeStatement.objectGetSync(this.nativeDatabase, bindParams)
198
- : this.nativeStatement.arrayGetSync(this.nativeDatabase, bindParams);
183
+ const columnValues = this.nativeStatement.getSync(
184
+ this.nativeDatabase,
185
+ ...normalizeParams(...params)
186
+ );
199
187
  return columnValues != null ? composeRow<T>(columnNames, columnValues) : null;
200
188
  }
201
189
 
@@ -210,11 +198,11 @@ export class Statement {
210
198
  */
211
199
  public allSync<T>(...params: VariadicBindParams): T[];
212
200
  public allSync<T>(...params: unknown[]): T[] {
213
- const { params: bindParams, shouldPassAsObject } = normalizeParams(...params);
214
201
  const columnNames = this.getColumnNamesSync();
215
- const columnValuesList = shouldPassAsObject
216
- ? this.nativeStatement.objectGetAllSync(this.nativeDatabase, bindParams)
217
- : this.nativeStatement.arrayGetAllSync(this.nativeDatabase, bindParams);
202
+ const columnValuesList = this.nativeStatement.getAllSync(
203
+ this.nativeDatabase,
204
+ ...normalizeParams(...params)
205
+ );
218
206
  return composeRows<T>(columnNames, columnValuesList);
219
207
  }
220
208
 
@@ -246,25 +234,42 @@ export class Statement {
246
234
  }
247
235
 
248
236
  /**
249
- * Normalize the bind params to an array or object.
237
+ * Normalize the bind params to data structure that can be passed to native module.
238
+ * The data structure is a tuple of [primitiveParams, blobParams, shouldPassAsArray].
250
239
  * @hidden
251
240
  */
252
- export function normalizeParams(...params: any[]): {
253
- params: BindParams;
254
- shouldPassAsObject: boolean;
255
- } {
241
+ export function normalizeParams(...params: any[]): [BindPrimitiveParams, BindBlobParams, boolean] {
256
242
  let bindParams = params.length > 1 ? params : (params[0] as BindParams);
257
243
  if (bindParams == null) {
258
244
  bindParams = [];
259
245
  }
260
- if (typeof bindParams !== 'object') {
246
+ if (
247
+ typeof bindParams !== 'object' ||
248
+ bindParams instanceof ArrayBuffer ||
249
+ ArrayBuffer.isView(bindParams)
250
+ ) {
261
251
  bindParams = [bindParams];
262
252
  }
263
- const shouldPassAsObject = !Array.isArray(bindParams);
264
- return {
265
- params: bindParams,
266
- shouldPassAsObject,
267
- };
253
+ const shouldPassAsArray = Array.isArray(bindParams);
254
+ if (Array.isArray(bindParams)) {
255
+ bindParams = bindParams.reduce<Record<string, BindValue>>((acc, value, index) => {
256
+ acc[index] = value;
257
+ return acc;
258
+ }, {});
259
+ }
260
+
261
+ const primitiveParams: BindPrimitiveParams = {};
262
+ const blobParams: BindBlobParams = {};
263
+ for (const key in bindParams) {
264
+ const value = bindParams[key];
265
+ if (value instanceof Uint8Array) {
266
+ blobParams[key] = value;
267
+ } else {
268
+ primitiveParams[key] = value;
269
+ }
270
+ }
271
+
272
+ return [primitiveParams, blobParams, shouldPassAsArray];
268
273
  }
269
274
 
270
275
  /**