Skip to content

Commit 0dcc25e

Browse files
committed
setup 'bare' SchemaCreation for Derby + less garbage noise from distict
1 parent 3b893cb commit 0dcc25e

File tree

2 files changed

+50
-31
lines changed

2 files changed

+50
-31
lines changed

lib/arjdbc/derby/adapter.rb

Lines changed: 35 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
ArJdbc.load_java_part :Derby
22

33
require 'arjdbc/util/table_copier'
4+
require 'arjdbc/derby/schema_creation' # AR 4.x
45

56
module ArJdbc
67
module Derby
@@ -223,13 +224,6 @@ def table_definition(*args)
223224
new_table_definition(TableDefinition, *args)
224225
end
225226

226-
# @override fix case where AR passes `:default => nil, :null => true`
227-
def add_column_options!(sql, options)
228-
options.delete(:default) if options.has_key?(:default) && options[:default].nil?
229-
sql << " DEFAULT #{quote(options.delete(:default))}" if options.has_key?(:default)
230-
super
231-
end
232-
233227
# @override
234228
def empty_insert_statement_value
235229
'VALUES ( DEFAULT )' # won't work as Derby does need to know the columns count
@@ -270,37 +264,21 @@ def add_column(table_name, column_name, type, options = {})
270264
add_column_sql = "ALTER TABLE #{quote_table_name(table_name)} ADD #{quote_column_name(column_name)} #{type_to_sql(type, options[:limit], options[:precision], options[:scale])}"
271265
add_column_options!(add_column_sql, options)
272266
execute(add_column_sql)
273-
end
274-
275-
# SELECT DISTINCT clause for a given set of columns and a given ORDER BY clause.
276-
#
277-
# Derby requires the ORDER BY columns in the select list for distinct queries, and
278-
# requires that the ORDER BY include the distinct column.
279-
# ```
280-
# distinct("posts.id", "posts.created_at desc")
281-
# ```
282-
# @note This is based on distinct method for the PostgreSQL Adapter.
283-
# @override
284-
def distinct(columns, order_by)
285-
return "DISTINCT #{columns}" if order_by.blank?
267+
end unless const_defined? :SchemaCreation
286268

287-
# construct a clean list of column names from the ORDER BY clause, removing
288-
# any asc/desc modifiers
289-
order_columns = [order_by].flatten.map{|o| o.split(',').collect { |s| s.split.first } }.flatten.reject(&:blank?)
290-
order_columns = order_columns.zip((0...order_columns.size).to_a).map { |s,i| "#{s} AS alias_#{i}" }
291-
292-
# return a DISTINCT clause that's distinct on the columns we want but includes
293-
# all the required columns for the ORDER BY to work properly
294-
sql = "DISTINCT #{columns}, #{order_columns * ', '}"
295-
sql
296-
end
269+
# @override fix case where AR passes `:default => nil, :null => true`
270+
def add_column_options!(sql, options)
271+
options.delete(:default) if options.has_key?(:default) && options[:default].nil?
272+
sql << " DEFAULT #{quote(options.delete(:default))}" if options.has_key?(:default)
273+
super
274+
end unless const_defined? :SchemaCreation
297275

298276
# @override
299277
def remove_column(table_name, *column_names)
300278
for column_name in column_names.flatten
301279
execute "ALTER TABLE #{quote_table_name(table_name)} DROP COLUMN #{quote_column_name(column_name)} RESTRICT"
302280
end
303-
end
281+
end unless const_defined? :SchemaCreation
304282

305283
# @override
306284
def change_column(table_name, column_name, type, options = {})
@@ -354,6 +332,32 @@ def rename_column(table_name, column_name, new_column_name)
354332
" TO #{quote_column_name(new_column_name)}"
355333
end
356334

335+
# SELECT DISTINCT clause for a given set of columns and a given ORDER BY clause.
336+
#
337+
# Derby requires the ORDER BY columns in the select list for distinct queries, and
338+
# requires that the ORDER BY include the distinct column.
339+
# ```
340+
# distinct("posts.id", "posts.created_at desc")
341+
# ```
342+
# @note This is based on distinct method for the PostgreSQL Adapter.
343+
# @override
344+
def distinct(columns, order_by)
345+
return "DISTINCT #{columns}" if order_by.blank?
346+
347+
# construct a clean list of column names from the ORDER BY clause, removing
348+
# any asc/desc modifiers
349+
order_columns = [ order_by ].flatten!
350+
order_columns.map! do |o|
351+
o.split(',').collect! { |s| s.split.first }
352+
end # .flatten!
353+
order_columns.reject!(&:blank?)
354+
order_columns = order_columns.zip((0...order_columns.size).to_a).map { |s, i| "#{s} AS alias_#{i}" }
355+
356+
# return a DISTINCT clause that's distinct on the columns we want but includes
357+
# all the required columns for the ORDER BY to work properly
358+
"DISTINCT #{columns}, #{order_columns * ', '}"
359+
end
360+
357361
# @override
358362
def primary_keys(table_name)
359363
@connection.primary_keys table_name.to_s.upcase
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
module ArJdbc
2+
module Derby
3+
# @private
4+
class SchemaCreation < ::ActiveRecord::ConnectionAdapters::AbstractAdapter::SchemaCreation
5+
6+
private
7+
8+
end
9+
end
10+
11+
def schema_creation
12+
SchemaCreation.new self
13+
end
14+
15+
end if ::ActiveRecord::ConnectionAdapters::AbstractAdapter.const_defined? :SchemaCreation

0 commit comments

Comments
 (0)