Browse files

Fix Issue #127, MySQL generator for ALTER COLUMN

  • Loading branch information...
1 parent a235e2e commit 250ef368f144c4d3c3ad6d52a4e580178426e518 @mde mde committed Dec 5, 2013
View
10 lib/adapters/sql/mysql.js
@@ -1,7 +1,6 @@
var utils = require('utilities')
, mysql
- , Generator = require('../../../lib/generators/sql').Generator
- , generator = new Generator()
+ , generator = require('../../../lib/generators/sql')
, model = require('../../../lib')
, Query = require('../../../lib/query/query').Query
, BaseAdapter = require('./base').Adapter
@@ -26,6 +25,7 @@ Adapter = function (options) {
this.type = 'sql';
this.config = _baseConfig;
this.client = null;
+ this.generator = generator.getGeneratorForAdapter(this);
utils.mixin(this.config, opts);
@@ -192,14 +192,12 @@ utils.mixin(Adapter.prototype, new (function () {
};
this.createTable = function (names, callback) {
- generator.COLUMN_NAME_DELIMITER = this.COLUMN_NAME_DELIMITER;
- var sql = generator.createTable(names);
+ var sql = this.generator.createTable(names);
this.exec(sql, callback);
};
this.dropTable = function (names, callback) {
- generator.COLUMN_NAME_DELIMITER = this.COLUMN_NAME_DELIMITER;
- var sql = generator.dropTable(names);
+ var sql = this.generator.dropTable(names);
this.exec(sql, callback);
};
})());
View
8 lib/adapters/sql/postgres.js
@@ -1,7 +1,6 @@
var utils = require('utilities')
, pg
- , Generator = require('../../../lib/generators/sql').Generator
- , generator = new Generator()
+ , generator = require('../../../lib/generators/sql')
, model = require('../../index')
, Query = require('../../query/query').Query
, BaseAdapter = require('./base').Adapter
@@ -27,6 +26,7 @@ Adapter = function (options) {
this.type = 'sql';
this.config = _baseConfig;
this.client = null;
+ this.generator = generator.getGeneratorForAdapter(this);
utils.mixin(this.config, opts);
@@ -195,12 +195,12 @@ utils.mixin(Adapter.prototype, new (function () {
};
this.createTable = function (names, callback) {
- var sql = generator.createTable(names);
+ var sql = this.generator.createTable(names);
this.exec(sql, callback);
};
this.dropTable = function (names, callback) {
- var sql = generator.dropTable(names);
+ var sql = this.generator.dropTable(names);
this.exec(sql, callback);
};
View
8 lib/adapters/sql/sqlite.js
@@ -1,8 +1,7 @@
var utils = require('utilities')
, sqlite3
, EventEmitter = require('events').EventEmitter
- , Generator = require('../../../lib/generators/sql').Generator
- , generator = new Generator()
+ , generator = require('../../../lib/generators/sql')
, model = require('../../../lib')
, Query = require('../../../lib/query/query').Query
, BaseAdapter = require('./base').Adapter
@@ -24,6 +23,7 @@ Adapter = function (options) {
this.type = 'sql';
this.config = _baseConfig;
this.client = null;
+ this.generator = generator.getGeneratorForAdapter(this);
utils.mixin(this.config, opts);
@@ -193,12 +193,12 @@ utils.mixin(Adapter.prototype, new (function () {
};
this.createTable = function (names, callback) {
- var sql = generator.createTable(names);
+ var sql = this.generator.createTable(names);
this.exec(sql, callback);
};
this.dropTable = function (names, callback) {
- var sql = generator.dropTable(names);
+ var sql = this.generator.dropTable(names);
this.exec(sql, callback);
};
})());
View
40 lib/generators/sql.js
@@ -1,5 +1,6 @@
var utils = require('utilities')
- , Generator
+ , StandardGenerator
+ , MySQLGenerator
, datatypeMap;
// TODO Better map, SQL-implementation specific
@@ -15,11 +16,11 @@ datatypeMap = {
, 'object': 'TEXT'
};
-Generator = function () {
+StandardGenerator = function () {
this.COLUMN_NAME_DELIMITER = '"';
};
-Generator.prototype = new (function () {
+StandardGenerator.prototype = new (function () {
this.getDatatype = function (jsType) {
return datatypeMap[jsType];
@@ -44,23 +45,23 @@ Generator.prototype = new (function () {
, delimiter = this.COLUMN_NAME_DELIMITER;
sql += delimiter + utils.string.snakeize(prop.name) + delimiter;
return sql;
- }
+ };
this.alterColumnStatement = function (prop) {
var sql = 'ALTER COLUMN '
, delimiter = this.COLUMN_NAME_DELIMITER;
sql += delimiter + utils.string.snakeize(prop.name) + delimiter + ' ';
sql += 'TYPE ' + this.getDatatype(prop.datatype);
return sql;
- }
+ };
this.renameColumnStatement = function (prop) {
var sql = 'RENAME COLUMN '
, delimiter = this.COLUMN_NAME_DELIMITER;
sql += delimiter + utils.string.snakeize(prop.name) + delimiter + ' ';
sql += 'TO ' + delimiter + utils.string.snakeize(prop.newName) + delimiter;
return sql;
- }
+ };
// CREATE TABLE distributors (did integer, name varchar(40));
this.createTableStatement = function (name, props, options) {
@@ -157,5 +158,30 @@ Generator.prototype = new (function () {
})();
-module.exports.Generator = Generator;
+MySQLGenerator = function () {
+ this.COLUMN_NAME_DELIMITER = '`';
+};
+
+MySQLGenerator.prototype = Object.create(StandardGenerator.prototype);
+
+MySQLGenerator.prototype.alterColumnStatement = function (prop) {
+ var sql = 'MODIFY COLUMN '
+ , delimiter = this.COLUMN_NAME_DELIMITER;
+ sql += delimiter + utils.string.snakeize(prop.name) + delimiter + ' ';
+ sql += this.getDatatype(prop.datatype);
+ return sql;
+};
+
+module.exports = {
+ StandardGenerator: StandardGenerator
+, MySQLGenerator: MySQLGenerator
+, getGeneratorForAdapter: function (adapter) {
+ if (adapter.name == 'mysql') {
+ return new MySQLGenerator();
+ }
+ else {
+ return new StandardGenerator();
+ }
+ }
+};
View
3 lib/migration/migration.js
@@ -8,8 +8,7 @@ var utils = require('utilities')
Migration = function (name, adapter) {
this.name = name;
this.adapter = adapter || defaultAdapter;
- this.generator = new Generator();
- this.generator.COLUMN_NAME_DELIMITER = this.adapter.COLUMN_NAME_DELIMITER;
+ this.generator = this.adapter.generator;
};
Migration.prototype = new (function () {
View
9 test/integration/adapters/sql/mysql.js
@@ -4,8 +4,6 @@ var utils = require('utilities')
, helpers = require('.././helpers')
, eagerAssnTests = require('./eager_assn')
, Adapter = require('../../../../lib/adapters/sql/mysql').Adapter
- , Generator = require('../../../../lib/generators/sql').Generator
- , generator = new Generator()
, adapter
, currentId
, tests
@@ -25,14 +23,11 @@ tests = {
, database: 'model_test'
});
-
- generator.COLUMN_NAME_DELIMITER = adapter.COLUMN_NAME_DELIMITER;
-
adapter.once('connect', function () {
var sql = '';
- sql += generator.dropTable(relations);
- sql += generator.createTable(relations);
+ sql += adapter.generator.dropTable(relations);
+ sql += adapter.generator.createTable(relations);
adapter.exec(sql, function (err, data) {
if (err) {
View
6 test/integration/adapters/sql/postgres.js
@@ -4,8 +4,6 @@ var utils = require('utilities')
, helpers = require('../helpers')
, eagerAssnTests = require('./eager_assn')
, Adapter = require('../../../../lib/adapters/sql/postgres').Adapter
- , Generator = require('../../../../lib/generators/sql').Generator
- , generator = new Generator()
, adapter
, currentId
, tests
@@ -23,8 +21,8 @@ tests = {
adapter.once('connect', function () {
var sql = '';
- sql += generator.dropTable(relations);
- sql += generator.createTable(relations);
+ sql += adapter.generator.dropTable(relations);
+ sql += adapter.generator.createTable(relations);
adapter.exec(sql, function (err, data) {
if (err) {
View
7 test/integration/adapters/sql/sqlite.js
@@ -4,8 +4,7 @@ var utils = require('utilities')
, helpers = require('../helpers')
, eagerAssnTests = require('./eager_assn')
, Adapter = require('../../../../lib/adapters/sql/sqlite').Adapter
- , Generator = require('../../../../lib/generators/sql').Generator
- , generator = new Generator()
+ , generator = require('../../../../lib/generators/sql')
, adapter
, currentId
, tests
@@ -23,8 +22,8 @@ tests = {
adapter.once('connect', function () {
var sql = '';
- sql += generator.dropTable(relations);
- sql += generator.createTable(relations);
+ sql += adapter.generator.dropTable(relations);
+ sql += adapter.generator.createTable(relations);
adapter.exec(sql, function (err, data) {
if (err) {
View
4 test/unit/generators/sql.js
@@ -3,8 +3,8 @@ var assert = require('assert')
, utils = require('utilities')
, model = require('../../../lib')
, Zooby = require('../../fixtures/zooby').Zooby
- , Generator = require('../../../lib/generators/sql').Generator
- , generator = new Generator()
+ , StandardGenerator = require('../../../lib/generators/sql').StandardGenerator
+ , generator = new StandardGenerator()
, tests
, strIncl;
View
2 test/unit/migration.js
@@ -1,6 +1,7 @@
var assert = require('assert')
, Migration = require('../../lib/migration').Migration
+ , StandardGenerator = require('../../lib/generators/sql').StandardGenerator
, utils = require('utilities')
, tests
, fakeAdapter
@@ -13,6 +14,7 @@ fakeAdapter = {
, exec: function (sql, cb) {
cb(null, sql);
}
+, generator: new StandardGenerator()
};
createCallback = function (next) {
return function (err, data) {

0 comments on commit 250ef36

Please sign in to comment.