Comparing version 0.1.24 to 0.1.25
@@ -10,12 +10,12 @@ exports.CreateQuery = CreateQuery; | ||
var tmpl = function (template, args) { | ||
var has = {}.hasOwnProperty; | ||
for (var key in args) { | ||
if (!has.call(args, key)) { | ||
continue; | ||
} | ||
var token = '{{' + key + '}}'; | ||
template = template.split(token).join(args[key]); | ||
} | ||
var has = {}.hasOwnProperty; | ||
for (var key in args) { | ||
if (!has.call(args, key)) { | ||
continue; | ||
} | ||
var token = '{{' + key + '}}'; | ||
template = template.split(token).join(args[key]); | ||
} | ||
return template; | ||
return template; | ||
}; | ||
@@ -29,16 +29,16 @@ /** | ||
var buildFieldsList = function (dialect, structure) { | ||
if (!structure) { | ||
return ""; | ||
} | ||
var tpl = "'{{NAME}}' {{TYPE}}", fields = [], has = {}.hasOwnProperty; | ||
for (var field in structure) { | ||
if (has.call(structure, field)) { | ||
fields.push(tmpl(tpl, { | ||
NAME: field, | ||
TYPE: dialect.DataTypes[structure[field]] | ||
})); | ||
} | ||
} | ||
if (!structure) { | ||
return ""; | ||
} | ||
var tpl = "'{{NAME}}' {{TYPE}}", fields = [], has = {}.hasOwnProperty; | ||
for (var field in structure) { | ||
if (has.call(structure, field)) { | ||
fields.push(tmpl(tpl, { | ||
NAME: field, | ||
TYPE: dialect.DataTypes[structure[field]] | ||
})); | ||
} | ||
} | ||
return fields.join(','); | ||
return fields.join(','); | ||
}; | ||
@@ -54,62 +54,62 @@ | ||
function CreateQuery(Dialect, opts) { | ||
var sql = {}; | ||
var tableName = null; | ||
var structure = {}; | ||
var sql = {}; | ||
var tableName = null; | ||
var structure = {}; | ||
return { | ||
/** | ||
* Set the table name | ||
* @param table_name | ||
* @returns {*} | ||
*/ | ||
table: function (table_name) { | ||
tableName = table_name; | ||
return { | ||
/** | ||
* Set the table name | ||
* @param table_name | ||
* @returns {*} | ||
*/ | ||
table: function (table_name) { | ||
tableName = table_name; | ||
return this; | ||
}, | ||
/** | ||
* Add a field | ||
* @param name | ||
* @param type | ||
* @returns {Object} | ||
*/ | ||
field: function (name, type) { | ||
structure[name] = type; | ||
return this; | ||
}, | ||
/** | ||
* Add a field | ||
* @param name | ||
* @param type | ||
* @returns {Object} | ||
*/ | ||
field: function (name, type) { | ||
structure[name] = type; | ||
return this; | ||
}, | ||
/** | ||
* Set all the fields | ||
* @param fields | ||
* @returns {Object} | ||
*/ | ||
fields: function (fields) { | ||
if (!fields) { | ||
return structure; | ||
} | ||
structure = fields; | ||
return this; | ||
}, | ||
/** | ||
* Set all the fields | ||
* @param fields | ||
* @returns {Object} | ||
*/ | ||
fields: function (fields) { | ||
if (!fields) { | ||
return structure; | ||
} | ||
structure = fields; | ||
return this; | ||
}, | ||
return this; | ||
}, | ||
/** | ||
* Build a query from the passed params | ||
* @returns {string} | ||
*/ | ||
build: function () { | ||
var query, fieldsList, template = "CREATE TABLE '{{TABLE_NAME}}'({{FIELDS_LIST}})"; | ||
/** | ||
* Build a query from the passed params | ||
* @returns {string} | ||
*/ | ||
build: function () { | ||
var query, fieldsList, template = "CREATE TABLE '{{TABLE_NAME}}'({{FIELDS_LIST}})"; | ||
if(!tableName){ | ||
return ''; | ||
} | ||
if(!tableName){ | ||
return ''; | ||
} | ||
fieldsList = buildFieldsList(Dialect, structure); | ||
fieldsList = buildFieldsList(Dialect, structure); | ||
return tmpl(template, { | ||
TABLE_NAME: tableName, | ||
FIELDS_LIST: fieldsList | ||
}); | ||
return tmpl(template, { | ||
TABLE_NAME: tableName, | ||
FIELDS_LIST: fieldsList | ||
}); | ||
} | ||
}; | ||
} | ||
}; | ||
} |
@@ -5,7 +5,7 @@ var util = require("util"); | ||
exports.DataTypes = { | ||
id: 'INT IDENTITY(1,1) NOT NULL PRIMARY KEY', | ||
int: 'INT', | ||
float: 'FLOAT', | ||
bool: 'BIT', | ||
text: 'TEXT' | ||
id: 'INT IDENTITY(1,1) NOT NULL PRIMARY KEY', | ||
int: 'INT', | ||
float: 'FLOAT', | ||
bool: 'BIT', | ||
text: 'TEXT' | ||
}; | ||
@@ -29,3 +29,3 @@ | ||
} | ||
return "[" + el + "]"; | ||
return "[" + el + "]"; | ||
}).join("."); | ||
@@ -32,0 +32,0 @@ }; |
@@ -5,7 +5,7 @@ var util = require("util"); | ||
exports.DataTypes = { | ||
id: 'INTEGER PRIMARY KEY AUTO_INCREMENT', | ||
int: 'INTEGER', | ||
float: 'FLOAT(12,2)', | ||
bool: 'TINYINT(1)', | ||
text: 'TEXT' | ||
id: 'INTEGER PRIMARY KEY AUTO_INCREMENT', | ||
int: 'INTEGER', | ||
float: 'FLOAT(12,2)', | ||
bool: 'TINYINT(1)', | ||
text: 'TEXT' | ||
}; | ||
@@ -12,0 +12,0 @@ |
@@ -5,7 +5,7 @@ var util = require("util"); | ||
exports.DataTypes = { | ||
id: 'SERIAL PRIMARY KEY', | ||
int: 'INTEGER', | ||
float: 'REAL', | ||
bool: 'SMALLINT', | ||
text: 'TEXT' | ||
id: 'SERIAL PRIMARY KEY', | ||
int: 'INTEGER', | ||
float: 'REAL', | ||
bool: 'SMALLINT', | ||
text: 'TEXT' | ||
}; | ||
@@ -12,0 +12,0 @@ |
@@ -6,8 +6,8 @@ var util = require("util"); | ||
exports.DataTypes = { | ||
isSQLITE: true, | ||
id: 'INTEGER PRIMARY KEY AUTOINCREMENT', | ||
int: 'INTEGER', | ||
float: 'FLOAT(12,2)', | ||
bool: 'TINYINT(1)', | ||
text: 'TEXT' | ||
isSQLITE: true, | ||
id: 'INTEGER PRIMARY KEY AUTOINCREMENT', | ||
int: 'INTEGER', | ||
float: 'FLOAT(12,2)', | ||
bool: 'TINYINT(1)', | ||
text: 'TEXT' | ||
}; | ||
@@ -14,0 +14,0 @@ |
@@ -19,47 +19,47 @@ | ||
module.exports.dateToString = function (date, timeZone, opts) { | ||
var dt = new Date(date); | ||
var dt = new Date(date); | ||
if (timeZone != 'local') { | ||
var tz = convertTimezone(timeZone); | ||
if (timeZone != 'local') { | ||
var tz = convertTimezone(timeZone); | ||
dt.setTime(dt.getTime() + (dt.getTimezoneOffset() * 60000)); | ||
if (tz !== false) { | ||
dt.setTime(dt.getTime() + (tz * 60000)); | ||
} | ||
} | ||
dt.setTime(dt.getTime() + (dt.getTimezoneOffset() * 60000)); | ||
if (tz !== false) { | ||
dt.setTime(dt.getTime() + (tz * 60000)); | ||
} | ||
} | ||
var year = dt.getFullYear(); | ||
var month = zeroPad(dt.getMonth() + 1); | ||
var day = zeroPad(dt.getDate()); | ||
var hour = zeroPad(dt.getHours()); | ||
var minute = zeroPad(dt.getMinutes()); | ||
var second = zeroPad(dt.getSeconds()); | ||
var milli = zeroPad(dt.getMilliseconds(), 3); | ||
var year = dt.getFullYear(); | ||
var month = zeroPad(dt.getMonth() + 1); | ||
var day = zeroPad(dt.getDate()); | ||
var hour = zeroPad(dt.getHours()); | ||
var minute = zeroPad(dt.getMinutes()); | ||
var second = zeroPad(dt.getSeconds()); | ||
var milli = zeroPad(dt.getMilliseconds(), 3); | ||
if (opts.dialect == 'mysql') { | ||
return year + '-' + month + '-' + day + ' ' + hour + ':' + minute + ':' + second + '.' + milli; | ||
} else { | ||
return year + '-' + month + '-' + day + 'T' + hour + ':' + minute + ':' + second + '.' + milli + 'Z'; | ||
} | ||
if (opts.dialect == 'mysql') { | ||
return year + '-' + month + '-' + day + ' ' + hour + ':' + minute + ':' + second + '.' + milli; | ||
} else { | ||
return year + '-' + month + '-' + day + 'T' + hour + ':' + minute + ':' + second + '.' + milli + 'Z'; | ||
} | ||
} | ||
function zeroPad(number, n) { | ||
if (arguments.length == 1) n = 2; | ||
if (arguments.length == 1) n = 2; | ||
number = "" + number; | ||
number = "" + number; | ||
while (number.length < n) { | ||
number = "0" + number; | ||
} | ||
return number; | ||
while (number.length < n) { | ||
number = "0" + number; | ||
} | ||
return number; | ||
} | ||
function convertTimezone(tz) { | ||
if (tz == "Z") return 0; | ||
if (tz == "Z") return 0; | ||
var m = tz.match(/([\+\-\s])(\d\d):?(\d\d)?/); | ||
if (m) { | ||
return (m[1] == '-' ? -1 : 1) * (parseInt(m[2], 10) + ((m[3] ? parseInt(m[3], 10) : 0) / 60)) * 60; | ||
} | ||
return false; | ||
var m = tz.match(/([\+\-\s])(\d\d):?(\d\d)?/); | ||
if (m) { | ||
return (m[1] == '-' ? -1 : 1) * (parseInt(m[2], 10) + ((m[3] ? parseInt(m[3], 10) : 0) / 60)) * 60; | ||
} | ||
return false; | ||
} |
@@ -30,5 +30,5 @@ var CreateQuery = require("./Create").CreateQuery; | ||
escapeVal : Dialect.escapeVal.bind(Dialect), | ||
create: function(){ | ||
return new CreateQuery(Dialect, opts); | ||
}, | ||
create: function(){ | ||
return new CreateQuery(Dialect, opts); | ||
}, | ||
select: function () { | ||
@@ -35,0 +35,0 @@ return new SelectQuery(Dialect, opts); |
@@ -95,3 +95,3 @@ var Helpers = require('./Helpers'); | ||
}, | ||
from: function (table, from_id, to_table, to_id) { | ||
from: function (table, from_id, to_table, to_id, fromOpts) { | ||
var from = { | ||
@@ -108,8 +108,15 @@ t: table, // table | ||
var a, f = from_id, t; | ||
if (arguments.length == 3) { | ||
a = sql.from[sql.from.length - 1].a; | ||
t = to_table; | ||
var args = Array.prototype.slice.call(arguments); | ||
var last = args[args.length - 1]; | ||
if (typeof last == 'object' && !Array.isArray(last)) { | ||
from.opts = args.pop(); | ||
} | ||
if (args.length == 3) { | ||
a = sql.from[sql.from.length - 1].a; | ||
t = to_table; | ||
} else { | ||
a = get_table_alias(to_table); | ||
t = to_id; | ||
a = get_table_alias(to_table); | ||
t = to_id; | ||
} | ||
@@ -119,11 +126,11 @@ | ||
if (f.length && t.length) { | ||
if (Array.isArray(f) && Array.isArray(t) && f.length == t.length) { | ||
for (i = 0; i < f.length; i++) { | ||
from.j.push([f[i], a, t[i]]); | ||
} | ||
} else { | ||
from.j.push([f, a, t]); | ||
} | ||
if (Array.isArray(f) && Array.isArray(t) && f.length == t.length) { | ||
for (i = 0; i < f.length; i++) { | ||
from.j.push([f[i], a, t[i]]); | ||
} | ||
} else { | ||
from.j.push([f, a, t]); | ||
} | ||
} else { | ||
throw new Error(); | ||
throw new Error(); | ||
} | ||
@@ -200,3 +207,3 @@ | ||
build: function () { | ||
var query = [], tmp, i, ii, j, ord, str, select_all = true; | ||
var query = [], tmp, from, i, ii, j, ord, str, select_all = true; | ||
var having = []; | ||
@@ -325,26 +332,31 @@ | ||
for (i = 0; i < sql.from.length; i++) { | ||
from = sql.from[i]; | ||
if (i > 0) { | ||
if (from.opts && from.opts.joinType) { | ||
query.push(from.opts.joinType.toUpperCase()); | ||
} | ||
query.push("JOIN"); | ||
} | ||
if (sql.from.length == 1 && !sql.where_exists) { | ||
query.push(Dialect.escapeId(sql.from[i].t)); | ||
query.push(Dialect.escapeId(from.t)); | ||
} else { | ||
query.push(Dialect.escapeId(sql.from[i].t) + " " + Dialect.escapeId(sql.from[i].a)); | ||
query.push(Dialect.escapeId(from.t) + " " + Dialect.escapeId(from.a)); | ||
} | ||
if (i > 0) { | ||
query.push("ON"); | ||
query.push("ON"); | ||
for (ii = 0; ii < sql.from[i].j.length; ii++) { | ||
if (ii > 0) { | ||
query.push("AND"); | ||
} | ||
query.push( | ||
Dialect.escapeId(sql.from[i].a, sql.from[i].j[ii][0]) + | ||
" = " + | ||
Dialect.escapeId(sql.from[i].j[ii][1], sql.from[i].j[ii][2]) | ||
); | ||
} | ||
for (ii = 0; ii < from.j.length; ii++) { | ||
if (ii > 0) { | ||
query.push("AND"); | ||
} | ||
query.push( | ||
Dialect.escapeId(from.a, from.j[ii][0]) + | ||
" = " + | ||
Dialect.escapeId(from.j[ii][1], from.j[ii][2]) | ||
); | ||
} | ||
if (i < sql.from.length - 1) { | ||
query.push(")"); | ||
query.push(")"); | ||
} | ||
@@ -351,0 +363,0 @@ } |
@@ -29,14 +29,14 @@ var Helpers = require('./Helpers'); | ||
opts = opts || {}; | ||
if (where.e) { | ||
// EXISTS | ||
// EXISTS | ||
wheres = []; | ||
if(Array.isArray(where.e.l[0]) && Array.isArray(where.e.l[1])) { | ||
for (i = 0; i < where.e.l[0].length; i++) { | ||
wheres.push(Dialect.escapeId(where.e.l[0][i]) + " = " + Dialect.escapeId(where.e.tl, where.e.l[1][i])); | ||
} | ||
} else { | ||
wheres.push(Dialect.escapeId(where.e.l[0]) + " = " + Dialect.escapeId(where.e.tl, where.e.l[1])); | ||
} | ||
wheres = []; | ||
if(Array.isArray(where.e.l[0]) && Array.isArray(where.e.l[1])) { | ||
for (i = 0; i < where.e.l[0].length; i++) { | ||
wheres.push(Dialect.escapeId(where.e.l[0][i]) + " = " + Dialect.escapeId(where.e.tl, where.e.l[1][i])); | ||
} | ||
} else { | ||
wheres.push(Dialect.escapeId(where.e.l[0]) + " = " + Dialect.escapeId(where.e.tl, where.e.l[1])); | ||
} | ||
@@ -48,3 +48,3 @@ return [ | ||
"AND " + buildOrGroup(Dialect, { t: null, w: where.w }, opts) + | ||
")" | ||
")" | ||
]; | ||
@@ -51,0 +51,0 @@ } |
@@ -9,3 +9,3 @@ { | ||
], | ||
"version": "0.1.24", | ||
"version": "0.1.25", | ||
"license": "MIT", | ||
@@ -12,0 +12,0 @@ "repository": { |
@@ -96,1 +96,6 @@ var common = require('../common'); | ||
); | ||
assert.equal( | ||
dialect.defaultValuesStmt, | ||
"DEFAULT VALUES" | ||
); |
@@ -96,1 +96,6 @@ var common = require('../common'); | ||
); | ||
assert.equal( | ||
dialect.defaultValuesStmt, | ||
"VALUES()" | ||
); |
@@ -101,1 +101,6 @@ var common = require('../common'); | ||
); | ||
assert.equal( | ||
dialect.defaultValuesStmt, | ||
"DEFAULT VALUES" | ||
); |
@@ -96,1 +96,6 @@ var common = require('../common'); | ||
); | ||
assert.equal( | ||
dialect.defaultValuesStmt, | ||
"DEFAULT VALUES" | ||
); |
@@ -10,2 +10,7 @@ var common = require('../common'); | ||
assert.equal( | ||
common.Insert().into('table1').set({}).build(), | ||
"INSERT INTO `table1` VALUES()" | ||
); | ||
assert.equal( | ||
common.Insert().into('table1').set({ col: 1 }).build(), | ||
@@ -12,0 +17,0 @@ "INSERT INTO `table1` (`col`) VALUES (1)" |
@@ -63,2 +63,9 @@ var common = require('../common'); | ||
assert.equal( | ||
common.Select().from('table1').select('id1') | ||
.from('table2', 'id2', 'id1', { joinType: 'left inner' }).select('id2').build(), | ||
"SELECT `t1`.`id1`, `t2`.`id2` FROM `table1` `t1` LEFT INNER JOIN `table2` `t2` ON `t2`.`id2` = `t1`.`id1`" | ||
) | ||
assert.equal( | ||
common.Select().from('table1').select('id1', 'name') | ||
@@ -106,5 +113,5 @@ .from('table2', 'id2', 'table1', 'id1').select('id2').build(), | ||
assert.equal( | ||
common.Select().from('table1') | ||
.from('table2',['id2a', 'id2b'], 'table1', ['id1a', 'id1b']).count('id').build(), | ||
"SELECT COUNT(`t2`.`id`) FROM `table1` `t1` JOIN `table2` `t2` ON `t2`.`id2a` = `t1`.`id1a` AND `t2`.`id2b` = `t1`.`id1b`" | ||
common.Select().from('table1') | ||
.from('table2',['id2a', 'id2b'], 'table1', ['id1a', 'id1b']).count('id').build(), | ||
"SELECT COUNT(`t2`.`id`) FROM `table1` `t1` JOIN `table2` `t2` ON `t2`.`id2a` = `t1`.`id1a` AND `t2`.`id2b` = `t1`.`id1b`" | ||
); |
Sorry, the diff of this file is not supported yet
New author
Supply chain riskA new npm collaborator published a version of the package for the first time. New collaborators are usually benign additions to a project, but do indicate a change to the security surface area of a package.
Found 1 instance in 1 package
61847
42
2118
1