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 485cf460
authored
Oct 31, 2011
by
Sascha Depold
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
allow charset definition + test on travis
1 parent
b35685ce
Hide whitespace changes
Inline
Side-by-side
Showing
5 changed files
with
34 additions
and
32 deletions
lib/sequelize/model-definition.js
lib/sequelize/query-generator.js
spec/config/config.js
test/Model/equals.js
test/config.js
lib/sequelize/model-definition.js
View file @
485cf46
...
...
@@ -72,7 +72,7 @@ ModelDefinition.prototype.sync = function(options) {
var
self
=
this
var
doQuery
=
function
()
{
self
.
query
(
QueryGenerator
.
createTableQuery
(
self
.
tableName
,
self
.
attributes
))
self
.
query
(
QueryGenerator
.
createTableQuery
(
self
.
tableName
,
self
.
attributes
,
options
))
.
on
(
'success'
,
function
()
{
self
.
emit
(
'success'
,
self
)
})
.
on
(
'failure'
,
function
(
err
)
{
self
.
emit
(
'failure'
,
err
)
})
}
...
...
@@ -109,7 +109,7 @@ ModelDefinition.prototype.count = function(options) {
ModelDefinition
.
prototype
.
max
=
function
(
field
,
options
)
{
var
self
=
this
var
emitter
=
new
Utils
.
CustomEventEmitter
(
function
()
{
self
.
query
(
QueryGenerator
.
maxQuery
(
self
.
tableName
,
field
,
options
),
self
,
{
plain
:
true
}).
on
(
'success'
,
function
(
obj
)
{
emitter
.
emit
(
'success'
,
obj
[
'max'
])
...
...
@@ -119,7 +119,7 @@ ModelDefinition.prototype.max = function(field, options) {
}
ModelDefinition
.
prototype
.
min
=
function
(
field
,
options
)
{
var
self
=
this
var
emitter
=
new
Utils
.
CustomEventEmitter
(
function
()
{
self
.
query
(
QueryGenerator
.
minQuery
(
self
.
tableName
,
field
,
options
),
self
,
{
plain
:
true
}).
on
(
'success'
,
function
(
obj
)
{
emitter
.
emit
(
'success'
,
obj
[
'min'
])
...
...
lib/sequelize/query-generator.js
View file @
485cf46
...
...
@@ -7,8 +7,8 @@ var QueryGenerator = module.exports = {
*/
createTableQuery
:
function
(
tableName
,
attributes
,
options
)
{
options
=
options
||
{}
var
query
=
"CREATE TABLE IF NOT EXISTS <%= table %> (<%= attributes%>)
;
"
var
query
=
"CREATE TABLE IF NOT EXISTS <%= table %> (<%= attributes%>)"
,
primaryKeys
=
[]
,
attrStr
=
Utils
.
_
.
map
(
attributes
,
function
(
dataType
,
attr
)
{
var
dt
=
dataType
...
...
@@ -21,25 +21,28 @@ var QueryGenerator = module.exports = {
}).
join
(
", "
)
,
values
=
{
table
:
Utils
.
addTicks
(
tableName
),
attributes
:
attrStr
}
,
pkString
=
primaryKeys
.
map
(
function
(
pk
)
{
return
Utils
.
addTicks
(
pk
)}).
join
(
", "
)
if
(
pkString
.
length
>
0
)
values
.
attributes
+=
", PRIMARY KEY ("
+
pkString
+
")"
if
(
options
.
charset
)
query
+=
" DEFAULT CHARSET="
+
options
.
charset
query
+=
";"
return
Utils
.
_
.
template
(
query
)(
values
)
},
dropTableQuery
:
function
(
tableName
,
options
)
{
options
=
options
||
{}
var
query
=
"DROP TABLE IF EXISTS <%= table %>;"
return
Utils
.
_
.
template
(
query
)({
table
:
Utils
.
addTicks
(
tableName
)})
},
/*
Returns a query for selecting elements in the database <tableName>.
Options:
- attributes -> An array of attributes (e.g. ['name', 'birthday']). Default: *
- where -> A hash with conditions (e.g. {name: 'foo'})
- where -> A hash with conditions (e.g. {name: 'foo'})
OR an ID as integer
OR a string with conditions (e.g. 'name="foo"').
If you use a string, you have to escape it on your own.
...
...
@@ -53,9 +56,9 @@ var QueryGenerator = module.exports = {
options
.
table
=
Utils
.
addTicks
(
tableName
)
options
.
attributes
=
options
.
attributes
&&
options
.
attributes
.
map
(
function
(
attr
){
return
Utils
.
addTicks
(
attr
)}).
join
(
", "
)
options
.
attributes
=
options
.
attributes
||
'*'
var
query
=
"SELECT <%= attributes %> FROM <%= table %>"
if
(
options
.
where
)
{
options
.
where
=
QueryGenerator
.
getWhereConditions
(
options
.
where
)
query
+=
" WHERE <%= where %>"
...
...
@@ -69,12 +72,12 @@ var QueryGenerator = module.exports = {
if
(
options
.
offset
)
query
+=
" LIMIT <%= offset %>, <%= limit %>"
else
query
+=
" LIMIT <%= limit %>"
}
query
+=
";"
return
Utils
.
_
.
template
(
query
)(
options
)
},
countQuery
:
function
(
tableName
,
options
)
{
return
QueryGenerator
.
selectQuery
(
tableName
,
options
).
replace
(
"*"
,
"count(*)"
)
},
...
...
@@ -89,7 +92,7 @@ var QueryGenerator = module.exports = {
*/
insertQuery
:
function
(
tableName
,
attrValueHash
)
{
var
query
=
"INSERT INTO <%= table %> (<%= attributes %>) VALUES (<%= values %>);"
var
replacements
=
{
table
:
Utils
.
addTicks
(
tableName
),
attributes
:
Utils
.
_
.
keys
(
attrValueHash
).
map
(
function
(
attr
){
return
Utils
.
addTicks
(
attr
)}).
join
(
","
),
...
...
@@ -97,7 +100,7 @@ var QueryGenerator = module.exports = {
return
Utils
.
escape
((
value
instanceof
Date
)
?
Utils
.
toSqlDate
(
value
)
:
value
)
}).
join
(
","
)
}
return
Utils
.
_
.
template
(
query
)(
replacements
)
},
...
...
@@ -106,7 +109,7 @@ var QueryGenerator = module.exports = {
Parameters:
- tableName -> Name of the table
- values -> A hash with attribute-value-pairs
- where -> A hash with conditions (e.g. {name: 'foo'})
- where -> A hash with conditions (e.g. {name: 'foo'})
OR an ID as integer
OR a string with conditions (e.g. 'name="foo"').
If you use a string, you have to escape it on your own.
...
...
@@ -120,15 +123,15 @@ var QueryGenerator = module.exports = {
}).
join
(
","
),
where
:
QueryGenerator
.
getWhereConditions
(
where
)
}
return
Utils
.
_
.
template
(
query
)(
replacements
)
},
/*
Returns a deletion query.
Parameters:
- tableName -> Name of the table
- where -> A hash with conditions (e.g. {name: 'foo'})
- where -> A hash with conditions (e.g. {name: 'foo'})
OR an ID as integer
OR a string with conditions (e.g. 'name="foo"').
If you use a string, you have to escape it on your own.
...
...
@@ -138,23 +141,23 @@ var QueryGenerator = module.exports = {
deleteQuery
:
function
(
tableName
,
where
,
options
)
{
options
=
options
||
{}
options
.
limit
=
options
.
limit
||
1
var
query
=
"DELETE FROM <%= table %> WHERE <%= where %> LIMIT <%= limit %>"
var
replacements
=
{
table
:
Utils
.
addTicks
(
tableName
),
where
:
QueryGenerator
.
getWhereConditions
(
where
),
limit
:
Utils
.
escape
(
options
.
limit
)
}
return
Utils
.
_
.
template
(
query
)(
replacements
)
},
/*
Takes something and transforms it into values of a where condition.
*/
getWhereConditions
:
function
(
smth
)
{
var
result
=
null
if
(
Utils
.
isHash
(
smth
))
result
=
QueryGenerator
.
hashToWhereConditions
(
smth
)
else
if
(
typeof
smth
==
'number'
)
...
...
@@ -166,7 +169,7 @@ var QueryGenerator = module.exports = {
return
result
},
/*
Takes a hash and transforms it into a mysql where condition: {key: value, key2: value2} ==> key=value AND key2=value2
The values are transformed by the relevant datatype.
...
...
@@ -179,4 +182,4 @@ var QueryGenerator = module.exports = {
return
(
_value
==
'NULL'
)
?
_key
+
" IS NULL"
:
[
_key
,
_value
].
join
(
"="
)
}).
join
(
" AND "
)
}
}
\ No newline at end of file
}
spec/config/config.js
View file @
485cf46
...
...
@@ -4,6 +4,6 @@ module.exports = {
database
:
'sequelize_test'
,
host
:
'127.0.0.1'
,
rand
:
function
()
{
return
parseInt
(
Math
.
random
()
*
999
99
)
return
parseInt
(
Math
.
random
()
*
999
)
}
}
test/Model/equals.js
View file @
485cf46
...
...
@@ -21,7 +21,7 @@ module.exports = {
name
:
Sequelize
.
STRING
,
bio
:
Sequelize
.
TEXT
})
User
.
sync
({
force
:
true
}).
on
(
'success'
,
function
()
{
User
.
sync
({
force
:
true
,
charset
:
'latin1'
}).
on
(
'success'
,
function
()
{
User
.
create
({
foo
:
'1'
,
bar
:
'2'
,
name
:
'hallo'
,
bio
:
'welt'
}).
on
(
'success'
,
function
(
u
)
{
assert
.
eql
(
u
.
equals
(
u
),
true
)
exit
(
function
(){})
...
...
test/config.js
View file @
485cf46
...
...
@@ -4,6 +4,6 @@ module.exports = {
database
:
'sequelize_test'
,
host
:
'127.0.0.1'
,
rand
:
function
()
{
return
parseInt
(
Math
.
random
()
*
999
9999
)
return
parseInt
(
Math
.
random
()
*
999
)
}
}
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