activerecord-jdbc-adapter 0.8 → 0.8.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (38) hide show
  1. data/History.txt +21 -0
  2. data/Manifest.txt +12 -0
  3. data/README.txt +1 -0
  4. data/Rakefile +19 -13
  5. data/lib/active_record/connection_adapters/cachedb_adapter.rb +1 -0
  6. data/lib/active_record/connection_adapters/jdbc_adapter.rb +22 -5
  7. data/lib/active_record/connection_adapters/jdbc_adapter_spec.rb +3 -0
  8. data/lib/active_record/connection_adapters/sqlite3_adapter.rb +13 -0
  9. data/lib/jdbc_adapter/jdbc.rake +32 -29
  10. data/lib/jdbc_adapter/jdbc_adapter_internal.jar +0 -0
  11. data/lib/jdbc_adapter/jdbc_cachedb.rb +33 -0
  12. data/lib/jdbc_adapter/jdbc_db2.rb +96 -7
  13. data/lib/jdbc_adapter/jdbc_derby.rb +35 -28
  14. data/lib/jdbc_adapter/jdbc_mssql.rb +62 -103
  15. data/lib/jdbc_adapter/jdbc_mysql.rb +34 -25
  16. data/lib/jdbc_adapter/jdbc_oracle.rb +7 -7
  17. data/lib/jdbc_adapter/jdbc_postgre.rb +1 -1
  18. data/lib/jdbc_adapter/jdbc_sqlite3.rb +188 -0
  19. data/lib/jdbc_adapter/jdbc_sybase.rb +39 -0
  20. data/lib/jdbc_adapter/tsql_helper.rb +59 -0
  21. data/lib/jdbc_adapter/version.rb +1 -1
  22. data/src/java/jdbc_adapter/JdbcAdapterInternalService.java +66 -40
  23. data/src/java/jdbc_adapter/JdbcMySQLSpec.java +2 -2
  24. data/test/cachedb_simple_test.rb +6 -0
  25. data/test/db/cachedb.rb +9 -0
  26. data/test/db/mssql.rb +9 -0
  27. data/test/db/oracle.rb +22 -2
  28. data/test/db/sqlite3.rb +11 -0
  29. data/test/db2_simple_test.rb +4 -0
  30. data/test/jdbc_common.rb +3 -1
  31. data/test/manualTestDatabase.rb +1 -5
  32. data/test/models/entry.rb +1 -1
  33. data/test/mssql_simple_test.rb +6 -0
  34. data/test/mysql_simple_test.rb +9 -0
  35. data/test/oracle_simple_test.rb +23 -0
  36. data/test/simple.rb +3 -2
  37. data/test/sqlite3_simple_test.rb +6 -0
  38. metadata +14 -2
data/History.txt CHANGED
@@ -1,3 +1,24 @@
1
+ == 0.8.1
2
+
3
+ - Now sporting a JDBC sqlite3 adapter! Thanks Joseph Athman.
4
+ - Added support for InterSystems Cache database (Ryan Bell)
5
+ - Fix for JRUBY-2256
6
+ - JRUBY-1638, JRUBY-2404, JRUBY-2463: schema.table handling and Oracle NUMBER fixes (Darcy Schultz & Jesse Hu)
7
+ - Add structure dump and other DDL-ish for DB2 (courtesy abedra and stuarthalloway)
8
+ - Fix missing quote_table_name function under Rails 1.2.6 and earlier
9
+ - Small tweaks to jdbc.rake to select proper config
10
+ - JRUBY-2011: Fix MSSQL string un-quoting issue (Silvio Fonseca)
11
+ - JRUBY-1977, 17427: Fix information_schema select issue with MSSQL (Matt Burke)
12
+ - 20479: Improve get_table_name for MSSQL (Aslak Hellesøy)
13
+ - 20243: numerics improvements for MSSQL (Aslak Hellesøy)
14
+ - 20172: don't quote table names for MSSQL (Thor Marius Henrichsen)
15
+ - 19729: check for primary key existence in postgres during insert (Martin Luder)
16
+ - JRUBY-2297, 18846: retrying failing SQL statements is harmful when not autocommitting (Craig McMillan)
17
+ - 10021: very preliminary sybase support. (Mark Atkinson) Not usable until collision w/ sqlserver driver is resolved.
18
+ - JRUBY-2312, JRUBY-2319, JRUBY-2322: Oracle timestamping issues (Jesse Hu & Michael König)
19
+ - JRUBY-2422: Fix MySQL referential integrity and rollback issues
20
+ - JRUBY-2382: mysql string quoting fails with ArrayIndexOutofBoundsException
21
+
1
22
  == 0.8
