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

Inherits:
BaseDatabase
  • Object
show all
Includes:
Util
Defined in:
lib/dbd/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



301
302
303
304
305
306
# File 'lib/dbd/OCI8.rb', line 301

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

#[]=(attr, value) ⇒ Object



308
309
310
311
312
313
# File 'lib/dbd/OCI8.rb', line 308

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

#columns(table) ⇒ Object

SQLs are copied from DBD::Oracle.



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
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
# File 'lib/dbd/OCI8.rb', line 239

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



219
220
221
222
223
# File 'lib/dbd/OCI8.rb', line 219

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

#disconnectObject



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

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

#pingObject



212
213
214
215
216
217
# File 'lib/dbd/OCI8.rb', line 212

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

#prepare(statement) ⇒ Object



199
200
201
202
203
204
205
206
207
208
209
210
# File 'lib/dbd/OCI8.rb', line 199

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



225
226
227
228
229
# File 'lib/dbd/OCI8.rb', line 225

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

#tablesObject



231
232
233
234
235
236
# File 'lib/dbd/OCI8.rb', line 231

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