Class: SwissDB::DataStore
- Inherits:
-
Android::Database::SQLite::SQLiteOpenHelper
- Object
- Android::Database::SQLite::SQLiteOpenHelper
- SwissDB::DataStore
- Defined in:
- lib/swiss_db/data_store.rb
Constant Summary collapse
- ContentValues =
Android::Content::ContentValues
Class Method Summary collapse
Instance Method Summary collapse
-
#coerces(v) ⇒ Object
transform values.
-
#destroy_all(db = writable_db, table) ⇒ Object
deleting all records.
-
#insert(db = writable_db, table, hash_values) ⇒ Object
insert.
-
#onCreate(db) ⇒ Object
create.
- #onUpgrade(db, oldVersion, newVersion) ⇒ Object
- #select(db = writable_db, table, values, model) ⇒ Object
-
#select_all(db = writable_db, table, model) ⇒ Object
retrieve.
-
#update(db = writable_db, table, values, where_values) ⇒ Object
update.
- #writable_db ⇒ Object
Class Method Details
Instance Method Details
#coerces(v) ⇒ Object
transform values
80 81 82 83 84 85 86 87 |
# File 'lib/swiss_db/data_store.rb', line 80 def coerces(v) if v.is_a?(Time) formatter = Java::Text::SimpleDateFormat.new('yyyy-MM-dd hh:mm:ss.SSS') formatter.format(v).to_s else v.to_s end end |
#destroy_all(db = writable_db, table) ⇒ Object
deleting all records
74 75 76 77 |
# File 'lib/swiss_db/data_store.rb', line 74 def destroy_all(db=writable_db, table) # WARNING! puts "destroying all from #{table}" db.delete(table, nil, nil) end |
#insert(db = writable_db, table, hash_values) ⇒ Object
insert
30 31 32 33 34 35 36 37 38 |
# File 'lib/swiss_db/data_store.rb', line 30 def insert(db=writable_db, table, hash_values) # puts "inserting data in #{table}" values = ContentValues.new(hash_values.count) hash_values.each do |k, v| values.put(k, coerces(v)) end result = db.insert(table, nil, values) result end |
#onCreate(db) ⇒ Object
create
25 26 27 |
# File 'lib/swiss_db/data_store.rb', line 25 def onCreate(db) SwissDB.create_tables_from_schema(db) end |
#onUpgrade(db, oldVersion, newVersion) ⇒ Object
17 18 19 20 21 22 |
# File 'lib/swiss_db/data_store.rb', line 17 def onUpgrade(db, oldVersion, newVersion) # TODO when migrations are implemented mp "Calling onUpgrade" mp "old version: #{oldVersion}" mp "new version: #{newVersion}" end |
#select(db = writable_db, table, values, model) ⇒ Object
47 48 49 50 51 52 53 54 55 56 |
# File 'lib/swiss_db/data_store.rb', line 47 def select(db=writable_db, table, values, model) puts "selecting data from #{table}" value_str = values.map do |k, v| "#{k} = '#{coerces(v)}'" end.join(" AND ") sql = "select * from '#{table}' where #{value_str}" puts sql cursor = db.rawQuery(sql, nil) Cursor.new(cursor, model) # we wrap their cursor end |
#select_all(db = writable_db, table, model) ⇒ Object
retrieve
40 41 42 43 44 45 |
# File 'lib/swiss_db/data_store.rb', line 40 def select_all(db=writable_db, table, model) sql = "select * from '#{table}'" puts sql cursor = db.rawQuery(sql, nil) Cursor.new(cursor, model) # we wrap their cursor end |
#update(db = writable_db, table, values, where_values) ⇒ Object
update
60 61 62 63 64 65 66 67 68 69 70 |
# File 'lib/swiss_db/data_store.rb', line 60 def update(db=writable_db, table, values, where_values) value_str = values.map do |k, v| "'#{k}' = '#{coerces(v)}'" end.join(",") where_str = where_values.map do |k, v| "#{k} = '#{coerces(v)}'" end.join(",") sql = "update '#{table}' set #{value_str} where #{where_str}" puts sql db.execSQL sql end |
#writable_db ⇒ Object
9 10 11 |
# File 'lib/swiss_db/data_store.rb', line 9 def writable_db getWritableDatabase end |