@@ -92,8 +92,8 @@ def type_cast(value)
9292 return nil if value . nil? || value == 'NULL' || value =~ /^\s *NULL\s *$/i
9393 case type
9494 when :string then value
95- when :integer then defined? ( value . to_i ) ? value . to_i : ( value ? 1 : 0 )
96- when :primary_key then defined? ( value . to_i ) ? value . to_i : ( value ? 1 : 0 )
95+ when :integer then value . respond_to? ( : to_i) ? value . to_i : ( value ? 1 : 0 )
96+ when :primary_key then value . respond_to? ( : to_i) ? value . to_i : ( value ? 1 : 0 )
9797 when :float then value . to_f
9898 when :datetime then ArJdbc ::DB2 ::Column . cast_to_date_or_time ( value )
9999 when :date then ArJdbc ::DB2 ::Column . cast_to_date_or_time ( value )
@@ -192,15 +192,14 @@ def table_definition(*args)
192192 def prefetch_primary_key? ( table_name = nil )
193193 # TRUE if the table has no identity column
194194 names = table_name . upcase . split ( "." )
195- sql = ""
196195 if as400?
197196 sql = "SELECT 1 FROM SYSIBM.SQLPRIMARYKEYS WHERE "
198- sql += "TABLE_SCHEM = '#{ names . first } ' AND " if names . size == 2
199- sql += "TABLE_NAME = '#{ names . last } '"
197+ sql << "TABLE_SCHEM = '#{ names . first } ' AND " if names . size == 2
198+ sql << "TABLE_NAME = '#{ names . last } '"
200199 else
201200 sql = "SELECT 1 FROM SYSCAT.COLUMNS WHERE IDENTITY = 'Y' "
202- sql += "AND TABSCHEMA = '#{ names . first } ' " if names . size == 2
203- sql += "AND TABNAME = '#{ names . last } '"
201+ sql << "AND TABSCHEMA = '#{ names . first } ' " if names . size == 2
202+ sql << "AND TABNAME = '#{ names . last } '"
204203 end
205204 select_one ( sql ) . nil?
206205 end
@@ -216,7 +215,7 @@ def execute_and_auto_confirm(sql)
216215 @connection . execute_update "call qsys.qcmdexc('ADDRPYLE SEQNBR(9876) MSGID(CPA32B2) RPY(''I'')',0000000045.00000)"
217216 rescue Exception => e
218217 raise "Could not call CHGJOB INQMSGRPY(*SYSRPYL) and ADDRPYLE SEQNBR(9876) MSGID(CPA32B2) RPY('I').\n " +
219- "Do you have authority to do this?\n \n " + e . to_s
218+ "Do you have authority to do this?\n \n #{ e . inspect } "
220219 end
221220
222221 result = execute sql
@@ -226,7 +225,7 @@ def execute_and_auto_confirm(sql)
226225 @connection . execute_update "call qsys.qcmdexc('RMVRPYLE SEQNBR(9876)',0000000021.00000)"
227226 rescue Exception => e
228227 raise "Could not call CHGJOB INQMSGRPY(*DFT) and RMVRPYLE SEQNBR(9876).\n " +
229- "Do you have authority to do this?\n \n " + e . to_s
228+ "Do you have authority to do this?\n \n #{ e . inspect } "
230229 end
231230 result
232231 end
@@ -240,6 +239,7 @@ def _execute(sql, name = nil)
240239 @connection . execute_update ( sql )
241240 end
242241 end
242+ private :_execute
243243
244244 def last_insert_id ( sql )
245245 table_name = sql . split ( /\s / ) [ 2 ]
@@ -436,14 +436,12 @@ def replace_limit_offset!(sql, limit, offset)
436436 sql
437437 end
438438
439- def reorg_table ( table_name )
440- unless as400?
441- @connection . execute_update "call sysproc.admin_cmd ('REORG TABLE #{ table_name } ')"
442- end
439+ def reorg_table ( table_name , name = nil )
440+ exec_update "call sysproc.admin_cmd ('REORG TABLE #{ table_name } ')" , name , [ ] unless as400?
443441 end
444442 private :reorg_table
445443
446- def runstats_for_table ( tablename , priority = 10 )
444+ def runstats_for_table ( tablename , priority = 10 )
447445 @connection . execute_update "call sysproc.admin_cmd('RUNSTATS ON TABLE #{ tablename } WITH DISTRIBUTION AND DETAILED INDEXES ALL UTIL_IMPACT_PRIORITY #{ priority } ')"
448446 end
449447
@@ -476,7 +474,7 @@ def rename_column(table_name, column_name, new_column_name) #:nodoc:
476474 raise NotImplementedError , "rename_column is not supported on IBM i"
477475 else
478476 execute "ALTER TABLE #{ table_name } RENAME COLUMN #{ column_name } TO #{ new_column_name } "
479- reorg_table ( table_name )
477+ reorg_table ( table_name , 'Rename Column' )
480478 end
481479 end
482480
@@ -487,7 +485,7 @@ def change_column_null(table_name, column_name, null)
487485 sql = "ALTER TABLE #{ table_name } ALTER COLUMN #{ column_name } SET NOT NULL"
488486 end
489487 as400? ? execute_and_auto_confirm ( sql ) : execute ( sql )
490- reorg_table ( table_name )
488+ reorg_table ( table_name , 'Change Column' )
491489 end
492490
493491 def change_column_default ( table_name , column_name , default )
@@ -497,14 +495,14 @@ def change_column_default(table_name, column_name, default)
497495 sql = "ALTER TABLE #{ table_name } ALTER COLUMN #{ column_name } SET WITH DEFAULT #{ quote ( default ) } "
498496 end
499497 as400? ? execute_and_auto_confirm ( sql ) : execute ( sql )
500- reorg_table ( table_name )
498+ reorg_table ( table_name , 'Change Column' )
501499 end
502500
503501 def change_column ( table_name , column_name , type , options = { } )
504502 data_type = type_to_sql ( type , options [ :limit ] , options [ :precision ] , options [ :scale ] )
505503 sql = "ALTER TABLE #{ table_name } ALTER COLUMN #{ column_name } SET DATA TYPE #{ data_type } "
506504 as400? ? execute_and_auto_confirm ( sql ) : execute ( sql )
507- reorg_table ( table_name )
505+ reorg_table ( table_name , 'Change Column' )
508506
509507 if options . include? ( :default ) and options . include? ( :null )
510508 # which to run first?
@@ -528,13 +526,13 @@ def remove_column(table_name, *column_names) #:nodoc:
528526 sql = "ALTER TABLE #{ table_name } DROP COLUMN #{ column_name } "
529527 as400? ? execute_and_auto_confirm ( sql ) : execute ( sql )
530528 end
531- reorg_table ( table_name )
529+ reorg_table ( table_name , 'Remove Column' )
532530 end
533531
534532 # http://publib.boulder.ibm.com/infocenter/db2luw/v9r7/topic/com.ibm.db2.luw.sql.ref.doc/doc/r0000980.html
535533 def rename_table ( name , new_name ) #:nodoc:
536534 execute "RENAME TABLE #{ name } TO #{ new_name } "
537- reorg_table ( new_name )
535+ reorg_table ( new_name , 'Rename Table' )
538536 end
539537
540538 def tables
0 commit comments