Class: ActiveRecord::ConnectionAdapters::IBM_IDS

Inherits:
IBM_DataServer show all
Defined in:
lib/active_record/connection_adapters/ibm_db_adapter.rb

Overview

class IBM_DB2_I5

Instance Method Summary collapse

Methods inherited from IBM_DataServer

#check_reserved_words, #create_index_after_table, #execute, #initialize, #limit_not_supported_types, #prepare, #remove_column, #select, #select_rows, #setup_for_lob_table

Constructor Details

This class inherits a constructor from ActiveRecord::ConnectionAdapters::IBM_DataServer

Instance Method Details

#change_column(table_name, column_name, type, options) ⇒ Object



4059
4060
4061
4062
4063
4064
4065
4066
4067
4068
4069
# File 'lib/active_record/connection_adapters/ibm_db_adapter.rb', line 4059

def change_column(table_name, column_name, type, options)
  if !options[:null].nil? && !options[:null]
    execute "ALTER TABLE #{table_name} MODIFY #{column_name} #{@adapter.type_to_sql(type, options[:limit],
                                                                                    options[:precision], options[:scale])} NOT NULL"
  else
    execute "ALTER TABLE #{table_name} MODIFY #{column_name} #{@adapter.type_to_sql(type, options[:limit],
                                                                                    options[:precision], options[:scale])}"
  end
  change_column_default(table_name, column_name, options[:default]) unless options[:default].nil?
  reorg_table(table_name)
end

#change_column_default(table_name, column_name, default) ⇒ Object

IDS specific ALTER TABLE statement to add a default clause IDS requires the data type to be explicitly specified when adding the DEFAULT clause



4074
4075
4076
4077
4078
4079
4080
4081
4082
4083
4084
4085
4086
4087
4088
4089
4090
4091
# File 'lib/active_record/connection_adapters/ibm_db_adapter.rb', line 4074

def change_column_default(table_name, column_name, default)
  sql_type = nil
  is_nullable = true
  @adapter.columns(table_name).select do |col|
    if col.name == column_name
      sql_type = @adapter.type_to_sql(col.sql_type, col.limit, col.precision, col.scale)
      is_nullable = col.null
    end
  end
  # SQL statement which alters column's default value
  change_column_sql = "ALTER TABLE #{table_name} MODIFY #{column_name} #{sql_type} DEFAULT #{@adapter.quote(default)}"
  change_column_sql << ' NOT NULL' unless is_nullable
  stmt = execute(change_column_sql)
  reorg_table(table_name)
# Ensures to free the resources associated with the statement
ensure
  IBM_DB.free_stmt(stmt) if stmt
end

#change_column_null(table_name, column_name, null, default) ⇒ Object

IDS specific ALTER TABLE statement to change the nullability of a column



4094
4095
4096
4097
4098
4099
4100
4101
4102
4103
4104
4105
4106
4107
4108
4109
4110
4111
# File 'lib/active_record/connection_adapters/ibm_db_adapter.rb', line 4094

def change_column_null(table_name, column_name, null, default)
  change_column_default table_name, column_name, default unless default.nil?
  sql_type = nil
  @adapter.columns(table_name).select do |col|
    sql_type = @adapter.type_to_sql(col.sql_type, col.limit, col.precision, col.scale) if col.name == column_name
  end
  unless null.nil?
    change_column_sql = if !null
                          "ALTER TABLE #{table_name} MODIFY #{column_name} #{sql_type} NOT NULL"
                        else
                          "ALTER TABLE #{table_name} MODIFY #{column_name} #{sql_type}"
                        end
    stmt = execute(change_column_sql)
    reorg_table(table_name)
  end
ensure
  IBM_DB.free_stmt(stmt) if stmt
end

#get_datetime_mappingObject

This method returns the IDS SQL type corresponding to the Rails datetime/timestamp type



4120
4121
4122
# File 'lib/active_record/connection_adapters/ibm_db_adapter.rb', line 4120

def get_datetime_mapping
  'datetime year to fraction(5)'
end

#get_double_mappingObject

This method returns the IDS SQL type corresponding to Rails double type



4131
4132
4133
# File 'lib/active_record/connection_adapters/ibm_db_adapter.rb', line 4131

def get_double_mapping
  'double precision'
end

#get_time_mappingObject

This method returns the IDS SQL type corresponding to the Rails time type



4126
4127
4128
# File 'lib/active_record/connection_adapters/ibm_db_adapter.rb', line 4126

def get_time_mapping
  'datetime hour to second'
end

