Module: ArJdbc::Derby
- Includes:
- Util::TableCopier
- Defined in:
- lib/arjdbc/derby/adapter.rb,
lib/arjdbc/derby/schema_creation.rb
Defined Under Namespace
Modules: Column Classes: SchemaCreation
Constant Summary collapse
- ADAPTER_NAME =
'Derby'.freeze
- NATIVE_DATABASE_TYPES =
{ :primary_key => "int GENERATED BY DEFAULT AS identity NOT NULL PRIMARY KEY", :string => { :name => "varchar", :limit => 255 }, # 32672 :text => { :name => "clob" }, # 2,147,483,647 :char => { :name => "char", :limit => 254 }, # JDBC :limit => 254 :binary => { :name => "blob" }, # 2,147,483,647 :float => { :name => "float", :limit => 8 }, # DOUBLE PRECISION :real => { :name => "real", :limit => 4 }, # JDBC :limit => 23 :double => { :name => "double", :limit => 8 }, # JDBC :limit => 52 :decimal => { :name => "decimal", :precision => 5, :scale => 0 }, # JDBC :limit => 31 :numeric => { :name => "decimal", :precision => 5, :scale => 0 }, # JDBC :limit => 31 :integer => { :name => "integer", :limit => 4 }, # JDBC :limit => 10 :smallint => { :name => "smallint", :limit => 2 }, # JDBC :limit => 5 :bigint => { :name => "bigint", :limit => 8 }, # JDBC :limit => 19 :date => { :name => "date" }, :time => { :name => "time" }, :datetime => { :name => "timestamp" }, :timestamp => { :name => "timestamp" }, :xml => { :name => "xml" }, :boolean => { :name => "smallint", :limit => 1 }, # TODO boolean (since 10.7) :object => { :name => "object" }, }
Class Method Summary collapse
- .column_selector ⇒ Object
-
.emulate_booleans ⇒ Object
deprecated
Deprecated.
Use #emulate_booleans? instead.
- .emulate_booleans=(emulate) ⇒ Object
-
.emulate_booleans? ⇒ Boolean
Boolean emulation can be disabled using :.
- .jdbc_connection_class ⇒ Object
Instance Method Summary collapse
- #adapter_name ⇒ Object
- #add_column(table_name, column_name, type, options = {}) ⇒ Object
- #add_column_options!(sql, options) ⇒ Object
- #add_limit_offset!(sql, options) ⇒ Object
- #change_column(table_name, column_name, type, options = {}) ⇒ Object
- #columns_for_distinct(columns, orders) ⇒ Object
- #configure_connection ⇒ Object
-
#create_savepoint(name = current_savepoint_name(true)) ⇒ Object
Ensure the savepoint name is unused before creating it.
-
#create_schema(schema) ⇒ Object
Creates a new Derby schema.
-
#current_schema ⇒ String
The current schema name.
-
#distinct(columns, order_by) ⇒ Object
SELECT DISTINCT clause for a given set of columns and a given ORDER BY clause.
-
#drop_schema(schema) ⇒ Object
Drops an existing schema, needs to be empty (no DB objects).
- #empty_insert_statement_value ⇒ Object
- #execute(sql, name = nil, binds = []) ⇒ Object
-
#existing_savepoint_name?(name) ⇒ Boolean
UNTESTED.
- #index_name_length ⇒ Object
-
#last_insert_id ⇒ Fixnum
Returns the value of an identity column of the last INSERT statement made over this connection.
- #native_database_types ⇒ Object
- #primary_keys(table_name) ⇒ Object
- #quote(value, column = nil) ⇒ Object
- #quote_column_name(name) ⇒ Object
- #quote_table_name_for_assignment(table, attr) ⇒ Object
- #quoted_date(value) ⇒ Object
- #remove_column(table_name, *column_names) ⇒ Object (also: #remove_columns)
- #remove_index(table_name, options) ⇒ Object
- #rename_column(table_name, column_name, new_column_name) ⇒ Object
- #rename_table(name, new_name) ⇒ Object
- #reset_column_information ⇒ Object
- #reset_pk_sequence!(table, pk = nil, sequence = nil) ⇒ Object
-
#reset_sequence!(table, column, sequence = nil) ⇒ Object
Set the sequence to the max value of the table's column.
-
#set_schema(schema) ⇒ Object
(also: #current_schema=)
Change the current (implicit) Derby schema to be used for this connection.
- #supports_ddl_transactions? ⇒ Boolean
- #supports_foreign_keys? ⇒ Boolean
- #table_definition(*args) ⇒ Object
- #tables(name = nil) ⇒ Object
- #truncate(table_name, name = nil) ⇒ Object
-
#type_to_sql(type, limit = nil, precision = nil, scale = nil) ⇒ Object
Convert the specified column type to a SQL string.
Methods included from Util::TableCopier
#alter_table, #copy_table, #copy_table_contents, #copy_table_indexes, #move_table
Class Method Details
.column_selector ⇒ Object
25 26 27 |
# File 'lib/arjdbc/derby/adapter.rb', line 25 def self.column_selector [ /derby/i, lambda { |config, column| column.extend(Column) } ] end |
.emulate_booleans ⇒ Object
Use #emulate_booleans? instead.
38 |
# File 'lib/arjdbc/derby/adapter.rb', line 38 def self.emulate_booleans; @@emulate_booleans; end |
.emulate_booleans=(emulate) ⇒ Object
40 |
# File 'lib/arjdbc/derby/adapter.rb', line 40 def self.emulate_booleans=(emulate); @@emulate_booleans = emulate; end |
.emulate_booleans? ⇒ Boolean
Boolean emulation can be disabled using :
ArJdbc::Derby.emulate_booleans = false
36 |
# File 'lib/arjdbc/derby/adapter.rb', line 36 def self.emulate_booleans?; @@emulate_booleans; end |
.jdbc_connection_class ⇒ Object
20 21 22 |
# File 'lib/arjdbc/derby/adapter.rb', line 20 def self.jdbc_connection_class ::ActiveRecord::ConnectionAdapters::DerbyJdbcConnection end |
Instance Method Details
#adapter_name ⇒ Object
80 81 82 |
# File 'lib/arjdbc/derby/adapter.rb', line 80 def adapter_name ADAPTER_NAME end |
#add_column(table_name, column_name, type, options = {}) ⇒ Object
269 270 271 272 273 |
# File 'lib/arjdbc/derby/adapter.rb', line 269 def add_column(table_name, column_name, type, = {}) add_column_sql = "ALTER TABLE #{quote_table_name(table_name)} ADD #{quote_column_name(column_name)} #{type_to_sql(type, [:limit], [:precision], [:scale])}" (add_column_sql, ) execute(add_column_sql) end |
#add_column_options!(sql, options) ⇒ Object
276 277 278 279 280 |
# File 'lib/arjdbc/derby/adapter.rb', line 276 def (sql, ) .delete(:default) if .has_key?(:default) && [:default].nil? sql << " DEFAULT #{quote(.delete(:default))}" if .has_key?(:default) super end |
#add_limit_offset!(sql, options) ⇒ Object
Only used with (non-AREL) ActiveRecord 2.3.
462 463 464 465 466 |
# File 'lib/arjdbc/derby/adapter.rb', line 462 def add_limit_offset!(sql, ) sql << " OFFSET #{[:offset]} ROWS" if [:offset] # ROWS/ROW and FIRST/NEXT mean the same sql << " FETCH FIRST #{[:limit]} ROWS ONLY" if [:limit] end |
#change_column(table_name, column_name, type, options = {}) ⇒ Object
307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 |
# File 'lib/arjdbc/derby/adapter.rb', line 307 def change_column(table_name, column_name, type, = {}) # TODO this needs a review since now we're likely to be on >= 10.8 # Notes about changing in Derby: # http://db.apache.org/derby/docs/10.2/ref/rrefsqlj81859.html#rrefsqlj81859__rrefsqlj37860) # # We support changing columns using the strategy outlined in: # https://issues.apache.org/jira/browse/DERBY-1515 # # This feature has not made it into a formal release and is not in Java 6. # We will need to conditionally support this (supposed to arrive for 10.3.0.0). # null/not nulling is easy, handle that separately if .include?(:null) # This seems to only work with 10.2 of Derby if .delete(:null) == false execute "ALTER TABLE #{quote_table_name(table_name)} ALTER COLUMN #{quote_column_name(column_name)} NOT NULL" else execute "ALTER TABLE #{quote_table_name(table_name)} ALTER COLUMN #{quote_column_name(column_name)} NULL" end end # anything left to do? unless .empty? begin execute "ALTER TABLE #{quote_table_name(table_name)} ALTER COLUMN " << " #{quote_column_name(column_name)} SET DATA TYPE #{type_to_sql(type, [:limit])}" rescue transaction do temp_new_column_name = "#{column_name}_newtype" # 1) ALTER TABLE t ADD COLUMN c1_newtype NEWTYPE; add_column table_name, temp_new_column_name, type, # 2) UPDATE t SET c1_newtype = c1; execute "UPDATE #{quote_table_name(table_name)} SET " << " #{quote_column_name(temp_new_column_name)} = " << " CAST(#{quote_column_name(column_name)} AS #{type_to_sql(type, [:limit])})" # 3) ALTER TABLE t DROP COLUMN c1; remove_column table_name, column_name # 4) ALTER TABLE t RENAME COLUMN c1_newtype to c1; rename_column table_name, temp_new_column_name, column_name end end end end |
#columns_for_distinct(columns, orders) ⇒ Object
372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 |
# File 'lib/arjdbc/derby/adapter.rb', line 372 def columns_for_distinct(columns, orders) return columns if orders.blank? # construct a clean list of column names from the ORDER BY clause, # removing any ASC/DESC modifiers order_columns = [ orders ]; order_columns.flatten! # AR 3.x vs 4.x order_columns.map! do |column| column = column.to_sql unless column.is_a?(String) # handle AREL node column.split(',').collect! { |s| s.split.first } end.flatten! order_columns.reject!(&:blank?) order_columns = order_columns.zip (0...order_columns.size).to_a order_columns = order_columns.map { |s, i| "#{s} AS alias_#{i}" } columns = [ columns ]; columns.flatten! columns.push( *order_columns ).join(', ') # return a DISTINCT clause that's distinct on the columns we want but # includes all the required columns for the ORDER BY to work properly end |
#configure_connection ⇒ Object
84 85 86 87 88 89 90 91 92 |
# File 'lib/arjdbc/derby/adapter.rb', line 84 def configure_connection # must be done or SELECT...FOR UPDATE won't work how we expect : if tx_isolation = config[:transaction_isolation] @connection.transaction_isolation = tx_isolation end # if a user name was specified upon connection, the user's name is the # default schema for the connection, if a schema with that name exists set_schema(config[:schema]) if config[:schema] end |
#create_savepoint(name = current_savepoint_name(true)) ⇒ Object
Ensure the savepoint name is unused before creating it.
150 151 152 153 |
# File 'lib/arjdbc/derby/adapter.rb', line 150 def create_savepoint(name = current_savepoint_name(true)) release_savepoint(name) if existing_savepoint_name?(name) super(name) end |
#create_schema(schema) ⇒ Object
Creates a new Derby schema.
427 428 429 |
# File 'lib/arjdbc/derby/adapter.rb', line 427 def create_schema(schema) execute "CREATE SCHEMA #{schema}", 'Create Schema' end |
#current_schema ⇒ String
Returns the current schema name.
413 414 415 416 |
# File 'lib/arjdbc/derby/adapter.rb', line 413 def current_schema @current_schema ||= select_value "SELECT CURRENT SCHEMA FROM SYS.SYSSCHEMAS FETCH FIRST 1 ROWS ONLY", 'SCHEMA' end |
#distinct(columns, order_by) ⇒ Object
This is based on distinct method for the PostgreSQL Adapter.
SELECT DISTINCT clause for a given set of columns and a given ORDER BY clause.
Derby requires the ORDER BY columns in the select list for distinct queries, and requires that the ORDER BY include the distinct column.
distinct("posts.id", "posts.created_at desc")
367 368 369 |
# File 'lib/arjdbc/derby/adapter.rb', line 367 def distinct(columns, order_by) "DISTINCT #{columns_for_distinct(columns, order_by)}" end |
#drop_schema(schema) ⇒ Object
Drops an existing schema, needs to be empty (no DB objects).
432 433 434 |
# File 'lib/arjdbc/derby/adapter.rb', line 432 def drop_schema(schema) execute "DROP SCHEMA #{schema} RESTRICT", 'Drop Schema' end |
#empty_insert_statement_value ⇒ Object
234 235 236 |
# File 'lib/arjdbc/derby/adapter.rb', line 234 def empty_insert_statement_value ::Arel::Visitors::Derby::VALUES_DEFAULT # Derby needs to know the columns end |
#execute(sql, name = nil, binds = []) ⇒ Object
469 470 471 472 473 474 475 |
# File 'lib/arjdbc/derby/adapter.rb', line 469 def execute(sql, name = nil, binds = []) sql = to_sql(sql, binds) insert = self.class.insert?(sql) update = ! insert && ! self.class.select?(sql) sql = correct_is_null(sql, insert || update) super(sql, name, binds) end |
#existing_savepoint_name?(name) ⇒ Boolean
UNTESTED
144 145 146 |
# File 'lib/arjdbc/derby/adapter.rb', line 144 def existing_savepoint_name?(name) (@connection.instance_variable_get('@savepoints') || {}).key? name end |
#index_name_length ⇒ Object
94 95 96 |
# File 'lib/arjdbc/derby/adapter.rb', line 94 def index_name_length 128 end |
#last_insert_id ⇒ Fixnum
Check the IDENTITY_VAL_LOCAL function for documentation.
Returns the value of an identity column of the last INSERT statement made over this connection.
481 482 483 |
# File 'lib/arjdbc/derby/adapter.rb', line 481 def last_insert_id @connection.identity_val_local end |
#native_database_types ⇒ Object
139 140 141 |
# File 'lib/arjdbc/derby/adapter.rb', line 139 def native_database_types NATIVE_DATABASE_TYPES end |
#primary_keys(table_name) ⇒ Object
393 394 395 |
# File 'lib/arjdbc/derby/adapter.rb', line 393 def primary_keys(table_name) @connection.primary_keys table_name.to_s.upcase end |
#quote(value, column = nil) ⇒ Object
156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 |
# File 'lib/arjdbc/derby/adapter.rb', line 156 def quote(value, column = nil) return super if column && ArJdbc::AR42 && column.cast_type.is_a?(ActiveRecord::Type::Serialized) return value.quoted_id if value.respond_to?(:quoted_id) return value if sql_literal?(value) return 'NULL' if value.nil? column_type = column && column.type if column_type == :string || column_type == :text # Derby is not permissive # e.g. sending an Integer to a VARCHAR column will fail case value when BigDecimal then value = value.to_s('F') when Numeric then value = value.to_s when true, false then value = value.to_s when Date, Time then value = quoted_date(value) else # on 2.3 attribute serialization needs to_yaml here value = value.to_s if ActiveRecord::VERSION::MAJOR >= 3 end end case value when String, ActiveSupport::Multibyte::Chars if column_type == :text "CAST('#{quote_string(value)}' AS CLOB)" elsif column_type == :binary "CAST(X'#{quote_binary(value)}' AS BLOB)" elsif column_type == :xml "XMLPARSE(DOCUMENT '#{quote_string(value)}' PRESERVE WHITESPACE)" elsif column_type == :integer value.to_i elsif column_type == :float value.to_f else "'#{quote_string(value)}'" end else super end end |
#quote_column_name(name) ⇒ Object
451 452 453 |
# File 'lib/arjdbc/derby/adapter.rb', line 451 def quote_column_name(name) %Q{"#{name.to_s.upcase.gsub('"', '""')}"} end |
#quote_table_name_for_assignment(table, attr) ⇒ Object
456 457 458 |
# File 'lib/arjdbc/derby/adapter.rb', line 456 def quote_table_name_for_assignment(table, attr) quote_column_name(attr) end |
#quoted_date(value) ⇒ Object
197 198 199 200 201 202 203 204 205 |
# File 'lib/arjdbc/derby/adapter.rb', line 197 def quoted_date(value) if value.acts_like?(:time) && value.respond_to?(:usec) usec = sprintf("%06d", value.usec) value = ::ActiveRecord::Base.default_timezone == :utc ? value.getutc : value.getlocal "#{value.strftime("%Y-%m-%d %H:%M:%S")}.#{usec}" else super end end |
#remove_column(table_name, *column_names) ⇒ Object Also known as: remove_columns
285 286 287 |
# File 'lib/arjdbc/derby/adapter.rb', line 285 def remove_column(table_name, column_name, type = nil, = {}) do_remove_column(table_name, column_name) end |
#remove_index(table_name, options) ⇒ Object
260 261 262 |
# File 'lib/arjdbc/derby/adapter.rb', line 260 def remove_index(table_name, ) execute "DROP INDEX #{index_name(table_name, )}" end |
#rename_column(table_name, column_name, new_column_name) ⇒ Object
353 354 355 356 |
# File 'lib/arjdbc/derby/adapter.rb', line 353 def rename_column(table_name, column_name, new_column_name) execute "RENAME COLUMN #{quote_table_name(table_name)}.#{quote_column_name(column_name)} " << " TO #{quote_column_name(new_column_name)}" end |
#rename_table(name, new_name) ⇒ Object
265 266 267 |
# File 'lib/arjdbc/derby/adapter.rb', line 265 def rename_table(name, new_name) execute "RENAME TABLE #{quote_table_name(name)} TO #{quote_table_name(new_name)}" end |
#reset_column_information ⇒ Object
134 135 136 |
# File 'lib/arjdbc/derby/adapter.rb', line 134 def reset_column_information initialize_type_map(type_map) end |
#reset_pk_sequence!(table, pk = nil, sequence = nil) ⇒ Object
245 246 247 248 249 250 251 252 |
# File 'lib/arjdbc/derby/adapter.rb', line 245 def reset_pk_sequence!(table, pk = nil, sequence = nil) klasses = classes_for_table_name(table) klass = klasses.nil? ? nil : klasses.first pk = klass.primary_key unless klass.nil? if pk && klass.columns_hash[pk].type == :integer reset_sequence!(klass.table_name, pk) end end |
#reset_sequence!(table, column, sequence = nil) ⇒ Object
Set the sequence to the max value of the table's column.
240 241 242 243 |
# File 'lib/arjdbc/derby/adapter.rb', line 240 def reset_sequence!(table, column, sequence = nil) mpk = select_value("SELECT MAX(#{quote_column_name(column)}) FROM #{quote_table_name(table)}") execute("ALTER TABLE #{quote_table_name(table)} ALTER COLUMN #{quote_column_name(column)} RESTART WITH #{mpk.to_i + 1}") end |
#set_schema(schema) ⇒ Object Also known as: current_schema=
Change the current (implicit) Derby schema to be used for this connection.
419 420 421 422 |
# File 'lib/arjdbc/derby/adapter.rb', line 419 def set_schema(schema) @current_schema = nil execute "SET SCHEMA #{schema}", 'SCHEMA' end |
#supports_ddl_transactions? ⇒ Boolean
403 |
# File 'lib/arjdbc/derby/adapter.rb', line 403 def supports_ddl_transactions?; true end |
#supports_foreign_keys? ⇒ Boolean
406 |
# File 'lib/arjdbc/derby/adapter.rb', line 406 def supports_foreign_keys?; true end |
#table_definition(*args) ⇒ Object
229 230 231 |
# File 'lib/arjdbc/derby/adapter.rb', line 229 def table_definition(*args) new_table_definition(TableDefinition, *args) end |
#tables(name = nil) ⇒ Object
398 399 400 |
# File 'lib/arjdbc/derby/adapter.rb', line 398 def tables(name = nil) @connection.tables(nil, current_schema) end |
#truncate(table_name, name = nil) ⇒ Object
408 409 410 |
# File 'lib/arjdbc/derby/adapter.rb', line 408 def truncate(table_name, name = nil) execute "TRUNCATE TABLE #{quote_table_name(table_name)}", name end |
#type_to_sql(type, limit = nil, precision = nil, scale = nil) ⇒ Object
Convert the specified column type to a SQL string.
212 213 214 215 216 217 |
# File 'lib/arjdbc/derby/adapter.rb', line 212 def type_to_sql(type, limit = nil, precision = nil, scale = nil) return super unless NO_LIMIT_TYPES.include?(t = type.to_s.downcase.to_sym) native_type = NATIVE_DATABASE_TYPES[t] native_type.is_a?(Hash) ? native_type[:name] : native_type end |