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 ec532539
authored
Jan 28, 2012
by
Sascha Depold
Browse files
Options
Browse Files
Download
Plain Diff
Merge pull request #125 from megshark/master
Fixed sql logging, added tests
2 parents
070dd44b
9ef6a23c
Hide whitespace changes
Inline
Side-by-side
Showing
10 changed files
with
159 additions
and
6 deletions
lib/associations/has-many-double-linked.js
lib/dialects/mysql/query.js
lib/dialects/query.js
lib/dialects/sqlite/query.js
lib/emitters/custom-event-emitter.js
lib/model-factory.js
lib/query-chainer.js
lib/query-interface.js
spec/associations/has-many.spec.js
spec/model-factory.spec.js
lib/associations/has-many-double-linked.js
View file @
ec53253
...
@@ -21,6 +21,7 @@ module.exports = (function() {
...
@@ -21,6 +21,7 @@ module.exports = (function() {
self
.
__factory
.
target
.
findAllJoin
(
self
.
__factory
.
connectorModel
.
tableName
,
{
where
:
where
})
self
.
__factory
.
target
.
findAllJoin
(
self
.
__factory
.
connectorModel
.
tableName
,
{
where
:
where
})
.
on
(
'success'
,
function
(
objects
)
{
customEventEmitter
.
emit
(
'success'
,
objects
)
})
.
on
(
'success'
,
function
(
objects
)
{
customEventEmitter
.
emit
(
'success'
,
objects
)
})
.
on
(
'failure'
,
function
(
err
){
customEventEmitter
.
emit
(
'failure'
,
err
)
})
.
on
(
'failure'
,
function
(
err
){
customEventEmitter
.
emit
(
'failure'
,
err
)
})
.
on
(
'sql'
,
function
(
sql
)
{
customEventEmitter
.
emit
(
'sql'
,
sql
)})
})
})
return
customEventEmitter
.
run
()
return
customEventEmitter
.
run
()
...
@@ -31,6 +32,7 @@ module.exports = (function() {
...
@@ -31,6 +32,7 @@ module.exports = (function() {
destroyObsoleteAssociations
.
call
(
this
,
oldAssociations
,
newAssociations
)
destroyObsoleteAssociations
.
call
(
this
,
oldAssociations
,
newAssociations
)
.
error
(
function
(
err
)
{
emitter
.
emit
(
'failure'
,
err
)
})
.
error
(
function
(
err
)
{
emitter
.
emit
(
'failure'
,
err
)
})
.
on
(
'sql'
,
function
(
sql
)
{
emitter
.
emit
(
'sql'
,
sql
)
})
.
success
(
function
()
{
.
success
(
function
()
{
var
chainer
=
new
Utils
.
QueryChainer
var
chainer
=
new
Utils
.
QueryChainer
,
association
=
self
.
__factory
.
target
.
associations
[
self
.
__factory
.
associationAccessor
]
,
association
=
self
.
__factory
.
target
.
associations
[
self
.
__factory
.
associationAccessor
]
...
@@ -49,6 +51,7 @@ module.exports = (function() {
...
@@ -49,6 +51,7 @@ module.exports = (function() {
.
run
()
.
run
()
.
success
(
function
()
{
emitter
.
emit
(
'success'
,
newAssociations
)
})
.
success
(
function
()
{
emitter
.
emit
(
'success'
,
newAssociations
)
})
.
error
(
function
(
err
)
{
emitter
.
emit
(
'failure'
,
err
)
})
.
error
(
function
(
err
)
{
emitter
.
emit
(
'failure'
,
err
)
})
.
on
(
'sql'
,
function
(
sql
)
{
emitter
.
emit
(
'sql'
,
sql
)
})
})
})
}
}
...
@@ -82,6 +85,7 @@ module.exports = (function() {
...
@@ -82,6 +85,7 @@ module.exports = (function() {
.
run
()
.
run
()
.
success
(
function
()
{
emitter
.
emit
(
'success'
,
null
)
})
.
success
(
function
()
{
emitter
.
emit
(
'success'
,
null
)
})
.
error
(
function
(
err
)
{
emitter
.
emit
(
'failure'
,
err
)
})
.
error
(
function
(
err
)
{
emitter
.
emit
(
'failure'
,
err
)
})
.
on
(
'sql'
,
function
(
sql
)
{
emitter
.
emit
(
'sql'
,
sql
)
})
}
}
})
})
})
})
...
...
lib/dialects/mysql/query.js
View file @
ec53253
...
@@ -26,8 +26,8 @@ module.exports = (function() {
...
@@ -26,8 +26,8 @@ module.exports = (function() {
if
(
this
.
options
.
logging
)
if
(
this
.
options
.
logging
)
console
.
log
(
'Executing: '
+
this
.
sql
)
console
.
log
(
'Executing: '
+
this
.
sql
)
this
.
client
.
query
(
this
.
sql
,
function
(
err
,
results
,
fields
)
{
this
.
client
.
query
(
this
.
sql
,
function
(
err
,
results
,
fields
)
{
//allow clients to listen to sql to do their own logging or whatnot
self
.
emit
(
'sql'
,
self
.
sql
)
self
.
emit
(
'sql'
,
self
.
sql
)
err
?
onFailure
.
call
(
self
,
err
)
:
onSuccess
.
call
(
self
,
results
,
fields
)
err
?
onFailure
.
call
(
self
,
err
)
:
onSuccess
.
call
(
self
,
results
,
fields
)
}).
setMaxListeners
(
100
)
}).
setMaxListeners
(
100
)
...
@@ -76,6 +76,7 @@ module.exports = (function() {
...
@@ -76,6 +76,7 @@ module.exports = (function() {
unbindClient
.
call
(
this
)
unbindClient
.
call
(
this
)
this
.
emit
(
'success'
,
result
)
this
.
emit
(
'success'
,
result
)
}
}
var
onFailure
=
function
(
err
)
{
var
onFailure
=
function
(
err
)
{
...
...
lib/dialects/query.js
View file @
ec53253
...
@@ -20,5 +20,6 @@ module.exports = (function() {
...
@@ -20,5 +20,6 @@ module.exports = (function() {
return
this
return
this
}
}
return
Query
return
Query
})()
})()
lib/dialects/sqlite/query.js
View file @
ec53253
...
@@ -25,10 +25,9 @@ module.exports = (function() {
...
@@ -25,10 +25,9 @@ module.exports = (function() {
,
isUpdateCommand
=
(
self
.
sql
.
toLowerCase
().
indexOf
(
'update'
)
==
0
)
,
isUpdateCommand
=
(
self
.
sql
.
toLowerCase
().
indexOf
(
'update'
)
==
0
)
,
databaseMethod
=
(
isInsertCommand
||
isUpdateCommand
)
?
'run'
:
'all'
,
databaseMethod
=
(
isInsertCommand
||
isUpdateCommand
)
?
'run'
:
'all'
//allow clients to listen to sql to do their own logging or whatnot
self
.
emit
(
'sql'
,
self
.
sql
)
self
.
database
[
databaseMethod
](
self
.
sql
,
function
(
err
,
results
)
{
self
.
database
[
databaseMethod
](
self
.
sql
,
function
(
err
,
results
)
{
//allow clients to listen to sql to do their own logging or whatnot
self
.
emit
(
'sql'
,
self
.
sql
)
err
?
onFailure
.
call
(
self
,
err
)
:
onSuccess
.
call
(
self
,
results
,
this
)
err
?
onFailure
.
call
(
self
,
err
)
:
onSuccess
.
call
(
self
,
results
,
this
)
})
})
})
})
...
...
lib/emitters/custom-event-emitter.js
View file @
ec53253
...
@@ -33,5 +33,6 @@ module.exports = (function() {
...
@@ -33,5 +33,6 @@ module.exports = (function() {
return
this
return
this
}
}
return
CustomEventEmitter
return
CustomEventEmitter
})()
})()
lib/model-factory.js
View file @
ec53253
...
@@ -68,6 +68,7 @@ module.exports = (function() {
...
@@ -68,6 +68,7 @@ module.exports = (function() {
.
createTable
(
self
.
tableName
,
self
.
attributes
,
options
)
.
createTable
(
self
.
tableName
,
self
.
attributes
,
options
)
.
success
(
function
()
{
emitter
.
emit
(
'success'
,
self
)
})
.
success
(
function
()
{
emitter
.
emit
(
'success'
,
self
)
})
.
error
(
function
(
err
)
{
emitter
.
emit
(
'failure'
,
err
)
})
.
error
(
function
(
err
)
{
emitter
.
emit
(
'failure'
,
err
)
})
.
on
(
'sql'
,
function
(
sql
)
{
emitter
.
emit
(
'sql'
,
sql
)
})
}
}
if
(
options
.
force
)
if
(
options
.
force
)
...
...
lib/query-chainer.js
View file @
ec53253
...
@@ -99,6 +99,7 @@ module.exports = (function() {
...
@@ -99,6 +99,7 @@ module.exports = (function() {
self
.
fails
.
push
(
err
)
self
.
fails
.
push
(
err
)
finish
.
call
(
self
)
finish
.
call
(
self
)
})
})
.
on
(
'sql'
,
function
(
sql
){
self
.
eventEmitter
.
emit
(
'sql'
,
sql
)
})
}
}
var
finish
=
function
()
{
var
finish
=
function
()
{
...
...
lib/query-interface.js
View file @
ec53253
...
@@ -201,9 +201,10 @@ module.exports = (function() {
...
@@ -201,9 +201,10 @@ module.exports = (function() {
return
new
Utils
.
CustomEventEmitter
(
function
(
emitter
)
{
return
new
Utils
.
CustomEventEmitter
(
function
(
emitter
)
{
var
sql
=
self
.
QueryGenerator
.
selectQuery
(
tableName
,
options
)
var
sql
=
self
.
QueryGenerator
.
selectQuery
(
tableName
,
options
)
self
.
sequelize
var
qry
=
self
.
sequelize
.
query
(
sql
,
null
,
{
plain
:
true
,
raw
:
true
})
.
query
(
sql
,
null
,
{
plain
:
true
,
raw
:
true
})
.
success
(
function
(
data
)
{
qry
.
success
(
function
(
data
)
{
self
.
emit
(
'rawSelect'
,
null
)
self
.
emit
(
'rawSelect'
,
null
)
emitter
.
emit
(
'success'
,
data
[
attributeSelector
])
emitter
.
emit
(
'success'
,
data
[
attributeSelector
])
})
})
...
@@ -211,6 +212,9 @@ module.exports = (function() {
...
@@ -211,6 +212,9 @@ module.exports = (function() {
self
.
emit
(
'rawSelect'
,
err
)
self
.
emit
(
'rawSelect'
,
err
)
emitter
.
emit
(
'failure'
,
err
)
emitter
.
emit
(
'failure'
,
err
)
})
})
qry
.
on
(
'sql'
,
function
(
sql
)
{
emitter
.
emit
(
'sql'
,
sql
)
})
}).
run
()
}).
run
()
}
}
...
@@ -244,6 +248,9 @@ module.exports = (function() {
...
@@ -244,6 +248,9 @@ module.exports = (function() {
self
.
emit
(
methodName
,
err
)
self
.
emit
(
methodName
,
err
)
emitter
.
emit
(
'failure'
,
err
)
emitter
.
emit
(
'failure'
,
err
)
})
})
query
.
on
(
'sql'
,
function
(
sql
)
{
emitter
.
emit
(
'sql'
,
sql
)
})
}).
run
()
}).
run
()
}
}
...
...
spec/associations/has-many.spec.js
View file @
ec53253
...
@@ -240,6 +240,45 @@ describe('HasMany', function() {
...
@@ -240,6 +240,45 @@ describe('HasMany', function() {
})
})
})
})
it
(
"allows join table to be specified"
,
function
()
{
Helpers
.
async
(
function
(
done
)
{
var
Child
=
sequelize
.
define
(
'Child'
,
{
name
:
Sequelize
.
STRING
},
{
underscore
:
true
,
freezeTableName
:
true
})
var
Parent
=
sequelize
.
define
(
'Parent'
,
{
name
:
Sequelize
.
STRING
},
{
underscore
:
true
,
freezeTableName
:
true
})
var
ParentJoin
=
sequelize
.
define
(
'ParentRelationship'
,
{
parent_id
:
Sequelize
.
INTEGER
,
child_id
:
Sequelize
.
INTEGER
},
{
underscore
:
true
,
freezeTableName
:
true
})
Parent
.
hasMany
(
Child
,
{
as
:
'Children'
,
foreignKey
:
'child_id'
,
joinTableName
:
'ParentRelationship'
})
Child
.
hasMany
(
Parent
,
{
as
:
'Parents'
,
foreignKey
:
'parent_id'
,
joinTableName
:
'ParentRelationship'
})
var
parents
=
[]
ParentJoin
.
sync
({
force
:
true
}).
success
(
function
()
{
Parent
.
sync
({
force
:
true
}).
success
(
function
()
{
Child
.
sync
({
force
:
true
}).
success
(
function
()
{
Parent
.
create
({
name
:
'mom'
}).
success
(
function
(
mom
)
{
parents
.
push
(
mom
)
Parent
.
create
({
name
:
'dad'
}).
success
(
function
(
dad
)
{
parents
.
push
(
dad
)
Child
.
create
({
name
:
'baby'
}).
success
(
function
(
baby
)
{
baby
.
setParents
(
parents
).
success
(
function
(){
parents
[
0
].
getChildren
().
success
(
function
(
children
){
expect
(
children
).
not
.
toBe
(
null
)
expect
(
children
.
length
).
toBeDefined
()
expect
(
children
.
length
).
toEqual
(
1
)
expect
(
children
[
0
]).
toBeDefined
()
expect
(
children
[
0
].
name
).
toEqual
(
'baby'
)
done
()
})
})
})
})
})
})
})
})
})
})
it
(
"gets and sets the connector models"
,
function
()
{
it
(
"gets and sets the connector models"
,
function
()
{
Helpers
.
async
(
function
(
done
)
{
Helpers
.
async
(
function
(
done
)
{
var
Person
=
sequelize
.
define
(
'Person'
,
{
name
:
Sequelize
.
STRING
})
var
Person
=
sequelize
.
define
(
'Person'
,
{
name
:
Sequelize
.
STRING
})
...
...
spec/model-factory.spec.js
View file @
ec53253
...
@@ -183,6 +183,19 @@ describe('ModelFactory', function() {
...
@@ -183,6 +183,19 @@ describe('ModelFactory', function() {
})
})
})
})
})
})
it
(
'allows sql logging'
,
function
()
{
Helpers
.
async
(
function
(
done
)
{
User
.
create
({
name
:
'Fluffy Bunny'
,
smth
:
'else'
})
.
on
(
'sql'
,
function
(
sql
)
{
expect
(
sql
).
toBeDefined
()
expect
(
sql
.
toUpperCase
().
indexOf
(
"INSERT"
)).
toBeGreaterThan
(
-
1
)
done
()
})
})
})
})
})
describe
(
'destroy'
,
function
()
{
describe
(
'destroy'
,
function
()
{
...
@@ -210,6 +223,29 @@ describe('ModelFactory', function() {
...
@@ -210,6 +223,29 @@ describe('ModelFactory', function() {
})
})
})
})
it
(
'allows sql logging of delete statements'
,
function
()
{
Helpers
.
async
(
function
(
done
)
{
User
=
sequelize
.
define
(
'User'
,
{
name
:
Sequelize
.
STRING
,
bio
:
Sequelize
.
TEXT
})
User
.
sync
({
force
:
true
}).
success
(
done
)
})
Helpers
.
async
(
function
(
done
)
{
User
.
create
({
name
:
'hallo'
,
bio
:
'welt'
}).
success
(
function
(
u
)
{
User
.
all
().
success
(
function
(
users
)
{
expect
(
users
.
length
).
toEqual
(
1
)
u
.
destroy
().
on
(
'sql'
,
function
(
sql
)
{
expect
(
sql
).
toBeDefined
()
expect
(
sql
.
toUpperCase
().
indexOf
(
"DELETE"
)).
toBeGreaterThan
(
-
1
)
done
()
}).
error
(
function
(
err
)
{
console
.
log
(
err
)
})
}).
error
(
function
(
err
)
{
console
.
log
(
err
)
})
})
})
})
it
(
'marks the database entry as deleted if model is paranoid'
,
function
()
{
it
(
'marks the database entry as deleted if model is paranoid'
,
function
()
{
Helpers
.
async
(
function
(
done
)
{
Helpers
.
async
(
function
(
done
)
{
User
=
sequelize
.
define
(
'User'
,
{
User
=
sequelize
.
define
(
'User'
,
{
...
@@ -228,6 +264,27 @@ describe('ModelFactory', function() {
...
@@ -228,6 +264,27 @@ describe('ModelFactory', function() {
})
})
})
})
})
})
it
(
'allows sql logging of update statements'
,
function
()
{
Helpers
.
async
(
function
(
done
)
{
User
=
sequelize
.
define
(
'User'
,
{
name
:
Sequelize
.
STRING
,
bio
:
Sequelize
.
TEXT
},
{
paranoid
:
true
})
User
.
sync
({
force
:
true
}).
success
(
done
)
})
Helpers
.
async
(
function
(
done
)
{
User
.
create
({
name
:
'meg'
,
bio
:
'none'
}).
success
(
function
(
u
)
{
expect
(
u
).
toBeDefined
()
expect
(
u
).
not
.
toBe
(
null
)
u
.
destroy
().
on
(
'sql'
,
function
(
sql
)
{
expect
(
sql
).
toBeDefined
()
expect
(
sql
.
toUpperCase
().
indexOf
(
"UPDATE"
)).
toBeGreaterThan
(
-
1
)
done
()
})
})
})
})
})
})
describe
(
'find'
,
function
()
{
describe
(
'find'
,
function
()
{
...
@@ -276,6 +333,17 @@ describe('ModelFactory', function() {
...
@@ -276,6 +333,17 @@ describe('ModelFactory', function() {
})
})
})
})
it
(
'allows sql logging'
,
function
()
{
Helpers
.
async
(
function
(
done
)
{
User
.
find
({
where
:
{
name
:
'foo'
}
})
.
on
(
'sql'
,
function
(
sql
)
{
expect
(
sql
).
toBeDefined
()
expect
(
sql
.
toUpperCase
().
indexOf
(
"SELECT"
)).
toBeGreaterThan
(
-
1
)
done
()
})
})
})
it
(
'ignores passed limit option'
,
function
()
{
it
(
'ignores passed limit option'
,
function
()
{
Helpers
.
async
(
function
(
done
)
{
Helpers
.
async
(
function
(
done
)
{
User
.
find
({
limit
:
10
}).
success
(
function
(
user
)
{
User
.
find
({
limit
:
10
}).
success
(
function
(
user
)
{
...
@@ -433,6 +501,17 @@ describe('ModelFactory', function() {
...
@@ -433,6 +501,17 @@ describe('ModelFactory', function() {
})
})
})
})
it
(
'allows sql logging'
,
function
()
{
Helpers
.
async
(
function
(
done
)
{
User
.
count
()
.
on
(
'sql'
,
function
(
sql
)
{
expect
(
sql
).
toBeDefined
()
expect
(
sql
.
toUpperCase
().
indexOf
(
"SELECT"
)).
toBeGreaterThan
(
-
1
)
done
()
})
})
})
it
(
'filters object'
,
function
()
{
it
(
'filters object'
,
function
()
{
Helpers
.
async
(
function
(
done
)
{
Helpers
.
async
(
function
(
done
)
{
User
.
create
({
name
:
'user1'
}).
success
(
function
()
{
User
.
create
({
name
:
'user1'
}).
success
(
function
()
{
...
@@ -459,6 +538,16 @@ describe('ModelFactory', function() {
...
@@ -459,6 +538,16 @@ describe('ModelFactory', function() {
})
})
})
})
})
})
it
(
'allows sql logging'
,
function
()
{
Helpers
.
async
(
function
(
done
)
{
User
.
min
(
'age'
)
.
on
(
'sql'
,
function
(
sql
)
{
expect
(
sql
).
toBeDefined
()
expect
(
sql
.
toUpperCase
().
indexOf
(
"SELECT"
)).
toBeGreaterThan
(
-
1
)
done
()
})
})
})
})
})
describe
(
'max'
,
function
()
{
describe
(
'max'
,
function
()
{
...
@@ -471,6 +560,16 @@ describe('ModelFactory', function() {
...
@@ -471,6 +560,16 @@ describe('ModelFactory', function() {
})
})
})
})
})
})
it
(
'allows sql logging'
,
function
()
{
Helpers
.
async
(
function
(
done
)
{
User
.
max
(
'age'
)
.
on
(
'sql'
,
function
(
sql
)
{
expect
(
sql
).
toBeDefined
()
expect
(
sql
.
toUpperCase
().
indexOf
(
"SELECT"
)).
toBeGreaterThan
(
-
1
)
done
()
})
})
})
})
})
describe
(
'equals'
,
function
()
{
describe
(
'equals'
,
function
()
{
...
...
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