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 18910ff1
authored
Feb 12, 2014
by
Jan Aagaard Meier
Browse files
Options
Browse Files
Download
Plain Diff
Merge pull request #1364 from SohumB/bugfix/use-ishash-to-avoid-buffers
Fixes #1358
2 parents
8aafd1b1
d6f936e4
Hide whitespace changes
Inline
Side-by-side
Showing
5 changed files
with
90 additions
and
11 deletions
lib/dao-factory.js
lib/dialects/abstract/query-generator.js
lib/dialects/postgres/query-generator.js
lib/utils.js
test/dao-factory/findAll.test.js
lib/dao-factory.js
View file @
18910ff
...
@@ -526,7 +526,10 @@ module.exports = (function() {
...
@@ -526,7 +526,10 @@ module.exports = (function() {
options
=
{
where
:
parsedId
}
options
=
{
where
:
parsedId
}
}
else
if
(
typeof
options
===
'object'
)
{
}
else
if
(
typeof
options
===
'object'
)
{
options
=
Utils
.
_
.
clone
(
options
)
options
=
Utils
.
_
.
clone
(
options
,
function
(
thing
)
{
if
(
Buffer
.
isBuffer
(
thing
))
{
return
thing
}
return
undefined
;
})
if
(
options
.
hasOwnProperty
(
'include'
)
&&
options
.
include
)
{
if
(
options
.
hasOwnProperty
(
'include'
)
&&
options
.
include
)
{
hasJoin
=
true
hasJoin
=
true
...
@@ -1443,11 +1446,14 @@ module.exports = (function() {
...
@@ -1443,11 +1446,14 @@ module.exports = (function() {
var
optClone
=
function
(
options
)
{
var
optClone
=
function
(
options
)
{
return
Utils
.
_
.
cloneDeep
(
options
,
function
(
elem
)
{
return
Utils
.
_
.
cloneDeep
(
options
,
function
(
elem
)
{
// The DAOFactories used for include are pass by ref, so don't clone them.
Otherwise return undefined, meaning, 'handle this lodash'
// The DAOFactories used for include are pass by ref, so don't clone them.
if
(
elem
instanceof
DAOFactory
||
elem
instanceof
Utils
.
col
||
elem
instanceof
Utils
.
literal
||
elem
instanceof
Utils
.
cast
||
elem
instanceof
Utils
.
fn
||
elem
instanceof
Utils
.
and
||
elem
instanceof
Utils
.
or
)
{
if
(
elem
instanceof
DAOFactory
||
elem
instanceof
Utils
.
col
||
elem
instanceof
Utils
.
literal
||
elem
instanceof
Utils
.
cast
||
elem
instanceof
Utils
.
fn
||
elem
instanceof
Utils
.
and
||
elem
instanceof
Utils
.
or
)
{
return
elem
return
elem
}
}
// Unfortunately, lodash.cloneDeep doesn't preserve Buffer.isBuffer, which we have to rely on for binary data
if
(
Buffer
.
isBuffer
(
elem
))
{
return
elem
;
}
// Otherwise return undefined, meaning, 'handle this lodash'
return
undefined
return
undefined
})
})
}
}
...
...
lib/dialects/abstract/query-generator.js
View file @
18910ff
...
@@ -901,6 +901,8 @@ module.exports = (function() {
...
@@ -901,6 +901,8 @@ module.exports = (function() {
result
=
this
.
hashToWhereConditions
(
smth
)
result
=
this
.
hashToWhereConditions
(
smth
)
}
else
if
(
typeof
smth
===
"string"
)
{
}
else
if
(
typeof
smth
===
"string"
)
{
result
=
smth
result
=
smth
}
else
if
(
Buffer
.
isBuffer
(
smth
))
{
result
=
this
.
escape
(
smth
)
}
else
if
(
Array
.
isArray
(
smth
))
{
}
else
if
(
Array
.
isArray
(
smth
))
{
var
treatAsAnd
=
smth
.
reduce
(
function
(
treatAsAnd
,
arg
)
{
var
treatAsAnd
=
smth
.
reduce
(
function
(
treatAsAnd
,
arg
)
{
if
(
treatAsAnd
)
{
if
(
treatAsAnd
)
{
...
...
lib/dialects/postgres/query-generator.js
View file @
18910ff
...
@@ -173,10 +173,10 @@ module.exports = (function() {
...
@@ -173,10 +173,10 @@ module.exports = (function() {
}
}
}
}
if
(
col
&&
((
!!
coltype
&&
coltype
.
match
(
/
\[\]
$/
)
!==
null
)
||
(
col
.
toString
().
match
(
/
\[\]
$/
)
!==
null
)))
{
if
(
col
&&
((
!!
coltype
&&
coltype
.
match
(
/
\[\]
$/
)
!==
null
)
||
(
col
.
toString
().
match
(
/
\[\]
$/
)
!==
null
)))
{
_value
=
'ARRAY['
+
value
.
map
(
this
.
escape
).
join
(
','
)
+
']::'
+
(
!!
col
.
type
?
col
.
type
:
col
.
toString
())
_value
=
'ARRAY['
+
value
.
map
(
this
.
escape
.
bind
(
this
)
).
join
(
','
)
+
']::'
+
(
!!
col
.
type
?
col
.
type
:
col
.
toString
())
return
[
_key
,
_value
].
join
(
" && "
)
return
[
_key
,
_value
].
join
(
" && "
)
}
else
{
}
else
{
_value
=
"("
+
value
.
map
(
this
.
escape
).
join
(
','
)
+
")"
_value
=
"("
+
value
.
map
(
this
.
escape
.
bind
(
this
)
).
join
(
','
)
+
")"
return
[
_key
,
_value
].
join
(
" "
+
logicResult
+
" "
)
return
[
_key
,
_value
].
join
(
" "
+
logicResult
+
" "
)
}
}
},
},
...
@@ -789,7 +789,7 @@ module.exports = (function() {
...
@@ -789,7 +789,7 @@ module.exports = (function() {
dataType
=
dataType
.
replace
(
/NOT NULL/
,
''
)
dataType
=
dataType
.
replace
(
/NOT NULL/
,
''
)
}
}
if
(
dataType
.
lastIndexOf
(
'BLOB'
)
!==
-
1
)
{
if
(
dataType
.
lastIndexOf
(
'BLOB'
)
!==
-
1
||
dataType
.
lastIndexOf
(
'BINARY'
)
!==
-
1
)
{
dataType
=
'bytea'
dataType
=
'bytea'
}
}
...
...
lib/utils.js
View file @
18910ff
...
@@ -180,7 +180,7 @@ var Utils = module.exports = {
...
@@ -180,7 +180,7 @@ var Utils = module.exports = {
_where
[
i
].
in
=
_where
[
i
].
in
||
[]
_where
[
i
].
in
=
_where
[
i
].
in
||
[]
_where
[
i
].
in
.
concat
(
where
[
i
])
_where
[
i
].
in
.
concat
(
where
[
i
])
}
}
else
if
(
type
===
"object"
)
{
else
if
(
Utils
.
isHash
(
where
[
i
])
)
{
Object
.
keys
(
where
[
i
]).
forEach
(
function
(
ii
)
{
Object
.
keys
(
where
[
i
]).
forEach
(
function
(
ii
)
{
logic
=
self
.
getWhereLogic
(
ii
,
where
[
i
][
ii
]);
logic
=
self
.
getWhereLogic
(
ii
,
where
[
i
][
ii
]);
...
@@ -212,7 +212,7 @@ var Utils = module.exports = {
...
@@ -212,7 +212,7 @@ var Utils = module.exports = {
}
}
})
})
}
}
else
if
(
type
===
"string"
||
type
===
"number"
||
type
===
"boolean"
)
{
else
if
(
type
===
"string"
||
type
===
"number"
||
type
===
"boolean"
||
Buffer
.
isBuffer
(
where
[
i
])
)
{
_where
[
i
].
lazy
=
_where
[
i
].
lazy
||
{
conditions
:
[],
bindings
:
[]}
_where
[
i
].
lazy
=
_where
[
i
].
lazy
||
{
conditions
:
[],
bindings
:
[]}
if
(
type
===
"boolean"
)
{
if
(
type
===
"boolean"
)
{
_where
[
i
].
lazy
.
conditions
[
_where
[
i
].
lazy
.
conditions
.
length
]
=
'= '
+
SqlString
.
escape
(
where
[
i
],
false
,
null
,
dialect
)
// sqlite is special
_where
[
i
].
lazy
.
conditions
[
_where
[
i
].
lazy
.
conditions
.
length
]
=
'= '
+
SqlString
.
escape
(
where
[
i
],
false
,
null
,
dialect
)
// sqlite is special
...
@@ -321,7 +321,7 @@ var Utils = module.exports = {
...
@@ -321,7 +321,7 @@ var Utils = module.exports = {
}
}
},
},
isHash
:
function
(
obj
)
{
isHash
:
function
(
obj
)
{
return
Utils
.
_
.
isObject
(
obj
)
&&
!
Array
.
isArray
(
obj
);
return
Utils
.
_
.
isObject
(
obj
)
&&
!
Array
.
isArray
(
obj
)
&&
!
Buffer
.
isBuffer
(
obj
)
;
},
},
hasChanged
:
function
(
attrValue
,
value
)
{
hasChanged
:
function
(
attrValue
,
value
)
{
//If attribute value is Date, check value as a date
//If attribute value is Date, check value as a date
...
@@ -348,7 +348,7 @@ var Utils = module.exports = {
...
@@ -348,7 +348,7 @@ var Utils = module.exports = {
if
([
'number'
,
'string'
].
indexOf
(
typeof
arg
)
!==
-
1
)
{
if
([
'number'
,
'string'
].
indexOf
(
typeof
arg
)
!==
-
1
)
{
result
=
true
result
=
true
}
else
{
}
else
{
result
=
(
arg
instanceof
Date
)
result
=
(
arg
instanceof
Date
)
||
Buffer
.
isBuffer
(
arg
);
}
}
}
}
})
})
...
...
test/dao-factory/findAll.test.js
View file @
18910ff
...
@@ -24,7 +24,8 @@ describe(Support.getTestDialectTeaser("DAOFactory"), function () {
...
@@ -24,7 +24,8 @@ describe(Support.getTestDialectTeaser("DAOFactory"), function () {
data
:
DataTypes
.
STRING
,
data
:
DataTypes
.
STRING
,
intVal
:
DataTypes
.
INTEGER
,
intVal
:
DataTypes
.
INTEGER
,
theDate
:
DataTypes
.
DATE
,
theDate
:
DataTypes
.
DATE
,
aBool
:
DataTypes
.
BOOLEAN
aBool
:
DataTypes
.
BOOLEAN
,
binary
:
DataTypes
.
STRING
(
16
,
true
)
})
})
this
.
User
.
sync
({
force
:
true
}).
success
(
function
()
{
this
.
User
.
sync
({
force
:
true
}).
success
(
function
()
{
...
@@ -63,9 +64,12 @@ describe(Support.getTestDialectTeaser("DAOFactory"), function () {
...
@@ -63,9 +64,12 @@ describe(Support.getTestDialectTeaser("DAOFactory"), function () {
beforeEach
(
function
(
done
)
{
beforeEach
(
function
(
done
)
{
var
self
=
this
var
self
=
this
this
.
buf
=
new
Buffer
(
16
);
this
.
buf
.
fill
(
'\x01'
);
this
.
User
.
bulkCreate
([
this
.
User
.
bulkCreate
([
{
username
:
'boo'
,
intVal
:
5
,
theDate
:
'2013-01-01 12:00'
},
{
username
:
'boo'
,
intVal
:
5
,
theDate
:
'2013-01-01 12:00'
},
{
username
:
'boo2'
,
intVal
:
10
,
theDate
:
'2013-01-10 12:00'
}
{
username
:
'boo2'
,
intVal
:
10
,
theDate
:
'2013-01-10 12:00'
,
binary
:
this
.
buf
}
]).
success
(
function
(
user2
)
{
]).
success
(
function
(
user2
)
{
done
()
done
()
})
})
...
@@ -92,6 +96,19 @@ describe(Support.getTestDialectTeaser("DAOFactory"), function () {
...
@@ -92,6 +96,19 @@ describe(Support.getTestDialectTeaser("DAOFactory"), function () {
});
});
})
})
it
(
'should not break when using smart syntax on binary fields'
,
function
(
done
)
{
this
.
User
.
findAll
({
where
:
{
binary
:
[
this
.
buf
,
this
.
buf
]
}
}).
success
(
function
(
users
){
expect
(
users
).
to
.
have
.
length
(
1
)
expect
(
users
[
0
].
binary
).
to
.
be
.
an
.
instanceof
.
string
expect
(
users
[
0
].
username
).
to
.
equal
(
'boo2'
)
done
();
});
});
it
(
'should be able to find a row using like'
,
function
(
done
)
{
it
(
'should be able to find a row using like'
,
function
(
done
)
{
this
.
User
.
findAll
({
this
.
User
.
findAll
({
where
:
{
where
:
{
...
@@ -220,6 +237,60 @@ describe(Support.getTestDialectTeaser("DAOFactory"), function () {
...
@@ -220,6 +237,60 @@ describe(Support.getTestDialectTeaser("DAOFactory"), function () {
})
})
})
})
it
(
'should be able to handle binary values through associations as well...'
,
function
(
done
)
{
var
User
=
this
.
User
;
var
Binary
=
this
.
sequelize
.
define
(
'Binary'
,
{
id
:
{
type
:
DataTypes
.
STRING
(
16
,
true
),
primaryKey
:
true
}
})
var
buf1
=
this
.
buf
var
buf2
=
new
Buffer
(
16
)
buf2
.
fill
(
'\x02'
)
User
.
belongsTo
(
Binary
,
{
foreignKey
:
'binary'
})
User
.
sync
({
force
:
true
}).
success
(
function
()
{
Binary
.
sync
({
force
:
true
}).
success
(
function
()
{
User
.
bulkCreate
([
{
username
:
'boo5'
,
aBool
:
false
},
{
username
:
'boo6'
,
aBool
:
true
}
]).
success
(
function
()
{
Binary
.
bulkCreate
([
{
id
:
buf1
},
{
id
:
buf2
}
]).
success
(
function
()
{
User
.
find
(
1
).
success
(
function
(
user
)
{
Binary
.
find
(
buf1
).
success
(
function
(
binary
)
{
user
.
setBinary
(
binary
).
success
(
function
()
{
User
.
find
(
2
).
success
(
function
(
_user
)
{
Binary
.
find
(
buf2
).
success
(
function
(
_binary
)
{
_user
.
setBinary
(
_binary
).
success
(
function
()
{
_user
.
getBinary
().
success
(
function
(
_binaryRetrieved
)
{
user
.
getBinary
().
success
(
function
(
binaryRetrieved
)
{
expect
(
binaryRetrieved
.
id
).
to
.
be
.
an
.
instanceof
.
string
expect
(
_binaryRetrieved
.
id
).
to
.
be
.
an
.
instanceof
.
string
expect
(
binaryRetrieved
.
id
).
to
.
have
.
length
(
16
)
expect
(
_binaryRetrieved
.
id
).
to
.
have
.
length
(
16
)
expect
(
binaryRetrieved
.
id
.
toString
()).
to
.
be
.
equal
(
buf1
.
toString
())
expect
(
_binaryRetrieved
.
id
.
toString
()).
to
.
be
.
equal
(
buf2
.
toString
())
done
()
})
})
})
})
})
})
})
})
})
})
})
})
})
it
(
'should be able to return a record with primaryKey being null for new inserts'
,
function
(
done
)
{
it
(
'should be able to return a record with primaryKey being null for new inserts'
,
function
(
done
)
{
var
Session
=
this
.
sequelize
.
define
(
'Session'
,
{
var
Session
=
this
.
sequelize
.
define
(
'Session'
,
{
token
:
{
type
:
DataTypes
.
TEXT
,
allowNull
:
false
},
token
:
{
type
:
DataTypes
.
TEXT
,
allowNull
:
false
},
...
...
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