Class: ZenginLite::Database Private

Inherits:
Object
  • Object
show all
Defined in:
lib/zengin_lite/database.rb

This class is part of a private API. You should avoid using this class if possible, as it may be removed or be changed in the future.

Class Method Summary collapse

Class Method Details

.closeObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



19
20
21
22
# File 'lib/zengin_lite/database.rb', line 19

def close
  @connection&.close
  @connection = nil
end

.connectionObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



7
8
9
10
11
12
13
14
15
16
17
# File 'lib/zengin_lite/database.rb', line 7

def connection
  @connection ||= begin
    unless File.exist?(ZenginLite::DB_PATH)
      raise "Database file not found: #{ZenginLite::DB_PATH}"
    end
    
    db = SQLite3::Database.new(ZenginLite::DB_PATH.to_s, readonly: true)
    db.results_as_hash = false
    db
  end
end

.each_bank(&block) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



52
53
54
55
56
# File 'lib/zengin_lite/database.rb', line 52

def each_bank(&block)
  connection.execute('SELECT code, name, kana, hira, roma FROM banks') do |row|
    block.call(row)
  end
end

.find_bank(code:) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Query methods



25
26
27
28
29
30
# File 'lib/zengin_lite/database.rb', line 25

def find_bank(code:)
  connection.get_first_row(
    'SELECT code, name, kana, hira, roma FROM banks WHERE code = ?',
    [code]
  )
end

.find_branch(bank_code:, branch_code:) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



58
59
60
61
62
63
# File 'lib/zengin_lite/database.rb', line 58

def find_branch(bank_code:, branch_code:)
  connection.get_first_row(
    'SELECT code, bank_code, name, kana, hira, roma FROM branches WHERE bank_code = ? AND code = ?',
    [bank_code, branch_code]
  )
end

.find_branches(bank_code:, limit: 100) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



65
66
67
68
69
70
71
# File 'lib/zengin_lite/database.rb', line 65

def find_branches(bank_code:, limit: 100)
  limit ||= -1
  connection.execute(
    'SELECT code, bank_code, name, kana, hira, roma FROM branches WHERE bank_code = ? LIMIT ?',
    [bank_code, limit]
  )
end

.metadata(key) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



73
74
75
76
77
78
79
# File 'lib/zengin_lite/database.rb', line 73

def (key)
  row = connection.get_first_row(
    'SELECT value FROM metadata WHERE key = ?',
    [key]
  )
  row&.first
end

.search_banks(name: nil, kana: nil, limit: 10) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/zengin_lite/database.rb', line 32

def search_banks(name: nil, kana: nil, limit: 10)
  sql = 'SELECT code, name, kana, hira, roma FROM banks WHERE 1=1'
  params = []
  
  if name
    sql += ' AND name LIKE ?'
    params << "%#{name}%"
  end
  
  if kana
    sql += ' AND kana LIKE ?'
    params << "%#{kana}%"
  end
  
  sql += ' LIMIT ?'
  params << limit
  
  connection.execute(sql, params)
end

.statsObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



81
82
83
84
85
86
87
88
# File 'lib/zengin_lite/database.rb', line 81

def stats
  {
    banks_count: connection.get_first_value('SELECT COUNT(*) FROM banks'),
    branches_count: connection.get_first_value('SELECT COUNT(*) FROM branches'),
    updated_at: ('updated_at'),
    md5: ('md5')
  }
end