libsqlite
Version:
Legacy SQLITE library for browser
1,138 lines (1,062 loc) • 69.7 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.SQLiteDatabase = exports.SQLiteDatabaseConfiguration = undefined;
var _getIterator2 = require("babel-runtime/core-js/get-iterator");
var _getIterator3 = _interopRequireDefault(_getIterator2);
var _classCallCheck2 = require("babel-runtime/helpers/classCallCheck");
var _classCallCheck3 = _interopRequireDefault(_classCallCheck2);
var _createClass2 = require("babel-runtime/helpers/createClass");
var _createClass3 = _interopRequireDefault(_createClass2);
var _input = require("hjs-io/lib/input");
var _reader = require("hjs-io/lib/reader");
var _aggregate = require("eventslib/lib/aggregate");
var _http = require("libhttp/lib/http");
var _conf = require("conflib/lib/conf");
var _event = require("./event");
var _cursor = require("./cursor");
var _helper = require("./helper");
var _query = require("./query");
var _parser = require("./parser");
var _session = require("./session");
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
var SQLiteDatabaseConfiguration = exports.SQLiteDatabaseConfiguration = function () {
function SQLiteDatabaseConfiguration() {
var _ref = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {},
_ref$name = _ref.name,
name = _ref$name === undefined ? "" : _ref$name,
_ref$displayName = _ref.displayName,
displayName = _ref$displayName === undefined ? "" : _ref$displayName,
_ref$size = _ref.size,
size = _ref$size === undefined ? 0 : _ref$size,
_ref$version = _ref.version,
version = _ref$version === undefined ? 1 : _ref$version,
_ref$mode = _ref.mode,
mode = _ref$mode === undefined ? 0 : _ref$mode,
_ref$path = _ref.path,
path = _ref$path === undefined ? "" : _ref$path,
_ref$create_model = _ref.create_model,
create_model = _ref$create_model === undefined ? "" : _ref$create_model,
_ref$drop_model = _ref.drop_model,
drop_model = _ref$drop_model === undefined ? "" : _ref$drop_model;
(0, _classCallCheck3.default)(this, SQLiteDatabaseConfiguration);
this.mName = name;
this.mDisplayName = displayName;
this.mSize = size;
this.mVersion = version;
this.mMode = mode;
this.mPath = path;
this.mCreateModel = create_model;
this.mDropModel = drop_model;
}
(0, _createClass3.default)(SQLiteDatabaseConfiguration, [{
key: "getCreateModel",
value: function getCreateModel() {
return this.mCreateModel;
}
}, {
key: "getDisplayName",
value: function getDisplayName() {
return this.mDisplayName;
}
}, {
key: "getDropModel",
value: function getDropModel() {
return this.mDropModel;
}
}, {
key: "getMode",
value: function getMode() {
return this.mMode;
}
}, {
key: "getName",
value: function getName() {
return this.mName;
}
}, {
key: "getPath",
value: function getPath() {
return this.mPath;
}
}, {
key: "getSize",
value: function getSize() {
return this.mSize;
}
}, {
key: "getVersion",
value: function getVersion() {
return this.mVersion;
}
}, {
key: "isInMemoryDb",
value: function isInMemoryDb() {
return this.mPath === SQLiteDatabaseConfiguration.MEMORY_DB_PATH;
}
}, {
key: "toString",
value: function toString() {
var str = "SQLiteDatabaseConfiguration[name:" + this.mName + ",\n displayName:" + this.mDisplayName + ",\n size:" + this.mSize + ",\n version:" + this.mVersion + ",\n mode:" + this.mMode + ",\n path:" + this.mPath + ",\n createModel:" + this.mCreateModel + ",\n dropModel:" + this.mDropModel + "]";
return str;
}
}]);
return SQLiteDatabaseConfiguration;
}(); /** @babel */
SQLiteDatabaseConfiguration.MEMORY_DB_PATH = ":memory:";
SQLiteDatabaseConfiguration.MEMORY_DB_DISPLAY_NAME = "memory database";
var sActiveDatabases = {};
var SQLiteDatabase = exports.SQLiteDatabase = function () {
function SQLiteDatabase(configuration) {
(0, _classCallCheck3.default)(this, SQLiteDatabase);
if (!configuration) {
throw new ReferenceError("NullPointerException configuration must not be null.");
}
this.mConfiguration = configuration;
this.mListeners = new _aggregate.EventListenerAggregate(SQLiteListener);
this.mSession = new _session.SQLiteSession(this);
this.mCursorFactory = null;
this.mOpen = false;
}
(0, _createClass3.default)(SQLiteDatabase, [{
key: "abs",
value: function abs() {
var X = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : 0;
var adapter = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : null;
if (this.isOpen()) {
this.execSQL("SELECT abs(" + X + ")", null, adapter);
} else {
this.throwIfNotOpenLocked();
}
}
}, {
key: "addColumn",
value: function addColumn() {
var tableName = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : "";
var columnDef = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : "";
var adapter = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : null;
if (this.isOpen()) {
this.execSQL("ALTER TABLE " + tableName + " ADD COLUMN " + columnDef, [], adapter);
} else {
this.throwIfNotOpenLocked();
}
}
}, {
key: "addListener",
value: function addListener(listener) {
this.mListeners.add(listener);
}
}, {
key: "beginTransaction",
value: function beginTransaction() {
var sql = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : "";
var bindArgs = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : [];
var transactionListener = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : null;
if (this.isOpen()) {
this.mLastQuery = sql;
this.mSession.beginTransaction(sql, bindArgs, transactionListener);
} else {
this.throwIfNotOpenLocked();
}
}
}, {
key: "changes",
value: function changes() {
var adapter = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : null;
if (this.isOpen()) {
this.execSQL("SELECT changes()", null, adapter);
} else {
this.throwIfNotOpenLocked();
}
}
}, {
key: "coalesce",
value: function coalesce() {
var values = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : [];
var adapter = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : null;
if (this.isOpen()) {
this.execSQL("SELECT coalesce(" + values.join(",") + ")", null, adapter);
} else {
this.throwIfNotOpenLocked();
}
}
}, {
key: "close",
value: function close() {
if (this.mOpen) {
this.mDatabase = null;
this.mOpen = false;
_conf.Configuration.getInstance().setBDClosed();
} else {
this.throwIfNotOpenLocked();
}
}
}, {
key: "compileStatement",
value: function compileStatement() {
var editTable = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : "";
var query = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : "";
var bindArgs = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : [];
var stmt = _query.SQLiteStatement.getStatements(sql);
this.mLastQuery = query;
if (!stmt) {
stmt = _query.SQLiteStatement.setStatements(query, new _query.SQLiteStatement({
database: this, query: query, editTable: editTable, bindArgs: bindArgs
}));
} else {
stmt.setBindings(bindArgs);
}
return stmt;
}
}, {
key: "createSession",
value: function createSession() {
if (this.isOpen()) {
return new _session.SQLiteSession(this);
} else {
this.throwIfNotOpenLocked();
}
}
}, {
key: "date",
value: function date() {
var timestring = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : "";
var modifiers = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : [];
var adapter = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : null;
if (this.isOpen()) {
this.execSQL("SELECT date(" + timestring + (modifiers && modifiers.length ? "," + modifiers.join(",") : "") + ")", null, adapter);
} else {
this.throwIfNotOpenLocked();
}
}
}, {
key: "datetime",
value: function datetime() {
var timestring = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : "";
var modifiers = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : [];
var adapter = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : null;
if (this.isOpen()) {
this.execSQL("SELECT datetime(" + timestring + (modifiers && modifiers.length ? "," + modifiers.join(",") : "") + ")", null, adapter);
} else {
this.throwIfNotOpenLocked();
}
}
}, {
key: "deleteQuery",
value: function deleteQuery() {
var _ref2 = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {},
_ref2$editTable = _ref2.editTable,
editTable = _ref2$editTable === undefined ? "" : _ref2$editTable,
_ref2$query = _ref2.query,
query = _ref2$query === undefined ? "" : _ref2$query,
_ref2$bindArgs = _ref2.bindArgs,
bindArgs = _ref2$bindArgs === undefined ? [] : _ref2$bindArgs,
_ref2$catchError = _ref2.catchError,
catchError = _ref2$catchError === undefined ? false : _ref2$catchError,
_ref2$adapter = _ref2.adapter,
adapter = _ref2$adapter === undefined ? null : _ref2$adapter;
if (this.isOpen()) {
var _sql = "DELETE FROM " + editTable + (query && query.length !== 0 ? " WHERE " + query : "");
var statement = this.compileStatement(editTable, _sql, bindArgs);
if (adapter) {
adapter.setTable(editTable);
adapter.setCatchError(catchError);
}
statement.executeUpdateDelete(adapter);
} else {
this.throwIfNotOpenLocked();
}
}
}, {
key: "dropTable",
value: function dropTable() {
var _ref3 = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {},
_ref3$table = _ref3.table,
table = _ref3$table === undefined ? "" : _ref3$table,
_ref3$adapter = _ref3.adapter,
adapter = _ref3$adapter === undefined ? null : _ref3$adapter;
if (this.isOpen()) {
this.execSQL("DROP TABLE IF EXISTS " + table, [], adapter);
} else {
this.throwIfNotOpenLocked();
}
}
}, {
key: "endTransaction",
value: function endTransaction(transaction, result) {
if (this.isOpen()) {
this.mSession.endTransaction(transaction, result);
} else {
this.throwIfNotOpenLocked();
}
}
}, {
key: "enqueueTransaction",
value: function enqueueTransaction(queue) {
if (this.mOpen) {
queue.begin();
} else {
this.throwIfNotOpenLocked();
}
}
}, {
key: "execRemoteSQL",
value: function execRemoteSQL() {
var _this = this;
var _ref4 = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {},
path = _ref4.path,
listener = _ref4.listener;
new _http.HTTPConnection({
url: path,
method: "GET",
responseType: "arraybuffer",
handler: {
onHandleRequest: function onHandleRequest(event) {
var type = event.type;
if (type === _http.LOAD_END_STATE) {
var idx = 0;
var line = null;
var queue = _this.getQueue(path);
var input = new Uint8Array(event.response.getMessageBody());
var reader = new _reader.BufferedReader({
input: new _reader.InputStreamReader(new _input.ByteArrayInputStream({ input: input })),
size: input.length
});
while (line = reader.readLine(true)) {
queue.EXEC({ name: "exec-" + idx, sql: line });
idx++;
}
queue.execute(listener);
}
}
}
});
}
}, {
key: "execSQL",
value: function execSQL() {
var sql = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : "";
var _this2 = this;
var bindArgs = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : [];
var adapter = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : null;
if (this.isOpen()) {
bindArgs = bindArgs || [];
var type = _helper.DatabaseUtils.getSqlStatementType(sql);
if (type === _helper.DatabaseUtils.STATEMENT_ATTACH || type === _helper.DatabaseUtils.STATEMENT_BEGIN || type === _helper.DatabaseUtils.STATEMENT_COMMIT || type === _helper.DatabaseUtils.STATEMENT_ABORT || type === _helper.DatabaseUtils.STATEMENT_PRAGMA ||
/*type == DatabaseUtils.STATEMENT_DDL ||*/
type === _helper.DatabaseUtils.STATEMENT_UNPREPARED) {
var error = new SyntaxError(sql + " statement not supported.");
if (adapter && adapter.canCatchError()) {
console.log(type);
adapter.rollback(this.createSession(), error);
} else {
this.notifyListeners(new _event.SQLiteEvent({
source: this,
id: _event.SQLiteEvent.ERROR,
data: error
}));
}
} else {
this.beginTransaction(sql, bindArgs, new _event.SQLiteTransactionListener({
onBegin: function onBegin(session) {
if (adapter) {
adapter.begin(session);
}
},
onCommit: function onCommit(session, resultSet) {
if (adapter) {
adapter.commit(session, new _cursor.ExecCursor({
database: _this2,
tableInfo: _helper.TableInfo.getTable(adapter.getTable()),
resultSet: resultSet
}));
} else {
_this2.notifyListeners(new _event.SQLiteEvent({
source: _this2,
id: _event.SQLiteEvent.EXECUTE,
data: resultSet
}));
}
},
onRollback: function onRollback(session, error) {
if (adapter && adapter.canCatchError()) {
adapter.rollback(session, error);
} else {
_this2.notifyListeners(new _event.SQLiteEvent({
source: _this2,
id: _event.SQLiteEvent.ERROR,
data: error
}));
}
}
}));
}
} else {
this.throwIfNotOpenLocked();
}
}
}, {
key: "getConfiguration",
value: function getConfiguration() {
return this.mConfiguration;
}
}, {
key: "getCreateTableModel",
value: function getCreateTableModel() {
return this.mConfiguration.getCreateTableModel();
}
}, {
key: "getCursorFactory",
value: function getCursorFactory() {
return this.mCursorFactory;
}
}, {
key: "getDropTableModel",
value: function getDropTableModel() {
return this.mConfiguration.getDropTableModel();
}
}, {
key: "getLabel",
value: function getLabel() {
return this.mConfiguration.getDisplayName();
}
}, {
key: "getLastQuery",
value: function getLastQuery() {
return this.mLastQuery;
}
}, {
key: "getMode",
value: function getMode() {
return this.mConfiguration.getMode();
}
}, {
key: "getName",
value: function getName() {
return this.mConfiguration.getName();
}
}, {
key: "getPath",
value: function getPath() {
return this.mConfiguration.getPath();
}
}, {
key: "getQueue",
value: function getQueue(name) {
return new _session.SQLiteQueue(name, this);
}
}, {
key: "getSize",
value: function getSize() {
return this.mConfiguration.getSize();
}
}, {
key: "getTables",
value: function getTables() {
return _helper.TableInfo.getTables();
}
}, {
key: "getTableNames",
value: function getTableNames() {
return _helper.TableInfo.getTableNames();
}
}, {
key: "getVersion",
value: function getVersion() {
return this.mConfiguration.getVersion();
}
}, {
key: "glob",
value: function glob() {
var X = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : "";
var Y = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : "";
var adapter = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : null;
if (this.isOpen()) {
this.execSQL("SELECT glob(" + X + "," + Y + ")", null, adapter);
} else {
this.throwIfNotOpenLocked();
}
}
}, {
key: "hex",
value: function hex() {
var X = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : [];
var adapter = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : null;
if (this.isOpen()) {
this.execSQL("SELECT hex('" + _helper.Hex.arrayBufferToHex(X) + "')", null, adapter);
} else {
this.throwIfNotOpenLocked();
}
}
}, {
key: "ifnull",
value: function ifnull() {
var X = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : "";
var Y = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : "";
var adapter = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : null;
if (this.isOpen()) {
this.execSQL("SELECT ifnull(" + (!X ? "NULL" : X) + "," + (!Y ? "NULL" : Y) + ")", null, adapter);
} else {
this.throwIfNotOpenLocked();
}
}
}, {
key: "insertQuery",
value: function insertQuery() {
var _ref5 = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {},
_ref5$editTable = _ref5.editTable,
editTable = _ref5$editTable === undefined ? "" : _ref5$editTable,
_ref5$nullColumnHack = _ref5.nullColumnHack,
nullColumnHack = _ref5$nullColumnHack === undefined ? "" : _ref5$nullColumnHack,
_ref5$initialValues = _ref5.initialValues,
initialValues = _ref5$initialValues === undefined ? {} : _ref5$initialValues,
_ref5$adapter = _ref5.adapter,
adapter = _ref5$adapter === undefined ? null : _ref5$adapter;
if (this.isOpen()) {
this.insertQueryWithOnConflict({ editTable: editTable, nullColumnHack: nullColumnHack, initialValues: initialValues, conflictAlgorithm: 0, catchError: true, adapter: adapter });
} else {
this.throwIfNotOpenLocked();
}
}
}, {
key: "insertQueryOrThrow",
value: function insertQueryOrThrow() {
var _ref6 = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {},
_ref6$editTable = _ref6.editTable,
editTable = _ref6$editTable === undefined ? "" : _ref6$editTable,
_ref6$nullColumnHack = _ref6.nullColumnHack,
nullColumnHack = _ref6$nullColumnHack === undefined ? "" : _ref6$nullColumnHack,
_ref6$initialValues = _ref6.initialValues,
initialValues = _ref6$initialValues === undefined ? {} : _ref6$initialValues,
_ref6$catchError = _ref6.catchError,
catchError = _ref6$catchError === undefined ? false : _ref6$catchError,
_ref6$adapter = _ref6.adapter,
adapter = _ref6$adapter === undefined ? null : _ref6$adapter;
if (this.isOpen()) {
this.insertQueryWithOnConflict({ editTable: editTable, nullColumnHack: nullColumnHack, initialValues: initialValues, conflictAlgorithm: 0, catchError: catchError, adapter: adapter });
} else {
this.throwIfNotOpenLocked();
}
}
}, {
key: "insertQueryWithOnConflict",
value: function insertQueryWithOnConflict() {
var _ref7 = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {},
_ref7$editTable = _ref7.editTable,
editTable = _ref7$editTable === undefined ? "" : _ref7$editTable,
_ref7$nullColumnHack = _ref7.nullColumnHack,
nullColumnHack = _ref7$nullColumnHack === undefined ? "" : _ref7$nullColumnHack,
_ref7$initialValues = _ref7.initialValues,
initialValues = _ref7$initialValues === undefined ? {} : _ref7$initialValues,
_ref7$conflictAlgorit = _ref7.conflictAlgorithm,
conflictAlgorithm = _ref7$conflictAlgorit === undefined ? 0 : _ref7$conflictAlgorit,
_ref7$catchError = _ref7.catchError,
catchError = _ref7$catchError === undefined ? false : _ref7$catchError,
_ref7$adapter = _ref7.adapter,
adapter = _ref7$adapter === undefined ? null : _ref7$adapter;
if (this.isOpen()) {
var _sql2 = "";
var bindArgs = null;
var n = _helper.DatabaseUtils.countValues(initialValues);
var size = initialValues && n > 0 ? n : 0;
_sql2 += "INSERT" + SQLiteDatabase.CONFLICT_VALUES[conflictAlgorithm] + " INTO " + editTable + '(';
if (size > 0) {
bindArgs = new Array(size);
var index = 0;
for (var colName in initialValues) {
if (initialValues.hasOwnProperty(colName)) {
_sql2 += index > 0 ? "," : "";
_sql2 += colName;
bindArgs[index] = initialValues[colName];
index++;
}
}
_sql2 += ')' + " VALUES (";
for (var i = 0; i < size; i++) {
_sql2 += i > 0 ? ",?" : "?";
}
} else {
_sql2 += nullColumnHack + ") VALUES (NULL";
}
_sql2 += ')';
var statement = this.compileStatement(editTable, _sql2, bindArgs);
if (adapter) {
adapter.setTable(editTable);
adapter.setCatchError(catchError);
}
statement.executeInsert(adapter);
} else {
this.throwIfNotOpenLocked();
}
}
}, {
key: "inTransaction",
value: function inTransaction() {
if (this.isOpen()) {
return this.mSession.hasTransaction();
} else {
this.throwIfNotOpenLocked();
}
}
}, {
key: "isInMemoryDatabase",
value: function isInMemoryDatabase() {
return this.getName() === SQLiteDatabaseConfiguration.MEMORY_DB_PATH;
}
}, {
key: "isOpen",
value: function isOpen() {
return this.mOpen;
}
}, {
key: "isReadOnly",
value: function isReadOnly() {
return this.getMode() === SQLiteDatabase.OPEN_READONLY;
}
}, {
key: "julianday",
value: function julianday() {
var timestring = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : "";
var modifiers = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : [];
var adapter = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : null;
if (this.isOpen()) {
this.execSQL("SELECT julianday(" + timestring + (modifiers && modifiers.length ? "," + modifiers.join(",") : "") + ")", null, adapter);
} else {
this.throwIfNotOpenLocked();
}
}
}, {
key: "lastInsertRowid",
value: function lastInsertRowid() {
var adapter = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : null;
if (this.isOpen()) {
this.execSQL("SELECT last_insert_rowid()", null, adapter);
} else {
this.throwIfNotOpenLocked();
}
}
}, {
key: "length",
value: function length() {
var X = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : "";
var adapter = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : null;
if (this.isOpen()) {
this.execSQL("SELECT length(" + X + ")", null, adapter);
} else {
this.throwIfNotOpenLocked();
}
}
}, {
key: "like",
value: function like() {
var X = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : "";
var Y = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : "";
var Z = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : "";
var adapter = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : null;
if (this.isOpen()) {
this.execSQL("SELECT like(" + X + "," + Y + (Z ? "," + Z : "") + ")", null, adapter);
} else {
this.throwIfNotOpenLocked();
}
}
}, {
key: "lower",
value: function lower() {
var X = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : "";
var adapter = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : null;
if (this.isOpen()) {
this.execSQL("SELECT lower(" + X + ")", null, adapter);
} else {
this.throwIfNotOpenLocked();
}
}
}, {
key: "ltrim",
value: function ltrim() {
var X = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : "";
var Y = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : "";
var adapter = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : null;
if (this.isOpen()) {
this.execSQL("SELECT ltrim(" + X + (Y ? "," + Y : "") + ")", null, adapter);
} else {
this.throwIfNotOpenLocked();
}
}
}, {
key: "max",
value: function max() {
var values = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : [];
var adapter = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : null;
if (this.isOpen()) {
this.execSQL("SELECT max(" + values.join(",") + ")", null, adapter);
} else {
this.throwIfNotOpenLocked();
}
}
}, {
key: "min",
value: function min() {
var values = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : [];
var adapter = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : null;
if (this.isOpen()) {
this.execSQL("SELECT min(" + values.join(",") + ")", null, adapter);
} else {
this.throwIfNotOpenLocked();
}
}
}, {
key: "needUpgrade",
value: function needUpgrade(newVersion) {
return newVersion > this.getVersion();
}
}, {
key: "newCursorLoader",
value: function newCursorLoader() {
var _ref8 = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {},
id = _ref8.id,
context = _ref8.context,
listener = _ref8.listener,
executor = _ref8.executor,
_ref8$editTable = _ref8.editTable,
editTable = _ref8$editTable === undefined ? "" : _ref8$editTable,
_ref8$projection = _ref8.projection,
projection = _ref8$projection === undefined ? [] : _ref8$projection,
_ref8$selection = _ref8.selection,
selection = _ref8$selection === undefined ? "" : _ref8$selection,
_ref8$selectionArgs = _ref8.selectionArgs,
selectionArgs = _ref8$selectionArgs === undefined ? [] : _ref8$selectionArgs,
_ref8$groupBy = _ref8.groupBy,
groupBy = _ref8$groupBy === undefined ? "" : _ref8$groupBy,
_ref8$having = _ref8.having,
having = _ref8$having === undefined ? "" : _ref8$having,
_ref8$sortOrder = _ref8.sortOrder,
sortOrder = _ref8$sortOrder === undefined ? "" : _ref8$sortOrder,
_ref8$limit = _ref8.limit,
limit = _ref8$limit === undefined ? "" : _ref8$limit;
return new _cursor.CursorLoader({
id: id,
context: context,
listener: listener,
executor: executor,
database: this,
editTable: editTable,
projection: projection,
selection: selection,
selectionArgs: selectionArgs,
groupBy: groupBy,
having: having,
sortOrder: sortOrder,
limit: limit
});
}
}, {
key: "notifyListeners",
value: function notifyListeners(evt) {
var listeners = this.mListeners.getListenersInternal();
var _iteratorNormalCompletion = true;
var _didIteratorError = false;
var _iteratorError = undefined;
try {
for (var _iterator = (0, _getIterator3.default)(listeners), _step; !(_iteratorNormalCompletion = (_step = _iterator.next()).done); _iteratorNormalCompletion = true) {
var listener = _step.value;
listener.onHandleEvent(evt);
}
} catch (err) {
_didIteratorError = true;
_iteratorError = err;
} finally {
try {
if (!_iteratorNormalCompletion && _iterator.return) {
_iterator.return();
}
} finally {
if (_didIteratorError) {
throw _iteratorError;
}
}
}
}
}, {
key: "nullif",
value: function nullif() {
var X = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : "";
var Y = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : "";
var adapter = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : null;
if (this.isOpen()) {
this.execSQL("SELECT nullif(" + (!X ? "NULL" : X) + "," + (!Y ? "NULL" : Y) + ")", null, adapter);
} else {
this.throwIfNotOpenLocked();
}
}
}, {
key: "onCorruption",
value: function onCorruption(evt) {
this.notifyListeners(evt);
}
}, {
key: "open",
value: function open() {
var _this3 = this;
var configuration = _conf.Configuration.getInstance();
try {
var mode = this.getMode();
switch (mode) {
case SQLiteDatabase.OPEN_READONLY:
case SQLiteDatabase.OPEN_READWRITE:
if (!this.mOpen) {
var created = configuration.isDBCreated();
this.mDatabase = window.openDatabase(this.mConfiguration.getName(), "" + this.mConfiguration.getVersion(), this.mConfiguration.getSize(), null);
if (this.mOpen = this.mDatabase !== null || this.mDatabase !== undefined) {
configuration.setDBOpened();
if (!created) {
configuration.setDBCreated(true);
var path = this.getCreateTableModel();
var parser = new _parser.SQLiteParser();
var listener = new _event.SQLiteParserListener({
onParseEvent: function onParseEvent(evt) {
var statements = evt.getData();
var queue = new _session.SQLiteTransactionQueue({
database: _this3,
complete: function complete(manager) {
_this3.notifyListeners(new _event.SQLiteEvent({
source: _this3,
id: _event.SQLiteEvent.CREATE
}));
}
});
var createStatement = "CREATE TABLE IF NOT EXISTS ";
var _iteratorNormalCompletion2 = true;
var _didIteratorError2 = false;
var _iteratorError2 = undefined;
try {
for (var _iterator2 = (0, _getIterator3.default)(statements), _step2; !(_iteratorNormalCompletion2 = (_step2 = _iterator2.next()).done); _iteratorNormalCompletion2 = true) {
var statement = _step2.value;
var idx1 = statement.indexOf(createStatement);
var idx2 = statement.indexOf("(");
if (idx1 !== -1 && idx2 !== -1) {
var tableName = statement.substring(createStatement.length, idx2).trim();
var tableInfo = _helper.TableInfo.setTable(tableName, new _helper.TableInfo(tableName));
tableInfo.parse(statement);
}
queue.offer({ statement: statement, bindArgs: [] });
}
} catch (err) {
_didIteratorError2 = true;
_iteratorError2 = err;
} finally {
try {
if (!_iteratorNormalCompletion2 && _iterator2.return) {
_iterator2.return();
}
} finally {
if (_didIteratorError2) {
throw _iteratorError2;
}
}
}
_this3.enqueueTransaction(queue);
parser.removeListener(listener);
}
});
parser.addListener(listener);
parser.loadFile(path);
} else {
this.notifyListeners(new _event.SQLiteEvent({
source: this,
id: _event.SQLiteEvent.OPEN
}));
}
} else {
throw new ReferenceError("SQLiteException database not created");
}
}
return this;
}
} catch (e) {
console.log("Failed to open database '" + this.getLabel() + "'." + e.message);
configuration.setDBCreated(false);
this.close();
this.notifyListeners(new _event.SQLiteEvent({
source: this,
id: _event.SQLiteEvent.ERROR,
data: e
}));
}
return null;
}
}, {
key: "query",
value: function query() {
var _ref9 = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {},
_ref9$editTable = _ref9.editTable,
editTable = _ref9$editTable === undefined ? "" : _ref9$editTable,
_ref9$columns = _ref9.columns,
columns = _ref9$columns === undefined ? [] : _ref9$columns,
_ref9$selection = _ref9.selection,
selection = _ref9$selection === undefined ? "" : _ref9$selection,
_ref9$selectionArgs = _ref9.selectionArgs,
selectionArgs = _ref9$selectionArgs === undefined ? [] : _ref9$selectionArgs,
_ref9$groupBy = _ref9.groupBy,
groupBy = _ref9$groupBy === undefined ? "" : _ref9$groupBy,
_ref9$having = _ref9.having,
having = _ref9$having === undefined ? "" : _ref9$having,
_ref9$orderBy = _ref9.orderBy,
orderBy = _ref9$orderBy === undefined ? "" : _ref9$orderBy,
_ref9$limit = _ref9.limit,
limit = _ref9$limit === undefined ? "" : _ref9$limit,
_ref9$adapter = _ref9.adapter,
adapter = _ref9$adapter === undefined ? null : _ref9$adapter;
if (this.isOpen()) {
this.queryWithFactory({ cursorFactory: null, distinct: false, editTable: editTable, columns: columns, selection: selection, selectionArgs: selectionArgs, groupBy: groupBy, having: having, orderBy: orderBy, limit: limit, adapter: adapter });
} else {
this.throwIfNotOpenLocked();
}
}
}, {
key: "queryWithFactory",
value: function queryWithFactory() {
var _ref10 = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {},
cursorFactory = _ref10.cursorFactory,
_ref10$distinct = _ref10.distinct,
distinct = _ref10$distinct === undefined ? false : _ref10$distinct,
_ref10$editTable = _ref10.editTable,
editTable = _ref10$editTable === undefined ? "" : _ref10$editTable,
_ref10$columns = _ref10.columns,
columns = _ref10$columns === undefined ? [] : _ref10$columns,
_ref10$selection = _ref10.selection,
selection = _ref10$selection === undefined ? "" : _ref10$selection,
_ref10$selectionArgs = _ref10.selectionArgs,
selectionArgs = _ref10$selectionArgs === undefined ? [] : _ref10$selectionArgs,
_ref10$groupBy = _ref10.groupBy,
groupBy = _ref10$groupBy === undefined ? "" : _ref10$groupBy,
_ref10$having = _ref10.having,
having = _ref10$having === undefined ? "" : _ref10$having,
_ref10$orderBy = _ref10.orderBy,
orderBy = _ref10$orderBy === undefined ? "" : _ref10$orderBy,
_ref10$limit = _ref10.limit,
limit = _ref10$limit === undefined ? "" : _ref10$limit,
_ref10$adapter = _ref10.adapter,
adapter = _ref10$adapter === undefined ? null : _ref10$adapter;
if (this.isOpen()) {
var query = _query.SQLiteQueryBuilder.buildQueryString(distinct, editTable, columns, selection, groupBy, having, orderBy, limit);
editTable = _helper.DatabaseUtils.findEditTable(editTable);
this.rawQueryWithFactory({ cursorFactory: cursorFactory, query: query, selectionArgs: selectionArgs, editTable: editTable, adapter: adapter });
} else {
this.throwIfNotOpenLocked();
}
}
}, {
key: "quote",
value: function quote() {
var value = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : "";
var adapter = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : null;
if (this.isOpen()) {
this.execSQL("SELECT quote(" + value + ")", null, adapter);
} else {
this.throwIfNotOpenLocked();
}
}
}, {
key: "rawQuery",
value: function rawQuery() {
var _ref11 = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {},
_ref11$editTable = _ref11.editTable,
editTable = _ref11$editTable === undefined ? "" : _ref11$editTable,
_ref11$query = _ref11.query,
query = _ref11$query === undefined ? "" : _ref11$query,
_ref11$selectionArgs = _ref11.selectionArgs,
selectionArgs = _ref11$selectionArgs === undefined ? [] : _ref11$selectionArgs,
_ref11$adapter = _ref11.adapter,
adapter = _ref11$adapter === undefined ? null : _ref11$adapter;
if (this.isOpen()) {
this.rawQueryWithFactory({ cursorFactory: null, editTable: editTable, query: query, selectionArgs: selectionArgs, catchError: false, adapter: adapter });
} else {
this.throwIfNotOpenLocked();
}
}
}, {
key: "rawQueryWithFactory",
value: function rawQueryWithFactory() {
var _ref12 = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {},
_ref12$cursorFactory = _ref12.cursorFactory,
cursorFactory = _ref12$cursorFactory === undefined ? null : _ref12$cursorFactory,
_ref12$editTable = _ref12.editTable,
editTable = _ref12$editTable === undefined ? "" : _ref12$editTable,
_ref12$query = _ref12.query,
query = _ref12$query === undefined ? "" : _ref12$query,
_ref12$selectionArgs = _ref12.selectionArgs,
selectionArgs = _ref12$selectionArgs === undefined ? [] : _ref12$selectionArgs,
_ref12$catchError = _ref12.catchError,
catchError = _ref12$catchError === undefined ? false : _ref12$catchError,
_ref12$adapter = _ref12.adapter,
adapter = _ref12$adapter === undefined ? null : _ref12$adapter;
if (this.isOpen()) {
this.mLastQuery = query;
var driver = _cursor.SQLiteCursorDriver.setDriver(query, new _cursor.SQLiteCursorDriver({
database: this,
query: query,
editTable: editTable
}));
if (adapter) {
adapter.setTable(editTable);
adapter.setCatchError(catchError);
}
driver.query(cursorFactory ? cursorFactory : this.mCursorFactory, selectionArgs, adapter);
} else {
this.throwIfNotOpenLocked();
}
}
}, {
key: "removeListener",
value: function removeListener(listener) {
this.mListeners.remove(listener);
}
}, {
key: "renameTable",
value: function renameTable() {
var tableName = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : "";
var newTableName = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : "";
var adapter = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : null;
if (this.isOpen()) {
this.execSQL("ALTER TABLE " + tableName + " RENAME TO " + newTableName, [], adapter);
} else {
this.throwIfNotOpenLocked();
}
}
}, {
key: "reopenReadWrite",
value: function reopenReadWrite() {
if (!this.isReadOnly()) {
return;
}
this.close();
this.open();
}
}, {
key: "replace",
value: function replace() {
var X = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : "";
var Y = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : "";
var Z = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : "";
var adapter = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : null;
if (this.isOpen()) {
this.execSQL("SELECT replace(" + X + "," + Y + "," + Z + ")", null, adapter);
} else {
this.throwIfNotOpenLocked();
}
}
}, {
key: "replaceQuery",
value: function replaceQuery() {
var _ref13 = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {},
_ref13$editTable = _ref13.editTable,
editTable = _ref13$editTable === undefined ? "" : _ref13$editTable,
_ref13$nullColumnHack = _ref13.nullColumnHack,
nullColumnHack = _ref13$nullColumnHack === undefined ? "" : _ref13$nullColumnHack,
_ref13$initialValues = _ref13.initialValues,
initialValues = _ref13$initialValues === undefined ? {} : _ref13$initialValues,
_ref13$adapter = _ref13.adapter,
adapter = _ref13$adapter === undefined ? null : _ref13$adapter;
if (this.isOpen()) {
this.i