knex/lib/schema/compiler.js

116 lines
3.0 KiB
JavaScript
Raw Normal View History

const {
pushQuery,
pushAdditional,
unshiftQuery,
} = require('./internal/helpers');
2016-03-02 17:07:05 +01:00
// The "SchemaCompiler" takes all of the query statements which have been
// gathered in the "SchemaBuilder" and turns them into an array of
// properly formatted / bound query strings.
2021-01-01 17:46:10 +02:00
class SchemaCompiler {
constructor(client, builder) {
this.builder = builder;
this._commonBuilder = this.builder;
this.client = client;
this.schema = builder._schema;
this.bindings = [];
this.bindingsHolder = this;
2021-01-01 17:46:10 +02:00
this.formatter = client.formatter(builder);
this.formatter.bindings = this.bindings;
2021-01-01 17:46:10 +02:00
this.sequence = [];
}
createSchema() {
throwOnlyPGError('createSchema');
2021-01-01 17:46:10 +02:00
}
createSchemaIfNotExists() {
throwOnlyPGError('createSchemaIfNotExists');
2021-01-01 17:46:10 +02:00
}
2021-01-01 17:46:10 +02:00
dropSchema() {
throwOnlyPGError('dropSchema');
}
2016-03-02 17:07:05 +01:00
2021-01-01 17:46:10 +02:00
dropSchemaIfExists() {
throwOnlyPGError('dropSchemaIfExists');
}
dropTable(tableName) {
this.pushQuery(
this.dropTablePrefix +
this.formatter.wrap(prefixedTableName(this.schema, tableName))
);
2021-01-01 17:46:10 +02:00
}
2016-03-02 17:07:05 +01:00
dropTableIfExists(tableName) {
this.pushQuery(
this.dropTablePrefix +
'if exists ' +
this.formatter.wrap(prefixedTableName(this.schema, tableName))
);
2021-01-01 17:46:10 +02:00
}
2016-03-02 17:07:05 +01:00
raw(sql, bindings) {
2016-03-02 17:07:05 +01:00
this.sequence.push(this.client.raw(sql, bindings).toSQL());
2021-01-01 17:46:10 +02:00
}
2016-03-02 17:07:05 +01:00
toSQL() {
const sequence = this.builder._sequence;
for (let i = 0, l = sequence.length; i < l; i++) {
const query = sequence[i];
2016-03-02 17:07:05 +01:00
this[query.method].apply(this, query.args);
}
return this.sequence;
2021-01-01 17:46:10 +02:00
}
async generateDdlCommands() {
const generatedCommands = this.toSQL();
return Array.isArray(generatedCommands)
? generatedCommands
: [generatedCommands];
}
2021-01-01 17:46:10 +02:00
}
SchemaCompiler.prototype.dropTablePrefix = 'drop table ';
SchemaCompiler.prototype.alterTable = buildTable('alter');
SchemaCompiler.prototype.createTable = buildTable('create');
SchemaCompiler.prototype.createTableIfNotExists = buildTable('createIfNot');
SchemaCompiler.prototype.pushQuery = pushQuery;
SchemaCompiler.prototype.pushAdditional = pushAdditional;
SchemaCompiler.prototype.unshiftQuery = unshiftQuery;
2016-03-02 17:07:05 +01:00
function buildTable(type) {
2020-04-19 00:40:23 +02:00
return function (tableName, fn) {
const builder = this.client.tableBuilder(type, tableName, fn);
2016-03-02 17:07:05 +01:00
Add queryContext to schema and query builders (#2314) * feat(query-builder): add hookContext for wrapIdentifier * refactor: use isUndefined * test(transaction): test passing of hookContext * feat(runnner): pass context to postProcessResponse * test(runner): test postProcessResponse for raw responses * test(raw): test passing of hookContext * feat: add hookContext to Raw and SchemaBuilder * test(transaction): fix test for hookContext * chore: fix lint error * fix: check for hookContext before calling it * test(transaction): fix hookContext test * chore: remove whitespace * test(hookContext): test cloning of context object * refactor: hookContext -> queryContext * minor: use more descriptive variable name i.e. refactor: `context` => `queryContext` * fix: remove unnecessary checks for query builder * fix(Raw): pass query builder to formatter * fix(SchemaCompiler): pass schema builder to formatter * refactor: add addQueryContext helper * feat: add queryContext to TableBuilder and ColumnBuilder * fix(TableCompiler): pass table builder to formatter * fix(ColumnCompiler): pass column builder to formatter * fix(pushQuery): fix passing builder to formatter * test(Schema|Table|ColumnCompiler): test passing queryContext * fix(SchemaCompiler): pass queryContext to TableCompiler * fix(TableCompiler): pass queryContext to ColumnCompiler * test: add queryContext tests for all schema dialects * test(TableCompiler): test overwriting queryContext from SchemaCompiler * test(Raw): test passing queryContext to wrapIdentifier * tests: run all the tests
2018-02-01 23:41:01 +01:00
// pass queryContext down to tableBuilder but do not overwrite it if already set
const queryContext = this.builder.queryContext();
if (queryContext !== undefined && builder.queryContext() === undefined) {
Add queryContext to schema and query builders (#2314) * feat(query-builder): add hookContext for wrapIdentifier * refactor: use isUndefined * test(transaction): test passing of hookContext * feat(runnner): pass context to postProcessResponse * test(runner): test postProcessResponse for raw responses * test(raw): test passing of hookContext * feat: add hookContext to Raw and SchemaBuilder * test(transaction): fix test for hookContext * chore: fix lint error * fix: check for hookContext before calling it * test(transaction): fix hookContext test * chore: remove whitespace * test(hookContext): test cloning of context object * refactor: hookContext -> queryContext * minor: use more descriptive variable name i.e. refactor: `context` => `queryContext` * fix: remove unnecessary checks for query builder * fix(Raw): pass query builder to formatter * fix(SchemaCompiler): pass schema builder to formatter * refactor: add addQueryContext helper * feat: add queryContext to TableBuilder and ColumnBuilder * fix(TableCompiler): pass table builder to formatter * fix(ColumnCompiler): pass column builder to formatter * fix(pushQuery): fix passing builder to formatter * test(Schema|Table|ColumnCompiler): test passing queryContext * fix(SchemaCompiler): pass queryContext to TableCompiler * fix(TableCompiler): pass queryContext to ColumnCompiler * test: add queryContext tests for all schema dialects * test(TableCompiler): test overwriting queryContext from SchemaCompiler * test(Raw): test passing queryContext to wrapIdentifier * tests: run all the tests
2018-02-01 23:41:01 +01:00
builder.queryContext(queryContext);
}
2016-03-02 17:07:05 +01:00
builder.setSchema(this.schema);
const sql = builder.toSQL();
2016-03-02 17:07:05 +01:00
for (let i = 0, l = sql.length; i < l; i++) {
2016-03-02 17:07:05 +01:00
this.sequence.push(sql[i]);
}
};
}
function prefixedTableName(prefix, table) {
return prefix ? `${prefix}.${table}` : table;
}
2021-01-01 17:46:10 +02:00
function throwOnlyPGError(operationName) {
throw new Error(
`${operationName} is not supported for this dialect (only PostgreSQL supports it currently).`
);
}
module.exports = SchemaCompiler;