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 4caf1090
authored
Apr 05, 2013
by
zanamixx
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Add string escape for postgresql in custom query
1 parent
477289e6
Hide whitespace changes
Inline
Side-by-side
Showing
6 changed files
with
29 additions
and
19 deletions
.gitignore
lib/sequelize.js
lib/sql-string.js
lib/utils.js
spec-jasmine/config/config.js
spec/config/config.js
.gitignore
View file @
4caf109
...
@@ -4,4 +4,6 @@ test*.js
...
@@ -4,4 +4,6 @@ test*.js
.DS_STORE
.DS_STORE
node_modules
node_modules
npm-debug.log
npm-debug.log
spec/config/config.js
spec-jasmine/config/config.js
*~
*~
lib/sequelize.js
View file @
4caf109
...
@@ -171,7 +171,7 @@ module.exports = (function() {
...
@@ -171,7 +171,7 @@ module.exports = (function() {
Sequelize
.
prototype
.
query
=
function
(
sql
,
callee
,
options
,
replacements
)
{
Sequelize
.
prototype
.
query
=
function
(
sql
,
callee
,
options
,
replacements
)
{
if
(
arguments
.
length
===
4
)
{
if
(
arguments
.
length
===
4
)
{
sql
=
Utils
.
format
([
sql
].
concat
(
replacements
))
sql
=
Utils
.
format
([
sql
].
concat
(
replacements
)
,
this
.
options
.
dialect
)
}
else
if
(
arguments
.
length
===
3
)
{
}
else
if
(
arguments
.
length
===
3
)
{
options
=
options
options
=
options
}
else
if
(
arguments
.
length
===
2
)
{
}
else
if
(
arguments
.
length
===
2
)
{
...
...
lib/sql-string.js
View file @
4caf109
...
@@ -7,7 +7,7 @@ SqlString.escapeId = function (val, forbidQualified) {
...
@@ -7,7 +7,7 @@ SqlString.escapeId = function (val, forbidQualified) {
return
'`'
+
val
.
replace
(
/`/g
,
'``'
).
replace
(
/
\.
/g
,
'`.`'
)
+
'`'
;
return
'`'
+
val
.
replace
(
/`/g
,
'``'
).
replace
(
/
\.
/g
,
'`.`'
)
+
'`'
;
};
};
SqlString
.
escape
=
function
(
val
,
stringifyObjects
,
timeZone
)
{
SqlString
.
escape
=
function
(
val
,
stringifyObjects
,
timeZone
,
dialect
)
{
if
(
val
===
undefined
||
val
===
null
)
{
if
(
val
===
undefined
||
val
===
null
)
{
return
'NULL'
;
return
'NULL'
;
}
}
...
@@ -37,17 +37,22 @@ SqlString.escape = function(val, stringifyObjects, timeZone) {
...
@@ -37,17 +37,22 @@ SqlString.escape = function(val, stringifyObjects, timeZone) {
}
}
}
}
val
=
val
.
replace
(
/
[\0\n\r\b\t\\\'\"\x
1a
]
/g
,
function
(
s
)
{
if
(
dialect
==
"postgres"
)
{
switch
(
s
)
{
// http://www.postgresql.org/docs/8.2/static/sql-syntax-lexical.html#SQL-SYNTAX-STRINGS
case
"\0"
:
return
"\\0"
;
val
=
val
.
replace
(
/'/g
,
"''"
);
case
"\n"
:
return
"\\n"
;
}
else
{
case
"\r"
:
return
"\\r"
;
val
=
val
.
replace
(
/
[\0\n\r\b\t\\\'\"\x
1a
]
/g
,
function
(
s
)
{
case
"\b"
:
return
"\\b"
;
switch
(
s
)
{
case
"\t"
:
return
"\\t"
;
case
"\0"
:
return
"\\0"
;
case
"\x1a"
:
return
"\\Z"
;
case
"\n"
:
return
"\\n"
;
default
:
return
"\\"
+
s
;
case
"\r"
:
return
"\\r"
;
}
case
"\b"
:
return
"\\b"
;
});
case
"\t"
:
return
"\\t"
;
case
"\x1a"
:
return
"\\Z"
;
default
:
return
"\\"
+
s
;
}
});
}
return
"'"
+
val
+
"'"
;
return
"'"
+
val
+
"'"
;
};
};
...
@@ -58,7 +63,7 @@ SqlString.arrayToList = function(array, timeZone) {
...
@@ -58,7 +63,7 @@ SqlString.arrayToList = function(array, timeZone) {
}).
join
(
', '
);
}).
join
(
', '
);
};
};
SqlString
.
format
=
function
(
sql
,
values
,
timeZone
)
{
SqlString
.
format
=
function
(
sql
,
values
,
timeZone
,
dialect
)
{
values
=
[].
concat
(
values
);
values
=
[].
concat
(
values
);
return
sql
.
replace
(
/
\?
/g
,
function
(
match
)
{
return
sql
.
replace
(
/
\?
/g
,
function
(
match
)
{
...
@@ -66,7 +71,7 @@ SqlString.format = function(sql, values, timeZone) {
...
@@ -66,7 +71,7 @@ SqlString.format = function(sql, values, timeZone) {
return
match
;
return
match
;
}
}
return
SqlString
.
escape
(
values
.
shift
(),
false
,
timeZone
);
return
SqlString
.
escape
(
values
.
shift
(),
false
,
timeZone
,
dialect
);
});
});
};
};
...
...
lib/utils.js
View file @
4caf109
...
@@ -47,8 +47,9 @@ var Utils = module.exports = {
...
@@ -47,8 +47,9 @@ var Utils = module.exports = {
escape
:
function
(
s
)
{
escape
:
function
(
s
)
{
return
SqlString
.
escape
(
s
,
true
,
"local"
).
replace
(
/
\\
"/g
,
'"'
)
return
SqlString
.
escape
(
s
,
true
,
"local"
).
replace
(
/
\\
"/g
,
'"'
)
},
},
format
:
function
(
arr
)
{
format
:
function
(
arr
,
dialect
)
{
return
SqlString
.
format
(
arr
.
shift
(),
arr
)
var
timeZone
=
null
;
return
SqlString
.
format
(
arr
.
shift
(),
arr
,
timeZone
,
dialect
)
},
},
isHash
:
function
(
obj
)
{
isHash
:
function
(
obj
)
{
return
Utils
.
_
.
isObject
(
obj
)
&&
!
Array
.
isArray
(
obj
);
return
Utils
.
_
.
isObject
(
obj
)
&&
!
Array
.
isArray
(
obj
);
...
...
spec-jasmine/config/config.js
View file @
4caf109
...
@@ -18,7 +18,8 @@ module.exports = {
...
@@ -18,7 +18,8 @@ module.exports = {
postgres
:
{
postgres
:
{
database
:
'sequelize_test'
,
database
:
'sequelize_test'
,
username
:
"postgres"
,
username
:
"root"
,
password
:
"toor"
,
port
:
5432
,
port
:
5432
,
pool
:
{
maxConnections
:
5
,
maxIdleTime
:
30
}
pool
:
{
maxConnections
:
5
,
maxIdleTime
:
30
}
}
}
...
...
spec/config/config.js
View file @
4caf109
...
@@ -24,7 +24,8 @@ module.exports = {
...
@@ -24,7 +24,8 @@ module.exports = {
postgres
:
{
postgres
:
{
database
:
'sequelize_test'
,
database
:
'sequelize_test'
,
username
:
"postgres"
,
username
:
"root"
,
password
:
"toor"
,
port
:
5432
,
port
:
5432
,
pool
:
{
maxConnections
:
5
,
maxIdleTime
:
30
}
pool
:
{
maxConnections
:
5
,
maxIdleTime
:
30
}
}
}
...
...
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