2
23
 
3
24
  - NOTE: This release is only compatible with JRuby 1.1RC3 or later.
data/Manifest.txt CHANGED
@@ -3,6 +3,7 @@ Manifest.txt
3
3
  README.txt
4
4
  Rakefile
5
5
  LICENSE.txt
6
+ lib/active_record/connection_adapters/cachedb_adapter.rb
6
7
  lib/active_record/connection_adapters/derby_adapter.rb
7
8
  lib/active_record/connection_adapters/h2_adapter.rb
8
9
  lib/active_record/connection_adapters/hsqldb_adapter.rb
@@ -12,6 +13,8 @@ lib/active_record/connection_adapters/jndi_adapter.rb
12
13
  lib/active_record/connection_adapters/mysql_adapter.rb
13
14
  lib/active_record/connection_adapters/oracle_adapter.rb
14
15
  lib/active_record/connection_adapters/postgresql_adapter.rb
16
+ lib/active_record/connection_adapters/sqlite3_adapter.rb
17
+ lib/jdbc_adapter/jdbc_cachedb.rb
15
18
  lib/jdbc_adapter/jdbc_db2.rb
16
19
  lib/jdbc_adapter/jdbc_derby.rb
17
20
  lib/jdbc_adapter/jdbc_firebird.rb
@@ -21,13 +24,18 @@ lib/jdbc_adapter/jdbc_mssql.rb
21
24
  lib/jdbc_adapter/jdbc_mysql.rb
22
25
  lib/jdbc_adapter/jdbc_oracle.rb
23
26
  lib/jdbc_adapter/jdbc_postgre.rb
27
+ lib/jdbc_adapter/jdbc_sqlite3.rb
28
+ lib/jdbc_adapter/jdbc_sybase.rb
24
29
  lib/jdbc_adapter/missing_functionality_helper.rb
25
30
  lib/jdbc_adapter/rake_tasks.rb
31
+ lib/jdbc_adapter/tsql_helper.rb
26
32
  lib/jdbc_adapter/version.rb
27
33
  lib/jdbc_adapter.rb
28
34
  lib/jdbc_adapter/jdbc_adapter_internal.jar
29
35
  test/activerecord/connection_adapters/type_conversion_test.rb
30
36
  test/activerecord/connections/native_jdbc_mysql/connection.rb
37
+ test/cachedb_simple_test.rb
38
+ test/db/cachedb.rb
31
39
  test/db/db2.rb
32
40
  test/db/derby.rb
33
41
  test/db/h2.rb
@@ -35,9 +43,11 @@ test/db/hsqldb.rb
35
43
  test/db/jdbc.rb
36
44
  test/db/jndi_config.rb
37
45
  test/db/logger.rb
46
+ test/db/mssql.rb
38
47
  test/db/mysql.rb
39
48
  test/db/oracle.rb
40
49
  test/db/postgres.rb
50
+ test/db/sqlite3.rb
41
51
  test/db2_simple_test.rb
42
52
  test/derby_multibyte_test.rb
43
53
  test/derby_simple_test.rb
@@ -61,12 +71,14 @@ test/models/auto_id.rb
61
71
  test/models/data_types.rb
62
72
  test/models/entry.rb
63
73
  test/models/reserved_word.rb
74
+ test/mssql_simple_test.rb
64
75
  test/mysql_multibyte_test.rb
65
76
  test/mysql_simple_test.rb
66
77
  test/oracle_simple_test.rb
67
78
  test/postgres_reserved_test.rb
68
79
  test/postgres_simple_test.rb
69
80
  test/simple.rb
81
+ test/sqlite3_simple_test.rb
70
82
  lib/jdbc_adapter/jdbc.rake
71
83
  src/java/jdbc_adapter/JdbcAdapterInternalService.java
