2014-09-01 17:18:45 +02:00
|
|
|
'use strict';
|
|
|
|
|
2014-04-08 16:25:57 -04:00
|
|
|
var _ = require('lodash');
|
2014-04-27 19:35:36 -04:00
|
|
|
var inherits = require('inherits');
|
|
|
|
var EventEmitter = require('events').EventEmitter;
|
|
|
|
|
2014-04-08 16:25:57 -04:00
|
|
|
// Constructor for the builder instance, typically called from
|
|
|
|
// `knex.builder`, accepting the current `knex` instance,
|
|
|
|
// and pulling out the `client` and `grammar` from the current
|
|
|
|
// knex instance.
|
2014-04-27 19:35:36 -04:00
|
|
|
function SchemaBuilder() {
|
2014-04-09 10:11:41 -04:00
|
|
|
this._sequence = [];
|
2014-04-15 11:43:47 -04:00
|
|
|
this._errors = [];
|
2014-04-27 19:35:36 -04:00
|
|
|
}
|
|
|
|
inherits(SchemaBuilder, EventEmitter);
|
2014-04-15 11:43:47 -04:00
|
|
|
|
|
|
|
// Each of the schema builder methods just add to the
|
|
|
|
// "_sequence" array for consistency.
|
2014-04-16 01:23:50 -04:00
|
|
|
_.each([
|
2014-09-30 12:52:41 -04:00
|
|
|
'createTable', 'createTableIfNotExists', 'createSchema',
|
|
|
|
'createSchemaIfNotExists', 'dropSchema', 'dropSchemaIfExists',
|
|
|
|
'table', 'alterTable', 'hasTable', 'hasColumn',
|
2014-06-09 19:58:01 -04:00
|
|
|
'dropTable', 'renameTable', 'dropTableIfExists', 'raw', 'debug'
|
2014-04-16 01:23:50 -04:00
|
|
|
], function(method) {
|
2014-04-15 11:43:47 -04:00
|
|
|
SchemaBuilder.prototype[method] = function() {
|
|
|
|
if (method === 'table') method = 'alterTable';
|
|
|
|
this._sequence.push({
|
|
|
|
method: method,
|
|
|
|
args: _.toArray(arguments)
|
|
|
|
});
|
|
|
|
return this;
|
|
|
|
};
|
|
|
|
});
|
2014-04-08 16:25:57 -04:00
|
|
|
|
|
|
|
SchemaBuilder.prototype.toString = function() {
|
|
|
|
return this.toQuery();
|
|
|
|
};
|
|
|
|
|
2014-04-09 10:11:41 -04:00
|
|
|
SchemaBuilder.prototype.toSQL = function() {
|
|
|
|
var SchemaCompiler = this.client.SchemaCompiler;
|
|
|
|
return new SchemaCompiler(this).toSQL();
|
|
|
|
};
|
|
|
|
|
2014-04-08 16:25:57 -04:00
|
|
|
require('../interface')(SchemaBuilder);
|
|
|
|
|
2014-09-30 12:52:41 -04:00
|
|
|
module.exports = SchemaBuilder;
|