# File lib/sequel/adapters/jdbc/derby.rb, line 149 def primary_key_index_re PRIMARY_KEY_INDEX_RE end
module Sequel::JDBC::Derby::DatabaseMethods
Instance methods for Derby Database objects accessed via JDBC.
Constants
- DATABASE_ERROR_REGEXPS
- PRIMARY_KEY_INDEX_RE
Public Instance Methods
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.
# File lib/sequel/adapters/jdbc/derby.rb, line 27 def cast_type_literal(type) (type == String) ? 'CHAR(254)' : super end
Derby uses the :derby database type.
# File lib/sequel/adapters/jdbc/derby.rb, line 32 def database_type :derby end
Derby uses an IDENTITY sequence for autoincrementing columns.
# File lib/sequel/adapters/jdbc/derby.rb, line 37 def serial_primary_key_options {:primary_key => true, :type => :integer, :identity=>true, :start_with=>1} end
Derby supports transaction DDL statements.
# File lib/sequel/adapters/jdbc/derby.rb, line 51 def supports_transactional_ddl? true end
The SVN version of the database.
# File lib/sequel/adapters/jdbc/derby.rb, line 42 def svn_version @svn_version ||= begin v = synchronize{|c| c.get_meta_data.get_database_product_version} v =~ /\((\d+)\)\z/ $1.to_i end end
Private Instance Methods
Derby optimizes away Sequel's default check of SELECT NULL FROM table, so use a SELECT * FROM table there.
# File lib/sequel/adapters/jdbc/derby.rb, line 59 def _table_exists?(ds) ds.first end
Derby-specific syntax for renaming columns and changing a columns type/nullity.
# File lib/sequel/adapters/jdbc/derby.rb, line 64 def alter_table_sql(table, op) case op[:op] when :rename_column "RENAME COLUMN #{quote_schema_table(table)}.#{quote_identifier(op[:name])} TO #{quote_identifier(op[:new_name])}" when :set_column_type # Derby is very limited in changing a columns type, so adding a new column and then dropping the existing column is # the best approach, as mentioned in the Derby documentation. temp_name = :x_sequel_temp_column_x [alter_table_sql(table, op.merge(:op=>:add_column, :name=>temp_name)), from(table).update_sql(temp_name=>::Sequel::SQL::Cast.new(op[:name], op[:type])), alter_table_sql(table, op.merge(:op=>:drop_column)), alter_table_sql(table, op.merge(:op=>:rename_column, :name=>temp_name, :new_name=>op[:name]))] when :set_column_null "ALTER TABLE #{quote_schema_table(table)} ALTER COLUMN #{quote_identifier(op[:name])} #{op[:null] ? 'NULL' : 'NOT NULL'}" else super end end
Derby doesn't allow specifying NULL for columns, only NOT NULL.
# File lib/sequel/adapters/jdbc/derby.rb, line 84 def column_definition_null_sql(sql, column) sql << " NOT NULL" if column.fetch(:null, column[:allow_null]) == false 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.
# File lib/sequel/adapters/jdbc/derby.rb, line 97 def create_table_as(name, sql, options) super from(name).insert(sql.is_a?(Dataset) ? sql : dataset.with_sql(sql)) end
Derby currently only requires WITH NO DATA, with a separate insert to import data.
# File lib/sequel/adapters/jdbc/derby.rb, line 104 def create_table_as_sql(name, sql, options) "#{create_table_prefix_sql(name, options)} AS #{sql} WITH NO DATA" end
Temporary table creation on Derby uses DECLARE instead of CREATE.
# File lib/sequel/adapters/jdbc/derby.rb, line 109 def create_table_prefix_sql(name, options) if options[:temp] "DECLARE GLOBAL TEMPORARY TABLE #{quote_identifier(name)}" else super end end
Add NOT LOGGED for temporary tables to improve performance.
# File lib/sequel/adapters/jdbc/derby.rb, line 89 def create_table_sql(name, generator, options) s = super s << ' NOT LOGGED' if options[:temp] s end
# File lib/sequel/adapters/jdbc/derby.rb, line 124 def database_error_regexps DATABASE_ERROR_REGEXPS end
Use IDENTITY_VAL_LOCAL() to get the last inserted id.
# File lib/sequel/adapters/jdbc/derby.rb, line 129 def last_insert_id(conn, opts=OPTS) statement(conn) do |stmt| sql = 'SELECT IDENTITY_VAL_LOCAL() FROM sysibm.sysdummy1' rs = log_yield(sql){stmt.executeQuery(sql)} rs.next rs.getInt(1) end end
Primary key indexes appear to be named sqlNNNN on Derby
Derby uses RENAME TABLE syntax to rename tables.
# File lib/sequel/adapters/jdbc/derby.rb, line 144 def rename_table_sql(name, new_name) "RENAME TABLE #{quote_schema_table(name)} TO #{quote_schema_table(new_name)}" end
Handle nil values by using setNull with the correct parameter type.
# File lib/sequel/adapters/jdbc/derby.rb, line 139 def set_ps_arg_nil(cps, i) cps.setNull(i, cps.getParameterMetaData.getParameterType(i)) end
If an :identity option is present in the column, add the necessary IDENTITY SQL.
# File lib/sequel/adapters/jdbc/derby.rb, line 154 def type_literal(column) if column[:identity] sql = "#{super} GENERATED BY DEFAULT AS IDENTITY" if sw = column[:start_with] sql << " (START WITH #{sw.to_i}" sql << " INCREMENT BY #{column[:increment_by].to_i}" if column[:increment_by] sql << ")" end sql else super end end
Derby uses clob for text types.
# File lib/sequel/adapters/jdbc/derby.rb, line 169 def uses_clob_for_text? true end
The SQL query to issue to check if a connection is valid.
# File lib/sequel/adapters/jdbc/derby.rb, line 174 def valid_connection_sql @valid_connection_sql ||= select(1).sql end