knex/lib/dialects/sqlite3/query/compiler.js

120 lines
3.6 KiB
JavaScript
Raw Normal View History

// SQLite3 Query Builder & Compiler
2015-05-09 14:01:19 -04:00
'use strict';
2015-05-09 13:58:18 -04:00
var _ = require('lodash');
var inherits = require('inherits');
var QueryCompiler = require('../../../query/compiler');
var assign = require('lodash/object/assign');
function QueryCompiler_SQLite3(client, builder) {
2015-05-09 13:58:18 -04:00
QueryCompiler.call(this, client, builder);
}
2015-05-09 13:58:18 -04:00
inherits(QueryCompiler_SQLite3, QueryCompiler);
assign(QueryCompiler_SQLite3.prototype, {
// The locks are not applicable in SQLite3
2015-05-09 13:58:18 -04:00
forShare: emptyStr,
forUpdate: emptyStr,
// SQLite requires us to build the multi-row insert as a listing of select with
// unions joining them together. So we'll build out this list of columns and
// then join them all together with select unions to complete the queries.
2015-05-09 13:58:18 -04:00
insert: function insert() {
var insertValues = this.single.insert || [];
var sql = 'insert into ' + this.tableName + ' ';
if (Array.isArray(insertValues)) {
if (insertValues.length === 0) {
2015-05-09 13:58:18 -04:00
return '';
} else if (insertValues.length === 1 && insertValues[0] && _.isEmpty(insertValues[0])) {
return sql + this._emptyInsertValue;
}
} else if (typeof insertValues === 'object' && _.isEmpty(insertValues)) {
2015-05-09 13:58:18 -04:00
return sql + this._emptyInsertValue;
}
2015-05-09 13:58:18 -04:00
var insertData = this._prepInsert(insertValues);
if (_.isString(insertData)) {
2015-05-09 13:58:18 -04:00
return sql + insertData;
}
2015-05-09 13:58:18 -04:00
if (insertData.columns.length === 0) {
return '';
}
2015-05-09 13:58:18 -04:00
sql += '(' + this.formatter.columnize(insertData.columns) + ')';
if (insertData.values.length === 1) {
2015-05-09 13:58:18 -04:00
return sql + ' values (' + this.formatter.parameterize(insertData.values[0]) + ')';
}
2015-05-09 13:58:18 -04:00
var blocks = [];
var i = -1;
while (++i < insertData.values.length) {
2015-05-09 13:58:18 -04:00
var i2 = -1,
block = blocks[i] = [];
var current = insertData.values[i];
while (++i2 < insertData.columns.length) {
2015-05-09 13:58:18 -04:00
block.push(this.formatter.alias(this.formatter.parameter(current[i2]), this.formatter.wrap(insertData.columns[i2])));
}
2015-05-09 13:58:18 -04:00
blocks[i] = block.join(', ');
}
2015-05-09 13:58:18 -04:00
return sql + ' select ' + blocks.join(' union all select ');
},
// Compile a truncate table statement into SQL.
2015-05-09 13:58:18 -04:00
truncate: function truncate() {
var table = this.tableName;
return {
sql: 'delete from ' + table,
2015-05-09 13:58:18 -04:00
output: function output() {
return this.query({ sql: 'delete from sqlite_sequence where name = ' + table })['catch'](function () {});
}
2015-05-09 13:58:18 -04:00
};
},
// Compiles a `columnInfo` query
2015-05-09 13:58:18 -04:00
columnInfo: function columnInfo() {
var column = this.single.columnInfo;
return {
2015-05-09 13:58:18 -04:00
sql: 'PRAGMA table_info(' + this.single.table + ')',
output: function output(resp) {
var maxLengthRegex = /.*\((\d+)\)/;
var out = _.reduce(resp, function (columns, val) {
2015-05-09 13:58:18 -04:00
var type = val.type;
var maxLength = (maxLength = type.match(maxLengthRegex)) && maxLength[1];
type = maxLength ? type.split('(')[0] : type;
columns[val.name] = {
type: type.toLowerCase(),
maxLength: maxLength,
nullable: !val.notnull,
defaultValue: val.dflt_value
2015-05-09 13:58:18 -04:00
};
return columns;
}, {});
return column && out[column] || out;
}
2015-05-09 13:58:18 -04:00
};
},
2015-05-09 13:58:18 -04:00
limit: function limit() {
var noLimit = !this.single.limit && this.single.limit !== 0;
if (noLimit && !this.single.offset) return '';
// Workaround for offset only,
// see http://stackoverflow.com/questions/10491492/sqllite-with-skip-offset-only-not-limit
2015-05-09 13:58:18 -04:00
return 'limit ' + this.formatter.parameter(noLimit ? -1 : this.single.limit);
}
2015-05-09 13:58:18 -04:00
});
function emptyStr() {
2015-05-09 13:58:18 -04:00
return '';
}
2015-05-09 13:58:18 -04:00
module.exports = QueryCompiler_SQLite3;