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 5e0118ef
authored
Jun 25, 2013
by
Daniel Durante
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Added basic where logic to sequelize
1 parent
3c14f562
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
305 additions
and
10 deletions
lib/dialects/mysql/query-generator.js
lib/dialects/postgres/query-generator.js
spec/dao-factory.spec.js
lib/dialects/mysql/query-generator.js
View file @
5e0118e
...
...
@@ -431,11 +431,24 @@ module.exports = (function() {
result
.
push
([
_key
,
_value
].
join
(
" IN "
))
}
else
if
((
value
)
&&
(
typeof
value
==
'object'
)
&&
!
(
value
instanceof
Date
))
{
// is value an object?
//using as sentinel for join column => value
_value
=
this
.
quoteIdentifiers
(
value
.
join
)
result
.
push
([
_key
,
_value
].
join
(
"="
))
if
(
!!
value
.
join
)
{
//using as sentinel for join column => value
_value
=
this
.
quoteIdentifiers
(
value
.
join
)
result
.
push
([
_key
,
_value
].
join
(
"="
))
}
else
{
for
(
var
logic
in
value
)
{
var
logicResult
=
this
.
getWhereLogic
(
logic
)
if
(
logicResult
===
"BETWEEN"
||
logicResult
===
"NOT BETWEEN"
)
{
_value
=
this
.
escape
(
value
[
logic
][
0
])
var
_value2
=
this
.
escape
(
value
[
logic
][
1
])
result
.
push
(
' ('
+
_key
+
' '
+
logicResult
+
' '
+
_value
+
' AND '
+
_value2
+
') '
)
}
else
{
_value
=
this
.
escape
(
value
[
logic
])
result
.
push
([
_key
,
_value
].
join
(
' '
+
logicResult
+
' '
))
}
}
}
}
else
{
_value
=
this
.
escape
(
value
)
result
.
push
((
_value
==
'NULL'
)
?
_key
+
" IS NULL"
:
[
_key
,
_value
].
join
(
"="
))
...
...
@@ -445,6 +458,33 @@ module.exports = (function() {
return
result
.
join
(
" AND "
)
},
getWhereLogic
:
function
(
logic
)
{
switch
(
logic
)
{
case
'gte'
:
return
'>='
break
case
'gt'
:
return
'>'
break
case
'lte'
:
return
'<='
break
case
'lt'
:
return
'<'
break
case
'ne'
:
return
'!='
break
case
'between'
:
return
'BETWEEN'
break
case
'nbetween'
:
case
'notbetween'
:
return
'NOT BETWEEN'
break
}
},
attributesToSQL
:
function
(
attributes
)
{
var
result
=
{}
...
...
lib/dialects/postgres/query-generator.js
View file @
5e0118e
...
...
@@ -522,9 +522,24 @@ module.exports = (function() {
result
.
push
([
_key
,
_value
].
join
(
" IN "
))
}
else
if
((
value
)
&&
(
typeof
value
===
"object"
))
{
//using as sentinel for join column => value
_value
=
this
.
quoteIdentifiers
(
value
.
join
)
result
.
push
([
_key
,
_value
].
join
(
"="
))
if
(
!!
value
.
join
)
{
//using as sentinel for join column => value
_value
=
this
.
quoteIdentifiers
(
value
.
join
)
result
.
push
([
_key
,
_value
].
join
(
"="
))
}
else
{
for
(
var
logic
in
value
)
{
var
logicResult
=
this
.
getWhereLogic
(
logic
)
if
(
logicResult
===
"BETWEEN"
||
logicResult
===
"NOT BETWEEN"
)
{
_value
=
this
.
escape
(
value
[
logic
][
0
])
var
_value2
=
this
.
escape
(
value
[
logic
][
1
])
result
.
push
(
' ('
+
_key
+
' '
+
logicResult
+
' '
+
_value
+
' AND '
+
_value2
+
') '
)
}
else
{
_value
=
this
.
escape
(
value
[
logic
])
result
.
push
([
_key
,
_value
].
join
(
' '
+
logicResult
+
' '
))
}
}
}
}
else
{
_value
=
this
.
escape
(
value
)
result
.
push
((
_value
==
'NULL'
)
?
_key
+
" IS NULL"
:
[
_key
,
_value
].
join
(
"="
))
...
...
@@ -534,6 +549,33 @@ module.exports = (function() {
return
result
.
join
(
' AND '
)
},
getWhereLogic
:
function
(
logic
)
{
switch
(
logic
)
{
case
'gte'
:
return
'>='
break
case
'gt'
:
return
'>'
break
case
'lte'
:
return
'<='
break
case
'lt'
:
return
'<'
break
case
'ne'
:
return
'!='
break
case
'between'
:
return
'BETWEEN'
break
case
'nbetween'
:
case
'notbetween'
:
return
'NOT BETWEEN'
break
}
},
attributesToSQL
:
function
(
attributes
)
{
var
result
=
{}
...
...
spec/dao-factory.spec.js
View file @
5e0118e
...
...
@@ -18,7 +18,9 @@ describe(Helpers.getTestDialectTeaser("DAOFactory"), function() {
this
.
User
=
sequelize
.
define
(
'User'
,
{
username
:
DataTypes
.
STRING
,
secretValue
:
DataTypes
.
STRING
,
data
:
DataTypes
.
STRING
data
:
DataTypes
.
STRING
,
intVal
:
DataTypes
.
INTEGER
,
theDate
:
DataTypes
.
DATE
})
}.
bind
(
this
),
onComplete
:
function
()
{
...
...
@@ -807,7 +809,8 @@ describe(Helpers.getTestDialectTeaser("DAOFactory"), function() {
,
User
=
this
.
sequelize
.
define
(
'ParanoidUser'
,
{
username
:
Sequelize
.
STRING
,
secretValue
:
Sequelize
.
STRING
,
data
:
Sequelize
.
STRING
data
:
Sequelize
.
STRING
,
intVal
:
{
type
:
Sequelize
.
INTEGER
,
defaultValue
:
1
}
},
{
paranoid
:
true
})
...
...
@@ -842,6 +845,216 @@ describe(Helpers.getTestDialectTeaser("DAOFactory"), function() {
})
// - destroy
describe
(
'special where conditions'
,
function
()
{
before
(
function
(
done
)
{
var
self
=
this
this
.
User
.
create
({
username
:
'boo'
,
intVal
:
5
,
theDate
:
'2013-01-01 12:00'
}).
success
(
function
(
user
){
self
.
user
=
user
self
.
User
.
create
({
username
:
'boo2'
,
intVal
:
10
,
theDate
:
'2013-01-10 12:00'
}).
success
(
function
(
user2
){
self
.
user2
=
user2
done
()
})
})
})
it
(
'should be able to find a row between a certain date'
,
function
(
done
)
{
this
.
User
.
findAll
({
where
:
{
theDate
:
{
between
:
[
'2013-01-02'
,
'2013-01-11'
]
}
}
}).
success
(
function
(
users
)
{
expect
(
users
[
0
].
username
).
toEqual
(
'boo2'
)
expect
(
users
[
0
].
intVal
).
toEqual
(
10
)
done
()
})
})
it
(
'should be able to find a row between a certain date and an additional where clause'
,
function
(
done
)
{
this
.
User
.
findAll
({
where
:
{
theDate
:
{
between
:
[
'2013-01-02'
,
'2013-01-11'
]
},
intVal
:
10
}
}).
success
(
function
(
users
)
{
expect
(
users
[
0
].
username
).
toEqual
(
'boo2'
)
expect
(
users
[
0
].
intVal
).
toEqual
(
10
)
done
()
})
})
it
(
'should be able to find a row not between a certain integer'
,
function
(
done
)
{
this
.
User
.
findAll
({
where
:
{
intVal
:
{
nbetween
:
[
8
,
10
]
}
}
}).
success
(
function
(
users
)
{
expect
(
users
[
0
].
username
).
toEqual
(
'boo'
)
expect
(
users
[
0
].
intVal
).
toEqual
(
5
)
done
()
})
})
it
(
'should be able to find a row using not between and between logic'
,
function
(
done
)
{
this
.
User
.
findAll
({
where
:
{
theDate
:
{
between
:
[
'2012-12-10'
,
'2013-01-02'
],
nbetween
:
[
'2013-01-04'
,
'2013-01-20'
]
}
}
}).
success
(
function
(
users
)
{
expect
(
users
[
0
].
username
).
toEqual
(
'boo'
)
expect
(
users
[
0
].
intVal
).
toEqual
(
5
)
done
()
})
})
it
(
'should be able to find a row using not between and between logic with dates'
,
function
(
done
)
{
this
.
User
.
findAll
({
where
:
{
theDate
:
{
between
:
[
new
Date
(
'2012-12-10'
),
new
Date
(
'2013-01-02'
)],
nbetween
:
[
new
Date
(
'2013-01-04'
),
new
Date
(
'2013-01-20'
)]
}
}
}).
success
(
function
(
users
)
{
expect
(
users
[
0
].
username
).
toEqual
(
'boo'
)
expect
(
users
[
0
].
intVal
).
toEqual
(
5
)
done
()
})
})
it
(
'should be able to find a row using greater than or equal to logic with dates'
,
function
(
done
)
{
this
.
User
.
findAll
({
where
:
{
theDate
:
{
gte
:
new
Date
(
'2013-01-09'
)
}
}
}).
success
(
function
(
users
)
{
expect
(
users
[
0
].
username
).
toEqual
(
'boo2'
)
expect
(
users
[
0
].
intVal
).
toEqual
(
10
)
done
()
})
})
it
(
'should be able to find a row using greater than or equal to'
,
function
(
done
)
{
this
.
User
.
find
({
where
:
{
intVal
:
{
gte
:
6
}
}
}).
success
(
function
(
user
)
{
expect
(
user
.
username
).
toEqual
(
'boo2'
)
expect
(
user
.
intVal
).
toEqual
(
10
)
done
()
})
})
it
(
'should be able to find a row using greater than'
,
function
(
done
)
{
this
.
User
.
find
({
where
:
{
intVal
:
{
gt
:
5
}
}
}).
success
(
function
(
user
)
{
expect
(
user
.
username
).
toEqual
(
'boo2'
)
expect
(
user
.
intVal
).
toEqual
(
10
)
done
()
})
})
it
(
'should be able to find a row using lesser than or equal to'
,
function
(
done
)
{
this
.
User
.
find
({
where
:
{
intVal
:
{
lte
:
5
}
}
}).
success
(
function
(
user
)
{
expect
(
user
.
username
).
toEqual
(
'boo'
)
expect
(
user
.
intVal
).
toEqual
(
5
)
done
()
})
})
it
(
'should be able to find a row using lesser than'
,
function
(
done
)
{
this
.
User
.
find
({
where
:
{
intVal
:
{
lt
:
6
}
}
}).
success
(
function
(
user
)
{
expect
(
user
.
username
).
toEqual
(
'boo'
)
expect
(
user
.
intVal
).
toEqual
(
5
)
done
()
})
})
it
(
'should have no problem finding a row using lesser and greater than'
,
function
(
done
)
{
this
.
User
.
findAll
({
where
:
{
intVal
:
{
lt
:
6
,
gt
:
4
}
}
}).
success
(
function
(
users
)
{
expect
(
users
[
0
].
username
).
toEqual
(
'boo'
)
expect
(
users
[
0
].
intVal
).
toEqual
(
5
)
done
()
})
})
it
(
'should be able to find a row using not equal to logic'
,
function
(
done
)
{
this
.
User
.
find
({
where
:
{
intVal
:
{
ne
:
10
}
}
}).
success
(
function
(
user
)
{
expect
(
user
.
username
).
toEqual
(
'boo'
)
expect
(
user
.
intVal
).
toEqual
(
5
)
done
()
})
})
it
(
'should be able to find multiple users with any of the special where logic properties'
,
function
(
done
)
{
this
.
User
.
findAll
({
where
:
{
intVal
:
{
lte
:
10
}
}
}).
success
(
function
(
users
)
{
expect
(
users
[
0
].
username
).
toEqual
(
'boo'
)
expect
(
users
[
0
].
intVal
).
toEqual
(
5
)
expect
(
users
[
1
].
username
).
toEqual
(
'boo2'
)
expect
(
users
[
1
].
intVal
).
toEqual
(
10
)
done
()
})
})
})
describe
(
'find'
,
function
find
()
{
before
(
function
(
done
)
{
this
.
User
.
create
({
...
...
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