2013-01-05 03:36:59 +04:00
|
|
|
|
/* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
|
|
|
|
|
2022-09-29 09:52:33 +03:00
|
|
|
|
import { XPCOMUtils } from "resource://gre/modules/XPCOMUtils.sys.mjs";
|
2013-01-05 03:36:59 +04:00
|
|
|
|
|
Bug 1792341 - Migrate more toolkit/modules consumers to use direct ES module import. r=Gijs,webdriver-reviewers,perftest-reviewers,necko-reviewers,geckoview-reviewers,preferences-reviewers,application-update-reviewers,pip-reviewers,credential-management-reviewers,sgalich,owlish,bytesized,AlexandruIonescu,whimboo,mconley,mixedpuppy
Mainly automated changes. Some manual ESLint fixes and whitespace cleanup.
Differential Revision: https://phabricator.services.mozilla.com/D158452
2022-10-18 14:21:26 +03:00
|
|
|
|
import { setTimeout } from "resource://gre/modules/Timer.sys.mjs";
|
2013-01-05 03:36:59 +04:00
|
|
|
|
|
2022-06-07 07:29:50 +03:00
|
|
|
|
const lazy = {};
|
|
|
|
|
|
Bug 1792365 - Convert toolkit/modules consumers to use ES module imports directly. r=webdriver-reviewers,perftest-reviewers,geckoview-reviewers,extension-reviewers,preferences-reviewers,desktop-theme-reviewers,application-update-reviewers,pip-reviewers,credential-management-reviewers,robwu,Gijs,sgalich,bytesized,AlexandruIonescu,dao,m_kato
Differential Revision: https://phabricator.services.mozilla.com/D158094
2022-09-29 09:52:34 +03:00
|
|
|
|
ChromeUtils.defineESModuleGetters(lazy, {
|
|
|
|
|
FileUtils: "resource://gre/modules/FileUtils.sys.mjs",
|
|
|
|
|
Log: "resource://gre/modules/Log.sys.mjs",
|
|
|
|
|
PromiseUtils: "resource://gre/modules/PromiseUtils.sys.mjs",
|
|
|
|
|
});
|
|
|
|
|
|
2022-06-07 07:29:50 +03:00
|
|
|
|
XPCOMUtils.defineLazyModuleGetters(lazy, {
|
2017-08-09 23:03:36 +03:00
|
|
|
|
AsyncShutdown: "resource://gre/modules/AsyncShutdown.jsm",
|
|
|
|
|
});
|
|
|
|
|
|
2014-06-12 01:40:17 +04:00
|
|
|
|
XPCOMUtils.defineLazyServiceGetter(
|
2022-06-07 07:29:50 +03:00
|
|
|
|
lazy,
|
2014-06-12 01:40:17 +04:00
|
|
|
|
"FinalizationWitnessService",
|
|
|
|
|
"@mozilla.org/toolkit/finalizationwitness;1",
|
|
|
|
|
"nsIFinalizationWitnessService"
|
|
|
|
|
);
|
2013-01-05 03:36:59 +04:00
|
|
|
|
|
2015-09-23 10:01:00 +03:00
|
|
|
|
// Regular expression used by isInvalidBoundLikeQuery
|
|
|
|
|
var likeSqlRegex = /\bLIKE\b\s(?![@:?])/i;
|
|
|
|
|
|
2013-01-05 03:36:59 +04:00
|
|
|
|
// Counts the number of created connections per database basename(). This is
|
|
|
|
|
// used for logging to distinguish connection instances.
|
2015-09-15 21:19:45 +03:00
|
|
|
|
var connectionCounters = new Map();
|
2013-01-05 03:36:59 +04:00
|
|
|
|
|
2014-09-22 20:32:43 +04:00
|
|
|
|
// Tracks identifiers of wrapped connections, that are Storage connections
|
2022-10-25 23:47:58 +03:00
|
|
|
|
// opened through mozStorage and then wrapped by Sqlite.jsm to use its syntactic
|
2014-09-22 20:32:43 +04:00
|
|
|
|
// sugar API. Since these connections have an unknown origin, we use this set
|
|
|
|
|
// to differentiate their behavior.
|
2015-09-15 21:19:45 +03:00
|
|
|
|
var wrappedConnections = new Set();
|
2014-09-22 20:32:43 +04:00
|
|
|
|
|
2014-05-13 09:00:00 +04:00
|
|
|
|
/**
|
|
|
|
|
* Once `true`, reject any attempt to open or close a database.
|
|
|
|
|
*/
|
2015-09-15 21:19:45 +03:00
|
|
|
|
var isClosed = false;
|
2014-05-13 09:00:00 +04:00
|
|
|
|
|
2015-09-15 21:19:45 +03:00
|
|
|
|
var Debugging = {
|
2014-06-26 08:24:21 +04:00
|
|
|
|
// Tests should fail if a connection auto closes. The exception is
|
|
|
|
|
// when finalization itself is tested, in which case this flag
|
|
|
|
|
// should be set to false.
|
|
|
|
|
failTestsOnAutoClose: true,
|
|
|
|
|
};
|
|
|
|
|
|
2015-09-23 10:01:00 +03:00
|
|
|
|
/**
|
|
|
|
|
* Helper function to check whether LIKE is implemented using proper bindings.
|
|
|
|
|
*
|
|
|
|
|
* @param sql
|
|
|
|
|
* (string) The SQL query to be verified.
|
|
|
|
|
* @return boolean value telling us whether query was correct or not
|
|
|
|
|
*/
|
|
|
|
|
function isInvalidBoundLikeQuery(sql) {
|
|
|
|
|
return likeSqlRegex.test(sql);
|
|
|
|
|
}
|
|
|
|
|
|
2014-06-12 01:40:17 +04:00
|
|
|
|
// Displays a script error message
|
|
|
|
|
function logScriptError(message) {
|
|
|
|
|
let consoleMessage = Cc["@mozilla.org/scripterror;1"].createInstance(
|
|
|
|
|
Ci.nsIScriptError
|
|
|
|
|
);
|
|
|
|
|
let stack = new Error();
|
|
|
|
|
consoleMessage.init(
|
|
|
|
|
message,
|
|
|
|
|
stack.fileName,
|
|
|
|
|
null,
|
|
|
|
|
stack.lineNumber,
|
|
|
|
|
0,
|
|
|
|
|
Ci.nsIScriptError.errorFlag,
|
|
|
|
|
"component javascript"
|
|
|
|
|
);
|
|
|
|
|
Services.console.logMessage(consoleMessage);
|
|
|
|
|
|
2014-06-26 08:24:21 +04:00
|
|
|
|
// This `Promise.reject` will cause tests to fail. The debugging
|
|
|
|
|
// flag can be used to suppress this for tests that explicitly
|
|
|
|
|
// test auto closes.
|
|
|
|
|
if (Debugging.failTestsOnAutoClose) {
|
|
|
|
|
Promise.reject(new Error(message));
|
|
|
|
|
}
|
2014-06-12 01:40:17 +04:00
|
|
|
|
}
|
|
|
|
|
|
2014-09-22 20:32:43 +04:00
|
|
|
|
/**
|
2017-05-17 16:51:02 +03:00
|
|
|
|
* Gets connection identifier from its database file name.
|
2014-09-22 20:32:43 +04:00
|
|
|
|
*
|
2017-05-17 16:51:02 +03:00
|
|
|
|
* @param fileName
|
|
|
|
|
* A database file string name.
|
2014-09-22 20:32:43 +04:00
|
|
|
|
* @return the connection identifier.
|
|
|
|
|
*/
|
2017-05-17 16:51:02 +03:00
|
|
|
|
function getIdentifierByFileName(fileName) {
|
|
|
|
|
let number = connectionCounters.get(fileName) || 0;
|
|
|
|
|
connectionCounters.set(fileName, number + 1);
|
|
|
|
|
return fileName + "#" + number;
|
2014-09-22 20:32:43 +04:00
|
|
|
|
}
|
|
|
|
|
|
2021-11-01 12:34:18 +03:00
|
|
|
|
/**
|
|
|
|
|
* Convert mozIStorageError to common NS_ERROR_*
|
|
|
|
|
* The conversion is mostly based on the one in
|
|
|
|
|
* mozStoragePrivateHelpers::ConvertResultCode, plus a few additions.
|
|
|
|
|
*
|
|
|
|
|
* @param {integer} result a mozIStorageError result code.
|
|
|
|
|
* @returns {integer} an NS_ERROR_* result code.
|
|
|
|
|
*/
|
|
|
|
|
function convertStorageErrorResult(result) {
|
|
|
|
|
switch (result) {
|
|
|
|
|
case Ci.mozIStorageError.PERM:
|
|
|
|
|
case Ci.mozIStorageError.AUTH:
|
|
|
|
|
case Ci.mozIStorageError.CANTOPEN:
|
|
|
|
|
return Cr.NS_ERROR_FILE_ACCESS_DENIED;
|
|
|
|
|
case Ci.mozIStorageError.LOCKED:
|
|
|
|
|
return Cr.NS_ERROR_FILE_IS_LOCKED;
|
|
|
|
|
case Ci.mozIStorageError.READONLY:
|
|
|
|
|
return Cr.NS_ERROR_FILE_READ_ONLY;
|
|
|
|
|
case Ci.mozIStorageError.ABORT:
|
|
|
|
|
case Ci.mozIStorageError.INTERRUPT:
|
|
|
|
|
return Cr.NS_ERROR_ABORT;
|
|
|
|
|
case Ci.mozIStorageError.TOOBIG:
|
|
|
|
|
case Ci.mozIStorageError.FULL:
|
|
|
|
|
return Cr.NS_ERROR_FILE_NO_DEVICE_SPACE;
|
|
|
|
|
case Ci.mozIStorageError.NOMEM:
|
|
|
|
|
return Cr.NS_ERROR_OUT_OF_MEMORY;
|
|
|
|
|
case Ci.mozIStorageError.BUSY:
|
|
|
|
|
return Cr.NS_ERROR_STORAGE_BUSY;
|
|
|
|
|
case Ci.mozIStorageError.CONSTRAINT:
|
|
|
|
|
return Cr.NS_ERROR_STORAGE_CONSTRAINT;
|
|
|
|
|
case Ci.mozIStorageError.NOLFS:
|
|
|
|
|
case Ci.mozIStorageError.IOERR:
|
|
|
|
|
return Cr.NS_ERROR_STORAGE_IOERR;
|
|
|
|
|
case Ci.mozIStorageError.SCHEMA:
|
|
|
|
|
case Ci.mozIStorageError.MISMATCH:
|
|
|
|
|
case Ci.mozIStorageError.MISUSE:
|
|
|
|
|
case Ci.mozIStorageError.RANGE:
|
|
|
|
|
return Ci.NS_ERROR_UNEXPECTED;
|
|
|
|
|
case Ci.mozIStorageError.CORRUPT:
|
|
|
|
|
case Ci.mozIStorageError.EMPTY:
|
|
|
|
|
case Ci.mozIStorageError.FORMAT:
|
|
|
|
|
case Ci.mozIStorageError.NOTADB:
|
|
|
|
|
return Cr.NS_ERROR_FILE_CORRUPTED;
|
|
|
|
|
default:
|
|
|
|
|
return Cr.NS_ERROR_FAILURE;
|
|
|
|
|
}
|
|
|
|
|
}
|
2014-05-13 09:00:00 +04:00
|
|
|
|
/**
|
2022-10-25 23:47:58 +03:00
|
|
|
|
* Barriers used to ensure that Sqlite.jsm is shutdown after all
|
2014-05-13 09:00:00 +04:00
|
|
|
|
* its clients.
|
|
|
|
|
*/
|
2022-06-07 07:29:50 +03:00
|
|
|
|
XPCOMUtils.defineLazyGetter(lazy, "Barriers", () => {
|
2014-05-13 09:00:00 +04:00
|
|
|
|
let Barriers = {
|
|
|
|
|
/**
|
|
|
|
|
* Public barrier that clients may use to add blockers to the
|
2022-10-25 23:47:58 +03:00
|
|
|
|
* shutdown of Sqlite.jsm. Triggered by profile-before-change.
|
2014-05-13 09:00:00 +04:00
|
|
|
|
* Once all blockers of this barrier are lifted, we close the
|
|
|
|
|
* ability to open new connections.
|
|
|
|
|
*/
|
2022-06-07 07:29:50 +03:00
|
|
|
|
shutdown: new lazy.AsyncShutdown.Barrier(
|
2022-10-25 23:47:58 +03:00
|
|
|
|
"Sqlite.jsm: wait until all clients have completed their task"
|
2014-05-13 09:00:00 +04:00
|
|
|
|
),
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Private barrier blocked by connections that are still open.
|
|
|
|
|
* Triggered after Barriers.shutdown is lifted and `isClosed` is
|
|
|
|
|
* set to `true`.
|
|
|
|
|
*/
|
2022-06-07 07:29:50 +03:00
|
|
|
|
connections: new lazy.AsyncShutdown.Barrier(
|
2022-10-25 23:47:58 +03:00
|
|
|
|
"Sqlite.jsm: wait until all connections are closed"
|
2014-05-13 09:00:00 +04:00
|
|
|
|
),
|
|
|
|
|
};
|
|
|
|
|
|
2014-06-12 01:40:17 +04:00
|
|
|
|
/**
|
|
|
|
|
* Observer for the event which is broadcasted when the finalization
|
|
|
|
|
* witness `_witness` of `OpenedConnection` is garbage collected.
|
|
|
|
|
*
|
|
|
|
|
* The observer is passed the connection identifier of the database
|
|
|
|
|
* connection that is being finalized.
|
|
|
|
|
*/
|
2016-11-12 02:22:34 +03:00
|
|
|
|
let finalizationObserver = function(subject, topic, identifier) {
|
2014-09-22 20:32:43 +04:00
|
|
|
|
let connectionData = ConnectionData.byId.get(identifier);
|
2014-06-12 01:40:17 +04:00
|
|
|
|
|
|
|
|
|
if (connectionData === undefined) {
|
|
|
|
|
logScriptError(
|
|
|
|
|
"Error: Attempt to finalize unknown Sqlite connection: " +
|
2014-09-22 20:32:43 +04:00
|
|
|
|
identifier +
|
|
|
|
|
"\n"
|
|
|
|
|
);
|
2014-06-12 01:40:17 +04:00
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2014-09-22 20:32:43 +04:00
|
|
|
|
ConnectionData.byId.delete(identifier);
|
|
|
|
|
logScriptError(
|
|
|
|
|
"Warning: Sqlite connection '" +
|
|
|
|
|
identifier +
|
2014-06-12 01:40:17 +04:00
|
|
|
|
"' was not properly closed. Auto-close triggered by garbage collection.\n"
|
|
|
|
|
);
|
|
|
|
|
connectionData.close();
|
|
|
|
|
};
|
2017-04-15 00:39:22 +03:00
|
|
|
|
Services.obs.addObserver(finalizationObserver, "sqlite-finalization-witness");
|
2014-06-12 01:40:17 +04:00
|
|
|
|
|
2014-05-13 09:00:00 +04:00
|
|
|
|
/**
|
2022-10-25 23:47:58 +03:00
|
|
|
|
* Ensure that Sqlite.jsm:
|
2014-05-13 09:00:00 +04:00
|
|
|
|
* - informs its clients before shutting down;
|
|
|
|
|
* - lets clients open connections during shutdown, if necessary;
|
|
|
|
|
* - waits for all connections to be closed before shutdown.
|
|
|
|
|
*/
|
2022-06-07 07:29:50 +03:00
|
|
|
|
lazy.AsyncShutdown.profileBeforeChange.addBlocker(
|
2022-10-25 23:47:58 +03:00
|
|
|
|
"Sqlite.jsm shutdown blocker",
|
2014-05-13 09:00:00 +04:00
|
|
|
|
async function() {
|
|
|
|
|
await Barriers.shutdown.wait();
|
|
|
|
|
// At this stage, all clients have had a chance to open (and close)
|
|
|
|
|
// their databases. Some previous close operations may still be pending,
|
|
|
|
|
// so we need to wait until they are complete before proceeding.
|
|
|
|
|
|
|
|
|
|
// Prevent any new opening.
|
|
|
|
|
isClosed = true;
|
|
|
|
|
|
|
|
|
|
// Now, wait until all databases are closed
|
|
|
|
|
await Barriers.connections.wait();
|
2014-06-12 01:40:17 +04:00
|
|
|
|
|
|
|
|
|
// Everything closed, no finalization events to catch
|
|
|
|
|
Services.obs.removeObserver(
|
|
|
|
|
finalizationObserver,
|
|
|
|
|
"sqlite-finalization-witness"
|
|
|
|
|
);
|
2014-05-13 09:00:00 +04:00
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
function status() {
|
|
|
|
|
if (isClosed) {
|
|
|
|
|
// We are waiting for the connections to close. The interesting
|
|
|
|
|
// status is therefore the list of connections still pending.
|
|
|
|
|
return {
|
|
|
|
|
description: "Waiting for connections to close",
|
2014-08-28 13:09:00 +04:00
|
|
|
|
state: Barriers.connections.state,
|
|
|
|
|
};
|
2014-05-13 09:00:00 +04:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// We are still in the first stage: waiting for the barrier
|
|
|
|
|
// to be lifted. The interesting status is therefore that of
|
|
|
|
|
// the barrier.
|
|
|
|
|
return {
|
|
|
|
|
description: "Waiting for the barrier to be lifted",
|
2014-08-28 13:09:00 +04:00
|
|
|
|
state: Barriers.shutdown.state,
|
|
|
|
|
};
|
2014-05-13 09:00:00 +04:00
|
|
|
|
}
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
return Barriers;
|
|
|
|
|
});
|
2013-01-05 03:36:59 +04:00
|
|
|
|
|
2014-06-12 01:40:06 +04:00
|
|
|
|
/**
|
|
|
|
|
* Connection data with methods necessary for closing the connection.
|
|
|
|
|
*
|
|
|
|
|
* To support auto-closing in the event of garbage collection, this
|
|
|
|
|
* data structure contains all the connection data of an opened
|
|
|
|
|
* connection and all of the methods needed for sucessfully closing
|
|
|
|
|
* it.
|
|
|
|
|
*
|
|
|
|
|
* By putting this information in its own separate object, it is
|
|
|
|
|
* possible to store an additional reference to it without preventing
|
|
|
|
|
* a garbage collection of a finalization witness in
|
|
|
|
|
* OpenedConnection. When the witness detects a garbage collection,
|
|
|
|
|
* this object can be used to close the connection.
|
|
|
|
|
*
|
|
|
|
|
* This object contains more methods than just `close`. When
|
|
|
|
|
* OpenedConnection needs to use the methods in this object, it will
|
|
|
|
|
* dispatch its method calls here.
|
|
|
|
|
*/
|
2016-11-11 01:48:04 +03:00
|
|
|
|
function ConnectionData(connection, identifier, options = {}) {
|
2022-06-07 07:29:50 +03:00
|
|
|
|
this._log = lazy.Log.repository.getLoggerWithMessagePrefix(
|
2022-10-25 23:47:58 +03:00
|
|
|
|
"Sqlite.jsm",
|
2021-09-09 19:22:49 +03:00
|
|
|
|
`Connection ${identifier}: `
|
2014-09-22 20:32:43 +04:00
|
|
|
|
);
|
2021-09-09 19:22:49 +03:00
|
|
|
|
this._log.manageLevelFromPref("toolkit.sqlitejsm.loglevel");
|
|
|
|
|
this._log.debug("Opened");
|
2014-06-12 01:40:06 +04:00
|
|
|
|
|
|
|
|
|
this._dbConn = connection;
|
2014-09-22 20:32:43 +04:00
|
|
|
|
|
|
|
|
|
// This is a unique identifier for the connection, generated through
|
2017-05-17 16:51:02 +03:00
|
|
|
|
// getIdentifierByFileName. It may be used for logging or as a key in Maps.
|
2014-09-22 20:32:43 +04:00
|
|
|
|
this._identifier = identifier;
|
|
|
|
|
|
2014-06-12 01:40:06 +04:00
|
|
|
|
this._open = true;
|
|
|
|
|
|
|
|
|
|
this._cachedStatements = new Map();
|
|
|
|
|
this._anonymousStatements = new Map();
|
|
|
|
|
this._anonymousCounter = 0;
|
|
|
|
|
|
|
|
|
|
// A map from statement index to mozIStoragePendingStatement, to allow for
|
|
|
|
|
// canceling prior to finalizing the mozIStorageStatements.
|
|
|
|
|
this._pendingStatements = new Map();
|
|
|
|
|
|
|
|
|
|
// Increments for each executed statement for the life of the connection.
|
|
|
|
|
this._statementCounter = 0;
|
|
|
|
|
|
2015-05-05 13:45:25 +03:00
|
|
|
|
// Increments whenever we request a unique operation id.
|
|
|
|
|
this._operationsCounter = 0;
|
|
|
|
|
|
2018-03-01 09:44:40 +03:00
|
|
|
|
if ("defaultTransactionType" in options) {
|
|
|
|
|
this.defaultTransactionType = options.defaultTransactionType;
|
|
|
|
|
} else {
|
|
|
|
|
this.defaultTransactionType = convertStorageTransactionType(
|
|
|
|
|
this._dbConn.defaultTransactionType
|
|
|
|
|
);
|
|
|
|
|
}
|
2021-09-09 19:22:48 +03:00
|
|
|
|
// Tracks whether this instance initiated a transaction.
|
|
|
|
|
this._initiatedTransaction = false;
|
2014-12-11 17:50:51 +03:00
|
|
|
|
// Manages a chain of transactions promises, so that new transactions
|
|
|
|
|
// always happen in queue to the previous ones. It never rejects.
|
|
|
|
|
this._transactionQueue = Promise.resolve();
|
2014-06-12 01:40:06 +04:00
|
|
|
|
|
|
|
|
|
this._idleShrinkMS = options.shrinkMemoryOnConnectionIdleMS;
|
|
|
|
|
if (this._idleShrinkMS) {
|
|
|
|
|
this._idleShrinkTimer = Cc["@mozilla.org/timer;1"].createInstance(
|
|
|
|
|
Ci.nsITimer
|
|
|
|
|
);
|
|
|
|
|
// We wait for the first statement execute to start the timer because
|
|
|
|
|
// shrinking now would not do anything.
|
|
|
|
|
}
|
|
|
|
|
|
2014-12-11 17:50:51 +03:00
|
|
|
|
// Deferred whose promise is resolved when the connection closing procedure
|
|
|
|
|
// is complete.
|
2022-06-07 07:29:50 +03:00
|
|
|
|
this._deferredClose = lazy.PromiseUtils.defer();
|
2014-07-07 08:29:00 +04:00
|
|
|
|
this._closeRequested = false;
|
2014-06-12 01:40:06 +04:00
|
|
|
|
|
2015-05-05 13:45:25 +03:00
|
|
|
|
// An AsyncShutdown barrier used to make sure that we wait until clients
|
|
|
|
|
// are done before shutting down the connection.
|
2022-06-07 07:29:50 +03:00
|
|
|
|
this._barrier = new lazy.AsyncShutdown.Barrier(
|
2015-05-05 13:45:25 +03:00
|
|
|
|
`${this._identifier}: waiting for clients`
|
|
|
|
|
);
|
|
|
|
|
|
2022-06-07 07:29:50 +03:00
|
|
|
|
lazy.Barriers.connections.client.addBlocker(
|
2014-09-22 20:32:43 +04:00
|
|
|
|
this._identifier + ": waiting for shutdown",
|
2014-07-07 08:29:00 +04:00
|
|
|
|
this._deferredClose.promise,
|
2016-12-31 05:47:25 +03:00
|
|
|
|
() => ({
|
2014-09-22 20:32:43 +04:00
|
|
|
|
identifier: this._identifier,
|
2014-07-07 08:29:00 +04:00
|
|
|
|
isCloseRequested: this._closeRequested,
|
|
|
|
|
hasDbConn: !!this._dbConn,
|
2021-09-09 19:22:48 +03:00
|
|
|
|
initiatedTransaction: this._initiatedTransaction,
|
2014-07-07 08:29:00 +04:00
|
|
|
|
pendingStatements: this._pendingStatements.size,
|
|
|
|
|
statementCounter: this._statementCounter,
|
|
|
|
|
})
|
2014-06-12 01:40:06 +04:00
|
|
|
|
);
|
2018-03-01 21:43:07 +03:00
|
|
|
|
|
2021-09-09 19:22:50 +03:00
|
|
|
|
// We avoid creating a timer for every transaction, because in most cases they
|
|
|
|
|
// are not canceled and they are only used as a timeout.
|
|
|
|
|
// Instead the timer is reused when it's sufficiently close to the previous
|
|
|
|
|
// creation time (see `_getTimeoutPromise` for more info).
|
2018-03-01 21:43:07 +03:00
|
|
|
|
this._timeoutPromise = null;
|
|
|
|
|
// The last timestamp when we should consider using `this._timeoutPromise`.
|
|
|
|
|
this._timeoutPromiseExpires = 0;
|
2014-06-12 01:40:06 +04:00
|
|
|
|
}
|
|
|
|
|
|
2014-06-12 01:40:17 +04:00
|
|
|
|
/**
|
|
|
|
|
* Map of connection identifiers to ConnectionData objects
|
|
|
|
|
*
|
|
|
|
|
* The connection identifier is a human-readable name of the
|
|
|
|
|
* database. Used by finalization witnesses to be able to close opened
|
|
|
|
|
* connections on garbage collection.
|
|
|
|
|
*
|
2014-09-22 20:32:43 +04:00
|
|
|
|
* Key: _identifier of ConnectionData
|
2014-06-12 01:40:17 +04:00
|
|
|
|
* Value: ConnectionData object
|
|
|
|
|
*/
|
|
|
|
|
ConnectionData.byId = new Map();
|
|
|
|
|
|
2014-06-12 01:40:06 +04:00
|
|
|
|
ConnectionData.prototype = Object.freeze({
|
2015-05-05 13:45:25 +03:00
|
|
|
|
/**
|
|
|
|
|
* Run a task, ensuring that its execution will not be interrupted by shutdown.
|
|
|
|
|
*
|
|
|
|
|
* As the operations of this module are asynchronous, a sequence of operations,
|
|
|
|
|
* or even an individual operation, can still be pending when the process shuts
|
|
|
|
|
* down. If any of this operations is a write, this can cause data loss, simply
|
|
|
|
|
* because the write has not been completed (or even started) by shutdown.
|
|
|
|
|
*
|
|
|
|
|
* To avoid this risk, clients are encouraged to use `executeBeforeShutdown` for
|
|
|
|
|
* any write operation, as follows:
|
|
|
|
|
*
|
|
|
|
|
* myConnection.executeBeforeShutdown("Bookmarks: Removing a bookmark",
|
2019-01-04 11:41:35 +03:00
|
|
|
|
* async function(db) {
|
2015-05-05 13:45:25 +03:00
|
|
|
|
* // The connection will not be closed and shutdown will not proceed
|
|
|
|
|
* // until this task has completed.
|
|
|
|
|
*
|
|
|
|
|
* // `db` exposes the same API as `myConnection` but provides additional
|
|
|
|
|
* // logging support to help debug hard-to-catch shutdown timeouts.
|
|
|
|
|
*
|
2019-01-04 11:41:35 +03:00
|
|
|
|
* await db.execute(...);
|
2015-05-05 13:45:25 +03:00
|
|
|
|
* }));
|
|
|
|
|
*
|
|
|
|
|
* @param {string} name A human-readable name for the ongoing operation, used
|
|
|
|
|
* for logging and debugging purposes.
|
2022-10-25 23:47:58 +03:00
|
|
|
|
* @param {function(db)} task A function that takes as argument a Sqlite.jsm
|
2015-05-05 13:45:25 +03:00
|
|
|
|
* db and returns a Promise.
|
|
|
|
|
*/
|
2016-12-30 02:34:54 +03:00
|
|
|
|
executeBeforeShutdown(parent, name, task) {
|
2015-05-05 13:45:25 +03:00
|
|
|
|
if (!name) {
|
|
|
|
|
throw new TypeError("Expected a human-readable name as first argument");
|
|
|
|
|
}
|
|
|
|
|
if (typeof task != "function") {
|
|
|
|
|
throw new TypeError("Expected a function as second argument");
|
|
|
|
|
}
|
|
|
|
|
if (this._closeRequested) {
|
|
|
|
|
throw new Error(
|
2020-03-09 00:45:16 +03:00
|
|
|
|
`${this._identifier}: cannot execute operation ${name}, the connection is already closing`
|
2015-05-05 13:45:25 +03:00
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Status, used for AsyncShutdown crash reports.
|
|
|
|
|
let status = {
|
|
|
|
|
// The latest command started by `task`, either as a
|
|
|
|
|
// sql string, or as one of "<not started>" or "<closing>".
|
|
|
|
|
command: "<not started>",
|
|
|
|
|
|
|
|
|
|
// `true` if `command` was started but not completed yet.
|
|
|
|
|
isPending: false,
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
// An object with the same API as `this` but with
|
|
|
|
|
// additional logging. To keep logging simple, we
|
|
|
|
|
// assume that `task` is not running several queries
|
|
|
|
|
// concurrently.
|
|
|
|
|
let loggedDb = Object.create(parent, {
|
|
|
|
|
execute: {
|
2017-05-12 15:41:20 +03:00
|
|
|
|
value: async (sql, ...rest) => {
|
2015-05-05 13:45:25 +03:00
|
|
|
|
status.isPending = true;
|
|
|
|
|
status.command = sql;
|
|
|
|
|
try {
|
2017-05-12 15:41:20 +03:00
|
|
|
|
return await this.execute(sql, ...rest);
|
2015-05-05 13:45:25 +03:00
|
|
|
|
} finally {
|
|
|
|
|
status.isPending = false;
|
|
|
|
|
}
|
2017-05-12 15:41:20 +03:00
|
|
|
|
},
|
2015-05-05 13:45:25 +03:00
|
|
|
|
},
|
|
|
|
|
close: {
|
2017-05-12 15:41:20 +03:00
|
|
|
|
value: async () => {
|
2020-10-28 16:43:16 +03:00
|
|
|
|
status.isPending = true;
|
2015-05-05 13:45:25 +03:00
|
|
|
|
status.command = "<close>";
|
|
|
|
|
try {
|
2017-05-12 15:41:20 +03:00
|
|
|
|
return await this.close();
|
2015-05-05 13:45:25 +03:00
|
|
|
|
} finally {
|
|
|
|
|
status.isPending = false;
|
|
|
|
|
}
|
2017-05-12 15:41:20 +03:00
|
|
|
|
},
|
2015-05-05 13:45:25 +03:00
|
|
|
|
},
|
|
|
|
|
executeCached: {
|
2017-05-12 15:41:20 +03:00
|
|
|
|
value: async (sql, ...rest) => {
|
2020-10-28 16:43:16 +03:00
|
|
|
|
status.isPending = true;
|
|
|
|
|
status.command = "cached: " + sql;
|
2015-05-05 13:45:25 +03:00
|
|
|
|
try {
|
2017-05-12 15:41:20 +03:00
|
|
|
|
return await this.executeCached(sql, ...rest);
|
2015-05-05 13:45:25 +03:00
|
|
|
|
} finally {
|
|
|
|
|
status.isPending = false;
|
|
|
|
|
}
|
2017-05-12 15:41:20 +03:00
|
|
|
|
},
|
2015-05-05 13:45:25 +03:00
|
|
|
|
},
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
let promiseResult = task(loggedDb);
|
|
|
|
|
if (
|
|
|
|
|
!promiseResult ||
|
|
|
|
|
typeof promiseResult != "object" ||
|
|
|
|
|
!("then" in promiseResult)
|
|
|
|
|
) {
|
|
|
|
|
throw new TypeError("Expected a Promise");
|
|
|
|
|
}
|
|
|
|
|
let key = `${this._identifier}: ${name} (${this._getOperationId()})`;
|
|
|
|
|
let promiseComplete = promiseResult.catch(() => {});
|
|
|
|
|
this._barrier.client.addBlocker(key, promiseComplete, {
|
|
|
|
|
fetchState: () => status,
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
return (async () => {
|
|
|
|
|
try {
|
|
|
|
|
return await promiseResult;
|
|
|
|
|
} finally {
|
2017-10-15 21:50:30 +03:00
|
|
|
|
this._barrier.client.removeBlocker(key, promiseComplete);
|
2015-05-05 13:45:25 +03:00
|
|
|
|
}
|
|
|
|
|
})();
|
|
|
|
|
},
|
2016-12-30 02:34:54 +03:00
|
|
|
|
close() {
|
2014-07-07 08:29:00 +04:00
|
|
|
|
this._closeRequested = true;
|
|
|
|
|
|
2014-06-12 01:40:06 +04:00
|
|
|
|
if (!this._dbConn) {
|
|
|
|
|
return this._deferredClose.promise;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
this._log.debug("Request to close connection.");
|
|
|
|
|
this._clearIdleShrinkTimer();
|
|
|
|
|
|
2015-05-05 13:45:25 +03:00
|
|
|
|
return this._barrier.wait().then(() => {
|
|
|
|
|
if (!this._dbConn) {
|
2016-02-04 00:12:18 +03:00
|
|
|
|
return undefined;
|
2015-05-05 13:45:25 +03:00
|
|
|
|
}
|
2014-12-11 17:50:51 +03:00
|
|
|
|
return this._finalize();
|
2015-05-05 13:45:25 +03:00
|
|
|
|
});
|
2014-06-12 01:40:06 +04:00
|
|
|
|
},
|
|
|
|
|
|
2016-12-30 02:34:54 +03:00
|
|
|
|
clone(readOnly = false) {
|
2014-06-12 01:40:06 +04:00
|
|
|
|
this.ensureOpen();
|
|
|
|
|
|
|
|
|
|
this._log.debug("Request to clone connection.");
|
|
|
|
|
|
|
|
|
|
let options = {
|
|
|
|
|
connection: this._dbConn,
|
2016-12-30 02:34:54 +03:00
|
|
|
|
readOnly,
|
2014-06-12 01:40:06 +04:00
|
|
|
|
};
|
|
|
|
|
if (this._idleShrinkMS) {
|
|
|
|
|
options.shrinkMemoryOnConnectionIdleMS = this._idleShrinkMS;
|
2019-07-05 12:15:43 +03:00
|
|
|
|
}
|
2014-06-12 01:40:06 +04:00
|
|
|
|
|
|
|
|
|
return cloneStorageConnection(options);
|
|
|
|
|
},
|
2016-12-30 02:34:54 +03:00
|
|
|
|
_getOperationId() {
|
2015-05-05 13:45:25 +03:00
|
|
|
|
return this._operationsCounter++;
|
|
|
|
|
},
|
2016-12-30 02:34:54 +03:00
|
|
|
|
_finalize() {
|
2014-06-12 01:40:06 +04:00
|
|
|
|
this._log.debug("Finalizing connection.");
|
|
|
|
|
// Cancel any pending statements.
|
|
|
|
|
for (let [, /* k */ statement] of this._pendingStatements) {
|
|
|
|
|
statement.cancel();
|
|
|
|
|
}
|
|
|
|
|
this._pendingStatements.clear();
|
|
|
|
|
|
|
|
|
|
// We no longer need to track these.
|
|
|
|
|
this._statementCounter = 0;
|
|
|
|
|
|
|
|
|
|
// Next we finalize all active statements.
|
|
|
|
|
for (let [, /* k */ statement] of this._anonymousStatements) {
|
|
|
|
|
statement.finalize();
|
|
|
|
|
}
|
|
|
|
|
this._anonymousStatements.clear();
|
|
|
|
|
|
|
|
|
|
for (let [, /* k */ statement] of this._cachedStatements) {
|
|
|
|
|
statement.finalize();
|
|
|
|
|
}
|
|
|
|
|
this._cachedStatements.clear();
|
|
|
|
|
|
|
|
|
|
// This guards against operations performed between the call to this
|
|
|
|
|
// function and asyncClose() finishing. See also bug 726990.
|
|
|
|
|
this._open = false;
|
|
|
|
|
|
2022-10-25 23:47:58 +03:00
|
|
|
|
// We must always close the connection at the Sqlite.jsm-level, not
|
2014-09-22 20:32:43 +04:00
|
|
|
|
// necessarily at the mozStorage-level.
|
|
|
|
|
let markAsClosed = () => {
|
2021-09-09 19:22:49 +03:00
|
|
|
|
this._log.debug("Closed");
|
2014-06-12 01:40:06 +04:00
|
|
|
|
// Now that the connection is closed, no need to keep
|
|
|
|
|
// a blocker for Barriers.connections.
|
2022-06-07 07:29:50 +03:00
|
|
|
|
lazy.Barriers.connections.client.removeBlocker(
|
|
|
|
|
this._deferredClose.promise
|
|
|
|
|
);
|
2014-12-11 17:50:51 +03:00
|
|
|
|
this._deferredClose.resolve();
|
2017-10-15 21:50:30 +03:00
|
|
|
|
};
|
2014-09-22 20:32:43 +04:00
|
|
|
|
if (wrappedConnections.has(this._identifier)) {
|
|
|
|
|
wrappedConnections.delete(this._identifier);
|
2015-06-04 16:09:37 +03:00
|
|
|
|
this._dbConn = null;
|
2014-09-22 20:32:43 +04:00
|
|
|
|
markAsClosed();
|
|
|
|
|
} else {
|
|
|
|
|
this._log.debug("Calling asyncClose().");
|
2020-06-13 06:10:45 +03:00
|
|
|
|
try {
|
|
|
|
|
this._dbConn.asyncClose(markAsClosed);
|
|
|
|
|
} catch (ex) {
|
|
|
|
|
// If for any reason asyncClose fails, we must still remove the
|
|
|
|
|
// shutdown blockers and resolve _deferredClose.
|
|
|
|
|
markAsClosed();
|
|
|
|
|
} finally {
|
|
|
|
|
this._dbConn = null;
|
|
|
|
|
}
|
2014-09-22 20:32:43 +04:00
|
|
|
|
}
|
2014-12-11 17:50:51 +03:00
|
|
|
|
return this._deferredClose.promise;
|
2014-06-12 01:40:06 +04:00
|
|
|
|
},
|
|
|
|
|
|
2016-12-30 02:34:54 +03:00
|
|
|
|
executeCached(sql, params = null, onRow = null) {
|
2014-06-12 01:40:06 +04:00
|
|
|
|
this.ensureOpen();
|
|
|
|
|
|
|
|
|
|
if (!sql) {
|
|
|
|
|
throw new Error("sql argument is empty.");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
let statement = this._cachedStatements.get(sql);
|
|
|
|
|
if (!statement) {
|
|
|
|
|
statement = this._dbConn.createAsyncStatement(sql);
|
|
|
|
|
this._cachedStatements.set(sql, statement);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
this._clearIdleShrinkTimer();
|
|
|
|
|
|
2014-12-11 17:50:51 +03:00
|
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
|
try {
|
|
|
|
|
this._executeStatement(sql, statement, params, onRow).then(
|
|
|
|
|
result => {
|
|
|
|
|
this._startIdleShrinkTimer();
|
|
|
|
|
resolve(result);
|
|
|
|
|
},
|
|
|
|
|
error => {
|
|
|
|
|
this._startIdleShrinkTimer();
|
|
|
|
|
reject(error);
|
|
|
|
|
}
|
|
|
|
|
);
|
|
|
|
|
} catch (ex) {
|
|
|
|
|
this._startIdleShrinkTimer();
|
|
|
|
|
throw ex;
|
|
|
|
|
}
|
|
|
|
|
});
|
2014-06-12 01:40:06 +04:00
|
|
|
|
},
|
|
|
|
|
|
2016-12-30 02:34:54 +03:00
|
|
|
|
execute(sql, params = null, onRow = null) {
|
2014-06-12 01:40:06 +04:00
|
|
|
|
if (typeof sql != "string") {
|
|
|
|
|
throw new Error("Must define SQL to execute as a string: " + sql);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
this.ensureOpen();
|
|
|
|
|
|
|
|
|
|
let statement = this._dbConn.createAsyncStatement(sql);
|
|
|
|
|
let index = this._anonymousCounter++;
|
|
|
|
|
|
|
|
|
|
this._anonymousStatements.set(index, statement);
|
|
|
|
|
this._clearIdleShrinkTimer();
|
|
|
|
|
|
|
|
|
|
let onFinished = () => {
|
|
|
|
|
this._anonymousStatements.delete(index);
|
|
|
|
|
statement.finalize();
|
|
|
|
|
this._startIdleShrinkTimer();
|
|
|
|
|
};
|
|
|
|
|
|
2014-12-11 17:50:51 +03:00
|
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
|
try {
|
|
|
|
|
this._executeStatement(sql, statement, params, onRow).then(
|
|
|
|
|
rows => {
|
|
|
|
|
onFinished();
|
|
|
|
|
resolve(rows);
|
|
|
|
|
},
|
|
|
|
|
error => {
|
|
|
|
|
onFinished();
|
|
|
|
|
reject(error);
|
|
|
|
|
}
|
|
|
|
|
);
|
|
|
|
|
} catch (ex) {
|
|
|
|
|
onFinished();
|
|
|
|
|
throw ex;
|
|
|
|
|
}
|
|
|
|
|
});
|
2014-06-12 01:40:06 +04:00
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
get transactionInProgress() {
|
Bug 1613835 - Use a SQLite API call to check for in-progress transactions. r=mak
Previously, `mozIStorageConnection#transactionInProgress` returned true
only if a transaction was started via `beginTransaction()`. This meant
that manually executing `BEGIN`, as `Sqlite.jsm` and the Rust bindings
do, wouldn't accurately report if a transaction was in progress.
Similarly, the flag wasn't accurate in cases where SQLite automatically
rolled back a transaction.
Fortunately, SQLite provides the `sqlite3_get_autocommit()` function,
which we can use to determine if a transaction is open or not. This
commit refactors the `transactionInProgress` getter, along with all
`Connection` methods that depend on it, to use the SQLite API instead
of managing that state on the connection. `mozStorageTransaction` and
`Sqlite.jsm` still use their own flags to decide whether to commit
their transactions, for reasons explained in the IDL comment.
This commit also moves `transactionInProgress` to
`mozIStorageAsyncConnection`, so that `Sqlite.jsm` can use it, and
exposes it to Rust.
Differential Revision: https://phabricator.services.mozilla.com/D63732
--HG--
extra : moz-landing-system : lando
2020-03-04 01:57:39 +03:00
|
|
|
|
return this._open && this._dbConn.transactionInProgress;
|
2014-06-12 01:40:06 +04:00
|
|
|
|
},
|
|
|
|
|
|
2016-12-30 02:34:54 +03:00
|
|
|
|
executeTransaction(func, type) {
|
2021-09-09 19:22:50 +03:00
|
|
|
|
// Identify the caller for debugging purposes.
|
|
|
|
|
let caller = new Error().stack
|
|
|
|
|
.split("\n", 3)
|
|
|
|
|
.pop()
|
|
|
|
|
.match(/^([^@]+@).*\/([^\/:]+)[:0-9]*$/);
|
|
|
|
|
caller = caller[1] + caller[2];
|
|
|
|
|
this._log.debug(`Transaction (type ${type}) requested by: ${caller}`);
|
|
|
|
|
|
2018-03-01 09:44:40 +03:00
|
|
|
|
if (type == OpenedConnection.prototype.TRANSACTION_DEFAULT) {
|
|
|
|
|
type = this.defaultTransactionType;
|
|
|
|
|
} else if (!OpenedConnection.TRANSACTION_TYPES.includes(type)) {
|
|
|
|
|
throw new Error("Unknown transaction type: " + type);
|
2015-05-05 13:45:25 +03:00
|
|
|
|
}
|
2014-06-12 01:40:06 +04:00
|
|
|
|
this.ensureOpen();
|
|
|
|
|
|
2021-09-09 19:22:50 +03:00
|
|
|
|
// If a transaction yields on a never resolved promise, or is mistakenly
|
|
|
|
|
// nested, it could hang the transactions queue forever. Thus we timeout
|
|
|
|
|
// the execution after a meaningful amount of time, to ensure in any case
|
|
|
|
|
// we'll proceed after a while.
|
|
|
|
|
let timeoutPromise = this._getTimeoutPromise();
|
2014-06-12 01:40:06 +04:00
|
|
|
|
|
2014-12-11 17:50:51 +03:00
|
|
|
|
let promise = this._transactionQueue.then(() => {
|
|
|
|
|
if (this._closeRequested) {
|
|
|
|
|
throw new Error("Transaction canceled due to a closed connection.");
|
2014-06-12 01:40:06 +04:00
|
|
|
|
}
|
|
|
|
|
|
2014-12-11 17:50:51 +03:00
|
|
|
|
let transactionPromise = (async () => {
|
|
|
|
|
// At this point we should never have an in progress transaction, since
|
|
|
|
|
// they are enqueued.
|
2021-09-09 19:22:48 +03:00
|
|
|
|
if (this._initiatedTransaction) {
|
2021-09-09 19:22:49 +03:00
|
|
|
|
this._log.error(
|
2014-12-11 17:50:51 +03:00
|
|
|
|
"Unexpected transaction in progress when trying to start a new one."
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
try {
|
|
|
|
|
// We catch errors in statement execution to detect nested transactions.
|
|
|
|
|
try {
|
|
|
|
|
await this.execute("BEGIN " + type + " TRANSACTION");
|
2021-09-09 19:22:50 +03:00
|
|
|
|
this._log.debug(`Begin transaction`);
|
2021-09-09 19:22:48 +03:00
|
|
|
|
this._initiatedTransaction = true;
|
2014-12-11 17:50:51 +03:00
|
|
|
|
} catch (ex) {
|
|
|
|
|
// Unfortunately, if we are wrapping an existing connection, a
|
|
|
|
|
// transaction could have been started by a client of the same
|
2022-10-25 23:47:58 +03:00
|
|
|
|
// connection that doesn't use Sqlite.jsm (e.g. C++ consumer).
|
2014-12-11 17:50:51 +03:00
|
|
|
|
// The best we can do is proceed without a transaction and hope
|
|
|
|
|
// things won't break.
|
|
|
|
|
if (wrappedConnections.has(this._identifier)) {
|
2015-12-18 06:05:00 +03:00
|
|
|
|
this._log.warn(
|
|
|
|
|
"A new transaction could not be started cause the wrapped connection had one in progress",
|
|
|
|
|
ex
|
|
|
|
|
);
|
2014-12-11 17:50:51 +03:00
|
|
|
|
} else {
|
2015-12-18 06:05:00 +03:00
|
|
|
|
this._log.warn(
|
|
|
|
|
"A transaction was already in progress, likely a nested transaction",
|
|
|
|
|
ex
|
|
|
|
|
);
|
2014-12-11 17:50:51 +03:00
|
|
|
|
throw ex;
|
|
|
|
|
}
|
|
|
|
|
}
|
2014-06-12 01:40:06 +04:00
|
|
|
|
|
2014-12-11 17:50:51 +03:00
|
|
|
|
let result;
|
|
|
|
|
try {
|
2021-09-09 19:22:50 +03:00
|
|
|
|
result = await Promise.race([func(), timeoutPromise]);
|
2014-12-11 17:50:51 +03:00
|
|
|
|
} catch (ex) {
|
|
|
|
|
// It's possible that the exception has been caused by trying to
|
|
|
|
|
// close the connection in the middle of a transaction.
|
|
|
|
|
if (this._closeRequested) {
|
2015-12-18 06:05:00 +03:00
|
|
|
|
this._log.warn(
|
|
|
|
|
"Connection closed while performing a transaction",
|
|
|
|
|
ex
|
|
|
|
|
);
|
2014-12-11 17:50:51 +03:00
|
|
|
|
} else {
|
2021-09-09 19:22:50 +03:00
|
|
|
|
// Otherwise the function didn't resolve before the timeout, or
|
|
|
|
|
// generated an unexpected error. Then we rollback.
|
|
|
|
|
if (ex.becauseTimedOut) {
|
|
|
|
|
let caller_module = caller.split(":", 1)[0];
|
|
|
|
|
Services.telemetry.keyedScalarAdd(
|
|
|
|
|
"mozstorage.sqlitejsm_transaction_timeout",
|
|
|
|
|
caller_module,
|
|
|
|
|
1
|
|
|
|
|
);
|
|
|
|
|
this._log.error(
|
|
|
|
|
`The transaction requested by ${caller} timed out. Rolling back`,
|
|
|
|
|
ex
|
|
|
|
|
);
|
|
|
|
|
} else {
|
|
|
|
|
this._log.error(
|
|
|
|
|
`Error during transaction requested by ${caller}. Rolling back`,
|
|
|
|
|
ex
|
|
|
|
|
);
|
|
|
|
|
}
|
2014-12-11 17:50:51 +03:00
|
|
|
|
// If we began a transaction, we must rollback it.
|
2021-09-09 19:22:48 +03:00
|
|
|
|
if (this._initiatedTransaction) {
|
2014-12-11 17:50:51 +03:00
|
|
|
|
try {
|
|
|
|
|
await this.execute("ROLLBACK TRANSACTION");
|
2021-09-09 19:22:50 +03:00
|
|
|
|
this._initiatedTransaction = false;
|
|
|
|
|
this._log.debug(`Roll back transaction`);
|
2014-12-11 17:50:51 +03:00
|
|
|
|
} catch (inner) {
|
2021-09-09 19:22:49 +03:00
|
|
|
|
this._log.error("Could not roll back transaction", inner);
|
2014-12-11 17:50:51 +03:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
// Rethrow the exception.
|
|
|
|
|
throw ex;
|
|
|
|
|
}
|
2014-06-12 01:40:06 +04:00
|
|
|
|
|
2014-12-11 17:50:51 +03:00
|
|
|
|
// See comment above about connection being closed during transaction.
|
|
|
|
|
if (this._closeRequested) {
|
|
|
|
|
this._log.warn(
|
|
|
|
|
"Connection closed before committing the transaction."
|
|
|
|
|
);
|
|
|
|
|
throw new Error(
|
|
|
|
|
"Connection closed before committing the transaction."
|
|
|
|
|
);
|
|
|
|
|
}
|
2014-06-12 01:40:06 +04:00
|
|
|
|
|
2014-12-11 17:50:51 +03:00
|
|
|
|
// If we began a transaction, we must commit it.
|
2021-09-09 19:22:48 +03:00
|
|
|
|
if (this._initiatedTransaction) {
|
2014-12-11 17:50:51 +03:00
|
|
|
|
try {
|
|
|
|
|
await this.execute("COMMIT TRANSACTION");
|
2021-09-09 19:22:50 +03:00
|
|
|
|
this._log.debug(`Commit transaction`);
|
2014-12-11 17:50:51 +03:00
|
|
|
|
} catch (ex) {
|
2015-12-18 06:05:00 +03:00
|
|
|
|
this._log.warn("Error committing transaction", ex);
|
2014-12-11 17:50:51 +03:00
|
|
|
|
throw ex;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return result;
|
|
|
|
|
} finally {
|
2021-09-09 19:22:48 +03:00
|
|
|
|
this._initiatedTransaction = false;
|
2014-12-11 17:50:51 +03:00
|
|
|
|
}
|
|
|
|
|
})();
|
|
|
|
|
|
|
|
|
|
return Promise.race([transactionPromise, timeoutPromise]);
|
|
|
|
|
});
|
|
|
|
|
// Atomically update the queue before anyone else has a chance to enqueue
|
|
|
|
|
// further transactions.
|
2017-10-15 21:50:30 +03:00
|
|
|
|
this._transactionQueue = promise.catch(ex => {
|
2021-09-09 19:22:49 +03:00
|
|
|
|
this._log.error(ex);
|
2017-10-15 21:50:30 +03:00
|
|
|
|
});
|
2015-05-05 13:45:25 +03:00
|
|
|
|
|
|
|
|
|
// Make sure that we do not shutdown the connection during a transaction.
|
|
|
|
|
this._barrier.client.addBlocker(
|
|
|
|
|
`Transaction (${this._getOperationId()})`,
|
|
|
|
|
this._transactionQueue
|
|
|
|
|
);
|
2014-12-11 17:50:51 +03:00
|
|
|
|
return promise;
|
2014-06-12 01:40:06 +04:00
|
|
|
|
},
|
|
|
|
|
|
2016-12-30 02:34:54 +03:00
|
|
|
|
shrinkMemory() {
|
2021-09-09 19:22:49 +03:00
|
|
|
|
this._log.debug("Shrinking memory usage.");
|
2014-06-12 01:40:06 +04:00
|
|
|
|
let onShrunk = this._clearIdleShrinkTimer.bind(this);
|
2020-04-17 15:36:05 +03:00
|
|
|
|
return this.execute("PRAGMA shrink_memory").then(onShrunk, onShrunk);
|
2014-06-12 01:40:06 +04:00
|
|
|
|
},
|
|
|
|
|
|
2016-12-30 02:34:54 +03:00
|
|
|
|
discardCachedStatements() {
|
2014-06-12 01:40:06 +04:00
|
|
|
|
let count = 0;
|
|
|
|
|
for (let [, /* k */ statement] of this._cachedStatements) {
|
|
|
|
|
++count;
|
|
|
|
|
statement.finalize();
|
|
|
|
|
}
|
|
|
|
|
this._cachedStatements.clear();
|
|
|
|
|
this._log.debug("Discarded " + count + " cached statements.");
|
|
|
|
|
return count;
|
|
|
|
|
},
|
|
|
|
|
|
2017-07-31 23:27:23 +03:00
|
|
|
|
interrupt() {
|
2021-09-09 19:22:49 +03:00
|
|
|
|
this._log.debug("Trying to interrupt.");
|
2017-07-31 23:27:23 +03:00
|
|
|
|
this.ensureOpen();
|
|
|
|
|
this._dbConn.interrupt();
|
|
|
|
|
},
|
|
|
|
|
|
2014-06-12 01:40:06 +04:00
|
|
|
|
/**
|
|
|
|
|
* Helper method to bind parameters of various kinds through
|
|
|
|
|
* reflection.
|
|
|
|
|
*/
|
2016-12-30 02:34:54 +03:00
|
|
|
|
_bindParameters(statement, params) {
|
2014-06-12 01:40:06 +04:00
|
|
|
|
if (!params) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2016-02-19 12:24:54 +03:00
|
|
|
|
function bindParam(obj, key, val) {
|
2017-02-06 21:30:19 +03:00
|
|
|
|
let isBlob =
|
|
|
|
|
val && typeof val == "object" && val.constructor.name == "Uint8Array";
|
2017-04-13 19:49:45 +03:00
|
|
|
|
let args = [key, val];
|
|
|
|
|
if (isBlob) {
|
|
|
|
|
args.push(val.length);
|
2019-07-05 12:15:43 +03:00
|
|
|
|
}
|
2016-02-19 12:24:54 +03:00
|
|
|
|
let methodName = `bind${isBlob ? "Blob" : ""}By${
|
|
|
|
|
typeof key == "number" ? "Index" : "Name"
|
|
|
|
|
}`;
|
|
|
|
|
obj[methodName](...args);
|
|
|
|
|
}
|
|
|
|
|
|
2014-06-12 01:40:06 +04:00
|
|
|
|
if (Array.isArray(params)) {
|
|
|
|
|
// It's an array of separate params.
|
2019-07-31 18:21:50 +03:00
|
|
|
|
if (params.length && typeof params[0] == "object" && params[0] !== null) {
|
2014-06-12 01:40:06 +04:00
|
|
|
|
let paramsArray = statement.newBindingParamsArray();
|
|
|
|
|
for (let p of params) {
|
|
|
|
|
let bindings = paramsArray.newBindingParams();
|
2016-08-03 09:23:10 +03:00
|
|
|
|
for (let [key, value] of Object.entries(p)) {
|
2016-02-19 12:24:54 +03:00
|
|
|
|
bindParam(bindings, key, value);
|
2014-06-12 01:40:06 +04:00
|
|
|
|
}
|
|
|
|
|
paramsArray.addParams(bindings);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
statement.bindParameters(paramsArray);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Indexed params.
|
|
|
|
|
for (let i = 0; i < params.length; i++) {
|
2016-02-19 12:24:54 +03:00
|
|
|
|
bindParam(statement, i, params[i]);
|
2014-06-12 01:40:06 +04:00
|
|
|
|
}
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Named params.
|
|
|
|
|
if (params && typeof params == "object") {
|
|
|
|
|
for (let k in params) {
|
2016-02-19 12:24:54 +03:00
|
|
|
|
bindParam(statement, k, params[k]);
|
2014-06-12 01:40:06 +04:00
|
|
|
|
}
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
throw new Error(
|
|
|
|
|
"Invalid type for bound parameters. Expected Array or " +
|
|
|
|
|
"object. Got: " +
|
|
|
|
|
params
|
|
|
|
|
);
|
|
|
|
|
},
|
|
|
|
|
|
2016-12-30 02:34:54 +03:00
|
|
|
|
_executeStatement(sql, statement, params, onRow) {
|
2014-06-12 01:40:06 +04:00
|
|
|
|
if (statement.state != statement.MOZ_STORAGE_STATEMENT_READY) {
|
|
|
|
|
throw new Error("Statement is not ready for execution.");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (onRow && typeof onRow != "function") {
|
|
|
|
|
throw new Error("onRow must be a function. Got: " + onRow);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
this._bindParameters(statement, params);
|
|
|
|
|
|
|
|
|
|
let index = this._statementCounter++;
|
|
|
|
|
|
2022-06-07 07:29:50 +03:00
|
|
|
|
let deferred = lazy.PromiseUtils.defer();
|
2014-06-12 01:40:06 +04:00
|
|
|
|
let userCancelled = false;
|
|
|
|
|
let errors = [];
|
|
|
|
|
let rows = [];
|
2014-09-26 17:17:00 +04:00
|
|
|
|
let handledRow = false;
|
2014-06-12 01:40:06 +04:00
|
|
|
|
|
|
|
|
|
// Don't incur overhead for serializing params unless the messages go
|
|
|
|
|
// somewhere.
|
2022-06-07 07:29:50 +03:00
|
|
|
|
if (this._log.level <= lazy.Log.Level.Trace) {
|
2014-06-12 01:40:06 +04:00
|
|
|
|
let msg = "Stmt #" + index + " " + sql;
|
|
|
|
|
|
|
|
|
|
if (params) {
|
|
|
|
|
msg += " - " + JSON.stringify(params);
|
|
|
|
|
}
|
|
|
|
|
this._log.trace(msg);
|
|
|
|
|
} else {
|
|
|
|
|
this._log.debug("Stmt #" + index + " starting");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
let self = this;
|
|
|
|
|
let pending = statement.executeAsync({
|
2016-12-30 02:34:54 +03:00
|
|
|
|
handleResult(resultSet) {
|
2014-06-12 01:40:06 +04:00
|
|
|
|
// .cancel() may not be immediate and handleResult() could be called
|
|
|
|
|
// after a .cancel().
|
|
|
|
|
for (
|
|
|
|
|
let row = resultSet.getNextRow();
|
|
|
|
|
row && !userCancelled;
|
|
|
|
|
row = resultSet.getNextRow()
|
|
|
|
|
) {
|
|
|
|
|
if (!onRow) {
|
|
|
|
|
rows.push(row);
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
2014-09-26 17:17:00 +04:00
|
|
|
|
handledRow = true;
|
|
|
|
|
|
2014-06-12 01:40:06 +04:00
|
|
|
|
try {
|
2017-08-19 16:10:44 +03:00
|
|
|
|
onRow(row, () => {
|
2015-12-03 20:58:56 +03:00
|
|
|
|
userCancelled = true;
|
|
|
|
|
pending.cancel();
|
2017-08-19 16:10:44 +03:00
|
|
|
|
});
|
|
|
|
|
} catch (e) {
|
2015-12-18 06:05:00 +03:00
|
|
|
|
self._log.warn("Exception when calling onRow callback", e);
|
2014-06-12 01:40:06 +04:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
|
2016-12-30 02:34:54 +03:00
|
|
|
|
handleError(error) {
|
2021-09-09 19:22:49 +03:00
|
|
|
|
self._log.warn(
|
2014-06-12 01:40:06 +04:00
|
|
|
|
"Error when executing SQL (" + error.result + "): " + error.message
|
|
|
|
|
);
|
|
|
|
|
errors.push(error);
|
|
|
|
|
},
|
|
|
|
|
|
2016-12-30 02:34:54 +03:00
|
|
|
|
handleCompletion(reason) {
|
2014-06-12 01:40:06 +04:00
|
|
|
|
self._log.debug("Stmt #" + index + " finished.");
|
|
|
|
|
self._pendingStatements.delete(index);
|
|
|
|
|
|
|
|
|
|
switch (reason) {
|
|
|
|
|
case Ci.mozIStorageStatementCallback.REASON_FINISHED:
|
2017-07-31 23:27:23 +03:00
|
|
|
|
case Ci.mozIStorageStatementCallback.REASON_CANCELED:
|
2014-09-26 17:17:00 +04:00
|
|
|
|
// If there is an onRow handler, we always instead resolve to a
|
|
|
|
|
// boolean indicating whether the onRow handler was called or not.
|
|
|
|
|
let result = onRow ? handledRow : rows;
|
2014-06-12 01:40:06 +04:00
|
|
|
|
deferred.resolve(result);
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
case Ci.mozIStorageStatementCallback.REASON_ERROR:
|
2015-12-03 20:58:56 +03:00
|
|
|
|
let error = new Error(
|
|
|
|
|
"Error(s) encountered during statement execution: " +
|
|
|
|
|
errors.map(e => e.message).join(", ")
|
|
|
|
|
);
|
2014-06-12 01:40:06 +04:00
|
|
|
|
error.errors = errors;
|
2020-02-11 20:16:58 +03:00
|
|
|
|
|
2021-11-01 12:34:18 +03:00
|
|
|
|
// Forward the error result.
|
|
|
|
|
// Corruption is the most critical one so it's handled apart.
|
|
|
|
|
if (errors.some(e => e.result == Ci.mozIStorageError.CORRUPT)) {
|
2020-02-11 20:16:58 +03:00
|
|
|
|
error.result = Cr.NS_ERROR_FILE_CORRUPTED;
|
2021-11-01 12:34:18 +03:00
|
|
|
|
} else {
|
|
|
|
|
// Just use the first error result in the other cases.
|
|
|
|
|
error.result = convertStorageErrorResult(errors[0]?.result);
|
2020-02-11 20:16:58 +03:00
|
|
|
|
}
|
|
|
|
|
|
2014-06-12 01:40:06 +04:00
|
|
|
|
deferred.reject(error);
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
default:
|
|
|
|
|
deferred.reject(
|
|
|
|
|
new Error("Unknown completion reason code: " + reason)
|
|
|
|
|
);
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
this._pendingStatements.set(index, pending);
|
|
|
|
|
return deferred.promise;
|
|
|
|
|
},
|
|
|
|
|
|
2016-12-30 02:34:54 +03:00
|
|
|
|
ensureOpen() {
|
2014-06-12 01:40:06 +04:00
|
|
|
|
if (!this._open) {
|
|
|
|
|
throw new Error("Connection is not open.");
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
|
2016-12-30 02:34:54 +03:00
|
|
|
|
_clearIdleShrinkTimer() {
|
2014-06-12 01:40:06 +04:00
|
|
|
|
if (!this._idleShrinkTimer) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
this._idleShrinkTimer.cancel();
|
|
|
|
|
},
|
|
|
|
|
|
2016-12-30 02:34:54 +03:00
|
|
|
|
_startIdleShrinkTimer() {
|
2014-06-12 01:40:06 +04:00
|
|
|
|
if (!this._idleShrinkTimer) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
this._idleShrinkTimer.initWithCallback(
|
|
|
|
|
this.shrinkMemory.bind(this),
|
|
|
|
|
this._idleShrinkMS,
|
|
|
|
|
this._idleShrinkTimer.TYPE_ONE_SHOT
|
|
|
|
|
);
|
2018-03-01 21:43:07 +03:00
|
|
|
|
},
|
|
|
|
|
|
2021-09-09 19:22:50 +03:00
|
|
|
|
/**
|
|
|
|
|
* Returns a promise that will resolve after a time comprised between 80% of
|
|
|
|
|
* `TRANSACTIONS_TIMEOUT_MS` and `TRANSACTIONS_TIMEOUT_MS`. Use
|
|
|
|
|
* this method instead of creating several individual timers that may survive
|
|
|
|
|
* longer than necessary.
|
|
|
|
|
*/
|
2018-03-01 21:43:07 +03:00
|
|
|
|
_getTimeoutPromise() {
|
|
|
|
|
if (this._timeoutPromise && Cu.now() <= this._timeoutPromiseExpires) {
|
|
|
|
|
return this._timeoutPromise;
|
|
|
|
|
}
|
|
|
|
|
let timeoutPromise = new Promise((resolve, reject) => {
|
|
|
|
|
setTimeout(() => {
|
|
|
|
|
// Clear out this._timeoutPromise if it hasn't changed since we set it.
|
|
|
|
|
if (this._timeoutPromise == timeoutPromise) {
|
|
|
|
|
this._timeoutPromise = null;
|
|
|
|
|
}
|
2021-09-09 19:22:50 +03:00
|
|
|
|
let e = new Error(
|
|
|
|
|
"Transaction timeout, most likely caused by unresolved pending work."
|
2018-03-01 21:43:07 +03:00
|
|
|
|
);
|
2021-09-09 19:22:50 +03:00
|
|
|
|
e.becauseTimedOut = true;
|
|
|
|
|
reject(e);
|
|
|
|
|
}, Sqlite.TRANSACTIONS_TIMEOUT_MS);
|
2018-03-01 21:43:07 +03:00
|
|
|
|
});
|
|
|
|
|
this._timeoutPromise = timeoutPromise;
|
|
|
|
|
this._timeoutPromiseExpires =
|
2021-09-09 19:22:50 +03:00
|
|
|
|
Cu.now() + Sqlite.TRANSACTIONS_TIMEOUT_MS * 0.2;
|
2018-03-01 21:43:07 +03:00
|
|
|
|
return this._timeoutPromise;
|
2014-06-12 01:40:06 +04:00
|
|
|
|
},
|
|
|
|
|
});
|
|
|
|
|
|
2013-01-05 03:36:59 +04:00
|
|
|
|
/**
|
|
|
|
|
* Opens a connection to a SQLite database.
|
|
|
|
|
*
|
|
|
|
|
* The following parameters can control the connection:
|
|
|
|
|
*
|
|
|
|
|
* path -- (string) The filesystem path of the database file to open. If the
|
|
|
|
|
* file does not exist, a new database will be created.
|
|
|
|
|
*
|
|
|
|
|
* sharedMemoryCache -- (bool) Whether multiple connections to the database
|
|
|
|
|
* share the same memory cache. Sharing the memory cache likely results
|
|
|
|
|
* in less memory utilization. However, sharing also requires connections
|
|
|
|
|
* to obtain a lock, possibly making database access slower. Defaults to
|
|
|
|
|
* true.
|
|
|
|
|
*
|
2013-01-25 01:30:20 +04:00
|
|
|
|
* shrinkMemoryOnConnectionIdleMS -- (integer) If defined, the connection
|
|
|
|
|
* will attempt to minimize its memory usage after this many
|
|
|
|
|
* milliseconds of connection idle. The connection is idle when no
|
|
|
|
|
* statements are executing. There is no default value which means no
|
|
|
|
|
* automatic memory minimization will occur. Please note that this is
|
|
|
|
|
* *not* a timer on the idle service and this could fire while the
|
|
|
|
|
* application is active.
|
2013-01-05 03:36:59 +04:00
|
|
|
|
*
|
2016-07-18 18:46:45 +03:00
|
|
|
|
* readOnly -- (bool) Whether to open the database with SQLITE_OPEN_READONLY
|
|
|
|
|
* set. If used, writing to the database will fail. Defaults to false.
|
|
|
|
|
*
|
|
|
|
|
* ignoreLockingMode -- (bool) Whether to ignore locks on the database held
|
|
|
|
|
* by other connections. If used, implies readOnly. Defaults to false.
|
|
|
|
|
* USE WITH EXTREME CAUTION. This mode WILL produce incorrect results or
|
|
|
|
|
* return "false positive" corruption errors if other connections write
|
|
|
|
|
* to the DB at the same time.
|
|
|
|
|
*
|
2013-01-05 03:36:59 +04:00
|
|
|
|
* FUTURE options to control:
|
|
|
|
|
*
|
|
|
|
|
* special named databases
|
|
|
|
|
* pragma TEMP STORE = MEMORY
|
|
|
|
|
* TRUNCATE JOURNAL
|
|
|
|
|
* SYNCHRONOUS = full
|
|
|
|
|
*
|
|
|
|
|
* @param options
|
|
|
|
|
* (Object) Parameters to control connection and open options.
|
|
|
|
|
*
|
|
|
|
|
* @return Promise<OpenedConnection>
|
|
|
|
|
*/
|
|
|
|
|
function openConnection(options) {
|
2022-06-07 07:29:50 +03:00
|
|
|
|
let log = lazy.Log.repository.getLoggerWithMessagePrefix(
|
2022-10-25 23:47:58 +03:00
|
|
|
|
"Sqlite.jsm",
|
2021-09-09 19:22:49 +03:00
|
|
|
|
`ConnectionOpener: `
|
|
|
|
|
);
|
|
|
|
|
log.manageLevelFromPref("toolkit.sqlitejsm.loglevel");
|
2013-01-05 03:36:59 +04:00
|
|
|
|
|
|
|
|
|
if (!options.path) {
|
|
|
|
|
throw new Error("path not specified in connection options.");
|
|
|
|
|
}
|
|
|
|
|
|
2014-05-13 09:00:00 +04:00
|
|
|
|
if (isClosed) {
|
|
|
|
|
throw new Error(
|
2022-10-25 23:47:58 +03:00
|
|
|
|
"Sqlite.jsm has been shutdown. Cannot open connection to: " + options.path
|
2014-05-13 09:00:00 +04:00
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
2013-01-05 03:36:59 +04:00
|
|
|
|
// Retains absolute paths and normalizes relative as relative to profile.
|
2021-01-08 19:08:42 +03:00
|
|
|
|
let path = options.path;
|
|
|
|
|
let file;
|
|
|
|
|
try {
|
2022-06-07 07:29:50 +03:00
|
|
|
|
file = lazy.FileUtils.File(path);
|
2021-01-08 19:08:42 +03:00
|
|
|
|
} catch (ex) {
|
|
|
|
|
// For relative paths, we will get an exception from trying to initialize
|
|
|
|
|
// the file. We must then join this path to the profile directory.
|
|
|
|
|
if (ex.result == Cr.NS_ERROR_FILE_UNRECOGNIZED_PATH) {
|
|
|
|
|
path = PathUtils.joinRelative(
|
|
|
|
|
Services.dirsvc.get("ProfD", Ci.nsIFile).path,
|
|
|
|
|
options.path
|
|
|
|
|
);
|
2022-06-07 07:29:50 +03:00
|
|
|
|
file = lazy.FileUtils.File(path);
|
2021-01-08 19:08:42 +03:00
|
|
|
|
} else {
|
|
|
|
|
throw ex;
|
|
|
|
|
}
|
|
|
|
|
}
|
2013-01-05 03:36:59 +04:00
|
|
|
|
|
|
|
|
|
let sharedMemoryCache =
|
|
|
|
|
"sharedMemoryCache" in options ? options.sharedMemoryCache : true;
|
|
|
|
|
|
2013-01-25 01:30:20 +04:00
|
|
|
|
let openedOptions = {};
|
|
|
|
|
|
|
|
|
|
if ("shrinkMemoryOnConnectionIdleMS" in options) {
|
|
|
|
|
if (!Number.isInteger(options.shrinkMemoryOnConnectionIdleMS)) {
|
|
|
|
|
throw new Error(
|
|
|
|
|
"shrinkMemoryOnConnectionIdleMS must be an integer. " +
|
|
|
|
|
"Got: " +
|
|
|
|
|
options.shrinkMemoryOnConnectionIdleMS
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
openedOptions.shrinkMemoryOnConnectionIdleMS =
|
|
|
|
|
options.shrinkMemoryOnConnectionIdleMS;
|
|
|
|
|
}
|
|
|
|
|
|
2018-03-01 09:44:40 +03:00
|
|
|
|
if ("defaultTransactionType" in options) {
|
|
|
|
|
let defaultTransactionType = options.defaultTransactionType;
|
|
|
|
|
if (!OpenedConnection.TRANSACTION_TYPES.includes(defaultTransactionType)) {
|
|
|
|
|
throw new Error(
|
|
|
|
|
"Unknown default transaction type: " + defaultTransactionType
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
openedOptions.defaultTransactionType = defaultTransactionType;
|
|
|
|
|
}
|
|
|
|
|
|
2021-01-08 19:08:42 +03:00
|
|
|
|
let identifier = getIdentifierByFileName(PathUtils.filename(path));
|
2013-01-05 03:36:59 +04:00
|
|
|
|
|
2021-09-09 19:22:49 +03:00
|
|
|
|
log.debug("Opening database: " + path + " (" + identifier + ")");
|
2014-12-11 17:50:51 +03:00
|
|
|
|
|
|
|
|
|
return new Promise((resolve, reject) => {
|
2022-02-17 09:22:27 +03:00
|
|
|
|
let dbOpenOptions = Ci.mozIStorageService.OPEN_DEFAULT;
|
|
|
|
|
if (sharedMemoryCache) {
|
|
|
|
|
dbOpenOptions |= Ci.mozIStorageService.OPEN_SHARED;
|
2013-06-27 17:01:00 +04:00
|
|
|
|
}
|
2016-07-18 18:46:45 +03:00
|
|
|
|
if (options.readOnly) {
|
2022-02-17 09:22:27 +03:00
|
|
|
|
dbOpenOptions |= Ci.mozIStorageService.OPEN_READONLY;
|
2016-07-18 18:46:45 +03:00
|
|
|
|
}
|
|
|
|
|
if (options.ignoreLockingMode) {
|
2022-02-17 09:22:27 +03:00
|
|
|
|
dbOpenOptions |= Ci.mozIStorageService.OPEN_IGNORE_LOCKING_MODE;
|
|
|
|
|
dbOpenOptions |= Ci.mozIStorageService.OPEN_READONLY;
|
2016-07-18 18:46:45 +03:00
|
|
|
|
}
|
|
|
|
|
|
2022-02-17 09:22:27 +03:00
|
|
|
|
let dbConnectionOptions = Ci.mozIStorageService.CONNECTION_DEFAULT;
|
2016-07-18 18:46:45 +03:00
|
|
|
|
|
2014-12-11 17:50:51 +03:00
|
|
|
|
Services.storage.openAsyncDatabase(
|
|
|
|
|
file,
|
2022-02-17 09:22:27 +03:00
|
|
|
|
dbOpenOptions,
|
|
|
|
|
dbConnectionOptions,
|
2014-12-11 17:50:51 +03:00
|
|
|
|
(status, connection) => {
|
|
|
|
|
if (!connection) {
|
2021-09-09 19:22:49 +03:00
|
|
|
|
log.error(`Could not open connection to ${path}: ${status}`);
|
2019-01-11 14:58:24 +03:00
|
|
|
|
let error = new Components.Exception(
|
|
|
|
|
`Could not open connection to ${path}: ${status}`,
|
|
|
|
|
status
|
|
|
|
|
);
|
2018-01-24 10:11:54 +03:00
|
|
|
|
reject(error);
|
2014-12-11 17:50:51 +03:00
|
|
|
|
return;
|
|
|
|
|
}
|
2021-09-09 19:22:49 +03:00
|
|
|
|
log.debug("Connection opened");
|
2014-12-11 17:50:51 +03:00
|
|
|
|
try {
|
|
|
|
|
resolve(
|
|
|
|
|
new OpenedConnection(
|
|
|
|
|
connection.QueryInterface(Ci.mozIStorageAsyncConnection),
|
2016-06-29 15:47:36 +03:00
|
|
|
|
identifier,
|
|
|
|
|
openedOptions
|
2019-07-05 12:15:43 +03:00
|
|
|
|
)
|
2016-06-29 15:47:36 +03:00
|
|
|
|
);
|
2014-12-11 17:50:51 +03:00
|
|
|
|
} catch (ex) {
|
2021-09-09 19:22:49 +03:00
|
|
|
|
log.error("Could not open database", ex);
|
2016-06-29 15:47:36 +03:00
|
|
|
|
connection.asyncClose();
|
2014-12-11 17:50:51 +03:00
|
|
|
|
reject(ex);
|
2019-07-05 12:15:43 +03:00
|
|
|
|
}
|
2014-12-11 17:50:51 +03:00
|
|
|
|
}
|
|
|
|
|
);
|
2013-06-27 17:01:00 +04:00
|
|
|
|
});
|
2013-01-05 03:36:59 +04:00
|
|
|
|
}
|
|
|
|
|
|
2014-04-08 16:09:26 +04:00
|
|
|
|
/**
|
|
|
|
|
* Creates a clone of an existing and open Storage connection. The clone has
|
|
|
|
|
* the same underlying characteristics of the original connection and is
|
2014-09-22 20:32:43 +04:00
|
|
|
|
* returned in form of an OpenedConnection handle.
|
2014-04-08 16:09:26 +04:00
|
|
|
|
*
|
|
|
|
|
* The following parameters can control the cloned connection:
|
|
|
|
|
*
|
|
|
|
|
* connection -- (mozIStorageAsyncConnection) The original Storage connection
|
|
|
|
|
* to clone. It's not possible to clone connections to memory databases.
|
|
|
|
|
*
|
|
|
|
|
* readOnly -- (boolean) - If true the clone will be read-only. If the
|
|
|
|
|
* original connection is already read-only, the clone will be, regardless
|
|
|
|
|
* of this option. If the original connection is using the shared cache,
|
|
|
|
|
* this parameter will be ignored and the clone will be as privileged as
|
|
|
|
|
* the original connection.
|
|
|
|
|
* shrinkMemoryOnConnectionIdleMS -- (integer) If defined, the connection
|
|
|
|
|
* will attempt to minimize its memory usage after this many
|
|
|
|
|
* milliseconds of connection idle. The connection is idle when no
|
|
|
|
|
* statements are executing. There is no default value which means no
|
|
|
|
|
* automatic memory minimization will occur. Please note that this is
|
|
|
|
|
* *not* a timer on the idle service and this could fire while the
|
|
|
|
|
* application is active.
|
|
|
|
|
*
|
|
|
|
|
*
|
|
|
|
|
* @param options
|
|
|
|
|
* (Object) Parameters to control connection and clone options.
|
|
|
|
|
*
|
|
|
|
|
* @return Promise<OpenedConnection>
|
|
|
|
|
*/
|
|
|
|
|
function cloneStorageConnection(options) {
|
2022-06-07 07:29:50 +03:00
|
|
|
|
let log = lazy.Log.repository.getLoggerWithMessagePrefix(
|
2022-10-25 23:47:58 +03:00
|
|
|
|
"Sqlite.jsm",
|
2021-09-09 19:22:49 +03:00
|
|
|
|
`ConnectionCloner: `
|
|
|
|
|
);
|
|
|
|
|
log.manageLevelFromPref("toolkit.sqlitejsm.loglevel");
|
2014-04-08 16:09:26 +04:00
|
|
|
|
|
|
|
|
|
let source = options && options.connection;
|
|
|
|
|
if (!source) {
|
|
|
|
|
throw new TypeError("connection not specified in clone options.");
|
|
|
|
|
}
|
2016-12-13 00:50:10 +03:00
|
|
|
|
if (!(source instanceof Ci.mozIStorageAsyncConnection)) {
|
2014-05-13 09:00:00 +04:00
|
|
|
|
throw new TypeError("Connection must be a valid Storage connection.");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (isClosed) {
|
Bug 1482608 - Add basic Rust bindings for mozStorage. r=nika,asuth,mak
This commit wraps just enough of the mozStorage API to support the
bookmarks mirror. It's not complete: for example, there's no way
to open, clone, or close a connection, because the mirror handles
that from JS. The wrapper also omits shutdown blocking and retrying on
`SQLITE_BUSY`.
This commit also changes the behavior of sync and async mozStorage
connections. Async (`mozIStorageAsyncConnection`) methods may be called
from any thread on any connection. Sync (`mozIStorageConnection`)
methods may be called from any thread on a sync connection, and from
background threads on an async connection. All connections now QI
to `mozIStorageConnection`, but attempting to call a sync method on
an async connection from the main thread throws.
Finally, this commit exposes an `OpenedConnection::unsafeRawConnection`
getter in Sqlite.jsm, for JS code to access the underlying connection.
Differential Revision: https://phabricator.services.mozilla.com/D20073
--HG--
extra : moz-landing-system : lando
2019-03-25 07:49:18 +03:00
|
|
|
|
throw new Error(
|
2022-10-25 23:47:58 +03:00
|
|
|
|
"Sqlite.jsm has been shutdown. Cannot clone connection to: " +
|
Bug 1482608 - Add basic Rust bindings for mozStorage. r=nika,asuth,mak
This commit wraps just enough of the mozStorage API to support the
bookmarks mirror. It's not complete: for example, there's no way
to open, clone, or close a connection, because the mirror handles
that from JS. The wrapper also omits shutdown blocking and retrying on
`SQLITE_BUSY`.
This commit also changes the behavior of sync and async mozStorage
connections. Async (`mozIStorageAsyncConnection`) methods may be called
from any thread on any connection. Sync (`mozIStorageConnection`)
methods may be called from any thread on a sync connection, and from
background threads on an async connection. All connections now QI
to `mozIStorageConnection`, but attempting to call a sync method on
an async connection from the main thread throws.
Finally, this commit exposes an `OpenedConnection::unsafeRawConnection`
getter in Sqlite.jsm, for JS code to access the underlying connection.
Differential Revision: https://phabricator.services.mozilla.com/D20073
--HG--
extra : moz-landing-system : lando
2019-03-25 07:49:18 +03:00
|
|
|
|
source.databaseFile.path
|
|
|
|
|
);
|
2014-04-08 16:09:26 +04:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
let openedOptions = {};
|
|
|
|
|
|
|
|
|
|
if ("shrinkMemoryOnConnectionIdleMS" in options) {
|
|
|
|
|
if (!Number.isInteger(options.shrinkMemoryOnConnectionIdleMS)) {
|
|
|
|
|
throw new TypeError(
|
|
|
|
|
"shrinkMemoryOnConnectionIdleMS must be an integer. " +
|
|
|
|
|
"Got: " +
|
|
|
|
|
options.shrinkMemoryOnConnectionIdleMS
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
openedOptions.shrinkMemoryOnConnectionIdleMS =
|
|
|
|
|
options.shrinkMemoryOnConnectionIdleMS;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
let path = source.databaseFile.path;
|
2021-01-08 19:08:42 +03:00
|
|
|
|
let identifier = getIdentifierByFileName(PathUtils.filename(path));
|
2014-04-08 16:09:26 +04:00
|
|
|
|
|
2021-09-09 19:22:49 +03:00
|
|
|
|
log.debug("Cloning database: " + path + " (" + identifier + ")");
|
2014-04-08 16:09:26 +04:00
|
|
|
|
|
2014-12-11 17:50:51 +03:00
|
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
|
source.asyncClone(!!options.readOnly, (status, connection) => {
|
|
|
|
|
if (!connection) {
|
2021-09-09 19:22:49 +03:00
|
|
|
|
log.error("Could not clone connection: " + status);
|
2014-12-11 17:50:51 +03:00
|
|
|
|
reject(new Error("Could not clone connection: " + status));
|
2020-11-01 23:37:26 +03:00
|
|
|
|
return;
|
2014-12-11 17:50:51 +03:00
|
|
|
|
}
|
2021-09-09 19:22:49 +03:00
|
|
|
|
log.debug("Connection cloned");
|
2014-12-11 17:50:51 +03:00
|
|
|
|
try {
|
|
|
|
|
let conn = connection.QueryInterface(Ci.mozIStorageAsyncConnection);
|
|
|
|
|
resolve(new OpenedConnection(conn, identifier, openedOptions));
|
|
|
|
|
} catch (ex) {
|
2021-09-09 19:22:49 +03:00
|
|
|
|
log.error("Could not clone database", ex);
|
2016-06-29 15:47:36 +03:00
|
|
|
|
connection.asyncClose();
|
2014-12-11 17:50:51 +03:00
|
|
|
|
reject(ex);
|
|
|
|
|
}
|
|
|
|
|
});
|
2014-04-08 16:09:26 +04:00
|
|
|
|
});
|
|
|
|
|
}
|
2013-01-05 03:36:59 +04:00
|
|
|
|
|
2014-09-22 20:32:43 +04:00
|
|
|
|
/**
|
2022-10-25 23:47:58 +03:00
|
|
|
|
* Wraps an existing and open Storage connection with Sqlite.jsm API. The
|
2014-09-22 20:32:43 +04:00
|
|
|
|
* wrapped connection clone has the same underlying characteristics of the
|
|
|
|
|
* original connection and is returned in form of an OpenedConnection handle.
|
|
|
|
|
*
|
2022-10-25 23:47:58 +03:00
|
|
|
|
* Clients are responsible for closing both the Sqlite.jsm wrapper and the
|
2014-09-22 20:32:43 +04:00
|
|
|
|
* underlying mozStorage connection.
|
|
|
|
|
*
|
|
|
|
|
* The following parameters can control the wrapped connection:
|
|
|
|
|
*
|
|
|
|
|
* connection -- (mozIStorageAsyncConnection) The original Storage connection
|
|
|
|
|
* to wrap.
|
|
|
|
|
*
|
|
|
|
|
* @param options
|
|
|
|
|
* (Object) Parameters to control connection and wrap options.
|
|
|
|
|
*
|
|
|
|
|
* @return Promise<OpenedConnection>
|
|
|
|
|
*/
|
|
|
|
|
function wrapStorageConnection(options) {
|
2022-06-07 07:29:50 +03:00
|
|
|
|
let log = lazy.Log.repository.getLoggerWithMessagePrefix(
|
2022-10-25 23:47:58 +03:00
|
|
|
|
"Sqlite.jsm",
|
2021-09-09 19:22:49 +03:00
|
|
|
|
`ConnectionCloner: `
|
|
|
|
|
);
|
|
|
|
|
log.manageLevelFromPref("toolkit.sqlitejsm.loglevel");
|
2014-09-22 20:32:43 +04:00
|
|
|
|
|
|
|
|
|
let connection = options && options.connection;
|
|
|
|
|
if (!connection || !(connection instanceof Ci.mozIStorageAsyncConnection)) {
|
|
|
|
|
throw new TypeError("connection not specified or invalid.");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (isClosed) {
|
Bug 1482608 - Add basic Rust bindings for mozStorage. r=nika,asuth,mak
This commit wraps just enough of the mozStorage API to support the
bookmarks mirror. It's not complete: for example, there's no way
to open, clone, or close a connection, because the mirror handles
that from JS. The wrapper also omits shutdown blocking and retrying on
`SQLITE_BUSY`.
This commit also changes the behavior of sync and async mozStorage
connections. Async (`mozIStorageAsyncConnection`) methods may be called
from any thread on any connection. Sync (`mozIStorageConnection`)
methods may be called from any thread on a sync connection, and from
background threads on an async connection. All connections now QI
to `mozIStorageConnection`, but attempting to call a sync method on
an async connection from the main thread throws.
Finally, this commit exposes an `OpenedConnection::unsafeRawConnection`
getter in Sqlite.jsm, for JS code to access the underlying connection.
Differential Revision: https://phabricator.services.mozilla.com/D20073
--HG--
extra : moz-landing-system : lando
2019-03-25 07:49:18 +03:00
|
|
|
|
throw new Error(
|
2022-10-25 23:47:58 +03:00
|
|
|
|
"Sqlite.jsm has been shutdown. Cannot wrap connection to: " +
|
Bug 1482608 - Add basic Rust bindings for mozStorage. r=nika,asuth,mak
This commit wraps just enough of the mozStorage API to support the
bookmarks mirror. It's not complete: for example, there's no way
to open, clone, or close a connection, because the mirror handles
that from JS. The wrapper also omits shutdown blocking and retrying on
`SQLITE_BUSY`.
This commit also changes the behavior of sync and async mozStorage
connections. Async (`mozIStorageAsyncConnection`) methods may be called
from any thread on any connection. Sync (`mozIStorageConnection`)
methods may be called from any thread on a sync connection, and from
background threads on an async connection. All connections now QI
to `mozIStorageConnection`, but attempting to call a sync method on
an async connection from the main thread throws.
Finally, this commit exposes an `OpenedConnection::unsafeRawConnection`
getter in Sqlite.jsm, for JS code to access the underlying connection.
Differential Revision: https://phabricator.services.mozilla.com/D20073
--HG--
extra : moz-landing-system : lando
2019-03-25 07:49:18 +03:00
|
|
|
|
connection.databaseFile.path
|
|
|
|
|
);
|
2014-09-22 20:32:43 +04:00
|
|
|
|
}
|
|
|
|
|
|
2017-05-17 16:51:02 +03:00
|
|
|
|
let identifier = getIdentifierByFileName(connection.databaseFile.leafName);
|
2014-09-22 20:32:43 +04:00
|
|
|
|
|
2021-09-09 19:22:49 +03:00
|
|
|
|
log.debug("Wrapping database: " + identifier);
|
2014-09-22 20:32:43 +04:00
|
|
|
|
return new Promise(resolve => {
|
|
|
|
|
try {
|
|
|
|
|
let conn = connection.QueryInterface(Ci.mozIStorageAsyncConnection);
|
|
|
|
|
let wrapper = new OpenedConnection(conn, identifier);
|
|
|
|
|
// We must not handle shutdown of a wrapped connection, since that is
|
|
|
|
|
// already handled by the opener.
|
|
|
|
|
wrappedConnections.add(identifier);
|
|
|
|
|
resolve(wrapper);
|
|
|
|
|
} catch (ex) {
|
2021-09-09 19:22:49 +03:00
|
|
|
|
log.error("Could not wrap database", ex);
|
2014-09-22 20:32:43 +04:00
|
|
|
|
throw ex;
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2013-01-05 03:36:59 +04:00
|
|
|
|
/**
|
|
|
|
|
* Handle on an opened SQLite database.
|
|
|
|
|
*
|
|
|
|
|
* This is essentially a glorified wrapper around mozIStorageConnection.
|
|
|
|
|
* However, it offers some compelling advantages.
|
|
|
|
|
*
|
|
|
|
|
* The main functions on this type are `execute` and `executeCached`. These are
|
|
|
|
|
* ultimately how all SQL statements are executed. It's worth explaining their
|
|
|
|
|
* differences.
|
|
|
|
|
*
|
|
|
|
|
* `execute` is used to execute one-shot SQL statements. These are SQL
|
|
|
|
|
* statements that are executed one time and then thrown away. They are useful
|
|
|
|
|
* for dynamically generated SQL statements and clients who don't care about
|
|
|
|
|
* performance (either their own or wasting resources in the overall
|
|
|
|
|
* application). Because of the performance considerations, it is recommended
|
|
|
|
|
* to avoid `execute` unless the statement you are executing will only be
|
|
|
|
|
* executed once or seldomly.
|
|
|
|
|
*
|
|
|
|
|
* `executeCached` is used to execute a statement that will presumably be
|
|
|
|
|
* executed multiple times. The statement is parsed once and stuffed away
|
|
|
|
|
* inside the connection instance. Subsequent calls to `executeCached` will not
|
|
|
|
|
* incur the overhead of creating a new statement object. This should be used
|
|
|
|
|
* in preference to `execute` when a specific SQL statement will be executed
|
|
|
|
|
* multiple times.
|
|
|
|
|
*
|
|
|
|
|
* Instances of this type are not meant to be created outside of this file.
|
|
|
|
|
* Instead, first open an instance of `UnopenedSqliteConnection` and obtain
|
|
|
|
|
* an instance of this type by calling `open`.
|
|
|
|
|
*
|
|
|
|
|
* FUTURE IMPROVEMENTS
|
|
|
|
|
*
|
|
|
|
|
* Ability to enqueue operations. Currently there can be race conditions,
|
|
|
|
|
* especially as far as transactions are concerned. It would be nice to have
|
|
|
|
|
* an enqueueOperation(func) API that serially executes passed functions.
|
|
|
|
|
*
|
|
|
|
|
* Support for SAVEPOINT (named/nested transactions) might be useful.
|
|
|
|
|
*
|
|
|
|
|
* @param connection
|
|
|
|
|
* (mozIStorageConnection) Underlying SQLite connection.
|
2014-09-22 20:32:43 +04:00
|
|
|
|
* @param identifier
|
|
|
|
|
* (string) The unique identifier of this database. It may be used for
|
|
|
|
|
* logging or as a key in Maps.
|
|
|
|
|
* @param options [optional]
|
2013-01-25 01:30:20 +04:00
|
|
|
|
* (object) Options to control behavior of connection. See
|
|
|
|
|
* `openConnection`.
|
2013-01-05 03:36:59 +04:00
|
|
|
|
*/
|
2016-11-11 01:48:04 +03:00
|
|
|
|
function OpenedConnection(connection, identifier, options = {}) {
|
2014-06-12 01:40:06 +04:00
|
|
|
|
// Store all connection data in a field distinct from the
|
|
|
|
|
// witness. This enables us to store an additional reference to this
|
|
|
|
|
// field without preventing garbage collection of
|
|
|
|
|
// OpenedConnection. On garbage collection, we will still be able to
|
|
|
|
|
// close the database using this extra reference.
|
2014-09-22 20:32:43 +04:00
|
|
|
|
this._connectionData = new ConnectionData(connection, identifier, options);
|
2014-06-12 01:40:17 +04:00
|
|
|
|
|
|
|
|
|
// Store the extra reference in a map with connection identifier as
|
|
|
|
|
// key.
|
2014-09-22 20:32:43 +04:00
|
|
|
|
ConnectionData.byId.set(
|
|
|
|
|
this._connectionData._identifier,
|
2014-06-12 01:40:17 +04:00
|
|
|
|
this._connectionData
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
// Make a finalization witness. If this object is garbage collected
|
|
|
|
|
// before its `forget` method has been called, an event with topic
|
|
|
|
|
// "sqlite-finalization-witness" is broadcasted along with the
|
|
|
|
|
// connection identifier string of the database.
|
2022-06-07 07:29:50 +03:00
|
|
|
|
this._witness = lazy.FinalizationWitnessService.make(
|
2014-06-12 01:40:17 +04:00
|
|
|
|
"sqlite-finalization-witness",
|
2014-09-22 20:32:43 +04:00
|
|
|
|
this._connectionData._identifier
|
|
|
|
|
);
|
2013-01-05 03:36:59 +04:00
|
|
|
|
}
|
|
|
|
|
|
2018-03-01 09:44:40 +03:00
|
|
|
|
OpenedConnection.TRANSACTION_TYPES = ["DEFERRED", "IMMEDIATE", "EXCLUSIVE"];
|
|
|
|
|
|
|
|
|
|
// Converts a `mozIStorageAsyncConnection::TRANSACTION_*` constant into the
|
|
|
|
|
// corresponding `OpenedConnection.TRANSACTION_TYPES` constant.
|
|
|
|
|
function convertStorageTransactionType(type) {
|
|
|
|
|
if (!(type in OpenedConnection.TRANSACTION_TYPES)) {
|
|
|
|
|
throw new Error("Unknown storage transaction type: " + type);
|
|
|
|
|
}
|
|
|
|
|
return OpenedConnection.TRANSACTION_TYPES[type];
|
|
|
|
|
}
|
|
|
|
|
|
2013-01-05 03:36:59 +04:00
|
|
|
|
OpenedConnection.prototype = Object.freeze({
|
2018-03-01 09:44:40 +03:00
|
|
|
|
TRANSACTION_DEFAULT: "DEFAULT",
|
2013-01-18 22:11:22 +04:00
|
|
|
|
TRANSACTION_DEFERRED: "DEFERRED",
|
|
|
|
|
TRANSACTION_IMMEDIATE: "IMMEDIATE",
|
|
|
|
|
TRANSACTION_EXCLUSIVE: "EXCLUSIVE",
|
|
|
|
|
|
Bug 1482608 - Add basic Rust bindings for mozStorage. r=nika,asuth,mak
This commit wraps just enough of the mozStorage API to support the
bookmarks mirror. It's not complete: for example, there's no way
to open, clone, or close a connection, because the mirror handles
that from JS. The wrapper also omits shutdown blocking and retrying on
`SQLITE_BUSY`.
This commit also changes the behavior of sync and async mozStorage
connections. Async (`mozIStorageAsyncConnection`) methods may be called
from any thread on any connection. Sync (`mozIStorageConnection`)
methods may be called from any thread on a sync connection, and from
background threads on an async connection. All connections now QI
to `mozIStorageConnection`, but attempting to call a sync method on
an async connection from the main thread throws.
Finally, this commit exposes an `OpenedConnection::unsafeRawConnection`
getter in Sqlite.jsm, for JS code to access the underlying connection.
Differential Revision: https://phabricator.services.mozilla.com/D20073
--HG--
extra : moz-landing-system : lando
2019-03-25 07:49:18 +03:00
|
|
|
|
/**
|
|
|
|
|
* Returns a handle to the underlying `mozIStorageAsyncConnection`. This is
|
2022-10-25 23:47:58 +03:00
|
|
|
|
* ⚠️ **extremely unsafe** ⚠️ because `Sqlite.jsm` continues to manage the
|
Bug 1482608 - Add basic Rust bindings for mozStorage. r=nika,asuth,mak
This commit wraps just enough of the mozStorage API to support the
bookmarks mirror. It's not complete: for example, there's no way
to open, clone, or close a connection, because the mirror handles
that from JS. The wrapper also omits shutdown blocking and retrying on
`SQLITE_BUSY`.
This commit also changes the behavior of sync and async mozStorage
connections. Async (`mozIStorageAsyncConnection`) methods may be called
from any thread on any connection. Sync (`mozIStorageConnection`)
methods may be called from any thread on a sync connection, and from
background threads on an async connection. All connections now QI
to `mozIStorageConnection`, but attempting to call a sync method on
an async connection from the main thread throws.
Finally, this commit exposes an `OpenedConnection::unsafeRawConnection`
getter in Sqlite.jsm, for JS code to access the underlying connection.
Differential Revision: https://phabricator.services.mozilla.com/D20073
--HG--
extra : moz-landing-system : lando
2019-03-25 07:49:18 +03:00
|
|
|
|
* connection's lifecycle, including transactions and shutdown blockers.
|
|
|
|
|
* Misusing the raw connection can easily lead to data loss, memory leaks,
|
|
|
|
|
* and errors.
|
|
|
|
|
*
|
|
|
|
|
* Consumers of the raw connection **must not** close or re-wrap it,
|
2022-10-25 23:47:58 +03:00
|
|
|
|
* and should not run statements concurrently with `Sqlite.jsm`.
|
Bug 1482608 - Add basic Rust bindings for mozStorage. r=nika,asuth,mak
This commit wraps just enough of the mozStorage API to support the
bookmarks mirror. It's not complete: for example, there's no way
to open, clone, or close a connection, because the mirror handles
that from JS. The wrapper also omits shutdown blocking and retrying on
`SQLITE_BUSY`.
This commit also changes the behavior of sync and async mozStorage
connections. Async (`mozIStorageAsyncConnection`) methods may be called
from any thread on any connection. Sync (`mozIStorageConnection`)
methods may be called from any thread on a sync connection, and from
background threads on an async connection. All connections now QI
to `mozIStorageConnection`, but attempting to call a sync method on
an async connection from the main thread throws.
Finally, this commit exposes an `OpenedConnection::unsafeRawConnection`
getter in Sqlite.jsm, for JS code to access the underlying connection.
Differential Revision: https://phabricator.services.mozilla.com/D20073
--HG--
extra : moz-landing-system : lando
2019-03-25 07:49:18 +03:00
|
|
|
|
*
|
|
|
|
|
* It's _much_ safer to open a `mozIStorage{Async}Connection` yourself,
|
|
|
|
|
* and access it from JavaScript via `Sqlite.wrapStorageConnection`.
|
|
|
|
|
* `unsafeRawConnection` is an escape hatch for cases where you can't
|
|
|
|
|
* do that.
|
|
|
|
|
*
|
|
|
|
|
* Please do _not_ add new uses of `unsafeRawConnection` without review
|
|
|
|
|
* from a storage peer.
|
|
|
|
|
*/
|
|
|
|
|
get unsafeRawConnection() {
|
|
|
|
|
return this._connectionData._dbConn;
|
|
|
|
|
},
|
|
|
|
|
|
2019-10-16 00:22:57 +03:00
|
|
|
|
/**
|
|
|
|
|
* Returns the maximum number of bound parameters for statements executed
|
|
|
|
|
* on this connection.
|
|
|
|
|
*
|
|
|
|
|
* @type {number}
|
|
|
|
|
*/
|
|
|
|
|
get variableLimit() {
|
|
|
|
|
return this.unsafeRawConnection.variableLimit;
|
|
|
|
|
},
|
|
|
|
|
|
2013-01-05 03:36:59 +04:00
|
|
|
|
/**
|
|
|
|
|
* The integer schema version of the database.
|
|
|
|
|
*
|
|
|
|
|
* This is 0 if not schema version has been set.
|
2013-06-27 17:01:00 +04:00
|
|
|
|
*
|
|
|
|
|
* @return Promise<int>
|
2013-01-05 03:36:59 +04:00
|
|
|
|
*/
|
2018-01-17 20:13:47 +03:00
|
|
|
|
getSchemaVersion(schemaName = "main") {
|
2020-08-06 18:07:25 +03:00
|
|
|
|
return this.execute(`PRAGMA ${schemaName}.user_version`).then(result =>
|
|
|
|
|
result[0].getInt32(0)
|
2013-06-27 17:01:00 +04:00
|
|
|
|
);
|
2013-01-05 03:36:59 +04:00
|
|
|
|
},
|
|
|
|
|
|
2018-01-17 20:13:47 +03:00
|
|
|
|
setSchemaVersion(value, schemaName = "main") {
|
2013-06-27 17:01:00 +04:00
|
|
|
|
if (!Number.isInteger(value)) {
|
|
|
|
|
// Guarding against accidental SQLi
|
|
|
|
|
throw new TypeError("Schema version must be an integer. Got " + value);
|
|
|
|
|
}
|
2014-06-12 01:40:06 +04:00
|
|
|
|
this._connectionData.ensureOpen();
|
2018-01-17 20:13:47 +03:00
|
|
|
|
return this.execute(`PRAGMA ${schemaName}.user_version = ${value}`);
|
2013-01-05 03:36:59 +04:00
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Close the database connection.
|
|
|
|
|
*
|
|
|
|
|
* This must be performed when you are finished with the database.
|
|
|
|
|
*
|
|
|
|
|
* Closing the database connection has the side effect of forcefully
|
|
|
|
|
* cancelling all active statements. Therefore, callers should ensure that
|
|
|
|
|
* all active statements have completed before closing the connection, if
|
|
|
|
|
* possible.
|
|
|
|
|
*
|
|
|
|
|
* The returned promise will be resolved once the connection is closed.
|
2014-05-13 09:00:00 +04:00
|
|
|
|
* Successive calls to close() return the same promise.
|
2013-01-05 03:36:59 +04:00
|
|
|
|
*
|
|
|
|
|
* IMPROVEMENT: Resolve the promise to a closed connection which can be
|
|
|
|
|
* reopened.
|
|
|
|
|
*
|
|
|
|
|
* @return Promise<>
|
|
|
|
|
*/
|
2016-12-30 02:34:54 +03:00
|
|
|
|
close() {
|
2014-06-12 01:40:17 +04:00
|
|
|
|
// Unless cleanup has already been done by a previous call to
|
|
|
|
|
// `close`, delete the database entry from map and tell the
|
|
|
|
|
// finalization witness to forget.
|
2014-09-22 20:32:43 +04:00
|
|
|
|
if (ConnectionData.byId.has(this._connectionData._identifier)) {
|
|
|
|
|
ConnectionData.byId.delete(this._connectionData._identifier);
|
2014-06-12 01:40:17 +04:00
|
|
|
|
this._witness.forget();
|
|
|
|
|
}
|
2014-06-12 01:40:06 +04:00
|
|
|
|
return this._connectionData.close();
|
2013-01-18 22:11:22 +04:00
|
|
|
|
},
|
|
|
|
|
|
2014-04-08 16:09:26 +04:00
|
|
|
|
/**
|
|
|
|
|
* Clones this connection to a new Sqlite one.
|
|
|
|
|
*
|
|
|
|
|
* The following parameters can control the cloned connection:
|
|
|
|
|
*
|
|
|
|
|
* @param readOnly
|
|
|
|
|
* (boolean) - If true the clone will be read-only. If the original
|
|
|
|
|
* connection is already read-only, the clone will be, regardless of
|
|
|
|
|
* this option. If the original connection is using the shared cache,
|
|
|
|
|
* this parameter will be ignored and the clone will be as privileged as
|
|
|
|
|
* the original connection.
|
|
|
|
|
*
|
|
|
|
|
* @return Promise<OpenedConnection>
|
|
|
|
|
*/
|
2016-12-30 02:34:54 +03:00
|
|
|
|
clone(readOnly = false) {
|
2014-06-12 01:40:06 +04:00
|
|
|
|
return this._connectionData.clone(readOnly);
|
2013-01-05 03:36:59 +04:00
|
|
|
|
},
|
|
|
|
|
|
2016-12-30 02:34:54 +03:00
|
|
|
|
executeBeforeShutdown(name, task) {
|
2015-05-05 13:45:25 +03:00
|
|
|
|
return this._connectionData.executeBeforeShutdown(this, name, task);
|
|
|
|
|
},
|
|
|
|
|
|
2013-01-05 03:36:59 +04:00
|
|
|
|
/**
|
|
|
|
|
* Execute a SQL statement and cache the underlying statement object.
|
|
|
|
|
*
|
|
|
|
|
* This function executes a SQL statement and also caches the underlying
|
|
|
|
|
* derived statement object so subsequent executions are faster and use
|
|
|
|
|
* less resources.
|
|
|
|
|
*
|
|
|
|
|
* This function optionally binds parameters to the statement as well as
|
|
|
|
|
* optionally invokes a callback for every row retrieved.
|
|
|
|
|
*
|
|
|
|
|
* By default, no parameters are bound and no callback will be invoked for
|
|
|
|
|
* every row.
|
|
|
|
|
*
|
|
|
|
|
* Bound parameters can be defined as an Array of positional arguments or
|
|
|
|
|
* an object mapping named parameters to their values. If there are no bound
|
|
|
|
|
* parameters, the caller can pass nothing or null for this argument.
|
|
|
|
|
*
|
|
|
|
|
* Callers are encouraged to pass objects rather than Arrays for bound
|
|
|
|
|
* parameters because they prevent foot guns. With positional arguments, it
|
|
|
|
|
* is simple to modify the parameter count or positions without fixing all
|
|
|
|
|
* users of the statement. Objects/named parameters are a little safer
|
|
|
|
|
* because changes in order alone won't result in bad things happening.
|
|
|
|
|
*
|
|
|
|
|
* When `onRow` is not specified, all returned rows are buffered before the
|
|
|
|
|
* returned promise is resolved. For INSERT or UPDATE statements, this has
|
|
|
|
|
* no effect because no rows are returned from these. However, it has
|
|
|
|
|
* implications for SELECT statements.
|
|
|
|
|
*
|
|
|
|
|
* If your SELECT statement could return many rows or rows with large amounts
|
|
|
|
|
* of data, for performance reasons it is recommended to pass an `onRow`
|
|
|
|
|
* handler. Otherwise, the buffering may consume unacceptable amounts of
|
|
|
|
|
* resources.
|
|
|
|
|
*
|
2017-08-19 16:10:44 +03:00
|
|
|
|
* If the second parameter of an `onRow` handler is called during execution
|
|
|
|
|
* of the `onRow` handler, the execution of the statement is immediately
|
|
|
|
|
* cancelled. Subsequent rows will not be processed and no more `onRow`
|
|
|
|
|
* invocations will be made. The promise is resolved immediately.
|
2013-01-05 03:36:59 +04:00
|
|
|
|
*
|
2017-08-19 16:10:44 +03:00
|
|
|
|
* If an exception is thrown by the `onRow` handler, the exception is logged
|
|
|
|
|
* and processing of subsequent rows occurs as if nothing happened. The
|
|
|
|
|
* promise is still resolved (not rejected).
|
2013-01-05 03:36:59 +04:00
|
|
|
|
*
|
|
|
|
|
* The return value is a promise that will be resolved when the statement
|
|
|
|
|
* has completed fully.
|
|
|
|
|
*
|
|
|
|
|
* The promise will be rejected with an `Error` instance if the statement
|
|
|
|
|
* did not finish execution fully. The `Error` may have an `errors` property.
|
|
|
|
|
* If defined, it will be an Array of objects describing individual errors.
|
|
|
|
|
* Each object has the properties `result` and `message`. `result` is a
|
|
|
|
|
* numeric error code and `message` is a string description of the problem.
|
|
|
|
|
*
|
|
|
|
|
* @param name
|
|
|
|
|
* (string) The name of the registered statement to execute.
|
|
|
|
|
* @param params optional
|
|
|
|
|
* (Array or object) Parameters to bind.
|
|
|
|
|
* @param onRow optional
|
|
|
|
|
* (function) Callback to receive each row from result.
|
|
|
|
|
*/
|
2016-12-30 02:34:54 +03:00
|
|
|
|
executeCached(sql, params = null, onRow = null) {
|
2015-09-23 10:01:00 +03:00
|
|
|
|
if (isInvalidBoundLikeQuery(sql)) {
|
|
|
|
|
throw new Error("Please enter a LIKE clause with bindings");
|
|
|
|
|
}
|
2014-06-12 01:40:06 +04:00
|
|
|
|
return this._connectionData.executeCached(sql, params, onRow);
|
2013-01-05 03:36:59 +04:00
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Execute a one-shot SQL statement.
|
|
|
|
|
*
|
|
|
|
|
* If you find yourself feeding the same SQL string in this function, you
|
|
|
|
|
* should *not* use this function and instead use `executeCached`.
|
|
|
|
|
*
|
|
|
|
|
* See `executeCached` for the meaning of the arguments and extended usage info.
|
|
|
|
|
*
|
2014-06-12 01:40:06 +04:00
|
|
|
|
* @param sql
|
|
|
|
|
* (string) SQL to execute.
|
|
|
|
|
* @param params optional
|
|
|
|
|
* (Array or Object) Parameters to bind to the statement.
|
|
|
|
|
* @param onRow optional
|
|
|
|
|
* (function) Callback to receive result of a single row.
|
|
|
|
|
*/
|
2016-12-30 02:34:54 +03:00
|
|
|
|
execute(sql, params = null, onRow = null) {
|
2015-09-23 10:01:00 +03:00
|
|
|
|
if (isInvalidBoundLikeQuery(sql)) {
|
|
|
|
|
throw new Error("Please enter a LIKE clause with bindings");
|
|
|
|
|
}
|
2014-06-12 01:40:06 +04:00
|
|
|
|
return this._connectionData.execute(sql, params, onRow);
|
2013-01-05 03:36:59 +04:00
|
|
|
|
},
|
|
|
|
|
|
2018-03-01 09:44:40 +03:00
|
|
|
|
/**
|
|
|
|
|
* The default behavior for transactions run on this connection.
|
|
|
|
|
*/
|
|
|
|
|
get defaultTransactionType() {
|
|
|
|
|
return this._connectionData.defaultTransactionType;
|
|
|
|
|
},
|
|
|
|
|
|
2013-01-05 03:36:59 +04:00
|
|
|
|
/**
|
|
|
|
|
* Whether a transaction is currently in progress.
|
Bug 1613835 - Use a SQLite API call to check for in-progress transactions. r=mak
Previously, `mozIStorageConnection#transactionInProgress` returned true
only if a transaction was started via `beginTransaction()`. This meant
that manually executing `BEGIN`, as `Sqlite.jsm` and the Rust bindings
do, wouldn't accurately report if a transaction was in progress.
Similarly, the flag wasn't accurate in cases where SQLite automatically
rolled back a transaction.
Fortunately, SQLite provides the `sqlite3_get_autocommit()` function,
which we can use to determine if a transaction is open or not. This
commit refactors the `transactionInProgress` getter, along with all
`Connection` methods that depend on it, to use the SQLite API instead
of managing that state on the connection. `mozStorageTransaction` and
`Sqlite.jsm` still use their own flags to decide whether to commit
their transactions, for reasons explained in the IDL comment.
This commit also moves `transactionInProgress` to
`mozIStorageAsyncConnection`, so that `Sqlite.jsm` can use it, and
exposes it to Rust.
Differential Revision: https://phabricator.services.mozilla.com/D63732
--HG--
extra : moz-landing-system : lando
2020-03-04 01:57:39 +03:00
|
|
|
|
*
|
|
|
|
|
* Note that this is true if a transaction is active on the connection,
|
2022-10-25 23:47:58 +03:00
|
|
|
|
* regardless of whether it was started by `Sqlite.jsm` or another consumer.
|
Bug 1613835 - Use a SQLite API call to check for in-progress transactions. r=mak
Previously, `mozIStorageConnection#transactionInProgress` returned true
only if a transaction was started via `beginTransaction()`. This meant
that manually executing `BEGIN`, as `Sqlite.jsm` and the Rust bindings
do, wouldn't accurately report if a transaction was in progress.
Similarly, the flag wasn't accurate in cases where SQLite automatically
rolled back a transaction.
Fortunately, SQLite provides the `sqlite3_get_autocommit()` function,
which we can use to determine if a transaction is open or not. This
commit refactors the `transactionInProgress` getter, along with all
`Connection` methods that depend on it, to use the SQLite API instead
of managing that state on the connection. `mozStorageTransaction` and
`Sqlite.jsm` still use their own flags to decide whether to commit
their transactions, for reasons explained in the IDL comment.
This commit also moves `transactionInProgress` to
`mozIStorageAsyncConnection`, so that `Sqlite.jsm` can use it, and
exposes it to Rust.
Differential Revision: https://phabricator.services.mozilla.com/D63732
--HG--
extra : moz-landing-system : lando
2020-03-04 01:57:39 +03:00
|
|
|
|
* See the explanation above `mozIStorageConnection.transactionInProgress` for
|
|
|
|
|
* why this distinction matters.
|
2013-01-05 03:36:59 +04:00
|
|
|
|
*/
|
|
|
|
|
get transactionInProgress() {
|
2014-06-12 01:40:06 +04:00
|
|
|
|
return this._connectionData.transactionInProgress;
|
2013-01-05 03:36:59 +04:00
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Perform a transaction.
|
|
|
|
|
*
|
2014-12-11 17:50:51 +03:00
|
|
|
|
* *****************************************************************************
|
|
|
|
|
* YOU SHOULD _NEVER_ NEST executeTransaction CALLS FOR ANY REASON, NOR
|
|
|
|
|
* DIRECTLY, NOR THROUGH OTHER PROMISES.
|
|
|
|
|
* FOR EXAMPLE, NEVER DO SOMETHING LIKE:
|
2019-01-04 11:41:35 +03:00
|
|
|
|
* await executeTransaction(async function () {
|
2014-12-11 17:50:51 +03:00
|
|
|
|
* ...some_code...
|
2019-01-04 11:41:35 +03:00
|
|
|
|
* await executeTransaction(async function () { // WRONG!
|
2014-12-11 17:50:51 +03:00
|
|
|
|
* ...some_code...
|
|
|
|
|
* })
|
2019-01-04 11:41:35 +03:00
|
|
|
|
* await someCodeThatExecuteTransaction(); // WRONG!
|
|
|
|
|
* await neverResolvedPromise; // WRONG!
|
2014-12-11 17:50:51 +03:00
|
|
|
|
* });
|
|
|
|
|
* NESTING CALLS WILL BLOCK ANY FUTURE TRANSACTION UNTIL A TIMEOUT KICKS IN.
|
|
|
|
|
* *****************************************************************************
|
|
|
|
|
*
|
2019-01-04 11:41:35 +03:00
|
|
|
|
* A transaction is specified by a user-supplied function that is an
|
|
|
|
|
* async function. The function receives this connection instance as its argument.
|
2013-01-05 03:36:59 +04:00
|
|
|
|
*
|
2019-01-04 11:41:35 +03:00
|
|
|
|
* The supplied function is expected to return promises. These are often
|
2013-01-05 03:36:59 +04:00
|
|
|
|
* promises created by calling `execute` and `executeCached`. If the
|
|
|
|
|
* generator is exhausted without any errors being thrown, the
|
|
|
|
|
* transaction is committed. If an error occurs, the transaction is
|
|
|
|
|
* rolled back.
|
|
|
|
|
*
|
|
|
|
|
* The returned value from this function is a promise that will be resolved
|
|
|
|
|
* once the transaction has been committed or rolled back. The promise will
|
|
|
|
|
* be resolved to whatever value the supplied function resolves to. If
|
|
|
|
|
* the transaction is rolled back, the promise is rejected.
|
|
|
|
|
*
|
|
|
|
|
* @param func
|
|
|
|
|
* (function) What to perform as part of the transaction.
|
|
|
|
|
* @param type optional
|
|
|
|
|
* One of the TRANSACTION_* constants attached to this type.
|
|
|
|
|
*/
|
2018-03-01 09:44:40 +03:00
|
|
|
|
executeTransaction(func, type = this.TRANSACTION_DEFAULT) {
|
2014-06-12 01:40:06 +04:00
|
|
|
|
return this._connectionData.executeTransaction(() => func(this), type);
|
2013-01-05 03:36:59 +04:00
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
/**
|
2013-09-28 01:04:17 +04:00
|
|
|
|
* Whether a table exists in the database (both persistent and temporary tables).
|
2013-01-05 03:36:59 +04:00
|
|
|
|
*
|
|
|
|
|
* @param name
|
|
|
|
|
* (string) Name of the table.
|
|
|
|
|
*
|
|
|
|
|
* @return Promise<bool>
|
|
|
|
|
*/
|
2016-12-30 02:34:54 +03:00
|
|
|
|
tableExists(name) {
|
2013-01-05 03:36:59 +04:00
|
|
|
|
return this.execute(
|
2013-09-28 01:04:17 +04:00
|
|
|
|
"SELECT name FROM (SELECT * FROM sqlite_master UNION ALL " +
|
|
|
|
|
"SELECT * FROM sqlite_temp_master) " +
|
|
|
|
|
"WHERE type = 'table' AND name=?",
|
2013-01-05 03:36:59 +04:00
|
|
|
|
[name]
|
|
|
|
|
).then(function onResult(rows) {
|
2019-09-14 12:39:26 +03:00
|
|
|
|
return Promise.resolve(!!rows.length);
|
2013-01-05 03:36:59 +04:00
|
|
|
|
});
|
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
/**
|
2013-09-28 01:04:17 +04:00
|
|
|
|
* Whether a named index exists (both persistent and temporary tables).
|
2013-01-05 03:36:59 +04:00
|
|
|
|
*
|
|
|
|
|
* @param name
|
|
|
|
|
* (string) Name of the index.
|
|
|
|
|
*
|
|
|
|
|
* @return Promise<bool>
|
|
|
|
|
*/
|
2016-12-30 02:34:54 +03:00
|
|
|
|
indexExists(name) {
|
2013-01-05 03:36:59 +04:00
|
|
|
|
return this.execute(
|
2013-09-28 01:04:17 +04:00
|
|
|
|
"SELECT name FROM (SELECT * FROM sqlite_master UNION ALL " +
|
|
|
|
|
"SELECT * FROM sqlite_temp_master) " +
|
|
|
|
|
"WHERE type = 'index' AND name=?",
|
2013-01-05 03:36:59 +04:00
|
|
|
|
[name]
|
|
|
|
|
).then(function onResult(rows) {
|
2019-09-14 12:39:26 +03:00
|
|
|
|
return Promise.resolve(!!rows.length);
|
2013-01-05 03:36:59 +04:00
|
|
|
|
});
|
|
|
|
|
},
|
|
|
|
|
|
2013-01-24 23:10:19 +04:00
|
|
|
|
/**
|
|
|
|
|
* Free up as much memory from the underlying database connection as possible.
|
|
|
|
|
*
|
|
|
|
|
* @return Promise<>
|
|
|
|
|
*/
|
2016-12-30 02:34:54 +03:00
|
|
|
|
shrinkMemory() {
|
2014-06-12 01:40:06 +04:00
|
|
|
|
return this._connectionData.shrinkMemory();
|
2013-01-24 23:10:19 +04:00
|
|
|
|
},
|
|
|
|
|
|
2013-01-25 12:38:59 +04:00
|
|
|
|
/**
|
2013-01-26 03:05:15 +04:00
|
|
|
|
* Discard all cached statements.
|
|
|
|
|
*
|
|
|
|
|
* Note that this relies on us being non-interruptible between
|
|
|
|
|
* the insertion or retrieval of a statement in the cache and its
|
|
|
|
|
* execution: we finalize all statements, which is only safe if
|
|
|
|
|
* they will not be executed again.
|
2013-01-25 12:38:59 +04:00
|
|
|
|
*
|
|
|
|
|
* @return (integer) the number of statements discarded.
|
|
|
|
|
*/
|
2016-12-30 02:34:54 +03:00
|
|
|
|
discardCachedStatements() {
|
2014-06-12 01:40:06 +04:00
|
|
|
|
return this._connectionData.discardCachedStatements();
|
2013-01-25 01:30:20 +04:00
|
|
|
|
},
|
2017-07-31 23:27:23 +03:00
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Interrupts pending database operations returning at the first opportunity.
|
|
|
|
|
* Statement execution will throw an NS_ERROR_ABORT failure.
|
|
|
|
|
* Can only be used on read-only connections.
|
|
|
|
|
*/
|
|
|
|
|
interrupt() {
|
|
|
|
|
this._connectionData.interrupt();
|
|
|
|
|
},
|
2013-01-05 03:36:59 +04:00
|
|
|
|
});
|
|
|
|
|
|
2022-09-29 09:52:33 +03:00
|
|
|
|
export var Sqlite = {
|
2021-09-09 19:22:50 +03:00
|
|
|
|
// The maximum time to wait before considering a transaction stuck and
|
|
|
|
|
// issuing a ROLLBACK, see `executeTransaction`. Could be modified by tests.
|
|
|
|
|
TRANSACTIONS_TIMEOUT_MS: 300000, // 5 minutes
|
|
|
|
|
|
2016-12-30 02:34:54 +03:00
|
|
|
|
openConnection,
|
|
|
|
|
cloneStorageConnection,
|
|
|
|
|
wrapStorageConnection,
|
2014-05-13 09:00:00 +04:00
|
|
|
|
/**
|
|
|
|
|
* Shutdown barrier client. May be used by clients to perform last-minute
|
|
|
|
|
* cleanup prior to the shutdown of this module.
|
|
|
|
|
*
|
|
|
|
|
* See the documentation of AsyncShutdown.Barrier.prototype.client.
|
|
|
|
|
*/
|
|
|
|
|
get shutdown() {
|
2022-06-07 07:29:50 +03:00
|
|
|
|
return lazy.Barriers.shutdown.client;
|
2017-05-26 01:40:55 +03:00
|
|
|
|
},
|
|
|
|
|
failTestsOnAutoClose(enabled) {
|
|
|
|
|
Debugging.failTestsOnAutoClose = enabled;
|
|
|
|
|
},
|
2013-01-05 03:36:59 +04:00
|
|
|
|
};
|