Class: FeideeUtils::Database

Inherits:
SQLite3::Database
  • Object
show all
Defined in:
lib/feidee_utils/database.rb

Constant Summary collapse

Tables =
{
  accounts: "t_account",
  account_groups: "t_account_group",
  categories: "t_category",
  transactions: "t_transaction",

  metadata: "t_metadata",
  profile: "t_profile",
  id_seed: "t_id_seed",
}.freeze
PotentialUsefulTables =
%w(
  t_account_book
  t_account_info
  t_budget_item
  t_fund_holding
  t_fund_trans
  t_module_stock_holding
  t_module_stock_tran
  t_tradingEntity
  t_user
).freeze
Header =
"SQLite format 3\0".force_encoding("binary")
FeideeHeader_iOS =
"%$^#&!@_@- -!F\xff\0".force_encoding('binary')
FeideeHeader_Android =
("\0" * 13 + "F\xff\0").force_encoding("binary")
AllHeaders =
[FeideeHeader_iOS, FeideeHeader_Android, Header]
NoDeleteSuffixTables =
%w(
  account category tag tradingEntity
  transaction transaction_template
)

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(private_sqlite, strip = false) ⇒ Database

Returns a new instance of Database.



35
36
37
38
39
40
41
42
43
44
45
# File 'lib/feidee_utils/database.rb', line 35

def initialize(private_sqlite, strip = false)
  @sqlite_file = Database.feidee_to_sqlite(private_sqlite)

  super(@sqlite_file.path)

  
  drop_unused_tables if strip

  # TODO: make Ledger a first class object.
  @ledger = Record.generate_subclasses(self)
end

Instance Attribute Details

#last_modified_atObject (readonly)

Returns the value of attribute last_modified_at.



31
32
33
# File 'lib/feidee_utils/database.rb', line 31

def last_modified_at
  @last_modified_at
end

#ledgerObject (readonly)

Returns the value of attribute ledger.



33
34
35
# File 'lib/feidee_utils/database.rb', line 33

def ledger
  @ledger
end

#ledger_nameObject (readonly)

Returns the value of attribute ledger_name.



31
32
33
# File 'lib/feidee_utils/database.rb', line 31

def ledger_name
  @ledger_name
end

#missing_tablesObject (readonly)

Returns the value of attribute missing_tables.



32
33
34
# File 'lib/feidee_utils/database.rb', line 32

def missing_tables
  @missing_tables
end

#platformObject (readonly)

Returns the value of attribute platform.



31
32
33
# File 'lib/feidee_utils/database.rb', line 31

def platform
  @platform
end

#sqlite_fileObject (readonly)

Returns the value of attribute sqlite_file.



30
31
32
# File 'lib/feidee_utils/database.rb', line 30

def sqlite_file
  @sqlite_file
end

Class Method Details

.feidee_to_sqlite(private_sqlite, sqlite_file = nil) ⇒ Object



121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
# File 'lib/feidee_utils/database.rb', line 121

def feidee_to_sqlite(private_sqlite, sqlite_file = nil)
  # Discard the first a few bytes content.
  private_header = private_sqlite.read(Header.length)

  unless AllHeaders.include? private_header
    raise "Unexpected private sqlite header #{private_header.inspect}."
  end

  # Write the rest to a tempfile.
  sqlite_file ||= Tempfile.new("kingdee_sqlite", binmode: true)
  sqlite_file.binmode
  sqlite_file.write(Header)
  sqlite_file.write(private_sqlite.read)
  sqlite_file.fsync
  sqlite_file
end

.open_file(file_name) ⇒ Object



112
113
114
# File 'lib/feidee_utils/database.rb', line 112

def open_file(file_name)
  Database.new(File.open(file_name))
end

.sqlite_to_feidee(sqlite, feidee_file = nil, platform = :iOS) ⇒ Object



138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
# File 'lib/feidee_utils/database.rb', line 138

def sqlite_to_feidee(sqlite, feidee_file = nil, platform = :iOS)
  sqlite.read(Header.length)
  private_header = if platform == :iOS
                     FeideeHeader_iOS
                   else
                     FeideeHeader_Android
                   end

  feidee_file ||= Tempfile.new("feidee_sqlite", binmode: true)
  feidee_file.binmode
  feidee_file.write(private_header)
  feidee_file.write(sqlite.read)
  feidee_file.fsync
  feidee_file
end

.trash_table_name(name) ⇒ Object



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

def trash_table_name name
  NoDeleteSuffixTables.each do |core_name|
    if name == "t_" + core_name then
      return "t_" + "deleted_" + core_name;
    end
  end

  name + "_delete"
end

Instance Method Details

#sqlite_backup(dest_file_path) ⇒ Object



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

def sqlite_backup(dest_file_path)
  self.execute("vacuum;")

  backup_sqlite_db = SQLite3::Database.new(dest_file_path.to_s)
  backup_obj = SQLite3::Backup.new(backup_sqlite_db, "main", self, "main")
  backup_obj.step(-1)
  backup_obj.finish
  backup_sqlite_db.close
end

#validate_global_integrityObject



57
58
59
60
61
# File 'lib/feidee_utils/database.rb', line 57

def validate_global_integrity
  @ledger.constants.each do |const|
    @ledger.const_get(const).validate_global_integrity if const != :Database
  end
end