|
1 | 1 | ArJdbc.load_java_part :Derby |
2 | 2 |
|
3 | 3 | require 'arjdbc/util/table_copier' |
| 4 | +require 'arjdbc/derby/schema_creation' # AR 4.x |
4 | 5 |
|
5 | 6 | module ArJdbc |
6 | 7 | module Derby |
@@ -223,13 +224,6 @@ def table_definition(*args) |
223 | 224 | new_table_definition(TableDefinition, *args) |
224 | 225 | end |
225 | 226 |
|
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 | | - |
233 | 227 | # @override |
234 | 228 | def empty_insert_statement_value |
235 | 229 | '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 = {}) |
270 | 264 | 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])}" |
271 | 265 | add_column_options!(add_column_sql, options) |
272 | 266 | 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 |
286 | 268 |
|
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 |
297 | 275 |
|
298 | 276 | # @override |
299 | 277 | def remove_column(table_name, *column_names) |
300 | 278 | for column_name in column_names.flatten |
301 | 279 | execute "ALTER TABLE #{quote_table_name(table_name)} DROP COLUMN #{quote_column_name(column_name)} RESTRICT" |
302 | 280 | end |
303 | | - end |
| 281 | + end unless const_defined? :SchemaCreation |
304 | 282 |
|
305 | 283 | # @override |
306 | 284 | def change_column(table_name, column_name, type, options = {}) |
@@ -354,6 +332,32 @@ def rename_column(table_name, column_name, new_column_name) |
354 | 332 | " TO #{quote_column_name(new_column_name)}" |
355 | 333 | end |
356 | 334 |
|
| 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 | + |
357 | 361 | # @override |
358 | 362 | def primary_keys(table_name) |
359 | 363 | @connection.primary_keys table_name.to_s.upcase |
|
0 commit comments