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
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
138 additions
and
70 deletions
sequelize.js
sequelize.js
View file @
68a9341
...
@@ -81,54 +81,108 @@ exports.Sequelize.prototype = {
...
@@ -81,54 +81,108 @@ exports.Sequelize.prototype = {
var
TableWrapper
=
function
(
sequelize
,
tableName
,
attributes
)
{
var
TableWrapper
=
function
(
sequelize
,
tableName
,
attributes
)
{
var
table
=
function
(
values
)
{
var
table
=
function
(
values
)
{
var
self
=
this
var
self
=
this
Helper
.
Hash
.
keys
(
values
).
forEach
(
function
(
key
)
{
Helper
.
Hash
.
forEach
(
values
,
function
(
value
,
key
)
{
if
(
attributes
[
key
])
{
if
(
attributes
[
key
])
self
[
key
]
=
values
[
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
.
id
=
null
// specify id as null to declare this object as unsaved and as not present in the database
this
.
tableName
=
tableName
this
.
tableName
=
tableName
this
.
attributes
=
attributes
this
.
attributes
=
attributes
}
}
table
.
sync
=
function
(
callback
)
{
// class methods
var
fields
=
[
"id INT"
]
var
classMethods
=
{
Helper
.
Hash
.
keys
(
attributes
).
forEach
(
function
(
name
)
{
fields
.
push
(
name
+
" "
+
attributes
[
name
])
})
sync
:
function
(
callback
)
{
var
query
=
"CREATE TABLE IF NOT EXISTS "
+
tableName
+
" ("
+
fields
.
join
(
', '
)
+
")"
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
)
{
},
var
query
=
"DROP TABLE IF EXISTS "
+
tableName
sequelize
.
query
(
query
,
callback
)
drop
:
function
(
callback
)
{
}
var
query
=
"DROP TABLE IF EXISTS "
+
tableName
sequelize
.
query
(
query
,
function
()
{
table
.
findAll
=
function
(
callback
)
{
if
(
callback
)
callback
(
table
)
var
query
=
"SELECT * FROM "
+
tableName
})
sequelize
.
query
(
query
,
callback
)
},
findAll
:
function
(
callback
)
{
var
query
=
"SELECT * FROM "
+
tableName
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
=
{
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
)
{
save
:
function
(
callback
)
{
var
query
=
null
var
query
=
null
var
self
=
this
if
(
this
.
id
==
null
)
{
if
(
this
.
id
==
null
)
{
query
=
Helper
.
evaluateTemplate
(
query
=
Helper
.
evaluateTemplate
(
"INSERT INTO %{table} (%{fields}) VALUES (%{values})"
,
"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
{
}
else
{
query
=
evaluateTemplate
(
query
=
evaluateTemplate
(
"UPDATE %{table} SET %{values} WHERE id = %{id}"
,
"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
return
table
}
}
...
@@ -140,55 +194,63 @@ var Helper = {
...
@@ -140,55 +194,63 @@ var Helper = {
sys
.
puts
(
sys
.
inspect
(
obj
))
sys
.
puts
(
sys
.
inspect
(
obj
))
},
},
values
:
function
(
object
)
{
SQL
:
{
var
result
=
{}
valuesForInsertQuery
:
function
(
object
)
{
Helper
.
Hash
.
keys
(
object
.
attributes
).
forEach
(
function
(
key
)
{
var
actualValues
=
object
.
values
,
result
[
key
]
=
object
[
key
]
result
=
[]
})
return
result
Helper
.
Hash
.
keys
(
actualValues
).
forEach
(
function
(
key
)
{
},
var
value
=
actualValues
[
key
],
dataType
=
object
.
attributes
[
key
]
valuesForInsertQuery
:
function
(
object
)
{
var
actualValues
=
Helper
.
values
(
object
),
result
.
push
(
Helper
.
SQL
.
transformValueByDataType
(
value
,
dataType
))
result
=
[]
})
Helper
.
Hash
.
keys
(
actualValues
).
forEach
(
function
(
key
)
{
return
result
var
value
=
actualValues
[
key
],
},
dataType
=
object
.
attributes
[
key
]
fieldsForInsertQuery
:
function
(
object
)
{
result
.
push
(
Helper
.
transformValueByDataType
(
value
,
dataType
))
return
Helper
.
Hash
.
keys
(
object
.
values
).
join
(
", "
)
})
},
transformValueByDataType
:
function
(
value
,
dataType
)
{
var
result
=
null
switch
(
dataType
)
{
case
exports
.
Sequelize
.
INTEGER
:
result
=
value
;
break
;
default
:
result
=
"'"
+
value
+
"'"
;
break
;
}
return
result
},
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
.
SQL
.
transformValueByDataType
(
value
,
dataType
)].
join
(
" = "
))
})
return
result
.
join
(
options
.
seperator
||
", "
)
},
return
result
hashToWhereConditions
:
function
(
conditions
)
{
},
if
(
typeof
conditions
==
'number'
)
return
(
'id = '
+
conditions
)
fieldsForInsertQuery
:
function
(
object
)
{
else
{
return
Helper
.
Hash
.
keys
(
Helper
.
values
(
object
)).
join
(
", "
)
var
result
=
[]
},
Helper
.
Hash
.
forEach
(
conditions
,
function
(
value
,
key
)
{
result
.
push
(
key
+
"="
+
Helper
.
SQL
.
transformValueByDataType
(
value
))
transformValueByDataType
:
function
(
value
,
dataType
)
{
})
var
result
=
null
return
result
.
join
(
" AND "
)
switch
(
dataType
)
{
}
case
exports
.
Sequelize
.
INTEGER
:
result
=
value
;
break
;
default
:
result
=
"'"
+
value
+
"'"
;
break
;
}
}
return
result
},
valuesForUpdate
:
function
(
object
)
{
var
actualValues
=
Helper
.
values
(
object
),
result
=
[]
Helper
.
Hash
.
keys
(
actualValues
).
forEach
(
function
(
key
)
{
var
value
=
actualValues
[
key
],
dataType
=
object
.
attributes
[
key
]
result
.
push
([
key
,
Helper
.
transformValueByDataType
(
value
,
dataType
)].
join
(
" = "
))
})
return
result
.
join
(
", "
)
},
},
evaluateTemplate
:
function
(
template
,
replacements
)
{
evaluateTemplate
:
function
(
template
,
replacements
)
{
...
@@ -200,6 +262,12 @@ var Helper = {
...
@@ -200,6 +262,12 @@ var Helper = {
},
},
Hash
:
{
Hash
:
{
forEach
:
function
(
object
,
func
)
{
Helper
.
Hash
.
keys
(
object
).
forEach
(
function
(
key
)
{
func
(
object
[
key
],
key
,
object
)
})
},
keys
:
function
(
object
)
{
keys
:
function
(
object
)
{
var
results
=
[]
var
results
=
[]
for
(
var
property
in
object
)
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