activerecord-jdbc-adapter 1.3.0.beta1 → 1.3.0.beta2

Sign up to get free protection for your applications and to get access to all the features.
Files changed (161) hide show
  1. data/.gitignore +12 -11
  2. data/.travis.yml +36 -7
  3. data/Appraisals +3 -3
  4. data/Gemfile +1 -1
  5. data/Gemfile.lock +13 -6
  6. data/History.txt +64 -0
  7. data/README.md +8 -1
  8. data/Rakefile +3 -1
  9. data/gemfiles/rails23.gemfile +1 -1
  10. data/gemfiles/rails23.gemfile.lock +6 -5
  11. data/gemfiles/rails30.gemfile +1 -1
  12. data/gemfiles/rails30.gemfile.lock +7 -6
  13. data/gemfiles/rails31.gemfile +1 -1
  14. data/gemfiles/rails31.gemfile.lock +6 -5
  15. data/gemfiles/rails32.gemfile +1 -1
  16. data/gemfiles/rails32.gemfile.lock +6 -5
  17. data/gemfiles/rails40.gemfile +2 -4
  18. data/gemfiles/rails40.gemfile.lock +37 -51
  19. data/lib/active_record/connection_adapters/as400_adapter.rb +2 -0
  20. data/lib/active_record/connection_adapters/db2_adapter.rb +1 -1
  21. data/lib/arel/visitors/db2.rb +5 -1
  22. data/lib/arel/visitors/hsqldb.rb +1 -0
  23. data/lib/arel/visitors/sql_server.rb +55 -13
  24. data/lib/arjdbc/db2/adapter.rb +197 -227
  25. data/lib/arjdbc/db2/as400.rb +124 -0
  26. data/lib/arjdbc/db2/connection_methods.rb +20 -1
  27. data/lib/arjdbc/derby/adapter.rb +17 -85
  28. data/lib/arjdbc/derby/connection_methods.rb +2 -1
  29. data/lib/arjdbc/discover.rb +55 -47
  30. data/lib/arjdbc/h2/adapter.rb +52 -18
  31. data/lib/arjdbc/h2/connection_methods.rb +10 -2
  32. data/lib/arjdbc/hsqldb/adapter.rb +33 -9
  33. data/lib/arjdbc/hsqldb/connection_methods.rb +10 -2
  34. data/lib/arjdbc/informix.rb +2 -1
  35. data/lib/arjdbc/jdbc.rb +5 -1
  36. data/lib/arjdbc/jdbc/adapter.rb +167 -89
  37. data/lib/arjdbc/jdbc/adapter_java.jar +0 -0
  38. data/lib/arjdbc/jdbc/adapter_require.rb +46 -0
  39. data/lib/arjdbc/jdbc/base_ext.rb +25 -3
  40. data/lib/arjdbc/jdbc/callbacks.rb +9 -8
  41. data/lib/arjdbc/jdbc/column.rb +8 -20
  42. data/lib/arjdbc/jdbc/connection.rb +69 -80
  43. data/lib/arjdbc/jdbc/extension.rb +6 -8
  44. data/lib/arjdbc/jdbc/jdbc.rake +3 -141
  45. data/lib/arjdbc/jdbc/rake_tasks.rb +3 -10
  46. data/lib/arjdbc/mssql/adapter.rb +108 -34
  47. data/lib/arjdbc/mssql/connection_methods.rb +3 -1
  48. data/lib/arjdbc/mssql/limit_helpers.rb +3 -2
  49. data/lib/arjdbc/mssql/lock_helpers.rb +5 -1
  50. data/lib/arjdbc/mysql/adapter.rb +127 -70
  51. data/lib/arjdbc/mysql/connection_methods.rb +5 -2
  52. data/lib/arjdbc/oracle/adapter.rb +124 -94
  53. data/lib/arjdbc/oracle/connection_methods.rb +2 -1
  54. data/lib/arjdbc/postgresql/adapter.rb +99 -67
  55. data/lib/arjdbc/postgresql/column_cast.rb +3 -5
  56. data/lib/arjdbc/postgresql/connection_methods.rb +6 -6
  57. data/lib/arjdbc/railtie.rb +3 -1
  58. data/lib/arjdbc/sqlite3/adapter.rb +60 -43
  59. data/lib/arjdbc/sqlite3/connection_methods.rb +9 -9
  60. data/lib/arjdbc/sybase.rb +1 -1
  61. data/lib/arjdbc/tasks.rb +13 -0
  62. data/lib/arjdbc/tasks/database_tasks.rb +50 -0
  63. data/lib/arjdbc/tasks/databases.rake +89 -0
  64. data/lib/arjdbc/tasks/databases3.rake +203 -0
  65. data/lib/arjdbc/tasks/databases4.rake +39 -0
  66. data/lib/arjdbc/tasks/db2_database_tasks.rb +104 -0
  67. data/lib/arjdbc/tasks/derby_database_tasks.rb +95 -0
  68. data/lib/arjdbc/tasks/h2_database_tasks.rb +29 -0
  69. data/lib/arjdbc/tasks/hsqldb_database_tasks.rb +70 -0
  70. data/lib/arjdbc/tasks/jdbc_database_tasks.rb +122 -0
  71. data/lib/arjdbc/tasks/mssql_database_tasks.rb +36 -0
  72. data/lib/arjdbc/tasks/oracle/enhanced_structure_dump.rb +297 -0
  73. data/lib/arjdbc/tasks/oracle_database_tasks.rb +62 -0
  74. data/lib/arjdbc/version.rb +1 -1
  75. data/pom.xml +11 -12
  76. data/rails_generators/jdbc_generator.rb +1 -1
  77. data/rails_generators/templates/config/initializers/jdbc.rb +8 -5
  78. data/rails_generators/templates/lib/tasks/jdbc.rake +7 -4
  79. data/rakelib/02-test.rake +42 -15
  80. data/rakelib/compile.rake +29 -2
  81. data/rakelib/db.rake +2 -1
  82. data/rakelib/rails.rake +23 -6
  83. data/src/java/arjdbc/ArJdbcModule.java +175 -0
  84. data/src/java/arjdbc/db2/DB2Module.java +2 -1
  85. data/src/java/arjdbc/derby/DerbyModule.java +5 -24
  86. data/src/java/arjdbc/hsqldb/HSQLDBModule.java +3 -2
  87. data/src/java/arjdbc/jdbc/AdapterJavaService.java +3 -46
  88. data/src/java/arjdbc/jdbc/RubyJdbcConnection.java +1001 -259
  89. data/src/java/arjdbc/mssql/MSSQLModule.java +2 -1
  90. data/src/java/arjdbc/mysql/MySQLModule.java +4 -3
  91. data/src/java/arjdbc/mysql/MySQLRubyJdbcConnection.java +12 -7
  92. data/src/java/arjdbc/oracle/OracleModule.java +2 -1
  93. data/src/java/arjdbc/sqlite3/SQLite3Module.java +2 -1
  94. data/src/java/arjdbc/sqlite3/SQLite3RubyJdbcConnection.java +12 -0
  95. data/test/db/db2.rb +14 -7
  96. data/test/db/db2/rake_test.rb +82 -0
  97. data/test/db/db2/rake_test_data.sql +35 -0
  98. data/test/db/db2/simple_test.rb +20 -0
  99. data/test/db/db2/unit_test.rb +3 -1
  100. data/test/db/derby.rb +7 -5
  101. data/test/db/derby/rake_test.rb +96 -0
  102. data/test/db/derby/simple_test.rb +10 -2
  103. data/test/db/h2.rb +6 -8
  104. data/test/db/h2/identity_column_test.rb +35 -0
  105. data/test/db/h2/offset_test.rb +49 -0
  106. data/test/db/h2/rake_test.rb +98 -0
  107. data/test/db/h2/schema_dump_test.rb +5 -1
  108. data/test/db/hsqldb.rb +6 -10
  109. data/test/db/hsqldb/rake_test.rb +101 -0
  110. data/test/db/hsqldb/schema_dump_test.rb +5 -1
  111. data/test/db/hsqldb/simple_test.rb +8 -0
  112. data/test/db/jndi_config.rb +1 -3
  113. data/test/db/jndi_pooled_config.rb +1 -3
  114. data/test/db/mssql/limit_offset_test.rb +23 -14
  115. data/test/db/mssql/rake_test.rb +143 -0
  116. data/test/db/mysql/_rails_test_mysql.32.out +1069 -1252
  117. data/test/db/mysql/nonstandard_primary_key_test.rb +21 -24
  118. data/test/db/mysql/rake_test.rb +97 -0
  119. data/test/db/mysql/schema_dump_test.rb +11 -11
  120. data/test/db/mysql/simple_test.rb +52 -3
  121. data/test/db/mysql/statement_escaping_test.rb +46 -0
  122. data/test/db/oracle/rake_test.rb +100 -0
  123. data/test/db/oracle/simple_test.rb +48 -0
  124. data/test/db/postgres/_rails_test_postgres.32.out +998 -1370
  125. data/test/db/postgres/active_schema_unit_test.rb +68 -0
  126. data/test/db/postgres/connection_test.rb +10 -2
  127. data/test/db/postgres/data_types_test.rb +2 -2
  128. data/test/db/postgres/ltree_test.rb +6 -5
  129. data/test/db/postgres/native_types_test.rb +1 -5
  130. data/test/db/postgres/rake_test.rb +117 -0
  131. data/test/db/postgres/schema_dump_test.rb +9 -2
  132. data/test/db/postgres/schema_test.rb +4 -2
  133. data/test/db/postgres/simple_test.rb +57 -16
  134. data/test/db/sqlite3.rb +3 -10
  135. data/test/db/sqlite3/_rails_test_sqlite3.32.out +1070 -1298
  136. data/test/db/sqlite3/rake_test.rb +71 -0
  137. data/test/db/sqlite3/simple_test.rb +9 -9
  138. data/test/has_many_through.rb +4 -1
  139. data/test/jdbc/db2.rb +14 -1
  140. data/test/jdbc_column_test.rb +23 -0
  141. data/test/{generic_jdbc_connection_test.rb → jdbc_connection_test.rb} +22 -17
  142. data/test/jndi_callbacks_test.rb +26 -28
  143. data/test/jndi_test.rb +7 -16
  144. data/test/models/data_types.rb +2 -1
  145. data/test/models/thing.rb +1 -0
  146. data/test/rails/mysql.rb +13 -0
  147. data/test/rails/sqlite3/version.rb +6 -0
  148. data/test/rails_stub.rb +31 -0
  149. data/test/rake_test_support.rb +298 -0
  150. data/test/serialize.rb +2 -4
  151. data/test/{helper.rb → shared_helper.rb} +0 -0
  152. data/test/simple.rb +167 -93
  153. data/test/test_helper.rb +52 -16
  154. metadata +388 -354
  155. data/lib/pg.rb +0 -26
  156. data/test/abstract_db_create.rb +0 -139
  157. data/test/activerecord/connection_adapters/type_conversion_test.rb +0 -36
  158. data/test/db/mssql/db_create_test.rb +0 -29
  159. data/test/db/mysql/db_create_test.rb +0 -33
  160. data/test/db/postgres/db_create_test.rb +0 -44
  161. data/test/db/postgres/db_drop_test.rb +0 -17
