Revert to old driver setup, because it works

This commit is contained in:
Timothy Warren 2015-12-07 11:16:04 -05:00
parent f84bbaa477
commit 104e439230
6 changed files with 85 additions and 96 deletions

View File

@ -20,7 +20,7 @@ let d = {
* @return {String} - The quoted sql fragment * @return {String} - The quoted sql fragment
* @private * @private
*/ */
_quote: function(str) { _quote(str) {
return (helpers.isString(str) && ! (str.startsWith(d.identifierStartChar) || str.endsWith(d.identifierEndChar))) return (helpers.isString(str) && ! (str.startsWith(d.identifierStartChar) || str.endsWith(d.identifierEndChar)))
? `${d.identifierStartChar}${str}${d.identifierEndChar}` ? `${d.identifierStartChar}${str}${d.identifierEndChar}`
: str; : str;
@ -34,7 +34,7 @@ let d = {
* @param {Number|null} offset - Number of rows to skip * @param {Number|null} offset - Number of rows to skip
* @return {String} - Modified SQL statement * @return {String} - Modified SQL statement
*/ */
limit: function(sql, limit, offset) { limit(sql, limit, offset) {
sql += ` LIMIT ${limit}`; sql += ` LIMIT ${limit}`;
if (helpers.isNumber(offset)) if (helpers.isNumber(offset))
@ -51,7 +51,7 @@ let d = {
* @param {String} table - Table name to quote * @param {String} table - Table name to quote
* @return {String} - Quoted table name * @return {String} - Quoted table name
*/ */
quoteTable: function(table) { quoteTable(table) {
// Quote after prefix // Quote after prefix
return d.quoteIdentifiers(table); return d.quoteIdentifiers(table);
}, },
@ -62,7 +62,7 @@ let d = {
* @param {String|String[]} - String or array of strings to quote identifiers * @param {String|String[]} - String or array of strings to quote identifiers
* @return {String|String[]} - Quoted identifier(s) * @return {String|String[]} - Quoted identifier(s)
*/ */
quoteIdentifiers: function(str) { quoteIdentifiers(str) {
let hiers, raw; let hiers, raw;
let pattern = new RegExp( let pattern = new RegExp(
`${d.identifierStartChar}(` `${d.identifierStartChar}(`
@ -108,7 +108,7 @@ let d = {
* @param {String} table - Table to truncate * @param {String} table - Table to truncate
* @return {String} - Truncation SQL * @return {String} - Truncation SQL
*/ */
truncate: function(table) { truncate(table) {
let sql = (d.hasTruncate) let sql = (d.hasTruncate)
? 'TRUNCATE ' ? 'TRUNCATE '
: 'DELETE FROM '; : 'DELETE FROM ';
@ -125,7 +125,7 @@ let d = {
* @param {Array} [data] - The array of object containing data to insert * @param {Array} [data] - The array of object containing data to insert
* @return {String} - Query and data to insert * @return {String} - Query and data to insert
*/ */
insertBatch: function(table, data) { insertBatch(table, data) {
let vals = [], let vals = [],
fields = Object.keys(data[0]), fields = Object.keys(data[0]),
sql = "", sql = "",
@ -145,13 +145,11 @@ let d = {
// object inserted // object inserted
table = d.quoteTable(table); table = d.quoteTable(table);
sql += `INSERT INTO ${table} (` sql += `INSERT INTO ${table} (${d.quoteIdentifiers(fields).join(",")}) VALUES `;
+ d.quoteIdentifiers(fields).join(",")
+ ") VALUES ";
// Create placeholder groups // Create placeholder groups
params = Array(fields.length).fill('?'); params = Array(fields.length).fill('?');
paramString = "(" + params.join(',') + ")"; paramString = `(${params.join(',')})`;
paramList = Array(data.length).fill(paramString); paramList = Array(data.length).fill(paramString);
sql += paramList.join(','); sql += paramList.join(',');

View File

@ -1,18 +0,0 @@
'use strict';
let driverBase = require('./DriverBase'),
getArgs = require('getargs');
module.exports = class DriverClass {
constructor(/* properties:object */) {
let args = getArgs('[properties]:object', arguments);
args.properties = args.properties || {};
Object.keys(driverBase).forEach(key => {
this[key] = (Object.keys(args.properties).indexOf(key) !== -1)
? args.properties[key]
: driverBase[key];
});
}
}

View File

@ -1,44 +1,47 @@
"use strict"; "use strict";
let helpers = require('../helpers'), var helpers = require('../helpers');
Driver = require('../DriverClass');
class Firebird extends Driver { /**
constructor() { * Driver for Firebird databases
super({ *
hasTruncate: false * @module drivers/firebird
}); */
} module.exports = (function() {
delete require.cache[require.resolve('../Driver')];
var driver = require('../Driver');
driver.hasTruncate = false;
/** /**
* Set the limit clause * Generate a limit clause for firebird, which uses the syntax closest to the SQL standard
* *
* @param {String} origSql - SQL statement to modify * @param {String} sql
* @param {Number} limit - Maximum number of rows to fetch * @param {Number} limit
* @param {Number|null} offset - Number of rows to skip * @param {Number} offset
* @return {String} - Modified SQL statement * @return {String}
*/ */
limit(origSql, limit, offset) { driver.limit = function(origSql, limit, offset) {
let sql = `FIRST ${limit}`; var sql = 'FIRST ' + limit;
if (helpers.isNumber(offset)) if (helpers.isNumber(offset))
{ {
sql += ` SKIP ${offset}`; sql += ' SKIP ' + offset;
} }
return origSql.replace(/SELECT/i, "SELECT " + sql); return origSql.replace(/SELECT/i, "SELECT " + sql);;
} };
/** /**
* SQL to insert a group of rows * SQL to insert a group of rows
* *
* @param {String} table - The table to insert to * @param {String} table - The table to insert to
* @param {Array} [data] - The array of object containing data to insert * @param {Array} [data] - The array of object containing data to insert
* @return {String} - Query and data to insert * @return {String}
*/ */
insertBatch() { driver.insertBatch = function(table, data) {
throw new Error("Not Implemented"); throw new Error("Not Implemented");
} };
}
module.exports = new Firebird(); return driver;
}());

View File

@ -1,32 +1,30 @@
"use strict"; "use strict";
let helpers = require('../helpers'), /**
Driver = require('../DriverClass'); * Driver for MySQL databases
*
* @module drivers/mysql
*/
module.exports = (function() {
delete require.cache[require.resolve('../Driver')];
var driver = require('../Driver'),
helpers = require('../helpers');
class Mysql extends Driver { driver.identifierStartChar = '`';
constructor() { driver.identifierEndChar = '`';
super({
identifierStartChar: '`',
identifierEndChar: '`'
});
}
/** /**
* Set the limit clause * Override default limit method because mysql likes to be different
*
* @param {String} sql - SQL statement to modify
* @param {Number} limit - Maximum number of rows to fetch
* @param {Number|null} offset - Number of rows to skip
* @return {String} - Modified SQL statement
*/ */
limit(sql, limit, offset) { driver.limit = function(sql, limit, offset) {
if ( ! helpers.isNumber(offset)) if ( ! helpers.isNumber(offset))
{ {
return sql += ` LIMIT ${limit}`; return sql += " LIMIT " + limit;
} }
return sql += ` LIMIT ${offset}, ${limit}`; return sql += " LIMIT " + offset + "," + limit;
} };
}
module.exports = new Mysql(); return driver;
}());

View File

@ -1,10 +1,13 @@
"use strict"; "use strict";
let Driver = require('../DriverClass');
/** /**
* Driver for PostgreSQL databases * Driver for PostgreSQL databases
* *
* @module drivers/pg * @module drivers/pg
*/ */
module.exports = new Driver(); module.exports = (function() {
delete require.cache[require.resolve('../Driver')];
var driver = require('../Driver');
return driver;
}());

View File

@ -1,26 +1,31 @@
"use strict"; "use strict";
let helpers = require('../helpers'), /**
Driver = require('../DriverClass'); * Driver for Sqlite databases
*
* @module drivers/sqlite
*/
module.exports = (function() {
delete require.cache[require.resolve('../Driver')];
var driver = require('../Driver'),
helpers = require('../helpers');
class Sqlite extends Driver { // Sqlite doesn't have a truncate command
constructor() { driver.hasTruncate = false;
super({
hasTruncate: false
});
}
/** /**
* SQL to insert a group of rows * SQL to insert a group of rows
* Override default to have better compatibility
* *
* @param {String} table - The table to insert to * @param {String} table - The table to insert to
* @param {Array} [data] - The array of object containing data to insert * @param {Array} [data] - The array of object containing data to insert
* @return {String} - Query and data to insert * @return {String}
*/ */
insertBatch(table, data) { driver.insertBatch = function(table, data) {
// Get the data values to insert, so they can // Get the data values to insert, so they can
// be parameterized // be parameterized
let sql = "", var sql = "",
vals = [], vals = [],
cols = [], cols = [],
fields = [], fields = [],
@ -30,27 +35,27 @@ class Sqlite extends Driver {
paramList = []; paramList = [];
data.forEach(obj => { data.forEach(function(obj) {
let row = []; var row = [];
Object.keys(obj).forEach(key => { Object.keys(obj).forEach(function(key) {
row.push(obj[key]); row.push(obj[key]);
}); });
vals.push(row); vals.push(row);
}); });
sql += "INSERT INTO " + this.quoteTable(table) + "\n"; sql += "INSERT INTO " + driver.quoteTable(table) + "\n";
// Get the field names from the keys of the first // Get the field names from the keys of the first
// object to be inserted // object to be inserted
fields = Object.keys(first); fields = Object.keys(first);
Object.keys(first).forEach(key => { Object.keys(first).forEach(function(key) {
cols.push("'" + this._quote(first[key]) + "' AS " + this.quoteIdentifiers(key)); cols.push("'" + driver._quote(first[key]) + "' AS " + driver.quoteIdentifiers(key));
}); });
sql += "SELECT " + cols.join(', ') + "\n"; sql += "SELECT " + cols.join(', ') + "\n";
vals.forEach(row_values => { vals.forEach(function(row_values) {
let quoted = row_values.map(value => { var quoted = row_values.map(function(value) {
return String(value).replace("'", "'\'"); return String(value).replace("'", "'\'");
}); });
sql += "UNION ALL SELECT '" + quoted.join("', '") + "'\n"; sql += "UNION ALL SELECT '" + quoted.join("', '") + "'\n";
@ -61,6 +66,6 @@ class Sqlite extends Driver {
values: null values: null
}; };
} }
}
module.exports = new Sqlite(); return driver;
}());