72
84
  src/java/jdbc_adapter/JdbcConnectionFactory.java
data/README.txt CHANGED
@@ -26,6 +26,7 @@ What's there, and what is not there:
26
26
  * rename_column
27
27
  * HSQLDB - Complete
28
28
  * H2 - Complete
29
+ * SQLite3 - work in progress
29
30
 
30
31
  Other databases will require testing and likely a custom configuration module. Please join the jruby-extras mailing-list[http://rubyforge.org/mail/?group_id=2014] to help us discover support for more databases.
31
32
 
data/Rakefile CHANGED
@@ -5,14 +5,16 @@ task :default => [:java_compile, :test]
5
5
 
6
6
  def java_classpath_arg # myriad of ways to discover JRuby classpath
7
7
  begin
8
- jruby_cpath = Java::java.lang.System.getProperty('java.class.path')
8
+ cpath = Java::java.lang.System.getProperty('java.class.path').split(File::PATH_SEPARATOR)
9
+ cpath += Java::java.lang.System.getProperty('sun.boot.class.path').split(File::PATH_SEPARATOR)
10
+ jruby_cpath = cpath.compact.join(File::PATH_SEPARATOR)
9
11
  rescue => e
10
12
  end
11
13
  unless jruby_cpath
12
14
  jruby_cpath = ENV['JRUBY_PARENT_CLASSPATH'] || ENV['JRUBY_HOME'] &&
13
15
  FileList["#{ENV['JRUBY_HOME']}/lib/*.jar"].join(File::PATH_SEPARATOR)
14
16
  end
15
- jruby_cpath ? "-cp #{jruby_cpath}" : ""
17
+ jruby_cpath ? "-cp \"#{jruby_cpath}\"" : ""
16
18
  end
17
19
 
18
20
  desc "Compile the native Java code."
@@ -25,23 +27,13 @@ task :java_compile do
25
27
  end
26
28
  file "lib/jdbc_adapter/jdbc_adapter_internal.jar" => :java_compile
27
29
 
28
- task :more_clean do
29
- rm_rf FileList['derby*']
30
- rm_rf FileList['test.db.*']
31
- rm_rf "test/reports"
32
- rm_f FileList['lib/**/*.jar']
33
- rm_f "manifest.mf"
34
- end
35
-
36
- task :clean => :more_clean
37
-
38
30
  task :filelist do
39
31
  puts FileList['pkg/**/*'].inspect
40
32
  end
41
33
 
42
34
  if RUBY_PLATFORM =~ /java/
43
35
  # TODO: add more databases into the standard tests here.
44
- task :test => [:test_mysql, :test_jdbc, :test_derby, :test_hsqldb, :test_h2]
36
+ task :test => [:test_mysql, :test_jdbc, :test_derby, :test_hsqldb, :test_h2, :test_sqlite3]
45
37
  else
46
38
  task :test => [:test_mysql]
47
39
  end
@@ -85,6 +77,17 @@ Rake::TestTask.new(:test_db2) do |t|
85
77
  t.libs << 'test'
86
78
  end
87
79
 
80
+ # Ensure InterSystems CacheDB driver is on your classpath before launching rake
81
+ Rake::TestTask.new(:test_cachedb) do | t |
82
+ t.test_files = FileList[ 'test/cachedb_simple_test.rb' ]
83
+ t.libs << 'test'
84
+ end
85
+
86
+ # Ensure that the jTDS driver in on your classpath before launching rake
87
+ Rake::TestTask.new(:test_mssql) do | t |
88
+ t.test_files = FileList[ 'test/mssql_simple_test.rb' ]
89
+ t.libs << 'test'
90
+ end
88
91
 
89
92
  MANIFEST = FileList["History.txt", "Manifest.txt", "README.txt",
90
93
  "Rakefile", "LICENSE.txt", "lib/**/*.rb", "lib/jdbc_adapter/jdbc_adapter_internal.jar", "test/**/*.rb",
@@ -159,3 +162,6 @@ end
159
162
  task "all:release" => "#{prefix}:release"
160
163
  end
161
164
  end
165
+
166
+ require 'rake/clean'
167
+ CLEAN.include 'derby*', 'test.db.*','test/reports', 'test.sqlite3','lib/**/*.jar','manifest.mf'
@@ -0,0 +1 @@
1
+ require 'active_record/connection_adapters/jdbc_adapter'
@@ -135,6 +135,7 @@ module ActiveRecord
135
135
  lambda {|r| r['type_name'] =~ /^decimal$/i},
136
136
  lambda {|r| r['type_name'] =~ /^numeric$/i},
137
137
  lambda {|r| r['type_name'] =~ /^number$/i},
138
+ lambda {|r| r['type_name'] =~ /^real$/i},
138
139
  lambda {|r| r['precision'] == '38'},
139
140
  lambda {|r| r['data_type'] == '2'}],
140
141
  :float => [ lambda {|r| [Jdbc::Types::FLOAT,Jdbc::Types::DOUBLE, Jdbc::Types::REAL].include?(r['data_type'].to_i)},
@@ -146,17 +147,21 @@ module ActiveRecord
146
147
  :datetime => [ lambda {|r| Jdbc::Types::TIMESTAMP == r['data_type'].to_i},
147
148
  lambda {|r| r['type_name'] =~ /^datetime$/i},
148
149
  lambda {|r| r['type_name'] =~ /^timestamp$/i},
149
- lambda {|r| r['type_name'] =~ /^date/i}],
150
+ lambda {|r| r['type_name'] =~ /^date/i},
151
+ lambda {|r| r['type_name'] =~ /^integer/i}], #Num of milliseconds for SQLite3 JDBC Driver
150
152
  :timestamp => [ lambda {|r| Jdbc::Types::TIMESTAMP == r['data_type'].to_i},
151
153
  lambda {|r| r['type_name'] =~ /^timestamp$/i},
152
154
  lambda {|r| r['type_name'] =~ /^datetime/i},
153
- lambda {|r| r['type_name'] =~ /^date/i}],
155
+ lambda {|r| r['type_name'] =~ /^date/i},
156
+ lambda {|r| r['type_name'] =~ /^integer/i}], #Num of milliseconds for SQLite3 JDBC Driver
154
157
  :time => [ lambda {|r| Jdbc::Types::TIME == r['data_type'].to_i},
155
158
  lambda {|r| r['type_name'] =~ /^time$/i},
156
- lambda {|r| r['type_name'] =~ /^date/i}],
159
+ lambda {|r| r['type_name'] =~ /^date/i},
160
+ lambda {|r| r['type_name'] =~ /^integer/i}], #Num of milliseconds for SQLite3 JDBC Driver
157
161
  :date => [ lambda {|r| Jdbc::Types::DATE == r['data_type'].to_i},
158
162
  lambda {|r| r['type_name'] =~ /^date$/i},
159
- lambda {|r| r['type_name'] =~ /^date/i}],
163
+ lambda {|r| r['type_name'] =~ /^date/i},
164
+ lambda {|r| r['type_name'] =~ /^integer/i}], #Num of milliseconds for SQLite3 JDBC Driver3
160
165
  :binary => [ lambda {|r| [Jdbc::Types::LONGVARBINARY,Jdbc::Types::BINARY,Jdbc::Types::BLOB].include?(r['data_type'].to_i)},
161
166
  lambda {|r| r['type_name'] =~ /^blob/i},
162
167
  lambda {|r| r['type_name'] =~ /sub_type 0$/i}, # For FireBird
@@ -166,7 +171,8 @@ module ActiveRecord
166
171
  lambda {|r| r['type_name'] =~ /^bool/i},
167
172
  lambda {|r| r['data_type'] == '-7'},
168
173
  lambda {|r| r['type_name'] =~ /^tinyint$/i},
169
- lambda {|r| r['type_name'] =~ /^decimal$/i}]
174
+ lambda {|r| r['type_name'] =~ /^decimal$/i},
175
+ lambda {|r| r['type_name'] =~ /^integer$/i}]
170
176
  }
