liborm
Version:
ORM that use the legacy libsqlite
320 lines (282 loc) • 10.9 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.SQLiteResult = undefined;
var _getPrototypeOf = require("babel-runtime/core-js/object/get-prototype-of");
var _getPrototypeOf2 = _interopRequireDefault(_getPrototypeOf);
var _classCallCheck2 = require("babel-runtime/helpers/classCallCheck");
var _classCallCheck3 = _interopRequireDefault(_classCallCheck2);
var _createClass2 = require("babel-runtime/helpers/createClass");
var _createClass3 = _interopRequireDefault(_createClass2);
var _possibleConstructorReturn2 = require("babel-runtime/helpers/possibleConstructorReturn");
var _possibleConstructorReturn3 = _interopRequireDefault(_possibleConstructorReturn2);
var _inherits2 = require("babel-runtime/helpers/inherits");
var _inherits3 = _interopRequireDefault(_inherits2);
var _helper = require("libsqlite/lib/helper");
var _cursor = require("libsqlite/lib/cursor");
var _entity = require("./entity");
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
var SQLiteResult = exports.SQLiteResult = function (_Cursor) {
(0, _inherits3.default)(SQLiteResult, _Cursor);
function SQLiteResult() {
var _ref = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {},
entity = _ref.entity,
_ref$resultSet = _ref.resultSet,
resultSet = _ref$resultSet === undefined ? {} : _ref$resultSet;
(0, _classCallCheck3.default)(this, SQLiteResult);
var _this = (0, _possibleConstructorReturn3.default)(this, (SQLiteResult.__proto__ || (0, _getPrototypeOf2.default)(SQLiteResult)).call(this));
if (!entity) {
throw new ReferenceError("IllegalArgumentException entity object cannot be null");
}
if (!resultSet) {
throw new ReferenceError("IllegalArgumentException resultSet object cannot be null");
}
_this.mEditTable = entity.getTableName();
_this.mColumnNameMap = null;
_this.mEntity = entity;
_this.mResultSet = resultSet;
_this.mColumns = _this.mEntity.getKeys();
if (!_this.mColumnNameMap) {
var map = {};
var columns = _this.mColumns;
var columnCount = columns.length;
while (columnCount--) {
map[columns[columnCount]] = columnCount;
}
_this.mColumnNameMap = map;
}
return _this;
}
(0, _createClass3.default)(SQLiteResult, [{
key: "fillWindow",
value: function fillWindow() {
var requiredPos = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : 0;
var len = this.mResultSet.rows.length;
if (this.mCount === _cursor.SQLiteCursor.NO_COUNT) {
var num = len;
var item = null;
this.mRows = [];
var rows = this.mResultSet.rows;
for (var i = requiredPos; i < len; i++) {
item = rows.item(i);
if (item) {
this.mRows[i] = item;
} else {
num--;
}
}
this.mCount = num;
} else {
this.mCount = len - requiredPos;
this.mRows = this.mRows.splice(requiredPos, this.mCount);
}
console.log("received count(*) from fillWindow: " + this.mCount);
}
}, {
key: "getBlob",
value: function getBlob() {
var columnIndex = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : 0;
this.checkPosition();
return this.getValue(columnIndex);
}
}, {
key: "getColumnIndex",
value: function getColumnIndex() {
var columnName = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : "";
var periodIndex = columnName.lastIndexOf('.');
if (periodIndex !== -1) {
console.log("requesting column name with table name -- " + columnName);
columnName = columnName.substring(periodIndex + 1);
}
var i = this.mColumnNameMap[columnName];
if (i !== null && i !== undefined) {
return i;
}
return -1;
}
}, {
key: "getColumnNames",
value: function getColumnNames() {
return this.mColumns;
}
}, {
key: "getCount",
value: function getCount() {
if (this.mCount === _cursor.SQLiteCursor.NO_COUNT) {
this.fillWindow(0);
}
return this.mCount;
}
}, {
key: "getDouble",
value: function getDouble() {
var columnIndex = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : 0;
this.checkPosition();
return this.getValue(columnIndex);
}
}, {
key: "getFloat",
value: function getFloat() {
var columnIndex = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : 0;
this.checkPosition();
return this.getValue(columnIndex);
}
}, {
key: "getInsertedId",
value: function getInsertedId() {
return this.mResultSet.insertId;
}
}, {
key: "getInt",
value: function getInt() {
var columnIndex = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : 0;
this.checkPosition();
return this.getValue(columnIndex);
}
}, {
key: "getLong",
value: function getLong() {
var columnIndex = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : 0;
this.checkPosition();
return this.getValue(columnIndex);
}
}, {
key: "getRowsAffected",
value: function getRowsAffected() {
return this.mResultSet.rowsAffected;
}
}, {
key: "getRowValue",
value: function getRowValue() {
return this.mRows[this.mPos];
}
}, {
key: "getShort",
value: function getShort() {
var columnIndex = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : 0;
this.checkPosition();
return this.getValue(columnIndex);
}
}, {
key: "getString",
value: function getString() {
var columnIndex = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : 0;
this.checkPosition();
return this.getValue(columnIndex);
}
}, {
key: "getType",
value: function getType() {
var columnIndex = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : 0;
this.checkPosition();
return _helper.DatabaseUtils.getTypeOfObject(this.getValue(columnIndex));
}
}, {
key: "getValue",
value: function getValue() {
var columnIndex = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : 0;
var row = this.mRows[this.mPos];
return row[this.mColumns[columnIndex]];
}
}, {
key: "hasChanged",
value: function hasChanged() {
return this.getRowsAffected() > 0;
}
}, {
key: "isBlob",
value: function isBlob() {
var columnIndex = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : 0;
return this.getType(columnIndex) === _cursor.Cursor.FIELD_TYPE_BLOB;
}
}, {
key: "isInt",
value: function isInt() {
var columnIndex = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : 0;
return this.getType(columnIndex) === _cursor.Cursor.FIELD_TYPE_INTEGER;
}
}, {
key: "isNull",
value: function isNull() {
var columnIndex = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : 0;
return this.getType(columnIndex) === _cursor.Cursor.FIELD_TYPE_NULL;
}
}, {
key: "isString",
value: function isString() {
var columnIndex = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : 0;
return this.getType(columnIndex) === _cursor.Cursor.FIELD_TYPE_STRING;
}
}, {
key: "newEntity",
value: function newEntity() {
var entity = _entity.Entity.getTableInstance(this.mEditTable);
for (var p in this.mColumnNameMap) {
if (this.mColumnNameMap.hasOwnProperty(p)) {
var value = this.getValue(this.mColumnNameMap[p]);
if (value) {
entity.setProperty(p, value);
}
}
}
return entity;
}
}, {
key: "newRowEntity",
value: function newRowEntity() {
var row = {};
for (var p in this.mColumnNameMap) {
if (this.mColumnNameMap.hasOwnProperty(p)) {
var value = this.getValue(this.mColumnNameMap[p]);
if (value) {
row[p] = value;
}
}
}
return row;
}
}, {
key: "newRowValue",
value: function newRowValue() {
var row = {};
var rowResult = this.getRowValue();
for (var p in rowResult) {
if (rowResult.hasOwnProperty(p)) {
var value = rowResult[p];
if (value) {
row[p] = value;
}
}
}
return row;
}
}, {
key: "onMove",
value: function onMove() {
var oldPosition = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : -1;
var newPosition = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : 0;
if (!this.mRows || newPosition >= this.mRows.length) {
this.fillWindow(newPosition);
}
return true;
}
}, {
key: "requery",
value: function requery() {
if (this.mRows) {
this.mRows = [];
}
this.mPos = -1;
this.mCount = _cursor.SQLiteCursor.NO_COUNT;
}
}, {
key: "setWindow",
value: function setWindow() {
var resultSet = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
this.mResultSet = resultSet;
this.mCount = _cursor.SQLiteCursor.NO_COUNT;
}
}]);
return SQLiteResult;
}(_cursor.Cursor); /** @babel */