Binary file
@@ -0,0 +1,46 @@
1
+ module ActiveRecord
2
+
3
+ if defined? ConnectionAdapters::ConnectionSpecification::Resolver # 4.0
4
+ ConnectionAdapters::ConnectionSpecification::Resolver
5
+ elsif defined? Base::ConnectionSpecification::Resolver # 3.2
6
+ Base::ConnectionSpecification::Resolver
7
+ else class << Base; self; end # 2.3, 3.0, 3.1 :
8
+ # def self.establish_connection ... on ActiveRecord::Base
9
+ end.class_eval do
10
+
11
+ def require(path)
12
+ # NOTE: we're inspecting resolver.spec connection resolution which does :
13
+ # `require "active_record/connection_adapters/#{spec[:adapter]}_adapter"`
14
+ # ...
15
+ # this {#require} method is only re-defined on a Resolver object and thus
16
+ # will not hurt performance - it will only be called for a few times (most
17
+ # likely once), this should still be fine for AR < 3.2 where this patch
18
+ # ends up on `class << ActiveRecord::Base` since models usually rely on
19
+ # Rails's auto-loading of (missing) constants and rarely use `require`.
20
+ #
21
+ # other alternative (to make sure we do not need to eager load AR built-in
22
+ # adapters) would be to mingle with the $LOAD_PATH which seems worse ...
23
+ case path
24
+ when 'active_record/connection_adapters/mysql_adapter'
25
+ $LOADED_FEATURES << 'active_record/connection_adapters/mysql_adapter.rb'
26
+ super('arjdbc/mysql')
27
+ when 'active_record/connection_adapters/mysql2_adapter'
28
+ $LOADED_FEATURES << 'active_record/connection_adapters/mysql2_adapter.rb'
29
+ super('arjdbc/mysql')
30
+ when 'active_record/connection_adapters/postgresql_adapter'
31
+ $LOADED_FEATURES << 'active_record/connection_adapters/postgresql_adapter.rb'
32
+ super('arjdbc/postgresql')
33
+ when 'active_record/connection_adapters/sqlite_adapter'
34
+ $LOADED_FEATURES << 'active_record/connection_adapters/sqlite_adapter.rb'
35
+ super('arjdbc/sqlite3')
36
+ when 'active_record/connection_adapters/sqlite3_adapter'
37
+ $LOADED_FEATURES << 'active_record/connection_adapters/sqlite3_adapter.rb'
38
+ super('arjdbc/sqlite3')
39
+ else super
40
+ end
41
+ super
42
+ end
43
+
44
+ end
45
+
46
+ end
@@ -4,7 +4,7 @@ module ActiveRecord
4
4
  class << self
