Class: Cure::DatabaseService

Inherits:
Object
  • Object
show all
Includes:
Configuration
Defined in:
lib/cure/database.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Configuration

#config, #create_config, #register_config

Constructor Details

#initializeDatabaseService

Returns a new instance of DatabaseService.



41
42
43
44
# File 'lib/cure/database.rb', line 41

def initialize
  @database = init_database
  setup_db
end

Instance Attribute Details

#databaseSequel::SQLite::Database (readonly)

Returns:

  • (Sequel::SQLite::Database)


39
40
41
# File 'lib/cure/database.rb', line 39

def database
  @database
end

Instance Method Details

#add_column(tbl_name, new_column, default: "") ⇒ Object



118
119
120
121
122
123
# File 'lib/cure/database.rb', line 118

def add_column(tbl_name, new_column, default: "")
  tbl_name = tbl_name.to_sym if tbl_name.class != Symbol
  new_column = new_column.to_sym if new_column.class != Symbol

  @database.add_column(tbl_name, new_column, String, default: default)
end

#all_translationsObject



66
67
68
# File 'lib/cure/database.rb', line 66

def all_translations
  @database.from(:translations).all
end

#copy_column(tbl_name, from_column, to_column) ⇒ Object



144
145
146
147
148
149
150
151
# File 'lib/cure/database.rb', line 144

def copy_column(tbl_name, from_column, to_column)
  tbl_name = tbl_name.to_sym if tbl_name.class != Symbol
  from_column = from_column.to_sym if from_column.class != Symbol
  to_column = to_column.to_sym if to_column.class != Symbol

  add_column tbl_name, to_column
  run("UPDATE #{tbl_name} SET #{to_column} = #{from_column}")
end

#create_table(tbl_name, columns, auto_increment: true) ⇒ Object

Parameters:

  • tbl_name (Symbol, String)
  • columns (Array)


72
73
74
75
76
77
78
79
80
81
# File 'lib/cure/database.rb', line 72

def create_table(tbl_name, columns, auto_increment: true)
  tbl_name = tbl_name.to_sym if tbl_name.class != Symbol

  @database.create_table tbl_name do
    primary_key :_id, auto_increment: auto_increment
    columns.each do |col_name|
      column col_name.to_sym, String
    end
  end
end

#find_translation(source_value) ⇒ Object



62
63
64
# File 'lib/cure/database.rb', line 62

def find_translation(source_value)
  @database.from(:translations).where(source_value: source_value).get(:value)
end

#find_variable(property_name) ⇒ Object

App Service calls



58
59
60
# File 'lib/cure/database.rb', line 58

def find_variable(property_name)
  @database.from(:variables).where(name: property_name).get(:value)
end

#insert_batched_rows(tbl_name, rows) ⇒ Object

Parameters:



114
115
116
# File 'lib/cure/database.rb', line 114

def insert_batched_rows(tbl_name, rows)
  @database[tbl_name.to_sym].import(@database[tbl_name.to_sym].columns, rows)
end

#insert_row(tbl_name, row) ⇒ Object

Parameters:



108
109
110
# File 'lib/cure/database.rb', line 108

def insert_row(tbl_name, row)
  @database[tbl_name.to_sym].insert(row)
end

#list_columns(tbl_name) ⇒ Object



132
133
134
# File 'lib/cure/database.rb', line 132

def list_columns(tbl_name)
  @database[tbl_name.to_sym].columns
end

#list_tablesObject



171
172
173
174
175
176
177
178
179
180
181
# File 'lib/cure/database.rb', line 171

def list_tables
  tbl_arr = @database.tables
  tbl_arr.delete(:variables)

  if tbl_arr.include?(:translations)
    tbl_arr.delete(:translations)
    tbl_arr.push(:translations)
  end

  tbl_arr
end

#remove_column(tbl_name, remove_column) ⇒ Object



125
126
127
128
129
130
# File 'lib/cure/database.rb', line 125

def remove_column(tbl_name, remove_column)
  tbl_name = tbl_name.to_sym if tbl_name.class != Symbol
  remove_column = remove_column.to_sym if remove_column.class != Symbol

  @database.drop_column tbl_name, remove_column
end

#rename_column(tbl_name, old_column, new_column) ⇒ Object



136
137
138
139
140
141
142
# File 'lib/cure/database.rb', line 136

def rename_column(tbl_name, old_column, new_column)
  tbl_name = tbl_name.to_sym if tbl_name.class != Symbol
  old_column = old_column.to_sym if old_column.class != Symbol
  new_column = new_column.to_sym if new_column.class != Symbol

  @database.rename_column tbl_name, old_column, new_column
end

#run(query, opts = {}) ⇒ Object



153
154
155
# File 'lib/cure/database.rb', line 153

def run(query, opts={})
  @database.run(query, opts)
end

#setup_dbObject



46
47
48
49
50
51
52
53
54
55
# File 'lib/cure/database.rb', line 46

def setup_db
  # Load this from config defined by user?
  @database.execute <<-SQL
    PRAGMA journal_mode = OFF;
    PRAGMA synchronous = 0;
    PRAGMA cache_size = 1000000;
    PRAGMA locking_mode = EXCLUSIVE;
    PRAGMA temp_store = MEMORY;
  SQL
end

#table_count(tbl_name) ⇒ Object

Parameters:



89
90
91
# File 'lib/cure/database.rb', line 89

def table_count(tbl_name)
  @database[tbl_name.to_sym].count
end

#table_exist?(tbl_name) ⇒ TrueClass, FalseClass Also known as: table_exists?

Parameters:

Returns:

  • (TrueClass, FalseClass)


99
100
101
102
103
# File 'lib/cure/database.rb', line 99

def table_exist?(tbl_name)
  tbl_name = tbl_name.to_sym if tbl_name.class != Symbol

  @database.table_exists?(tbl_name)
end

#truncate_table(tbl_name) ⇒ Object

Parameters:



84
85
86
# File 'lib/cure/database.rb', line 84

def truncate_table(tbl_name)
  @database[tbl_name.to_sym].truncate
end

#with_paged_result(tbl_name, chunk_size: 100, &block) ⇒ Object

Can we decouple query from named range? Probably more difficult than it seems. But would be nice to create two queries that doesn’t require two tables (named ranges).



160
161
162
163
164
165
166
167
168
169
# File 'lib/cure/database.rb', line 160

def with_paged_result(tbl_name, chunk_size: 100, &block)
  raise "No block given" unless block

  query = config.template.queries.find(tbl_name)
  if query
    @database[query.query].order(:_id).paged_each(rows_per_fetch: chunk_size, &block)
  else
    @database[tbl_name.to_sym].order(:_id).paged_each(rows_per_fetch: chunk_size, &block)
  end
end

#with_transaction(&block) ⇒ Object



93
94
95
# File 'lib/cure/database.rb', line 93

def with_transaction(&block)
  @database.transaction({}, &block)
end