Class: SCAnalytics::Connections::Oracle

Inherits:
Connection
  • Object
show all
Defined in:
lib/sc_analytics/connections/oracle.rb

Instance Attribute Summary collapse

Attributes inherited from Connection

#host, #port, #username

Instance Method Summary collapse

Constructor Details

#initialize(connection_name, config) ⇒ Oracle

Returns a new instance of Oracle.



10
11
12
13
14
15
16
17
18
19
20
# File 'lib/sc_analytics/connections/oracle.rb', line 10

def initialize(connection_name, config)
  super

  @host = config[:connection][:description][:address][:host]
  @port = config[:connection][:description][:address][:port]
  @service_name = config[:connection][:description][:connect_data][:service_name]
  @sid = config[:connection][:description][:connect_data][:sid]

  @connect_string = generate_connect_string_for(config[:connection])
  connect
end

Instance Attribute Details

#connect_stringObject (readonly)

Returns the value of attribute connect_string.



4
5
6
# File 'lib/sc_analytics/connections/oracle.rb', line 4

def connect_string
  @connect_string
end

#service_nameObject (readonly)

Returns the value of attribute service_name.



4
5
6
# File 'lib/sc_analytics/connections/oracle.rb', line 4

def service_name
  @service_name
end

#sidObject (readonly)

Returns the value of attribute sid.



4
5
6
# File 'lib/sc_analytics/connections/oracle.rb', line 4

def sid
  @sid
end

Instance Method Details

#connectObject



48
49
50
51
52
# File 'lib/sc_analytics/connections/oracle.rb', line 48

def connect
  super do
    @client = OCI8.new @username, @password, @connect_string
  end
end

#define_col_types_for(query, cursor) ⇒ Object



22
23
24
25
26
27
28
# File 'lib/sc_analytics/connections/oracle.rb', line 22

def define_col_types_for(query, cursor)
  (query.columns_to_cast || {}).each_pair do |type, cols|
    cols.each do |col|
      cursor.define(col, type)
    end
  end
end

#generate_connect_string_for(conn_hsh, conn_str = "") ⇒ Object



54
55
56
57
58
59
60
61
62
# File 'lib/sc_analytics/connections/oracle.rb', line 54

def generate_connect_string_for(conn_hsh, conn_str = "")
  conn_hsh.each_pair do |descriptor,detail|
    conn_str << "("
    conn_str << "#{descriptor.to_s}="
    detail.is_a?(Hash) ? conn_str = generate_connect_string_for(detail, conn_str) : conn_str << detail.to_s
    conn_str << ")"
  end
  conn_str
end

#get_cursor_for(query) ⇒ Object



30
31
32
# File 'lib/sc_analytics/connections/oracle.rb', line 30

def get_cursor_for(query)
  cursor = @client.parse(query.sql)
end

#run(query, cursor) ⇒ Object



34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/sc_analytics/connections/oracle.rb', line 34

def run(query, cursor)
  cursor.exec
  define_col_types_for query, cursor

  headers = cursor.get_col_names
  results = []
  
  while row = cursor.fetch
    results << row
  end

  Struct::QueryResult.new(headers, results)
end