5
5
  # Allow adapters to provide their own {#reset_column_information} method.
6
6
  # @note This only affects the current thread's connection.
7
- def reset_column_information_with_arjdbc
7
+ def reset_column_information_with_arjdbc # :nodoc:
8
8
  # invoke the adapter-specific reset_column_information method
9
9
  connection.reset_column_information if connection.respond_to?(:reset_column_information)
10
10
  reset_column_information_without_arjdbc
@@ -17,14 +17,36 @@ module ActiveRecord
17
17
 
18
18
  # Represents exceptions that have propagated up through the JDBC API.
19
19
  class JDBCError < ActiveRecordError
20
- # The vendor code or error number that came from the database
20
+ # The vendor code or error number that came from the database.
21
+ # @note writer being used by the Java API
21
22
  attr_accessor :errno
22
23
  # The full Java SQLException object that was raised.
24
+ # @note writer being used by the Java API
23
25
  attr_accessor :sql_exception
26
+
27
+ attr_reader :original_exception, :raw_backtrace
28
+
29
+ def initialize(message = nil, original_exception = nil) # $!
30
+ super(message)
31
+ @original_exception = original_exception
32
+ end
33
+
34
+ def set_backtrace(backtrace)
35
+ @raw_backtrace = backtrace
36
+ if nested = original_exception
37
+ backtrace = backtrace - (
38
+ nested.respond_to?(:raw_backtrace) ? nested.raw_backtrace : nested.backtrace )
39
+ backtrace << "#{nested.backtrace.first}: #{nested.message} (#{nested.class.name})"
40
+ backtrace += nested.backtrace[1..-1] || []
41
+ end
42
+ super(backtrace)
43
+ end
44
+
24
45
  end
