-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
400 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
'use strict'; | ||
|
||
var _ = require('lodash'); | ||
var BaseQuery = require('../../query/base'); | ||
var ColumnAlterer = require('./column_alterer'); | ||
|
||
/** | ||
* A query that allows altering tables. | ||
* | ||
* You will not create this query object directly. Instead, you will | ||
* receive it via {@link Schema#alterTable}. | ||
* | ||
* @protected | ||
* @constructor AlterTableQuery | ||
* @extends BaseQuery | ||
*/ | ||
var AlterTableQuery = BaseQuery.extend(/** @lends AlterTableQuery# */ { | ||
init: function() { throw new Error('AlterTableQuery must be spawned.'); }, | ||
|
||
/** | ||
* Override of {@link BaseQuery#_create}. | ||
* | ||
* @method | ||
* @private | ||
* @see {@link BaseQuery#_create} | ||
* @see {@link Schema#alterTable} for parameter details. | ||
*/ | ||
_create: function(name, cb) { | ||
if (!cb) { throw new Error('Missing callback to create columns.'); } | ||
this._super(); | ||
this._name = name; | ||
this._alterer = ColumnAlterer.create(); | ||
|
||
cb(this._alterer); | ||
|
||
var pkCount = _(this._alterer.added) | ||
.map('options').filter('primaryKey').size(); | ||
if (pkCount > 1) { | ||
throw new Error('Table may only have one primary key column.'); | ||
} | ||
}, | ||
|
||
/** | ||
* Duplication implementation. | ||
* | ||
* @method | ||
* @protected | ||
* @see {@link BaseQuery#_take} | ||
*/ | ||
_take: function(orig) { | ||
this._super(orig); | ||
this._name = orig._name; | ||
this._alterer = orig._alterer; | ||
}, | ||
|
||
/** | ||
* Override of {@link BaseQuery#sql}. | ||
* | ||
* @method | ||
* @protected | ||
* @see {@link BaseQuery#sql} | ||
*/ | ||
sql: function() { | ||
return this._adapter.phrasing.alterTable({ | ||
name: this._name, | ||
added: this._alterer.added, | ||
dropped: this._alterer.dropped, | ||
renamed: this._alterer.renamed | ||
}); | ||
} | ||
|
||
}); | ||
|
||
module.exports = AlterTableQuery.reopenClass({ | ||
__name__: 'AlterTableQuery' | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
'use strict'; | ||
|
||
var ColumnCreator = require('./column_creator'); | ||
var property = require('../../util/property').fn; | ||
|
||
/** | ||
* A class that can create, drop & alter columns for tables. | ||
* | ||
* @protected | ||
* @constructor ColumnAlterer | ||
* @param {Array} columns An empty array that will be mutated to contain | ||
* {@link ColumnAlterer~Column} objects as they are created. | ||
*/ | ||
var ColumnAlterer = ColumnCreator.extend({ | ||
init: function() { | ||
this._super.apply(this, arguments); | ||
this._dropped = []; | ||
}, | ||
|
||
/** | ||
* Alias for {@link ColumnCreator#columns}. | ||
* | ||
* @private | ||
* @scope internal | ||
* @type {Array.<Column>} | ||
* @readonly | ||
*/ | ||
added: property({ property: 'columns' }), | ||
|
||
/** | ||
* Names of dropped columns. | ||
* | ||
* @private | ||
* @scope internal | ||
* @type {Array.<String>} | ||
* @readonly | ||
*/ | ||
dropped: property(), | ||
|
||
/** | ||
* Drop a column. | ||
* | ||
* @method | ||
* @public | ||
* @param {String} column The column name. | ||
*/ | ||
drop: function(column) { | ||
this._dropped.push(column); | ||
} | ||
|
||
}); | ||
|
||
module.exports = ColumnAlterer.reopenClass({ | ||
__name__: 'Table~ColumnAlterer' | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.