Class: Arxutils_Sqlite3::Dbutil::Dbconnect

Inherits:
Object
  • Object
show all
Defined in:
lib/arxutils_sqlite3/dbutil/dbconnect.rb

Overview

DB操作用ユーティリティクラス

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(dbconfig_dest_path, env, log_path) ⇒ Dbconnect

DB接続までの初期化に必要なディレクトリの確認、作成



33
34
35
36
37
38
39
40
# File 'lib/arxutils_sqlite3/dbutil/dbconnect.rb', line 33

def initialize(dbconfig_dest_path, env, log_path)
  # 接続開始時刻
  @connect_time = nil
  # DB格納ディレクトリ名
  @dbconfig_dest_path = dbconfig_dest_path
  @env = env
  @log_path = log_path
end

Class Method Details

.db_connect(config, dbconfig, env) ⇒ Object



18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/arxutils_sqlite3/dbutil/dbconnect.rb', line 18

def self.db_connect(config, dbconfig, env)
  # DB構成ファイルへのパス
  dbconfig_path = config.setup_for_dbconfig_path(dbconfig)
  # DB用ログファイルへのパス
  log_path = config.setup_for_db_log_path(dbconfig)
  # DB接続
  dbconnect = Arxutils_Sqlite3::Dbutil::Dbconnect.new(
    dbconfig_path,
    env,
    log_path
  )
  dbconnect.connect
end

.make_log_file_name(dbconfig, log_file_base_name) ⇒ Object



14
15
16
# File 'lib/arxutils_sqlite3/dbutil/dbconnect.rb', line 14

def self.make_log_file_name(dbconfig, log_file_base_name)
  format("%s-%s", dbconfig.to_s, log_file_base_name)
end

Instance Method Details

#connectObject

DB接続、DB用ログファイルの設定



43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/arxutils_sqlite3/dbutil/dbconnect.rb', line 43

def connect
  unless @connect_time
    begin
      # p "@dbconfig_dest_path=#{@dbconfig_dest_path}"
      dbconfig, _value = Ykxutils.yaml_load_file_compati(@dbconfig_dest_path)
      # p "dbconfig=#{dbconfig}"
      # p "@env=#{@env}"
      # x =  dbconfig[@env]
      # p x 
      ActiveRecord::Base.establish_connection(dbconfig[@env])
      ActiveRecord::Base.logger = Logger.new(@log_path)
      @connect_time = DateTime.now.new_offset
    rescue StandardError => e
      p e.message
    end
  end
  @connect_time
end