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 59605e88
authored
Nov 10, 2011
by
Sascha Depold
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
use new private method style
1 parent
77c55541
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
119 additions
and
110 deletions
lib/query.js
lib/sequelize.js
lib/query.js
View file @
59605e8
var
Utils
=
require
(
"./utils"
)
var
Utils
=
require
(
"./utils"
)
var
Query
=
module
.
exports
=
function
(
client
,
callee
,
options
)
{
var
self
=
this
this
.
client
=
client
module
.
exports
=
(
function
()
{
this
.
callee
=
callee
var
Query
=
function
(
client
,
callee
,
options
)
{
this
.
options
=
options
||
{}
var
self
=
this
this
.
bindClientFunction
=
function
(
err
)
{
self
.
onFailure
(
err
)
}
}
Utils
.
addEventEmitter
(
Query
)
Query
.
prototype
.
run
=
function
(
query
)
{
this
.
client
=
client
var
self
=
this
this
.
callee
=
callee
this
.
sql
=
query
this
.
options
=
options
||
{}
this
.
bindClientFunction
=
function
(
err
)
{
onFailure
.
call
(
self
,
err
)
}
}
Utils
.
addEventEmitter
(
Query
)
this
.
bindClient
()
Query
.
prototype
.
run
=
function
(
query
)
{
var
self
=
this
this
.
sql
=
query
if
(
this
.
options
.
logging
)
bindClient
.
call
(
this
)
console
.
log
(
'Executing: '
+
this
.
sql
)
this
.
client
.
query
(
this
.
sql
,
function
(
err
,
results
,
fields
)
{
if
(
this
.
options
.
logging
)
err
?
self
.
onFailure
(
err
)
:
self
.
onSuccess
(
self
.
sql
,
results
,
fields
)
console
.
log
(
'Executing: '
+
this
.
sql
)
}).
setMaxListeners
(
100
)
return
this
this
.
client
.
query
(
this
.
sql
,
function
(
err
,
results
,
fields
)
{
}
err
?
onFailure
.
call
(
self
,
err
)
:
onSuccess
.
call
(
self
,
self
.
sql
,
results
,
fields
)
}).
setMaxListeners
(
100
)
Query
.
prototype
.
bindClient
=
function
()
{
return
this
this
.
client
.
on
(
'error'
,
this
.
bindClientFunction
)
}
}
Query
.
prototype
.
un
bindClient
=
function
()
{
var
bindClient
=
function
()
{
this
.
client
.
removeListener
(
'error'
,
this
.
bindClientFunction
)
this
.
client
.
on
(
'error'
,
this
.
bindClientFunction
)
}
}
Query
.
prototype
.
onSuccess
=
function
(
query
,
results
,
fields
)
{
var
unbindClient
=
function
(
)
{
var
result
=
this
.
callee
this
.
client
.
removeListener
(
'error'
,
this
.
bindClientFunction
)
,
self
=
this
}
// add the inserted row id to the instance
var
onSuccess
=
function
(
query
,
results
,
fields
)
{
if
(
this
.
callee
&&
(
query
.
indexOf
(
'INSERT INTO'
)
==
0
)
&&
(
results
.
hasOwnProperty
(
'insertId'
)))
var
result
=
this
.
callee
this
.
callee
[
this
.
callee
.
__definition
.
autoIncrementField
]
=
results
.
insertId
,
self
=
this
// transform results into real model instances
// add the inserted row id to the instance
// return the first real model instance if options.plain is set (e.g. Model.find)
if
(
this
.
callee
&&
(
query
.
indexOf
(
'INSERT INTO'
)
==
0
)
&&
(
results
.
hasOwnProperty
(
'insertId'
)))
if
(
query
.
indexOf
(
'SELECT'
)
==
0
)
{
this
.
callee
[
this
.
callee
.
__definition
.
autoIncrementField
]
=
results
.
insertId
result
=
results
.
map
(
function
(
result
)
{
return
self
.
callee
.
build
(
result
,
{
isNewRecord
:
false
})
})
if
(
this
.
options
.
plain
)
// transform results into real model instances
result
=
(
result
.
length
==
0
)
?
null
:
result
[
0
]
// return the first real model instance if options.plain is set (e.g. Model.find)
if
(
query
.
indexOf
(
'SELECT'
)
==
0
)
{
result
=
results
.
map
(
function
(
result
)
{
return
self
.
callee
.
build
(
result
,
{
isNewRecord
:
false
})
})
if
(
this
.
options
.
plain
)
result
=
(
result
.
length
==
0
)
?
null
:
result
[
0
]
}
unbindClient
.
call
(
this
)
this
.
emit
(
'success'
,
result
)
}
}
this
.
unbindClient
()
var
onFailure
=
function
(
err
)
{
this
.
emit
(
'success'
,
result
)
unbindClient
.
call
(
this
)
}
this
.
emit
(
'failure'
,
err
,
this
.
callee
)
}
Query
.
prototype
.
onFailure
=
function
(
err
)
{
return
Query
this
.
unbindClient
()
})()
this
.
emit
(
'failure'
,
err
,
this
.
callee
)
}
lib/sequelize.js
View file @
59605e8
...
@@ -3,90 +3,94 @@ var Utils = require("./utils")
...
@@ -3,90 +3,94 @@ var Utils = require("./utils")
,
DataTypes
=
require
(
'./data-types'
)
,
DataTypes
=
require
(
'./data-types'
)
,
ModelManager
=
require
(
"./model-manager"
)
,
ModelManager
=
require
(
"./model-manager"
)
var
Sequelize
=
module
.
exports
=
function
(
database
,
username
,
password
,
options
)
{
module
.
exports
=
(
function
()
{
options
=
options
||
{}
var
Sequelize
=
function
(
database
,
username
,
password
,
options
)
{
options
=
options
||
{}
Utils
.
_
.
reject
(
options
,
function
(
_
,
key
)
{
return
[
"host"
,
"port"
,
"disableTableNameModification"
].
indexOf
(
key
)
>
-
1
Utils
.
_
.
reject
(
options
,
function
(
_
,
key
)
{
})
return
[
"host"
,
"port"
,
"disableTableNameModification"
].
indexOf
(
key
)
>
-
1
})
this
.
options
=
options
this
.
options
.
connector
=
this
.
options
.
connector
||
'mysql'
this
.
options
=
options
this
.
config
=
{
this
.
options
.
connector
=
this
.
options
.
connector
||
'mysql'
database
:
database
,
this
.
config
=
{
username
:
username
,
database
:
database
,
password
:
((
([
""
,
null
,
false
].
indexOf
(
password
)
>
-
1
)
||
(
typeof
password
==
'undefined'
))
?
null
:
password
),
username
:
username
,
host
:
options
.
host
||
'localhost'
,
password
:
((
([
""
,
null
,
false
].
indexOf
(
password
)
>
-
1
)
||
(
typeof
password
==
'undefined'
))
?
null
:
password
),
port
:
options
.
port
||
3306
host
:
options
.
host
||
'localhost'
,
port
:
options
.
port
||
3306
}
var
ConnectorManager
=
require
(
"./connectors/"
+
this
.
options
.
connector
+
"/connector-manager"
)
this
.
modelManager
=
new
ModelManager
(
this
)
this
.
connectorManager
=
new
ConnectorManager
(
this
,
this
.
config
)
}
}
var
ConnectorManager
=
require
(
"./connectors/"
+
this
.
options
.
connector
+
"/connector-manager"
)
Sequelize
.
Utils
=
Utils
Sequelize
.
Utils
.
_
.
map
(
DataTypes
,
function
(
sql
,
accessor
)
{
Sequelize
[
accessor
]
=
sql
})
this
.
modelManager
=
new
ModelManager
(
this
)
Sequelize
.
prototype
.
define
=
function
(
modelName
,
attributes
,
options
)
{
this
.
connectorManager
=
new
ConnectorManager
(
this
,
this
.
config
)
options
=
options
||
{}
}
Sequelize
.
Utils
=
Utils
if
(
this
.
options
.
define
)
Sequelize
.
Utils
.
_
.
map
(
DataTypes
,
function
(
sql
,
accessor
)
{
Sequelize
[
accessor
]
=
sql
}
)
options
=
Sequelize
.
Utils
.
merge
(
options
,
this
.
options
.
define
)
Sequelize
.
prototype
.
define
=
function
(
modelName
,
attributes
,
options
)
{
var
model
=
this
.
modelManager
.
addModel
(
new
ModelFactory
(
modelName
,
attributes
,
options
))
options
=
options
||
{}
if
(
this
.
options
.
define
)
return
model
options
=
Sequelize
.
Utils
.
merge
(
options
,
this
.
options
.
define
)
}
var
model
=
this
.
modelManager
.
addModel
(
new
ModelFactory
(
modelName
,
attributes
,
options
))
return
model
Sequelize
.
prototype
.
import
=
function
(
path
)
{
}
var
defineCall
=
require
(
path
)
return
defineCall
(
this
,
DataTypes
)
}
Sequelize
.
prototype
.
import
=
function
(
path
)
{
Sequelize
.
prototype
.
query
=
function
(
sql
,
callee
,
options
)
{
var
defineCall
=
require
(
path
)
options
=
options
||
{}
return
defineCall
(
this
,
DataTypes
)
}
Sequelize
.
prototype
.
query
=
function
(
sql
,
callee
,
options
)
{
if
(
this
.
options
.
query
)
options
=
options
||
{}
options
=
Sequelize
.
Utils
.
merge
(
options
,
this
.
options
.
query
)
if
(
this
.
options
.
query
)
options
.
logging
=
this
.
options
.
hasOwnProperty
(
'logging'
)
?
this
.
options
.
logging
:
true
options
=
Sequelize
.
Utils
.
merge
(
options
,
this
.
options
.
query
)
options
.
logging
=
this
.
options
.
hasOwnProperty
(
'logging'
)
?
this
.
options
.
logging
:
true
return
this
.
connectorManager
.
query
(
sql
,
callee
,
options
)
}
return
this
.
connectorManager
.
query
(
sql
,
callee
,
options
)
Sequelize
.
prototype
.
sync
=
function
(
options
)
{
}
options
=
options
||
{
}
Sequelize
.
prototype
.
sync
=
function
(
options
)
{
if
(
this
.
options
.
sync
)
options
=
options
||
{}
options
=
Sequelize
.
Utils
.
merge
(
options
,
this
.
options
.
sync
)
if
(
this
.
options
.
sync
)
var
self
=
this
options
=
Sequelize
.
Utils
.
merge
(
options
,
this
.
options
.
sync
)
var
eventEmitter
=
new
Utils
.
CustomEventEmitter
(
function
()
{
var
chainer
=
new
Utils
.
QueryChainer
var
self
=
this
self
.
modelManager
.
models
.
forEach
(
function
(
model
)
{
chainer
.
add
(
model
.
sync
(
options
))
})
var
eventEmitter
=
new
Utils
.
CustomEventEmitter
(
function
()
{
var
chainer
=
new
Utils
.
QueryChainer
self
.
modelManager
.
models
.
forEach
(
function
(
model
)
{
chainer
.
add
(
model
.
sync
(
options
))
})
chainer
.
run
()
.
on
(
'success'
,
function
()
{
eventEmitter
.
emit
(
'success'
,
null
)
})
.
on
(
'failure'
,
function
(
err
)
{
eventEmitter
.
emit
(
'failure'
,
err
)
})
})
return
eventEmitter
.
run
()
}
chainer
Sequelize
.
prototype
.
drop
=
function
()
{
.
run
()
var
self
=
this
.
on
(
'success'
,
function
()
{
eventEmitter
.
emit
(
'success'
,
null
)
})
.
on
(
'failure'
,
function
(
err
)
{
eventEmitter
.
emit
(
'failure'
,
err
)
})
})
return
eventEmitter
.
run
()
}
Sequelize
.
prototype
.
drop
=
function
(
)
{
return
new
Utils
.
CustomEventEmitter
(
function
(
emitter
)
{
var
self
=
this
var
chainer
=
new
Utils
.
QueryChainer
return
new
Utils
.
CustomEventEmitter
(
function
(
emitter
)
{
self
.
modelManager
.
models
.
forEach
(
function
(
model
)
{
chainer
.
add
(
model
.
drop
())
})
var
chainer
=
new
Utils
.
QueryChainer
self
.
modelManager
.
models
.
forEach
(
function
(
model
)
{
chainer
.
add
(
model
.
drop
())
})
chainer
.
run
()
.
on
(
'success'
,
function
()
{
emitter
.
emit
(
'success'
,
null
)
})
.
on
(
'failure'
,
function
(
err
)
{
emitter
.
emit
(
'failure'
,
err
)
})
}).
run
()
}
chainer
return
Sequelize
.
run
()
})()
.
on
(
'success'
,
function
()
{
emitter
.
emit
(
'success'
,
null
)
})
.
on
(
'failure'
,
function
(
err
)
{
emitter
.
emit
(
'failure'
,
err
)
})
}).
run
()
}
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