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
* @private
*/
_quote: function(str) {
_quote(str) {
return (helpers.isString(str) && ! (str.startsWith(d.identifierStartChar) || str.endsWith(d.identifierEndChar)))
? `${d.identifierStartChar}${str}${d.identifierEndChar}`
: str;
@ -34,7 +34,7 @@ let d = {
* @param {Number|null} offset - Number of rows to skip
* @return {String} - Modified SQL statement
*/
limit: function(sql, limit, offset) {
limit(sql, limit, offset) {
sql += ` LIMIT ${limit}`;
if (helpers.isNumber(offset))
@ -51,7 +51,7 @@ let d = {
* @param {String} table - Table name to quote
* @return {String} - Quoted table name
*/
quoteTable: function(table) {
quoteTable(table) {
// Quote after prefix
return d.quoteIdentifiers(table);
},
@ -62,7 +62,7 @@ let d = {
* @param {String|String[]} - String or array of strings to quote identifiers
* @return {String|String[]} - Quoted identifier(s)
*/
quoteIdentifiers: function(str) {
quoteIdentifiers(str) {
let hiers, raw;
let pattern = new RegExp(
`${d.identifierStartChar}(`
@ -108,7 +108,7 @@ let d = {
* @param {String} table - Table to truncate
* @return {String} - Truncation SQL
*/
truncate: function(table) {
truncate(table) {
let sql = (d.hasTruncate)
? 'TRUNCATE '
: 'DELETE FROM ';
@ -125,7 +125,7 @@ let d = {
* @param {Array} [data] - The array of object containing data to insert
* @return {String} - Query and data to insert
*/
insertBatch: function(table, data) {
insertBatch(table, data) {
let vals = [],
fields = Object.keys(data[0]),
sql = "",
@ -145,13 +145,11 @@ let d = {
// object inserted
table = d.quoteTable(table);
sql += `INSERT INTO ${table} (`
+ d.quoteIdentifiers(fields).join(",")
+ ") VALUES ";
sql += `INSERT INTO ${table} (${d.quoteIdentifiers(fields).join(",")}) VALUES `;
// Create placeholder groups
params = Array(fields.length).fill('?');
paramString = "(" + params.join(',') + ")";
paramString = `(${params.join(',')})`;
paramList = Array(data.length).fill(paramString);
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";
let helpers = require('../helpers'),
Driver = require('../DriverClass');
class Firebird extends Driver {
constructor() {
super({
hasTruncate: false
});
}
var helpers = require('../helpers');
/**
* Set the limit clause
* Driver for Firebird databases
*
* @param {String} origSql - 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
* @module drivers/firebird
*/
limit(origSql, limit, offset) {
let sql = `FIRST ${limit}`;
module.exports = (function() {
delete require.cache[require.resolve('../Driver')];
var driver = require('../Driver');
driver.hasTruncate = false;
/**
* Generate a limit clause for firebird, which uses the syntax closest to the SQL standard
*
* @param {String} sql
* @param {Number} limit
* @param {Number} offset
* @return {String}
*/
driver.limit = function(origSql, limit, offset) {
var sql = 'FIRST ' + limit;
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
*
* @param {String} table - The table to insert to
* @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");
}
}
};
module.exports = new Firebird();
return driver;
}());

View File

@ -1,32 +1,30 @@
"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 {
constructor() {
super({
identifierStartChar: '`',
identifierEndChar: '`'
});
}
driver.identifierStartChar = '`';
driver.identifierEndChar = '`';
/**
* Set the limit clause
*
* @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
* Override default limit method because mysql likes to be different
*/
limit(sql, limit, offset) {
driver.limit = function(sql, limit, 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";
let Driver = require('../DriverClass');
/**
* Driver for PostgreSQL databases
*
* @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";
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 {
constructor() {
super({
hasTruncate: false
});
}
// Sqlite doesn't have a truncate command
driver.hasTruncate = false;
/**
* SQL to insert a group of rows
* Override default to have better compatibility
*
* @param {String} table - The table to insert to
* @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
// be parameterized
let sql = "",
var sql = "",
vals = [],
cols = [],
fields = [],
@ -30,27 +35,27 @@ class Sqlite extends Driver {
paramList = [];
data.forEach(obj => {
let row = [];
Object.keys(obj).forEach(key => {
data.forEach(function(obj) {
var row = [];
Object.keys(obj).forEach(function(key) {
row.push(obj[key]);
});
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
// object to be inserted
fields = Object.keys(first);
Object.keys(first).forEach(key => {
cols.push("'" + this._quote(first[key]) + "' AS " + this.quoteIdentifiers(key));
Object.keys(first).forEach(function(key) {
cols.push("'" + driver._quote(first[key]) + "' AS " + driver.quoteIdentifiers(key));
});
sql += "SELECT " + cols.join(', ') + "\n";
vals.forEach(row_values => {
let quoted = row_values.map(value => {
vals.forEach(function(row_values) {
var quoted = row_values.map(function(value) {
return String(value).replace("'", "'\'");
});
sql += "UNION ALL SELECT '" + quoted.join("', '") + "'\n";
@ -61,6 +66,6 @@ class Sqlite extends Driver {
values: null
};
}
}
module.exports = new Sqlite();
return driver;
}());