The DB2 adapter works with the C-based CLI driver (rubyforge.org/projects/ruby-dbi/)
Options:
- :username — Defaults to nothing
- :password — Defaults to nothing
- :database — The name of the database. No default, must be provided.
- :schema — Database schema to be set initially.
Methods
- active?
- adapter_name
- add_limit_offset!
- begin_db_transaction
- columns
- commit_db_transaction
- execute
- indexes
- insert
- native_database_types
- new
- quote_column_name
- quote_string
- quoted_false
- quoted_true
- reconnect!
- rollback_db_transaction
- table_alias_length
- tables
Public Class methods
[ show source ]
# File vendor/rails/activerecord/lib/active_record/connection_adapters/db2_adapter.rb, line 41
41: def initialize(connection, logger, connection_options)
42: super(connection, logger)
43: @connection_options = connection_options
44: if schema = @connection_options[:schema]
45: with_statement do |stmt|
46: stmt.exec_direct("SET SCHEMA=#{schema}")
47: end
48: end
49: end
Public Instance methods
[ show source ]
# File vendor/rails/activerecord/lib/active_record/connection_adapters/db2_adapter.rb, line 172
172: def active?
173: @connection.select_one 'select 1 from ibm.sysdummy1'
174: true
175: rescue Exception
176: false
177: end
[ show source ]
# File vendor/rails/activerecord/lib/active_record/connection_adapters/db2_adapter.rb, line 85
85: def adapter_name()
86: 'DB2'
87: end
[ show source ]
# File vendor/rails/activerecord/lib/active_record/connection_adapters/db2_adapter.rb, line 93
93: def add_limit_offset!(sql, options)
94: if limit = options[:limit]
95: offset = options[:offset] || 0
96: # The following trick was added by andrea+rails@webcom.it.
97: sql.gsub!(/SELECT/i, 'SELECT B.* FROM (SELECT A.*, row_number() over () AS internal$rownum FROM (SELECT')
98: sql << ") A ) B WHERE B.internal$rownum > #{offset} AND B.internal$rownum <= #{limit + offset}"
99: end
100: end
[ show source ]
# File vendor/rails/activerecord/lib/active_record/connection_adapters/db2_adapter.rb, line 67
67: def begin_db_transaction
68: @connection.set_auto_commit_off
69: end
[ show source ]
# File vendor/rails/activerecord/lib/active_record/connection_adapters/db2_adapter.rb, line 131
131: def columns(table_name, name = nil)
132: result = []
133: schema = @connection_options[:schema] || '%'
134: with_statement do |stmt|
135: stmt.columns(table_name, schema).each do |c|
136: c_name = c[3].downcase
137: c_default = c[12] == 'NULL' ? nil : c[12]
138: c_default.gsub!(/^'(.*)'$/, '\1') if !c_default.nil?
139: c_type = c[5].downcase
140: c_type += "(#{c[6]})" if !c[6].nil? && c[6] != ''
141: result << Column.new(c_name, c_default, c_type, c[17] == 'YES')
142: end
143: end
144: result
145: end
[ show source ]
# File vendor/rails/activerecord/lib/active_record/connection_adapters/db2_adapter.rb, line 71
71: def commit_db_transaction
72: @connection.commit
73: @connection.set_auto_commit_on
74: end
[ show source ]
# File vendor/rails/activerecord/lib/active_record/connection_adapters/db2_adapter.rb, line 56
56: def execute(sql, name = nil)
57: rows_affected = 0
58: with_statement do |stmt|
59: log(sql, name) do
60: stmt.exec_direct(sql)
61: rows_affected = stmt.row_count
62: end
63: end
64: rows_affected
65: end
[ show source ]
# File vendor/rails/activerecord/lib/active_record/connection_adapters/db2_adapter.rb, line 111
111: def indexes(table_name, name = nil)
112: tmp = {}
113: schema = @connection_options[:schema] || ''
114: with_statement do |stmt|
115: stmt.indexes(table_name, schema).each do |t|
116: next unless t[5]
117: next if t[4] == 'SYSIBM' # Skip system indexes.
118: idx_name = t[5].downcase
119: col_name = t[8].downcase
120: if tmp.has_key?(idx_name)
121: tmp[idx_name].columns << col_name
122: else
123: is_unique = t[3] == 0
124: tmp[idx_name] = IndexDefinition.new(table_name, idx_name, is_unique, [col_name])
125: end
126: end
127: end
128: tmp.values
129: end
[ show source ]
# File vendor/rails/activerecord/lib/active_record/connection_adapters/db2_adapter.rb, line 51
51: def insert(sql, name = nil, pk = nil, id_value = nil, sequence_name = nil)
52: execute(sql, name = nil)
53: id_value || last_insert_id
54: end
[ show source ]
# File vendor/rails/activerecord/lib/active_record/connection_adapters/db2_adapter.rb, line 147
147: def native_database_types
148: {
149: :primary_key => 'int generated by default as identity (start with 42) primary key',
150: :string => { :name => 'varchar', :limit => 255 },
151: :text => { :name => 'clob', :limit => 32768 },
152: :integer => { :name => 'int' },
153: :float => { :name => 'float' },
154: :decimal => { :name => 'decimal' },
155: :datetime => { :name => 'timestamp' },
156: :timestamp => { :name => 'timestamp' },
157: :time => { :name => 'time' },
158: :date => { :name => 'date' },
159: :binary => { :name => 'blob', :limit => 32768 },
160: :boolean => { :name => 'decimal', :limit => 1 }
161: }
162: end
[ show source ]
# File vendor/rails/activerecord/lib/active_record/connection_adapters/db2_adapter.rb, line 81
81: def quote_column_name(column_name)
82: column_name
83: end
[ show source ]
# File vendor/rails/activerecord/lib/active_record/connection_adapters/db2_adapter.rb, line 89
89: def quote_string(string)
90: string.gsub(/'/, "''") # ' (for ruby-mode)
91: end
[ show source ]
# File vendor/rails/activerecord/lib/active_record/connection_adapters/db2_adapter.rb, line 168
168: def quoted_false
169: '0'
170: end
[ show source ]
# File vendor/rails/activerecord/lib/active_record/connection_adapters/db2_adapter.rb, line 164
164: def quoted_true
165: '1'
166: end
[ show source ]
# File vendor/rails/activerecord/lib/active_record/connection_adapters/db2_adapter.rb, line 179
179: def reconnect!
180: end
[ show source ]
# File vendor/rails/activerecord/lib/active_record/connection_adapters/db2_adapter.rb, line 76
76: def rollback_db_transaction
77: @connection.rollback
78: @connection.set_auto_commit_on
79: end
[ show source ]
# File vendor/rails/activerecord/lib/active_record/connection_adapters/db2_adapter.rb, line 182
182: def table_alias_length
183: 128
184: end
[ show source ]
# File vendor/rails/activerecord/lib/active_record/connection_adapters/db2_adapter.rb, line 102
102: def tables(name = nil)
103: result = []
104: schema = @connection_options[:schema] || '%'
105: with_statement do |stmt|
106: stmt.tables(schema).each { |t| result << t[2].downcase }
107: end
108: result
109: end