2014-09-01 17:18:45 +02:00
|
|
|
'use strict';
|
|
|
|
|
2015-04-22 15:39:29 -04:00
|
|
|
var _ = require('lodash')
|
|
|
|
var inherits = require('inherits')
|
|
|
|
var EventEmitter = require('events').EventEmitter
|
2014-04-27 19:35:36 -04:00
|
|
|
|
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.
|
2015-04-19 16:31:52 -04:00
|
|
|
function SchemaBuilder(client) {
|
|
|
|
this.client = client
|
|
|
|
this._sequence = []
|
2015-04-28 19:34:11 -04:00
|
|
|
this._debug = client.config && client.config.debug
|
2014-04-27 19:35:36 -04:00
|
|
|
}
|
2015-04-19 16:31:52 -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([
|
2015-04-19 16:31:52 -04:00
|
|
|
'createTable',
|
|
|
|
'createTableIfNotExists',
|
|
|
|
'createSchema',
|
|
|
|
'createSchemaIfNotExists',
|
|
|
|
'dropSchema',
|
|
|
|
'dropSchemaIfExists',
|
|
|
|
'createExtension',
|
|
|
|
'createExtensionIfNotExists',
|
|
|
|
'dropExtension',
|
|
|
|
'dropExtensionIfExists',
|
|
|
|
'table',
|
|
|
|
'alterTable',
|
|
|
|
'hasTable',
|
|
|
|
'hasColumn',
|
|
|
|
'dropTable',
|
|
|
|
'renameTable',
|
|
|
|
'dropTableIfExists',
|
2015-04-28 19:34:11 -04:00
|
|
|
'raw'
|
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;
|
2015-04-19 16:31:52 -04:00
|
|
|
}
|
|
|
|
})
|
2014-04-08 16:25:57 -04:00
|
|
|
|
2015-04-22 10:34:14 -04:00
|
|
|
require('../interface')(SchemaBuilder)
|
|
|
|
|
2014-04-08 16:25:57 -04:00
|
|
|
SchemaBuilder.prototype.toString = function() {
|
2015-04-19 16:31:52 -04:00
|
|
|
return this.toQuery()
|
|
|
|
}
|
2014-04-08 16:25:57 -04:00
|
|
|
|
2014-04-09 10:11:41 -04:00
|
|
|
SchemaBuilder.prototype.toSQL = function() {
|
2015-04-19 16:31:52 -04:00
|
|
|
return this.client.schemaCompiler(this).toSQL()
|
|
|
|
}
|
2014-04-09 10:11:41 -04:00
|
|
|
|
2015-04-19 16:31:52 -04:00
|
|
|
module.exports = SchemaBuilder
|