Class: Connection

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

Constant Summary collapse

KEYS =
[:username, :password, :host, :database]

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(configs, after_connect_handler = nil) ⇒ Connection

Returns a new instance of Connection.



3
4
5
6
7
# File 'lib/connection.rb', line 3

def initialize(configs, after_connect_handler=nil)
  @configs = configs
  @after_connect_handler = after_connect_handler
  use :default_connection
end

Instance Attribute Details

#errorObject (readonly)

Returns the value of attribute error.



9
10
11
# File 'lib/connection.rb', line 9

def error
  @error
end

#nameObject (readonly)

Returns the value of attribute name.



9
10
11
# File 'lib/connection.rb', line 9

def name
  @name
end

#resultsObject (readonly)

Returns the value of attribute results.



9
10
11
# File 'lib/connection.rb', line 9

def results
  @results
end

Instance Method Details

#error?Boolean

Returns:

  • (Boolean)


11
12
13
# File 'lib/connection.rb', line 11

def error?
  !@error.nil?
end

#exec(sql) ⇒ Object



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/connection.rb', line 22

def exec(sql)
  begin
    result = @client.execute sql
    rows = result.each(:symbolize_keys => true, :empty_sets => true, :as => :array)
    @results = Hashie::Mash.new({
                                  :columns => result.fields, 
                                  :rows => rows, 
                                  :affected => result.do, 
                                  :return_code => @client.return_code
                                })
    @error = nil
    @results
  rescue TinyTds::Error => e
    @result = nil
    @error = Hashie::Mash.new(:error => e.to_s, :severity => e.severity, :db_error_number => e.db_error_number)
  end
end

#one_column_one_row?Boolean

Returns:

  • (Boolean)


15
16
17
18
19
20
# File 'lib/connection.rb', line 15

def one_column_one_row?
  return false if error?
  @results.rows.size == 1 && @results.columns.size == 1
rescue
  false
end

#use(name = :default_connection) ⇒ Object



42
43
44
45
46
# File 'lib/connection.rb', line 42

def use(name = :default_connection)
  return false unless @configs.has_key?(name.to_s)
  connect @configs[name.to_s]
  return true
end