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.
3158 3159 3160 3161 |
# File 'lib/active_record/connection_adapters/ibm_db_adapter.rb', line 3158 def initialize(adapter, ar3) super(adapter,ar3) @limit = @offset = nil end |
Instance Method Details
#change_column(table_name, column_name, type, options) ⇒ Object
3221 3222 3223 3224 3225 3226 3227 3228 3229 3230 3231 3232 3233 3234 3235 3236 3237 3238 3239 3240 3241 3242 3243 3244 3245 3246 3247 |
# File 'lib/active_record/connection_adapters/ibm_db_adapter.rb', line 3221 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
3259 3260 3261 3262 3263 3264 3265 3266 3267 3268 3269 3270 3271 3272 3273 3274 3275 |
# File 'lib/active_record/connection_adapters/ibm_db_adapter.rb', line 3259 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
3278 3279 3280 3281 3282 3283 3284 3285 3286 3287 3288 3289 3290 3291 3292 3293 3294 3295 3296 |
# File 'lib/active_record/connection_adapters/ibm_db_adapter.rb', line 3278 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
3249 3250 3251 3252 3253 3254 3255 3256 |
# File 'lib/active_record/connection_adapters/ibm_db_adapter.rb', line 3249 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
3300 3301 3302 3303 |
# File 'lib/active_record/connection_adapters/ibm_db_adapter.rb', line 3300 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
3313 3314 3315 3316 |
# File 'lib/active_record/connection_adapters/ibm_db_adapter.rb', line 3313 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
3307 3308 3309 3310 |
# File 'lib/active_record/connection_adapters/ibm_db_adapter.rb', line 3307 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
3176 3177 3178 3179 3180 3181 3182 3183 3184 3185 3186 3187 3188 3189 3190 3191 3192 3193 3194 3195 3196 3197 3198 3199 3200 3201 3202 3203 3204 3205 3206 3207 3208 3209 3210 3211 3212 3213 3214 3215 3216 3217 3218 3219 |
# File 'lib/active_record/connection_adapters/ibm_db_adapter.rb', line 3176 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.}" 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
3168 3169 3170 3171 |
# File 'lib/active_record/connection_adapters/ibm_db_adapter.rb', line 3168 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
3163 3164 3165 3166 |
# File 'lib/active_record/connection_adapters/ibm_db_adapter.rb', line 3163 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
3320 3321 3322 3323 |
# File 'lib/active_record/connection_adapters/ibm_db_adapter.rb', line 3320 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
3326 3327 3328 3329 |
# File 'lib/active_record/connection_adapters/ibm_db_adapter.rb', line 3326 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
3340 3341 3342 3343 |
# File 'lib/active_record/connection_adapters/ibm_db_adapter.rb', line 3340 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
3333 3334 3335 3336 |
# File 'lib/active_record/connection_adapters/ibm_db_adapter.rb', line 3333 def set_text_default(value) @adapter.puts_log "set_text_default" "'#{value}'" end |