25
46
 
26
47
  module ConnectionAdapters # :nodoc:
27
- # Allows properly re-defining methods that may already be alias_method_chain'd.
48
+ # Allows properly re-defining methods that may already be alias-chain-ed.
49
+ # Query caching works even with overriden alias_method_chain'd methods.
28
50
  module ShadowCoreMethods
29
51
  def alias_chained_method(name, feature, target)
30
52
  # NOTE: aliasing for things such as columns (with feature query_cache)
@@ -1,6 +1,8 @@
1
1
  module ActiveRecord
2
2
  module ConnectionAdapters
3
+
3
4
  module JdbcConnectionPoolCallbacks
5
+
4
6
  def self.included(base)
5
7
  if base.respond_to?(:set_callback) # Rails 3 callbacks
6
8
  base.set_callback :checkin, :after, :on_checkin
@@ -11,10 +13,6 @@ module ActiveRecord
11
13
  end
12
14
  end
13
15
 
14
- def self.needed?
15
- ActiveRecord::Base.respond_to?(:connection_pool)
16
- end
17
-
18
16
  def on_checkin
19
17
  # default implementation does nothing
20
18
  end
@@ -22,13 +20,15 @@ module ActiveRecord
22
20
  def on_checkout
23
21
  # default implementation does nothing
