Class: ActiveRecord::ConnectionAdapters::IBM_IDS
- Inherits:
-
IBM_DataServer
- Object
- IBM_DataServer
- ActiveRecord::ConnectionAdapters::IBM_IDS
- Defined in:
- lib/active_record/connection_adapters/ibm_db_adapter.rb
Overview
class IBM_DB2_I5
Instance Method Summary collapse
- #change_column(table_name, column_name, type, options) ⇒ Object
-
#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.
-
#change_column_null(table_name, column_name, null, default) ⇒ Object
IDS specific ALTER TABLE statement to change the nullability of a column.
-
#get_datetime_mapping ⇒ Object
This method returns the IDS SQL type corresponding to the Rails datetime/timestamp type.
-
#get_double_mapping ⇒ Object
This method returns the IDS SQL type corresponding to Rails double type.
-
#get_time_mapping ⇒ Object
This method returns the IDS SQL type corresponding to the Rails time type.
-
#last_generated_id(stmt) ⇒ Object
Method that returns the last automatically generated ID on the given @connection.
-
#primary_key_definition(start_id) ⇒ Object
End of rename_column.
-
#query_offset_limit(sql, offset, limit) ⇒ Object
Handling offset/limit as per Informix requirements.
-
#query_offset_limit!(sql, offset, limit, options) ⇒ Object
Handling offset/limit as per Informix requirements.
-
#rename_column(table_name, column_name, new_column_name) ⇒ Object
IDS specific ALTER TABLE statement to rename a column.
-
#reorg_table(table_name) ⇒ Object
Reorganizes the table for column changes.
-
#set_binary_default(value) ⇒ Object
This method throws an error when trying to create a default value on a BLOB/CLOB column for IDS.
-
#set_binary_value ⇒ Object
For Informix Dynamic Server, we treat binary value same as we treat a text value.
-
#set_case(value) ⇒ Object
For Informix Dynamic Server, the arguments to the meta-data functions need to be in lower-case.
-
#set_schema(schema) ⇒ Object
IDS does not support the SET SCHEMA syntax.
-
#set_text_default(value) ⇒ Object
This method throws an error when trying to create a default value on a BLOB/CLOB column for IDS.
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
2632 2633 2634 2635 2636 2637 2638 2639 2640 2641 2642 |
# File 'lib/active_record/connection_adapters/ibm_db_adapter.rb', line 2632 def change_column(table_name, column_name, type, ) if ![:null].nil? && ![:null] execute "ALTER TABLE #{table_name} MODIFY #{column_name} #{@adapter.type_to_sql(type, [:limit], [:precision], [:scale])} NOT NULL" else execute "ALTER TABLE #{table_name} MODIFY #{column_name} #{@adapter.type_to_sql(type, [:limit], [:precision], [:scale])}" end if ![:default].nil? change_column_default(table_name, column_name, [:default]) end 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
2647 2648 2649 2650 2651 2652 2653 2654 2655 2656 2657 2658 2659 2660 2661 2662 2663 2664 |
# File 'lib/active_record/connection_adapters/ibm_db_adapter.rb', line 2647 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.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
2667 2668 2669 2670 2671 2672 2673 2674 2675 2676 2677 2678 2679 2680 2681 2682 2683 2684 2685 2686 2687 2688 2689 |
# File 'lib/active_record/connection_adapters/ibm_db_adapter.rb', line 2667 def change_column_null(table_name,column_name,null,default) if !default.nil? change_column_default table_name, column_name, default end sql_type = nil @adapter.columns(table_name).select do |col| if (col.name == column_name) sql_type = @adapter.type_to_sql(col.type, col.limit, col.precision, col.scale) end end if !null.nil? if !null change_column_sql = "ALTER TABLE #{table_name} MODIFY #{column_name} #{sql_type} NOT NULL" else change_column_sql = "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_mapping ⇒ Object
This method returns the IDS SQL type corresponding to the Rails datetime/timestamp type
2698 2699 2700 |
# File 'lib/active_record/connection_adapters/ibm_db_adapter.rb', line 2698 def get_datetime_mapping return "datetime year to fraction(5)" end |
#get_double_mapping ⇒ Object
This method returns the IDS SQL type corresponding to Rails double type
2709 2710 2711 |
# File 'lib/active_record/connection_adapters/ibm_db_adapter.rb', line 2709 def get_double_mapping return "double precision" end |
#get_time_mapping ⇒ Object
This method returns the IDS SQL type corresponding to the Rails time type
2704 2705 2706 |
# File 'lib/active_record/connection_adapters/ibm_db_adapter.rb', line 2704 def get_time_mapping return "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
2753 2754 2755 |
# File 'lib/active_record/connection_adapters/ibm_db_adapter.rb', line 2753 def last_generated_id(stmt) IBM_DB.get_last_serial_value(stmt) end |
#primary_key_definition(start_id) ⇒ Object
End of rename_column
2628 2629 2630 |
# File 'lib/active_record/connection_adapters/ibm_db_adapter.rb', line 2628 def primary_key_definition(start_id) return "SERIAL(#{start_id}) PRIMARY KEY" end |
#query_offset_limit(sql, offset, limit) ⇒ Object
Handling offset/limit as per Informix requirements
2714 2715 2716 2717 2718 2719 2720 2721 2722 2723 2724 2725 2726 2727 2728 |
# File 'lib/active_record/connection_adapters/ibm_db_adapter.rb', line 2714 def query_offset_limit(sql, offset, limit) if limit != 0 if !offset.nil? # Modifying the SQL to utilize the skip and limit amounts sql.gsub!(/SELECT/i,"SELECT SKIP #{offset} LIMIT #{limit}") else # Modifying the SQL to retrieve only the first #{limit} rows sql = sql.gsub!("SELECT","SELECT FIRST #{limit}") end else # Modifying the SQL to ensure that no rows will be returned sql.gsub!(/SELECT/i,"SELECT * FROM (SELECT") sql << ") WHERE 0 = 1" end end |
#query_offset_limit!(sql, offset, limit, options) ⇒ Object
Handling offset/limit as per Informix requirements
2731 2732 2733 2734 2735 2736 2737 2738 2739 2740 2741 2742 2743 2744 2745 |
# File 'lib/active_record/connection_adapters/ibm_db_adapter.rb', line 2731 def query_offset_limit!(sql, offset, limit, ) if limit != 0 if !offset.nil? # Modifying the SQL to utilize the skip and limit amounts sql.gsub!(/SELECT/i,"SELECT SKIP #{offset} LIMIT #{limit}") else # Modifying the SQL to retrieve only the first #{limit} rows sql = sql.gsub!("SELECT","SELECT FIRST #{limit}") end else # Modifying the SQL to ensure that no rows will be returned sql.gsub!(/SELECT/i,"SELECT * FROM (SELECT") sql << ") WHERE 0 = 1" end end |
#rename_column(table_name, column_name, new_column_name) ⇒ Object
IDS specific ALTER TABLE statement to rename a column
2594 2595 2596 2597 2598 2599 2600 2601 2602 2603 2604 2605 2606 2607 2608 2609 2610 2611 2612 2613 2614 2615 2616 2617 2618 2619 2620 2621 2622 2623 2624 2625 2626 |
# File 'lib/active_record/connection_adapters/ibm_db_adapter.rb', line 2594 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? empty_condition = _table_name.empty? || _column_name.empty? || _new_column_name.empty? unless nil_condition 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 ) if error_msg && !error_msg.empty? raise "Rename column failed : #{error_msg}" else raise StandardError.new('An unexpected error occurred during renaming the column') end 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
2692 2693 2694 |
# File 'lib/active_record/connection_adapters/ibm_db_adapter.rb', line 2692 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.”
2760 2761 2762 2763 2764 |
# File 'lib/active_record/connection_adapters/ibm_db_adapter.rb', line 2760 def set_binary_default(value) unless (value == 'NULL') raise "Informix Dynamic Server only allows NULL as a valid default value for a BLOB data type" end end |
#set_binary_value ⇒ Object
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)
2769 2770 2771 |
# File 'lib/active_record/connection_adapters/ibm_db_adapter.rb', line 2769 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
2784 2785 2786 |
# File 'lib/active_record/connection_adapters/ibm_db_adapter.rb', line 2784 def set_case(value) value.downcase end |
#set_schema(schema) ⇒ Object
IDS does not support the SET SCHEMA syntax
2590 2591 |
# File 'lib/active_record/connection_adapters/ibm_db_adapter.rb', line 2590 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.”
2776 2777 2778 2779 2780 |
# File 'lib/active_record/connection_adapters/ibm_db_adapter.rb', line 2776 def set_text_default(value) unless (value == 'NULL') raise "Informix Dynamic Server only allows NULL as a valid default value for a CLOB data type" end end |