| Class | Sequel::IBMDB::Database |
| In: |
lib/sequel/adapters/ibmdb.rb
|
| Parent: | Sequel::Database |
| conversion_procs | [R] | Hash of connection procs for converting |
REORG the related table whenever it is altered. This is not always required, but it is necessary for compatibilty with other Sequel code in many cases.
# File lib/sequel/adapters/ibmdb.rb, line 188
188: def alter_table(name, generator=nil)
189: res = super
190: reorg(name)
191: res
192: end
Create a new connection object for the given server.
# File lib/sequel/adapters/ibmdb.rb, line 195
195: def connect(server)
196: opts = server_opts(server)
197:
198: # use uncataloged connection so that host and port can be supported
199: connection_string = ( \
200: 'Driver={IBM DB2 ODBC DRIVER};' \
201: "Database=#{opts[:database]};" \
202: "Hostname=#{opts[:host]};" \
203: "Port=#{opts[:port] || 50000};" \
204: 'Protocol=TCPIP;' \
205: "Uid=#{opts[:user]};" \
206: "Pwd=#{opts[:password]};" \
207: )
208:
209: Connection.new(connection_string)
210: end
Execute the given SQL on the database.
# File lib/sequel/adapters/ibmdb.rb, line 213
213: def execute(sql, opts={}, &block)
214: if sql.is_a?(Symbol)
215: execute_prepared_statement(sql, opts, &block)
216: else
217: synchronize(opts[:server]){|c| _execute(c, sql, opts, &block)}
218: end
219: rescue Connection::Error => e
220: raise_error(e)
221: end
Execute the given SQL on the database, returning the last inserted identity value.
# File lib/sequel/adapters/ibmdb.rb, line 225
225: def execute_insert(sql, opts={})
226: synchronize(opts[:server]) do |c|
227: if sql.is_a?(Symbol)
228: execute_prepared_statement(sql, opts)
229: else
230: _execute(c, sql, opts)
231: end
232: _execute(c, "SELECT IDENTITY_VAL_LOCAL() FROM SYSIBM.SYSDUMMY1", opts){|stmt| i = stmt.fetch_array.first.to_i; i}
233: end
234: rescue Connection::Error => e
235: raise_error(e)
236: end
Execute a prepared statement named by name on the database.
# File lib/sequel/adapters/ibmdb.rb, line 239
239: def execute_prepared_statement(ps_name, opts)
240: args = opts[:arguments]
241: ps = prepared_statement(ps_name)
242: sql = ps.prepared_sql
243: synchronize(opts[:server]) do |conn|
244: unless conn.prepared_statements.fetch(ps_name, []).first == sql
245: log_yield("PREPARE #{ps_name}: #{sql}"){conn.prepare(sql, ps_name)}
246: end
247: args = args.map{|v| v.nil? ? nil : prepared_statement_arg(v)}
248: log_sql = "EXECUTE #{ps_name}"
249: if ps.log_sql
250: log_sql << " ("
251: log_sql << sql
252: log_sql << ")"
253: end
254: begin
255: stmt = log_yield(log_sql, args){conn.execute_prepared(ps_name, *args)}
256: if block_given?
257: yield(stmt)
258: else
259: stmt.affected
260: end
261: ensure
262: stmt.free_result if stmt
263: end
264: end
265: end
On DB2, a table might need to be REORGed if you are testing existence of it. This REORGs automatically if the database raises a specific error that indicates it should be REORGed.
# File lib/sequel/adapters/ibmdb.rb, line 270
270: def table_exists?(name)
271: v ||= false # only retry once
272: sch, table_name = schema_and_table(name)
273: name = SQL::QualifiedIdentifier.new(sch, table_name) if sch
274: from(name).first
275: true
276: rescue DatabaseError => e
277: if e.to_s =~ /Operation not allowed for reason code "7" on table/ && v == false
278: # table probably needs reorg
279: reorg(name)
280: v = true
281: retry
282: end
283: false
284: end