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

Commit 884a93ed by Kevin Martin

Added ENUM type

1 parent 602b1436
...@@ -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"
} }
......
...@@ -84,6 +84,10 @@ module.exports = (function() { ...@@ -84,6 +84,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].type.match(/^ENUM\(/)) {
query = "CREATE TYPE " + Utils.escape("enum_" + tableName + "_" + attr) + " AS " + attributes[attr].type "; " + query
}
} }
var values = { var values = {
...@@ -130,6 +134,10 @@ module.exports = (function() { ...@@ -130,6 +134,10 @@ module.exports = (function() {
attrName: addQuotes(attrName), attrName: addQuotes(attrName),
definition: pgDataTypeMapping(tableName, attrName, definition) definition: pgDataTypeMapping(tableName, attrName, definition)
})) }))
if (definition.type.match(/^ENUM\(/)) {
query = "CREATE TYPE " + Utils.escape("enum_" + tableName + "_" + attrName) + " AS " + definition.type "; " + query
}
} }
return Utils._.template(query)({ tableName: addQuotes(tableName), attributes: attrString.join(', ') }) return Utils._.template(query)({ tableName: addQuotes(tableName), attributes: attrString.join(', ') })
...@@ -167,6 +175,10 @@ module.exports = (function() { ...@@ -167,6 +175,10 @@ module.exports = (function() {
}) })
sql.push(attrSql) sql.push(attrSql)
if (definition.type.match(/^ENUM\(/)) {
query = "CREATE TYPE " + Utils.escape("enum_" + tableName + "_" + attributeName) + " AS " + definition.type "; " + query
}
} }
return sql.join('') return sql.join('')
...@@ -181,6 +193,10 @@ module.exports = (function() { ...@@ -181,6 +193,10 @@ module.exports = (function() {
before: addQuotes(attrBefore), before: addQuotes(attrBefore),
after: addQuotes(attributeName), after: addQuotes(attributeName),
})) }))
if (attributes[attributeName].type.match(/^ENUM\(/)) {
query = "CREATE TYPE " + Utils.escape("enum_" + tableName + "_" + attributeName) + " AS " + attributes[attributeName].type "; " + query
}
} }
return Utils._.template(query)({ tableName: addQuotes(tableName), attributes: attrString.join(', ') }) return Utils._.template(query)({ tableName: addQuotes(tableName), attributes: attrString.join(', ') })
...@@ -476,6 +492,12 @@ module.exports = (function() { ...@@ -476,6 +492,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'
} }
......
...@@ -123,6 +123,10 @@ module.exports = (function() { ...@@ -123,6 +123,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"
} }
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!