Class: Rigrate::Oracle

Inherits:
Driver
  • Object
show all
Defined in:
lib/rigrate/interface/oracle.rb

Instance Attribute Summary

Attributes inherited from Driver

#db

Instance Method Summary collapse

Methods inherited from Driver

#connect, #extract_conn_param, #extract_tbl_from_sql, #method_missing

Constructor Details

#initialize(uri) ⇒ Oracle

Returns a new instance of Oracle.



7
8
9
10
11
12
# File 'lib/rigrate/interface/oracle.rb', line 7

def initialize(uri)
  key_params = [:username, :password, :sid, :privilege]
  opts = params_pack(extract_from_db_path(uri), key_params)
  @db = OCI8.new(*opts.values)
  @db.autocommit = true
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method in the class Rigrate::Driver

Instance Method Details

#commitObject



164
165
166
# File 'lib/rigrate/interface/oracle.rb', line 164

def commit
  @db.commit
end

#convert_question_mark_to_symbol(sql, param_size) ⇒ Object



62
63
64
65
66
67
68
# File 'lib/rigrate/interface/oracle.rb', line 62

def convert_question_mark_to_symbol(sql, param_size)
  param_size.times do |idx|
    sql.sub!('?', ":#{idx + 1}")
  end

  sql
end

#execute(sql, *args) ⇒ Object Also known as: insert, update, delete



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/rigrate/interface/oracle.rb', line 33

def execute(sql, *args)
  sql = convert_question_mark_to_symbol(sql, args.first.size)
  begin
    cursor = @db.parse(sql)
    args.each do |row|
      if Rigrate::Row === row
        row = row.data
      end
      cursor.exec(*row)
    end
  rescue Exception => e
     Rigrate.logger.error("execute SQL [#{sql}] ARGS [#{args.size}] -> #{e.backtrace.join('\n')}")
     raise DriverError.new("execute error #{e.message}")
  end
end

#extract_from_db_path(uri) ⇒ Object

oracle://scott:tiger@foctest?privilege=:SYSOPER



118
119
120
121
122
123
124
125
126
127
128
129
# File 'lib/rigrate/interface/oracle.rb', line 118

def extract_from_db_path(uri)
  uri = URI.parse(uri)
  args = {}

  args[:username] = uri.user if uri.user
  args[:password] = uri.password if uri.password
  args[:sid] = uri.host if uri.host
  URI::decode_www_form(uri.query.to_s).to_h.each do |key, val|
    args[key] = val
  end
  args
end

#params_pack(hash, keys) ⇒ Object



148
149
150
151
152
153
154
# File 'lib/rigrate/interface/oracle.rb', line 148

def params_pack(hash, keys)
  keys.each do |key|
    hash.delete(key) unless hash.keys.include? key
  end

  hash
end

#primary_key(tbl_name) ⇒ Object



131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
# File 'lib/rigrate/interface/oracle.rb', line 131

def primary_key(tbl_name)
  str =<<SQL
  select a.column_name 
   from user_cons_columns a, user_constraints b 
   where a.constraint_name = b.constraint_name 
   and b.constraint_type = 'P' 
  and a.table_name = '#{tbl_name}'
SQL

  result = []
  @db.exec(str) do |row|
    result << row.first
  end

  result
end

#rollbackObject



168
169
170
# File 'lib/rigrate/interface/oracle.rb', line 168

def rollback
  @db.rollbak
end

#select(sql, *args) ⇒ Object



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/rigrate/interface/oracle.rb', line 14

def select(sql, *args)
  target_tbl_name = extract_tbl_from_sql(sql)
  sql = convert_question_mark_to_symbol(sql, args.size)

  ResultSet.new.tap do |rs|
    cursor = @db.parse(sql)
    rs.db = self
    rs.target_tbl_name = target_tbl_name
    rs.rows = []
    cursor.exec(*args)
    while row = cursor.fetch
      new_row = Row.new(to_rb_row(row))
      yield new_row if block_given?
      rs.rows << new_row
    end
    rs.column_info = statement_fields(cursor)
  end
end

#statement_fields(cursor) ⇒ Object



52
53
54
55
56
57
58
59
60
# File 'lib/rigrate/interface/oracle.rb', line 52

def statement_fields(cursor)
  cols = []

  cursor..each do |field|
    cols << Column.new(field.name, field.data_type)
  end

  cols
end

#to_native_row(row, column_info) ⇒ Object

TODO add blob/clob support



71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
# File 'lib/rigrate/interface/oracle.rb', line 71

def to_native_row(row, column_info)
  column_info.each_with_index do |col_info, idx|
    case col_info.type.to_s.downcase.to_sym
    when :blob
      new_val = OCI8::BLOB.new(@db, row[idx])
    when :clob
      new_val = OCI8::CLOB.new(@db, row[idx])
    when :bclob
      new_val = OCI8::NCLOB.new(@db, row[idx])
    when :date
      if row[idx]
        if Time === row[idx]
          new_val = row[idx]
        elsif String === row[idx] && row[idx].size > 0
          new_val = Time.parse(row[idx])
        end
      else
        new_val = ''
      end
    else
      new_val = row[idx]
    end

    if new_val.nil?
      new_val = ''
    end

    row.[]=(idx, new_val, false)
  end

  row
end

#to_rb_row(row) ⇒ Object



104
105
106
107
108
109
110
111
112
113
114
115
# File 'lib/rigrate/interface/oracle.rb', line 104

def to_rb_row(row)
  row.map do |field|
    type = field.class
    if [OCI8::BLOB, OCI8::CLOB, OCI8::NCLOB].include? type
      field.read
    elsif Time == type
      field.to_s          
    else
      field
    end
  end
end

#transactionObject



160
161
162
# File 'lib/rigrate/interface/oracle.rb', line 160

def transaction
  @db.autocommit = false
end

#transaction_active?Boolean

Returns:

  • (Boolean)


156
157
158
# File 'lib/rigrate/interface/oracle.rb', line 156

def transaction_active?
  ! @db.autocommit?
end