24
22
  end
23
+
25
24
  end
26
25
 
27
26
  module JndiConnectionPoolCallbacks
28
- def self.prepare(adapter, conn)
29
- if ActiveRecord::Base.respond_to?(:connection_pool) && conn.jndi_connection?
30
- adapter.extend self
31
- conn.disconnect! # disconnect initial connection in JdbcConnection#initialize
27
+
28
+ def self.prepare(adapter, connection)
29
+ if adapter.is_a?(JdbcConnectionPoolCallbacks) && connection.jndi?
30
+ adapter.extend self # extend JndiConnectionPoolCallbacks
31
+ connection.disconnect! # disconnect initial (JNDI) connection if any
32
32
  end
33
33
  end
34
34
 
@@ -40,5 +40,6 @@ module ActiveRecord
40
40
  reconnect!
41
41
  end
42
42
  end
43
+
43
44
  end
44
45
  end
@@ -36,28 +36,16 @@ module ActiveRecord
36
36
  end
37
37
 
38
38
  public
39
-
39
+
40
40
  def self.column_types
41
- # reset the column types if the # of constants changed since last call
42
- @column_types ||= begin
43
- types = driver_constants.select { |c| c.respond_to? :column_selector }
44
- types.map! { |c| c.column_selector }
45
- types.inject({}) { |h, val| h[ val[0] ] = val[1]; h }
41
+ types = {}
42
+ for mod in ::ArJdbc.modules
43
+ if mod.respond_to?(:column_selector)
44
+ sel = mod.column_selector # [ matcher, block ]
45
+ types[ sel[0] ] = sel[1]
46
+ end
46
47
  end
47
- end
48
-
49
- def self.driver_constants
50
- reset_constants
51
- @driver_constants ||= ::ArJdbc.constants.map { |c| ::ArJdbc.const_get c }
52
- end
53
-
54
- def self.reset_constants!
55
- @driver_constants = nil; @column_types = nil
56
- end
57
-
58
- def self.reset_constants
59
- return false if ! defined?(@driver_constants) || ! @driver_constants
60
- reset_constants! if ::ArJdbc.constants.size != @driver_constants.size
48
+ types
61
49
  end
62
50
 
63
51
  end
@@ -1,9 +1,8 @@
1
1
  module ActiveRecord
2
2
  module ConnectionAdapters
3
+ # @note this class is mostly implemented in Java: *RubyJdbcConnection.java*
3
4
  class JdbcConnection
4
5
 
5
- attr_reader :adapter, :connection_factory
6
-
7
6
  # @native_database_types - setup properly by adapter= versus set_native_database_types.
8
7
  # This contains type information for the adapter. Individual adapters can make tweaks
9
8
  # by defined modify_types
@@ -14,12 +13,11 @@ module ActiveRecord
14
13
 
15
14
  def initialize(config)
16
15
  self.config = config
17
- @connection = nil
18
- @jndi_connection = false
19
- configure_connection # ConfigHelper#configure_connection
20
- connection # force the connection to load (@see RubyJDbcConnection.connection)
21
- set_native_database_types
22
- @stmts = {} # AR compatibility - statement cache not used
16
+ @connection = nil; @jndi = nil
17
+ # @stmts = {} # AR compatibility - statement cache not used
18
+ setup_connection_factory
19
+ connection # force connection to load (@see RubyJdbcConnection.connection)
20
+ set_native_database_types # so we can set the native types
23
21
  rescue Java::JavaSql::SQLException => e
24
22
  e = e.cause if defined?(NativeException) && e.is_a?(NativeException) # JRuby-1.6.8
25
23
  error = e.getMessage || e.getSQLState
@@ -30,22 +28,15 @@ module ActiveRecord
30
28
  raise error
31
29
  end
32
30
 
