Class: DBI::DBD::OCI8::Database

Inherits:
BaseDatabase
  • Object
show all
Includes:
Util
Defined in:
lib/DBD/OCI8/OCI8.rb

Defined Under Namespace

Classes: DummyQuoter

Constant Summary

Constants included from Util

Util::ERROR_MAP

Instance Method Summary collapse

Methods included from Util

#raise_dbierror

Instance Method Details

#[](attr) ⇒ Object



275
276
277
278
279
280
# File 'lib/DBD/OCI8/OCI8.rb', line 275

def [](attr)
  case attr
  when 'AutoCommit'
    @handle.autocommit?
  end
end

#[]=(attr, value) ⇒ Object



282
283
284
285
286
287
# File 'lib/DBD/OCI8/OCI8.rb', line 282

def []=(attr, value)
  case attr
  when 'AutoCommit'
    @handle.autocommit = value
  end
end

#columns(table) ⇒ Object

SQLs are copied from DBD::Oracle.



213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
# File 'lib/DBD/OCI8/OCI8.rb', line 213

def columns(table)
  tab = @handle.describe_table(table)
  cols = tab.columns
  cols.collect! do |col|
    (col)
  end

  dbh = DBI::DatabaseHandle.new(self)

  pk_index_name = nil
  dbh.select_all(<<EOS, tab.obj_schema, tab.obj_name) do |row|
select index_name
from all_constraints
 where constraint_type = 'P'
 and owner = :1
 and table_name = :2
EOS
    pk_index_name = row[0]
  end

  indices = {}
  primaries = {}
  uniques = {}
  dbh.select_all(<<EOS, tab.obj_schema, tab.obj_name) do |row|
select a.column_name, a.index_name, b.uniqueness
from all_ind_columns a, all_indexes b
 where a.index_name = b.index_name
 and a.index_owner = b.owner
 and a.table_owner = :1
 and a.table_name = :2
EOS
    col_name, index_name, uniqueness = row
    indices[col_name] = true
    primaries[col_name] = true if index_name == pk_index_name
    uniques[col_name] = true if uniqueness == 'UNIQUE'
  end

  dbh.select_all(<<EOS, tab.obj_schema, tab.obj_name).collect do |row|
select column_id, column_name, data_default
from all_tab_columns
 where owner = :1
 and table_name = :2
EOS
    col_id, col_name, default = row

    col = cols[col_id.to_i - 1]
    col_name = col['name']

    if default && default[0] == ?'
      default = default[1..-2].gsub(/''/, "'")
    end

    col['indexed']   = indices[col_name]   || false
    col['primary']   = primaries[col_name] || false
    col['unique']    = uniques[col_name]   || false
    col['default']   = default
    col
  end
rescue OCIException => err
  raise_dbierror(err)
end

#commitObject



193
194
195
196
197
# File 'lib/DBD/OCI8/OCI8.rb', line 193

def commit
  @handle.commit()
rescue OCIException => err
  raise_dbierror(err)
end

#disconnectObject



167
168
169
170
171
# File 'lib/DBD/OCI8/OCI8.rb', line 167

def disconnect
  @handle.logoff
rescue OCIException => err
  raise_dbierror(err)
end

#pingObject



186
187
188
189
190
191
# File 'lib/DBD/OCI8/OCI8.rb', line 186

def ping
  @handle.exec("BEGIN NULL; END;")
  true
rescue
  false
end

#prepare(statement) ⇒ Object



173
174
175
176
177
178
179
180
181
182
183
184
# File 'lib/DBD/OCI8/OCI8.rb', line 173

def prepare( statement )
  # convert ?-style parameters to :1, :2 etc.
  prep_statement = DBI::SQL::PreparedStatement.new(DummyQuoter.new, statement)
  if prep_statement.unbound.size > 0
    arr = (1..(prep_statement.unbound.size)).collect{|i| ":#{i}"}
    statement = prep_statement.bind( arr ) 
  end
  cursor = @handle.parse(statement)
  Statement.new(cursor)
rescue OCIException => err
  raise_dbierror(err)
end

#rollbackObject



199
200
201
202
203
# File 'lib/DBD/OCI8/OCI8.rb', line 199

def rollback
  @handle.rollback()
rescue OCIException => err
  raise_dbierror(err)
end

#tablesObject



205
206
207
208
209
210
# File 'lib/DBD/OCI8/OCI8.rb', line 205

def tables
  stmt = execute("SELECT object_name FROM user_objects where object_type in ('TABLE', 'VIEW')")
  rows = stmt.fetch_all || []
  stmt.finish
  rows.collect {|row| row[0]} 
end