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 dbab5ae8
authored
Feb 06, 2013
by
Kevin Martin
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Added a few tests, DRY'd out some PG ENUM code
1 parent
8c17e416
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
23 additions
and
11 deletions
lib/dialects/postgres/query-generator.js
spec-jasmine/mysql/query-generator.spec.js
spec-jasmine/postgres/query-generator.spec.js
lib/dialects/postgres/query-generator.js
View file @
dbab5ae
...
...
@@ -36,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
()
}
...
...
@@ -65,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
}
...
...
@@ -87,7 +95,7 @@ module.exports = (function() {
attrStr
.
push
(
addQuotes
(
attr
)
+
" "
+
dataType
)
if
(
attributes
[
attr
].
match
(
/^ENUM
\(
/
))
{
query
=
"CREATE TYPE "
+
Utils
.
escape
(
"enum_"
+
tableName
+
"_"
+
attr
)
+
" AS "
+
attributes
[
attr
].
match
(
/^ENUM
\(
.+
\)
/
)[
0
]
+
"; "
+
query
query
=
pgEnum
(
tableName
,
attr
,
attributes
[
attr
]))
+
query
;
}
}
...
...
@@ -137,7 +145,7 @@ module.exports = (function() {
}))
if
(
definition
.
match
(
/^ENUM
\(
/
))
{
query
=
"CREATE TYPE "
+
Utils
.
escape
(
"enum_"
+
tableName
+
"_"
+
attrName
)
+
" AS "
+
definition
.
match
(
/^ENUM
\(
.+
\)
/
)[
0
]
+
"; "
+
query
query
=
pgEnum
(
tableName
,
attrName
,
definition
)
+
query
}
}
...
...
@@ -170,16 +178,17 @@ 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
})
sql
.
push
(
attrSql
)
if
(
definition
.
match
(
/^ENUM
\(
/
))
{
query
=
"CREATE TYPE "
+
Utils
.
escape
(
"enum_"
+
tableName
+
"_"
+
attributeName
)
+
" AS "
+
definition
.
match
(
/^ENUM
\(
.+
\)
/
)[
0
]
+
"; "
+
query
}
}
return
sql
.
join
(
''
)
...
...
@@ -194,10 +203,6 @@ module.exports = (function() {
before
:
addQuotes
(
attrBefore
),
after
:
addQuotes
(
attributeName
),
}))
if
(
attributes
[
attributeName
].
match
(
/^ENUM
\(
/
))
{
query
=
"CREATE TYPE "
+
Utils
.
escape
(
"enum_"
+
tableName
+
"_"
+
attributeName
)
+
" AS "
+
attributes
[
attributeName
].
match
(
/^ENUM
\(
.+
\)
/
)[
0
]
+
"; "
+
query
}
}
return
Utils
.
_
.
template
(
query
)({
tableName
:
addQuotes
(
tableName
),
attributes
:
attrString
.
join
(
', '
)
})
...
...
spec-jasmine/mysql/query-generator.spec.js
View file @
dbab5ae
...
...
@@ -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 @
dbab5ae
...
...
@@ -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