33
- def jndi_connection?
34
- @jndi_connection == true
35
- end
36
-
37
- def active?
38
- !! @connection
39
- end
31
+ attr_reader :connection_factory, :adapter, :config
40
32
 
33
+ # @see ActiveRecord::ConnectionAdapters::JdbcAdapter#initialize
41
34
  def adapter=(adapter)
42
35
  @adapter = adapter
43
36
  @native_database_types = dup_native_types
44
37
  @adapter.modify_types(@native_database_types)
45
38
  @adapter.config.replace(config)
46
39
  end
47
-
48
- private
49
40
 
50
41
  # Duplicate all native types into new hash structure so it can be modified
51
42
  # without destroying original structure.
@@ -60,80 +51,78 @@ module ActiveRecord
60
51
  end
61
52
  types
62
53
  end
54
+ private :dup_native_types
63
55
 
64
- module ConfigHelper
65
-
66
- attr_reader :config
67
-
68
- def config=(config)
69
- @config = config.symbolize_keys
70
- end
71
-
72
- # Configure this connection from the available configuration.
73
- # @see #configure_jdbc
74
- # @see #configure_jndi
75
- #
76
- # @note this has nothing to do with the configure_connection implemented
77
- # on some of the concrete adapters (e.g. {#ArJdbc::Postgres})
78
- def configure_connection
79
- config[:retry_count] ||= 5
80
- config[:connection_alive_sql] ||= "select 1"
81
- if config[:jndi]
82
- begin
83
- configure_jndi
84
- rescue => e
85
- warn "JNDI data source unavailable: #{e.message}; trying straight JDBC"
86
- configure_jdbc
87
- end
88
- else
89
- configure_jdbc
90
- end
91
- end
56
+ def config=(config)
57
+ @config = config.symbolize_keys
58
+ # NOTE: JDBC 4.0 drivers support checking if connection isValid
59
+ # thus no need to @config[:connection_alive_sql] ||= 'SELECT 1'
60
+ @config[:retry_count] ||= 5
61
+ @config
62
+ end
63
+
64
+ def jndi?; @jndi; end
65
+ alias_method :jndi_connection?, :jndi?
92
66
 
93
- def configure_jndi
94
- data_source = javax.naming.InitialContext.new.lookup config[:jndi].to_s
95
- @jndi_connection = true
96
- @connection_factory = JdbcConnectionFactory.impl do
97
- data_source.connection
67
+ # Sets the connection factory from the available configuration.
68
+ # @see #setup_jdbc_factory
69
+ # @see #setup_jndi_factory
70
+ #
71
+ # @note this has nothing to do with the configure_connection implemented
72
+ # on some of the concrete adapters (e.g. {#ArJdbc::Postgres})
73
+ def setup_connection_factory
74
+ if config[:jndi] || config[:data_source]
75
+ begin
76
+ setup_jndi_factory
77
+ rescue => e
78
+ warn "JNDI data source unavailable: #{e.message}; trying straight JDBC"
79
+ setup_jdbc_factory
98
80
  end
81
+ else
82
+ setup_jdbc_factory
99
83
  end
84
+ end
100
85
 
101
- def configure_jdbc
102
- if ! config[:url] || ( ! config[:driver] && ! config[:driver_instance] )
103
- raise ::ActiveRecord::ConnectionNotEstablished, "jdbc adapter requires :driver class and :url"
104
- end
105
-
106
- url = configure_url
107
- username = config[:username].to_s
108
- password = config[:password].to_s
109
- jdbc_driver = ( config[:driver_instance] ||=
110
- JdbcDriver.new(config[:driver].to_s, config[:properties]) )
86
+ protected
111
87
 
112
- @connection_factory = JdbcConnectionFactory.impl do
113
- jdbc_driver.connection(url, username, password)
114
- end
88
+ def setup_jndi_factory
89
+ data_source = config[:data_source] ||
90
+ Java::JavaxNaming::InitialContext.new.lookup(config[:jndi].to_s)
91
+
92
+ @jndi = true
93
+ @connection_factory = JdbcConnectionFactory.impl do
94
+ data_source.connection
115
95
  end
96
+ end
116
97
 
