不要怂,就是干,撸起袖子干!

Commit c2166c5a by Sascha Depold

Merge branch 'master' of git://github.com/domesticapps/sequelize into domesticapps-master

2 parents c50b1046 dbab5ae8
...@@ -6,5 +6,6 @@ module.exports = { ...@@ -6,5 +6,6 @@ module.exports = {
DATE: 'DATETIME', DATE: 'DATETIME',
BOOLEAN: 'TINYINT(1)', BOOLEAN: 'TINYINT(1)',
FLOAT: 'FLOAT', FLOAT: 'FLOAT',
NOW: 'NOW' NOW: 'NOW',
ENUM: 'ENUM'
} }
...@@ -381,6 +381,12 @@ module.exports = (function() { ...@@ -381,6 +381,12 @@ module.exports = (function() {
var template = "<%= type %>" var template = "<%= type %>"
, replacements = { type: dataType.type } , replacements = { type: dataType.type }
if (dataType.type === DataTypes.ENUM && Array.isArray(dataType.values) && dataType.values.length > 0) {
replacements.type = "ENUM(" + Utils._.map(dataType.values, function(value) {
return Utils.escape(value)
}).join(", ") + ")"
}
if (dataType.hasOwnProperty('allowNull') && (!dataType.allowNull)) { if (dataType.hasOwnProperty('allowNull') && (!dataType.allowNull)) {
template += " NOT NULL" template += " NOT NULL"
} }
......
var Utils = require("../../utils") var Utils = require("../../utils")
, util = require("util") , util = require("util")
, DataTypes = require("../../data-types")
, tables = {} , tables = {}
, primaryKeys = {}; , primaryKeys = {};
...@@ -35,6 +36,10 @@ function pgEscape(val) { ...@@ -35,6 +36,10 @@ function pgEscape(val) {
return "'"+val+"'"; return "'"+val+"'";
} }
function pgEnum(tableName, attr, dataType) {
return "CREATE TYPE " + Utils.escape("enum_" + tableName + "_" + attr) + " AS " + dataType.match(/^ENUM\(.+\)/)[0] + "; "
}
function padInt(i) { function padInt(i) {
return (i < 10) ? '0' + i.toString() : i.toString() return (i < 10) ? '0' + i.toString() : i.toString()
} }
...@@ -64,6 +69,10 @@ function pgDataTypeMapping(tableName, attr, dataType) { ...@@ -64,6 +69,10 @@ function pgDataTypeMapping(tableName, attr, dataType) {
tables[tableName][attr] = 'serial' tables[tableName][attr] = 'serial'
} }
if (dataType.match(/^ENUM\(/)) {
dataType = dataType.replace(/^ENUM\(.+\)/, Utils.escape("enum_" + tableName + "_" + attr))
}
return dataType return dataType
} }
...@@ -84,6 +93,10 @@ module.exports = (function() { ...@@ -84,6 +93,10 @@ module.exports = (function() {
for (var attr in attributes) { for (var attr in attributes) {
var dataType = pgDataTypeMapping(tableName, attr, attributes[attr]) var dataType = pgDataTypeMapping(tableName, attr, attributes[attr])
attrStr.push(addQuotes(attr) + " " + dataType) attrStr.push(addQuotes(attr) + " " + dataType)
if (attributes[attr].match(/^ENUM\(/)) {
query = pgEnum(tableName, attr, attributes[attr])) + query;
}
} }
var values = { var values = {
...@@ -130,6 +143,10 @@ module.exports = (function() { ...@@ -130,6 +143,10 @@ module.exports = (function() {
attrName: addQuotes(attrName), attrName: addQuotes(attrName),
definition: pgDataTypeMapping(tableName, attrName, definition) definition: pgDataTypeMapping(tableName, attrName, definition)
})) }))
if (definition.match(/^ENUM\(/)) {
query = pgEnum(tableName, attrName, definition) + query
}
} }
return Utils._.template(query)({ tableName: addQuotes(tableName), attributes: attrString.join(', ') }) return Utils._.template(query)({ tableName: addQuotes(tableName), attributes: attrString.join(', ') })
...@@ -161,6 +178,11 @@ module.exports = (function() { ...@@ -161,6 +178,11 @@ module.exports = (function() {
}) })
} }
if (definition.match(/^ENUM\(/)) {
query = pgEnum(tableName, attributeName, definition) + query
definition = definition.replace(/^ENUM\(.+\)/, Utils.escape("enum_" + tableName + "_" + attributeName))
}
attrSql += Utils._.template(query)({ attrSql += Utils._.template(query)({
tableName: addQuotes(tableName), tableName: addQuotes(tableName),
query: addQuotes(attributeName) + ' TYPE ' + definition query: addQuotes(attributeName) + ' TYPE ' + definition
...@@ -476,6 +498,12 @@ module.exports = (function() { ...@@ -476,6 +498,12 @@ module.exports = (function() {
var template = "<%= type %>" var template = "<%= type %>"
, replacements = { type: dataType.type } , replacements = { type: dataType.type }
if (dataType.type === DataTypes.ENUM && Array.isArray(dataType.values) && dataType.values.length > 0) {
replacements.type = "ENUM(" + Utils._.map(dataType.values, function(value) {
return Utils.escape(value)
}).join(", ") + ")"
}
if (dataType.type === "TINYINT(1)") { if (dataType.type === "TINYINT(1)") {
dataType.type = 'BOOLEAN' dataType.type = 'BOOLEAN'
} }
......
var Utils = require("../../utils") var Utils = require("../../utils")
, DataTypes = require("../../data-types")
var MySqlQueryGenerator = Utils._.extend( var MySqlQueryGenerator = Utils._.extend(
Utils._.clone(require("../query-generator")), Utils._.clone(require("../query-generator")),
Utils._.clone(require("../mysql/query-generator")) Utils._.clone(require("../mysql/query-generator"))
...@@ -123,6 +124,10 @@ module.exports = (function() { ...@@ -123,6 +124,10 @@ module.exports = (function() {
var template = "<%= type %>" var template = "<%= type %>"
, replacements = { type: dataType.type } , replacements = { type: dataType.type }
if (dataType.type === DataTypes.ENUM) {
replacements.type = "INTEGER"
}
if (dataType.hasOwnProperty('allowNull') && !dataType.allowNull && !dataType.primaryKey) { if (dataType.hasOwnProperty('allowNull') && !dataType.allowNull && !dataType.primaryKey) {
template += " NOT NULL" template += " NOT NULL"
} }
......
...@@ -22,6 +22,10 @@ describe('QueryGenerator', function() { ...@@ -22,6 +22,10 @@ describe('QueryGenerator', function() {
{ {
arguments: ['myTable', {title: 'VARCHAR(255)', name: 'VARCHAR(255)'}, {charset: 'latin1'}], arguments: ['myTable', {title: 'VARCHAR(255)', name: 'VARCHAR(255)'}, {charset: 'latin1'}],
expectation: "CREATE TABLE IF NOT EXISTS `myTable` (`title` VARCHAR(255), `name` VARCHAR(255)) ENGINE=InnoDB DEFAULT CHARSET=latin1;" expectation: "CREATE TABLE IF NOT EXISTS `myTable` (`title` VARCHAR(255), `name` VARCHAR(255)) ENGINE=InnoDB DEFAULT CHARSET=latin1;"
},
{
arguments: ['myTable', {title: 'ENUM("A", "B", "C")', name: 'VARCHAR(255)'}, {charset: 'latin1'}],
expectation: "CREATE TABLE IF NOT EXISTS `myTable` (`title` ENUM(\"A\", \"B\", \"C\"), `name` VARCHAR(255)) ENGINE=InnoDB DEFAULT CHARSET=latin1;"
} }
], ],
......
...@@ -23,7 +23,10 @@ describe('QueryGenerator', function() { ...@@ -23,7 +23,10 @@ describe('QueryGenerator', function() {
arguments: ['mySchema.myTable', {title: 'VARCHAR(255)', name: 'VARCHAR(255)'}], arguments: ['mySchema.myTable', {title: 'VARCHAR(255)', name: 'VARCHAR(255)'}],
expectation: "CREATE TABLE IF NOT EXISTS \"mySchema\".\"myTable\" (\"title\" VARCHAR(255), \"name\" VARCHAR(255));" expectation: "CREATE TABLE IF NOT EXISTS \"mySchema\".\"myTable\" (\"title\" VARCHAR(255), \"name\" VARCHAR(255));"
}, },
{
arguments: ['myTable', {title: 'ENUM("A", "B", "C")', name: 'VARCHAR(255)'}],
expectation: "CREATE TYPE \"enum_myTable_title\" AS ENUM(\"A\", \"B\", \"C\"); CREATE TABLE IF NOT EXISTS \"myTable\" (\"title\" \"enum_myTable_title\", \"name\" VARCHAR(255));"
}
], ],
dropTableQuery: [ dropTableQuery: [
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!