171
177
 
172
178
  def initialize(types)
@@ -414,8 +420,19 @@ module ActiveRecord
414
420
  end
415
421
  end
416
422
 
423
+ module CompatibilityMethods
424
+ def self.needed?(base)
425
+ !base.instance_methods.include?("quote_table_name")
426
+ end
427
+
428
+ def quote_table_name(name)
429
+ quote_column_name(name)
430
+ end
431
+ end
432
+
417
433
  class JdbcAdapter < AbstractAdapter
418
434
  extend ShadowCoreMethods
435
+ include CompatibilityMethods if CompatibilityMethods.needed?(self)
419
436
 
420
437
  attr_reader :config
421
438
 
@@ -8,3 +8,6 @@ require 'jdbc_adapter/jdbc_derby'
8
8
  require 'jdbc_adapter/jdbc_firebird'
9
9
  require 'jdbc_adapter/jdbc_db2'
10
10
  require 'jdbc_adapter/jdbc_mssql'
11
+ require 'jdbc_adapter/jdbc_cachedb'
12
+ require 'jdbc_adapter/jdbc_sqlite3'
13
+ require 'jdbc_adapter/jdbc_sybase'
@@ -0,0 +1,13 @@
1
+ tried_gem = false
2
+ begin
3
+ require "jdbc/sqlite3"
4
+ rescue LoadError
5
+ unless tried_gem
6
+ require 'rubygems'
7
+ gem "jdbc-sqlite3"
8
+ tried_gem = true
9
+ retry
10
+ end
11
+ # trust that the sqlite jar is already present
12
+ end
13
+ require 'active_record/connection_adapters/jdbc_adapter'
@@ -10,44 +10,46 @@ def redefine_task(*args, &block)
10
10
  end