117
- private
98
+ def setup_jdbc_factory
99
+ if ! config[:url] || ( ! config[:driver] && ! config[:driver_instance] )
100
+ raise ::ActiveRecord::ConnectionNotEstablished, "jdbc adapter requires :driver class and :url"
101
+ end
118
102
 
119
- def configure_url
120
- url = config[:url].to_s
121
- if Hash === config[:options]
122
- options = ''
123
- config[:options].each do |key, val|
124
- options << '&' unless options.empty?
125
- options << "#{key}=#{val}"
126
- end
127
- url = url['?'] ? "#{url}&#{options}" : "#{url}?#{options}" unless options.empty?
128
- config[:url] = url
129
- config[:options] = nil
130
- end
131
- url
103
+ url = jdbc_url
104
+ username = config[:username].to_s
105
+ password = config[:password].to_s
106
+ jdbc_driver = ( config[:driver_instance] ||=
107
+ JdbcDriver.new(config[:driver].to_s, config[:properties]) )
108
+
109
+ @jndi = false
110
+ @connection_factory = JdbcConnectionFactory.impl do
111
+ jdbc_driver.connection(url, username, password)
132
112
  end
113
+ end
133
114
 
134
- end # ConfigHelper
135
-
136
- include ConfigHelper
115
+ private
116
+
117
+ def jdbc_url
118
+ url = config[:url].to_s
119
+ if Hash === config[:options]
120
+ options = config[:options].map { |key, val| "#{key}=#{val}" }.join('&')
121
+ url = url['?'] ? "#{url}&#{options}" : "#{url}?#{options}" unless options.empty?
122
+ config[:url] = url; config[:options] = nil
123
+ end
124
+ url
125
+ end
137
126
 
138
127
  end
139
128
  end
@@ -30,16 +30,14 @@ module ArJdbc
30
30
  mod = const_set(name, Module.new)
31
31
  end
32
32
  (class << mod; self; end).instance_eval do
33
- unless respond_to?(:adapter_matcher)
34
- define_method :adapter_matcher do |_name, config|
35
- if block.arity == 1
36
- block.call(_name) ? mod : false
37
- else
38
- block.call(_name, config) ? mod : false
39
- end
33
+ define_method :adapter_matcher do |_name, config|
34
+ if block.arity == 1
35
+ block.call(_name) ? mod : false
36
+ else
37
+ block.call(_name, config) ? mod : false
40
38
  end
41
39
  end
42
- end
40
+ end unless mod.respond_to?(:adapter_matcher)
43
41
  end
44
42
 
45
43
  private
