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 f6cb74ae
authored
Jun 12, 2016
by
Felix Becker
Committed by
Mick Hansen
Jun 12, 2016
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
ES6 refactor of sql-string.js (#6080)
let, const, arrow functions, exports
1 parent
7dc351f5
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
23 additions
and
20 deletions
lib/sql-string.js
lib/sql-string.js
View file @
f6cb74a
'use strict'
;
'use strict'
;
/* jshint -W110 */
/* jshint -W110 */
var
dataTypes
=
require
(
'./data-types'
)
const
dataTypes
=
require
(
'./data-types'
);
,
_
=
require
(
'lodash'
)
const
_
=
require
(
'lodash'
);
,
SqlString
=
exports
;
SqlString
.
escapeId
=
function
(
val
,
forbidQualified
)
{
function
escapeId
(
val
,
forbidQualified
)
{
if
(
forbidQualified
)
{
if
(
forbidQualified
)
{
return
'`'
+
val
.
replace
(
/`/g
,
'``'
)
+
'`'
;
return
'`'
+
val
.
replace
(
/`/g
,
'``'
)
+
'`'
;
}
}
return
'`'
+
val
.
replace
(
/`/g
,
'``'
).
replace
(
/
\.
/g
,
'`.`'
)
+
'`'
;
return
'`'
+
val
.
replace
(
/`/g
,
'``'
).
replace
(
/
\.
/g
,
'`.`'
)
+
'`'
;
};
}
exports
.
escapeId
=
escapeId
;
SqlString
.
escape
=
function
(
val
,
timeZone
,
dialect
,
format
)
{
function
escape
(
val
,
timeZone
,
dialect
,
format
)
{
var
prependN
=
false
;
let
prependN
=
false
;
if
(
val
===
undefined
||
val
===
null
)
{
if
(
val
===
undefined
||
val
===
null
)
{
return
'NULL'
;
return
'NULL'
;
}
}
...
@@ -48,11 +48,11 @@ SqlString.escape = function(val, timeZone, dialect, format) {
...
@@ -48,11 +48,11 @@ SqlString.escape = function(val, timeZone, dialect, format) {
}
}
if
(
Array
.
isArray
(
val
))
{
if
(
Array
.
isArray
(
val
))
{
var
escape
=
_
.
partial
(
SqlString
.
escape
,
_
,
timeZone
,
dialect
,
format
);
const
partialEscape
=
_
.
partial
(
escape
,
_
,
timeZone
,
dialect
,
format
);
if
(
dialect
===
'postgres'
&&
!
format
)
{
if
(
dialect
===
'postgres'
&&
!
format
)
{
return
dataTypes
.
ARRAY
.
prototype
.
stringify
(
val
,
{
escape
:
escape
});
return
dataTypes
.
ARRAY
.
prototype
.
stringify
(
val
,
{
escape
});
}
}
return
val
.
map
(
e
scape
);
return
val
.
map
(
partialE
scape
);
}
}
if
(
!
val
.
replace
)
{
if
(
!
val
.
replace
)
{
...
@@ -64,7 +64,7 @@ SqlString.escape = function(val, timeZone, dialect, format) {
...
@@ -64,7 +64,7 @@ SqlString.escape = function(val, timeZone, dialect, format) {
// http://stackoverflow.com/q/603572/130598
// http://stackoverflow.com/q/603572/130598
val
=
val
.
replace
(
/'/g
,
"''"
);
val
=
val
.
replace
(
/'/g
,
"''"
);
}
else
{
}
else
{
val
=
val
.
replace
(
/
[\0\n\r\b\t\\\'\"\x
1a
]
/g
,
function
(
s
)
{
val
=
val
.
replace
(
/
[\0\n\r\b\t\\\'\"\x
1a
]
/g
,
s
=>
{
switch
(
s
)
{
switch
(
s
)
{
case
'\0'
:
return
'\\0'
;
case
'\0'
:
return
'\\0'
;
case
'\n'
:
return
'\\n'
;
case
'\n'
:
return
'\\n'
;
...
@@ -77,30 +77,33 @@ SqlString.escape = function(val, timeZone, dialect, format) {
...
@@ -77,30 +77,33 @@ SqlString.escape = function(val, timeZone, dialect, format) {
});
});
}
}
return
(
prependN
?
"N'"
:
"'"
)
+
val
+
"'"
;
return
(
prependN
?
"N'"
:
"'"
)
+
val
+
"'"
;
};
}
exports
.
escape
=
escape
;
SqlString
.
format
=
function
(
sql
,
values
,
timeZone
,
dialect
)
{
function
format
(
sql
,
values
,
timeZone
,
dialect
)
{
values
=
[].
concat
(
values
);
values
=
[].
concat
(
values
);
return
sql
.
replace
(
/
\?
/g
,
function
(
match
)
{
return
sql
.
replace
(
/
\?
/g
,
match
=>
{
if
(
!
values
.
length
)
{
if
(
!
values
.
length
)
{
return
match
;
return
match
;
}
}
return
SqlString
.
escape
(
values
.
shift
(),
timeZone
,
dialect
,
true
);
return
escape
(
values
.
shift
(),
timeZone
,
dialect
,
true
);
});
});
};
}
exports
.
format
=
format
;
SqlString
.
formatNamedParameters
=
function
(
sql
,
values
,
timeZone
,
dialect
)
{
function
formatNamedParameters
(
sql
,
values
,
timeZone
,
dialect
)
{
return
sql
.
replace
(
/
\:
+
(?!\d)(\w
+
)
/g
,
function
(
value
,
key
)
{
return
sql
.
replace
(
/
\:
+
(?!\d)(\w
+
)
/g
,
(
value
,
key
)
=>
{
if
(
'postgres'
===
dialect
&&
'::'
===
value
.
slice
(
0
,
2
))
{
if
(
'postgres'
===
dialect
&&
'::'
===
value
.
slice
(
0
,
2
))
{
return
value
;
return
value
;
}
}
if
(
values
[
key
]
!==
undefined
)
{
if
(
values
[
key
]
!==
undefined
)
{
return
SqlString
.
escape
(
values
[
key
],
timeZone
,
dialect
,
true
);
return
escape
(
values
[
key
],
timeZone
,
dialect
,
true
);
}
else
{
}
else
{
throw
new
Error
(
'Named parameter "'
+
value
+
'" has no value in the given object.'
);
throw
new
Error
(
'Named parameter "'
+
value
+
'" has no value in the given object.'
);
}
}
});
});
};
}
exports
.
formatNamedParameters
=
formatNamedParameters
;
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