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 ff7794d3
authored
Apr 01, 2011
by
Sascha Depold
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
added helper in utils for client.escape + added extended data types
1 parent
3a463042
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
47 additions
and
7 deletions
lib/sequelize/model-definition.js
lib/sequelize/query-generator.js
lib/sequelize/utils.js
test/Sequelize/define.js
lib/sequelize/model-definition.js
View file @
ff7794d
...
...
@@ -8,7 +8,7 @@ var ModelDefinition = module.exports = function(name, attributes, options) {
this
.
options
=
options
||
{}
this
.
name
=
name
this
.
tableName
=
name
this
.
attributes
=
attributes
this
.
attributes
=
Utils
.
simplifyAttributes
(
attributes
)
this
.
modelManager
=
null
// defined by model-manager during addModel
// additional attributes
...
...
lib/sequelize/query-generator.js
View file @
ff7794d
var
Utils
=
require
(
"./utils"
)
,
client
=
new
(
require
(
"mysql"
).
Client
)()
var
QueryGenerator
=
module
.
exports
=
{
/*
...
...
@@ -74,7 +73,7 @@ var QueryGenerator = module.exports = {
table
:
Utils
.
addTicks
(
tableName
),
attributes
:
Utils
.
_
.
keys
(
attrValueHash
).
map
(
function
(
attr
){
return
Utils
.
addTicks
(
attr
)}).
join
(
","
),
values
:
Utils
.
_
.
values
(
attrValueHash
).
map
(
function
(
value
){
return
client
.
escape
((
value
instanceof
Date
)
?
Utils
.
toSqlDate
(
value
)
:
value
)
return
Utils
.
escape
((
value
instanceof
Date
)
?
Utils
.
toSqlDate
(
value
)
:
value
)
}).
join
(
","
)
}
...
...
@@ -96,7 +95,7 @@ var QueryGenerator = module.exports = {
var
replacements
=
{
table
:
Utils
.
addTicks
(
tableName
),
values
:
Utils
.
_
.
map
(
values
,
function
(
value
,
key
){
return
Utils
.
addTicks
(
key
)
+
"="
+
client
.
escape
((
value
instanceof
Date
)
?
Utils
.
toSqlDate
(
value
)
:
value
)
return
Utils
.
addTicks
(
key
)
+
"="
+
Utils
.
escape
((
value
instanceof
Date
)
?
Utils
.
toSqlDate
(
value
)
:
value
)
}).
join
(
","
),
where
:
QueryGenerator
.
getWhereConditions
(
where
)
}
...
...
@@ -123,7 +122,7 @@ var QueryGenerator = module.exports = {
var
replacements
=
{
table
:
Utils
.
addTicks
(
tableName
),
where
:
QueryGenerator
.
getWhereConditions
(
where
),
limit
:
client
.
escape
(
options
.
limit
)
limit
:
Utils
.
escape
(
options
.
limit
)
}
return
Utils
.
_
.
template
(
query
)(
replacements
)
...
...
@@ -138,7 +137,7 @@ var QueryGenerator = module.exports = {
if
(
Utils
.
isHash
(
smth
))
result
=
QueryGenerator
.
hashToWhereConditions
(
smth
)
else
if
(
typeof
smth
==
'number'
)
result
=
Utils
.
addTicks
(
'id'
)
+
"="
+
client
.
escape
(
smth
)
result
=
Utils
.
addTicks
(
'id'
)
+
"="
+
Utils
.
escape
(
smth
)
else
if
(
typeof
smth
==
"string"
)
result
=
smth
...
...
@@ -151,7 +150,7 @@ var QueryGenerator = module.exports = {
*/
hashToWhereConditions
:
function
(
hash
)
{
return
Utils
.
_
.
map
(
hash
,
function
(
value
,
key
)
{
var
_value
=
client
.
escape
(
value
)
var
_value
=
Utils
.
escape
(
value
)
,
_key
=
Utils
.
addTicks
(
key
)
return
(
_value
==
'NULL'
)
?
_key
+
" IS NULL"
:
[
_key
,
_value
].
join
(
"="
)
...
...
lib/sequelize/utils.js
View file @
ff7794d
var
client
=
new
(
require
(
"mysql"
).
Client
)()
var
Utils
=
module
.
exports
=
{
_
:
require
(
"underscore"
),
addEventEmitter
:
function
(
_class
)
{
...
...
@@ -9,6 +10,9 @@ var Utils = module.exports = {
removeTicks
:
function
(
s
)
{
return
s
.
replace
(
"`"
,
""
)
},
escape
:
function
(
s
)
{
return
client
.
escape
(
s
)
},
isHash
:
function
(
obj
)
{
return
(
typeof
obj
==
'object'
)
&&
!
obj
.
hasOwnProperty
(
'length'
)
},
...
...
@@ -52,6 +56,28 @@ 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
.
default
)
{
template
+=
" DEFAULT <%= defaultValue %>"
replacements
.
defaultValue
=
Utils
.
escape
(
dataType
.
default
)
}
if
(
dataType
.
unique
)
template
+=
" UNIQUE"
result
[
name
]
=
Utils
.
_
.
template
(
template
)(
replacements
)
}
else
{
result
[
name
]
=
dataType
}
})
return
result
},
toSqlDate
:
function
(
date
)
{
return
[
[
...
...
test/Sequelize/define.js
View file @
ff7794d
...
...
@@ -7,5 +7,19 @@ module.exports = {
assert
.
eql
(
s
.
modelManager
.
all
.
length
,
0
)
s
.
define
(
'foo'
,
{
title
:
Sequelize
.
STRING
})
assert
.
eql
(
s
.
modelManager
.
all
.
length
,
1
)
},
'it should handle extended attributes'
:
function
()
{
var
s
=
new
Sequelize
(
'database'
,
'username'
,
'password'
)
var
User
=
s
.
define
(
'User'
,
{
name
:
Sequelize
.
STRING
,
username
:
{
type
:
Sequelize
.
STRING
,
unique
:
true
},
password
:
{
type
:
Sequelize
.
STRING
,
default
:
'password'
}
})
assert
.
eql
(
User
.
attributes
,
{
name
:
"VARCHAR(255)"
,
username
:
"VARCHAR(255) UNIQUE"
,
password
:
"VARCHAR(255) DEFAULT 'password'"
,
id
:
"INT NOT NULL auto_increment PRIMARY KEY"
})
}
}
\ No newline at end of file
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