@@ -1,141 +1,3 @@
1
- def redefine_task(*args, &block)
2
- task_name = Hash === args.first ? args.first.keys[0] : args.first
3
- existing_task = Rake.application.lookup task_name
4
- if existing_task
5
- class << existing_task
6
- public :instance_variable_set
7
- attr_reader :actions
8
- end
9
- existing_task.instance_variable_set "@prerequisites", FileList[]
10
- existing_task.actions.shift
11
- enhancements = existing_task.actions
12
- existing_task.instance_variable_set "@actions", []
13
- end
14
- redefined_task = task(*args, &block)
15
- enhancements.each {|enhancement| redefined_task.actions << enhancement} unless enhancements.nil?
16
- end
17
-
18
- def rails_env
19
- defined?(Rails.env) ? Rails.env : RAILS_ENV
20
- end
21
-
22
- namespace :db do
23
- redefine_task :create => :rails_env do
24
- create_database(ActiveRecord::Base.configurations[rails_env])
25
- end
26
- task :create => :load_config if Rake.application.lookup(:load_config)
27
-
28
- redefine_task :drop => :environment do
29
- config = ActiveRecord::Base.configurations[rails_env]
30
- begin
31
- db = find_database_name(config)
32
- ActiveRecord::Base.connection.drop_database(db)
33
- rescue
34
- drop_database(config.merge('adapter' => config['adapter'].sub(/^jdbc/, '')))
35
- end
36
- end
37
- task :drop => :load_config if Rake.application.lookup(:load_config)
38
-
39
- namespace :create do
40
- task :all => :rails_env
41
- end
42
-
43
- namespace :drop do
44
- task :all => :environment
45
- end
46
-
47
- class << self
48
- alias_method :previous_create_database, :create_database
49
- alias_method :previous_drop_database, :drop_database
50
- end
51
-
52
- def find_database_name(config)
53
- db = config['database']
54
- if config['adapter'] =~ /postgresql/i
55
- config = config.dup
56
- if config['url']
57
- url = config['url'].dup
58
- db = url[/\/([^\/]*)$/, 1]
59
- if db
60
- url[/\/([^\/]*)$/, 1] = 'postgres'
61
- config['url'] = url
62
- end
63
- else
64
- db = config['database']
65
- config['database'] = 'postgres'
66
- end
67
- ActiveRecord::Base.establish_connection(config)
68
- else
69
- ActiveRecord::Base.establish_connection(config)
70
- db = ActiveRecord::Base.connection.database_name
71
- end
72
- db
73
- end
74
-
75
- def create_database(config)
76
- begin
77
- ActiveRecord::Base.establish_connection(config)
78
- ActiveRecord::Base.connection
79
- rescue
80
- begin
81
- if url = config['url'] and url =~ /^(.*(?<!\/)\/)(?=\w)/
82
- url = $1
83
- end
84
-
85
- ActiveRecord::Base.establish_connection(config.merge({'database' => nil, 'url' => url}))
86
- ActiveRecord::Base.connection.create_database(config['database'], config)
87
- ActiveRecord::Base.establish_connection(config)
88
- rescue => e
89
- raise e unless config['adapter'] =~ /mysql|postgresql|sqlite/
90
- previous_create_database(config.merge('adapter' => config['adapter'].sub(/^jdbc/, '')))
91
- end
92
- end
93
- end
94
-
95
- def drop_database(config)
96
- previous_drop_database(config.merge('adapter' => config['adapter'].sub(/^jdbc/, '')))
97
- end
98
-
99
- namespace :structure do
100
- redefine_task :dump => :environment do
101
- abcs = ActiveRecord::Base.configurations
102
- ActiveRecord::Base.establish_connection(abcs[rails_env])
103
- filename = ENV['DB_STRUCTURE'] || "db/#{rails_env}_structure.sql"
104
- File.open(filename, "w+") { |f| f << ActiveRecord::Base.connection.structure_dump }
105
- if ActiveRecord::Base.connection.supports_migrations?
106
- File.open(filename, "a") { |f| f << ActiveRecord::Base.connection.dump_schema_information }
107
- end
108
- end
109
-
110
- redefine_task :load => :environment do
111
- abcs = ActiveRecord::Base.configurations
112
- ActiveRecord::Base.establish_connection(abcs[rails_env])
113
- filename = ENV['DB_STRUCTURE'] || "db/#{rails_env}_structure.sql"
114
- IO.read(filename).split(/;\n*/m).each do |ddl|
115
- ActiveRecord::Base.connection.execute(ddl)
116
- end
117
- end
118
- end
119
-
120
- namespace :test do
121
- redefine_task :clone_structure => [ "db:structure:dump", "db:test:purge" ] do
122
- abcs = ActiveRecord::Base.configurations
123
- abcs['test']['pg_params'] = '?allowEncodingChanges=true' if abcs['test']['adapter'] =~ /postgresql/i
124
- ActiveRecord::Base.establish_connection(abcs["test"])
125
- ActiveRecord::Base.connection.execute('SET foreign_key_checks = 0') if abcs["test"]["adapter"] =~ /mysql/i
126
- IO.readlines("db/#{rails_env}_structure.sql").join.split(";\n\n").each do |ddl|
127
- begin
128
- ActiveRecord::Base.connection.execute(ddl.chomp(';'))
129
- rescue Exception => ex
130
- puts ex.message
131
- end
132
- end
133
- end
134
-
135
- redefine_task :purge => :environment do
136
- abcs = ActiveRecord::Base.configurations
137
- db = find_database_name(abcs['test'])
138
- ActiveRecord::Base.connection.recreate_database(db, abcs['test'])
139
- end
140
- end
141
- end
1
+ # @deprecated
2
+ warn "[DEPRECATED] load 'arjdbc/tasks.rb' (or 'arjdbc/tasks/database.rake') instead of 'arjdbc/jdbc/jdbc.rake'"
3
+ load 'arjdbc/tasks.rb'