2018-05-29 10:09:13 -04:00
|
|
|
import { pushQuery, pushAdditional, unshiftQuery } from './helpers';
|
2016-03-02 17:07:05 +01:00
|
|
|
|
2018-07-09 08:10:34 -04:00
|
|
|
import { assign, isUndefined } from '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) {
|
2018-07-09 08:10:34 -04:00
|
|
|
this.builder = builder;
|
|
|
|
this.client = client;
|
2016-05-18 19:59:24 +10:00
|
|
|
this.schema = builder._schema;
|
2018-07-09 08:10:34 -04:00
|
|
|
this.formatter = client.formatter(builder);
|
|
|
|
this.sequence = [];
|
2016-03-02 17:07:05 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
assign(SchemaCompiler.prototype, {
|
2016-05-17 01:01:34 +10:00
|
|
|
pushQuery: pushQuery,
|
2016-03-02 17:07:05 +01:00
|
|
|
|
2016-05-17 01:01:34 +10:00
|
|
|
pushAdditional: pushAdditional,
|
2016-03-02 17:07:05 +01:00
|
|
|
|
2018-05-29 10:09:13 -04:00
|
|
|
unshiftQuery: unshiftQuery,
|
|
|
|
|
2016-03-02 17:07:05 +01:00
|
|
|
createTable: buildTable('create'),
|
|
|
|
|
|
|
|
createTableIfNotExists: buildTable('createIfNot'),
|
|
|
|
|
|
|
|
alterTable: buildTable('alter'),
|
|
|
|
|
|
|
|
dropTablePrefix: 'drop table ',
|
2016-05-17 01:01:34 +10:00
|
|
|
|
|
|
|
dropTable(tableName) {
|
|
|
|
this.pushQuery(
|
2018-07-09 08:10:34 -04:00
|
|
|
this.dropTablePrefix +
|
|
|
|
this.formatter.wrap(prefixedTableName(this.schema, tableName))
|
2016-05-17 01:01:34 +10:00
|
|
|
);
|
2016-03-02 17:07:05 +01:00
|
|
|
},
|
|
|
|
|
2016-05-17 01:01:34 +10:00
|
|
|
dropTableIfExists(tableName) {
|
|
|
|
this.pushQuery(
|
2018-07-09 08:10:34 -04:00
|
|
|
this.dropTablePrefix +
|
|
|
|
'if exists ' +
|
|
|
|
this.formatter.wrap(prefixedTableName(this.schema, tableName))
|
2016-05-17 01:01:34 +10:00
|
|
|
);
|
2016-03-02 17:07:05 +01:00
|
|
|
},
|
|
|
|
|
2016-05-17 01:01:34 +10:00
|
|
|
raw(sql, bindings) {
|
2016-03-02 17:07:05 +01:00
|
|
|
this.sequence.push(this.client.raw(sql, bindings).toSQL());
|
|
|
|
},
|
|
|
|
|
2016-05-17 01:01:34 +10: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;
|
2018-07-09 08:10:34 -04:00
|
|
|
},
|
|
|
|
});
|
2016-03-02 17:07:05 +01:00
|
|
|
|
|
|
|
function buildTable(type) {
|
|
|
|
return function(tableName, fn) {
|
2016-05-17 01:01:34 +10:00
|
|
|
const builder = this.client.tableBuilder(type, tableName, fn);
|
2016-03-02 17:07:05 +01:00
|
|
|
|
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);
|
2016-05-17 01:01:34 +10:00
|
|
|
const sql = builder.toSQL();
|
2016-03-02 17:07:05 +01:00
|
|
|
|
2016-05-17 01:01:34 +10: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;
|
|
|
|
}
|
|
|
|
|
2016-05-17 01:01:34 +10:00
|
|
|
export default SchemaCompiler;
|