Class: DataStore

Inherits:
Android::Database::SQLite::SQLiteOpenHelper
  • Object
show all
Defined in:
lib/swiss_db/data_store.rb

Overview

main connection point creates and upgrades our database for us and provides low level SQL features

Constant Summary collapse

DATABASE_NAME =
"swissdb"
DATABASE_VERSION =
1
ContentValues =
Android::Content::ContentValues

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.contextObject



19
20
21
# File 'lib/swiss_db/data_store.rb', line 19

def self.context
  @@context
end

.context=(context) ⇒ Object



15
16
17
# File 'lib/swiss_db/data_store.rb', line 15

def self.context=(context)
  @@context = context
end

.current_schema=(schema) ⇒ Object



11
12
13
# File 'lib/swiss_db/data_store.rb', line 11

def self.current_schema=(schema)
  @@current_schema = schema
end

.drop_dbObject



27
28
29
# File 'lib/swiss_db/data_store.rb', line 27

def self.drop_db
  @@context.deleteDatabase(DATABASE_NAME)
end

Instance Method Details

#create_table(db = writable_db, table_name, fields) ⇒ Object

create table



101
102
103
104
105
106
# File 'lib/swiss_db/data_store.rb', line 101

def create_table(db=writable_db, table_name, fields)
  fields_string = fields.map { |k, v| "#{k} #{v}" }.join(',')
  sql = "CREATE TABLE #{table_name}(#{fields_string})"
  puts sql
  db.execSQL sql
end

#destroy_all(db = writable_db, table) ⇒ Object

deleting all records



95
96
97
98
# File 'lib/swiss_db/data_store.rb', line 95

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



51
52
53
54
55
56
57
58
59
# File 'lib/swiss_db/data_store.rb', line 51

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, v)
  end
  result = db.insert(table, nil, values)
  result
end

#onCreate(db) ⇒ Object

create



38
39
40
41
42
43
44
45
46
47
48
# File 'lib/swiss_db/data_store.rb', line 38

def onCreate(db)
  # puts "table creation... running schema"
  # THIS RELIES ON SCHEMA CODE TO SUCCEED
  # NOTE: I don't know a better way of passing the schema here
  # If you do just change it. For now this works.
  # Thanks.
  @@current_schema.each do |k, v|
    create_table db, k, v
  end
  # database.execSQL("CREATE TABLE credentials(username TEXT, password TEXT)")
end

#onUpgrade(db, oldVersion, newVersion) ⇒ Object



31
32
33
34
35
# File 'lib/swiss_db/data_store.rb', line 31

def onUpgrade(db, oldVersion, newVersion)
  # maybe drop if needed...
  db.execSQL("DROP *")
  onCreate(db)
end

#select(db = writable_db, table, values, model) ⇒ Object



68
69
70
71
72
73
74
75
76
77
# File 'lib/swiss_db/data_store.rb', line 68

def select(db=writable_db, table, values, model)
  puts "selecting data from #{table}"
  value_str = values.map do |k, v|
    "#{k} = '#{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



61
62
63
64
65
66
# File 'lib/swiss_db/data_store.rb', line 61

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



81
82
83
84
85
86
87
88
89
90
91
# File 'lib/swiss_db/data_store.rb', line 81

def update(db=writable_db, table, values, where_values)
  value_str = values.map do |k, v|
    "'#{k}' = '#{v}'"
  end.join(",")
  where_str = where_values.map do |k, v|
    "#{k} = '#{v}'"
  end.join(",")
  sql = "update '#{table}' set #{value_str} where #{where_str}"
  puts sql
  db.execSQL sql
end

#writable_dbObject



23
24
25
# File 'lib/swiss_db/data_store.rb', line 23

def writable_db
  getWritableDatabase
end