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

Commit 884a93ed by Kevin Martin

Added ENUM type

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