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",
}.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
).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")
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.



32
33
34
35
36
37
38
39
40
41
# File 'lib/feidee_utils/database.rb', line 32

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

  super(@sqlite_file.path)

  
  drop_unused_tables if strip

  @namespaced = Record.generate_namespaced_record_classes(self)
end

Instance Attribute Details

#missing_tablesObject (readonly)

Returns the value of attribute missing_tables.



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

def missing_tables
  @missing_tables
end

#namespacedObject (readonly)

Returns the value of attribute namespaced.



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

def namespaced
  @namespaced
end

#platformObject (readonly)

Returns the value of attribute platform.



28
29
30
# File 'lib/feidee_utils/database.rb', line 28

def platform
  @platform
end

#sqlite_fileObject (readonly)

Returns the value of attribute sqlite_file.



27
28
29
# File 'lib/feidee_utils/database.rb', line 27

def sqlite_file
  @sqlite_file
end

#sqlite_nameObject (readonly)

Returns the value of attribute sqlite_name.



28
29
30
# File 'lib/feidee_utils/database.rb', line 28

def sqlite_name
  @sqlite_name
end

#sqlite_timestampObject (readonly)

Returns the value of attribute sqlite_timestamp.



28
29
30
# File 'lib/feidee_utils/database.rb', line 28

def sqlite_timestamp
  @sqlite_timestamp
end

Class Method Details

.feidee_to_sqlite(private_sqlite, sqlite_file = nil) ⇒ Object



107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
# File 'lib/feidee_utils/database.rb', line 107

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

  unless [FeideeHeader_iOS, FeideeHeader_Android, Header].include? private_header
    raise "Unexpected header #{private_header.inspect} in private sqlite file."
  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



99
100
101
# File 'lib/feidee_utils/database.rb', line 99

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

.trash_table_name(name) ⇒ Object



128
129
130
131
132
133
134
135
136
# File 'lib/feidee_utils/database.rb', line 128

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



43
44
45
46
47
48
49
50
51
# File 'lib/feidee_utils/database.rb', line 43

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_integrity_globallyObject



53
54
55
56
57
# File 'lib/feidee_utils/database.rb', line 53

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