# File lib/sequel/adapters/jdbc/derby.rb 156 def primary_key_index_re 157 /\Asql\d+\z/i 158 end
module Sequel::JDBC::Derby::DatabaseMethods
Constants
- DATABASE_ERROR_REGEXPS
Public Instance Methods
Source
# File lib/sequel/adapters/jdbc/derby.rb 25 def cast_type_literal(type) 26 (type == String) ? 'CHAR(254)' : super 27 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 29 def database_type 30 :derby 31 end
Source
# File lib/sequel/adapters/jdbc/derby.rb 33 def freeze 34 svn_version 35 super 36 end
Sequel::JDBC::Transactions#freeze
Source
# File lib/sequel/adapters/jdbc/derby.rb 39 def serial_primary_key_options 40 {:primary_key => true, :type => Integer, :identity=>true, :start_with=>1} 41 end
Derby
uses an IDENTITY sequence for autoincrementing columns.
Source
# File lib/sequel/adapters/jdbc/derby.rb 53 def supports_transactional_ddl? 54 true 55 end
Derby
supports transactional DDL statements.
Source
# File lib/sequel/adapters/jdbc/derby.rb 44 def svn_version 45 @svn_version ||= begin 46 v = synchronize{|c| c.get_meta_data.get_database_product_version} 47 v =~ /\((\d+)\)\z/ 48 $1.to_i 49 end 50 end
The SVN version of the database.
Private Instance Methods
Source
# File lib/sequel/adapters/jdbc/derby.rb 61 def _table_exists?(ds) 62 ds.first 63 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 65 def alter_table_sql(table, op) 66 case op[:op] 67 when :rename_column 68 "RENAME COLUMN #{quote_schema_table(table)}.#{quote_identifier(op[:name])} TO #{quote_identifier(op[:new_name])}" 69 when :set_column_type 70 # Derby is very limited in changing a columns type, so adding a new column and then dropping the existing column is 71 # the best approach, as mentioned in the Derby documentation. 72 temp_name = :x_sequel_temp_column_x 73 [alter_table_sql(table, op.merge(:op=>:add_column, :name=>temp_name)), 74 from(table).update_sql(temp_name=>::Sequel::SQL::Cast.new(op[:name], op[:type])), 75 alter_table_sql(table, op.merge(:op=>:drop_column)), 76 alter_table_sql(table, op.merge(:op=>:rename_column, :name=>temp_name, :new_name=>op[:name]))] 77 when :set_column_null 78 "ALTER TABLE #{quote_schema_table(table)} ALTER COLUMN #{quote_identifier(op[:name])} #{op[:null] ? 'NULL' : 'NOT NULL'}" 79 else 80 super 81 end 82 end
Source
# File lib/sequel/adapters/jdbc/derby.rb 85 def can_add_primary_key_constraint_on_nullable_columns? 86 false 87 end
Derby
does not allow adding primary key constraints to NULLable columns.
Source
# File lib/sequel/adapters/jdbc/derby.rb 90 def column_definition_null_sql(sql, column) 91 null = column.fetch(:null, column[:allow_null]) 92 sql << " NOT NULL" if null == false || (null.nil? && column[:primary_key]) 93 end
Derby
doesn’t allow specifying NULL for columns, only NOT NULL.
Source
# File lib/sequel/adapters/jdbc/derby.rb 104 def create_table_as(name, sql, options) 105 super 106 from(name).insert(sql.is_a?(Dataset) ? sql : dataset.with_sql(sql)) 107 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 111 def create_table_as_sql(name, sql, options) 112 "#{create_table_prefix_sql(name, options)} AS #{sql} WITH NO DATA" 113 end
Derby
currently only requires WITH NO DATA, with a separate insert to import data.
Source
# File lib/sequel/adapters/jdbc/derby.rb 116 def create_table_prefix_sql(name, options) 117 if options[:temp] 118 "DECLARE GLOBAL TEMPORARY TABLE #{create_table_table_name_sql(name, options)}" 119 else 120 super 121 end 122 end
Temporary table creation on Derby
uses DECLARE instead of CREATE.
Source
# File lib/sequel/adapters/jdbc/derby.rb 96 def create_table_sql(name, generator, options) 97 s = super 98 s += ' NOT LOGGED' if options[:temp] 99 s 100 end
Add NOT LOGGED for temporary tables to improve performance.
Source
# File lib/sequel/adapters/jdbc/derby.rb 131 def database_error_regexps 132 DATABASE_ERROR_REGEXPS 133 end
Source
# File lib/sequel/adapters/jdbc/derby.rb 136 def last_insert_id(conn, opts=OPTS) 137 statement(conn) do |stmt| 138 sql = 'SELECT IDENTITY_VAL_LOCAL() FROM sysibm.sysdummy1' 139 rs = log_connection_yield(sql, conn){stmt.executeQuery(sql)} 140 rs.next 141 rs.getLong(1) 142 end 143 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 151 def rename_table_sql(name, new_name) 152 "RENAME TABLE #{quote_schema_table(name)} TO #{quote_schema_table(new_name)}" 153 end
Derby
uses RENAME TABLE syntax to rename tables.
Source
# File lib/sequel/adapters/jdbc/derby.rb 146 def set_ps_arg_nil(cps, i) 147 cps.setNull(i, cps.getParameterMetaData.getParameterType(i)) 148 end
Handle nil values by using setNull with the correct parameter type.
Source
# File lib/sequel/adapters/jdbc/derby.rb 161 def type_literal(column) 162 if column[:identity] 163 sql = "#{super} GENERATED BY DEFAULT AS IDENTITY" 164 if sw = column[:start_with] 165 sql += " (START WITH #{sw.to_i}" 166 sql << " INCREMENT BY #{column[:increment_by].to_i}" if column[:increment_by] 167 sql << ")" 168 end 169 sql 170 else 171 super 172 end 173 end
If an :identity option is present in the column, add the necessary IDENTITY SQL
.
Source
# File lib/sequel/adapters/jdbc/derby.rb 176 def uses_clob_for_text? 177 true 178 end
Derby
uses clob for text types.
Source
# File lib/sequel/adapters/jdbc/derby.rb 180 def valid_connection_sql 181 @valid_connection_sql ||= select(1).sql 182 end