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 3cda2ae6
authored
Jun 19, 2013
by
Daniel Durante
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
PostgreSQL should now be able to insert empty arrays with typecasting. Closes #714
1 parent
2eb96be5
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
34 additions
and
7 deletions
lib/dialects/postgres/query-generator.js
lib/query-interface.js
spec/dao-factory.spec.js
lib/dialects/postgres/query-generator.js
View file @
3cda2ae
...
...
@@ -289,7 +289,7 @@ module.exports = (function() {
return
Utils
.
_
.
template
(
query
)(
options
)
},
insertQuery
:
function
(
tableName
,
attrValueHash
)
{
insertQuery
:
function
(
tableName
,
attrValueHash
,
attributes
)
{
attrValueHash
=
Utils
.
removeNullValuesFromHash
(
attrValueHash
,
this
.
options
.
omitNull
)
var
query
=
"INSERT INTO <%= table %> (<%= attributes %>) VALUES (<%= values %>) RETURNING *;"
...
...
@@ -306,14 +306,17 @@ module.exports = (function() {
}
});
var
rowValues
=
[]
Object
.
keys
(
attrValueHash
).
forEach
(
function
(
attr
)
{
rowValues
[
rowValues
.
length
]
=
this
.
escape
(
attrValueHash
[
attr
],
(
!!
attributes
&&
!!
attributes
[
attr
]
?
attributes
[
attr
]
:
undefined
))
}.
bind
(
this
))
var
replacements
=
{
table
:
this
.
quoteIdentifiers
(
tableName
)
,
attributes
:
Object
.
keys
(
attrValueHash
).
map
(
function
(
attr
){
return
this
.
quoteIdentifier
(
attr
)
}.
bind
(
this
)).
join
(
","
)
,
values
:
Utils
.
_
.
values
(
attrValueHash
).
map
(
function
(
value
){
return
this
.
escape
(
value
)
}.
bind
(
this
)).
join
(
","
)
,
values
:
rowValues
.
join
(
","
)
}
return
Utils
.
_
.
template
(
query
)(
replacements
)
...
...
@@ -734,7 +737,7 @@ module.exports = (function() {
return
identifiers
.
split
(
'.'
).
map
(
function
(
t
)
{
return
this
.
quoteIdentifier
(
t
,
force
)
}.
bind
(
this
)).
join
(
'.'
)
},
escape
:
function
(
val
)
{
escape
:
function
(
val
,
field
)
{
if
(
val
===
undefined
||
val
===
null
)
{
return
'NULL'
;
}
...
...
@@ -746,7 +749,11 @@ module.exports = (function() {
return
val
+
''
;
case
'object'
:
if
(
Array
.
isArray
(
val
))
{
return
'ARRAY['
+
val
.
map
(
function
(
it
)
{
return
this
.
escape
(
it
)
}.
bind
(
this
)).
join
(
','
)
+
']'
;
var
ret
=
'ARRAY['
+
val
.
map
(
function
(
it
)
{
return
this
.
escape
(
it
)
}.
bind
(
this
)).
join
(
','
)
+
']'
if
(
!!
field
&&
!!
field
.
type
)
{
ret
+=
'::'
+
field
.
type
.
replace
(
/
\(\d
+
\)
/g
,
''
)
}
return
ret
}
}
...
...
lib/query-interface.js
View file @
3cda2ae
...
...
@@ -254,7 +254,7 @@ module.exports = (function() {
}
QueryInterface
.
prototype
.
insert
=
function
(
dao
,
tableName
,
values
)
{
var
sql
=
this
.
QueryGenerator
.
insertQuery
(
tableName
,
values
)
var
sql
=
this
.
QueryGenerator
.
insertQuery
(
tableName
,
values
,
dao
.
daoFactory
.
rawAttributes
)
return
queryAndEmit
.
call
(
this
,
[
sql
,
dao
],
'insert'
,
{
success
:
function
(
obj
)
{
obj
.
isNewRecord
=
false
}
})
...
...
spec/dao-factory.spec.js
View file @
3cda2ae
...
...
@@ -281,6 +281,26 @@ describe(Helpers.getTestDialectTeaser("DAOFactory"), function() {
})
describe
(
'create'
,
function
()
{
it
(
"casts empty arrays correctly for postgresql"
,
function
(
done
)
{
if
(
dialect
!==
"postgres"
&&
dialect
!==
"postgresql-native"
)
{
expect
(
''
).
toEqual
(
''
)
return
done
()
}
var
User
=
this
.
sequelize
.
define
(
'UserWithArray'
,
{
myvals
:
{
type
:
Sequelize
.
ARRAY
(
Sequelize
.
INTEGER
)
},
mystr
:
{
type
:
Sequelize
.
ARRAY
(
Sequelize
.
STRING
)
}
})
User
.
sync
({
force
:
true
}).
success
(
function
()
{
User
.
create
({
myvals
:
[],
mystr
:
[]}).
on
(
'sql'
,
function
(
sql
){
expect
(
sql
.
indexOf
(
'ARRAY[]::INTEGER[]'
)).
toBeGreaterThan
(
-
1
)
expect
(
sql
.
indexOf
(
'ARRAY[]::VARCHAR[]'
)).
toBeGreaterThan
(
-
1
)
done
()
})
})
})
it
(
"doesn't allow duplicated records with unique:true"
,
function
(
done
)
{
var
User
=
this
.
sequelize
.
define
(
'UserWithUniqueUsername'
,
{
username
:
{
type
:
Sequelize
.
STRING
,
unique
:
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