Ben Ashton
3 years ago
8 changed files with 278 additions and 236 deletions
File diff suppressed because one or more lines are too long
@ -0,0 +1,46 @@
|
||||
export function query(sql, ...args) { |
||||
validateQuery(sql, ...args); |
||||
return buildQuery(sql, ...args); |
||||
} |
||||
|
||||
// Allow for slightly more complex parameter substitution.
|
||||
// Instances of "???" will be replaced by the same number of comma-separated
|
||||
// question marks as items in the corresponding nested parateter array
|
||||
// eg. buildQuery("SELECT (???) FROM ?", [["col1", "col2"], "table1"])
|
||||
// would output: ["SELECT (?, ?) FROM ?", ["col1", "col2", "table1"]]
|
||||
export function buildQuery(sql, ...args) { |
||||
const parts = sql.split("???"); |
||||
const subParamLengths = args |
||||
.filter(Array.isArray) |
||||
.map((a) => a.length); |
||||
if (parts.length !== subParamLengths.length + 1) { |
||||
throw new PawSQLiteError("Unable to build query: sub-" + |
||||
"paramters do not match sub-paramters in query"); |
||||
} |
||||
|
||||
const newQuery = parts.reduce((p1, p2, i) => { |
||||
const length = subParamLengths[i - 1]; |
||||
return p1 + new Array(length).fill("?").join(", ") + p2; |
||||
}); |
||||
|
||||
const flatParams = args.reduce((acc, v) => { |
||||
if (Array.isArray(v)) { |
||||
Array.prototype.push.apply(acc, v); |
||||
} else { |
||||
acc.push(v); |
||||
} |
||||
return acc; |
||||
}, []); |
||||
|
||||
return [newQuery, ...flatParams]; |
||||
} |
||||
|
||||
export function validateQuery(sql, ...args) { |
||||
const reg = /^\s*(BEGIN|END|COMMIT|ROLLBACK)(?:[^A-Z]|$)/i; |
||||
const match = reg.exec(sql); |
||||
if (match) { |
||||
const statement = match[1].toUpperCase(); |
||||
throw new PawSQLiteError("Manually managing transactions is " + |
||||
"forbidden. Found: \"" + statement + "\" statement."); |
||||
} |
||||
} |
@ -0,0 +1,46 @@
|
||||
export class TaskManager { |
||||
constructor() { |
||||
this._queue = []; |
||||
this._inTask = false; |
||||
} |
||||
|
||||
enqueue() { |
||||
let completeSignal; |
||||
let taskComplete = new Promise((r, _) => { |
||||
completeSignal = r; |
||||
}); |
||||
|
||||
let readySignal; |
||||
let dbReady = new Promise((r, _) => { |
||||
readySignal = () => { |
||||
r(completeSignal); |
||||
}; |
||||
}); |
||||
|
||||
this._queue.push({readySignal, taskComplete}); |
||||
|
||||
this._processQueue(); |
||||
return dbReady; |
||||
} |
||||
|
||||
async _processQueue() { |
||||
// We're already processing the queue
|
||||
if (this._inTask) { |
||||
return; |
||||
} |
||||
|
||||
while (true) { |
||||
let item = this._queue.shift(); |
||||
if (!item) { |
||||
return; |
||||
} |
||||
|
||||
this._inTask = true; |
||||
|
||||
item.readySignal(); |
||||
await item.taskComplete; |
||||
|
||||
this._inTask = false; |
||||
} |
||||
} |
||||
} |
@ -1,55 +0,0 @@
|
||||
import { Transaction } from "./transaction.mjs"; |
||||
|
||||
export class TransactionManager { |
||||
constructor(dbName, adapter) { |
||||
this.dbName = dbName; |
||||
this.adapter = adapter; |
||||
this._queue = []; |
||||
this._inTransaction = false; |
||||
} |
||||
|
||||
transaction() { |
||||
return new Transaction(this.dbName, this.adapter, |
||||
this.enqueue.bind(this)); |
||||
} |
||||
|
||||
enqueue() { |
||||
let completeSignal; |
||||
let transactionComplete = new Promise((r, _) => { |
||||
completeSignal = r; |
||||
}); |
||||
|
||||
let readySignal; |
||||
let dbReady = new Promise((r, _) => { |
||||
readySignal = () => { |
||||
r(completeSignal); |
||||
}; |
||||
}); |
||||
|
||||
this._queue.push({readySignal, transactionComplete}); |
||||
|
||||
this._processQueue(); |
||||
return dbReady; |
||||
} |
||||
|
||||
async _processQueue() { |
||||
// We're already processing the queue
|
||||
if (this._inTransaction) { |
||||
return; |
||||
} |
||||
|
||||
while (true) { |
||||
let item = this._queue.shift(); |
||||
if (!item) { |
||||
return; |
||||
} |
||||
|
||||
this._inTransaction = true; |
||||
|
||||
item.readySignal(); |
||||
await item.transactionComplete; |
||||
|
||||
this._inTransaction = false; |
||||
} |
||||
} |
||||
} |
Loading…
Reference in new issue