2016-03-02 17:07:05 +01:00
|
|
|
// SQLite3: Column Builder & Compiler
|
|
|
|
// -------
|
2019-06-04 00:37:17 +02:00
|
|
|
const inherits = require('inherits');
|
|
|
|
const SchemaCompiler = require('../../../schema/compiler');
|
2016-03-02 17:07:05 +01:00
|
|
|
|
2019-06-04 00:37:17 +02:00
|
|
|
const { some } = require('lodash');
|
2016-03-02 17:07:05 +01:00
|
|
|
|
|
|
|
// Schema Compiler
|
|
|
|
// -------
|
|
|
|
|
|
|
|
function SchemaCompiler_SQLite3() {
|
|
|
|
SchemaCompiler.apply(this, arguments);
|
|
|
|
}
|
2019-06-04 00:37:17 +02:00
|
|
|
|
2016-03-02 17:07:05 +01:00
|
|
|
inherits(SchemaCompiler_SQLite3, SchemaCompiler);
|
|
|
|
|
|
|
|
// Compile the query to determine if a table exists.
|
|
|
|
SchemaCompiler_SQLite3.prototype.hasTable = function(tableName) {
|
2016-05-17 01:01:34 +10:00
|
|
|
const sql =
|
|
|
|
`select * from sqlite_master ` +
|
|
|
|
`where type = 'table' and name = ${this.formatter.parameter(tableName)}`;
|
2018-07-09 08:10:34 -04:00
|
|
|
this.pushQuery({ sql, output: (resp) => resp.length > 0 });
|
2016-03-02 17:07:05 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
// Compile the query to determine if a column exists.
|
|
|
|
SchemaCompiler_SQLite3.prototype.hasColumn = function(tableName, column) {
|
|
|
|
this.pushQuery({
|
2016-05-17 01:01:34 +10:00
|
|
|
sql: `PRAGMA table_info(${this.formatter.wrap(tableName)})`,
|
|
|
|
output(resp) {
|
2019-03-13 22:58:59 +01:00
|
|
|
return some(resp, (col) => {
|
|
|
|
return (
|
2019-11-14 19:31:59 +01:00
|
|
|
this.client.wrapIdentifier(col.name.toLowerCase()) ===
|
|
|
|
this.client.wrapIdentifier(column.toLowerCase())
|
2019-03-13 22:58:59 +01:00
|
|
|
);
|
|
|
|
});
|
2018-07-09 08:10:34 -04:00
|
|
|
},
|
2016-03-02 17:07:05 +01:00
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
// Compile a rename table command.
|
|
|
|
SchemaCompiler_SQLite3.prototype.renameTable = function(from, to) {
|
2018-07-09 08:10:34 -04:00
|
|
|
this.pushQuery(
|
|
|
|
`alter table ${this.formatter.wrap(from)} rename to ${this.formatter.wrap(
|
|
|
|
to
|
|
|
|
)}`
|
|
|
|
);
|
2016-03-02 17:07:05 +01:00
|
|
|
};
|
|
|
|
|
2019-06-04 00:37:17 +02:00
|
|
|
module.exports = SchemaCompiler_SQLite3;
|