UNPKG

liborm

Version:

ORM that use the legacy libsqlite

482 lines (433 loc) 17.6 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.SQLiteCommand = exports.SQLiteCommandReceiver = exports.SelectionCommand = exports.CommandReceiver = exports.Command = undefined; var _getPrototypeOf = require("babel-runtime/core-js/object/get-prototype-of"); var _getPrototypeOf2 = _interopRequireDefault(_getPrototypeOf); var _possibleConstructorReturn2 = require("babel-runtime/helpers/possibleConstructorReturn"); var _possibleConstructorReturn3 = _interopRequireDefault(_possibleConstructorReturn2); var _inherits2 = require("babel-runtime/helpers/inherits"); var _inherits3 = _interopRequireDefault(_inherits2); 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 _time = require("hjs-core/lib/time"); var _action = require("eventslib/lib/action"); var _aggregate = require("eventslib/lib/aggregate"); var _list = require("hjs-collection/lib/list"); var _cursor = require("./cursor"); function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } var Command = exports.Command = function () { function Command(_ref) { var _ref$context = _ref.context, context = _ref$context === undefined ? null : _ref$context, _ref$execute = _ref.execute, execute = _ref$execute === undefined ? null : _ref$execute; (0, _classCallCheck3.default)(this, Command); this.mReceivers = new _aggregate.EventListenerAggregate(_action.ActionListener); if (context) { this.mContext = context; } if (execute) { this.execute = this.execute; } } (0, _createClass3.default)(Command, [{ key: "addReceiver", value: function addReceiver(receiver) { this.mReceivers.add(receiver); return this; } }, { key: "execute", value: function execute() { return null; } }, { key: "unexecute", value: function unexecute() { return null; } }, { key: "getContext", value: function getContext() { return this.mContext; } }, { key: "notifyReceivers", value: function notifyReceivers(event) { var listeners = this.mPropListeners.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.actionPerformed(event); } } catch (err) { _didIteratorError = true; _iteratorError = err; } finally { try { if (!_iteratorNormalCompletion && _iterator.return) { _iterator.return(); } } finally { if (_didIteratorError) { throw _iteratorError; } } } } }, { key: "removeReceiver", value: function removeReceiver(receiver) { this.mReceivers.remove(receiver); return this; } }, { key: "setContext", value: function setContext(context) { this.mContext = context; } }]); return Command; }(); /** @babel */ var CommandReceiver = exports.CommandReceiver = function (_ActionListener) { (0, _inherits3.default)(CommandReceiver, _ActionListener); function CommandReceiver() { var _ref2 = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {}, _ref2$actionPerformed = _ref2.actionPerformed, actionPerformed = _ref2$actionPerformed === undefined ? null : _ref2$actionPerformed; (0, _classCallCheck3.default)(this, CommandReceiver); return (0, _possibleConstructorReturn3.default)(this, (CommandReceiver.__proto__ || (0, _getPrototypeOf2.default)(CommandReceiver)).call(this, { actionPerformed: actionPerformed })); } return CommandReceiver; }(_action.ActionListener); var SelectionCommand = exports.SelectionCommand = function (_Command) { (0, _inherits3.default)(SelectionCommand, _Command); function SelectionCommand(_ref3) { var _ref3$context = _ref3.context, context = _ref3$context === undefined ? null : _ref3$context, _ref3$execute = _ref3.execute, execute = _ref3$execute === undefined ? null : _ref3$execute, _ref3$onSuccess = _ref3.onSuccess, onSuccess = _ref3$onSuccess === undefined ? null : _ref3$onSuccess, _ref3$onError = _ref3.onError, onError = _ref3$onError === undefined ? null : _ref3$onError; (0, _classCallCheck3.default)(this, SelectionCommand); var _this2 = (0, _possibleConstructorReturn3.default)(this, (SelectionCommand.__proto__ || (0, _getPrototypeOf2.default)(SelectionCommand)).call(this, { context: context, execute: execute })); _this2.mProcessing = false; if (onSuccess) { _this2.onSuccess = _this2.onSuccess; } if (onError) { _this2.onError = _this2.onError; } return _this2; } (0, _createClass3.default)(SelectionCommand, [{ key: "addSelection", value: function addSelection(selection) { return this; } }, { key: "isComplete", value: function isComplete() { return !this.isProcessing(); } }, { key: "isProcessing", value: function isProcessing() { return this.mProcessing; } }, { key: "onError", value: function onError(selection, error) {} }, { key: "onSuccess", value: function onSuccess(selection, result) {} }, { key: "removeSelection", value: function removeSelection(selection) { return this; } }]); return SelectionCommand; }(Command); var SQLiteCommandReceiver = exports.SQLiteCommandReceiver = function (_CommandReceiver) { (0, _inherits3.default)(SQLiteCommandReceiver, _CommandReceiver); function SQLiteCommandReceiver() { var _ref4 = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {}, _ref4$begin = _ref4.begin, begin = _ref4$begin === undefined ? null : _ref4$begin, _ref4$commit = _ref4.commit, commit = _ref4$commit === undefined ? null : _ref4$commit, _ref4$rollback = _ref4.rollback, rollback = _ref4$rollback === undefined ? null : _ref4$rollback, _ref4$handler = _ref4.handler, handler = _ref4$handler === undefined ? null : _ref4$handler; (0, _classCallCheck3.default)(this, SQLiteCommandReceiver); var _this3 = (0, _possibleConstructorReturn3.default)(this, (SQLiteCommandReceiver.__proto__ || (0, _getPrototypeOf2.default)(SQLiteCommandReceiver)).call(this)); if (begin) { _this3.begin = _this3.begin; } if (commit) { _this3.commit = _this3.commit; } if (rollback) { _this3.rollback = _this3.rollback; } if (handler) { _this3.mHandler = _this3.handler; } return _this3; } (0, _createClass3.default)(SQLiteCommandReceiver, [{ key: "actionPerformed", value: function actionPerformed(event) { var actionCommand = event.getActionCommand(); var command = event.getSource(); var result = actionCommand.result; var operation = actionCommand.operation; var selection = actionCommand.selection; switch (operation) { case SQLiteCommandReceiver.BEGIN: this.begin(command, selection); break; case SQLiteCommandReceiver.COMMIT: this.commit(command, selection, result); break; case SQLiteCommandReceiver.ROLLBACK: this.rollback(command, selection, result); break; } } }, { key: "begin", value: function begin(command, selection) { if (this.mHandler) { var msg = this.mHandler.obtainMessage({ what: SQLiteCommandReceiver.BEGIN, obj: { command: command, selection: selection } }); if (!this.mHandler.sendMessage(msg)) {} } } }, { key: "commit", value: function commit(command, selection, result) { if (this.mHandler) { var msg = this.mHandler.obtainMessage({ what: SQLiteCommandReceiver.COMMIT, obj: { command: command, selection: selection, result: result } }); if (!this.mHandler.sendMessage(msg)) {} } } }, { key: "rollback", value: function rollback(command, selection, error) { if (this.mHandler) { var msg = this.mHandler.obtainMessage({ what: SQLiteCommandReceiver.ROLLBACK, obj: { command: command, selection: selection, error: error } }); if (!this.mHandler.sendMessage(msg)) {} } } }]); return SQLiteCommandReceiver; }(CommandReceiver); SQLiteCommandReceiver.BEGIN = 0x00000001; SQLiteCommandReceiver.COMMIT = 0x00000010; SQLiteCommandReceiver.ROLLBACK = 0x00000100; var SQLiteCommand = exports.SQLiteCommand = function (_SelectionCommand) { (0, _inherits3.default)(SQLiteCommand, _SelectionCommand); function SQLiteCommand() { var _ref5 = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {}, _ref5$database = _ref5.database, database = _ref5$database === undefined ? null : _ref5$database, _ref5$timeout = _ref5.timeout, timeout = _ref5$timeout === undefined ? -1 : _ref5$timeout; (0, _classCallCheck3.default)(this, SQLiteCommand); var _this4 = (0, _possibleConstructorReturn3.default)(this, (SQLiteCommand.__proto__ || (0, _getPrototypeOf2.default)(SQLiteCommand)).call(this)); _this4.mDatabase = database; _this4.mTimeout = timeout || SQLiteCommand.WAIT_EXECUTION_TIME; _this4.mSelections = []; _this4.mPendingSelections = []; _this4.mNumSelectionsToConsume = -1; return _this4; } (0, _createClass3.default)(SQLiteCommand, [{ key: "addSelection", value: function addSelection(selections) { if (!Array.isArray(selections)) { selections = [selections]; } if (!this.isProcessing()) { console.log("added " + selections.length + " selection(s) to queue"); this.mSelections.push(selections); } else { console.log("pending " + this.mPendingSelections.length + " selection(s) in queue"); this.mPendingSelections.push(selections); } return this; } }, { key: "execute", value: function execute() { if (!this.isProcessing()) { var len = this.mSelections; if (len > 0) { var selections = null; this.mQueue = new _list.LinkedList(); for (var i = 0; i < len; i++) { selections = this.mSelections[i]; this.mQueue.offer(selections); } this.mCurrentIndex = 0; this.mProcessing = true; this.mSelections = []; this.executeSelections(); } else { this.mProcessing = false; } } return this; } }, { key: "executeSelections", value: function executeSelections() { var _this5 = this; if (this.isProcessing()) { var selections = this.mQueue.poll(); console.log(this.mQueue.size() + " pending selection(s)"); if (selections) { this.mPendindExecutions = []; this.mDatabase.transaction(function (tx) { var selection = null; var selectedObject = null; var len = selections.length; _this5.mNumSelectionsToConsume = len; for (var i = 0; i < len; i++) { _this5.mPendindExecutions.unshift(selection = selections[i]); selection.index = i; selectedObject = selection.select(); _this5.notifyReceivers(new _action.ActionEvent({ source: _this5, id: _action.ActionEvent.ACTION_PERFORMED, data: { operation: SQLiteCommandReceiver.BEGIN, selection: selection } })); tx.executeSql(selectedObject["query"], selectedObject["values"], function (tx, resultSet) { _this5.mNumSelectionsToConsume--; _this5.onSuccess(_this5.mPendindExecutions.pop(), resultSet); _this5.nextSelections(_this5.mTimeout); }, function (tx, error) { _this5.mNumSelectionsToConsume--; _this5.onError(_this5.mPendindExecutions.pop(), error); _this5.nextSelections(_this5.mTimeout); }); } }); } else { this.mCurrentIndex = 0; this.mProcessing = false; this.mQueue = null; this.mSelections = this.mPendingSelections; this.mPendingSelections = []; this.execute(); } } } }, { key: "getDatabase", value: function getDatabase() { return this.mDatabase; } }, { key: "getCurrentIndex", value: function getCurrentIndex() { return this.mCurrentIndex; } }, { key: "isComplete", value: function isComplete() { return this.mNumSelectionsToConsume === 0; } }, { key: "nextSelections", value: function nextSelections(timeout) { if (this.isComplete()) { this.mCurrentIndex++; _time.MILLISECONDS.sleep(this.executeSelections.bind(this), timeout); } } }, { key: "onError", value: function onError(selection, error) { this.notifyReceivers(new _action.ActionEvent({ source: this, id: _action.ActionEvent.ACTION_PERFORMED, data: { operation: SQLiteCommandReceiver.ROLLBACK, selection: selection, result: error } })); } }, { key: "onSuccess", value: function onSuccess(selection, result) { this.notifyReceivers(new _action.ActionEvent({ source: this, data: { operation: SQLiteCommandReceiver.COMMIT, selection: selection, result: new _cursor.SQLiteResult({ entity: selection.getModel(), resultSet: result }) } })); } }, { key: "removeSelection", value: function removeSelection(selections) { var index = -1; if (!this.isProcessing()) { index = this.mSelections.indexOf(selections); if (index !== -1) { this.mSelections.splice(index, 1); } } else { index = this.mPendingSelections.indexOf(selections); if (index !== -1) { this.mPendingSelections.splice(index, 1); } } return this; } }]); return SQLiteCommand; }(SelectionCommand); SQLiteCommand.WAIT_EXECUTION_TIME = 0;