Class: FeideeUtils::Database
- Inherits:
-
SQLite3::Database
- Object
- SQLite3::Database
- FeideeUtils::Database
- 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 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
-
#last_modified_at ⇒ Object
readonly
Returns the value of attribute last_modified_at.
-
#ledger ⇒ Object
readonly
Returns the value of attribute ledger.
-
#ledger_name ⇒ Object
readonly
Returns the value of attribute ledger_name.
-
#missing_tables ⇒ Object
readonly
Returns the value of attribute missing_tables.
-
#platform ⇒ Object
readonly
Returns the value of attribute platform.
-
#sqlite_file ⇒ Object
readonly
Returns the value of attribute sqlite_file.
Class Method Summary collapse
- .feidee_to_sqlite(private_sqlite, sqlite_file = nil) ⇒ Object
- .open_file(file_name) ⇒ Object
- .trash_table_name(name) ⇒ Object
Instance Method Summary collapse
-
#initialize(private_sqlite, strip = false) ⇒ Database
constructor
A new instance of Database.
- #sqlite_backup(dest_file_path) ⇒ Object
- #validate_global_integrity ⇒ Object
Constructor Details
#initialize(private_sqlite, strip = false) ⇒ Database
Returns a new instance of Database.
34 35 36 37 38 39 40 41 42 43 44 |
# File 'lib/feidee_utils/database.rb', line 34 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_namespaced_record_classes(self) end |
Instance Attribute Details
#last_modified_at ⇒ Object (readonly)
Returns the value of attribute last_modified_at.
30 31 32 |
# File 'lib/feidee_utils/database.rb', line 30 def last_modified_at @last_modified_at end |
#ledger ⇒ Object (readonly)
Returns the value of attribute ledger.
32 33 34 |
# File 'lib/feidee_utils/database.rb', line 32 def ledger @ledger end |
#ledger_name ⇒ Object (readonly)
Returns the value of attribute ledger_name.
30 31 32 |
# File 'lib/feidee_utils/database.rb', line 30 def ledger_name @ledger_name end |
#missing_tables ⇒ Object (readonly)
Returns the value of attribute missing_tables.
31 32 33 |
# File 'lib/feidee_utils/database.rb', line 31 def missing_tables @missing_tables end |
#platform ⇒ Object (readonly)
Returns the value of attribute platform.
30 31 32 |
# File 'lib/feidee_utils/database.rb', line 30 def platform @platform end |
#sqlite_file ⇒ Object (readonly)
Returns the value of attribute sqlite_file.
29 30 31 |
# File 'lib/feidee_utils/database.rb', line 29 def sqlite_file @sqlite_file end |
Class Method Details
.feidee_to_sqlite(private_sqlite, sqlite_file = nil) ⇒ Object
120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 |
# File 'lib/feidee_utils/database.rb', line 120 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
111 112 113 |
# File 'lib/feidee_utils/database.rb', line 111 def open_file(file_name) Database.new(File.open(file_name)) end |
.trash_table_name(name) ⇒ Object
144 145 146 147 148 149 150 151 152 |
# File 'lib/feidee_utils/database.rb', line 144 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
46 47 48 49 50 51 52 53 54 |
# File 'lib/feidee_utils/database.rb', line 46 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_integrity ⇒ Object
56 57 58 59 60 |
# File 'lib/feidee_utils/database.rb', line 56 def validate_global_integrity @ledger.constants.each do |const| @ledger.const_get(const).validate_global_integrity if const != :Database end end |