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 a8efaea4
authored
Mar 27, 2013
by
Sascha Depold
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
made addQuotes and removeQuotes parts of the QueryGenerator
1 parent
7139a630
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
225 additions
and
167 deletions
lib/dialects/mysql/query-generator.js
lib/dialects/postgres/query-generator.js
lib/dialects/mysql/query-generator.js
View file @
a8efaea
...
...
@@ -20,20 +20,20 @@ module.exports = (function() {
if
(
Utils
.
_
.
includes
(
dataType
,
'PRIMARY KEY'
))
{
primaryKeys
.
push
(
attr
)
attrStr
.
push
(
Utils
.
addTick
s
(
attr
)
+
" "
+
dataType
.
replace
(
/PRIMARY KEY/
,
''
))
attrStr
.
push
(
QueryGenerator
.
addQuote
s
(
attr
)
+
" "
+
dataType
.
replace
(
/PRIMARY KEY/
,
''
))
}
else
{
attrStr
.
push
(
Utils
.
addTick
s
(
attr
)
+
" "
+
dataType
)
attrStr
.
push
(
QueryGenerator
.
addQuote
s
(
attr
)
+
" "
+
dataType
)
}
}
}
var
values
=
{
table
:
Utils
.
addTick
s
(
tableName
),
table
:
QueryGenerator
.
addQuote
s
(
tableName
),
attributes
:
attrStr
.
join
(
", "
),
engine
:
options
.
engine
,
charset
:
(
options
.
charset
?
"DEFAULT CHARSET="
+
options
.
charset
:
""
)
}
,
pkString
=
primaryKeys
.
map
(
function
(
pk
)
{
return
Utils
.
addTick
s
(
pk
)
}).
join
(
", "
)
,
pkString
=
primaryKeys
.
map
(
function
(
pk
)
{
return
QueryGenerator
.
addQuote
s
(
pk
)
}).
join
(
", "
)
if
(
pkString
.
length
>
0
)
{
values
.
attributes
+=
", PRIMARY KEY ("
+
pkString
+
")"
...
...
@@ -47,7 +47,9 @@ module.exports = (function() {
var
query
=
"DROP TABLE IF EXISTS <%= table %>;"
return
Utils
.
_
.
template
(
query
)({
table
:
Utils
.
addTicks
(
tableName
)})
return
Utils
.
_
.
template
(
query
)({
table
:
QueryGenerator
.
addQuotes
(
tableName
)
})
},
renameTableQuery
:
function
(
before
,
after
)
{
...
...
@@ -118,12 +120,12 @@ module.exports = (function() {
,
table
=
null
options
=
options
||
{}
options
.
table
=
table
=
Array
.
isArray
(
tableName
)
?
tableName
.
map
(
function
(
tbl
){
return
Utils
.
addTicks
(
tbl
)
}).
join
(
", "
)
:
Utils
.
addTick
s
(
tableName
)
options
.
table
=
table
=
Array
.
isArray
(
tableName
)
?
tableName
.
map
(
function
(
tbl
){
return
QueryGenerator
.
addQuotes
(
tbl
)
}).
join
(
", "
)
:
QueryGenerator
.
addQuote
s
(
tableName
)
options
.
attributes
=
options
.
attributes
&&
options
.
attributes
.
map
(
function
(
attr
){
if
(
Array
.
isArray
(
attr
)
&&
attr
.
length
==
2
)
{
return
[
attr
[
0
],
Utils
.
addTick
s
(
attr
[
1
])].
join
(
' as '
)
return
[
attr
[
0
],
QueryGenerator
.
addQuote
s
(
attr
[
1
])].
join
(
' as '
)
}
else
{
return
attr
.
indexOf
(
Utils
.
TICK_CHAR
)
<
0
?
Utils
.
addTick
s
(
attr
)
:
attr
return
attr
.
indexOf
(
Utils
.
TICK_CHAR
)
<
0
?
QueryGenerator
.
addQuote
s
(
attr
)
:
attr
}
}).
join
(
", "
)
options
.
attributes
=
options
.
attributes
||
'*'
...
...
@@ -159,7 +161,7 @@ module.exports = (function() {
}
if
(
options
.
group
)
{
options
.
group
=
Array
.
isArray
(
options
.
group
)
?
options
.
group
.
map
(
function
(
grp
){
return
Utils
.
addTicks
(
grp
)}).
join
(
', '
)
:
Utils
.
addTick
s
(
options
.
group
)
options
.
group
=
Array
.
isArray
(
options
.
group
)
?
options
.
group
.
map
(
function
(
grp
){
return
QueryGenerator
.
addQuotes
(
grp
)}).
join
(
', '
)
:
QueryGenerator
.
addQuote
s
(
options
.
group
)
query
+=
" GROUP BY <%= group %>"
}
...
...
@@ -187,8 +189,8 @@ module.exports = (function() {
var
query
=
"INSERT INTO <%= table %> (<%= attributes %>) VALUES (<%= values %>);"
var
replacements
=
{
table
:
Utils
.
addTick
s
(
tableName
),
attributes
:
Object
.
keys
(
attrValueHash
).
map
(
function
(
attr
){
return
Utils
.
addTick
s
(
attr
)}).
join
(
","
),
table
:
QueryGenerator
.
addQuote
s
(
tableName
),
attributes
:
Object
.
keys
(
attrValueHash
).
map
(
function
(
attr
){
return
QueryGenerator
.
addQuote
s
(
attr
)}).
join
(
","
),
values
:
Utils
.
_
.
values
(
attrValueHash
).
map
(
function
(
value
){
return
Utils
.
escape
((
value
instanceof
Date
)
?
Utils
.
toSqlDate
(
value
)
:
value
)
}).
join
(
","
)
...
...
@@ -207,11 +209,11 @@ module.exports = (function() {
var
value
=
attrValueHash
[
key
]
,
_value
=
(
value
instanceof
Date
)
?
Utils
.
toSqlDate
(
value
)
:
value
values
.
push
(
Utils
.
addTick
s
(
key
)
+
"="
+
Utils
.
escape
(
_value
))
values
.
push
(
QueryGenerator
.
addQuote
s
(
key
)
+
"="
+
Utils
.
escape
(
_value
))
}
var
replacements
=
{
table
:
Utils
.
addTick
s
(
tableName
),
table
:
QueryGenerator
.
addQuote
s
(
tableName
),
values
:
values
.
join
(
","
),
where
:
QueryGenerator
.
getWhereConditions
(
where
)
}
...
...
@@ -225,7 +227,7 @@ module.exports = (function() {
var
query
=
"DELETE FROM <%= table %> WHERE <%= where %> LIMIT <%= limit %>"
var
replacements
=
{
table
:
Utils
.
addTick
s
(
tableName
),
table
:
QueryGenerator
.
addQuote
s
(
tableName
),
where
:
QueryGenerator
.
getWhereConditions
(
where
),
limit
:
Utils
.
escape
(
options
.
limit
)
}
...
...
@@ -243,11 +245,11 @@ module.exports = (function() {
var
value
=
attrValueHash
[
key
]
,
_value
=
(
value
instanceof
Date
)
?
Utils
.
toSqlDate
(
value
)
:
value
values
.
push
(
Utils
.
addTicks
(
key
)
+
"="
+
Utils
.
addTick
s
(
key
)
+
" + "
+
Utils
.
escape
(
_value
))
values
.
push
(
QueryGenerator
.
addQuotes
(
key
)
+
"="
+
QueryGenerator
.
addQuote
s
(
key
)
+
" + "
+
Utils
.
escape
(
_value
))
}
var
replacements
=
{
table
:
Utils
.
addTick
s
(
tableName
),
table
:
QueryGenerator
.
addQuote
s
(
tableName
),
values
:
values
.
join
(
","
),
where
:
QueryGenerator
.
getWhereConditions
(
where
)
}
...
...
@@ -342,7 +344,7 @@ module.exports = (function() {
var
value
=
hash
[
key
]
//handle qualified key names
var
_key
=
key
.
split
(
'.'
).
map
(
function
(
col
){
return
Utils
.
addTick
s
(
col
)}).
join
(
"."
)
var
_key
=
key
.
split
(
'.'
).
map
(
function
(
col
){
return
QueryGenerator
.
addQuote
s
(
col
)}).
join
(
"."
)
,
_value
=
null
if
(
Array
.
isArray
(
value
))
{
...
...
@@ -357,7 +359,7 @@ module.exports = (function() {
// is value an object?
//using as sentinel for join column => value
_value
=
value
.
join
.
split
(
'.'
).
map
(
function
(
col
){
return
Utils
.
addTick
s
(
col
)
}).
join
(
"."
)
_value
=
value
.
join
.
split
(
'.'
).
map
(
function
(
col
){
return
QueryGenerator
.
addQuote
s
(
col
)
}).
join
(
"."
)
result
.
push
([
_key
,
_value
].
join
(
"="
))
}
else
{
_value
=
Utils
.
escape
(
value
)
...
...
@@ -432,6 +434,14 @@ module.exports = (function() {
}
return
fields
},
addQuotes
:
function
(
s
,
quoteChar
)
{
return
Utils
.
addTicks
(
s
,
quoteChar
)
},
removeQuotes
:
function
(
s
,
quoteChar
)
{
return
Utils
.
removeTicks
(
s
,
quoteChar
)
}
}
...
...
lib/dialects/postgres/query-generator.js
View file @
a8efaea
var
Utils
=
require
(
"../../utils"
)
,
util
=
require
(
"util"
)
,
DataTypes
=
require
(
"../../data-types"
)
,
tables
=
{}
,
primaryKeys
=
{};
function
removeQuotes
(
s
,
quoteChar
)
{
quoteChar
=
quoteChar
||
'"'
return
s
.
replace
(
new
RegExp
(
quoteChar
,
'g'
),
''
)
}
function
addQuotes
(
s
,
quoteChar
)
{
quoteChar
=
quoteChar
||
'"'
return
removeQuotes
(
s
,
quoteChar
)
.
split
(
'.'
)
.
map
(
function
(
e
)
{
return
quoteChar
+
String
(
e
)
+
quoteChar
})
.
join
(
'.'
)
}
function
pgEscape
(
val
)
{
if
(
val
===
undefined
||
val
===
null
)
{
return
'NULL'
;
}
switch
(
typeof
val
)
{
case
'boolean'
:
return
(
val
)
?
'true'
:
'false'
;
case
'number'
:
return
val
+
''
;
case
'object'
:
if
(
Array
.
isArray
(
val
))
{
return
'ARRAY['
+
val
.
map
(
function
(
it
)
{
return
pgEscape
(
it
)
}).
join
(
','
)
+
']'
;
}
}
if
(
val
instanceof
Date
)
{
val
=
pgSqlDate
(
val
);
}
// http://www.postgresql.org/docs/8.2/static/sql-syntax-lexical.html#SQL-SYNTAX-STRINGS
val
=
val
.
replace
(
/'/g
,
"''"
);
return
"'"
+
val
+
"'"
;
}
function
pgEscapeAndQuote
(
val
)
{
return
addQuotes
(
removeQuotes
(
pgEscape
(
val
),
"'"
))
}
function
pgEnum
(
tableName
,
attr
,
dataType
)
{
var
enumName
=
pgEscapeAndQuote
(
"enum_"
+
tableName
+
"_"
+
attr
)
return
"DROP TYPE IF EXISTS "
+
enumName
+
"; CREATE TYPE "
+
enumName
+
" AS "
+
dataType
.
match
(
/^ENUM
\(
.+
\)
/
)[
0
]
+
"; "
}
function
padInt
(
i
)
{
return
(
i
<
10
)
?
'0'
+
i
.
toString
()
:
i
.
toString
()
}
function
pgSqlDate
(
dt
)
{
var
date
=
[
dt
.
getUTCFullYear
(),
padInt
(
dt
.
getUTCMonth
()
+
1
),
padInt
(
dt
.
getUTCDate
())
].
join
(
'-'
)
var
time
=
[
dt
.
getUTCHours
(),
padInt
(
dt
.
getUTCMinutes
()),
padInt
(
dt
.
getUTCSeconds
())].
join
(
':'
)
return
date
+
' '
+
time
+
'.'
+
((
dt
.
getTime
()
%
1000
)
*
1000
)
+
'Z'
}
function
pgDataTypeMapping
(
tableName
,
attr
,
dataType
)
{
if
(
Utils
.
_
.
includes
(
dataType
,
'PRIMARY KEY'
))
{
primaryKeys
[
tableName
].
push
(
attr
)
dataType
=
dataType
.
replace
(
/PRIMARY KEY/
,
''
)
}
if
(
Utils
.
_
.
includes
(
dataType
,
'TINYINT(1)'
))
{
dataType
=
dataType
.
replace
(
/TINYINT
\(
1
\)
/
,
'BOOLEAN'
)
}
if
(
Utils
.
_
.
includes
(
dataType
,
'DATETIME'
))
{
dataType
=
dataType
.
replace
(
/DATETIME/
,
'TIMESTAMP WITH TIME ZONE'
)
}
if
(
Utils
.
_
.
includes
(
dataType
,
'SERIAL'
))
{
dataType
=
dataType
.
replace
(
/INTEGER/
,
''
)
dataType
=
dataType
.
replace
(
/NOT NULL/
,
''
)
tables
[
tableName
][
attr
]
=
'serial'
}
if
(
dataType
.
match
(
/^ENUM
\(
/
))
{
dataType
=
dataType
.
replace
(
/^ENUM
\(
.+
\)
/
,
pgEscapeAndQuote
(
"enum_"
+
tableName
+
"_"
+
attr
))
}
return
dataType
}
var
Utils
=
require
(
"../../utils"
)
,
util
=
require
(
"util"
)
,
DataTypes
=
require
(
"../../data-types"
)
,
tables
=
{}
,
primaryKeys
=
{}
module
.
exports
=
(
function
()
{
var
QueryGenerator
=
{
...
...
@@ -100,20 +19,23 @@ module.exports = (function() {
,
attrStr
=
[]
for
(
var
attr
in
attributes
)
{
var
dataType
=
pgDataTypeMapping
(
tableName
,
attr
,
attributes
[
attr
])
attrStr
.
push
(
addQuotes
(
attr
)
+
" "
+
dataType
)
var
dataType
=
QueryGenerator
.
pgDataTypeMapping
(
tableName
,
attr
,
attributes
[
attr
])
attrStr
.
push
(
QueryGenerator
.
addQuotes
(
attr
)
+
" "
+
dataType
)
if
(
attributes
[
attr
].
match
(
/^ENUM
\(
/
))
{
query
=
pgEnum
(
tableName
,
attr
,
attributes
[
attr
])
+
query
;
query
=
QueryGenerator
.
pgEnum
(
tableName
,
attr
,
attributes
[
attr
])
+
query
}
}
var
values
=
{
table
:
addQuotes
(
tableName
),
table
:
QueryGenerator
.
addQuotes
(
tableName
),
attributes
:
attrStr
.
join
(
", "
),
}
var
pks
=
primaryKeys
[
tableName
].
map
(
function
(
pk
){
return
addQuotes
(
pk
)
}).
join
(
","
)
var
pks
=
primaryKeys
[
tableName
].
map
(
function
(
pk
){
return
QueryGenerator
.
addQuotes
(
pk
)
}).
join
(
","
)
if
(
pks
.
length
>
0
)
{
values
.
attributes
+=
", PRIMARY KEY ("
+
pks
+
")"
}
...
...
@@ -124,12 +46,17 @@ module.exports = (function() {
dropTableQuery
:
function
(
tableName
,
options
)
{
options
=
options
||
{}
var
query
=
"DROP TABLE IF EXISTS <%= table %>;"
return
Utils
.
_
.
template
(
query
)({
table
:
addQuotes
(
tableName
)
})
return
Utils
.
_
.
template
(
query
)({
table
:
QueryGenerator
.
addQuotes
(
tableName
)
})
},
renameTableQuery
:
function
(
before
,
after
)
{
var
query
=
"ALTER TABLE <%= before %> RENAME TO <%= after %>;"
return
Utils
.
_
.
template
(
query
)({
before
:
addQuotes
(
before
),
after
:
addQuotes
(
after
)
})
return
Utils
.
_
.
template
(
query
)({
before
:
QueryGenerator
.
addQuotes
(
before
),
after
:
QueryGenerator
.
addQuotes
(
after
)
})
},
showTablesQuery
:
function
()
{
...
...
@@ -138,7 +65,9 @@ module.exports = (function() {
describeTableQuery
:
function
(
tableName
)
{
var
query
=
'SELECT column_name as "Field", column_default as "Default", is_nullable as "Null", data_type as "Type" FROM information_schema.columns WHERE table_name = <%= table %>;'
return
Utils
.
_
.
template
(
query
)({
table
:
addQuotes
(
tableName
,
"'"
)
})
return
Utils
.
_
.
template
(
query
)({
table
:
QueryGenerator
.
addQuotes
(
tableName
,
"'"
)
})
},
addColumnQuery
:
function
(
tableName
,
attributes
)
{
...
...
@@ -149,21 +78,26 @@ module.exports = (function() {
var
definition
=
attributes
[
attrName
]
attrString
.
push
(
Utils
.
_
.
template
(
'<%= attrName %> <%= definition %>'
)({
attrName
:
addQuotes
(
attrName
),
definition
:
pgDataTypeMapping
(
tableName
,
attrName
,
definition
)
attrName
:
QueryGenerator
.
addQuotes
(
attrName
),
definition
:
QueryGenerator
.
pgDataTypeMapping
(
tableName
,
attrName
,
definition
)
}))
if
(
definition
.
match
(
/^ENUM
\(
/
))
{
query
=
pgEnum
(
tableName
,
attrName
,
definition
)
+
query
query
=
QueryGenerator
.
pgEnum
(
tableName
,
attrName
,
definition
)
+
query
}
}
return
Utils
.
_
.
template
(
query
)({
tableName
:
addQuotes
(
tableName
),
attributes
:
attrString
.
join
(
', '
)
})
return
Utils
.
_
.
template
(
query
)({
tableName
:
QueryGenerator
.
addQuotes
(
tableName
),
attributes
:
attrString
.
join
(
', '
)
})
},
removeColumnQuery
:
function
(
tableName
,
attributeName
)
{
var
query
=
"ALTER TABLE <%= tableName %> DROP COLUMN <%= attributeName %>;"
return
Utils
.
_
.
template
(
query
)({
tableName
:
addQuotes
(
tableName
),
attributeName
:
addQuotes
(
attributeName
)
})
return
Utils
.
_
.
template
(
query
)({
tableName
:
QueryGenerator
.
addQuotes
(
tableName
),
attributeName
:
QueryGenerator
.
addQuotes
(
attributeName
)
})
},
changeColumnQuery
:
function
(
tableName
,
attributes
)
{
...
...
@@ -176,40 +110,40 @@ module.exports = (function() {
if
(
definition
.
indexOf
(
'NOT NULL'
)
>
0
)
{
attrSql
+=
Utils
.
_
.
template
(
query
)({
tableName
:
addQuotes
(
tableName
),
query
:
addQuotes
(
attributeName
)
+
' SET NOT NULL'
tableName
:
QueryGenerator
.
addQuotes
(
tableName
),
query
:
QueryGenerator
.
addQuotes
(
attributeName
)
+
' SET NOT NULL'
})
definition
=
definition
.
replace
(
'NOT NULL'
,
''
).
trim
()
}
else
{
attrSql
+=
Utils
.
_
.
template
(
query
)({
tableName
:
addQuotes
(
tableName
),
query
:
addQuotes
(
attributeName
)
+
' DROP NOT NULL'
tableName
:
QueryGenerator
.
addQuotes
(
tableName
),
query
:
QueryGenerator
.
addQuotes
(
attributeName
)
+
' DROP NOT NULL'
})
}
if
(
definition
.
indexOf
(
'DEFAULT'
)
>
0
)
{
attrSql
+=
Utils
.
_
.
template
(
query
)({
tableName
:
addQuotes
(
tableName
),
query
:
addQuotes
(
attributeName
)
+
' SET DEFAULT'
+
definition
.
match
(
/DEFAULT
([^
;
]
+
)
/
)[
1
]
tableName
:
QueryGenerator
.
addQuotes
(
tableName
),
query
:
QueryGenerator
.
addQuotes
(
attributeName
)
+
' SET DEFAULT'
+
definition
.
match
(
/DEFAULT
([^
;
]
+
)
/
)[
1
]
})
definition
=
definition
.
replace
(
/
(
DEFAULT
[^
;
]
+
)
/
,
''
).
trim
()
}
else
{
attrSql
+=
Utils
.
_
.
template
(
query
)({
tableName
:
addQuotes
(
tableName
),
query
:
addQuotes
(
attributeName
)
+
' DROP DEFAULT'
tableName
:
QueryGenerator
.
addQuotes
(
tableName
),
query
:
QueryGenerator
.
addQuotes
(
attributeName
)
+
' DROP DEFAULT'
})
}
if
(
definition
.
match
(
/^ENUM
\(
/
))
{
query
=
pgEnum
(
tableName
,
attributeName
,
definition
)
+
query
query
=
QueryGenerator
.
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
tableName
:
QueryGenerator
.
addQuotes
(
tableName
),
query
:
QueryGenerator
.
addQuotes
(
attributeName
)
+
' TYPE '
+
definition
})
sql
.
push
(
attrSql
)
...
...
@@ -224,12 +158,15 @@ module.exports = (function() {
for
(
var
attributeName
in
attributes
)
{
attrString
.
push
(
Utils
.
_
.
template
(
'<%= before %> TO <%= after %>'
)({
before
:
addQuotes
(
attrBefore
),
after
:
addQuotes
(
attributeName
),
before
:
QueryGenerator
.
addQuotes
(
attrBefore
),
after
:
QueryGenerator
.
addQuotes
(
attributeName
),
}))
}
return
Utils
.
_
.
template
(
query
)({
tableName
:
addQuotes
(
tableName
),
attributes
:
attrString
.
join
(
', '
)
})
return
Utils
.
_
.
template
(
query
)({
tableName
:
QueryGenerator
.
addQuotes
(
tableName
),
attributes
:
attrString
.
join
(
', '
)
})
},
selectQuery
:
function
(
tableName
,
options
)
{
...
...
@@ -237,16 +174,28 @@ module.exports = (function() {
,
table
=
null
options
=
options
||
{}
options
.
table
=
table
=
Array
.
isArray
(
tableName
)
?
tableName
.
map
(
function
(
t
){
return
addQuotes
(
t
);}).
join
(
", "
)
:
addQuotes
(
tableName
)
if
(
Array
.
isArray
(
tableName
))
{
options
.
table
=
table
=
tableName
.
map
(
function
(
t
){
return
QueryGenerator
.
addQuotes
(
t
)
}).
join
(
", "
)
}
else
{
options
.
table
=
table
=
QueryGenerator
.
addQuotes
(
tableName
)
}
options
.
attributes
=
options
.
attributes
&&
options
.
attributes
.
map
(
function
(
attr
)
{
if
(
Array
.
isArray
(
attr
)
&&
attr
.
length
===
2
)
{
return
[
attr
[
0
],
addQuotes
(
removeQuotes
(
attr
[
1
],
'`'
))].
join
(
' as '
)
return
[
attr
[
0
],
QueryGenerator
.
addQuotes
(
QueryGenerator
.
removeQuotes
(
attr
[
1
],
'`'
))
].
join
(
' as '
)
}
else
if
(
attr
.
indexOf
(
'`'
)
>=
0
)
{
return
attr
.
replace
(
/`/g
,
'"'
)
}
else
{
return
addQuotes
(
attr
)
return
QueryGenerator
.
addQuotes
(
attr
)
}
}).
join
(
", "
)
options
.
attributes
=
options
.
attributes
||
'*'
if
(
options
.
include
)
{
...
...
@@ -261,6 +210,7 @@ module.exports = (function() {
optAttributes
=
optAttributes
.
concat
(
attributes
)
var
joinQuery
=
' LEFT OUTER JOIN "<%= table %>" AS "<%= as %>" ON "<%= tableLeft %>"."<%= attrLeft %>" = "<%= tableRight %>"."<%= attrRight %>"'
query
+=
Utils
.
_
.
template
(
joinQuery
)({
table
:
include
.
daoFactory
.
tableName
,
as
:
include
.
as
,
...
...
@@ -280,12 +230,21 @@ module.exports = (function() {
}
if
(
options
.
order
)
{
options
.
order
=
options
.
order
.
replace
(
/
([^
]
+
)(
.*
)
/
,
function
(
m
,
g1
,
g2
)
{
return
addQuotes
(
g1
)
+
g2
})
options
.
order
=
options
.
order
.
replace
(
/
([^
]
+
)(
.*
)
/
,
function
(
m
,
g1
,
g2
)
{
return
QueryGenerator
.
addQuotes
(
g1
)
+
g2
})
query
+=
" ORDER BY <%= order %>"
}
if
(
options
.
group
)
{
options
.
group
=
Array
.
isArray
(
options
.
group
)
?
options
.
group
.
map
(
function
(
grp
){
return
addQuotes
(
grp
)}).
join
(
', '
)
:
addQuotes
(
options
.
group
)
if
(
Array
.
isArray
(
options
.
group
))
{
options
.
group
=
options
.
group
.
map
(
function
(
grp
){
return
QueryGenerator
.
addQuotes
(
grp
)
}).
join
(
', '
)
}
else
{
options
.
group
=
QueryGenerator
.
addQuotes
(
options
.
group
)
}
query
+=
" GROUP BY <%= group %>"
}
...
...
@@ -322,11 +281,13 @@ module.exports = (function() {
});
var
replacements
=
{
table
:
addQuotes
(
tableName
),
attributes
:
Object
.
keys
(
attrValueHash
).
map
(
function
(
attr
){
return
addQuotes
(
attr
)}).
join
(
","
),
values
:
Utils
.
_
.
values
(
attrValueHash
).
map
(
function
(
value
){
return
pgEscape
(
value
)
}).
join
(
","
)
table
:
QueryGenerator
.
addQuotes
(
tableName
)
,
attributes
:
Object
.
keys
(
attrValueHash
).
map
(
function
(
attr
){
return
QueryGenerator
.
addQuotes
(
attr
)
}).
join
(
","
)
,
values
:
Utils
.
_
.
values
(
attrValueHash
).
map
(
function
(
value
){
return
QueryGenerator
.
pgEscape
(
value
)
}).
join
(
","
)
}
return
Utils
.
_
.
template
(
query
)(
replacements
)
...
...
@@ -340,13 +301,13 @@ module.exports = (function() {
for
(
var
key
in
attrValueHash
)
{
var
value
=
attrValueHash
[
key
]
values
.
push
(
addQuotes
(
key
)
+
"="
+
pgEscape
(
value
))
values
.
push
(
QueryGenerator
.
addQuotes
(
key
)
+
"="
+
QueryGenerator
.
pgEscape
(
value
))
}
var
replacements
=
{
table
:
addQuotes
(
tableName
),
table
:
QueryGenerator
.
addQuotes
(
tableName
),
values
:
values
.
join
(
","
),
where
:
QueryGenerator
.
getWhereConditions
(
where
)
where
:
QueryGenerator
.
getWhereConditions
(
where
)
}
return
Utils
.
_
.
template
(
query
)(
replacements
)
...
...
@@ -362,15 +323,17 @@ module.exports = (function() {
var
pks
;
if
(
primaryKeys
[
tableName
]
&&
primaryKeys
[
tableName
].
length
>
0
)
{
pks
=
primaryKeys
[
tableName
].
map
(
function
(
pk
)
{
return
addQuotes
(
pk
)
}).
join
(
','
)
pks
=
primaryKeys
[
tableName
].
map
(
function
(
pk
)
{
return
QueryGenerator
.
addQuotes
(
pk
)
}).
join
(
','
)
}
else
{
pks
=
addQuotes
(
'id'
)
pks
=
QueryGenerator
.
addQuotes
(
'id'
)
}
var
replacements
=
{
table
:
addQuotes
(
tableName
),
table
:
QueryGenerator
.
addQuotes
(
tableName
),
where
:
QueryGenerator
.
getWhereConditions
(
where
),
limit
:
pgEscape
(
options
.
limit
),
limit
:
QueryGenerator
.
pgEscape
(
options
.
limit
),
primaryKeys
:
primaryKeys
[
tableName
].
length
>
1
?
'('
+
pks
+
')'
:
pks
,
primaryKeysSelection
:
pks
}
...
...
@@ -386,13 +349,13 @@ module.exports = (function() {
for
(
var
key
in
attrValueHash
)
{
var
value
=
attrValueHash
[
key
]
values
.
push
(
addQuotes
(
key
)
+
"="
+
addQuotes
(
key
)
+
" + "
+
pgEscape
(
value
))
values
.
push
(
QueryGenerator
.
addQuotes
(
key
)
+
"="
+
QueryGenerator
.
addQuotes
(
key
)
+
" + "
+
QueryGenerator
.
pgEscape
(
value
))
}
var
replacements
=
{
table
:
addQuotes
(
tableName
),
table
:
QueryGenerator
.
addQuotes
(
tableName
),
values
:
values
.
join
(
","
),
where
:
QueryGenerator
.
getWhereConditions
(
where
)
where
:
QueryGenerator
.
getWhereConditions
(
where
)
}
return
Utils
.
_
.
template
(
query
)(
replacements
)
...
...
@@ -402,7 +365,7 @@ module.exports = (function() {
addIndexQuery
:
function
(
tableName
,
attributes
,
options
)
{
var
transformedAttributes
=
attributes
.
map
(
function
(
attribute
)
{
if
(
typeof
attribute
===
'string'
)
{
return
addQuotes
(
attribute
)
return
QueryGenerator
.
addQuotes
(
attribute
)
}
else
{
var
result
=
""
...
...
@@ -410,7 +373,7 @@ module.exports = (function() {
throw
new
Error
(
'The following index attribute has no attribute: '
+
util
.
inspect
(
attribute
))
}
result
+=
addQuotes
(
attribute
.
attribute
)
result
+=
QueryGenerator
.
addQuotes
(
attribute
.
attribute
)
if
(
attribute
.
length
)
{
result
+=
'('
+
attribute
.
length
+
')'
...
...
@@ -431,14 +394,14 @@ module.exports = (function() {
var
indexTable
=
tableName
.
split
(
'.'
)
options
=
Utils
.
_
.
extend
({
indicesType
:
null
,
indexName
:
Utils
.
_
.
underscored
(
indexTable
[
indexTable
.
length
-
1
]
+
'_'
+
onlyAttributeNames
.
join
(
'_'
)),
parser
:
null
indexName
:
Utils
.
_
.
underscored
(
indexTable
[
indexTable
.
length
-
1
]
+
'_'
+
onlyAttributeNames
.
join
(
'_'
)),
parser
:
null
},
options
||
{})
return
Utils
.
_
.
compact
([
"CREATE"
,
options
.
indicesType
,
"INDEX"
,
addQuotes
(
options
.
indexName
),
"CREATE"
,
options
.
indicesType
,
"INDEX"
,
QueryGenerator
.
addQuotes
(
options
.
indexName
),
(
options
.
indexType
?
(
'USING '
+
options
.
indexType
)
:
undefined
),
"ON"
,
addQuotes
(
tableName
),
'('
+
transformedAttributes
.
join
(
', '
)
+
')'
"ON"
,
QueryGenerator
.
addQuotes
(
tableName
),
'('
+
transformedAttributes
.
join
(
', '
)
+
')'
]).
join
(
' '
)
},
...
...
@@ -455,7 +418,10 @@ module.exports = (function() {
indexName
=
Utils
.
_
.
underscored
(
tableName
+
'_'
+
indexNameOrAttributes
.
join
(
'_'
))
}
return
Utils
.
_
.
template
(
sql
)({
tableName
:
addQuotes
(
tableName
),
indexName
:
addQuotes
(
indexName
)
})
return
Utils
.
_
.
template
(
sql
)({
tableName
:
QueryGenerator
.
addQuotes
(
tableName
),
indexName
:
QueryGenerator
.
addQuotes
(
indexName
)
})
},
getWhereConditions
:
function
(
smth
)
{
...
...
@@ -465,7 +431,7 @@ module.exports = (function() {
result
=
QueryGenerator
.
hashToWhereConditions
(
smth
)
}
else
if
(
typeof
smth
===
"number"
)
{
result
=
'\"id\"'
+
"="
+
pgEscape
(
smth
)
result
=
'\"id\"'
+
"="
+
QueryGenerator
.
pgEscape
(
smth
)
}
else
if
(
typeof
smth
===
"string"
)
{
result
=
smth
...
...
@@ -484,23 +450,23 @@ module.exports = (function() {
var
value
=
hash
[
key
]
//handle qualified key names
var
_key
=
key
.
split
(
'.'
).
map
(
function
(
col
){
return
addQuotes
(
col
)}).
join
(
"."
)
var
_key
=
key
.
split
(
'.'
).
map
(
function
(
col
){
return
QueryGenerator
.
addQuotes
(
col
)}).
join
(
"."
)
,
_value
=
null
if
(
Array
.
isArray
(
value
))
{
if
(
value
.
length
==
0
)
{
value
=
[
null
]
}
_value
=
"("
+
value
.
map
(
function
(
subValue
)
{
return
pgEscape
(
subValue
);
return
QueryGenerator
.
pgEscape
(
subValue
);
}).
join
(
','
)
+
")"
result
.
push
([
_key
,
_value
].
join
(
" IN "
))
}
else
if
((
value
)
&&
(
typeof
value
===
"object"
))
{
//using as sentinel for join column => value
_value
=
value
.
join
.
split
(
'.'
).
map
(
function
(
col
){
return
addQuotes
(
col
)}).
join
(
"."
)
_value
=
value
.
join
.
split
(
'.'
).
map
(
function
(
col
){
return
QueryGenerator
.
addQuotes
(
col
)}).
join
(
"."
)
result
.
push
([
_key
,
_value
].
join
(
"="
))
}
else
{
_value
=
pgEscape
(
value
)
_value
=
QueryGenerator
.
pgEscape
(
value
)
result
.
push
((
_value
==
'NULL'
)
?
_key
+
" IS NULL"
:
[
_key
,
_value
].
join
(
"="
))
}
}
...
...
@@ -546,7 +512,7 @@ module.exports = (function() {
if
(
dataType
.
defaultValue
!==
undefined
)
{
template
+=
" DEFAULT <%= defaultValue %>"
replacements
.
defaultValue
=
pgEscape
(
dataType
.
defaultValue
)
replacements
.
defaultValue
=
QueryGenerator
.
pgEscape
(
dataType
.
defaultValue
)
}
if
(
dataType
.
unique
)
{
...
...
@@ -584,13 +550,95 @@ module.exports = (function() {
var
template
=
'<%= protocol %>://<%= user %>:<%= password %>@<%= host %><% if(port) { %>:<%= port %><% } %>/<%= database %>'
;
return
Utils
.
_
.
template
(
template
)({
user
:
encodeURIComponent
(
config
.
username
),
user
:
encodeURIComponent
(
config
.
username
),
password
:
encodeURIComponent
(
config
.
password
),
database
:
config
.
database
,
host
:
config
.
host
,
port
:
config
.
port
,
host
:
config
.
host
,
port
:
config
.
port
,
protocol
:
config
.
protocol
})
},
removeQuotes
:
function
(
s
,
quoteChar
)
{
quoteChar
=
quoteChar
||
'"'
return
s
.
replace
(
new
RegExp
(
quoteChar
,
'g'
),
''
)
},
addQuotes
:
function
(
s
,
quoteChar
)
{
quoteChar
=
quoteChar
||
'"'
return
QueryGenerator
.
removeQuotes
(
s
,
quoteChar
)
.
split
(
'.'
)
.
map
(
function
(
e
)
{
return
quoteChar
+
String
(
e
)
+
quoteChar
})
.
join
(
'.'
)
},
pgEscape
:
function
(
val
)
{
if
(
val
===
undefined
||
val
===
null
)
{
return
'NULL'
;
}
switch
(
typeof
val
)
{
case
'boolean'
:
return
(
val
)
?
'true'
:
'false'
;
case
'number'
:
return
val
+
''
;
case
'object'
:
if
(
Array
.
isArray
(
val
))
{
return
'ARRAY['
+
val
.
map
(
function
(
it
)
{
return
QueryGenerator
.
pgEscape
(
it
)
}).
join
(
','
)
+
']'
;
}
}
if
(
val
instanceof
Date
)
{
val
=
QueryGenerator
.
pgSqlDate
(
val
);
}
// http://www.postgresql.org/docs/8.2/static/sql-syntax-lexical.html#SQL-SYNTAX-STRINGS
val
=
val
.
replace
(
/'/g
,
"''"
);
return
"'"
+
val
+
"'"
;
},
pgEscapeAndQuote
:
function
(
val
)
{
return
QueryGenerator
.
addQuotes
(
QueryGenerator
.
removeQuotes
(
QueryGenerator
.
pgEscape
(
val
),
"'"
))
},
pgEnum
:
function
(
tableName
,
attr
,
dataType
)
{
var
enumName
=
QueryGenerator
.
pgEscapeAndQuote
(
"enum_"
+
tableName
+
"_"
+
attr
)
return
"DROP TYPE IF EXISTS "
+
enumName
+
"; CREATE TYPE "
+
enumName
+
" AS "
+
dataType
.
match
(
/^ENUM
\(
.+
\)
/
)[
0
]
+
"; "
},
padInt
:
function
(
i
)
{
return
(
i
<
10
)
?
'0'
+
i
.
toString
()
:
i
.
toString
()
},
pgSqlDate
:
function
(
dt
)
{
var
date
=
[
dt
.
getUTCFullYear
(),
QueryGenerator
.
padInt
(
dt
.
getUTCMonth
()
+
1
),
QueryGenerator
.
padInt
(
dt
.
getUTCDate
())
].
join
(
'-'
)
var
time
=
[
dt
.
getUTCHours
(),
QueryGenerator
.
padInt
(
dt
.
getUTCMinutes
()),
QueryGenerator
.
padInt
(
dt
.
getUTCSeconds
())].
join
(
':'
)
return
date
+
' '
+
time
+
'.'
+
((
dt
.
getTime
()
%
1000
)
*
1000
)
+
'Z'
},
pgDataTypeMapping
:
function
(
tableName
,
attr
,
dataType
)
{
if
(
Utils
.
_
.
includes
(
dataType
,
'PRIMARY KEY'
))
{
primaryKeys
[
tableName
].
push
(
attr
)
dataType
=
dataType
.
replace
(
/PRIMARY KEY/
,
''
)
}
if
(
Utils
.
_
.
includes
(
dataType
,
'TINYINT(1)'
))
{
dataType
=
dataType
.
replace
(
/TINYINT
\(
1
\)
/
,
'BOOLEAN'
)
}
if
(
Utils
.
_
.
includes
(
dataType
,
'DATETIME'
))
{
dataType
=
dataType
.
replace
(
/DATETIME/
,
'TIMESTAMP WITH TIME ZONE'
)
}
if
(
Utils
.
_
.
includes
(
dataType
,
'SERIAL'
))
{
dataType
=
dataType
.
replace
(
/INTEGER/
,
''
)
dataType
=
dataType
.
replace
(
/NOT NULL/
,
''
)
tables
[
tableName
][
attr
]
=
'serial'
}
if
(
dataType
.
match
(
/^ENUM
\(
/
))
{
dataType
=
dataType
.
replace
(
/^ENUM
\(
.+
\)
/
,
QueryGenerator
.
pgEscapeAndQuote
(
"enum_"
+
tableName
+
"_"
+
attr
))
}
return
dataType
}
}
...
...
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