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
不要怂,就是干,撸起袖子干!
You need to sign in or sign up before continuing.
Commit 64c9c793
authored
Dec 13, 2011
by
sdepold
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
extracted simplifyAttributes to be dialect specific
1 parent
9e97c40d
Show whitespace changes
Inline
Side-by-side
Showing
10 changed files
with
77 additions
and
46 deletions
lib/associations/belongs-to.js
lib/associations/has-many.js
lib/associations/has-one.js
lib/connectors/mysql/query-generator.js
lib/model-factory.js
lib/model-manager.js
lib/model.js
lib/query-interface.js
lib/sequelize.js
lib/utils.js
lib/associations/belongs-to.js
View file @
64c9c79
...
...
@@ -22,8 +22,7 @@ module.exports = (function() {
this
.
identifier
=
this
.
options
.
foreignKey
||
Utils
.
_
.
underscoredIf
(
Utils
.
singularize
(
this
.
target
.
tableName
)
+
"Id"
,
this
.
source
.
options
.
underscored
)
newAttributes
[
this
.
identifier
]
=
{
type
:
DataTypes
.
INTEGER
}
Utils
.
_
.
extend
(
this
.
source
.
attributes
,
Utils
.
simplifyAttributes
(
newAttributes
))
Utils
.
_
.
extend
(
this
.
source
.
attributes
,
this
.
source
.
QueryGenerator
.
attributesToSQL
(
newAttributes
))
return
this
}
...
...
lib/associations/has-many.js
View file @
64c9c79
...
...
@@ -55,7 +55,7 @@ module.exports = (function() {
}
else
{
var
newAttributes
=
{}
newAttributes
[
this
.
identifier
]
=
{
type
:
DataTypes
.
INTEGER
}
Utils
.
_
.
extend
(
this
.
target
.
attributes
,
Utils
.
simplifyAttributes
(
newAttributes
))
Utils
.
_
.
extend
(
this
.
target
.
attributes
,
this
.
source
.
QueryGenerator
.
attributesToSQL
(
newAttributes
))
}
return
this
...
...
lib/associations/has-one.js
View file @
64c9c79
...
...
@@ -27,7 +27,7 @@ module.exports = (function() {
this
.
identifier
=
this
.
options
.
foreignKey
||
Utils
.
_
.
underscoredIf
(
Utils
.
singularize
(
this
.
source
.
tableName
)
+
"Id"
,
this
.
options
.
underscored
)
newAttributes
[
this
.
identifier
]
=
{
type
:
DataTypes
.
INTEGER
}
Utils
.
_
.
extend
(
this
.
target
.
attributes
,
Utils
.
simplifyAttributes
(
newAttributes
))
Utils
.
_
.
extend
(
this
.
target
.
attributes
,
this
.
source
.
QueryGenerator
.
attributesToSQL
(
newAttributes
))
return
this
}
...
...
lib/connectors/mysql/query-generator.js
View file @
64c9c79
...
...
@@ -247,6 +247,32 @@ module.exports = (function() {
return
(
_value
==
'NULL'
)
?
_key
+
" IS NULL"
:
[
_key
,
_value
].
join
(
"="
)
}
}).
join
(
" AND "
)
},
attributesToSQL
:
function
(
attributes
)
{
var
result
=
{}
Utils
.
_
.
map
(
attributes
,
function
(
dataType
,
name
)
{
if
(
Utils
.
isHash
(
dataType
))
{
var
template
=
"<%= type %>"
,
replacements
=
{
type
:
dataType
.
type
}
if
(
dataType
.
hasOwnProperty
(
'allowNull'
)
&&
(
!
dataType
.
allowNull
))
template
+=
" NOT NULL"
if
(
dataType
.
autoIncrement
)
template
+=
" auto_increment"
if
(
dataType
.
defaultValue
!=
undefined
)
{
template
+=
" DEFAULT <%= defaultValue %>"
replacements
.
defaultValue
=
Utils
.
escape
(
dataType
.
defaultValue
)
}
if
(
dataType
.
unique
)
template
+=
" UNIQUE"
if
(
dataType
.
primaryKey
)
template
+=
" PRIMARY KEY"
result
[
name
]
=
Utils
.
_
.
template
(
template
)(
replacements
)
}
else
{
result
[
name
]
=
dataType
}
})
return
result
}
}
...
...
lib/model-factory.js
View file @
64c9c79
...
...
@@ -18,21 +18,36 @@ module.exports = (function() {
this
.
name
=
name
this
.
tableName
=
this
.
options
.
freezeTableName
?
name
:
Utils
.
pluralize
(
name
)
this
.
attributes
=
Utils
.
simplifyAttributes
(
attributes
)
this
.
rawAttributes
=
attributes
this
.
modelManager
=
null
// defined
by model-manager during addModel
this
.
modelManager
=
null
// defined
in init function
this
.
associations
=
{}
// extract validation
this
.
validate
=
this
.
options
.
validate
||
{}
}
ModelFactory
.
prototype
.
init
=
function
(
modelManager
)
{
this
.
modelManager
=
modelManager
addDefaultAttributes
.
call
(
this
)
addOptionalClassMethods
.
call
(
this
)
findAutoIncrementField
.
call
(
this
)
return
this
}
ModelFactory
.
prototype
.
__defineGetter__
(
'QueryInterface'
,
function
()
{
return
this
.
modelManager
.
sequelize
.
getQueryInterface
()
Object
.
defineProperty
(
ModelFactory
.
prototype
,
'attributes'
,
{
get
:
function
()
{
return
this
.
QueryGenerator
.
attributesToSQL
(
this
.
rawAttributes
)
}
})
Object
.
defineProperty
(
ModelFactory
.
prototype
,
'QueryInterface'
,
{
get
:
function
()
{
return
this
.
modelManager
.
sequelize
.
getQueryInterface
()
}
})
Object
.
defineProperty
(
ModelFactory
.
prototype
,
'QueryGenerator'
,
{
get
:
function
()
{
return
this
.
QueryInterface
.
QueryGenerator
}
})
ModelFactory
.
prototype
.
sync
=
function
(
options
)
{
...
...
@@ -154,7 +169,6 @@ module.exports = (function() {
ModelFactory
.
prototype
.
__defineGetter__
(
'primaryKeys'
,
function
()
{
var
result
=
{}
Utils
.
_
.
each
(
this
.
attributes
,
function
(
dataTypeString
,
attributeName
)
{
if
((
attributeName
!=
'id'
)
&&
(
dataTypeString
.
indexOf
(
'PRIMARY KEY'
)
>
-
1
))
result
[
attributeName
]
=
dataTypeString
...
...
@@ -188,8 +202,15 @@ module.exports = (function() {
}
var
addDefaultAttributes
=
function
()
{
var
defaultAttributes
=
{
id
:
{
type
:
DataTypes
.
INTEGER
,
allowNull
:
false
,
primaryKey
:
true
,
autoIncrement
:
true
}}
,
self
=
this
var
self
=
this
,
defaultAttributes
=
{
id
:
{
type
:
DataTypes
.
INTEGER
,
allowNull
:
false
,
primaryKey
:
true
,
autoIncrement
:
true
}
}
if
(
this
.
hasPrimaryKeys
)
defaultAttributes
=
{}
...
...
@@ -201,8 +222,9 @@ module.exports = (function() {
defaultAttributes
[
Utils
.
_
.
underscoredIf
(
'deletedAt'
,
this
.
options
.
underscored
)]
=
{
type
:
DataTypes
.
DATE
}
}
defaultAttributes
=
Utils
.
simplifyAttributes
(
defaultAttributes
)
Utils
.
_
.
map
(
defaultAttributes
,
function
(
value
,
attr
)
{
self
.
attributes
[
attr
]
=
value
})
Utils
.
_
.
each
(
defaultAttributes
,
function
(
value
,
attr
)
{
self
.
rawAttributes
[
attr
]
=
value
})
}
var
findAutoIncrementField
=
function
()
{
...
...
lib/model-manager.js
View file @
64c9c79
...
...
@@ -5,7 +5,6 @@ module.exports = (function() {
}
ModelManager
.
prototype
.
addModel
=
function
(
model
)
{
model
.
modelManager
=
this
this
.
models
.
push
(
model
)
return
model
...
...
lib/model.js
View file @
64c9c79
...
...
@@ -27,8 +27,16 @@ module.exports = (function() {
destroy
:
'DestroyQuery'
}
Model
.
prototype
.
__defineGetter__
(
'QueryInterface'
,
function
()
{
return
this
.
__definition
.
modelManager
.
sequelize
.
getQueryInterface
()
Object
.
defineProperty
(
Model
.
prototype
,
'sequelize'
,
{
get
:
function
(){
return
this
.
__definition
.
modelManager
.
sequelize
}
})
Object
.
defineProperty
(
Model
.
prototype
,
'QueryInterface'
,
{
get
:
function
(){
return
this
.
sequelize
.
getQueryInterface
()
}
})
Object
.
defineProperty
(
Model
.
prototype
,
'QueryGenerator'
,
{
get
:
function
(){
return
this
.
QueryInterface
.
QueryGenerator
}
})
Model
.
prototype
.
save
=
function
()
{
...
...
lib/query-interface.js
View file @
64c9c79
...
...
@@ -88,7 +88,7 @@ module.exports = (function() {
else
attributes
[
attributeName
]
=
dataTypeOrOptions
var
options
=
Utils
.
simplifyAttributes
(
attributes
)
var
options
=
this
.
QueryGenerator
.
attributesToSQL
(
attributes
)
,
sql
=
this
.
QueryGenerator
.
addColumnQuery
(
tableName
,
options
)
return
queryAndEmit
.
call
(
this
,
sql
,
'addColumn'
)
...
...
@@ -107,7 +107,7 @@ module.exports = (function() {
else
attributes
[
attributeName
]
=
dataTypeOrOptions
var
options
=
Utils
.
simplifyAttributes
(
attributes
)
var
options
=
this
.
QueryGenerator
.
attributesToSQL
(
attributes
)
,
sql
=
this
.
QueryGenerator
.
changeColumnQuery
(
tableName
,
options
)
return
queryAndEmit
.
call
(
this
,
sql
,
'changeColumn'
)
...
...
@@ -130,7 +130,7 @@ module.exports = (function() {
var
sql
=
self
.
QueryGenerator
.
renameColumnQuery
(
tableName
,
attrNameBefore
,
Utils
.
simplifyAttributes
(
options
)
self
.
QueryGenerator
.
attributesToSQL
(
options
)
)
self
.
sequelize
.
query
(
sql
).
success
(
function
()
{
...
...
lib/sequelize.js
View file @
64c9c79
...
...
@@ -44,9 +44,11 @@ module.exports = (function() {
if
(
this
.
options
.
define
)
options
=
Sequelize
.
Utils
.
merge
(
options
,
this
.
options
.
define
)
var
model
=
this
.
modelManager
.
addModel
(
new
ModelFactory
(
modelName
,
attributes
,
options
)
)
var
factory
=
new
ModelFactory
(
modelName
,
attributes
,
options
)
return
model
this
.
modelManager
.
addModel
(
factory
.
init
(
this
.
modelManager
))
return
factory
}
Sequelize
.
prototype
.
import
=
function
(
path
)
{
...
...
lib/utils.js
View file @
64c9c79
...
...
@@ -87,31 +87,6 @@ var Utils = module.exports = {
return
(
"'"
+
value
+
"'"
)
},
simplifyAttributes
:
function
(
attributes
)
{
var
result
=
{}
Utils
.
_
.
map
(
attributes
,
function
(
dataType
,
name
)
{
if
(
Utils
.
isHash
(
dataType
))
{
var
template
=
"<%= type %>"
,
replacements
=
{
type
:
dataType
.
type
}
if
(
dataType
.
hasOwnProperty
(
'allowNull'
)
&&
(
!
dataType
.
allowNull
))
template
+=
" NOT NULL"
if
(
dataType
.
autoIncrement
)
template
+=
" auto_increment"
if
(
dataType
.
defaultValue
!=
undefined
)
{
template
+=
" DEFAULT <%= defaultValue %>"
replacements
.
defaultValue
=
Utils
.
escape
(
dataType
.
defaultValue
)
}
if
(
dataType
.
unique
)
template
+=
" UNIQUE"
if
(
dataType
.
primaryKey
)
template
+=
" PRIMARY KEY"
result
[
name
]
=
Utils
.
_
.
template
(
template
)(
replacements
)
}
else
{
result
[
name
]
=
dataType
}
})
return
result
},
toSqlDate
:
function
(
date
)
{
return
[
[
...
...
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