Class: Cubrid2::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/cubrid2/client.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(opts = {}) ⇒ Client

Returns a new instance of Client.

Raises:



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/cubrid2/client.rb', line 13

def initialize(opts = {})
  raise Cubrid2::Error, 'Options parameter must be a Hash' unless opts.is_a? Hash

  opts = Cubrid2::Util.key_hash_as_symbols(opts)
  @read_timeout = nil
  @query_options = self.class.default_query_options.dup
  @query_options.merge! opts

  %i[auto_commit].each do |key|
    next unless opts.key?(key)

    case key
    when :auto_commit
      send(:"#{key}=", !!opts[key]) # rubocop:disable Style/DoubleNegation
    else
      send(:"#{key}=", opts[key])
    end
  end

  flags = 0

  user     = opts[:username] || opts[:user]
  pass     = opts[:password] || opts[:pass]
  host     = opts[:host] || opts[:hostname]
  port     = opts[:port]
  database = opts[:database] || opts[:dbname] || opts[:db]

  # Correct the data types before passing these values down to the C level
  user = user.to_s unless user.nil?
  pass = pass.to_s unless pass.nil?
  host = host.to_s unless host.nil?
  port = port.to_i unless port.nil?
  database = database.to_s unless database.nil?

  @conn = Cubrid.connect database, host, port, user, pass
end

Instance Attribute Details

#connObject (readonly)

Returns the value of attribute conn.



5
6
7
# File 'lib/cubrid2/client.rb', line 5

def conn
  @conn
end

#query_optionsObject (readonly)

Returns the value of attribute query_options.



5
6
7
# File 'lib/cubrid2/client.rb', line 5

def query_options
  @query_options
end

#read_timeoutObject (readonly)

Returns the value of attribute read_timeout.



5
6
7
# File 'lib/cubrid2/client.rb', line 5

def read_timeout
  @read_timeout
end

Class Method Details

.default_query_optionsObject



7
8
9
10
11
# File 'lib/cubrid2/client.rb', line 7

def self.default_query_options
  @default_query_options ||= {
    auto_commit: true
  }
end

Instance Method Details

#_query(sql, _options) ⇒ Object



56
57
58
# File 'lib/cubrid2/client.rb', line 56

def _query(sql, _options)
  @conn.query(sql)
end

#auto_commitObject



85
86
87
# File 'lib/cubrid2/client.rb', line 85

def auto_commit
  @conn.auto_commit
end

#auto_commit=(flag) ⇒ Object



89
90
91
# File 'lib/cubrid2/client.rb', line 89

def auto_commit=(flag)
  @conn.auto_commit = flag
end

#closeObject



81
82
83
# File 'lib/cubrid2/client.rb', line 81

def close
  @conn.close
end

#infoObject



69
70
71
# File 'lib/cubrid2/client.rb', line 69

def info
  self.class.info
end

#pingObject



73
74
75
# File 'lib/cubrid2/client.rb', line 73

def ping
  @conn.server_version.present?
end

#query(sql, options = {}) ⇒ Object



50
51
52
53
54
# File 'lib/cubrid2/client.rb', line 50

def query(sql, options = {})
  Thread.handle_interrupt(::Cubrid2::Util::TIMEOUT_ERROR_CLASS => :never) do
    _query(sql, @query_options.merge(options))
  end
end

#query_infoObject



60
61
62
63
64
65
66
67
# File 'lib/cubrid2/client.rb', line 60

def query_info
  info = query_info_string
  return {} unless info

  info_hash = {}
  info.split.each_slice(2) { |s| info_hash[s[0].downcase.delete(':').to_sym] = s[1].to_i }
  info_hash
end

#server_versionObject



77
78
79
# File 'lib/cubrid2/client.rb', line 77

def server_version
  @conn.server_version
end