module Sequel::JDBC::Derby::DatabaseMethods

Constants

DATABASE_ERROR_REGEXPS

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
25 def cast_type_literal(type)
26   (type == String) ? 'CHAR(254)' : super
27 end
database_type() click to toggle source
   # File lib/sequel/adapters/jdbc/derby.rb
29 def database_type
30   :derby
31 end
freeze() click to toggle source
Calls superclass method Sequel::JDBC::Transactions#freeze
   # File lib/sequel/adapters/jdbc/derby.rb
33 def freeze
34   svn_version
35   super
36 end
serial_primary_key_options() click to toggle source

Derby uses an IDENTITY sequence for autoincrementing columns.

   # 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
supports_transactional_ddl?() click to toggle source

Derby supports transactional DDL statements.

   # File lib/sequel/adapters/jdbc/derby.rb
53 def supports_transactional_ddl?
54   true
55 end
svn_version() click to toggle source

The SVN version of the database.

   # 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

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
61 def _table_exists?(ds)
62   ds.first
63 end
alter_table_sql(table, op) click to toggle source
Calls superclass method
   # 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
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
85 def can_add_primary_key_constraint_on_nullable_columns?
86   false
87 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
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
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
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
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
111 def create_table_as_sql(name, sql, options)
112   "#{create_table_prefix_sql(name, options)} AS #{sql} WITH NO DATA"
113 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
116 def create_table_prefix_sql(name, options)
117   if options[:temp]
118     "DECLARE GLOBAL TEMPORARY TABLE #{quote_identifier(name)}"
119   else
120     super
121   end
122 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
 96 def create_table_sql(name, generator, options)
 97   s = super
 98   s += ' NOT LOGGED' if options[:temp]
 99   s
100 end
database_error_regexps() click to toggle source
    # File lib/sequel/adapters/jdbc/derby.rb
131 def database_error_regexps
132   DATABASE_ERROR_REGEXPS
133 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
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
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
156 def primary_key_index_re
157   /\Asql\d+\z/i
158 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
151 def rename_table_sql(name, new_name)
152   "RENAME TABLE #{quote_schema_table(name)} TO #{quote_schema_table(new_name)}"
153 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
146 def set_ps_arg_nil(cps, i)
147   cps.setNull(i, cps.getParameterMetaData.getParameterType(i))
148 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
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
uses_clob_for_text?() click to toggle source

Derby uses clob for text types.

    # File lib/sequel/adapters/jdbc/derby.rb
176 def uses_clob_for_text?
177   true
178 end
valid_connection_sql() click to toggle source
    # File lib/sequel/adapters/jdbc/derby.rb
180 def valid_connection_sql
181   @valid_connection_sql ||= select(1).sql
182 end