11
11
 
12
12
  namespace :db do
13
- redefine_task :create => :environment do
14
- create_database(ActiveRecord::Base.configurations[RAILS_ENV])
15
- end
13
+ if Rake::Task["db:create"]
14
+ redefine_task :create => :environment do
15
+ create_database(ActiveRecord::Base.configurations[RAILS_ENV])
16
+ end
16
17
 
17
- class << self; alias_method :previous_create_database, :create_database; end
18
- def create_database(config)
19
- begin
20
- ActiveRecord::Base.establish_connection(config)
21
- ActiveRecord::Base.connection
22
- rescue
18
+ class << self; alias_method :previous_create_database, :create_database; end
19
+ def create_database(config)
23
20
  begin
24
- url = config['url']
25
- if url
26
- if url =~ /^(.*\/)/
27
- url = $1
21
+ ActiveRecord::Base.establish_connection(config)
22
+ ActiveRecord::Base.connection
23
+ rescue
24
+ begin
25
+ url = config['url']
26
+ if url
27
+ if url =~ /^(.*\/)/
28
+ url = $1
29
+ end
28
30
  end
31
+
32
+ ActiveRecord::Base.establish_connection(config.merge({'database' => nil, 'url' => url}))
33
+ ActiveRecord::Base.connection.create_database(config['database'])
34
+ ActiveRecord::Base.establish_connection(config)
35
+ rescue
36
+ previous_create_database(config)
29
37
  end
38
+ end
39
+ end
30
40
 
31
- ActiveRecord::Base.establish_connection(config.merge({'database' => nil, 'url' => url}))
32
- ActiveRecord::Base.connection.create_database(config['database'])
41
+ redefine_task :drop => :environment do
42
+ config = ActiveRecord::Base.configurations[RAILS_ENV]
43
+ begin
33
44
  ActiveRecord::Base.establish_connection(config)
45
+ db = ActiveRecord::Base.connection.database_name
46
+ ActiveRecord::Base.connection.drop_database(db)
34
47
  rescue
35
- previous_create_database(config)
48
+ drop_database(config)
36
49
  end
37
50
  end
38
51
  end
39
52
 
40
- redefine_task :drop => :environment do
41
- config = ActiveRecord::Base.configurations[RAILS_ENV]
42
- begin
43
- ActiveRecord::Base.establish_connection(config)
44
- db = ActiveRecord::Base.connection.database_name
45
- ActiveRecord::Base.connection.drop_database(db)
46
- rescue
47
- drop_database(config)
48
- end
49
- end
50
-
51
53
  namespace :structure do
52
54
  redefine_task :dump => :environment do
53
55
  abcs = ActiveRecord::Base.configurations