#last_generated_id(stmt) ⇒ Object

Method that returns the last automatically generated ID on the given @connection. This method is required by the insert method. IDS returns the last generated serial value in the SQLCA unlike DB2 where the generated value has to be retrieved using the IDENTITY_VAL_LOCAL function. We used the “stmt” parameter to identify the statement resource from which to get the last generated value



4141
4142
4143
# File 'lib/active_record/connection_adapters/ibm_db_adapter.rb', line 4141

def last_generated_id(stmt)
  IBM_DB.get_last_serial_value(stmt)
end

#primary_key_definition(start_id) ⇒ Object

End of rename_column



4055
4056
4057
# File 'lib/active_record/connection_adapters/ibm_db_adapter.rb', line 4055

def primary_key_definition(start_id)
  "SERIAL(#{start_id})"
end

#rename_column(table_name, column_name, new_column_name) ⇒ Object

IDS specific ALTER TABLE statement to rename a column



4021
4022
4023
4024
4025
4026
4027
4028
4029
4030
4031
4032
4033
4034
4035
4036
4037
4038
4039
4040
4041
4042
4043
4044
4045
4046
4047
4048
4049
4050
4051
4052
4053
# File 'lib/active_record/connection_adapters/ibm_db_adapter.rb', line 4021

def rename_column(table_name, column_name, new_column_name)
  _table_name      = table_name.to_s
  _column_name     = column_name.to_s
  _new_column_name = new_column_name.to_s

  nil_condition    = _table_name.nil? || _column_name.nil? || _new_column_name.nil?
  unless nil_condition
    empty_condition = _table_name.empty? ||
                      _column_name.empty? ||
                      _new_column_name.empty?
  end

  if nil_condition || empty_condition
    raise ArgumentError, 'One of the arguments passed to rename_column is empty or nil'
  end

  begin
    rename_column_sql = "RENAME COLUMN #{table_name}.#{column_name} TO \
         #{new_column_name}"

    unless stmt = execute(rename_column_sql)
      error_msg = IBM_DB.getErrormsg(@adapter.connection, IBM_DB::DB_CONN)
      raise "Rename column failed : #{error_msg}" if error_msg && !error_msg.empty?

      raise StandardError.new('An unexpected error occurred during renaming the column')

    end

    reorg_table(_table_name)
  ensure
    IBM_DB.free_stmt(stmt) if stmt
  end # End of begin
end

#reorg_table(table_name) ⇒ Object

Reorganizes the table for column changes



4114
4115
4116
# File 'lib/active_record/connection_adapters/ibm_db_adapter.rb', line 4114

def reorg_table(table_name)
  execute("UPDATE STATISTICS FOR TABLE #{table_name}")
end

#set_binary_default(value) ⇒ Object

This method throws an error when trying to create a default value on a BLOB/CLOB column for IDS. The documentation states: “if the column is a BLOB or CLOB datatype, NULL is the only valid default value.”



4148
4149
4150
4151
4152
# File 'lib/active_record/connection_adapters/ibm_db_adapter.rb', line 4148

def set_binary_default(value)
  return if value == 'NULL'

  raise 'Informix Dynamic Server only allows NULL as a valid default value for a BLOB data type'
end

#set_binary_valueObject

For Informix Dynamic Server, we treat binary value same as we treat a text value. We support literals by converting the insert into a dummy insert and an update (See handle_lobs method above)



4157
4158
4159
# File 'lib/active_record/connection_adapters/ibm_db_adapter.rb', line 4157

def set_binary_value
  "'@@@IBMBINARY@@@'"
end

#set_case(value) ⇒ Object

For Informix Dynamic Server, the arguments to the meta-data functions need to be in lower-case



4172
4173
4174
# File 'lib/active_record/connection_adapters/ibm_db_adapter.rb', line 4172

def set_case(value)
  value.downcase
end

#set_schema(schema) ⇒ Object

IDS does not support the SET SCHEMA syntax



4018
# File 'lib/active_record/connection_adapters/ibm_db_adapter.rb', line 4018

def set_schema(schema); end

#set_text_default(value) ⇒ Object

This method throws an error when trying to create a default value on a BLOB/CLOB column for IDS. The documentation states: “if the column is a BLOB or CLOB datatype, NULL is the only valid default value.”



4164
4165
4166
4167
4168
# File 'lib/active_record/connection_adapters/ibm_db_adapter.rb', line 4164

def set_text_default(value)
  return if value == 'NULL'

  raise 'Informix Dynamic Server only allows NULL as a valid default value for a CLOB data type'
end