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 68a93410
authored
Jul 25, 2010
by
Sascha Depold
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
moved all sql helpers to Helper.SQL.X
1 parent
52b01550
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
100 additions
and
32 deletions
sequelize.js
sequelize.js
View file @
68a9341
...
...
@@ -81,54 +81,108 @@ exports.Sequelize.prototype = {
var
TableWrapper
=
function
(
sequelize
,
tableName
,
attributes
)
{
var
table
=
function
(
values
)
{
var
self
=
this
Helper
.
Hash
.
keys
(
values
).
forEach
(
function
(
key
)
{
if
(
attributes
[
key
])
{
self
[
key
]
=
values
[
key
]
}
Helper
.
Hash
.
forEach
(
values
,
function
(
value
,
key
)
{
if
(
attributes
[
key
])
self
[
key
]
=
value
})
this
.
id
=
null
// specify id as null to declare this object as unsaved and as not present in the database
this
.
tableName
=
tableName
this
.
attributes
=
attributes
}
table
.
sync
=
function
(
callback
)
{
var
fields
=
[
"id INT"
]
// class methods
var
classMethods
=
{
sync
:
function
(
callback
)
{
var
fields
=
[
"id INT NOT NULL auto_increment PRIMARY KEY"
]
Helper
.
Hash
.
keys
(
attributes
).
forEach
(
function
(
name
)
{
fields
.
push
(
name
+
" "
+
attributes
[
name
])
})
var
query
=
"CREATE TABLE IF NOT EXISTS "
+
tableName
+
" ("
+
fields
.
join
(
', '
)
+
")"
sequelize
.
query
(
query
,
callback
)
}
sequelize
.
query
(
query
,
function
()
{
if
(
callback
)
callback
(
table
)
})
},
table
.
drop
=
function
(
callback
)
{
drop
:
function
(
callback
)
{
var
query
=
"DROP TABLE IF EXISTS "
+
tableName
sequelize
.
query
(
query
,
callback
)
}
sequelize
.
query
(
query
,
function
()
{
if
(
callback
)
callback
(
table
)
})
},
table
.
findAll
=
function
(
callback
)
{
findAll
:
function
(
callback
)
{
var
query
=
"SELECT * FROM "
+
tableName
sequelize
.
query
(
query
,
callback
)
sequelize
.
query
(
query
,
function
(
result
)
{
var
objects
=
[]
result
.
forEach
(
function
(
resultSet
)
{
objects
.
push
(
table
.
sqlResultToObject
(
resultSet
))
})
if
(
callback
)
callback
(
objects
)
})
},
find
:
function
(
conditions
,
callback
)
{
var
query
=
Helper
.
evaluateTemplate
(
"SELECT * FROM %{table} WHERE %{conditions} ORDER BY id DESC LIMIT 1"
,
{
table
:
tableName
,
conditions
:
Helper
.
SQL
.
hashToWhereConditions
(
conditions
)
}
)
sequelize
.
query
(
query
,
function
(
result
)
{
if
(
callback
)
callback
(
table
.
sqlResultToObject
(
result
[
0
]))
})
},
sqlResultToObject
:
function
(
result
)
{
var
object
=
new
table
(
result
)
object
.
id
=
result
.
id
return
object
}
}
// instance methods
table
.
prototype
=
{
get
values
()
{
var
result
=
{}
var
self
=
this
Helper
.
Hash
.
keys
(
attributes
).
forEach
(
function
(
attribute
)
{
result
[
attribute
]
=
self
[
attribute
]
})
return
result
},
save
:
function
(
callback
)
{
var
query
=
null
var
self
=
this
if
(
this
.
id
==
null
)
{
query
=
Helper
.
evaluateTemplate
(
"INSERT INTO %{table} (%{fields}) VALUES (%{values})"
,
{
table
:
this
.
tableName
,
fields
:
Helper
.
fieldsForInsertQuery
(
this
),
values
:
Helper
.
valuesForInsertQuery
(
this
)
}
{
table
:
this
.
tableName
,
fields
:
Helper
.
SQL
.
fieldsForInsertQuery
(
this
),
values
:
Helper
.
SQL
.
valuesForInsertQuery
(
this
)
}
)
}
else
{
query
=
evaluateTemplate
(
"UPDATE %{table} SET %{values} WHERE id = %{id}"
,
{
table
:
this
.
tableName
,
values
:
Helper
.
valuesForUpdate
(
this
),
id
:
this
.
id
}
{
table
:
this
.
tableName
,
values
:
Helper
.
SQL
.
valuesForUpdate
(
this
),
id
:
this
.
id
}
)
}
sequelize
.
query
(
query
,
callback
)
sequelize
.
query
(
query
,
function
()
{
if
(
self
.
id
==
null
)
{
table
.
find
(
self
.
values
,
function
(
result
)
{
Helper
.
log
(
result
)
self
.
id
=
result
.
id
if
(
callback
)
callback
(
self
)
})
}
else
{
if
(
callback
)
callback
(
self
)
}
})
}
}
Helper
.
Hash
.
forEach
(
classMethods
,
function
(
func
,
funcName
)
{
table
[
funcName
]
=
func
})
return
table
}
...
...
@@ -140,30 +194,23 @@ var Helper = {
sys
.
puts
(
sys
.
inspect
(
obj
))
},
values
:
function
(
object
)
{
var
result
=
{}
Helper
.
Hash
.
keys
(
object
.
attributes
).
forEach
(
function
(
key
)
{
result
[
key
]
=
object
[
key
]
})
return
result
},
SQL
:
{
valuesForInsertQuery
:
function
(
object
)
{
var
actualValues
=
Helper
.
values
(
object
)
,
var
actualValues
=
object
.
values
,
result
=
[]
Helper
.
Hash
.
keys
(
actualValues
).
forEach
(
function
(
key
)
{
var
value
=
actualValues
[
key
],
dataType
=
object
.
attributes
[
key
]
result
.
push
(
Helper
.
transformValueByDataType
(
value
,
dataType
))
result
.
push
(
Helper
.
SQL
.
transformValueByDataType
(
value
,
dataType
))
})
return
result
},
fieldsForInsertQuery
:
function
(
object
)
{
return
Helper
.
Hash
.
keys
(
Helper
.
values
(
object
)
).
join
(
", "
)
return
Helper
.
Hash
.
keys
(
object
.
values
).
join
(
", "
)
},
transformValueByDataType
:
function
(
value
,
dataType
)
{
...
...
@@ -177,18 +224,33 @@ var Helper = {
return
result
},
valuesForUpdate
:
function
(
object
)
{
var
actualValues
=
Helper
.
values
(
object
)
,
valuesForUpdate
:
function
(
object
,
options
)
{
var
actualValues
=
object
.
values
,
result
=
[]
options
=
options
||
{}
Helper
.
Hash
.
keys
(
actualValues
).
forEach
(
function
(
key
)
{
var
value
=
actualValues
[
key
],
dataType
=
object
.
attributes
[
key
]
result
.
push
([
key
,
Helper
.
transformValueByDataType
(
value
,
dataType
)].
join
(
" = "
))
result
.
push
([
key
,
Helper
.
SQL
.
transformValueByDataType
(
value
,
dataType
)].
join
(
" = "
))
})
return
result
.
join
(
", "
)
return
result
.
join
(
options
.
seperator
||
", "
)
},
hashToWhereConditions
:
function
(
conditions
)
{
if
(
typeof
conditions
==
'number'
)
return
(
'id = '
+
conditions
)
else
{
var
result
=
[]
Helper
.
Hash
.
forEach
(
conditions
,
function
(
value
,
key
)
{
result
.
push
(
key
+
"="
+
Helper
.
SQL
.
transformValueByDataType
(
value
))
})
return
result
.
join
(
" AND "
)
}
}
},
evaluateTemplate
:
function
(
template
,
replacements
)
{
...
...
@@ -200,6 +262,12 @@ var Helper = {
},
Hash
:
{
forEach
:
function
(
object
,
func
)
{
Helper
.
Hash
.
keys
(
object
).
forEach
(
function
(
key
)
{
func
(
object
[
key
],
key
,
object
)
})
},
keys
:
function
(
object
)
{
var
results
=
[]
for
(
var
property
in
object
)
...
...
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