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

Derby uses an IDENTITY sequence for autoincrementing columns.

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

Derby supports transactional DDL statements.

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

The SVN version of the database.

   # File lib/sequel/adapters/jdbc/derby.rb
46 def svn_version
47   @svn_version ||= begin
48     v = synchronize{|c| c.get_meta_data.get_database_product_version}
49     v =~ /\((\d+)\)\z/
50     $1.to_i
51   end
52 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
63 def _table_exists?(ds)
64   ds.first
65 end
alter_table_sql(table, op) click to toggle source
Calls superclass method
   # File lib/sequel/adapters/jdbc/derby.rb
67 def alter_table_sql(table, op)
68   case op[:op]
69   when :rename_column
70     "RENAME COLUMN #{quote_schema_table(table)}.#{quote_identifier(op[:name])} TO #{quote_identifier(op[:new_name])}"
71   when :set_column_type
72     # Derby is very limited in changing a columns type, so adding a new column and then dropping the existing column is
73     # the best approach, as mentioned in the Derby documentation.
74     temp_name = :x_sequel_temp_column_x
75     [alter_table_sql(table, op.merge(:op=>:add_column, :name=>temp_name)),
76      from(table).update_sql(temp_name=>::Sequel::SQL::Cast.new(op[:name], op[:type])),
77      alter_table_sql(table, op.merge(:op=>:drop_column)),
78      alter_table_sql(table, op.merge(:op=>:rename_column, :name=>temp_name, :new_name=>op[:name]))]
79   when :set_column_null
80     "ALTER TABLE #{quote_schema_table(table)} ALTER COLUMN #{quote_identifier(op[:name])} #{op[:null] ? 'NULL' : 'NOT NULL'}"
81   else
82     super
83   end
84 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
87 def can_add_primary_key_constraint_on_nullable_columns?
88   false
89 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
92 def column_definition_null_sql(sql, column)
93   null = column.fetch(:null, column[:allow_null])
94   sql << " NOT NULL" if null == false || (null.nil? && column[:primary_key])
95 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
106 def create_table_as(name, sql, options)
107   super
108   from(name).insert(sql.is_a?(Dataset) ? sql : dataset.with_sql(sql))
109 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
113 def create_table_as_sql(name, sql, options)
114   "#{create_table_prefix_sql(name, options)} AS #{sql} WITH NO DATA"
115 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
118 def create_table_prefix_sql(name, options)
119   if options[:temp]
120     "DECLARE GLOBAL TEMPORARY TABLE #{create_table_table_name_sql(name, options)}"
121   else
122     super
123   end
124 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
 98 def create_table_sql(name, generator, options)
 99   s = super
100   s += ' NOT LOGGED' if options[:temp]
101   s
102 end
database_error_regexps() click to toggle source
    # File lib/sequel/adapters/jdbc/derby.rb
133 def database_error_regexps
134   DATABASE_ERROR_REGEXPS
135 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
138 def last_insert_id(conn, opts=OPTS)
139   statement(conn) do |stmt|
140     sql = 'SELECT IDENTITY_VAL_LOCAL() FROM sysibm.sysdummy1'
141     rs = log_connection_yield(sql, conn){stmt.executeQuery(sql)}
142     rs.next
143     rs.getLong(1)
144   end
145 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
158 def primary_key_index_re
159   /\Asql\d+\z/i
160 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
153 def rename_table_sql(name, new_name)
154   "RENAME TABLE #{quote_schema_table(name)} TO #{quote_schema_table(new_name)}"
155 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
148 def set_ps_arg_nil(cps, i)
149   cps.setNull(i, cps.getParameterMetaData.getParameterType(i))
150 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
163 def type_literal(column)
164   if column[:identity]
165     sql = "#{super} GENERATED BY DEFAULT AS IDENTITY"
166     if sw = column[:start_with]
167       sql += " (START WITH #{sw.to_i}"
168       sql << " INCREMENT BY #{column[:increment_by].to_i}" if column[:increment_by]
169       sql << ")"
170     end
171     sql
172   else
173     super
174   end
175 end
uses_clob_for_text?() click to toggle source

Derby uses clob for text types.

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