class SwissDB::DataStore

Constants

ContentValues

Public Class Methods

drop_db() click to toggle source
# File lib/swiss_db/data_store.rb, line 13
def self.drop_db
  SwissDB.context.deleteDatabase(SwissDB.db_name)
end

Public Instance Methods

coerces(v) click to toggle source

transform values

# 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) click to toggle source

deleting all records

# 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) click to toggle source

insert

# 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) click to toggle source

create

# File lib/swiss_db/data_store.rb, line 25
def onCreate(db)
  SwissDB.create_tables_from_schema(db)
end
onUpgrade(db, oldVersion, newVersion) click to toggle source
# 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) click to toggle source
# 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) click to toggle source

retrieve

# 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) click to toggle source

update

# 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() click to toggle source
# File lib/swiss_db/data_store.rb, line 9
def writable_db
  getWritableDatabase
end