Skip to content
Toggle navigation
Projects
Groups
Snippets
Help
public
/
sequelize
This project
Loading...
Sign in
Toggle navigation
Go to a project
Project
Repository
Issues
0
Merge Requests
0
Pipelines
Wiki
Snippets
Settings
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
不要怂,就是干,撸起袖子干!
Commit c2166c5a
authored
Feb 18, 2013
by
Sascha Depold
Browse files
Options
Browse Files
Download
Plain Diff
Merge branch 'master' of
git://github.com/domesticapps/sequelize
into domesticapps-master
2 parents
c50b1046
dbab5ae8
Hide whitespace changes
Inline
Side-by-side
Showing
6 changed files
with
49 additions
and
2 deletions
lib/data-types.js
lib/dialects/mysql/query-generator.js
lib/dialects/postgres/query-generator.js
lib/dialects/sqlite/query-generator.js
spec-jasmine/mysql/query-generator.spec.js
spec-jasmine/postgres/query-generator.spec.js
lib/data-types.js
View file @
c2166c5
...
...
@@ -6,5 +6,6 @@ module.exports = {
DATE
:
'DATETIME'
,
BOOLEAN
:
'TINYINT(1)'
,
FLOAT
:
'FLOAT'
,
NOW
:
'NOW'
NOW
:
'NOW'
,
ENUM
:
'ENUM'
}
lib/dialects/mysql/query-generator.js
View file @
c2166c5
...
...
@@ -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"
}
...
...
lib/dialects/postgres/query-generator.js
View file @
c2166c5
var
Utils
=
require
(
"../../utils"
)
,
util
=
require
(
"util"
)
,
DataTypes
=
require
(
"../../data-types"
)
,
tables
=
{}
,
primaryKeys
=
{};
...
...
@@ -35,6 +36,10 @@ function pgEscape(val) {
return
"'"
+
val
+
"'"
;
}
function
pgEnum
(
tableName
,
attr
,
dataType
)
{
return
"CREATE TYPE "
+
Utils
.
escape
(
"enum_"
+
tableName
+
"_"
+
attr
)
+
" AS "
+
dataType
.
match
(
/^ENUM
\(
.+
\)
/
)[
0
]
+
"; "
}
function
padInt
(
i
)
{
return
(
i
<
10
)
?
'0'
+
i
.
toString
()
:
i
.
toString
()
}
...
...
@@ -64,6 +69,10 @@ function pgDataTypeMapping(tableName, attr, dataType) {
tables
[
tableName
][
attr
]
=
'serial'
}
if
(
dataType
.
match
(
/^ENUM
\(
/
))
{
dataType
=
dataType
.
replace
(
/^ENUM
\(
.+
\)
/
,
Utils
.
escape
(
"enum_"
+
tableName
+
"_"
+
attr
))
}
return
dataType
}
...
...
@@ -84,6 +93,10 @@ module.exports = (function() {
for
(
var
attr
in
attributes
)
{
var
dataType
=
pgDataTypeMapping
(
tableName
,
attr
,
attributes
[
attr
])
attrStr
.
push
(
addQuotes
(
attr
)
+
" "
+
dataType
)
if
(
attributes
[
attr
].
match
(
/^ENUM
\(
/
))
{
query
=
pgEnum
(
tableName
,
attr
,
attributes
[
attr
]))
+
query
;
}
}
var
values
=
{
...
...
@@ -130,6 +143,10 @@ module.exports = (function() {
attrName
:
addQuotes
(
attrName
),
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
(
', '
)
})
...
...
@@ -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
)({
tableName
:
addQuotes
(
tableName
),
query
:
addQuotes
(
attributeName
)
+
' TYPE '
+
definition
...
...
@@ -476,6 +498,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'
}
...
...
lib/dialects/sqlite/query-generator.js
View file @
c2166c5
var
Utils
=
require
(
"../../utils"
)
,
DataTypes
=
require
(
"../../data-types"
)
var
MySqlQueryGenerator
=
Utils
.
_
.
extend
(
Utils
.
_
.
clone
(
require
(
"../query-generator"
)),
Utils
.
_
.
clone
(
require
(
"../mysql/query-generator"
))
...
...
@@ -123,6 +124,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"
}
...
...
spec-jasmine/mysql/query-generator.spec.js
View file @
c2166c5
...
...
@@ -22,6 +22,10 @@ describe('QueryGenerator', function() {
{
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;"
},
{
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;"
}
],
...
...
spec-jasmine/postgres/query-generator.spec.js
View file @
c2166c5
...
...
@@ -23,7 +23,10 @@ describe('QueryGenerator', function() {
arguments
:
[
'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
:
[
...
...
Write
Preview
Markdown
is supported
Attach a file
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to post a comment