|
12 | 12 |
|
13 | 13 | import { SQLiteCloudConnection } from './connection' |
14 | 14 | import { SQLiteCloudRowset } from './rowset' |
15 | | -import { SQLiteCloudConfig, SQLiteCloudError, RowCountCallback, SQLiteCloudArrayType } from './types' |
16 | | -import { prepareSql, popCallback } from './utilities' |
17 | | -import { Statement } from './statement' |
| 15 | +import { SQLiteCloudConfig, SQLiteCloudError, RowCountCallback, SQLiteCloudArrayType, SQLiteCloudCommand } from './types' |
| 16 | +import { popCallback } from './utilities' |
18 | 17 | import { ErrorCallback, ResultsCallback, RowCallback, RowsCallback } from './types' |
19 | 18 | import EventEmitter from 'eventemitter3' |
20 | 19 | import { isBrowser } from './utilities' |
21 | 20 | import { PubSub } from './pubsub' |
| 21 | +import { Statement } from './statement' |
22 | 22 |
|
23 | 23 | // Uses eventemitter3 instead of node events for browser compatibility |
24 | 24 | // https://github.com/primus/eventemitter3 |
@@ -204,12 +204,12 @@ export class Database extends EventEmitter { |
204 | 204 | public run<T>(sql: string, params: any, callback?: ResultsCallback<T>): this |
205 | 205 | public run(sql: string, ...params: any[]): this { |
206 | 206 | const { args, callback } = popCallback<ResultsCallback>(params) |
207 | | - const preparedSql = args?.length > 0 ? prepareSql(sql, ...args) : sql |
| 207 | + const command: SQLiteCloudCommand = { query: sql, parameters: args?.flat() } |
208 | 208 | this.getConnection((error, connection) => { |
209 | 209 | if (error || !connection) { |
210 | 210 | this.handleError(null, error as Error, callback) |
211 | 211 | } else { |
212 | | - connection.sendCommands(preparedSql, (error, results) => { |
| 212 | + connection.sendCommands(command, (error, results) => { |
213 | 213 | if (error) { |
214 | 214 | this.handleError(connection, error, callback) |
215 | 215 | } else { |
@@ -237,12 +237,12 @@ export class Database extends EventEmitter { |
237 | 237 | public get<T>(sql: string, params: any, callback?: RowCallback<T>): this |
238 | 238 | public get(sql: string, ...params: any[]): this { |
239 | 239 | const { args, callback } = popCallback<RowCallback>(params) |
240 | | - const preparedSql = args?.length > 0 ? prepareSql(sql, ...args) : sql |
| 240 | + const command: SQLiteCloudCommand = { query: sql, parameters: args?.flat() } |
241 | 241 | this.getConnection((error, connection) => { |
242 | 242 | if (error || !connection) { |
243 | 243 | this.handleError(null, error as Error, callback) |
244 | 244 | } else { |
245 | | - connection.sendCommands(preparedSql, (error, results) => { |
| 245 | + connection.sendCommands(command, (error, results) => { |
246 | 246 | if (error) { |
247 | 247 | this.handleError(connection, error, callback) |
248 | 248 | } else { |
@@ -275,12 +275,12 @@ export class Database extends EventEmitter { |
275 | 275 | public all<T>(sql: string, params: any, callback?: RowsCallback<T>): this |
276 | 276 | public all(sql: string, ...params: any[]): this { |
277 | 277 | const { args, callback } = popCallback<RowsCallback>(params) |
278 | | - const preparedSql = args?.length > 0 ? prepareSql(sql, ...args) : sql |
| 278 | + const command: SQLiteCloudCommand = { query: sql, parameters: args?.flat() } |
279 | 279 | this.getConnection((error, connection) => { |
280 | 280 | if (error || !connection) { |
281 | 281 | this.handleError(null, error as Error, callback) |
282 | 282 | } else { |
283 | | - connection.sendCommands(preparedSql, (error, results) => { |
| 283 | + connection.sendCommands(command, (error, results) => { |
284 | 284 | if (error) { |
285 | 285 | this.handleError(connection, error, callback) |
286 | 286 | } else { |
@@ -316,12 +316,12 @@ export class Database extends EventEmitter { |
316 | 316 | // extract optional parameters and one or two callbacks |
317 | 317 | const { args, callback, complete } = popCallback<RowCallback>(params) |
318 | 318 |
|
319 | | - const preparedSql = args?.length > 0 ? prepareSql(sql, ...args) : sql |
| 319 | + const command: SQLiteCloudCommand = { query: sql, parameters: args?.flat() } |
320 | 320 | this.getConnection((error, connection) => { |
321 | 321 | if (error || !connection) { |
322 | 322 | this.handleError(null, error as Error, callback) |
323 | 323 | } else { |
324 | | - connection.sendCommands(preparedSql, (error, rowset) => { |
| 324 | + connection.sendCommands(command, (error, rowset) => { |
325 | 325 | if (error) { |
326 | 326 | this.handleError(connection, error, callback) |
327 | 327 | } else { |
@@ -352,8 +352,7 @@ export class Database extends EventEmitter { |
352 | 352 | * they are bound to the prepared statement before calling the callback. |
353 | 353 | */ |
354 | 354 | public prepare<T = any>(sql: string, ...params: any[]): Statement<T> { |
355 | | - const { args, callback } = popCallback(params) |
356 | | - return new Statement(this, sql, ...args, callback) |
| 355 | + return new Statement(this, sql, ...params) |
357 | 356 | } |
358 | 357 |
|
359 | 358 | /** |
@@ -444,34 +443,29 @@ export class Database extends EventEmitter { |
444 | 443 | * @returns An array of rows in case of selections or an object with |
445 | 444 | * metadata in case of insert, update, delete. |
446 | 445 | */ |
447 | | - |
448 | 446 | public async sql(sql: TemplateStringsArray | string, ...values: any[]): Promise<any> { |
449 | | - let preparedSql = '' |
| 447 | + let query = '' |
450 | 448 |
|
451 | 449 | // sql is a TemplateStringsArray, the 'raw' property is specific to TemplateStringsArray |
452 | 450 | if (Array.isArray(sql) && 'raw' in sql) { |
| 451 | + query = '' |
453 | 452 | sql.forEach((string, i) => { |
454 | | - preparedSql += string + (i < values.length ? '?' : '') |
| 453 | + query += string + (i < values.length ? '?' : '') |
455 | 454 | }) |
456 | | - preparedSql = prepareSql(preparedSql, ...values) |
| 455 | + } else if (typeof sql === 'string') { |
| 456 | + query = sql |
457 | 457 | } else { |
458 | | - if (typeof sql === 'string') { |
459 | | - if (values?.length > 0) { |
460 | | - preparedSql = prepareSql(sql, ...values) |
461 | | - } else { |
462 | | - preparedSql = sql |
463 | | - } |
464 | | - } else { |
465 | | - throw new Error('Invalid sql') |
466 | | - } |
| 458 | + throw new Error('Invalid sql') |
467 | 459 | } |
468 | 460 |
|
| 461 | + const commands: SQLiteCloudCommand = { query, parameters: values } |
| 462 | + |
469 | 463 | return new Promise((resolve, reject) => { |
470 | 464 | this.getConnection((error, connection) => { |
471 | 465 | if (error || !connection) { |
472 | 466 | reject(error) |
473 | 467 | } else { |
474 | | - connection.sendCommands(preparedSql, (error, results) => { |
| 468 | + connection.sendCommands(commands, (error, results) => { |
475 | 469 | if (error) { |
476 | 470 | reject(error) |
477 | 471 | } else { |
|
0 commit comments