# File lib/sequel/adapters/jdbc/derby.rb 158 def primary_key_index_re 159 /\Asql\d+\z/i 160 end
module Sequel::JDBC::Derby::DatabaseMethods
Constants
- DATABASE_ERROR_REGEXPS
Public Instance Methods
Source
# File lib/sequel/adapters/jdbc/derby.rb 27 def cast_type_literal(type) 28 (type == String) ? 'CHAR(254)' : super 29 end
Derby doesn’t support casting integer to varchar, only integer to char, and char(254) appears to have the widest support (with char(255) failing). This does add a bunch of extra spaces at the end, but those will be trimmed elsewhere.
Source
# File lib/sequel/adapters/jdbc/derby.rb 31 def database_type 32 :derby 33 end
Source
# File lib/sequel/adapters/jdbc/derby.rb 35 def freeze 36 svn_version 37 super 38 end
Sequel::JDBC::Transactions#freeze
Source
# File lib/sequel/adapters/jdbc/derby.rb 41 def serial_primary_key_options 42 {:primary_key => true, :type => Integer, :identity=>true, :start_with=>1} 43 end
Derby uses an IDENTITY sequence for autoincrementing columns.
Source
# File lib/sequel/adapters/jdbc/derby.rb 55 def supports_transactional_ddl? 56 true 57 end
Derby supports transactional DDL statements.
Source
# File lib/sequel/adapters/jdbc/derby.rb 46 def svn_version 47 @svn_version ||= begin 48 v = synchronize{|c| c.get_meta_data.get_database_product_version} 49 v =~ /\((\d+)\)\z/ 50 $1.to_i 51 end 52 end
The SVN version of the database.
Private Instance Methods
Source
# File lib/sequel/adapters/jdbc/derby.rb 63 def _table_exists?(ds) 64 ds.first 65 end
Derby optimizes away Sequel’s default check of SELECT NULL FROM table, so use a SELECT * FROM table there.
Source
# File lib/sequel/adapters/jdbc/derby.rb 67 def alter_table_sql(table, op) 68 case op[:op] 69 when :rename_column 70 "RENAME COLUMN #{quote_schema_table(table)}.#{quote_identifier(op[:name])} TO #{quote_identifier(op[:new_name])}" 71 when :set_column_type 72 # Derby is very limited in changing a columns type, so adding a new column and then dropping the existing column is 73 # the best approach, as mentioned in the Derby documentation. 74 temp_name = :x_sequel_temp_column_x 75 [alter_table_sql(table, op.merge(:op=>:add_column, :name=>temp_name)), 76 from(table).update_sql(temp_name=>::Sequel::SQL::Cast.new(op[:name], op[:type])), 77 alter_table_sql(table, op.merge(:op=>:drop_column)), 78 alter_table_sql(table, op.merge(:op=>:rename_column, :name=>temp_name, :new_name=>op[:name]))] 79 when :set_column_null 80 "ALTER TABLE #{quote_schema_table(table)} ALTER COLUMN #{quote_identifier(op[:name])} #{op[:null] ? 'NULL' : 'NOT NULL'}" 81 else 82 super 83 end 84 end
Source
# File lib/sequel/adapters/jdbc/derby.rb 87 def can_add_primary_key_constraint_on_nullable_columns? 88 false 89 end
Derby does not allow adding primary key constraints to NULLable columns.
Source
# File lib/sequel/adapters/jdbc/derby.rb 92 def column_definition_null_sql(sql, column) 93 null = column.fetch(:null, column[:allow_null]) 94 sql << " NOT NULL" if null == false || (null.nil? && column[:primary_key]) 95 end
Derby doesn’t allow specifying NULL for columns, only NOT NULL.
Source
# File lib/sequel/adapters/jdbc/derby.rb 106 def create_table_as(name, sql, options) 107 super 108 from(name).insert(sql.is_a?(Dataset) ? sql : dataset.with_sql(sql)) 109 end
Insert data from the current table into the new table after creating the table, since it is not possible to do it in one step.
Source
# File lib/sequel/adapters/jdbc/derby.rb 113 def create_table_as_sql(name, sql, options) 114 "#{create_table_prefix_sql(name, options)} AS #{sql} WITH NO DATA" 115 end
Derby currently only requires WITH NO DATA, with a separate insert to import data.
Source
# File lib/sequel/adapters/jdbc/derby.rb 118 def create_table_prefix_sql(name, options) 119 if options[:temp] 120 "DECLARE GLOBAL TEMPORARY TABLE #{create_table_table_name_sql(name, options)}" 121 else 122 super 123 end 124 end
Temporary table creation on Derby uses DECLARE instead of CREATE.
Source
# File lib/sequel/adapters/jdbc/derby.rb 98 def create_table_sql(name, generator, options) 99 s = super 100 s += ' NOT LOGGED' if options[:temp] 101 s 102 end
Add NOT LOGGED for temporary tables to improve performance.
Source
# File lib/sequel/adapters/jdbc/derby.rb 133 def database_error_regexps 134 DATABASE_ERROR_REGEXPS 135 end
Source
# File lib/sequel/adapters/jdbc/derby.rb 138 def last_insert_id(conn, opts=OPTS) 139 statement(conn) do |stmt| 140 sql = 'SELECT IDENTITY_VAL_LOCAL() FROM sysibm.sysdummy1' 141 rs = log_connection_yield(sql, conn){stmt.executeQuery(sql)} 142 rs.next 143 rs.getLong(1) 144 end 145 end
Use IDENTITY_VAL_LOCAL() to get the last inserted id.
Source
Primary key indexes appear to be named sqlNNNN on Derby
Source
# File lib/sequel/adapters/jdbc/derby.rb 153 def rename_table_sql(name, new_name) 154 "RENAME TABLE #{quote_schema_table(name)} TO #{quote_schema_table(new_name)}" 155 end
Derby uses RENAME TABLE syntax to rename tables.
Source
# File lib/sequel/adapters/jdbc/derby.rb 148 def set_ps_arg_nil(cps, i) 149 cps.setNull(i, cps.getParameterMetaData.getParameterType(i)) 150 end
Handle nil values by using setNull with the correct parameter type.
Source
# File lib/sequel/adapters/jdbc/derby.rb 163 def type_literal(column) 164 if column[:identity] 165 sql = "#{super} GENERATED BY DEFAULT AS IDENTITY" 166 if sw = column[:start_with] 167 sql += " (START WITH #{sw.to_i}" 168 sql << " INCREMENT BY #{column[:increment_by].to_i}" if column[:increment_by] 169 sql << ")" 170 end 171 sql 172 else 173 super 174 end 175 end
If an :identity option is present in the column, add the necessary IDENTITY SQL.
Source
# File lib/sequel/adapters/jdbc/derby.rb 178 def uses_clob_for_text? 179 true 180 end
Derby uses clob for text types.
Source
# File lib/sequel/adapters/jdbc/derby.rb 182 def valid_connection_sql 183 @valid_connection_sql ||= select(1).sql 184 end