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 8b44643b
authored
Mar 08, 2014
by
Mick Hansen
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
yay, some logic works
1 parent
872f66ef
Hide whitespace changes
Inline
Side-by-side
Showing
7 changed files
with
64 additions
and
29 deletions
lib/associations/has-many-single-linked.js
lib/associations/has-many.js
lib/dao-factory.js
lib/dialects/abstract/query-generator.js
lib/sequelize.js
lib/utils.js
test/associations/has-many.test.js
lib/associations/has-many-single-linked.js
View file @
8b44643
...
...
@@ -2,32 +2,26 @@ var Utils = require('./../utils')
,
Transaction
=
require
(
'./../transaction'
)
module
.
exports
=
(
function
()
{
var
HasManySingleLinked
=
function
(
definition
,
instance
)
{
this
.
__factory
=
definition
var
HasManySingleLinked
=
function
(
association
,
instance
)
{
this
.
__factory
=
association
this
.
association
=
association
this
.
instance
=
instance
this
.
target
=
this
.
association
.
target
this
.
source
=
this
.
association
.
source
}
HasManySingleLinked
.
prototype
.
injectGetter
=
function
(
options
)
{
var
self
=
this
,
where
=
{}
,
smart
options
=
options
||
{}
var
primaryKey
=
Object
.
keys
(
this
.
instance
.
rawAttributes
).
filter
(
function
(
k
)
{
return
self
.
instance
.
rawAttributes
[
k
].
primaryKey
===
true
})
primaryKey
=
primaryKey
.
length
===
1
?
primaryKey
[
0
]
:
'id'
where
[
this
.
__factory
.
identifier
]
=
this
.
instance
[
primaryKey
]
if
(
options
.
where
)
{
smart
=
Utils
.
smartWhere
([
where
,
options
.
where
],
this
.
__factory
.
target
.
daoFactoryManager
.
sequelize
.
options
.
dialect
)
smart
=
Utils
.
compileSmartWhere
.
call
(
this
.
__factory
.
target
,
smart
,
this
.
__factory
.
target
.
daoFactoryManager
.
sequelize
.
options
.
dialect
)
if
(
smart
.
length
>
0
)
{
options
.
where
=
smart
}
}
else
{
options
.
where
=
where
}
options
.
where
=
new
Utils
.
and
([
new
Utils
.
where
(
this
.
target
.
rawAttributes
[
this
.
association
.
identifier
],
this
.
instance
[
this
.
source
.
primaryKeyAttribute
])
,
options
.
where
])
return
this
.
__factory
.
target
.
all
(
options
)
return
this
.
association
.
target
.
all
(
options
)
}
HasManySingleLinked
.
prototype
.
injectSetter
=
function
(
emitter
,
oldAssociations
,
newAssociations
,
defaultAttributes
)
{
...
...
lib/associations/has-many.js
View file @
8b44643
...
...
@@ -294,23 +294,20 @@ module.exports = (function() {
}).
run
()
}
obj
[
this
.
accessors
.
add
]
=
function
(
new
AssociatedObject
,
additionalAttributes
)
{
obj
[
this
.
accessors
.
add
]
=
function
(
new
Instance
,
additionalAttributes
)
{
var
instance
=
this
,
primaryKeys
=
Object
.
keys
(
newAssociatedObject
.
Model
.
primaryKeys
||
{})
,
primaryKey
=
primaryKeys
.
length
===
1
?
primaryKeys
[
0
]
:
'id'
,
where
=
{}
,
primaryKey
=
newInstance
.
Model
.
primaryKeyAttribute
,
where
=
new
Utils
.
where
(
self
.
target
.
rawAttributes
[
primaryKey
],
newInstance
[
primaryKey
])
where
[
newAssociatedObject
.
Model
.
getTableName
()
+
'.'
+
primaryKey
]
=
newAssociatedObject
[
primaryKey
]
return
new
Utils
.
CustomEventEmitter
(
function
(
emitter
)
{
instance
[
self
.
accessors
.
get
]({
where
:
where
})
.
proxy
(
emitter
,
{
events
:
[
'error'
,
'sql'
]})
.
success
(
function
(
currentAssociatedObjects
)
{
if
(
currentAssociatedObjects
.
length
===
0
||
Object
(
self
.
through
)
===
self
.
through
)
{
var
Class
=
Object
(
self
.
through
)
===
self
.
through
?
HasManyDoubleLinked
:
HasManySingleLinked
new
Class
(
self
,
instance
).
injectAdder
(
emitter
,
new
AssociatedObject
,
additionalAttributes
,
!!
currentAssociatedObjects
.
length
)
new
Class
(
self
,
instance
).
injectAdder
(
emitter
,
new
Instance
,
additionalAttributes
,
!!
currentAssociatedObjects
.
length
)
}
else
{
emitter
.
emit
(
'success'
,
new
AssociatedObject
);
emitter
.
emit
(
'success'
,
new
Instance
);
}
})
}).
run
()
...
...
lib/dao-factory.js
View file @
8b44643
...
...
@@ -259,6 +259,9 @@ module.exports = (function() {
})
Utils
.
_
.
each
(
self
.
rawAttributes
,
function
(
options
,
attribute
)
{
options
.
Model
=
self
options
.
fieldName
=
attribute
if
(
options
.
hasOwnProperty
(
type
))
{
_custom
[
attribute
]
=
options
[
type
]
}
...
...
lib/dialects/abstract/query-generator.js
View file @
8b44643
...
...
@@ -799,6 +799,7 @@ module.exports = (function() {
// Add WHERE to sub or main query
if
(
options
.
hasOwnProperty
(
'where'
))
{
options
.
where
=
this
.
getWhereConditions
(
options
.
where
,
tableName
,
Model
,
options
)
if
(
subQuery
)
{
subQueryItems
.
push
(
" WHERE "
+
options
.
where
)
...
...
@@ -954,11 +955,35 @@ module.exports = (function() {
if
((
smth
instanceof
Utils
.
and
)
||
(
smth
instanceof
Utils
.
or
))
{
var
connector
=
(
smth
instanceof
Utils
.
and
)
?
' AND '
:
' OR '
result
=
smth
.
args
.
map
(
function
(
arg
)
{
result
=
smth
.
args
.
filter
(
function
(
arg
)
{
return
arg
!==
undefined
}).
map
(
function
(
arg
)
{
return
self
.
getWhereConditions
(
arg
,
tableName
,
factory
,
options
,
prepend
)
}).
join
(
connector
)
result
=
"("
+
result
+
")"
}
else
if
(
smth
instanceof
Utils
.
where
)
{
var
value
=
smth
.
logic
,
key
=
this
.
quoteTable
(
smth
.
attribute
.
Model
.
getTableName
())
+
'.'
+
this
.
quoteIdentifier
(
smth
.
attribute
.
fieldName
)
,
logic
,
_result
=
[]
,
_value
if
(
typeof
value
===
'object'
)
{
for
(
logic
in
value
)
{
_result
.
push
([
key
,
this
.
escape
(
value
[
logic
])].
join
(
' '
+
Utils
.
getWhereLogic
(
logic
,
value
[
logic
])
+
' '
))
}
result
=
_result
.
join
(
" AND "
)
}
else
{
if
(
typeof
value
===
'boolean'
)
{
value
=
this
.
booleanValue
(
value
);
}
else
{
value
=
this
.
escape
(
value
)
}
result
=
(
value
==
'NULL'
)
?
key
+
" IS NULL"
:
[
key
,
value
].
join
(
"="
)
}
}
else
if
(
Utils
.
isHash
(
smth
))
{
if
(
prepend
)
{
options
.
keysEscaped
=
true
...
...
lib/sequelize.js
View file @
8b44643
...
...
@@ -467,6 +467,14 @@ module.exports = (function() {
return
new
Utils
.
or
(
Array
.
prototype
.
slice
.
call
(
arguments
))
}
Sequelize
.
where
=
Sequelize
.
prototype
.
where
=
function
()
{
return
new
Utils
.
where
(
Array
.
prototype
.
slice
.
call
(
arguments
))
}
Sequelize
.
condition
=
Sequelize
.
prototype
.
condition
=
function
()
{
return
new
Utils
.
condition
(
Array
.
prototype
.
slice
.
call
(
arguments
))
}
Sequelize
.
prototype
.
transaction
=
function
(
_options
,
_callback
)
{
var
options
=
(
typeof
_options
===
'function'
)
?
{}
:
_options
,
callback
=
(
typeof
_options
===
'function'
)
?
_options
:
_callback
...
...
lib/utils.js
View file @
8b44643
...
...
@@ -541,6 +541,11 @@ var Utils = module.exports = {
this
.
args
=
args
},
where
:
function
(
attribute
,
logic
)
{
this
.
attribute
=
attribute
this
.
logic
=
logic
},
generateUUID
:
function
()
{
return
'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'
.
replace
(
/
[
xy
]
/g
,
function
(
c
)
{
var
r
=
Math
.
random
()
*
16
|
0
,
v
=
c
==
'x'
?
r
:
(
r
&
0x3
|
0x8
)
...
...
@@ -553,6 +558,8 @@ var Utils = module.exports = {
}
}
Utils
.
condition
=
Utils
.
where
// I know this may seem silly, but this gives us the ability to recognize whether
// or not we should be escaping or if we should trust the user. Basically, it
// keeps things in perspective and organized.
...
...
test/associations/has-many.test.js
View file @
8b44643
...
...
@@ -104,7 +104,8 @@ describe(Support.getTestDialectTeaser("HasMany"), function() {
])
chainer
.
runSerially
()
.
success
(
function
(
_
,
label1
,
hasLabel1
,
hasLabel2
)
{
.
done
(
function
(
err
,
_
,
label1
,
hasLabel1
,
hasLabel2
)
{
expect
(
err
).
not
.
to
.
be
.
ok
expect
(
hasLabel1
).
to
.
be
.
true
expect
(
hasLabel2
).
to
.
be
.
false
done
()
...
...
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