Class | Sequel::IBMDB::Database |
In: |
lib/sequel/adapters/ibmdb.rb
|
Parent: | Sequel::Database |
conversion_procs | [R] | Hash of connection procs for converting |
# File lib/sequel/adapters/ibmdb.rb, line 168 168: def initialize(opts={}) 169: super 170: @conversion_procs = DB2_TYPES.dup 171: @conversion_procs[:timestamp] = method(:to_application_timestamp) 172: end
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 177 177: def alter_table(name, generator=nil, &block) 178: res = super 179: reorg(name) 180: res 181: end
Create a new connection object for the given server.
# File lib/sequel/adapters/ibmdb.rb, line 184 184: def connect(server) 185: opts = server_opts(server) 186: 187: # use uncataloged connection so that host and port can be supported 188: connection_string = ( \ 189: 'Driver={IBM DB2 ODBC DRIVER};' \ 190: "Database=#{opts[:database]};" \ 191: "Hostname=#{opts[:host]};" \ 192: "Port=#{opts[:port] || 50000};" \ 193: 'Protocol=TCPIP;' \ 194: "Uid=#{opts[:user]};" \ 195: "Pwd=#{opts[:password]};" \ 196: ) 197: 198: Connection.new(connection_string) 199: end
Execute the given SQL on the database.
# File lib/sequel/adapters/ibmdb.rb, line 202 202: def execute(sql, opts={}, &block) 203: if sql.is_a?(Symbol) 204: execute_prepared_statement(sql, opts, &block) 205: else 206: synchronize(opts[:server]){|c| _execute(c, sql, opts, &block)} 207: end 208: rescue Connection::Error => e 209: raise_error(e) 210: end
Execute the given SQL on the database, returning the last inserted identity value.
# File lib/sequel/adapters/ibmdb.rb, line 214 214: def execute_insert(sql, opts={}) 215: synchronize(opts[:server]) do |c| 216: if sql.is_a?(Symbol) 217: execute_prepared_statement(sql, opts) 218: else 219: _execute(c, sql, opts) 220: end 221: _execute(c, "SELECT IDENTITY_VAL_LOCAL() FROM SYSIBM.SYSDUMMY1", opts){|stmt| i = stmt.fetch_array.first.to_i; stmt.free; i} 222: end 223: rescue Connection::Error => e 224: raise_error(e) 225: end
Execute a prepared statement named by name on the database.
# File lib/sequel/adapters/ibmdb.rb, line 228 228: def execute_prepared_statement(ps_name, opts) 229: args = opts[:arguments] 230: ps = prepared_statements[ps_name] 231: sql = ps.prepared_sql 232: synchronize(opts[:server]) do |conn| 233: unless conn.prepared_statements.fetch(ps_name, []).first == sql 234: log_yield("Preparing #{ps_name}: #{sql}"){conn.prepare(sql, ps_name)} 235: end 236: args = args.map{|v| v.nil? ? nil : prepared_statement_arg(v)} 237: stmt = log_yield("Executing #{ps_name}: #{args.inspect}"){conn.execute_prepared(ps_name, *args)} 238: 239: if block_given? 240: begin 241: yield(stmt) 242: ensure 243: stmt.free 244: end 245: else 246: stmt.affected 247: end 248: end 249: end
Convert smallint type to boolean if convert_smallint_to_bool is true
# File lib/sequel/adapters/ibmdb.rb, line 252 252: def schema_column_type(db_type) 253: if Sequel::IBMDB.convert_smallint_to_bool && db_type =~ /smallint/i 254: :boolean 255: else 256: super 257: end 258: 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 263 263: def table_exists?(name) 264: v ||= false # only retry once 265: sch, table_name = schema_and_table(name) 266: name = SQL::QualifiedIdentifier.new(sch, table_name) if sch 267: from(name).first 268: true 269: rescue DatabaseError => e 270: if e.to_s =~ /Operation not allowed for reason code "7" on table/ && v == false 271: # table probably needs reorg 272: reorg(name) 273: v = true 274: retry 275: end 276: false 277: end