Created
October 1, 2014 18:46
-
-
Save gjcourt/6c98c0a65d54a3e2ddee to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
App.Storage = _.extend { Database: {} }, App.Storage | |
class App.Storage.Database.Statement | |
__error: (args...) -> | |
console.error args... | |
constructor: (@query, @args=[], @success=(->), @error=@__error) -> | |
class App.Storage.Database.Cursor | |
constructor: (@__db) -> | |
@__statements = [] | |
__success: (args...) -> | |
console.log args... | |
__error: (args...) -> | |
console.error args... | |
__complete: (tx, result_set) -> | |
console.log result_set | |
execute: (statement, args, success=@__success, error=@__error) -> | |
@__statements.push new App.Storage.Database.Statement statement, args, success, error | |
return | |
fetch: (complete=@__complete, error=@__error) -> | |
@__db.transaction (tx) => | |
for statement in @__statements | |
print 'EXECUTING', statement | |
tx.executeSql statement.query, statement.args, statement.success, statement.error | |
, | |
error | |
, | |
complete | |
return | |
class App.Storage.Database.Database | |
""" | |
Simple database API with CRUD capabilities. | |
""" | |
constructor: (name, version) -> | |
@__db = openDatabase(name, version, 'Memorang database', 5*1024*1024) | |
__error: (args...) -> | |
console.error args... | |
cursor: -> | |
return new App.Storage.Database.Cursor @__db | |
execute: (query, values_list, callback, error=@__error) -> | |
# print '[SQL]', query, values_list | |
cursor = @cursor() | |
for values in values_list | |
cursor.execute query, values, callback, error | |
cursor.fetch() | |
return | |
executeRead: (query, args, callback, error=@__error) -> | |
# print '[SQL]', query, args | |
cursor = @cursor() | |
cursor.execute query, args, callback, error | |
cursor.fetch() | |
return | |
create: (name, columns, callback) -> | |
cursor = @cursor() | |
query = "CREATE TABLE IF NOT EXISTS #{name} (#{columns.join(',')})" | |
cursor.execute query, [], callback | |
cursor.fetch() | |
return | |
insert: (table, fields, values_list, callback) -> | |
throw "Missing fields to insert" if not fields.length | |
throw "Missing data to insert" if not values_list.length | |
query = "INSERT INTO #{table} (#{fields.join(',')}) VALUES (#{[0...fields.length].map(-> "?").join(',')})" | |
@execute query, values_list, callback | |
return | |
read: (query, args, callback) -> | |
@executeRead query, args, callback | |
return | |
update: -> | |
throw "Not yet implemented" | |
delete: (table, fields, values, callback) -> | |
throw "Missing fields to insert" if not fields.length | |
throw "Missing mismatched args" if fields.length isnt values.length | |
query = "DELETE FROM #{table} WHERE #{fields.map((x) -> "#{x} = ?").join(' AND ')}" | |
debugger | |
@execute query, [values], callback |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment