238 lines
6.4 KiB
JavaScript
Raw Normal View History

2016-03-02 17:07:05 +01:00
// SQLite3
// -------
const defaults = require('lodash/defaults');
const map = require('lodash/map');
2021-01-31 13:40:13 +03:00
const { promisify } = require('util');
2016-03-02 17:07:05 +01:00
const Client = require('../../client');
2016-03-02 17:07:05 +01:00
const Raw = require('../../raw');
2021-01-01 18:46:16 +02:00
const Transaction = require('./execution/sqlite-transaction');
2021-01-09 17:59:53 +02:00
const SqliteQueryCompiler = require('./query/sqlite-querycompiler');
2021-01-01 17:46:10 +02:00
const SchemaCompiler = require('./schema/sqlite-compiler');
const ColumnCompiler = require('./schema/sqlite-columncompiler');
const TableCompiler = require('./schema/sqlite-tablecompiler');
2021-10-20 22:23:29 +02:00
const ViewCompiler = require('./schema/sqlite-viewcompiler');
const SQLite3_DDL = require('./schema/ddl');
const Formatter = require('../../formatter');
const QueryBuilder = require('./query/sqlite-querybuilder');
2016-03-02 17:07:05 +01:00
2021-01-31 13:40:13 +03:00
class Client_SQLite3 extends Client {
constructor(config) {
super(config);
if (config.useNullAsDefault === undefined) {
this.logger.warn(
'sqlite does not support inserting default values. Set the ' +
'`useNullAsDefault` flag to hide this warning. ' +
'(see docs http://knexjs.org/#Builder-insert).'
);
}
2016-03-02 17:07:05 +01:00
}
_driver() {
2022-04-21 19:05:40 +02:00
return require('sqlite3');
2021-01-31 13:40:13 +03:00
}
2016-03-02 17:07:05 +01:00
2016-09-13 08:15:58 -04:00
schemaCompiler() {
return new SchemaCompiler(this, ...arguments);
2021-01-31 13:40:13 +03:00
}
2016-03-02 17:07:05 +01:00
transaction() {
return new Transaction(this, ...arguments);
2021-01-31 13:40:13 +03:00
}
2021-01-07 23:34:46 +02:00
queryCompiler(builder, formatter) {
return new SqliteQueryCompiler(this, builder, formatter);
2021-01-31 13:40:13 +03:00
}
2016-03-02 17:07:05 +01:00
queryBuilder() {
return new QueryBuilder(this);
}
2021-10-20 22:23:29 +02:00
viewCompiler(builder, formatter) {
return new ViewCompiler(this, builder, formatter);
}
2016-09-13 08:15:58 -04:00
columnCompiler() {
return new ColumnCompiler(this, ...arguments);
2021-01-31 13:40:13 +03:00
}
2016-03-02 17:07:05 +01:00
2016-09-13 08:15:58 -04:00
tableCompiler() {
return new TableCompiler(this, ...arguments);
2021-01-31 13:40:13 +03:00
}
2016-03-02 17:07:05 +01:00
ddl(compiler, pragma, connection) {
return new SQLite3_DDL(this, compiler, pragma, connection);
2021-01-31 13:40:13 +03:00
}
2016-03-02 17:07:05 +01:00
wrapIdentifierImpl(value) {
return value !== '*' ? `\`${value.replace(/`/g, '``')}\`` : '*';
2021-01-31 13:40:13 +03:00
}
2016-03-02 17:07:05 +01:00
// Get a raw connection from the database, returning a promise with the connection object.
acquireRawConnection() {
return new Promise((resolve, reject) => {
// the default mode for sqlite3
let flags = this.driver.OPEN_READWRITE | this.driver.OPEN_CREATE;
if (this.connectionSettings.flags) {
if (!Array.isArray(this.connectionSettings.flags)) {
throw new Error(`flags must be an array of strings`);
}
this.connectionSettings.flags.forEach((_flag) => {
if (!_flag.startsWith('OPEN_') || !this.driver[_flag]) {
throw new Error(`flag ${_flag} not supported by node-sqlite3`);
}
flags = flags | this.driver[_flag];
});
}
const db = new this.driver.Database(
this.connectionSettings.filename,
flags,
(err) => {
if (err) {
return reject(err);
}
resolve(db);
2016-09-13 18:12:23 -04:00
}
);
});
2021-01-31 13:40:13 +03:00
}
2016-03-02 17:07:05 +01:00
// Used to explicitly close a connection, called internally by the pool when
// a connection times out or the pool is shutdown.
async destroyRawConnection(connection) {
const close = promisify((cb) => connection.close(cb));
return close();
2021-01-31 13:40:13 +03:00
}
2016-03-02 17:07:05 +01:00
// Runs the query on the specified connection, providing the bindings and any
// other necessary prep work.
_query(connection, obj) {
if (!obj.sql) throw new Error('The query is empty');
const { method } = obj;
let callMethod;
2016-03-02 17:07:05 +01:00
switch (method) {
case 'insert':
case 'update':
case 'counter':
case 'del':
callMethod = 'run';
break;
default:
callMethod = 'all';
}
2020-04-19 00:40:23 +02:00
return new Promise(function (resolver, rejecter) {
2016-03-02 17:07:05 +01:00
if (!connection || !connection[callMethod]) {
return rejecter(
new Error(`Error calling ${callMethod} on connection.`)
);
2016-03-02 17:07:05 +01:00
}
2020-04-19 00:40:23 +02:00
connection[callMethod](obj.sql, obj.bindings, function (err, response) {
if (err) return rejecter(err);
2016-03-02 17:07:05 +01:00
obj.response = response;
// We need the context here, as it contains
// the "this.lastID" or "this.changes"
obj.context = this;
return resolver(obj);
});
});
2021-01-31 13:40:13 +03:00
}
2016-03-02 17:07:05 +01:00
_stream(connection, obj, stream) {
if (!obj.sql) throw new Error('The query is empty');
const client = this;
2020-04-19 00:40:23 +02:00
return new Promise(function (resolver, rejecter) {
stream.on('error', rejecter);
stream.on('end', resolver);
return client
._query(connection, obj)
.then((obj) => obj.response)
2019-10-11 11:12:56 +03:00
.then((rows) => rows.forEach((row) => stream.write(row)))
2020-04-19 00:40:23 +02:00
.catch(function (err) {
stream.emit('error', err);
})
2020-04-19 00:40:23 +02:00
.then(function () {
stream.end();
});
});
2021-01-31 13:40:13 +03:00
}
2016-03-02 17:07:05 +01:00
// Ensures the response is returned in the same format as other clients.
processResponse(obj, runner) {
const ctx = obj.context;
const { response, returning } = obj;
if (obj.output) return obj.output.call(runner, response);
2016-03-02 17:07:05 +01:00
switch (obj.method) {
case 'select':
return response;
2016-03-02 17:07:05 +01:00
case 'first':
return response[0];
case 'pluck':
return map(response, obj.pluck);
case 'insert': {
if (returning) {
if (response) {
return response;
}
// ToDo Implement after https://github.com/microsoft/vscode-node-sqlite3/issues/15 is resolved
this.logger.warn(
'node-sqlite3 does not currently support RETURNING clause'
);
}
2016-03-02 17:07:05 +01:00
return [ctx.lastID];
}
2016-03-02 17:07:05 +01:00
case 'del':
case 'update':
case 'counter':
return ctx.changes;
default: {
2016-03-02 17:07:05 +01:00
return response;
}
2016-03-02 17:07:05 +01:00
}
2021-01-31 13:40:13 +03:00
}
2016-03-02 17:07:05 +01:00
poolDefaults() {
2021-01-31 13:40:13 +03:00
return defaults({ min: 1, max: 1 }, super.poolDefaults());
}
formatter(builder) {
return new Formatter(this, builder);
}
values(values, builder, formatter) {
if (Array.isArray(values)) {
if (Array.isArray(values[0])) {
return `( values ${values
.map(
(value) =>
`(${this.parameterize(value, undefined, builder, formatter)})`
)
.join(', ')})`;
}
return `(${this.parameterize(values, undefined, builder, formatter)})`;
}
if (values instanceof Raw) {
return `(${this.parameter(values, builder, formatter)})`;
}
return this.parameter(values, builder, formatter);
2021-01-31 13:40:13 +03:00
}
}
Object.assign(Client_SQLite3.prototype, {
dialect: 'sqlite3',
driverName: 'sqlite3',
});
2016-03-02 17:07:05 +01:00
module.exports = Client_SQLite3;