Skip to content

Commit 69005a4

Browse files
committed
[oracle] "better" rake tasks (kindly borrowed from the enhanced-adapter)
- support for #temporary_table? (required by enhanced_structure_dump) - added #charset and #collation as well as #database_parameters helpers
1 parent edf52fd commit 69005a4

File tree

4 files changed

+461
-53
lines changed

4 files changed

+461
-53
lines changed

lib/arjdbc/oracle/adapter.rb

Lines changed: 22 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -256,10 +256,6 @@ def drop_table(name, options = {}) #:nodoc:
256256
seq_name = options[:sequence_name] || default_sequence_name(name)
257257
execute "DROP SEQUENCE #{seq_name}" rescue nil
258258
end
259-
260-
def drop_database(name)
261-
tables.each { |table| drop_table(table) }
262-
end
263259

264260
def type_to_sql(type, limit = nil, precision = nil, scale = nil) #:nodoc:
265261
case type.to_sym
@@ -390,49 +386,7 @@ def remove_column(table_name, *column_names) #:nodoc:
390386
execute "ALTER TABLE #{quote_table_name(table_name)} DROP COLUMN #{quote_column_name(column_name)}"
391387
end
392388
end
393-
394-
def structure_dump #:nodoc:
395-
s = select_all("SELECT sequence_name FROM user_sequences").inject("") do |structure, seq|
396-
structure << "CREATE SEQUENCE #{seq.to_a.first.last};\n\n"
397-
end
398-
399-
select_all("SELECT table_name FROM user_tables").inject(s) do |structure, table|
400-
ddl = "CREATE TABLE #{table.to_a.first.last} (\n "
401-
cols = select_all(%Q{
402-
SELECT column_name, data_type, data_length, data_precision, data_scale, data_default, nullable
403-
FROM user_tab_columns
404-
WHERE table_name = '#{table.to_a.first.last}'
405-
ORDER by column_id
406-
}).map do |row|
407-
row = row.inject({}) { |h, args| h[ args[0].downcase ] = args[1]; h }
408-
col = "#{row['column_name'].downcase} #{row['data_type'].downcase}"
409-
if row['data_type'] == 'NUMBER' and ! row['data_precision'].nil?
410-
col << "(#{row['data_precision'].to_i}"
411-
col << ",#{row['data_scale'].to_i}" if ! row['data_scale'].nil?
412-
col << ')'
413-
elsif row['data_type'].include?('CHAR')
414-
col << "(#{row['data_length'].to_i})"
415-
end
416-
col << " default #{row['data_default']}" if !row['data_default'].nil?
417-
col << ' not null' if row['nullable'] == 'N'
418-
col
419-
end
420-
ddl << cols.join(",\n ")
421-
ddl << ");\n\n"
422-
structure << ddl
423-
end
424-
end
425-
426-
def structure_drop # :nodoc:
427-
drop = ''
428-
select_all("SELECT sequence_name FROM user_sequences").inject(drop) do |buff, seq|
429-
buff << "DROP SEQUENCE #{seq.to_a.first.last};\n\n"
430-
end
431-
select_all("SELECT table_name FROM user_tables").inject(drop) do |buff, table|
432-
buff << "DROP TABLE #{table.to_a.first.last} CASCADE CONSTRAINTS;\n\n"
433-
end
434-
end
435-
389+
436390
# SELECT DISTINCT clause for a given set of columns and a given ORDER BY clause.
437391
#
438392
# Oracle requires the ORDER BY columns to be in the SELECT list for DISTINCT
@@ -477,6 +431,10 @@ def extract_order_columns(order_by)
477431
end
478432
private :extract_order_columns
479433

434+
def temporary_table?(table_name) # :nodoc:
435+
select_value("SELECT temporary FROM user_tables WHERE table_name = '#{table_name.upcase}'") == 'Y'
436+
end
437+
480438
def tables # :nodoc:
481439
@connection.tables(nil, oracle_schema)
482440
end
@@ -490,16 +448,31 @@ def tablespace(table_name)
490448
select_value "SELECT tablespace_name FROM user_tables WHERE table_name='#{table_name.to_s.upcase}'"
491449
end
492450

451+
def charset
452+
database_parameters['NLS_CHARACTERSET']
453+
end
454+
455+
def collation
456+
database_parameters['NLS_COMP']
457+
end
458+
459+
def database_parameters
460+
return @database_parameters unless ( @database_parameters ||= {} ).empty?
461+
@connection.execute_query_raw("SELECT * FROM NLS_DATABASE_PARAMETERS") do
462+
|name, value| @database_parameters[name] = value
463+
end
464+
@database_parameters
465+
end
466+
493467
# QUOTING ==================================================
494468

495469
def quote_table_name(name) # :nodoc:
496470
name.to_s.split('.').map{ |n| n.split('@').map{ |m| quote_column_name(m) }.join('@') }.join('.')
497471
end
498472

499473
def quote_column_name(name) #:nodoc:
500-
name = name.to_s
501474
# if only valid lowercase column characters in name
502-
if name =~ /\A[a-z][a-z_0-9\$#]*\Z/
475+
if ( name = name.to_s ) =~ /\A[a-z][a-z_0-9\$#]*\Z/
503476
# putting double-quotes around an identifier causes Oracle to treat the
504477
# identifier as case sensitive (otherwise assumes case-insensitivity) !
505478
# all upper case is an exception, where double-quotes are meaningless

0 commit comments

Comments
 (0)