Class: SQLite

Inherits:
StorageType show all
Defined in:
lib/qooxview/storages/sqlite.rb

Instance Attribute Summary collapse

Attributes inherited from StorageType

#config, #data_cache, #name

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from StorageType

#add_field, #data_double, #data_each, data_load, data_save, #extract_data_old, #field_args, has?, #has_field, inherited, #initialize, #method_missing, new_st

Constructor Details

This class inherits a constructor from StorageType

Dynamic Method Handling

This class handles dynamic methods through the method_missing method in the class StorageType

Instance Attribute Details

#db_classObject (readonly)

Returns the value of attribute db_class.



14
15
16
# File 'lib/qooxview/storages/sqlite.rb', line 14

def db_class
  @db_class
end

Class Method Details

.dbs_close_allObject



199
200
201
202
203
204
# File 'lib/qooxview/storages/sqlite.rb', line 199

def self.dbs_close_all
  dputs(2) { 'Closing all dbs' }
  SQLite.with_all_sqlites { |sql|
    sql.close_db
  }
end

.dbs_open_loadObject



206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
# File 'lib/qooxview/storages/sqlite.rb', line 206

def self.dbs_open_load
  dputs(2) { 'Opening all dbs' }
  SQLite.with_all_sqlites { |sql|
    sql.open_db
  }
  dputs(2) { 'Loading all dbs' }
  RPCQooxdooService.entities { |e|
    e.is_loaded = false
  }
  RPCQooxdooService.entities { |e|
    unless e.is_loaded
      e.loading = true
      e.load
      e.loading = false
      u = Users.match_by_name('local')
      dputs(2){"User is #{u}"}
    end
  }
end

.dbs_open_load_migrateObject



226
227
228
229
230
# File 'lib/qooxview/storages/sqlite.rb', line 226

def self.dbs_open_load_migrate
  SQLite.dbs_open_load
  dputs(2) { 'Migrating all dbs' }
  RPCQooxdooService.migrate_all
end

.with_all_sqlitesObject



187
188
189
190
191
192
193
194
195
196
197
# File 'lib/qooxview/storages/sqlite.rb', line 187

def self.with_all_sqlites
  RPCQooxdooService.entities { |e|
    e.storage.each { |storage_name, storage_class|
      dputs(3) { "Found storage_name of #{storage_name.inspect}" }
      if storage_name.to_s =~ /^SQLite/
        dputs(3) { "Found #{e.name}::#{storage_name} as SQLite-something" }
        yield storage_class
      end
    }
  }
end

Instance Method Details

#close_dbObject

Allows for debugging when wanting to load another db



39
40
41
42
# File 'lib/qooxview/storages/sqlite.rb', line 39

def close_db
  dputs(4) { "Closing db #{@name_file}" }
  ActiveRecord::Base.remove_connection
end

#configure(config, name_base = 'stsql', name_file = 'sql.db') ⇒ Object



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/qooxview/storages/sqlite.rb', line 16

def configure(config, name_base = 'stsql', name_file = 'sql.db')
  dputs(2) { "Configuring SQLite with #{@name}" }
  @db_table = "#{name_base}_#{@name.downcase}s"
  @db_class_name = "#{name_base.capitalize}_#{@name.downcase}"
  @db_class = nil
  @sqlite_dir = File.join($data_dir, get_config('data',
                                                :StorageType, :data_dir))
  FileUtils.mkdir_p(@sqlite_dir)
  @name_file = name_file
  @db_file = File.join(@sqlite_dir, @name_file)
  # ActiveRecord::Base.logger = Logger.new('debug.log')
  ActiveRecord::Migration.verbose = false
  # ActiveRecord::Base.logger = Logger.new(STDERR)

  @mutex_es = Mutex.new

  dputs(4) { 'Opening database' }
  open_db

  super config
end

#data_create(data) ⇒ Object

Each new entry is directly stored, helping somewhat if the program or the computer crashes



102
103
104
105
106
107
108
109
110
111
112
113
# File 'lib/qooxview/storages/sqlite.rb', line 102

def data_create(data)
  # dputs_func
  dputs(5) { "Creating early data #{data.inspect} with #{data.class}" }
  dputs(5) { "hello for #{data.inspect}" }
  dputs(5) { "db_class is #{@db_class.inspect}" }
  e = @db_class.create(data)
  dputs(5) { 'hello - 2' }
  new_id = e.attributes[@data_field_id.to_s]
  dputs(5) { "New id is #{new_id}" }
  data[@data_field_id] = new_id
  dputs(5) { "Creating data: #{e.inspect}" }
end

