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 ff99163d
authored
Aug 13, 2013
by
Antoine Marcadet
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Adding the ability to use named parameters as the fourth parameter of query
1 parent
fbc3189b
Show whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
81 additions
and
0 deletions
lib/sequelize.js
lib/sql-string.js
lib/utils.js
test/sequelize.test.js
lib/sequelize.js
View file @
ff99163
...
@@ -215,7 +215,12 @@ module.exports = (function() {
...
@@ -215,7 +215,12 @@ 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
)
{
if
(
Array
.
isArray
(
replacements
))
{
sql
=
Utils
.
format
([
sql
].
concat
(
replacements
),
this
.
options
.
dialect
)
sql
=
Utils
.
format
([
sql
].
concat
(
replacements
),
this
.
options
.
dialect
)
}
else
{
sql
=
Utils
.
formatNamedParameters
(
sql
,
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 @
ff99163
...
@@ -106,6 +106,17 @@ SqlString.format = function(sql, values, timeZone, dialect) {
...
@@ -106,6 +106,17 @@ SqlString.format = function(sql, values, timeZone, dialect) {
});
});
};
};
SqlString
.
formatNamedParameters
=
function
(
sql
,
values
,
timeZone
,
dialect
)
{
return
sql
.
replace
(
/
\:(\w
+
)
/g
,
function
(
value
,
key
)
{
if
(
values
.
hasOwnProperty
(
key
))
{
return
SqlString
.
escape
(
values
[
key
],
false
,
timeZone
,
dialect
);
}
else
{
throw
new
Error
(
'Named parameter "'
+
value
+
'" as no value in the given object.'
);
}
});
};
SqlString
.
dateToString
=
function
(
date
,
timeZone
,
dialect
)
{
SqlString
.
dateToString
=
function
(
date
,
timeZone
,
dialect
)
{
var
dt
=
new
Date
(
date
);
var
dt
=
new
Date
(
date
);
...
...
lib/utils.js
View file @
ff99163
...
@@ -41,6 +41,10 @@ var Utils = module.exports = {
...
@@ -41,6 +41,10 @@ var Utils = module.exports = {
var
timeZone
=
null
;
var
timeZone
=
null
;
return
SqlString
.
format
(
arr
.
shift
(),
arr
,
timeZone
,
dialect
)
return
SqlString
.
format
(
arr
.
shift
(),
arr
,
timeZone
,
dialect
)
},
},
formatNamedParameters
:
function
(
sql
,
parameters
,
dialect
)
{
var
timeZone
=
null
;
return
SqlString
.
formatNamedParameters
(
sql
,
parameters
,
timeZone
,
dialect
)
},
// smartWhere can accept an array of {where} objects, or a single {where} object.
// smartWhere can accept an array of {where} objects, or a single {where} object.
// The smartWhere function breaks down the collection of where objects into a more
// The smartWhere function breaks down the collection of where objects into a more
// centralized object for each column so we can avoid duplicates
// centralized object for each column so we can avoid duplicates
...
...
test/sequelize.test.js
View file @
ff99163
...
@@ -177,6 +177,67 @@ describe(Support.getTestDialectTeaser("Sequelize"), function () {
...
@@ -177,6 +177,67 @@ describe(Support.getTestDialectTeaser("Sequelize"), function () {
})
})
})
})
it
(
'replaces named parameters with the passed object'
,
function
(
done
)
{
this
.
sequelize
.
query
(
'select :one as foo, :two as bar'
,
null
,
{
raw
:
true
},
{
one
:
1
,
two
:
2
}).
success
(
function
(
result
)
{
expect
(
result
).
to
.
deep
.
equal
([{
foo
:
1
,
bar
:
2
}])
done
()
})
})
it
(
'replaces named parameters with the passed object using the same key twice'
,
function
(
done
)
{
this
.
sequelize
.
query
(
'select :one as foo, :two as bar, :one as baz'
,
null
,
{
raw
:
true
},
{
one
:
1
,
two
:
2
}).
success
(
function
(
result
)
{
expect
(
result
).
to
.
deep
.
equal
([{
foo
:
1
,
bar
:
2
,
baz
:
1
}])
done
()
})
})
it
(
'replaces named parameters with the passed object having a null property'
,
function
(
done
)
{
this
.
sequelize
.
query
(
'select :one as foo, :two as bar'
,
null
,
{
raw
:
true
},
{
one
:
1
,
two
:
null
}).
success
(
function
(
result
)
{
expect
(
result
).
to
.
deep
.
equal
([{
foo
:
1
,
bar
:
null
}])
done
()
})
})
it
(
'throw an exception when key is missing in the passed object'
,
function
(
done
)
{
var
self
=
this
expect
(
function
()
{
self
.
sequelize
.
query
(
'select :one as foo, :two as bar, :three as baz'
,
null
,
{
raw
:
true
},
{
one
:
1
,
two
:
2
})
}).
to
.
throw
(
Error
,
/Named parameter ":
\w
+" as no value in the given object
\.
/g
)
done
()
})
it
(
'throw an exception with the passed number'
,
function
(
done
)
{
var
self
=
this
expect
(
function
()
{
self
.
sequelize
.
query
(
'select :one as foo, :two as bar'
,
null
,
{
raw
:
true
},
2
)
}).
to
.
throw
(
Error
,
/Named parameter ":
\w
+" as no value in the given object
\.
/g
)
done
()
})
it
(
'throw an exception with the passed empty object'
,
function
(
done
)
{
var
self
=
this
expect
(
function
()
{
self
.
sequelize
.
query
(
'select :one as foo, :two as bar'
,
null
,
{
raw
:
true
},
{})
}).
to
.
throw
(
Error
,
/Named parameter ":
\w
+" as no value in the given object
\.
/g
)
done
()
})
it
(
'throw an exception with the passed string'
,
function
(
done
)
{
var
self
=
this
expect
(
function
()
{
self
.
sequelize
.
query
(
'select :one as foo, :two as bar'
,
null
,
{
raw
:
true
},
'foobar'
)
}).
to
.
throw
(
Error
,
/Named parameter ":
\w
+" as no value in the given object
\.
/g
)
done
()
})
it
(
'throw an exception with the passed date'
,
function
(
done
)
{
var
self
=
this
expect
(
function
()
{
self
.
sequelize
.
query
(
'select :one as foo, :two as bar'
,
null
,
{
raw
:
true
},
new
Date
())
}).
to
.
throw
(
Error
,
/Named parameter ":
\w
+" as no value in the given object
\.
/g
)
done
()
})
it
(
'handles AS in conjunction with functions just fine'
,
function
(
done
)
{
it
(
'handles AS in conjunction with functions just fine'
,
function
(
done
)
{
this
.
sequelize
.
query
(
'SELECT '
+
(
dialect
===
"sqlite"
?
'date(\'now\')'
:
'NOW()'
)
+
' AS t'
).
success
(
function
(
result
)
{
this
.
sequelize
.
query
(
'SELECT '
+
(
dialect
===
"sqlite"
?
'date(\'now\')'
:
'NOW()'
)
+
' AS t'
).
success
(
function
(
result
)
{
expect
(
moment
(
result
[
0
].
t
).
isValid
()).
to
.
be
.
true
expect
(
moment
(
result
[
0
].
t
).
isValid
()).
to
.
be
.
true
...
...
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