From ed75657719338d86130a8716cfa2bf6a02fe1d8b Mon Sep 17 00:00:00 2001 From: Noel De Martin Date: Thu, 1 Feb 2024 16:22:16 +0100 Subject: [PATCH] MOBILE-4304 core: Implement insert row id --- ...sqlite.org+sqlite-wasm+3.45.0-build1.patch | 32 +++++++++++++++++-- .../emulator/classes/wasm-sqlite-object.ts | 6 +++- src/types/sqlite-wasm-custom.d.ts | 23 +++++++++++++ 3 files changed, 57 insertions(+), 4 deletions(-) create mode 100644 src/types/sqlite-wasm-custom.d.ts diff --git a/patches/@sqlite.org+sqlite-wasm+3.45.0-build1.patch b/patches/@sqlite.org+sqlite-wasm+3.45.0-build1.patch index 793eae2e0..a91134f24 100644 --- a/patches/@sqlite.org+sqlite-wasm+3.45.0-build1.patch +++ b/patches/@sqlite.org+sqlite-wasm+3.45.0-build1.patch @@ -1,5 +1,5 @@ diff --git a/node_modules/@sqlite.org/sqlite-wasm/sqlite-wasm/jswasm/sqlite3-bundler-friendly.mjs b/node_modules/@sqlite.org/sqlite-wasm/sqlite-wasm/jswasm/sqlite3-bundler-friendly.mjs -index b86a0aa..a9bf793 100644 +index b86a0aa..1be2b82 100644 --- a/node_modules/@sqlite.org/sqlite-wasm/sqlite-wasm/jswasm/sqlite3-bundler-friendly.mjs +++ b/node_modules/@sqlite.org/sqlite-wasm/sqlite-wasm/jswasm/sqlite3-bundler-friendly.mjs @@ -533,7 +533,7 @@ var sqlite3InitModule = (() => { @@ -11,7 +11,33 @@ index b86a0aa..a9bf793 100644 } function getBinary(file) { -@@ -12522,7 +12522,7 @@ var sqlite3InitModule = (() => { +@@ -10913,6 +10913,10 @@ var sqlite3InitModule = (() => { + } + }, + ++ lastInsertRowId: function () { ++ return capi.sqlite3_last_insert_rowid(affirmDbOpen(this).pointer); ++ }, ++ + dbFilename: function (dbName = 'main') { + return capi.sqlite3_db_filename(affirmDbOpen(this).pointer, dbName); + }, +@@ -11877,12 +11881,14 @@ var sqlite3InitModule = (() => { + if (!hadColNames) rc.columnNames = []; + + rc.callback = function (row, stmt) { ++ const rowId = rc.sql.includes('INSERT') ? db.lastInsertRowId() : undefined; + wState.post( + { + type: theCallback, + columnNames: rc.columnNames, + rowNumber: ++rowNumber, + row: row, ++ rowId, + }, + wState.xfer, + ); +@@ -12522,7 +12528,7 @@ var sqlite3InitModule = (() => { return promiseResolve_(sqlite3); }; const W = new Worker( @@ -20,7 +46,7 @@ index b86a0aa..a9bf793 100644 ); setTimeout(() => { if (undefined === promiseWasRejected) { -@@ -13445,7 +13445,7 @@ var sqlite3InitModule = (() => { +@@ -13445,7 +13451,7 @@ var sqlite3InitModule = (() => { }); return thePromise; }; diff --git a/src/core/features/emulator/classes/wasm-sqlite-object.ts b/src/core/features/emulator/classes/wasm-sqlite-object.ts index e76fb54e4..8dc35cddd 100644 --- a/src/core/features/emulator/classes/wasm-sqlite-object.ts +++ b/src/core/features/emulator/classes/wasm-sqlite-object.ts @@ -82,16 +82,19 @@ export class WasmSQLiteObject implements SQLiteObject { * @inheritdoc */ async executeSql(statement: string, params?: any[] | undefined): Promise { + let insertId: number | undefined = undefined; const rows = [] as unknown[]; await this.promiser('exec', { sql: statement, bind: params, - callback({ row, columnNames }) { + callback({ row, columnNames, rowId }) { if (!row) { return; } + insertId ||= rowId; + rows.push(columnNames.reduce((record, column, index) => { record[column] = row[index]; @@ -106,6 +109,7 @@ export class WasmSQLiteObject implements SQLiteObject { length: rows.length, }, rowsAffected: rows.length, + insertId, }; } diff --git a/src/types/sqlite-wasm-custom.d.ts b/src/types/sqlite-wasm-custom.d.ts new file mode 100644 index 000000000..d3151021a --- /dev/null +++ b/src/types/sqlite-wasm-custom.d.ts @@ -0,0 +1,23 @@ +// (C) Copyright 2015 Moodle Pty Ltd. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +export {}; + +declare module '@sqlite.org/sqlite-wasm' { + + export interface SqliteRowData { + rowId?: number; + } + +}