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

Derby uses an IDENTITY sequence for autoincrementing columns.

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

Derby supports transactional DDL statements.

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

The SVN version of the database.

   # File lib/sequel/adapters/jdbc/derby.rb
43 def svn_version
44   @svn_version ||= begin
45     v = synchronize{|c| c.get_meta_data.get_database_product_version}
46     v =~ /\((\d+)\)\z/
47     $1.to_i
48   end
49 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
60 def _table_exists?(ds)
61   ds.first
62 end
alter_table_sql(table, op) click to toggle source
Calls superclass method
   # File lib/sequel/adapters/jdbc/derby.rb
64 def alter_table_sql(table, op)
65   case op[:op]
66   when :rename_column
67     "RENAME COLUMN #{quote_schema_table(table)}.#{quote_identifier(op[:name])} TO #{quote_identifier(op[:new_name])}"
68   when :set_column_type
69     # Derby is very limited in changing a columns type, so adding a new column and then dropping the existing column is
70     # the best approach, as mentioned in the Derby documentation.
71     temp_name = :x_sequel_temp_column_x
72     [alter_table_sql(table, op.merge(:op=>:add_column, :name=>temp_name)),
73      from(table).update_sql(temp_name=>::Sequel::SQL::Cast.new(op[:name], op[:type])),
74      alter_table_sql(table, op.merge(:op=>:drop_column)),
75      alter_table_sql(table, op.merge(:op=>:rename_column, :name=>temp_name, :new_name=>op[:name]))]
76   when :set_column_null
77     "ALTER TABLE #{quote_schema_table(table)} ALTER COLUMN #{quote_identifier(op[:name])} #{op[:null] ? 'NULL' : 'NOT NULL'}"
78   else
79     super
80   end
81 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
84 def can_add_primary_key_constraint_on_nullable_columns?
85   false
86 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
89 def column_definition_null_sql(sql, column)
90   null = column.fetch(:null, column[:allow_null])
91   sql << " NOT NULL" if null == false || (null.nil? && column[:primary_key])
92 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
103 def create_table_as(name, sql, options)
104   super
105   from(name).insert(sql.is_a?(Dataset) ? sql : dataset.with_sql(sql))
106 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
110 def create_table_as_sql(name, sql, options)
111   "#{create_table_prefix_sql(name, options)} AS #{sql} WITH NO DATA"
112 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
115 def create_table_prefix_sql(name, options)
116   if options[:temp]
117     "DECLARE GLOBAL TEMPORARY TABLE #{quote_identifier(name)}"
118   else
119     super
120   end
121 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
95 def create_table_sql(name, generator, options)
96   s = super
97   s += ' NOT LOGGED' if options[:temp]
98   s
99 end
database_error_regexps() click to toggle source
    # File lib/sequel/adapters/jdbc/derby.rb
130 def database_error_regexps
131   DATABASE_ERROR_REGEXPS
132 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
135 def last_insert_id(conn, opts=OPTS)
136   statement(conn) do |stmt|
137     sql = 'SELECT IDENTITY_VAL_LOCAL() FROM sysibm.sysdummy1'
138     rs = log_connection_yield(sql, conn){stmt.executeQuery(sql)}
139     rs.next
140     rs.getLong(1)
141   end
142 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
155 def primary_key_index_re
156   /\Asql\d+\z/i
157 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
150 def rename_table_sql(name, new_name)
151   "RENAME TABLE #{quote_schema_table(name)} TO #{quote_schema_table(new_name)}"
152 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
145 def set_ps_arg_nil(cps, i)
146   cps.setNull(i, cps.getParameterMetaData.getParameterType(i))
147 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
160 def type_literal(column)
161   if column[:identity]
162     sql = "#{super} GENERATED BY DEFAULT AS IDENTITY"
163     if sw = column[:start_with]
164       sql += " (START WITH #{sw.to_i}"
165       sql << " INCREMENT BY #{column[:increment_by].to_i}" if column[:increment_by]
166       sql << ")"
167     end
168     sql
169   else
170     super
171   end
172 end
uses_clob_for_text?() click to toggle source

Derby uses clob for text types.

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