Class: Db2Query::DbClient

Inherits:
Object
  • Object
show all
Includes:
ActiveModel::Type::Helpers::Timezone
Defined in:
lib/db2_query/db_client.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(config) ⇒ DbClient

Returns a new instance of DbClient.



11
12
13
14
15
16
# File 'lib/db2_query/db_client.rb', line 11

def initialize(config)
  @dsn = config[:dsn]
  @idle_time_limit = config[:idle] || 5
  @client = new_client
  @last_transaction = Time.now
end

Instance Attribute Details

#dsnObject (readonly)

Returns the value of attribute dsn.



5
6
7
# File 'lib/db2_query/db_client.rb', line 5

def dsn
  @dsn
end

Instance Method Details

#active?Boolean

Returns:

  • (Boolean)


22
23
24
# File 'lib/db2_query/db_client.rb', line 22

def active?
  @client.connected?
end

#clientObject



50
51
52
53
54
# File 'lib/db2_query/db_client.rb', line 50

def client
  reconnect! unless connected_and_persist?
  @last_transaction = Time.now
  @client
end

#connected_and_persist?Boolean

Returns:

  • (Boolean)


26
27
28
# File 'lib/db2_query/db_client.rb', line 26

def connected_and_persist?
  active? && !expire?
end

#disconnect!Object



30
31
32
33
34
# File 'lib/db2_query/db_client.rb', line 30

def disconnect!
  @client.drop_all
  @client.disconnect if active?
  @client = nil
end

#expire?Boolean

Returns:

  • (Boolean)


18
19
20
# File 'lib/db2_query/db_client.rb', line 18

def expire?
  Time.now - @last_transaction > 60 * @idle_time_limit
end

#new_clientObject



36
37
38
39
40
41
42
43
# File 'lib/db2_query/db_client.rb', line 36

def new_client
  ODBC.connect(dsn).tap do |odbc_conn|
    odbc_conn.use_time = true
    odbc_conn.use_utc = is_utc?
  end
rescue ::ODBC::Error => e
  raise Db2Query::ConnectionError.new(e.message)
end

#reconnect!Object



45
46
47
48
# File 'lib/db2_query/db_client.rb', line 45

def reconnect!
  disconnect!
  @client = new_client
end