2016-03-02 17:07:05 +01:00
|
|
|
|
|
|
|
// SQLite3 Query Builder & Compiler
|
|
|
|
|
2016-05-17 01:01:34 +10:00
|
|
|
import inherits from 'inherits';
|
|
|
|
import QueryCompiler from '../../../query/compiler';
|
2018-01-03 00:05:48 +02:00
|
|
|
import { assign, each, isEmpty, isString, noop, reduce, identity } from 'lodash'
|
2016-03-02 17:07:05 +01:00
|
|
|
|
|
|
|
function QueryCompiler_SQLite3(client, builder) {
|
|
|
|
QueryCompiler.call(this, client, builder)
|
2018-02-15 13:28:25 +01:00
|
|
|
|
|
|
|
const {returning} = this.single;
|
|
|
|
|
|
|
|
if(returning) {
|
2018-05-29 17:42:03 +02:00
|
|
|
this.client.logger.warn(
|
|
|
|
'.returning() is not supported by sqlite3 and will not have any effect.'
|
|
|
|
);
|
2018-02-15 13:28:25 +01:00
|
|
|
}
|
2016-03-02 17:07:05 +01:00
|
|
|
}
|
|
|
|
inherits(QueryCompiler_SQLite3, QueryCompiler)
|
|
|
|
|
|
|
|
assign(QueryCompiler_SQLite3.prototype, {
|
|
|
|
|
|
|
|
// The locks are not applicable in SQLite3
|
|
|
|
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.
|
2016-05-17 01:01:34 +10:00
|
|
|
insert() {
|
|
|
|
const insertValues = this.single.insert || []
|
2016-09-13 12:14:04 +02:00
|
|
|
let sql = this.with() + `insert into ${this.tableName} `
|
2016-03-02 17:07:05 +01:00
|
|
|
|
|
|
|
if (Array.isArray(insertValues)) {
|
|
|
|
if (insertValues.length === 0) {
|
|
|
|
return ''
|
|
|
|
}
|
|
|
|
else if (insertValues.length === 1 && insertValues[0] && isEmpty(insertValues[0])) {
|
|
|
|
return sql + this._emptyInsertValue
|
|
|
|
}
|
|
|
|
} else if (typeof insertValues === 'object' && isEmpty(insertValues)) {
|
|
|
|
return sql + this._emptyInsertValue
|
|
|
|
}
|
|
|
|
|
2016-05-17 01:01:34 +10:00
|
|
|
const insertData = this._prepInsert(insertValues)
|
2016-03-02 17:07:05 +01:00
|
|
|
|
|
|
|
if (isString(insertData)) {
|
|
|
|
return sql + insertData
|
|
|
|
}
|
|
|
|
|
|
|
|
if (insertData.columns.length === 0) {
|
|
|
|
return '';
|
|
|
|
}
|
|
|
|
|
2016-05-17 01:01:34 +10:00
|
|
|
sql += `(${this.formatter.columnize(insertData.columns)})`
|
2016-03-02 17:07:05 +01:00
|
|
|
|
2016-05-11 21:01:56 +03:00
|
|
|
// backwards compatible error
|
2016-05-17 01:01:34 +10:00
|
|
|
if (this.client.valueForUndefined !== null) {
|
|
|
|
each(insertData.values, bindings => {
|
|
|
|
each(bindings, binding => {
|
|
|
|
if (binding === undefined) throw new TypeError(
|
|
|
|
'`sqlite` does not support inserting default values. Specify ' +
|
|
|
|
'values explicitly or use the `useNullAsDefault` config flag. ' +
|
|
|
|
'(see docs http://knexjs.org/#Builder-insert).'
|
|
|
|
);
|
|
|
|
});
|
2016-05-11 21:01:56 +03:00
|
|
|
});
|
2016-05-17 01:01:34 +10:00
|
|
|
}
|
2016-05-11 21:01:56 +03:00
|
|
|
|
2016-03-02 17:07:05 +01:00
|
|
|
if (insertData.values.length === 1) {
|
2016-05-17 01:01:34 +10:00
|
|
|
const parameters = this.formatter.parameterize(
|
|
|
|
insertData.values[0], this.client.valueForUndefined
|
|
|
|
);
|
|
|
|
return sql + ` values (${parameters})`
|
2016-03-02 17:07:05 +01:00
|
|
|
}
|
|
|
|
|
2016-05-17 01:01:34 +10:00
|
|
|
const blocks = []
|
2016-05-18 19:59:24 +10:00
|
|
|
let i = -1
|
2016-03-02 17:07:05 +01:00
|
|
|
while (++i < insertData.values.length) {
|
2016-05-17 01:01:34 +10:00
|
|
|
let i2 = -1;
|
|
|
|
const block = blocks[i] = [];
|
|
|
|
let current = insertData.values[i]
|
2016-05-11 21:01:56 +03:00
|
|
|
current = current === undefined ? this.client.valueForUndefined : current
|
2016-03-02 17:07:05 +01:00
|
|
|
while (++i2 < insertData.columns.length) {
|
|
|
|
block.push(this.formatter.alias(
|
|
|
|
this.formatter.parameter(current[i2]),
|
|
|
|
this.formatter.wrap(insertData.columns[i2])
|
|
|
|
))
|
|
|
|
}
|
|
|
|
blocks[i] = block.join(', ')
|
|
|
|
}
|
|
|
|
return sql + ' select ' + blocks.join(' union all select ')
|
|
|
|
},
|
|
|
|
|
|
|
|
// Compile a truncate table statement into SQL.
|
2016-05-17 01:01:34 +10:00
|
|
|
truncate() {
|
2017-11-24 08:00:20 +01:00
|
|
|
const { table } = this.single
|
2016-03-02 17:07:05 +01:00
|
|
|
return {
|
2017-11-24 08:00:20 +01:00
|
|
|
sql: `delete from ${this.tableName}`,
|
2016-05-17 01:01:34 +10:00
|
|
|
output() {
|
|
|
|
return this.query({
|
2017-11-24 08:00:20 +01:00
|
|
|
sql: `delete from sqlite_sequence where name = '${table}'`
|
2016-05-17 01:01:34 +10:00
|
|
|
}).catch(noop)
|
2016-03-02 17:07:05 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
// Compiles a `columnInfo` query
|
2016-05-17 01:01:34 +10:00
|
|
|
columnInfo() {
|
2018-01-03 00:05:48 +02:00
|
|
|
const column = this.single.columnInfo;
|
|
|
|
|
|
|
|
// The user may have specified a custom wrapIdentifier function in the config. We
|
|
|
|
// need to run the identifiers through that function, but not format them as
|
|
|
|
// identifiers otherwise.
|
|
|
|
const table = this.client.customWrapIdentifier(this.single.table, identity);
|
|
|
|
|
2016-03-02 17:07:05 +01:00
|
|
|
return {
|
2018-01-03 00:05:48 +02:00
|
|
|
sql: `PRAGMA table_info(\`${table}\`)`,
|
2016-05-17 01:01:34 +10:00
|
|
|
output(resp) {
|
|
|
|
const maxLengthRegex = /.*\((\d+)\)/
|
|
|
|
const out = reduce(resp, function (columns, val) {
|
|
|
|
let { type } = val
|
2018-02-21 10:37:33 +01:00
|
|
|
let maxLength = type.match(maxLengthRegex)
|
|
|
|
if (maxLength) {
|
|
|
|
maxLength = maxLength[1]
|
|
|
|
}
|
2016-03-02 17:07:05 +01:00
|
|
|
type = maxLength ? type.split('(')[0] : type
|
|
|
|
columns[val.name] = {
|
|
|
|
type: type.toLowerCase(),
|
2016-05-17 01:01:34 +10:00
|
|
|
maxLength,
|
2016-03-02 17:07:05 +01:00
|
|
|
nullable: !val.notnull,
|
|
|
|
defaultValue: val.dflt_value
|
|
|
|
}
|
|
|
|
return columns
|
|
|
|
}, {})
|
|
|
|
return column && out[column] || out
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2016-05-17 01:01:34 +10:00
|
|
|
limit() {
|
|
|
|
const noLimit = !this.single.limit && this.single.limit !== 0
|
2016-03-02 17:07:05 +01:00
|
|
|
if (noLimit && !this.single.offset) return ''
|
|
|
|
|
|
|
|
// Workaround for offset only,
|
|
|
|
// see http://stackoverflow.com/questions/10491492/sqllite-with-skip-offset-only-not-limit
|
2016-05-17 01:01:34 +10:00
|
|
|
return `limit ${this.formatter.parameter(noLimit ? -1 : this.single.limit)}`
|
2016-03-02 17:07:05 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
})
|
|
|
|
|
|
|
|
function emptyStr() {
|
|
|
|
return ''
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2016-05-17 01:01:34 +10:00
|
|
|
export default QueryCompiler_SQLite3
|