mirror of
https://github.com/knex/knex.git
synced 2025-12-27 06:58:39 +00:00
Nicer query builder interface
This commit is contained in:
parent
17b04db6da
commit
2e47b9d2e8
@ -70,7 +70,7 @@ assign(Client_MariaSQL.prototype, {
|
||||
return new Promise(function(resolver, rejecter) {
|
||||
if (!obj.sql) return resolver()
|
||||
var rows = [];
|
||||
var query = connection.query(SqlString.format(obj.sql, obj.bindings, false, tz), [])
|
||||
var query = connection.query(SqlString.format(obj.sql, obj.bindings, tz), [])
|
||||
query.on('result', function(result) {
|
||||
result.on('row', rowHandler(function(row) { rows.push(row); }))
|
||||
.on('end', function(data) {
|
||||
|
||||
@ -20,7 +20,7 @@ module.exports = function(Target) {
|
||||
if (this.client && this.client.prepBindings) {
|
||||
bindings = this.client.prepBindings(bindings, tz);
|
||||
}
|
||||
return SqlString.format(sql, bindings, true, tz);
|
||||
return SqlString.format(sql, bindings, tz);
|
||||
};
|
||||
|
||||
// Create a new instance of the `Runner`, passing in the current object.
|
||||
|
||||
1350
lib/query/builder.js
1350
lib/query/builder.js
File diff suppressed because it is too large
Load Diff
@ -1,21 +1,9 @@
|
||||
'use strict'
|
||||
|
||||
var SqlString = exports;
|
||||
var helpers = require('../helpers')
|
||||
|
||||
SqlString.escapeId = function (val, forbidQualified) {
|
||||
if (Array.isArray(val)) {
|
||||
return val.map(function(v) {
|
||||
return SqlString.escapeId(v, forbidQualified);
|
||||
}).join(', ');
|
||||
}
|
||||
|
||||
if (forbidQualified) {
|
||||
return '`' + val.replace(/`/g, '``') + '`';
|
||||
}
|
||||
return '`' + val.replace(/`/g, '``').replace(/\./g, '`.`') + '`';
|
||||
};
|
||||
|
||||
SqlString.escape = function(val, stringifyObjects, timeZone) {
|
||||
SqlString.escape = function(val, timeZone) {
|
||||
if (val == null) {
|
||||
return 'NULL';
|
||||
}
|
||||
@ -38,10 +26,11 @@ SqlString.escape = function(val, stringifyObjects, timeZone) {
|
||||
}
|
||||
|
||||
if (typeof val === 'object') {
|
||||
if (stringifyObjects) {
|
||||
val = val.toString();
|
||||
} else {
|
||||
return SqlString.objectToValues(val, timeZone);
|
||||
try {
|
||||
val = JSON.stringify(val)
|
||||
} catch (e) {
|
||||
helpers.warn(e)
|
||||
val = val + ''
|
||||
}
|
||||
}
|
||||
|
||||
@ -62,23 +51,19 @@ SqlString.escape = function(val, stringifyObjects, timeZone) {
|
||||
SqlString.arrayToList = function(array, timeZone) {
|
||||
return array.map(function(v) {
|
||||
if (Array.isArray(v)) return '(' + SqlString.arrayToList(v, timeZone) + ')';
|
||||
return SqlString.escape(v, true, timeZone);
|
||||
return SqlString.escape(v, timeZone);
|
||||
}).join(', ');
|
||||
};
|
||||
|
||||
SqlString.format = function(sql, values, stringifyObjects, timeZone) {
|
||||
SqlString.format = function(sql, values, timeZone) {
|
||||
values = values == null ? [] : [].concat(values);
|
||||
|
||||
var index = 0;
|
||||
return sql.replace(/\?\??/g, function(match) {
|
||||
return sql.replace(/\?/g, function(match) {
|
||||
if (index === values.length) {
|
||||
return match;
|
||||
}
|
||||
|
||||
var value = values[index++];
|
||||
|
||||
return match === '??' ? SqlString.escapeId(value) :
|
||||
SqlString.escape(value, stringifyObjects, timeZone);
|
||||
return SqlString.escape(value, timeZone)
|
||||
});
|
||||
};
|
||||
|
||||
@ -107,21 +92,7 @@ SqlString.dateToString = function(date, timeZone) {
|
||||
|
||||
SqlString.bufferToString = function bufferToString(buffer) {
|
||||
return "X'" + buffer.toString('hex') + "'";
|
||||
};
|
||||
|
||||
SqlString.objectToValues = function(object, timeZone) {
|
||||
var values = [];
|
||||
for (var key in object) {
|
||||
var value = object[key];
|
||||
if(typeof value === 'function') {
|
||||
continue;
|
||||
}
|
||||
|
||||
values.push(this.escapeId(key) + ' = ' + SqlString.escape(value, true, timeZone));
|
||||
}
|
||||
|
||||
return values.join(', ');
|
||||
};
|
||||
}
|
||||
|
||||
function zeroPad(number, length) {
|
||||
number = number.toString();
|
||||
|
||||
@ -77,9 +77,6 @@ module.exports = function(knex) {
|
||||
expect(msg).to.equal(err);
|
||||
return knex('accounts').where('id', id).select('first_name');
|
||||
}).then(function(resp) {
|
||||
|
||||
console.log(resp)
|
||||
|
||||
expect(resp.length).to.equal(0);
|
||||
});
|
||||
});
|
||||
|
||||
@ -4,13 +4,8 @@ var tape = require('tape')
|
||||
var QueryBuilder = require('../../lib/query/builder')
|
||||
|
||||
tape('accumulates multiple update calls #647', function(t) {
|
||||
|
||||
t.plan(1)
|
||||
|
||||
var qb = new QueryBuilder({})
|
||||
|
||||
qb.update('a', 1).update('b', 2)
|
||||
|
||||
t.deepEqual(qb._single.update, {a: 1, b: 2})
|
||||
|
||||
})
|
||||
})
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user