@@ -58,10 +60,11 @@ namespace :db do
58
60
  end
59
61
  end
60
62
  end
63
+
61
64
  namespace :test do
62
65
  redefine_task :clone_structure => [ "db:structure:dump", "db:test:purge" ] do
63
66
  abcs = ActiveRecord::Base.configurations
64
- ActiveRecord::Base.establish_connection(:test)
67
+ ActiveRecord::Base.establish_connection(abcs[:test])
65
68
  ActiveRecord::Base.connection.execute('SET foreign_key_checks = 0') if abcs["test"]["adapter"] =~ /mysql/i
66
69
  IO.readlines("db/#{RAILS_ENV}_structure.sql").join.split(";\n\n").each do |ddl|
67
70
  ActiveRecord::Base.connection.execute(ddl)
@@ -70,7 +73,7 @@ namespace :db do
70
73
 
71
74
  redefine_task :purge => :environment do
72
75
  abcs = ActiveRecord::Base.configurations
73
- ActiveRecord::Base.establish_connection(:test)
76
+ ActiveRecord::Base.establish_connection(abcs[:test])
74
77
  db = ActiveRecord::Base.connection.database_name
75
78
  ActiveRecord::Base.connection.recreate_database(db)
76
79
  end
@@ -0,0 +1,33 @@
1
+ require 'jdbc_adapter/tsql_helper'
2
+
3
+ module ::JdbcSpec
4
+ module ActiveRecordExtensions
5
+ def cachedb_connection( config )
6
+ config[:port] ||= 1972
7
+ config[:url] ||= "jdbc:Cache://#{config[:host]}:#{config[:port]}/#{ config[:database]}"
8
+ config[:driver] ||= "com.intersys.jdbc.CacheDriver"
9
+ jdbc_connection( config )
10
+ end
11
+ end
12
+
13
+ module CacheDB
14
+ include TSqlMethods
15
+
16
+ def self.column_selector
17
+ [ /cache/i, lambda { | cfg, col | col.extend( ::JdbcSpec::CacheDB::Column ) } ]
18
+ end
19
+
20
+ def self.adapter_selector
21
+ [ /cache/i, lambda { | cfg, adapt | adapt.extend( ::JdbcSpec::CacheDB ) } ]
22
+ end
23
+
24
+ module Column
25
+ end
26
+
27
+ def create_table(name, options = { })
28
+ super(name, options)
29
+ primary_key = options[:primary_key] || "id"
30
+ execute "ALTER TABLE #{name} ADD CONSTRAINT #{name}_PK PRIMARY KEY(#{primary_key})" unless options[:id] == false
31
+ end
32
+ end
33
+ end
@@ -17,14 +17,14 @@ module JdbcSpec
17
17
  adapt.extend(::JdbcSpec::DB2)
18
18
  end }]
19
19
  end
20
-
20
+
21
21
  module Column
22
22
  def type_cast(value)
23
23
  return nil if value.nil? || value =~ /^\s*null\s*$/i
24
24
  case type
25
25
  when :string then value
26
26
  when :integer then defined?(value.to_i) ? value.to_i : (value ? 1 : 0)
27
- when :primary_key then defined?(value.to_i) ? value.to_i : (value ? 1 : 0)
27
+ when :primary_key then defined?(value.to_i) ? value.to_i : (value ? 1 : 0)
28
28
  when :float then value.to_f
29
29
  when :datetime then cast_to_date_or_time(value)
30
30
  when :timestamp then cast_to_time(value)
@@ -50,7 +50,7 @@ module JdbcSpec
50
50
  Date.new(value.year, value.month, value.day) : value
51
51
  end
52
52
  end
53
-
53
+
54
54
  def modify_types(tp)
55
55
  tp[:primary_key] = 'int generated by default as identity (start with 42) primary key'
56
56
  tp[:string][:limit] = 255
@@ -58,7 +58,7 @@ module JdbcSpec
58
58
  tp[:boolean][:limit] = nil
59
59
  tp
60
60
  end
61
-
61
+
62
62
  def add_limit_offset!(sql, options)
63
63
  if limit = options[:limit]
64
64
  offset = options[:offset] || 0
