Class: OdbcConnectionWrapper

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

Constant Summary collapse

@@odbc_config_path =
"odbc_config/connections.yml"

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeOdbcConnectionWrapper

Returns a new instance of OdbcConnectionWrapper.



15
16
17
18
19
20
21
# File 'lib/marjoree/odbc_connection_wrapper.rb', line 15

def initialize
    yaml = IO.read("#{OdbcConnectionWrapper.odbc_config_path}")
    @odbc_config = YAML.load(ERB.new(yaml).result(binding))
    @odbc_connections = {}
    @default_odbc_name = @odbc_config.size == 1 ? @odbc_config.keys.first : @odbc_config['default']['odbc_connection']
    connect_me_to( 'default' )
end

Class Method Details

.odbc_config_pathObject



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

def self.odbc_config_path
  @@odbc_config_path
end

.odbc_config_path=(path) ⇒ Object



7
8
9
# File 'lib/marjoree/odbc_connection_wrapper.rb', line 7

def self.odbc_config_path=(path)
  @@odbc_config_path = path      
end

Instance Method Details

#connect_me_to(odbc_name) ⇒ Object



23
24
25
26
27
28
29
30
31
32
# File 'lib/marjoree/odbc_connection_wrapper.rb', line 23

def connect_me_to( odbc_name )
    odbc_name = @default_odbc_name if odbc_name == 'default'
    @connection = @odbc_connections[odbc_name]
    unless @connection
      value = @odbc_config[odbc_name]
      @connection = ODBC.connect(odbc_name, value['username'], value['password'] )
      @odbc_connections[odbc_name]=@connection
    end
    $db = @connection
end

#disconnect_meObject



34
35
36
37
38
# File 'lib/marjoree/odbc_connection_wrapper.rb', line 34

def disconnect_me
    @odbc_connections.each do |odbc_name, connection|            
        connection.drop_all
    end
end

#execute(sql, output_params = []) ⇒ Object



40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/marjoree/odbc_connection_wrapper.rb', line 40

def execute(sql, output_params = [])
    begin
        dbCall = @connection.prepare(sql)
        begin
            dbCall.execute
            return ResultSet.new(dbCall, output_params)
        rescue
            raise ODBC::Error.new("Failed executing: #{sql}\nDue to: #{$!}")
        end
    ensure
        dbCall.drop
    end
end

#get_output_params(proc_name) ⇒ Object



64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/marjoree/odbc_connection_wrapper.rb', line 64

def get_output_params(proc_name)
    begin
        dbCall = @connection.procedure_columns proc_name
        result_set = ResultSet.new(dbCall)
        result = []
        result_set.collect do |hash|
            if hash[:COLUMN_TYPE] == 4   #varchar
                result << hash
            end
        end
        return result
    ensure
        dbCall.drop 
    end
end

#has_output_params?(proc_name) ⇒ Boolean

Returns:

  • (Boolean)


54
55
56
57
58
59
60
61
62
# File 'lib/marjoree/odbc_connection_wrapper.rb', line 54

def has_output_params?(proc_name)
    begin
        dbCall = @connection.procedure_columns proc_name
        result_set = ResultSet.new(dbCall)
        return result_set.contains?({ :COLUMN_TYPE => 4 })
    ensure
        dbCall.drop
    end
end