Class: ActiveRecord::ConnectionAdapters::OracleEnhanced::JDBCConnection::Cursor

Inherits:
Object
  • Object
show all
Defined in:
lib/active_record/connection_adapters/oracle_enhanced/jdbc_connection.rb

Instance Method Summary collapse

Constructor Details

#initialize(connection, raw_statement, exec_sql = nil) ⇒ Cursor

Returns a new instance of Cursor.



326
327
328
329
330
# File 'lib/active_record/connection_adapters/oracle_enhanced/jdbc_connection.rb', line 326

def initialize(connection, raw_statement, exec_sql = nil)
  @raw_connection = connection
  @raw_statement = raw_statement
  @exec_sql = exec_sql  # non-nil for DDL/PL/SQL using createStatement
end

Instance Method Details

#bind_param(position, value) ⇒ Object



344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
# File 'lib/active_record/connection_adapters/oracle_enhanced/jdbc_connection.rb', line 344

def bind_param(position, value)
  case value
  when Integer
    @raw_statement.setLong(position, value)
  when Float
    @raw_statement.setFloat(position, value)
  when BigDecimal
    @raw_statement.setBigDecimal(position, value)
  when Java::OracleSql::BLOB
    @raw_statement.setBlob(position, value)
  when Java::OracleSql::CLOB
    @raw_statement.setClob(position, value)
  when Java::OracleSql::NCLOB
    @raw_statement.setClob(position, value)
  when Type::OracleEnhanced::Raw
    @raw_statement.setString(position, OracleEnhanced::Quoting.encode_raw(value))
  when Type::OracleEnhanced::CharacterString::Data
    @raw_statement.setFixedCHAR(position, value.to_s)
  when String
    @raw_statement.setString(position, value)
  when Java::OracleSql::DATE
    @raw_statement.setDATE(position, value)
  when Java::JavaSql::Timestamp
    @raw_statement.setTimestamp(position, value)
  when Time
    new_value = java.sql.Timestamp.new(value.to_i * 1000)
    new_value.setNanos(value.nsec)
    # Oracle JDBC getTimestamp uses the session timezone, but setTimestamp
    # (without Calendar) uses the JVM default timezone. Pass a Calendar
    # matching the session timezone so both sides use the same reference.
    tz_id = @raw_connection.session_time_zone || java.util.TimeZone.default.getID
    cal = java.util.Calendar.getInstance(java.util.TimeZone.getTimeZone(tz_id))
    @raw_statement.setTimestamp(position, new_value, cal)
  when NilClass
    # TODO: currently nil is always bound as NULL with VARCHAR type.
    # When nils will actually be used by ActiveRecord as bound parameters
    # then need to pass actual column type.
    @raw_statement.setNull(position, java.sql.Types::VARCHAR)
  else
    raise ArgumentError, "Don't know how to bind variable with type #{value.class}"
  end
end

#bind_params(*bind_vars) ⇒ Object



332
333
334
335
336
337
338
339
340
341
342
# File 'lib/active_record/connection_adapters/oracle_enhanced/jdbc_connection.rb', line 332

def bind_params(*bind_vars)
  index = 1
  bind_vars.flatten.each do |var|
    if Hash === var
      var.each { |key, val| bind_param key, val }
    else
      bind_param index, var
      index += 1
    end
  end
end

#bind_returning_param(position, bind_type) ⇒ Object



387
388
389
390
391
392
393
# File 'lib/active_record/connection_adapters/oracle_enhanced/jdbc_connection.rb', line 387

def bind_returning_param(position, bind_type)
  @returning_positions ||= []
  @returning_positions << position
  if bind_type == Integer
    @raw_statement.registerReturnParameter(position, java.sql.Types::BIGINT)
  end
end

#closeObject



460
461
462
463
464
465
466
467
468
469
470
471
472
473
# File 'lib/active_record/connection_adapters/oracle_enhanced/jdbc_connection.rb', line 460

def close
  @raw_statement.close
rescue Java::JavaSql::SQLException => e
  # Tolerate closing a statement whose underlying connection or
  # statement has already been torn down (typical after with_retry
  # has reset the connection). Re-raise anything else so genuine
  # driver/resource bugs surface.
  #
  #   ORA-17002 "Io exception"        - network/socket failure
  #   ORA-17008 "Closed Connection"   - connection already closed
  #   ORA-17009 "Closed Statement"    - statement already closed
  raise unless [17_002, 17_008, 17_009].include?(e.getErrorCode)
  nil
end

#column_namesObject Also known as: get_col_names



419
420
421
422
# File 'lib/active_record/connection_adapters/oracle_enhanced/jdbc_connection.rb', line 419

def column_names
  return [] unless @raw_result_set
  @column_names ||= (1...getColumnCount).map { |i| .getColumnName(i) }
end

#column_typesObject



414
415
416
417
# File 'lib/active_record/connection_adapters/oracle_enhanced/jdbc_connection.rb', line 414

def column_types
  return [] unless @raw_result_set
  @column_types ||= (1...getColumnCount).map { |i| .getColumnTypeName(i).to_sym }
end

#execObject



395
396
397
398
399
400
401
402
403
404
# File 'lib/active_record/connection_adapters/oracle_enhanced/jdbc_connection.rb', line 395

def exec
  if @exec_sql
    # DDL / PL/SQL block executed via createStatement — never returns rows
    @raw_statement.execute(@exec_sql)
  elsif @raw_statement.execute
    # execute() returns true when the result is a ResultSet (SELECT)
    @raw_result_set = @raw_statement.getResultSet
  end
  true
end

#exec_updateObject



406
407
408
# File 'lib/active_record/connection_adapters/oracle_enhanced/jdbc_connection.rb', line 406

def exec_update
  @raw_statement.executeUpdate
end

#fetch(options = {}) ⇒ Object



433
434
435
436
437
438
439
440
441
442
443
444
445
446
# File 'lib/active_record/connection_adapters/oracle_enhanced/jdbc_connection.rb', line 433

def fetch(options = {})
  if @raw_result_set.next
    get_lob_value = options[:get_lob_value]
    row_values = []
    column_types.each_with_index do |column_type, i|
      row_values <<
        @raw_connection.get_ruby_value_from_result_set(@raw_result_set, i + 1, column_type, get_lob_value)
    end
    row_values
  else
    @raw_result_set.close
    nil
  end
end

#get_returning_param(position, type) ⇒ Object



448
449
450
451
452
453
454
455
456
457
458
# File 'lib/active_record/connection_adapters/oracle_enhanced/jdbc_connection.rb', line 448

def get_returning_param(position, type)
  rs_position = @returning_positions.index(position) + 1
  rs = @raw_statement.getReturnResultSet
  if rs.next
    # Assuming that primary key will not be larger as long max value
    returning_id = rs.getLong(rs_position)
    rs.wasNull ? nil : returning_id
  else
    nil
  end
end

#metadataObject



410
411
412
# File 'lib/active_record/connection_adapters/oracle_enhanced/jdbc_connection.rb', line 410

def 
  @metadata ||= @raw_result_set&.
end

#row_countObject



429
430
431
# File 'lib/active_record/connection_adapters/oracle_enhanced/jdbc_connection.rb', line 429

def row_count
  @raw_statement.getUpdateCount
end

#select_statement?Boolean

Returns:

  • (Boolean)


425
426
427
# File 'lib/active_record/connection_adapters/oracle_enhanced/jdbc_connection.rb', line 425

def select_statement?
  !@raw_result_set.nil?
end