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 dd8782ce
authored
Mar 07, 2014
by
Jan Aagaard Meier
Browse files
Options
Browse Files
Download
Plain Diff
Merge pull request #1458 from tomchentw/hotfix/hstore_checking_fn
fix(dao): HSTORE checking like ENUM
2 parents
7e82661d
e97fb601
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
38 additions
and
19 deletions
lib/dao.js
lib/dialects/postgres/hstore.js
lib/dialects/postgres/query.js
test/postgres/dao.test.js
lib/dao.js
View file @
dd8782c
...
@@ -295,8 +295,8 @@ module.exports = (function() {
...
@@ -295,8 +295,8 @@ module.exports = (function() {
for
(
var
attrName
in
self
.
Model
.
rawAttributes
)
{
for
(
var
attrName
in
self
.
Model
.
rawAttributes
)
{
if
(
self
.
Model
.
rawAttributes
.
hasOwnProperty
(
attrName
))
{
if
(
self
.
Model
.
rawAttributes
.
hasOwnProperty
(
attrName
))
{
var
definition
=
self
.
Model
.
rawAttributes
[
attrName
]
var
definition
=
self
.
Model
.
rawAttributes
[
attrName
]
,
isHstore
=
!!
definition
.
type
&&
!!
definition
.
type
.
type
&&
definition
.
type
.
type
===
DataTypes
.
HSTORE
.
type
,
isHstore
=
!!
definition
.
type
&&
(
definition
.
type
.
toString
()
===
DataTypes
.
HSTORE
.
toString
())
,
isEnum
=
definition
.
type
&&
(
definition
.
type
.
toString
()
===
DataTypes
.
ENUM
.
toString
())
,
isEnum
=
!!
definition
.
type
&&
(
definition
.
type
.
toString
()
===
DataTypes
.
ENUM
.
toString
())
,
isMySQL
=
[
'mysql'
,
'mariadb'
].
indexOf
(
self
.
Model
.
daoFactoryManager
.
sequelize
.
options
.
dialect
)
!==
-
1
,
isMySQL
=
[
'mysql'
,
'mariadb'
].
indexOf
(
self
.
Model
.
daoFactoryManager
.
sequelize
.
options
.
dialect
)
!==
-
1
,
ciCollation
=
!!
self
.
Model
.
options
.
collate
&&
self
.
Model
.
options
.
collate
.
match
(
/_ci$/i
)
,
ciCollation
=
!!
self
.
Model
.
options
.
collate
&&
self
.
Model
.
options
.
collate
.
match
(
/_ci$/i
)
,
valueOutOfScope
,
valueOutOfScope
...
...
lib/dialects/postgres/hstore.js
View file @
dd8782c
...
@@ -34,9 +34,14 @@ module.exports = {
...
@@ -34,9 +34,14 @@ module.exports = {
}
}
},
},
parse
:
function
(
string
)
{
parse
:
function
(
string
)
{
var
self
=
this
var
self
=
this
,
object
=
{
}
if
((
'string'
!==
typeof
string
)
||
(
0
===
string
.
length
))
{
return
object
;
}
const
rx
=
/
\"((?:\\\"
|
[^
"
])
*
)\"\s
*
\=\>\s
*
((?:
true|false|NULL|
\d
+|
\d
+
\.\d
+|
\"((?:\\\"
|
[^
"
])
*
)\"))
/g
const
rx
=
/
\"((?:\\\"
|
[^
"
])
*
)\"\s
*
\=\>\s
*
((?:
true|false|NULL|
\d
+|
\d
+
\.\d
+|
\"((?:\\\"
|
[^
"
])
*
)\"))
/g
var
object
=
{
}
string
.
replace
(
rx
,
function
(
match
,
key
,
value
,
innerValue
)
{
string
.
replace
(
rx
,
function
(
match
,
key
,
value
,
innerValue
)
{
switch
(
value
)
{
switch
(
value
)
{
...
...
lib/dialects/postgres/query.js
View file @
dd8782c
...
@@ -137,7 +137,7 @@ module.exports = (function() {
...
@@ -137,7 +137,7 @@ module.exports = (function() {
for
(
var
key
in
rows
[
0
])
{
for
(
var
key
in
rows
[
0
])
{
if
(
rows
[
0
].
hasOwnProperty
(
key
))
{
if
(
rows
[
0
].
hasOwnProperty
(
key
))
{
var
record
=
rows
[
0
][
key
]
var
record
=
rows
[
0
][
key
]
if
(
!!
this
.
callee
.
Model
&&
!!
this
.
callee
.
Model
.
rawAttributes
&&
!!
this
.
callee
.
Model
.
rawAttributes
[
key
]
&&
!!
this
.
callee
.
Model
.
rawAttributes
[
key
].
type
&&
!!
this
.
callee
.
Model
.
rawAttributes
[
key
].
type
.
type
&&
this
.
callee
.
Model
.
rawAttributes
[
key
].
type
.
type
===
DataTypes
.
HSTORE
.
type
)
{
if
(
!!
this
.
callee
.
Model
&&
!!
this
.
callee
.
Model
.
rawAttributes
&&
!!
this
.
callee
.
Model
.
rawAttributes
[
key
]
&&
!!
this
.
callee
.
Model
.
rawAttributes
[
key
].
type
&&
this
.
callee
.
Model
.
rawAttributes
[
key
].
type
.
toString
()
===
DataTypes
.
HSTORE
.
toString
()
)
{
record
=
hstore
.
parse
(
record
)
record
=
hstore
.
parse
(
record
)
}
}
this
.
callee
.
dataValues
[
key
]
=
record
this
.
callee
.
dataValues
[
key
]
=
record
...
@@ -152,7 +152,7 @@ module.exports = (function() {
...
@@ -152,7 +152,7 @@ module.exports = (function() {
for
(
var
key
in
rows
[
0
])
{
for
(
var
key
in
rows
[
0
])
{
if
(
rows
[
0
].
hasOwnProperty
(
key
))
{
if
(
rows
[
0
].
hasOwnProperty
(
key
))
{
var
record
=
rows
[
0
][
key
]
var
record
=
rows
[
0
][
key
]
if
(
!!
this
.
callee
.
Model
&&
!!
this
.
callee
.
Model
.
rawAttributes
&&
!!
this
.
callee
.
Model
.
rawAttributes
[
key
]
&&
!!
this
.
callee
.
Model
.
rawAttributes
[
key
].
type
&&
!!
this
.
callee
.
Model
.
rawAttributes
[
key
].
type
.
type
&&
this
.
callee
.
Model
.
rawAttributes
[
key
].
type
.
type
===
DataTypes
.
HSTORE
.
type
)
{
if
(
!!
this
.
callee
.
Model
&&
!!
this
.
callee
.
Model
.
rawAttributes
&&
!!
this
.
callee
.
Model
.
rawAttributes
[
key
]
&&
!!
this
.
callee
.
Model
.
rawAttributes
[
key
].
type
&&
this
.
callee
.
Model
.
rawAttributes
[
key
].
type
.
toString
()
===
DataTypes
.
HSTORE
.
toString
()
)
{
record
=
hstore
.
parse
(
record
)
record
=
hstore
.
parse
(
record
)
}
}
this
.
callee
.
dataValues
[
key
]
=
record
this
.
callee
.
dataValues
[
key
]
=
record
...
...
test/postgres/dao.test.js
View file @
dd8782c
...
@@ -14,6 +14,7 @@ if (dialect.match(/^postgres/)) {
...
@@ -14,6 +14,7 @@ if (dialect.match(/^postgres/)) {
this
.
User
=
this
.
sequelize
.
define
(
'User'
,
{
this
.
User
=
this
.
sequelize
.
define
(
'User'
,
{
username
:
DataTypes
.
STRING
,
username
:
DataTypes
.
STRING
,
email
:
{
type
:
DataTypes
.
ARRAY
(
DataTypes
.
TEXT
)},
email
:
{
type
:
DataTypes
.
ARRAY
(
DataTypes
.
TEXT
)},
settings
:
DataTypes
.
HSTORE
,
document
:
{
type
:
DataTypes
.
HSTORE
,
defaultValue
:
'"default"=>"value"'
}
document
:
{
type
:
DataTypes
.
HSTORE
,
defaultValue
:
'"default"=>"value"'
}
})
})
this
.
User
.
sync
({
force
:
true
}).
success
(
function
()
{
this
.
User
.
sync
({
force
:
true
}).
success
(
function
()
{
...
@@ -48,10 +49,25 @@ if (dialect.match(/^postgres/)) {
...
@@ -48,10 +49,25 @@ if (dialect.match(/^postgres/)) {
})
})
})
})
it
(
'describeTable should tell me that a column is hstore and not USER-DEFINED'
,
function
(
done
)
{
describe
(
'hstore'
,
function
()
{
this
.
sequelize
.
queryInterface
.
describeTable
(
'Users'
).
success
(
function
(
table
)
{
it
(
'should tell me that a column is hstore and not USER-DEFINED'
,
function
(
done
)
{
expect
(
table
.
document
.
type
).
to
.
equal
(
'HSTORE'
)
this
.
sequelize
.
queryInterface
.
describeTable
(
'Users'
).
success
(
function
(
table
)
{
done
()
expect
(
table
.
settings
.
type
).
to
.
equal
(
'HSTORE'
)
expect
(
table
.
document
.
type
).
to
.
equal
(
'HSTORE'
)
done
()
})
})
it
(
'should stringify hstore with insert'
,
function
(
done
)
{
this
.
User
.
create
({
username
:
'bob'
,
email
:
[
'myemail@email.com'
],
settings
:
{
mailing
:
false
,
push
:
'facebook'
,
frequency
:
3
}
}).
on
(
'sql'
,
function
(
sql
)
{
var
expected
=
'INSERT INTO "Users" ("id","username","email","settings","document","createdAt","updatedAt") VALUES (DEFAULT,\'bob\',ARRAY[\'myemail@email.com\']::TEXT[],\'"mailing"=>false,"push"=>"facebook","frequency"=>3\',\'"default"=>"value"\''
expect
(
sql
.
indexOf
(
expected
)).
to
.
equal
(
0
)
done
()
})
})
})
})
})
...
@@ -242,19 +258,17 @@ if (dialect.match(/^postgres/)) {
...
@@ -242,19 +258,17 @@ if (dialect.match(/^postgres/)) {
var
self
=
this
var
self
=
this
this
.
User
this
.
User
.
create
({
username
:
'user'
,
email
:
[
'foo@bar.com'
],
document
:
{
created
:
{
test
:
'"value"'
}}})
.
create
({
username
:
'user'
,
email
:
[
'foo@bar.com'
],
settings
:
{
created
:
{
test
:
'"value"'
}}})
.
success
(
function
(
newUser
)
{
.
success
(
function
(
newUser
)
{
expect
(
newUser
.
document
).
to
.
deep
.
equal
({
created
:
{
test
:
'"value"'
}})
// Check to see if the default value for an hstore field works
expect
(
newUser
.
document
).
to
.
deep
.
equal
({
default
:
'value'
})
expect
(
newUser
.
settings
).
to
.
deep
.
equal
({
created
:
{
test
:
'"value"'
}})
// Check to see if updating an hstore field works
// Check to see if updating an hstore field works
newUser
.
updateAttributes
({
document
:
{
should
:
'update'
,
to
:
'this'
,
first
:
'place'
}}).
success
(
function
(
oldUser
){
newUser
.
updateAttributes
({
settings
:
{
should
:
'update'
,
to
:
'this'
,
first
:
'place'
}}).
success
(
function
(
oldUser
){
// Postgres always returns keys in alphabetical order (ascending)
// Postgres always returns keys in alphabetical order (ascending)
expect
(
oldUser
.
document
).
to
.
deep
.
equal
({
first
:
'place'
,
should
:
'update'
,
to
:
'this'
})
expect
(
oldUser
.
settings
).
to
.
deep
.
equal
({
first
:
'place'
,
should
:
'update'
,
to
:
'this'
})
// Check to see if the default value for an hstore field works
done
()
self
.
User
.
create
({
username
:
'user2'
,
email
:
[
'bar@baz.com'
]}).
success
(
function
(
defaultUser
){
expect
(
defaultUser
.
document
).
to
.
deep
.
equal
({
default
:
'value'
})
done
()
})
})
})
})
})
.
error
(
console
.
log
)
.
error
(
console
.
log
)
...
...
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