Class: ActiveRecord::ConnectionAdapters::OracleEnhancedConnection

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

Overview

interface independent methods

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#raw_connectionObject (readonly)

Returns the value of attribute raw_connection.



17
18
19
# File 'lib/active_record/connection_adapters/oracle_enhanced/connection.rb', line 17

def raw_connection
  @raw_connection
end

Class Method Details

.create(config) ⇒ Object

:nodoc:



6
7
8
9
10
11
12
13
14
15
# File 'lib/active_record/connection_adapters/oracle_enhanced/connection.rb', line 6

def self.create(config)
  case ORACLE_ENHANCED_CONNECTION
  when :oci
    OracleEnhancedOCIConnection.new(config)
  when :jdbc
    OracleEnhancedJDBCConnection.new(config)
  else
    nil
  end
end

Instance Method Details

#describe(name) ⇒ Object

Used always by JDBC connection as well by OCI connection when describing tables over database link



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/active_record/connection_adapters/oracle_enhanced/connection.rb', line 31

def describe(name)
  name = name.to_s
  if name.include?('@')
    name, db_link = name.split('@')
    default_owner = select_value("SELECT username FROM all_db_links WHERE db_link = '#{db_link.upcase}'")
    db_link = "@#{db_link}"
  else
    db_link = nil
    default_owner = @owner
  end
  real_name = OracleEnhancedAdapter.valid_table_name?(name) ? name.upcase : name
  if real_name.include?('.')
    table_owner, table_name = real_name.split('.')
  else
    table_owner, table_name = default_owner, real_name
  end
  sql = <<-SQL
    SELECT owner, table_name, 'TABLE' name_type
    FROM all_tables#{db_link}
    WHERE owner = '#{table_owner}'
      AND table_name = '#{table_name}'
    UNION ALL
    SELECT owner, view_name table_name, 'VIEW' name_type
    FROM all_views#{db_link}
    WHERE owner = '#{table_owner}'
      AND view_name = '#{table_name}'
    UNION ALL
    SELECT table_owner, DECODE(db_link, NULL, table_name, table_name||'@'||db_link), 'SYNONYM' name_type
    FROM all_synonyms#{db_link}
    WHERE owner = '#{table_owner}'
      AND synonym_name = '#{table_name}'
    UNION ALL
    SELECT table_owner, DECODE(db_link, NULL, table_name, table_name||'@'||db_link), 'SYNONYM' name_type
    FROM all_synonyms#{db_link}
    WHERE owner = 'PUBLIC'
      AND synonym_name = '#{real_name}'
  SQL
  if result = select_one(sql)
    case result['name_type']
    when 'SYNONYM'
      describe("#{result['owner'] && "#{result['owner']}."}#{result['table_name']}#{db_link}")
    else
      db_link ? [result['owner'], result['table_name'], db_link] : [result['owner'], result['table_name']]
    end
  else
    raise OracleEnhancedConnectionException, %Q{"DESC #{name}" failed; does it exist?}
  end
end

#oracle_downcase(column_name) ⇒ Object

Oracle column names by default are case-insensitive, but treated as upcase; for neatness, we’ll downcase within Rails. EXCEPT that folks CAN quote their column names when creating Oracle tables, which makes then case-sensitive. I don’t know anybody who does this, but we’ll handle the theoretical case of a camelCase column name. I imagine other dbs handle this different, since there’s a unit test that’s currently failing test_oci.



25
26
27
28
# File 'lib/active_record/connection_adapters/oracle_enhanced/connection.rb', line 25

def oracle_downcase(column_name)
  return nil if column_name.nil?
  column_name =~ /[a-z]/ ? column_name : column_name.downcase
end

#select_one(sql) ⇒ Object

Returns a record hash with the column names as keys and column values as values.



82
83
84
85
# File 'lib/active_record/connection_adapters/oracle_enhanced/connection.rb', line 82

def select_one(sql)
  result = select(sql)
  result.first if result
end

#select_value(sql) ⇒ Object

Returns a single value from a record



88
89
90
91
92
# File 'lib/active_record/connection_adapters/oracle_enhanced/connection.rb', line 88

def select_value(sql)
  if result = select_one(sql)
    result.values.first
  end
end

#select_values(sql, name = nil) ⇒ Object

Returns an array of the values of the first column in a select:

select_values("SELECT id FROM companies LIMIT 3") => [1,2,3]


96
97
98
99
# File 'lib/active_record/connection_adapters/oracle_enhanced/connection.rb', line 96

def select_values(sql, name = nil)
  result = select(sql, name = nil)
  result.map { |r| r.values.first }
end