2019-04-18 21:32:38 +00:00
|
|
|
// Copyright IBM Corp. 2014,2019. All Rights Reserved.
|
2016-05-06 04:50:59 +00:00
|
|
|
// Node module: loopback-connector
|
|
|
|
// This file is licensed under the MIT License.
|
|
|
|
// License text available at https://opensource.org/licenses/MIT
|
|
|
|
|
2017-03-06 23:40:47 +00:00
|
|
|
'use strict';
|
2014-10-13 16:47:11 +00:00
|
|
|
/*
|
|
|
|
* A mockup connector that extends SQL connector
|
|
|
|
*/
|
2019-08-02 13:50:51 +00:00
|
|
|
const util = require('util');
|
|
|
|
const SQLConnector = require('../../lib/sql');
|
|
|
|
const debug = require('debug')('loopback:connector:test-sql');
|
2015-05-15 17:27:08 +00:00
|
|
|
|
2019-08-02 13:50:51 +00:00
|
|
|
let transactionId = 0;
|
2015-05-15 17:27:08 +00:00
|
|
|
|
|
|
|
function MockTransaction(connector, name) {
|
|
|
|
this.connector = connector;
|
|
|
|
this.name = name;
|
2015-05-15 23:59:59 +00:00
|
|
|
this.data = {};
|
2015-05-15 17:27:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
MockTransaction.prototype.commit = function(cb) {
|
2019-08-02 13:50:51 +00:00
|
|
|
const self = this;
|
2015-05-15 23:59:59 +00:00
|
|
|
// Merge data from this TX to the global data var
|
2019-08-02 13:50:51 +00:00
|
|
|
for (const m in this.data) {
|
2015-05-15 23:59:59 +00:00
|
|
|
self.connector.data[m] = self.connector.data[m] || [];
|
2019-08-02 13:50:51 +00:00
|
|
|
for (let i = 0, n = this.data[m].length; i < n; i++) {
|
2015-05-15 23:59:59 +00:00
|
|
|
self.connector.data[m].push(this.data[m]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
this.data = {};
|
2015-05-15 17:27:08 +00:00
|
|
|
cb();
|
|
|
|
};
|
|
|
|
|
|
|
|
MockTransaction.prototype.rollback = function(cb) {
|
2015-05-15 23:59:59 +00:00
|
|
|
this.data = {};
|
2015-05-15 17:27:08 +00:00
|
|
|
cb();
|
|
|
|
};
|
2014-10-13 16:47:11 +00:00
|
|
|
|
|
|
|
exports.initialize = function initializeDataSource(dataSource, callback) {
|
|
|
|
process.nextTick(function() {
|
2015-05-13 17:14:44 +00:00
|
|
|
if (callback) {
|
2019-08-02 13:50:51 +00:00
|
|
|
const connector = new TestConnector(dataSource.settings);
|
2014-10-13 16:47:11 +00:00
|
|
|
connector.dataSource = dataSource;
|
|
|
|
dataSource.connector = connector;
|
|
|
|
callback(null, connector);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2015-05-13 17:14:44 +00:00
|
|
|
function TestConnector(settings) {
|
|
|
|
SQLConnector.call(this, 'testdb', settings);
|
2014-10-13 16:47:11 +00:00
|
|
|
this._tables = {};
|
2015-05-15 23:59:59 +00:00
|
|
|
this.data = {};
|
2014-10-13 16:47:11 +00:00
|
|
|
}
|
|
|
|
|
2015-05-13 17:14:44 +00:00
|
|
|
util.inherits(TestConnector, SQLConnector);
|
|
|
|
|
|
|
|
TestConnector.prototype.escapeName = function(name) {
|
|
|
|
return '`' + name + '`';
|
|
|
|
};
|
|
|
|
|
|
|
|
TestConnector.prototype.dbName = function(name) {
|
|
|
|
return name.toUpperCase();
|
|
|
|
};
|
|
|
|
|
|
|
|
TestConnector.prototype.getPlaceholderForValue = function(key) {
|
|
|
|
return '$' + key;
|
|
|
|
};
|
|
|
|
|
|
|
|
TestConnector.prototype.escapeValue = function(value) {
|
|
|
|
if (typeof value === 'number' || typeof value === 'boolean') {
|
|
|
|
return value;
|
|
|
|
}
|
|
|
|
if (typeof value === 'string') {
|
|
|
|
return "'" + value + "'";
|
|
|
|
}
|
|
|
|
if (value == null) {
|
|
|
|
return 'NULL';
|
|
|
|
}
|
|
|
|
if (typeof value === 'object') {
|
|
|
|
return String(value);
|
|
|
|
}
|
|
|
|
return value;
|
|
|
|
};
|
|
|
|
|
|
|
|
TestConnector.prototype.toColumnValue = function(prop, val) {
|
|
|
|
return val;
|
|
|
|
};
|
|
|
|
|
|
|
|
TestConnector.prototype._buildLimit = function(model, limit, offset) {
|
|
|
|
if (isNaN(limit)) {
|
|
|
|
limit = 0;
|
|
|
|
}
|
|
|
|
if (isNaN(offset)) {
|
|
|
|
offset = 0;
|
|
|
|
}
|
|
|
|
if (!limit && !offset) {
|
|
|
|
return '';
|
|
|
|
}
|
|
|
|
return 'LIMIT ' + (offset ? (offset + ',' + limit) : limit);
|
|
|
|
};
|
|
|
|
|
2015-05-15 17:27:08 +00:00
|
|
|
TestConnector.prototype.applyPagination =
|
|
|
|
function(model, stmt, filter) {
|
2017-03-06 23:40:47 +00:00
|
|
|
/* jshint unused:false */
|
2019-08-02 13:50:51 +00:00
|
|
|
const limitClause = this._buildLimit(model, filter.limit,
|
2015-05-15 17:27:08 +00:00
|
|
|
filter.offset || filter.skip);
|
|
|
|
return stmt.merge(limitClause);
|
|
|
|
};
|
|
|
|
|
|
|
|
TestConnector.prototype.escapeName = function(name) {
|
|
|
|
return '`' + name + '`';
|
|
|
|
};
|
|
|
|
|
|
|
|
TestConnector.prototype.dbName = function(name) {
|
|
|
|
return name.toUpperCase();
|
|
|
|
};
|
|
|
|
|
|
|
|
TestConnector.prototype.getPlaceholderForValue = function(key) {
|
|
|
|
return '$' + key;
|
|
|
|
};
|
|
|
|
|
|
|
|
TestConnector.prototype.escapeValue = function(value) {
|
|
|
|
if (typeof value === 'number' || typeof value === 'boolean') {
|
|
|
|
return value;
|
|
|
|
}
|
|
|
|
if (typeof value === 'string') {
|
|
|
|
return "'" + value + "'";
|
|
|
|
}
|
|
|
|
if (value == null) {
|
|
|
|
return 'NULL';
|
|
|
|
}
|
|
|
|
if (typeof value === 'object') {
|
|
|
|
return String(value);
|
|
|
|
}
|
|
|
|
return value;
|
|
|
|
};
|
|
|
|
|
|
|
|
TestConnector.prototype.toColumnValue = function(prop, val, escaping) {
|
|
|
|
return escaping ? this.escapeValue(val) : val;
|
|
|
|
};
|
|
|
|
|
|
|
|
TestConnector.prototype._buildLimit = function(model, limit, offset) {
|
|
|
|
if (isNaN(limit)) {
|
|
|
|
limit = 0;
|
|
|
|
}
|
|
|
|
if (isNaN(offset)) {
|
|
|
|
offset = 0;
|
|
|
|
}
|
|
|
|
if (!limit && !offset) {
|
|
|
|
return '';
|
|
|
|
}
|
|
|
|
return 'LIMIT ' + (offset ? (offset + ',' + limit) : limit);
|
|
|
|
};
|
|
|
|
|
2015-05-13 17:14:44 +00:00
|
|
|
TestConnector.prototype.applyPagination =
|
|
|
|
function(model, stmt, filter) {
|
2017-03-06 23:40:47 +00:00
|
|
|
/* jshint unused:false */
|
2019-08-02 13:50:51 +00:00
|
|
|
const limitClause = this._buildLimit(model, filter.limit,
|
2015-05-13 17:14:44 +00:00
|
|
|
filter.offset || filter.skip);
|
|
|
|
return stmt.merge(limitClause);
|
|
|
|
};
|
2014-10-13 16:47:11 +00:00
|
|
|
|
|
|
|
TestConnector.prototype.dropTable = function(model, cb) {
|
2019-08-02 13:50:51 +00:00
|
|
|
let err;
|
|
|
|
const exists = model in this._tables;
|
2014-10-13 16:47:11 +00:00
|
|
|
if (!exists) {
|
|
|
|
err = new Error('Model doesn\'t exist: ' + model);
|
|
|
|
} else {
|
|
|
|
delete this._tables[model];
|
|
|
|
}
|
|
|
|
process.nextTick(function() {
|
|
|
|
cb(err);
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
TestConnector.prototype.createTable = function(model, cb) {
|
2019-08-02 13:50:51 +00:00
|
|
|
let err;
|
|
|
|
const exists = model in this._tables;
|
2014-10-13 16:47:11 +00:00
|
|
|
if (exists) {
|
|
|
|
err = new Error('Model already exists: ' + model);
|
|
|
|
} else {
|
|
|
|
this._tables[model] = model;
|
|
|
|
}
|
|
|
|
process.nextTick(function() {
|
|
|
|
cb(err);
|
|
|
|
});
|
|
|
|
};
|
2015-05-13 17:14:44 +00:00
|
|
|
|
2015-05-15 17:27:08 +00:00
|
|
|
TestConnector.prototype.getInsertedId = function(model, info) {
|
|
|
|
return info;
|
|
|
|
};
|
|
|
|
|
2022-12-11 20:08:14 +00:00
|
|
|
TestConnector.prototype.getInsertedIds = function(model, info) {
|
|
|
|
return info;
|
|
|
|
};
|
|
|
|
|
2015-05-15 17:27:08 +00:00
|
|
|
TestConnector.prototype.fromColumnValue = function(propertyDef, value) {
|
|
|
|
return value;
|
|
|
|
};
|
|
|
|
|
|
|
|
TestConnector.prototype.beginTransaction = function(isolationLevel, cb) {
|
2019-08-02 13:50:51 +00:00
|
|
|
const name = 'tx_' + transactionId++;
|
2015-05-15 17:27:08 +00:00
|
|
|
cb(null, new MockTransaction(this, name));
|
|
|
|
};
|
|
|
|
|
|
|
|
TestConnector.prototype.commit = function(tx, cb) {
|
|
|
|
tx.commit(cb);
|
|
|
|
};
|
|
|
|
|
|
|
|
TestConnector.prototype.rollback = function(tx, cb) {
|
|
|
|
tx.rollback(cb);
|
|
|
|
};
|
|
|
|
|
2015-05-13 17:14:44 +00:00
|
|
|
TestConnector.prototype.executeSQL = function(sql, params, options, callback) {
|
2019-08-02 13:50:51 +00:00
|
|
|
const transaction = options.transaction;
|
|
|
|
const model = options.model;
|
|
|
|
const useTransaction = transaction && transaction.connector === this &&
|
2017-08-23 14:35:19 +00:00
|
|
|
transaction.connection;
|
2019-08-02 13:50:51 +00:00
|
|
|
const data = useTransaction ? transaction.connection.data : this.data;
|
|
|
|
const name = useTransaction ? transaction.connection.name : undefined;
|
2017-08-23 14:35:19 +00:00
|
|
|
if (sql.indexOf('INSERT') === 0) {
|
|
|
|
data[model] = data[model] || [];
|
|
|
|
data[model].push({sql: sql, params: params});
|
|
|
|
debug('INSERT', data, sql, name);
|
|
|
|
callback(null, 1);
|
2015-05-15 17:27:08 +00:00
|
|
|
} else {
|
2017-08-23 14:35:19 +00:00
|
|
|
debug('SELECT', data, sql, name);
|
2019-08-02 13:50:51 +00:00
|
|
|
const instances = data[model] || [];
|
|
|
|
const result = sql.indexOf('count(*) as "cnt"') !== -1 ?
|
2017-08-23 14:35:19 +00:00
|
|
|
[{cnt: instances.length}] : instances;
|
|
|
|
callback(null, result);
|
2015-05-15 17:27:08 +00:00
|
|
|
}
|
2015-05-13 17:14:44 +00:00
|
|
|
};
|