@@ -66,7 +66,7 @@ module JdbcSpec
66
66
  sql << ") A ) B WHERE B.internal$rownum > #{offset} AND B.internal$rownum <= #{limit + offset}"
67
67
  end
68
68
  end
69
-
69
+
70
70
  def quote_column_name(column_name)
71
71
  column_name
72
72
  end
@@ -79,7 +79,7 @@ module JdbcSpec
79
79
  return value.to_s
80
80
  end
81
81
  case value
82
- when String
82
+ when String
83
83
  if column && column.type == :binary
84
84
  "BLOB('#{quote_string(value)}')"
85
85
  else
@@ -88,7 +88,7 @@ module JdbcSpec
88
88
  else super
89
89
  end
90
90
  end
91
-
91
+
92
92
  def quote_string(string)
93
93
  string.gsub(/'/, "''") # ' (for ruby-mode)
94
94
  end
@@ -100,5 +100,94 @@ module JdbcSpec
100
100
  def quoted_false
101
101
  '0'
102
102
  end
103
+
104
+ def recreate_database(name)
105
+ do_not_drop = ["stmg_dbsize_info","hmon_atm_info","hmon_collection","policy"]
106
+ tables.each do |table|
107
+ unless do_not_drop.include?(table)
108
+ drop_table(table)
109
+ end
110
+ end
111
+ end
112
+
113
+ def remove_index(table_name, options = { })
114
+ execute "DROP INDEX #{quote_column_name(index_name(table_name, options))}"
115
+ end
116
+
117
+ # This method makes tests pass without understanding why.
118
+ # Don't use this in production.
119
+ def columns(table_name, name = nil)
120
+ super.select do |col|
121
+ # strip out "magic" columns from DB2 (?)
122
+ !/rolename|roleid|create_time|auditpolicyname|auditpolicyid|remarks/.match(col.name)
123
+ end
124
+ end
125
+
126
+ def add_quotes(name)
127
+ return name unless name
128
+ %Q{"#{name}"}
129
+ end
130
+
131
+ def strip_quotes(str)
132
+ return str unless str
133
+ return str unless /^(["']).*\1$/ =~ str
134
+ str[1..-2]
135
+ end
136
+
137
+ def expand_double_quotes(name)
138
+ return name unless name && name['"']
139
+ name.gsub(/"/,'""')
140
+ end
141
+
142
+
143
+ def structure_dump #:nodoc:
144
+ definition=""
145
+ rs = @connection.connection.meta_data.getTables(nil,nil,nil,["TABLE"].to_java(:string))
146
+ while rs.next
147
+ tname = rs.getString(3)
148
+ definition << "CREATE TABLE #{tname} (\n"
149
+ rs2 = @connection.connection.meta_data.getColumns(nil,nil,tname,nil)
150
+ first_col = true
151
+ while rs2.next
152
+ col_name = add_quotes(rs2.getString(4));
153
+ default = ""
154
+ d1 = rs2.getString(13)
155
+ default = d1 ? " DEFAULT #{d1}" : ""
156
+
157
+ type = rs2.getString(6)
158
+ col_size = rs2.getString(7)
159
+ nulling = (rs2.getString(18) == 'NO' ? " NOT NULL" : "")
160
+ create_col_string = add_quotes(expand_double_quotes(strip_quotes(col_name))) +
161
+ " " +
162
+ type +
163
+ "" +
164
+ nulling +
165
+ default
166
+ if !first_col
167
+ create_col_string = ",\n #{create_col_string}"
168
+ else
169
+ create_col_string = " #{create_col_string}"
170
+ end
171
+
172
+ definition << create_col_string
173
+
174
+ first_col = false
175
+ end
176
+ definition << ");\n\n"
177
+ end
178
+ definition
179
+ end
180
+
181
+ def dump_schema_information
182
+ begin
183
+ if (current_schema = ActiveRecord::Migrator.current_version) > 0
184
+ #TODO: Find a way to get the DB2 instace name to properly form the statement
185
+ return "INSERT INTO DB2INST2.SCHEMA_INFO (version) VALUES (#{current_schema})"
186
+ end
187
+ rescue ActiveRecord::StatementInvalid
188
+ # No Schema Info
189
+ end
190
+ end
191
+
103
192
  end
104
193
  end