#delete(id) ⇒ Object



176
177
178
179
180
181
182
183
184
185
# File 'lib/qooxview/storages/sqlite.rb', line 176

def delete(id)
  entry = @db_class.find_by(id: id)
  if entry != nil
    entry.destroy
    @entries[id].delete
    if e = @entries_save[id]
      e.delete
    end
  end
end

#delete_all(local_only = false) ⇒ Object



163
164
165
166
167
168
169
170
171
172
173
174
# File 'lib/qooxview/storages/sqlite.rb', line 163

def delete_all(local_only = false)
  if !local_only
    db_table = @db_table
    dputs(2) { "Deleting table #{db_table}" }
    ActiveRecord::Schema.define do
      if table_exists? db_table
        drop_table db_table.to_sym
      end
    end
    init_table
  end
end

#init_tableObject



115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
# File 'lib/qooxview/storages/sqlite.rb', line 115

def init_table
  # dputs_func
  db_table, fields = @db_table, @fields
  dputs(3) { "Initializing #{@db_class_name} with db_table=#{db_table.inspect}" }
  ActiveRecord::Schema.define do
    new_table = false
    if !table_exists? db_table
      dputs(2) { "Creating table #{db_table}" }
      create_table db_table
      new_table = true
    end
    dputs(3) { "Fields is #{fields.inspect}" }
    fields.each_key { |f|
      dputs(3) { "Checking for field #{f} in table #{db_table}" }
      if not columns(db_table).index { |c|
        c.name.to_s == f.to_s }
        dputs(new_table ? 4 : 1) { "Adding column #{f} of type " +
            "#{fields[f][:dtype]} to table #{db_table}" }
        case fields[f][:dtype]
          when /int/, /entity/
            add_column(db_table, f, :integer)
          when /bool/
            add_column(db_table, f, :boolean)
          when /float/
            add_column(db_table, f, :float)
          when /date/
            add_column(db_table, f, :date)
          else
            add_column(db_table, f, :string)
        end
      end
    }
  end
  #    ActiveRecord::Base.logger = nil
end

#loadObject

loads the data



152
153
154
155
156
157
158
159
160
161
# File 'lib/qooxview/storages/sqlite.rb', line 152

def load
  dputs(2) { "Loading data for #{@db_class_name}" }
  res = Hash[*@db_class.all.collect { |s|
               @entries[s[@data_field_id]] = s
               [s[@data_field_id].to_i, s.attributes.symbolize_keys]
             }.flatten(1)
  ]
  dputs(5) { "Result is: #{res.inspect}" }
  return res
end

#open_dbObject



44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/qooxview/storages/sqlite.rb', line 44

def open_db
  @mutex_es.synchronize {
    dputs(4) { "Opening connection to #{@name_file} - #{@sqlite_dir}" }
    ActiveRecord::Base.establish_connection(
        :adapter => 'sqlite3', :database => @db_file)

    dputs(4) { 'Initializing tables' }
    init_table

    dputs(4) { 'Getting Base' }
    eval("class #{@db_class_name} < ActiveRecord::Base; end")
    @db_class = eval(@db_class_name)
    dputs(4) { "db_class is #{db_class.inspect}" }

    @entries = {}
    @entries_save = {}
  }
end

#save(data, notmp: false) ⇒ Object

Saves the data stored, optionally takes an index to say which data needs to be saved



65
66
67
68
69
70
71
72
73
74
# File 'lib/qooxview/storages/sqlite.rb', line 65

def save(data, notmp: false)
  @mutex_es.synchronize {
    dputs(3) { "Saving #{@entries_save.count} entries in #{@db_class}" }
    @entries_save.each_value { |v|
      dputs(4){"Saving #{db_class} #{v.inspect} - #{v.id}"}
      v.save
    }
    @entries_save = {}
  }
end

#set_entry(data, field, value) ⇒ Object



76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
# File 'lib/qooxview/storages/sqlite.rb', line 76

def set_entry(data, field, value)
  @mutex_es.synchronize {
    # dp "#{db_class} #{data.inspect} #{field} #{value}"
    dputs(5) { "Searching id #{data.inspect}" }
    if @entries[data]
      @entries[data].save
    end
    dat = @db_class.find(data)
    # dat = @db_class.first(:conditions => {@data_field_id => data})
    # dp @entries[data]
    # dp dat
    # dp data
    @entries[data] ||= dat
    if entry = @entries[data]
      entry.send("#{field}=", value)
      @entries_save[data] = entry
      return value
    else
      dputs(2) { "Didn't find id #{data.inspect}" }
      return nil
    end
  }
end