1 line
24 KiB
Plaintext
1 line
24 KiB
Plaintext
{"version":3,"sources":["webpack://PawSQLite/webpack/bootstrap","webpack://PawSQLite/./src/adapters/cordova_pawsqlite/psql_adapter.mjs","webpack://PawSQLite/./src/adapters/cordova_pawsqlite/psql_adapter_error.mjs","webpack://PawSQLite/./src/adapters/websql/database_wrapper.mjs","webpack://PawSQLite/./src/adapters/websql/response_wrapper.mjs","webpack://PawSQLite/./src/adapters/websql/result_mapper.mjs","webpack://PawSQLite/./src/adapters/websql/transaction_manager.mjs","webpack://PawSQLite/./src/adapters/websql/websql_adapter.mjs","webpack://PawSQLite/./src/adapters/websql/websql_adapter_error.mjs","webpack://PawSQLite/./src/database.mjs","webpack://PawSQLite/./src/log.mjs","webpack://PawSQLite/./src/pawsqlite.mjs","webpack://PawSQLite/./src/pawsqlite_error.mjs","webpack://PawSQLite/./src/result.mjs","webpack://PawSQLite/./src/transaction.mjs","webpack://PawSQLite/./src/transaction_manager.mjs"],"names":[],"mappings":";;QAAA;QACA;;QAEA;QACA;;QAEA;QACA;QACA;QACA;QACA;QACA;QACA;QACA;QACA;QACA;;QAEA;QACA;;QAEA;QACA;;QAEA;QACA;QACA;;;QAGA;QACA;;QAEA;QACA;;QAEA;QACA;QACA;QACA,0CAA0C,gCAAgC;QAC1E;QACA;;QAEA;QACA;QACA;QACA,wDAAwD,kBAAkB;QAC1E;QACA,iDAAiD,cAAc;QAC/D;;QAEA;QACA;QACA;QACA;QACA;QACA;QACA;QACA;QACA;QACA;QACA;QACA,yCAAyC,iCAAiC;QAC1E,gHAAgH,mBAAmB,EAAE;QACrI;QACA;;QAEA;QACA;QACA;QACA,2BAA2B,0BAA0B,EAAE;QACvD,iCAAiC,eAAe;QAChD;QACA;QACA;;QAEA;QACA,sDAAsD,+DAA+D;;QAErH;QACA;;;QAGA;QACA;;;;;;;;;;;;;AClFA;AAAA;AAAA;AAAA;AAA4D;AACxB;;;AAG7B;AACP;AACA;AACA,iBAAiB,wEAAgB;AACjC,KAAK;AACL,GAAG;AACH;AACA;AACA,iBAAiB,wEAAgB;AACjC,KAAK;AACL,GAAG;AACH;AACA,IAAI,oDAAG;AACP;AACA,iBAAiB,wEAAgB;AACjC,KAAK;AACL,GAAG;AACH;AACA;AACA,iBAAiB,wEAAgB;AACjC,KAAK;AACL,GAAG;AACH;;;;;;;;;;;;;AC1BA;AAAA;AAAA;AAA2D;;;AAGpD,+BAA+B,mEAAc;AACpD;AACA;AACA;AACA,KAAK;AACL;AACA;AACA;AACA;AACA,KAAK;AACL;AACA;AACA;AACA;AACA;AACA;;AAEA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA,C;;;;;;;;;;;;AC9BA;AAAA;AAAA;AAA+D;;;AAGxD;AACP;AACA;AACA;AACA,4BAA4B,2EAAkB;AAC9C;;AAEA;AACA;AACA;AACA;AACA;;AAEA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA,KAAK;AACL;AACA;AACA;AACA,C;;;;;;;;;;;;AC7BA;AAAA;AAAO;AACP,oBAAoB;AACpB;AACA;AACA,GAAG;AACH;;;;;;;;;;;;;;;ACLA;AAAA;AAAO;AACP;AACA;AACA;AACA;;AAEA;AACA;AACA,GAAG;;AAEH;;AAEA;AACA;;AAEA,mBAAmB,gCAAgC;AACnD;AACA;AACA;;AAEA;AACA,C;;;;;;;;;;;;ACrBA;AAAA;AAAA;AAAA;AAAA;AAAgE;AAChB;AACZ;;;AAGpC;AACA;AACA;;AAEA;AACA;AACA;;AAEA;AACA;;AAEA;AACA;AACA;AACA,KAAK;AACL;;AAEA;AACA;AACA;AACA;AACA,KAAK;AACL;AACA,KAAK;AACL;AACA;;;AAGO;AACP;AACA;;AAEA;AACA;AACA;AACA;;AAEA;AACA;AACA;AACA;AACA,qBAAqB,4EAAkB;AACvC;AACA;AACA;AACA;AACA;AACA,SAAS;AACT,OAAO;AACP,KAAK,oBAAoB,oEAAS;AAClC;;AAEA;AACA;AACA;AACA,KAAK;AACL;;AAEA;AACA;AACA,6BAA6B,oEAAS;AACtC,KAAK;AACL;;AAEA;AACA;AACA;AACA;AACA,4DAA4D,oEAAS;AACrE,KAAK;AACL;;;AAGA;AACA;AACA;AACA;AACA;;AAEA;AACA;;AAEA;AACA;AACA;;;AAGA;AACA;;AAEA;AACA;;AAEA;AACA;AACA;AACA,WAAW;AACX;AACA;AACA;AACA;;AAEA;AACA;AACA;;AAEA;AACA,OAAO;AACP;AACA;AACA;AACA;AACA,YAAY,oDAAG,iBAAiB,gBAAgB,eAAe,iBAAiB;AAChF;AACA,SAAS;AACT;AACA;AACA;AACA;;AAEA;AACA;;;AAGA;AACA;AACA;AACA;AACA;;;AAGA;AACA;AACA;AACA,mBAAmB,4EAAkB;AACrC;AACA;AACA;AACA;AACA,gBAAgB,oEAAS;AACzB,OAAO;AACP,eAAe,4EAAkB;AACjC;AACA,OAAO;AACP,KAAK;AACL;;AAEA;AACA;AACA;AACA,C;;;;;;;;;;;;AC3JA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAgE;AACP;AACA;AACrB;;AAEpC;;AAEO;AACP;AACA;;AAEA;AACA,gCAAgC,qEAAe;AAC/C;;AAEA,WAAW,qEAAe,UAAU,UAAU;AAC9C,GAAG;;AAEH;AACA;AACA,WAAW,qEAAe;AAC1B,GAAG;;AAEH;AACA,IAAI,oDAAG;;AAEP;AACA;AACA,gBAAgB,4EAAkB;AAClC;;AAEA;AACA,WAAW,qEAAe;AAC1B,GAAG;;AAEH;AACA,cAAc,4EAAkB;AAChC;AACA;;;;;;;;;;;;;ACtCA;AAAA;AAAA;AAA2D;;;AAGpD,iCAAiC,mEAAc;AACtD;AACA;AACA;AACA;AACA;AACA;AACA;AACA,C;;;;;;;;;;;;ACXA;AAAA;AAAA;AAA+D;;;AAGxD;AACP;AACA;AACA;AACA;AACA;;AAEA,kCAAkC,2EAAkB;AACpD;;AAEA;AACA;;AAEA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;;AAEA;AACA;AACA;;AAEA;AACA;AACA;;AAEA;AACA;AACA;;;AAGA;AACA;AACA;;AAEA;AACA;AACA,KAAK;AACL;AACA;AACA;AACA;AACA;;AAEA;AACA;AACA;;AAEA;AACA;;;AAGA;AACA;AACA;AACA;AACA,C;;;;;;;;;;;;AC/DA;AAAA;AAAA;AAAA;;AAEO;AACP;AACA;AACA,G;AACA;;AAEO;AACP;AACA;AACA,C;;;;;;;;;;;;ACXA;AAAA;AAAA;AAAA;AAAA;AAAA;AAA0C;AACa;AACf;;AAExC;AAC4E;AACP;;;AAGtD;AACf,2CAA2C;AAC3C;;AAEA,iBAAiB,sDAAQ;AACzB;AACA;AACA,GAAG;AACH,uCAAuC;AACvC;;AAEA;AACA,GAAG;AACH,SAAS,oDAAW;AACpB,SAAS,mEAAc;AACvB,CAAC,EAAC;;;AAGF;AACA;AACA;AACA,GAAG;AACH;AACA;AACA;AACA,WAAW,wFAAW;AACtB,GAAG;AACH,WAAW,iFAAa;AACxB,GAAG;AACH,cAAc,mEAAc,qBAAqB,YAAY;AAC7D;AACA,C;;;;;;;;;;;;ACxCA;AAAA;AAAO;AACP;AACA;AACA;AACA;AACA,C;;;;;;;;;;;;ACLA;AAAA;AAAA;AAAuD;;;AAGhD;AACP;AACA;AACA;AACA,KAAK;AACL;AACA;;AAEA;AACA;AACA;AACA;AACA;AACA;AACA;AACA,C;;;;;;;;;;;;AClBA;AAAA;AAAA;AAAA;AAAsC;AACiB;;;AAGhD;AACP;AACA;AACA;AACA;;AAEA;AACA;;AAEA;AACA;AACA;AACA;;AAEA;AACA;AACA,gBAAgB,mEAAc;AAC9B;;AAEA;AACA;AACA;;AAEA;AACA;AACA,KAAK;AACL;AACA;AACA;AACA;AACA;AACA;;AAEA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA,gBAAgB,mEAAc;AAC9B;AACA;;AAEA;AACA;AACA;AACA,KAAK;;AAEL;AACA;AACA;AACA,OAAO;AACP;AACA;AACA;AACA,KAAK;;AAEL;AACA;;AAEA;AACA;AACA;;AAEA;AACA;AACA;;AAEA;AACA;AACA;AACA;AACA;AACA;AACA,OAAO;AACP;;AAEA;AACA;;AAEA;AACA;AACA;AACA;AACA;AACA,gBAAgB,mEAAc;AAC9B;AACA;;AAEA;AACA;;AAEA,eAAe,kDAAM;AACrB;;AAEA;AACA;AACA;;AAEA;AACA;AACA,gBAAgB,mEAAc;AAC9B;AACA;;AAEA;AACA;AACA;AACA,KAAK;AACL;AACA;;AAEA;AACA;AACA;AACA;AACA,KAAK;AACL;AACA;;AAEA;;AAEA;AACA;AACA;;AAEA,eAAe,kDAAM;AACrB;AACA,C;;;;;;;;;;;;ACzIA;AAAA;AAAA;AAAgD;;AAEzC;AACP;AACA;AACA;AACA;AACA;AACA;;AAEA;AACA,eAAe,4DAAW;AAC1B;AACA;;AAEA;AACA;AACA;AACA;AACA,KAAK;;AAEL;AACA;AACA;AACA;AACA;AACA,KAAK;;AAEL,sBAAsB,iCAAiC;;AAEvD;AACA;AACA;;AAEA;AACA;AACA;AACA;AACA;;AAEA;AACA;AACA;AACA;AACA;;AAEA;;AAEA;AACA;;AAEA;AACA;AACA;AACA,C","file":"PawSQLite.js","sourcesContent":[" \t// The module cache\n \tvar installedModules = {};\n\n \t// The require function\n \tfunction __webpack_require__(moduleId) {\n\n \t\t// Check if module is in cache\n \t\tif(installedModules[moduleId]) {\n \t\t\treturn installedModules[moduleId].exports;\n \t\t}\n \t\t// Create a new module (and put it into the cache)\n \t\tvar module = installedModules[moduleId] = {\n \t\t\ti: moduleId,\n \t\t\tl: false,\n \t\t\texports: {}\n \t\t};\n\n \t\t// Execute the module function\n \t\tmodules[moduleId].call(module.exports, module, module.exports, __webpack_require__);\n\n \t\t// Flag the module as loaded\n \t\tmodule.l = true;\n\n \t\t// Return the exports of the module\n \t\treturn module.exports;\n \t}\n\n\n \t// expose the modules object (__webpack_modules__)\n \t__webpack_require__.m = modules;\n\n \t// expose the module cache\n \t__webpack_require__.c = installedModules;\n\n \t// define getter function for harmony exports\n \t__webpack_require__.d = function(exports, name, getter) {\n \t\tif(!__webpack_require__.o(exports, name)) {\n \t\t\tObject.defineProperty(exports, name, { enumerable: true, get: getter });\n \t\t}\n \t};\n\n \t// define __esModule on exports\n \t__webpack_require__.r = function(exports) {\n \t\tif(typeof Symbol !== 'undefined' && Symbol.toStringTag) {\n \t\t\tObject.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });\n \t\t}\n \t\tObject.defineProperty(exports, '__esModule', { value: true });\n \t};\n\n \t// create a fake namespace object\n \t// mode & 1: value is a module id, require it\n \t// mode & 2: merge all properties of value into the ns\n \t// mode & 4: return value when already ns object\n \t// mode & 8|1: behave like require\n \t__webpack_require__.t = function(value, mode) {\n \t\tif(mode & 1) value = __webpack_require__(value);\n \t\tif(mode & 8) return value;\n \t\tif((mode & 4) && typeof value === 'object' && value && value.__esModule) return value;\n \t\tvar ns = Object.create(null);\n \t\t__webpack_require__.r(ns);\n \t\tObject.defineProperty(ns, 'default', { enumerable: true, value: value });\n \t\tif(mode & 2 && typeof value != 'string') for(var key in value) __webpack_require__.d(ns, key, function(key) { return value[key]; }.bind(null, key));\n \t\treturn ns;\n \t};\n\n \t// getDefaultExport function for compatibility with non-harmony modules\n \t__webpack_require__.n = function(module) {\n \t\tvar getter = module && module.__esModule ?\n \t\t\tfunction getDefault() { return module['default']; } :\n \t\t\tfunction getModuleExports() { return module; };\n \t\t__webpack_require__.d(getter, 'a', getter);\n \t\treturn getter;\n \t};\n\n \t// Object.prototype.hasOwnProperty.call\n \t__webpack_require__.o = function(object, property) { return Object.prototype.hasOwnProperty.call(object, property); };\n\n \t// __webpack_public_path__\n \t__webpack_require__.p = \"\";\n\n\n \t// Load entry module and return exports\n \treturn __webpack_require__(__webpack_require__.s = \"./src/pawsqlite.mjs\");\n","import { PSQLAdapterError } from \"./psql_adapter_error.mjs\";\nimport { log } from \"../../log.mjs\";\n\n\nexport const PSQLAdapter = {\n open: (dbName) => new Promise((resolve, reject) => {\n cordova.exec(resolve, (e) => {\n reject(new PSQLAdapterError(e));\n }, \"PawSQLite\", \"open\", [dbName]);\n }),\n close: (dbName) => new Promise((resolve, reject) => {\n cordova.exec(resolve, (e) => {\n reject(new PSQLAdapterError(e));\n }, \"PawSQLite\", \"close\", [dbName]);\n }),\n sql: (dbName, sql, ...args) => new Promise((resolve, reject) => {\n log(sql);\n cordova.exec(resolve, (e) => {\n reject(new PSQLAdapterError(e));\n }, \"PawSQLite\", \"sql\", [dbName, sql, ...args]);\n }),\n delete: (dbName) => new Promise((resolve, reject) => {\n cordova.exec(resolve, (e) => {\n reject(new PSQLAdapterError(e));\n }, \"PawSQLite\", \"delete\", [dbName]);\n })\n};\n","import { PawSQLiteError } from \"../../pawsqlite_error.mjs\";\n\n\nexport class PSQLAdapterError extends PawSQLiteError {\n constructor(response) {\n if (response.hasOwnProperty(\"message\")) {\n super(response.message);\n } else {\n super();\n }\n if (response.hasOwnProperty(\"name\")) {\n this.name = response.name;\n } else {\n this.name = \"PSQLAdapterError\";\n }\n if (response.hasOwnProperty(\"trace\")) {\n this.trace = response.trace;\n }\n }\n\n toString() {\n let str = this.name;\n if (this.hasOwnProperty(\"message\")) {\n str += \": \" + this.message;\n }\n if (this.hasOwnProperty(\"trace\")) {\n str += \"\\n\" + this.trace;\n }\n return str;\n }\n}","import { TransactionManager } from \"./transaction_manager.mjs\";\n\n\nexport class DatabaseWrapper {\n constructor(dbName, version) {\n this.name = dbName;\n this.db = openDatabase(this.name, version, \"\", 5 * 1024 * 1024);\n this.transManager = new TransactionManager(this.db);\n }\n\n sql(sql, ...args) {\n const reg = /^\\s*(BEGIN|END|COMMIT|ROLLBACK)(?:[^A-Z]|$)/i;\n const match = reg.exec(sql);\n if (match) {\n const statement = match[1].toUpperCase();\n\n switch(statement) {\n case \"BEGIN\":\n return this.transManager.begin();\n case \"END\":\n case \"COMMIT\":\n return this.transManager.commit();\n case \"ROLLBACK\":\n return this.transManager.rollback();\n }\n } else {\n return this.transManager.sql(sql, args);\n }\n }\n}","export const ResponseWrapper = {\n success: (obj = {}) => ({\n success: true,\n ...obj\n })\n};\n\n\n","export function mapResult(originalResult) {\n const newResult = {};\n if (!originalResult) {\n return newResult;\n }\n\n try {\n newResult.insertId = originalResult.insertId;\n } catch (e) {}\n\n newResult.rowsAffected = originalResult.rowsAffected;\n\n if (\"rows\" in originalResult) {\n newResult.rows = [];\n\n for (let i = 0; i < originalResult.rows.length; i++) {\n newResult.rows.push(originalResult.rows.item(i));\n }\n }\n\n return newResult;\n}","import { WebSQLAdapterError } from \"./websql_adapter_error.mjs\";\nimport { mapResult } from \"./result_mapper.mjs\";\nimport { log } from \"../../log.mjs\";\n\n\nclass Task {\n constructor(job, startsTransaction=false, endsTransaction=false) {\n this._job = job;\n\n if (startsTransaction && endsTransaction) {\n throw new Error(\"Task cannot start and end a transaction.\");\n }\n\n this.startsTransaction = startsTransaction;\n this.endsTransaction = endsTransaction;\n\n this.result = new Promise ((resolve, reject) => {\n this._resolve = resolve;\n this._reject = reject;\n });\n }\n\n run(tx) {\n return this._job(tx).then((result) => {\n this._resolve(result);\n return result;\n }, (e) => {\n this._reject(e);\n });\n }\n}\n\n\nexport class TransactionManager {\n constructor(db) {\n this.db = db;\n\n this._txCount = 0;\n this._tasks = [];\n this._processing = false;\n }\n\n begin() {\n return this._addTask(new Task((tx) => {\n return new Promise((resolve, reject) => {\n if (tx) {\n reject(new WebSQLAdapterError(\"BEGIN called with an active \" +\n \"transaction. This should not happen\"));\n return;\n }\n this.db.transaction((tx) => {\n resolve(tx);\n });\n });\n }, true)).then(() => mapResult());\n }\n\n sql(sql, args = []) {\n return this._addTask(new Task((tx) => {\n return this._executeSql(tx, sql, args);\n }));\n }\n\n commit() {\n return this._addTask(new Task((tx) => {\n return Promise.resolve(mapResult());\n }, false, true));\n }\n\n rollback() {\n // Hack to manually cause rollback:\n // Intentionally cause an error with rollbackOnError set to true\n return this._addTask(new Task((tx) => {\n return this._executeSql(tx, \"\", [], true).catch(() => mapResult());\n }, false, true));\n }\n\n\n async _process() {\n if (this._processing) {\n return;\n }\n this._processing = true;\n\n let tx = null;\n let keepaliveCount = 0;\n\n while (true) {\n const tasks = this._tasks;\n this._tasks = [];\n\n\n if (tasks.length) {\n const promises = [];\n\n for (const task of tasks) {\n const promise = task.run(tx);\n\n if (task.startsTransaction) {\n tx = await promise;\n this._txCount++;\n } else {\n if (task.endsTransaction) {\n tx = null;\n keepaliveCount = 0;\n }\n\n promises.push(promise);\n }\n }\n\n await Promise.all(promises);\n } else {\n if (tx) {\n await this._nop(tx);\n keepaliveCount++;\n if (keepaliveCount % 5000 === 0) {\n log(`Transaction: ${ this._txCount } Keepalive: #${ keepaliveCount }`);\n }\n } else {\n break;\n }\n }\n }\n\n this._processing = false;\n }\n\n\n _addTask(task) {\n this._tasks.push(task);\n this._process();\n return task.result;\n }\n\n\n _executeSql(tx, sql, sqlArgs = [], rollbackOnError = false) {\n return new Promise((resolve, reject) => {\n if (!tx) {\n reject(new WebSQLAdapterError(\"No transaction. This should not be \" +\n \" possible\"));\n return;\n }\n tx.executeSql(sql, sqlArgs, (tx, result) => {\n resolve(mapResult(result));\n }, (tx, e) => {\n reject(WebSQLAdapterError.from(e));\n return rollbackOnError;\n });\n });\n }\n\n _nop(tx) {\n return this._executeSql(tx, \"SELECT 1\");\n }\n}","import { WebSQLAdapterError } from \"./websql_adapter_error.mjs\";\nimport { DatabaseWrapper } from \"./database_wrapper.mjs\";\nimport { ResponseWrapper } from \"./response_wrapper.mjs\";\nimport { log } from \"../../log.mjs\";\n\nconst databases = new Map();\n\nexport const WebSQLAdapter = {\n open: async (dbName) => {\n const version = \"1.0\";\n\n if (!databases.has(dbName)) {\n databases.set(dbName, new DatabaseWrapper(dbName, version));\n }\n\n return ResponseWrapper.success({ version });\n },\n\n close: async (dbName) => {\n databases.delete(dbName);\n return ResponseWrapper.success();\n },\n\n sql: async (dbName, sql, ...args) => {\n log(sql);\n\n const db = databases.get(dbName);\n if (!db) {\n throw new WebSQLAdapterError(\"Database not open\");\n }\n\n const result = await db.sql(sql, ...args);\n return ResponseWrapper.success(result);\n },\n\n delete: async (dbName) => {\n throw new WebSQLAdapterError(\"Delete not implemented\");\n }\n};\n","import { PawSQLiteError } from \"../../pawsqlite_error.mjs\";\n\n\nexport class WebSQLAdapterError extends PawSQLiteError {\n static from(e) {\n let message = \"\";\n if (e && e.message) {\n message = e.message;\n }\n return new WebSQLAdapterError(message);\n }\n}","import { TransactionManager } from \"./transaction_manager.mjs\";\n\n\nexport class Database {\n constructor(dbName, adapter) {\n this.dbName = dbName;\n this.adapter = adapter;\n this.version = null;\n this.path = null;\n\n this.transactionManager = new TransactionManager(dbName, this.adapter);\n }\n\n async open() {\n let response = await this.adapter.open(this.dbName);\n\n if (response) {\n if (response.hasOwnProperty('version')) {\n this.version = response.version;\n }\n if (response.hasOwnProperty('path')) {\n this.path = response.path;\n }\n }\n\n // Allow chaining\n return this;\n }\n\n close() {\n return this.adapter.close(this.dbName);\n }\n\n transaction() {\n return this.transactionManager.transaction();\n }\n\n\n async autoTransaction(cb, inheritTx) {\n let tx = inheritTx || this.transaction();\n let result;\n\n try {\n result = await cb(tx);\n } catch (e) {\n if (!inheritTx) {\n await tx.rollback();\n }\n throw e;\n }\n\n if (!inheritTx) {\n await tx.commit();\n }\n\n return result;\n }\n\n\n // Helper method to start a transaction and execute a single SQL statement\n sql(sql, ...args) {\n return this.autoTransaction((tx) => tx.sql(sql, ...args));\n }\n}","let DEBUG = false;\n\nexport function log(...args) {\n if (DEBUG) {\n console.log(...args);\n } \n}\n\nexport function enableDebug(active) {\n DEBUG = !!active;\n log(\"PawSQLite: debugging \" + (DEBUG ? \"enabled\" : \"disabled\"));\n}","import { Database } from \"./database.mjs\";\nimport { PawSQLiteError } from \"./pawsqlite_error.mjs\";\nimport { enableDebug } from \"./log.mjs\";\n\n// Adapters\nimport { PSQLAdapter } from \"./adapters/cordova_pawsqlite/psql_adapter.mjs\";\nimport { WebSQLAdapter } from \"./adapters/websql/websql_adapter.mjs\";\n\n\nexport default {\n open: async function(dbName, options = {}) {\n const adapter = getAdapter(options.adapter);\n\n let db = new Database(dbName, adapter);\n await db.open();\n return db;\n },\n delete: function(dbName, options = {}) {\n const adapter = getAdapter(options.adapter);\n\n return adapter.delete(dbName);\n },\n debug: enableDebug,\n Error: PawSQLiteError\n};\n\n\nfunction getAdapter(adapterName) {\n if (!adapterName) {\n adapterName = \"pawsqlite\";\n } else {\n adapterName = adapterName.toLowerCase();\n }\n if (adapterName === \"pawsqlite\") {\n return PSQLAdapter;\n } else if (adapterName === \"websql\") {\n return WebSQLAdapter;\n } else {\n throw new PawSQLiteError(`Unknown Adapter: ${adapterName}`);\n }\n}","export class PawSQLiteError extends Error {\n constructor(message) {\n super(message);\n this.name = \"PawSQLiteError\";\n }\n}","import { PawSQLiteError } from \"./pawsqlite_error.mjs\";\n\n\nexport class Result extends Array {\n constructor(result) {\n if (result.hasOwnProperty(\"rows\")) {\n super(...result.rows);\n } else {\n super();\n }\n\n if (result.hasOwnProperty(\"insertId\")) {\n this.insertId = result.insertId;\n }\n if (result.hasOwnProperty(\"rowsAffected\")) {\n this.rowsAffected = result.rowsAffected;\n }\n }\n}","import { Result } from \"./result.mjs\";\nimport { PawSQLiteError } from \"./pawsqlite_error.mjs\";\n\n\nexport class Transaction {\n constructor(dbName, adapter, enqueue, rollbackOnError=false) {\n this.dbName = dbName;\n this.adapter = adapter;\n this._rollbackOnError = rollbackOnError;\n\n this._enqueue = enqueue;\n this._completeCb = null;\n\n this._readyWait = null;\n this._ready = false;\n this._finalized = false;\n }\n\n async sql(sql, ...args) {\n if (this._finalized) {\n throw new PawSQLiteError(\"Transaction has already completed\");\n }\n\n if (!this._ready) {\n await this._waitUntilReady();\n }\n\n try {\n return await this._executeSQL(sql, ...args);\n } catch (e) {\n if (this._rollbackOnError) {\n await this.rollback();\n }\n throw e;\n }\n }\n\n // Allow for slightly more complex parameter substitution.\n // Instances of \"???\" will be replaced by the same number of comma-separated\n // question marks as items in the corresponding nested parateter array\n // eg. buildQuery(\"SELECT (???) FROM ?\", [[\"col1\", \"col2\"], \"table1\"])\n // would output: [\"SELECT (?, ?) FROM ?\", [\"col1\", \"col2\", \"table1\"]]\n buildQuery(sql, ...args) {\n const parts = sql.split(\"???\");\n const subParamLengths = args\n .filter(Array.isArray)\n .map((a) => a.length);\n if (parts.length !== subParamLengths.length + 1) {\n throw new PawSQLiteError(\"Unable to build query: sub-\" +\n \"paramters do not match sub-paramters in query\");\n }\n\n const newQuery = parts.reduce((p1, p2, i) => {\n const length = subParamLengths[i - 1];\n return p1 + new Array(length).fill(\"?\").join(\", \") + p2;\n });\n\n const flatParams = args.reduce((acc, v) => {\n if (Array.isArray(v)) {\n Array.prototype.push.apply(acc, v);\n } else {\n acc.push(v);\n }\n return acc;\n }, []);\n\n return [newQuery, ...flatParams];\n }\n\n commit() {\n return this._complete(\"COMMIT\");\n }\n\n rollback() {\n return this._complete(\"ROLLBACK\");\n }\n\n async _waitUntilReady() {\n if (!this._readyWait) {\n this._readyWait = (async () => {\n this._completeCb = await this._enqueue();\n await this._begin();\n this._ready = true;\n })();\n }\n \n await this._readyWait;\n }\n\n async _executeSQL(sql, ...args) {\n const reg = /^\\s*(BEGIN|END|COMMIT|ROLLBACK)(?:[^A-Z]|$)/i;\n const match = reg.exec(sql);\n if (match) {\n const statement = match[1].toUpperCase();\n throw new PawSQLiteError(\"Manually managing transactions is \" +\n \"forbidden. Found: \\\"\" + statement + \"\\\" statement.\");\n }\n\n const result = await this.adapter.sql(this.dbName,\n ...this.buildQuery(sql, ...args));\n\n return new Result(result);\n }\n\n async _begin() {\n const result = await this.adapter.sql(this.dbName, \"BEGIN\");\n }\n\n async _complete(sql) {\n if (this._finalized) {\n throw new PawSQLiteError(\"Transaction has already completed\");\n }\n this._finalized = true;\n\n if (!this._readyWait) {\n // Transaction was unused\n return;\n } else if (!this._ready) {\n await this._waitUntilReady();\n }\n\n let result;\n let error;\n try {\n result = await this.adapter.sql(this.dbName, sql);\n } catch (e) {\n error = e;\n }\n \n this._completeCb();\n \n if (error) {\n throw error;\n }\n\n return new Result(result);\n }\n}","import { Transaction } from \"./transaction.mjs\";\n\nexport class TransactionManager {\n constructor(dbName, adapter) {\n this.dbName = dbName;\n this.adapter = adapter;\n this._queue = [];\n this._inTransaction = false;\n }\n\n transaction() {\n return new Transaction(this.dbName, this.adapter,\n this.enqueue.bind(this));\n }\n\n enqueue() {\n let completeSignal;\n let transactionComplete = new Promise((r, _) => {\n completeSignal = r;\n });\n\n let readySignal;\n let dbReady = new Promise((r, _) => {\n readySignal = () => {\n r(completeSignal);\n };\n });\n\n this._queue.push({readySignal, transactionComplete});\n\n this._processQueue();\n return dbReady;\n }\n\n async _processQueue() {\n // We're already processing the queue\n if (this._inTransaction) {\n return;\n }\n\n while (true) {\n let item = this._queue.shift();\n if (!item) {\n return;\n }\n\n this._inTransaction = true;\n\n item.readySignal();\n await item.transactionComplete;\n\n this._inTransaction = false;\n }\n }\n}"],"sourceRoot":""} |