Class: ActiveRecord::ConnectionAdapters::IBM_DB2
- Inherits:
-
IBM_DataServer
- Object
- IBM_DataServer
- ActiveRecord::ConnectionAdapters::IBM_DB2
- Defined in:
- lib/active_record/connection_adapters/ibm_db_adapter.rb
Overview
class IBM_DataServer
Direct Known Subclasses
Instance Method Summary collapse
- #change_column(table_name, column_name, type, options) ⇒ Object
-
#change_column_default(table_name, column_name, default) ⇒ Object
DB2 specific ALTER TABLE statement to add a default clause.
-
#change_column_null(table_name, column_name, null, default) ⇒ Object
DB2 specific ALTER TABLE statement to change the nullability of a column.
- #extract_new_default_value(default_or_changes) ⇒ Object
-
#get_datetime_mapping ⇒ Object
This method returns the DB2 SQL type corresponding to the Rails datetime/timestamp type.
-
#get_double_mapping ⇒ Object
This method returns the DB2 SQL type corresponding to Rails double type.
-
#get_time_mapping ⇒ Object
This method returns the DB2 SQL type corresponding to the Rails time type.
-
#initialize(adapter, ar3) ⇒ IBM_DB2
constructor
A new instance of IBM_DB2.
-
#last_generated_id(stmt) ⇒ Object
Returns the last automatically generated ID.
- #primary_key_definition(start_id) ⇒ Object
- #rename_column(_table_name, _column_name, _new_column_name) ⇒ Object
-
#set_binary_default(value) ⇒ Object
This method generates the default blob value specified for DB2 Dataservers.
-
#set_binary_value ⇒ Object
This method generates the blob value specified for DB2 Dataservers.
-
#set_case(value) ⇒ Object
For DB2 Dataservers , the arguments to the meta-data functions need to be in upper-case.
-
#set_text_default(value) ⇒ Object
This method generates the default clob value specified for DB2 Dataservers.
Methods inherited from IBM_DataServer
#check_reserved_words, #create_index_after_table, #execute, #limit_not_supported_types, #prepare, #remove_column, #reorg_table, #select, #select_rows, #set_schema, #setup_for_lob_table
Constructor Details
#initialize(adapter, ar3) ⇒ IBM_DB2
Returns a new instance of IBM_DB2.
3750 3751 3752 3753 |
# File 'lib/active_record/connection_adapters/ibm_db_adapter.rb', line 3750 def initialize(adapter, ar3) super(adapter, ar3) @limit = @offset = nil end |
Instance Method Details
#change_column(table_name, column_name, type, options) ⇒ Object
3809 3810 3811 3812 3813 3814 3815 3816 3817 3818 3819 3820 3821 3822 3823 3824 3825 3826 3827 3828 3829 3830 3831 |
# File 'lib/active_record/connection_adapters/ibm_db_adapter.rb', line 3809 def change_column(table_name, column_name, type, ) @adapter.puts_log "change_column #{table_name}, #{column_name}, #{type}" column = @adapter.column_for(table_name, column_name) data_type = @adapter.type_to_sql(type, [:limit], [:precision], [:scale]) if column.sql_type != data_type begin execute "ALTER TABLE #{table_name} ALTER #{column_name} SET DATA TYPE #{data_type}" rescue StandardError => e raise "#{e}" unless e..include?('SQLCODE=-190') raise StatementInvalid, "Please consult documentation for compatible data types while changing column datatype. \ The column datatype change to [#{data_type}] is not supported by this data server: #{e}" end reorg_table(table_name) end change_column_default(table_name, column_name, [:default]) if .key?(:default) return unless .key?(:null) change_column_null(table_name, column_name, [:null], nil) end |
#change_column_default(table_name, column_name, default) ⇒ Object
DB2 specific ALTER TABLE statement to add a default clause
3843 3844 3845 3846 3847 3848 3849 3850 3851 3852 3853 3854 3855 3856 3857 3858 3859 |
# File 'lib/active_record/connection_adapters/ibm_db_adapter.rb', line 3843 def change_column_default(table_name, column_name, default) @adapter.puts_log "change_column_default #{table_name} #{column_name}" @adapter.puts_log "Default: #{default}" default = extract_new_default_value(default) # SQL statement which alters column's default value change_column_sql = if default.nil? "ALTER TABLE #{table_name} ALTER COLUMN #{column_name} SET WITH DEFAULT NULL" else "ALTER TABLE #{table_name} ALTER COLUMN #{column_name} SET WITH DEFAULT #{@adapter.quote(default)}" end stmt = execute(change_column_sql) reorg_table(table_name) ensure IBM_DB.free_stmt(stmt) if stmt end |
#change_column_null(table_name, column_name, null, default) ⇒ Object
DB2 specific ALTER TABLE statement to change the nullability of a column
3862 3863 3864 3865 3866 3867 3868 3869 3870 3871 3872 3873 3874 3875 3876 3877 |
# File 'lib/active_record/connection_adapters/ibm_db_adapter.rb', line 3862 def change_column_null(table_name, column_name, null, default) @adapter.puts_log "change_column_null #{table_name} #{column_name}" change_column_default(table_name, column_name, default) unless default.nil? unless null.nil? change_column_sql = if null "ALTER TABLE #{table_name} ALTER #{column_name} DROP NOT NULL" else "ALTER TABLE #{table_name} ALTER #{column_name} SET NOT NULL" end stmt = execute(change_column_sql) reorg_table(table_name) end ensure IBM_DB.free_stmt(stmt) if stmt end |
#extract_new_default_value(default_or_changes) ⇒ Object
3833 3834 3835 3836 3837 3838 3839 3840 |
# File 'lib/active_record/connection_adapters/ibm_db_adapter.rb', line 3833 def extract_new_default_value(default_or_changes) @adapter.puts_log 'extract_new_default_value' if default_or_changes.is_a?(Hash) && default_or_changes.has_key?(:from) && default_or_changes.has_key?(:to) default_or_changes[:to] else default_or_changes end end |
#get_datetime_mapping ⇒ Object
This method returns the DB2 SQL type corresponding to the Rails datetime/timestamp type
3881 3882 3883 3884 |
# File 'lib/active_record/connection_adapters/ibm_db_adapter.rb', line 3881 def get_datetime_mapping @adapter.puts_log 'get_datetime_mapping' 'timestamp' end |
#get_double_mapping ⇒ Object
This method returns the DB2 SQL type corresponding to Rails double type
3894 3895 3896 3897 |
# File 'lib/active_record/connection_adapters/ibm_db_adapter.rb', line 3894 def get_double_mapping @adapter.puts_log 'get_double_mapping' 'double' end |
#get_time_mapping ⇒ Object
This method returns the DB2 SQL type corresponding to the Rails time type
3888 3889 3890 3891 |
# File 'lib/active_record/connection_adapters/ibm_db_adapter.rb', line 3888 def get_time_mapping @adapter.puts_log 'get_time_mapping' 'time' end |
#last_generated_id(stmt) ⇒ Object
Returns the last automatically generated ID. This method is required by the insert
method The “stmt” parameter is ignored for DB2 but used for IDS
3768 3769 3770 3771 3772 3773 3774 3775 3776 3777 3778 3779 3780 3781 3782 3783 3784 3785 3786 3787 3788 3789 3790 3791 3792 3793 3794 3795 3796 3797 3798 3799 3800 3801 3802 3803 3804 3805 3806 3807 |
# File 'lib/active_record/connection_adapters/ibm_db_adapter.rb', line 3768 def last_generated_id(stmt) # Queries the db to obtain the last ID that was automatically generated @adapter.puts_log 'last_generated_id' sql = 'SELECT IDENTITY_VAL_LOCAL() FROM SYSIBM.SYSDUMMY1' stmt = IBM_DB.prepare(@adapter.connection, sql) if stmt if IBM_DB.execute(stmt, nil) begin # Fetches the only record available (containing the last id) IBM_DB.fetch_row(stmt) # Retrieves and returns the result of the query with the last id. id_value = IBM_DB.result(stmt, 0) id_value.to_i rescue StandardError => e # Handle driver fetch errors error_msg = IBM_DB.getErrormsg(stmt, IBM_DB::DB_STMT) raise "Failed to retrieve last generated id: #{error_msg}" if error_msg && !error_msg.empty? error_msg = 'An unexpected error occurred during retrieval of last generated id' error_msg += ": #{e.}" unless e..empty? raise error_msg ensure # Free resources associated with the statement IBM_DB.free_stmt(stmt) if stmt end else error_msg = IBM_DB.getErrormsg(stmt, IBM_DB::DB_STMT) IBM_DB.free_stmt(stmt) if stmt raise "Failed to retrieve last generated id: #{error_msg}" if error_msg && !error_msg.empty? error_msg = 'An unexpected error occurred during retrieval of last generated id' raise error_msg end else error_msg = IBM_DB.getErrormsg(@adapter.connection, IBM_DB::DB_CONN) raise "Failed to retrieve last generated id due to error: #{error_msg}" if error_msg && !error_msg.empty? raise StandardError.new('An unexpected error occurred during retrieval of last generated id') end end |
#primary_key_definition(start_id) ⇒ Object
3760 3761 3762 3763 |
# File 'lib/active_record/connection_adapters/ibm_db_adapter.rb', line 3760 def primary_key_definition(start_id) @adapter.puts_log 'primary_key_definition' "INTEGER GENERATED BY DEFAULT AS IDENTITY (START WITH #{start_id})" end |
#rename_column(_table_name, _column_name, _new_column_name) ⇒ Object
3755 3756 3757 3758 |
# File 'lib/active_record/connection_adapters/ibm_db_adapter.rb', line 3755 def rename_column(_table_name, _column_name, _new_column_name) @adapter.puts_log 'rename_column' raise NotImplementedError, 'rename_column is not implemented yet in the IBM_DB Adapter' end |
#set_binary_default(value) ⇒ Object
This method generates the default blob value specified for DB2 Dataservers
3901 3902 3903 3904 |
# File 'lib/active_record/connection_adapters/ibm_db_adapter.rb', line 3901 def set_binary_default(value) @adapter.puts_log 'set_binary_default' "BLOB('#{value}')" end |
#set_binary_value ⇒ Object
This method generates the blob value specified for DB2 Dataservers
3907 3908 3909 3910 |
# File 'lib/active_record/connection_adapters/ibm_db_adapter.rb', line 3907 def set_binary_value @adapter.puts_log 'set_binary_value' "BLOB('?')" end |
#set_case(value) ⇒ Object
For DB2 Dataservers , the arguments to the meta-data functions need to be in upper-case
3921 3922 3923 3924 |
# File 'lib/active_record/connection_adapters/ibm_db_adapter.rb', line 3921 def set_case(value) @adapter.puts_log 'set_case' value.upcase end |
#set_text_default(value) ⇒ Object
This method generates the default clob value specified for DB2 Dataservers
3914 3915 3916 3917 |
# File 'lib/active_record/connection_adapters/ibm_db_adapter.rb', line 3914 def set_text_default(value) @adapter.puts_log 'set_text_default' "'#{value}'" end |