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

cast_type_literal(type) click to toggle source

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.

Calls superclass method
# File lib/sequel/adapters/jdbc/derby.rb, line 28
def cast_type_literal(type)
  (type == String) ? 'CHAR(254)' : super
end
database_type() click to toggle source

Derby uses the :derby database type.

# File lib/sequel/adapters/jdbc/derby.rb, line 33
def database_type
  :derby
end
freeze() click to toggle source
Calls superclass method Sequel::JDBC::Transactions#freeze
# File lib/sequel/adapters/jdbc/derby.rb, line 37
def freeze
  svn_version
  super
end
serial_primary_key_options() click to toggle source

Derby uses an IDENTITY sequence for autoincrementing columns.

# File lib/sequel/adapters/jdbc/derby.rb, line 43
def serial_primary_key_options
  {:primary_key => true, :type => :integer, :identity=>true, :start_with=>1}
end
supports_transactional_ddl?() click to toggle source

Derby supports transaction DDL statements.

# File lib/sequel/adapters/jdbc/derby.rb, line 57
def supports_transactional_ddl?
  true
end
svn_version() click to toggle source

The SVN version of the database.

# File lib/sequel/adapters/jdbc/derby.rb, line 48
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

_table_exists?(ds) click to toggle source

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 65
def _table_exists?(ds)
  ds.first
end
alter_table_sql(table, op) click to toggle source

Derby-specific syntax for renaming columns and changing a columns type/nullity.

Calls superclass method
# File lib/sequel/adapters/jdbc/derby.rb, line 70
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
can_add_primary_key_constraint_on_nullable_columns?() click to toggle source

Derby does not allow adding primary key constraints to NULLable columns.

# File lib/sequel/adapters/jdbc/derby.rb, line 90
def can_add_primary_key_constraint_on_nullable_columns?
  false
end
column_definition_null_sql(sql, column) click to toggle source

Derby doesn't allow specifying NULL for columns, only NOT NULL.

# File lib/sequel/adapters/jdbc/derby.rb, line 95
def column_definition_null_sql(sql, column)
  null = column.fetch(:null, column[:allow_null])
  sql << " NOT NULL" if null == false || (null.nil? && column[:primary_key])
end
create_table_as(name, sql, options) click to toggle source

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.

Calls superclass method
# File lib/sequel/adapters/jdbc/derby.rb, line 109
def create_table_as(name, sql, options)
  super
  from(name).insert(sql.is_a?(Dataset) ? sql : dataset.with_sql(sql))
end
create_table_as_sql(name, sql, options) click to toggle source

Derby currently only requires WITH NO DATA, with a separate insert to import data.

# File lib/sequel/adapters/jdbc/derby.rb, line 116
def create_table_as_sql(name, sql, options)
  "#{create_table_prefix_sql(name, options)} AS #{sql} WITH NO DATA"
end
create_table_prefix_sql(name, options) click to toggle source

Temporary table creation on Derby uses DECLARE instead of CREATE.

Calls superclass method
# File lib/sequel/adapters/jdbc/derby.rb, line 121
def create_table_prefix_sql(name, options)
  if options[:temp]
    "DECLARE GLOBAL TEMPORARY TABLE #{quote_identifier(name)}"
  else
    super
  end
end
create_table_sql(name, generator, options) click to toggle source

Add NOT LOGGED for temporary tables to improve performance.

Calls superclass method
# File lib/sequel/adapters/jdbc/derby.rb, line 101
def create_table_sql(name, generator, options)
  s = super
  s += ' NOT LOGGED' if options[:temp]
  s
end
database_error_regexps() click to toggle source
# File lib/sequel/adapters/jdbc/derby.rb, line 136
def database_error_regexps
  DATABASE_ERROR_REGEXPS
end
last_insert_id(conn, opts=OPTS) click to toggle source

Use IDENTITY_VAL_LOCAL() to get the last inserted id.

# File lib/sequel/adapters/jdbc/derby.rb, line 141
def last_insert_id(conn, opts=OPTS)
  statement(conn) do |stmt|
    sql = 'SELECT IDENTITY_VAL_LOCAL() FROM sysibm.sysdummy1'
    rs = log_connection_yield(sql, conn){stmt.executeQuery(sql)}
    rs.next
    rs.getLong(1)
  end
end
primary_key_index_re() click to toggle source

Primary key indexes appear to be named sqlNNNN on Derby

# File lib/sequel/adapters/jdbc/derby.rb, line 161
def primary_key_index_re
  PRIMARY_KEY_INDEX_RE
end
rename_table_sql(name, new_name) click to toggle source

Derby uses RENAME TABLE syntax to rename tables.

# File lib/sequel/adapters/jdbc/derby.rb, line 156
def rename_table_sql(name, new_name)
  "RENAME TABLE #{quote_schema_table(name)} TO #{quote_schema_table(new_name)}"
end
set_ps_arg_nil(cps, i) click to toggle source

Handle nil values by using setNull with the correct parameter type.

# File lib/sequel/adapters/jdbc/derby.rb, line 151
def set_ps_arg_nil(cps, i)
  cps.setNull(i, cps.getParameterMetaData.getParameterType(i))
end
type_literal(column) click to toggle source

If an :identity option is present in the column, add the necessary IDENTITY SQL.

Calls superclass method
# File lib/sequel/adapters/jdbc/derby.rb, line 166
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
uses_clob_for_text?() click to toggle source

Derby uses clob for text types.

# File lib/sequel/adapters/jdbc/derby.rb, line 181
def uses_clob_for_text?
  true
end
valid_connection_sql() click to toggle source

The SQL query to issue to check if a connection is valid.

# File lib/sequel/adapters/jdbc/derby.rb, line 186
def valid_connection_sql
  @valid_connection_sql ||= select(1).sql
end