2021-01-01 17:46:10 +02:00
|
|
|
// SQLite3: Column Builder & Compiler
|
|
|
|
// -------
|
|
|
|
const SchemaCompiler = require('../../../schema/compiler');
|
|
|
|
|
|
|
|
const some = require('lodash/some');
|
|
|
|
|
|
|
|
// Schema Compiler
|
|
|
|
// -------
|
|
|
|
|
|
|
|
class SchemaCompiler_SQLite3 extends SchemaCompiler {
|
2021-02-04 15:54:26 +02:00
|
|
|
constructor(client, builder) {
|
|
|
|
super(client, builder);
|
2021-01-01 17:46:10 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Compile the query to determine if a table exists.
|
|
|
|
hasTable(tableName) {
|
|
|
|
const sql =
|
|
|
|
`select * from sqlite_master ` +
|
2021-02-04 15:54:26 +02:00
|
|
|
`where type = 'table' and name = ${this.client.parameter(
|
2022-01-10 17:10:32 -03:00
|
|
|
this.formatter.wrap(tableName).replace(/`/g, ''),
|
2021-02-04 15:54:26 +02:00
|
|
|
this.builder,
|
|
|
|
this.bindingsHolder
|
|
|
|
)}`;
|
2021-01-01 17:46:10 +02:00
|
|
|
this.pushQuery({ sql, output: (resp) => resp.length > 0 });
|
|
|
|
}
|
|
|
|
|
|
|
|
// Compile the query to determine if a column exists.
|
|
|
|
hasColumn(tableName, column) {
|
|
|
|
this.pushQuery({
|
|
|
|
sql: `PRAGMA table_info(${this.formatter.wrap(tableName)})`,
|
|
|
|
output(resp) {
|
|
|
|
return some(resp, (col) => {
|
|
|
|
return (
|
|
|
|
this.client.wrapIdentifier(col.name.toLowerCase()) ===
|
|
|
|
this.client.wrapIdentifier(column.toLowerCase())
|
|
|
|
);
|
|
|
|
});
|
|
|
|
},
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
// Compile a rename table command.
|
|
|
|
renameTable(from, to) {
|
|
|
|
this.pushQuery(
|
|
|
|
`alter table ${this.formatter.wrap(from)} rename to ${this.formatter.wrap(
|
|
|
|
to
|
|
|
|
)}`
|
|
|
|
);
|
|
|
|
}
|
2021-01-03 04:10:26 +02:00
|
|
|
|
|
|
|
async generateDdlCommands() {
|
|
|
|
const sequence = this.builder._sequence;
|
|
|
|
for (let i = 0, l = sequence.length; i < l; i++) {
|
|
|
|
const query = sequence[i];
|
|
|
|
this[query.method].apply(this, query.args);
|
|
|
|
}
|
|
|
|
|
|
|
|
const commandSources = this.sequence;
|
|
|
|
|
2021-12-13 19:59:01 +01:00
|
|
|
if (commandSources.length === 1 && commandSources[0].statementsProducer) {
|
|
|
|
return commandSources[0].statementsProducer();
|
|
|
|
} else {
|
|
|
|
const result = [];
|
|
|
|
|
|
|
|
for (const commandSource of commandSources) {
|
|
|
|
const command = commandSource.sql;
|
|
|
|
|
|
|
|
if (Array.isArray(command)) {
|
|
|
|
result.push(...command);
|
|
|
|
} else {
|
|
|
|
result.push(command);
|
|
|
|
}
|
2021-01-03 04:10:26 +02:00
|
|
|
}
|
|
|
|
|
2021-12-13 19:59:01 +01:00
|
|
|
return { pre: [], sql: result, check: null, post: [] };
|
|
|
|
}
|
2021-01-03 04:10:26 +02:00
|
|
|
}
|
2021-01-01 17:46:10 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = SchemaCompiler_SQLite3;
|