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.
3104 3105 3106 3107 |
# File 'lib/active_record/connection_adapters/ibm_db_adapter.rb', line 3104 def initialize(adapter, ar3) super(adapter,ar3) @limit = @offset = nil end |
Instance Method Details
#change_column(table_name, column_name, type, options) ⇒ Object
3167 3168 3169 3170 3171 3172 3173 3174 3175 3176 3177 3178 3179 3180 3181 3182 3183 3184 3185 3186 3187 3188 3189 3190 3191 3192 3193 |
# File 'lib/active_record/connection_adapters/ibm_db_adapter.rb', line 3167 def change_column(table_name, column_name, type, ) @adapter.puts_log "change_column #{table_name.to_s}, #{column_name.to_s}, #{type.to_s}" 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 => exec_err if exec_err..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: #{exec_err}" else raise "#{exec_err}" end end reorg_table(table_name) end if .key?(:default) change_column_default(table_name, column_name, [:default]) end if .key?(:null) change_column_null(table_name, column_name, [:null], nil) end end |
#change_column_default(table_name, column_name, default) ⇒ Object
DB2 specific ALTER TABLE statement to add a default clause
3205 3206 3207 3208 3209 3210 3211 3212 3213 3214 3215 3216 3217 3218 3219 3220 3221 |
# File 'lib/active_record/connection_adapters/ibm_db_adapter.rb', line 3205 def change_column_default(table_name, column_name, default) @adapter.puts_log "change_column_default #{table_name.to_s} #{column_name.to_s}" @adapter.puts_log "Default: #{default}" default = extract_new_default_value(default) # SQL statement which alters column's default value if default.nil? change_column_sql = "ALTER TABLE #{table_name} ALTER COLUMN #{column_name} SET WITH DEFAULT NULL" else change_column_sql = "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
3224 3225 3226 3227 3228 3229 3230 3231 3232 3233 3234 3235 3236 3237 3238 3239 3240 3241 3242 |
# File 'lib/active_record/connection_adapters/ibm_db_adapter.rb', line 3224 def change_column_null(table_name, column_name, null, default) @adapter.puts_log "change_column_null #{table_name.to_s} #{column_name.to_s}" if !default.nil? change_column_default(table_name, column_name, default) end if !null.nil? if null change_column_sql = "ALTER TABLE #{table_name} ALTER #{column_name} DROP NOT NULL" else change_column_sql = "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
3195 3196 3197 3198 3199 3200 3201 3202 |
# File 'lib/active_record/connection_adapters/ibm_db_adapter.rb', line 3195 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
3246 3247 3248 3249 |
# File 'lib/active_record/connection_adapters/ibm_db_adapter.rb', line 3246 def get_datetime_mapping @adapter.puts_log "get_datetime_mapping" return "timestamp" end |
#get_double_mapping ⇒ Object
This method returns the DB2 SQL type corresponding to Rails double type
3259 3260 3261 3262 |
# File 'lib/active_record/connection_adapters/ibm_db_adapter.rb', line 3259 def get_double_mapping @adapter.puts_log "get_double_mapping" return "double" end |
#get_time_mapping ⇒ Object
This method returns the DB2 SQL type corresponding to the Rails time type
3253 3254 3255 3256 |
# File 'lib/active_record/connection_adapters/ibm_db_adapter.rb', line 3253 def get_time_mapping @adapter.puts_log "get_time_mapping" return "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
3122 3123 3124 3125 3126 3127 3128 3129 3130 3131 3132 3133 3134 3135 3136 3137 3138 3139 3140 3141 3142 3143 3144 3145 3146 3147 3148 3149 3150 3151 3152 3153 3154 3155 3156 3157 3158 3159 3160 3161 3162 3163 3164 3165 |
# File 'lib/active_record/connection_adapters/ibm_db_adapter.rb', line 3122 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) return id_value.to_i rescue StandardError => fetch_error # Handle driver fetch errors error_msg = IBM_DB.getErrormsg(stmt, IBM_DB::DB_STMT ) if error_msg && !error_msg.empty? raise "Failed to retrieve last generated id: #{error_msg}" else error_msg = "An unexpected error occurred during retrieval of last generated id" error_msg = error_msg + ": #{fetch_error.message}" if !fetch_error..empty? raise error_msg end 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 if error_msg && !error_msg.empty? raise "Failed to retrieve last generated id: #{error_msg}" else error_msg = "An unexpected error occurred during retrieval of last generated id" raise error_msg end end else error_msg = IBM_DB.getErrormsg(@adapter.connection, IBM_DB::DB_CONN ) if error_msg && !error_msg.empty? raise "Failed to retrieve last generated id due to error: #{error_msg}" else raise StandardError.new('An unexpected error occurred during retrieval of last generated id') end end end |
#primary_key_definition(start_id) ⇒ Object
3114 3115 3116 3117 |
# File 'lib/active_record/connection_adapters/ibm_db_adapter.rb', line 3114 def primary_key_definition(start_id) @adapter.puts_log "108" return "INTEGER GENERATED BY DEFAULT AS IDENTITY (START WITH #{start_id}) PRIMARY KEY NOT NULL" end |
#rename_column(table_name, column_name, new_column_name) ⇒ Object
3109 3110 3111 3112 |
# File 'lib/active_record/connection_adapters/ibm_db_adapter.rb', line 3109 def rename_column(table_name, column_name, new_column_name) @adapter.puts_log "primary_key_definition" 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
3266 3267 3268 3269 |
# File 'lib/active_record/connection_adapters/ibm_db_adapter.rb', line 3266 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
3272 3273 3274 3275 |
# File 'lib/active_record/connection_adapters/ibm_db_adapter.rb', line 3272 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
3286 3287 3288 3289 |
# File 'lib/active_record/connection_adapters/ibm_db_adapter.rb', line 3286 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
3279 3280 3281 3282 |
# File 'lib/active_record/connection_adapters/ibm_db_adapter.rb', line 3279 def set_text_default(value) @adapter.puts_log "set_text_default" "'#{value}'" end |