knex/src/schema/compiler.js

85 lines
2.2 KiB
JavaScript
Raw Normal View History

const { pushQuery, pushAdditional, unshiftQuery } = require('./helpers');
2016-03-02 17:07:05 +01:00
const { assign, isUndefined } = require('lodash');
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.
function SchemaCompiler(client, builder) {
this.builder = builder;
this._commonBuilder = this.builder;
this.client = client;
this.schema = builder._schema;
this.formatter = client.formatter(builder);
this.sequence = [];
2016-03-02 17:07:05 +01:00
}
assign(SchemaCompiler.prototype, {
pushQuery: pushQuery,
2016-03-02 17:07:05 +01:00
pushAdditional: pushAdditional,
2016-03-02 17:07:05 +01:00
unshiftQuery: unshiftQuery,
2016-03-02 17:07:05 +01:00
createTable: buildTable('create'),
createTableIfNotExists: buildTable('createIfNot'),
alterTable: buildTable('alter'),
dropTablePrefix: 'drop table ',
dropTable(tableName) {
this.pushQuery(
this.dropTablePrefix +
this.formatter.wrap(prefixedTableName(this.schema, tableName))
);
2016-03-02 17:07:05 +01:00
},
dropTableIfExists(tableName) {
this.pushQuery(
this.dropTablePrefix +
'if exists ' +
this.formatter.wrap(prefixedTableName(this.schema, tableName))
);
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());
},
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;
},
});
2016-03-02 17:07:05 +01:00
function buildTable(type) {
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 (!isUndefined(queryContext) && isUndefined(builder.queryContext())) {
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;
}
module.exports = SchemaCompiler;