activerecord-jdbc-adapter 0.6
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/History.txt +61 -0
- data/LICENSE +21 -0
- data/Manifest.txt +64 -0
- data/README.txt +116 -0
- data/Rakefile +146 -0
- data/lib/active_record/connection_adapters/derby_adapter.rb +13 -0
- data/lib/active_record/connection_adapters/h2_adapter.rb +1 -0
- data/lib/active_record/connection_adapters/hsqldb_adapter.rb +13 -0
- data/lib/active_record/connection_adapters/jdbc_adapter.rb +575 -0
- data/lib/active_record/connection_adapters/jdbc_adapter_spec.rb +10 -0
- data/lib/active_record/connection_adapters/jndi_adapter.rb +1 -0
- data/lib/active_record/connection_adapters/mysql_adapter.rb +13 -0
- data/lib/active_record/connection_adapters/oracle_adapter.rb +1 -0
- data/lib/active_record/connection_adapters/postgresql_adapter.rb +13 -0
- data/lib/jdbc_adapter.rb +32 -0
- data/lib/jdbc_adapter/jdbc_db2.rb +104 -0
- data/lib/jdbc_adapter/jdbc_derby.rb +362 -0
- data/lib/jdbc_adapter/jdbc_firebird.rb +109 -0
- data/lib/jdbc_adapter/jdbc_hsqldb.rb +168 -0
- data/lib/jdbc_adapter/jdbc_mimer.rb +134 -0
- data/lib/jdbc_adapter/jdbc_mssql.rb +356 -0
- data/lib/jdbc_adapter/jdbc_mysql.rb +168 -0
- data/lib/jdbc_adapter/jdbc_oracle.rb +340 -0
- data/lib/jdbc_adapter/jdbc_postgre.rb +347 -0
- data/lib/jdbc_adapter/missing_functionality_helper.rb +72 -0
- data/lib/jdbc_adapter/version.rb +5 -0
- data/lib/jdbc_adapter_internal.jar +0 -0
- data/lib/tasks/jdbc_databases.rake +72 -0
- data/src/java/JDBCDerbySpec.java +323 -0
- data/src/java/JDBCMySQLSpec.java +89 -0
- data/src/java/JdbcAdapterInternalService.java +953 -0
- data/test/activerecord/connection_adapters/type_conversion_test.rb +31 -0
- data/test/activerecord/connections/native_jdbc_mysql/connection.rb +25 -0
- data/test/db/derby.rb +18 -0
- data/test/db/h2.rb +11 -0
- data/test/db/hsqldb.rb +15 -0
- data/test/db/jdbc.rb +11 -0
- data/test/db/jndi_config.rb +30 -0
- data/test/db/logger.rb +3 -0
- data/test/db/mysql.rb +9 -0
- data/test/db/postgres.rb +9 -0
- data/test/derby_multibyte_test.rb +12 -0
- data/test/derby_simple_test.rb +12 -0
- data/test/generic_jdbc_connection_test.rb +9 -0
- data/test/h2_simple_test.rb +7 -0
- data/test/hsqldb_simple_test.rb +6 -0
- data/test/jdbc_adapter/jdbc_db2_test.rb +21 -0
- data/test/jdbc_common.rb +6 -0
- data/test/jndi_test.rb +37 -0
- data/test/manualTestDatabase.rb +195 -0
- data/test/minirunit.rb +109 -0
- data/test/minirunit/testConnect.rb +14 -0
- data/test/minirunit/testH2.rb +73 -0
- data/test/minirunit/testHsqldb.rb +73 -0
- data/test/minirunit/testLoadActiveRecord.rb +3 -0
- data/test/minirunit/testMysql.rb +83 -0
- data/test/minirunit/testRawSelect.rb +24 -0
- data/test/models/auto_id.rb +18 -0
- data/test/models/data_types.rb +18 -0
- data/test/models/entry.rb +20 -0
- data/test/mysql_multibyte_test.rb +6 -0
- data/test/mysql_simple_test.rb +13 -0
- data/test/postgres_simple_test.rb +12 -0
- data/test/simple.rb +157 -0
- metadata +112 -0
@@ -0,0 +1,31 @@
|
|
1
|
+
require 'java'
|
2
|
+
require 'models/data_types'
|
3
|
+
require 'active_record/connection_adapters/jdbc_adapter'
|
4
|
+
require 'db/derby'
|
5
|
+
require 'test/unit'
|
6
|
+
|
7
|
+
JInteger = java.lang.Integer
|
8
|
+
|
9
|
+
class TypeConversionTest < Test::Unit::TestCase
|
10
|
+
TEST_TIME = Time.at(1169964202)
|
11
|
+
def setup
|
12
|
+
DbTypeMigration.up
|
13
|
+
DbType.create(
|
14
|
+
:sample_timestamp => TEST_TIME,
|
15
|
+
:sample_decimal => JInteger::MAX_VALUE + 1)
|
16
|
+
end
|
17
|
+
|
18
|
+
def teardown
|
19
|
+
DbTypeMigration.down
|
20
|
+
end
|
21
|
+
|
22
|
+
def test_timestamp
|
23
|
+
types = DbType.find(:first)
|
24
|
+
assert_equal TEST_TIME, types.sample_timestamp.getutc
|
25
|
+
end
|
26
|
+
|
27
|
+
def test_decimal
|
28
|
+
types = DbType.find(:first)
|
29
|
+
assert_equal((JInteger::MAX_VALUE + 1), types.sample_decimal)
|
30
|
+
end
|
31
|
+
end
|
@@ -0,0 +1,25 @@
|
|
1
|
+
print "Using native JDBC (MySQL)\n"
|
2
|
+
require_dependency 'fixtures/course'
|
3
|
+
require 'logger'
|
4
|
+
|
5
|
+
RAILS_CONNECTION_ADAPTERS << 'jdbc'
|
6
|
+
require "active_record/connection_adapters/jdbc_adapter"
|
7
|
+
|
8
|
+
ActiveRecord::Base.logger = Logger.new("debug.log")
|
9
|
+
|
10
|
+
db1 = 'activerecord_unittest'
|
11
|
+
db2 = 'activerecord_unittest2'
|
12
|
+
|
13
|
+
ActiveRecord::Base.establish_connection(
|
14
|
+
:adapter => "jdbc",
|
15
|
+
:driver => "com.mysql.jdbc.Driver",
|
16
|
+
:url => "jdbc:mysql://localhost:3306/#{db1}",
|
17
|
+
:username => "rails"
|
18
|
+
)
|
19
|
+
|
20
|
+
Course.establish_connection(
|
21
|
+
:adapter => "jdbc",
|
22
|
+
:driver => "com.mysql.jdbc.Driver",
|
23
|
+
:url => "jdbc:mysql://localhost:3306/#{db2}",
|
24
|
+
:username => "rails"
|
25
|
+
)
|
data/test/db/derby.rb
ADDED
@@ -0,0 +1,18 @@
|
|
1
|
+
require 'logger'
|
2
|
+
|
3
|
+
config = {
|
4
|
+
:adapter => 'derby',
|
5
|
+
:database => "derby-testdb"
|
6
|
+
}
|
7
|
+
|
8
|
+
ActiveRecord::Base.establish_connection(config)
|
9
|
+
logger = Logger.new 'derby-testdb.log'
|
10
|
+
logger.level = Logger::DEBUG
|
11
|
+
ActiveRecord::Base.logger = logger
|
12
|
+
|
13
|
+
at_exit {
|
14
|
+
# Clean up derby files
|
15
|
+
require 'fileutils'
|
16
|
+
Dir.glob('derby-testdb/**/*') {|f| File.delete(f)}
|
17
|
+
FileUtils.rm_rf('derby-testdb')
|
18
|
+
}
|
data/test/db/h2.rb
ADDED
data/test/db/hsqldb.rb
ADDED
@@ -0,0 +1,15 @@
|
|
1
|
+
config = {
|
2
|
+
:adapter => 'hsqldb',
|
3
|
+
:database => 'test.db'
|
4
|
+
}
|
5
|
+
|
6
|
+
ActiveRecord::Base.establish_connection(config)
|
7
|
+
logger = Logger.new 'hsqldb-testdb.log'
|
8
|
+
logger.level = Logger::DEBUG
|
9
|
+
ActiveRecord::Base.logger = logger
|
10
|
+
|
11
|
+
at_exit {
|
12
|
+
# Clean up hsqldb when done
|
13
|
+
Dir['test.db*'].each {|f| File.delete(f)}
|
14
|
+
File.delete('hsqldb-testdb.log')
|
15
|
+
}
|
data/test/db/jdbc.rb
ADDED
@@ -0,0 +1,30 @@
|
|
1
|
+
require 'fileutils'
|
2
|
+
require 'active_record/connection_adapters/jdbc_adapter'
|
3
|
+
|
4
|
+
System = java.lang.System
|
5
|
+
Context = javax.naming.Context
|
6
|
+
InitialContext = javax.naming.InitialContext
|
7
|
+
Reference = javax.naming.Reference
|
8
|
+
StringRefAddr = javax.naming.StringRefAddr
|
9
|
+
|
10
|
+
System.set_property(Context::INITIAL_CONTEXT_FACTORY,
|
11
|
+
'com.sun.jndi.fscontext.RefFSContextFactory')
|
12
|
+
project_path = File.expand_path(File.dirname(__FILE__) + '/../..')
|
13
|
+
jndi_dir = project_path + '/jndi_test'
|
14
|
+
jdbc_dir = jndi_dir + '/jdbc'
|
15
|
+
FileUtils.mkdir_p jdbc_dir unless File.exist?(jdbc_dir)
|
16
|
+
|
17
|
+
System.set_property(Context::PROVIDER_URL, "file://#{jndi_dir}")
|
18
|
+
derby_ref = Reference.new('javax.sql.DataSource',
|
19
|
+
'org.apache.commons.dbcp.BasicDataSourceFactory',
|
20
|
+
nil)
|
21
|
+
derby_ref.add StringRefAddr.new('driverClassName',
|
22
|
+
'org.apache.derby.jdbc.EmbeddedDriver')
|
23
|
+
derby_ref.add StringRefAddr.new('url',
|
24
|
+
'jdbc:derby:derby-testdb;create=true')
|
25
|
+
derby_ref.add StringRefAddr.new('username', 'sa')
|
26
|
+
derby_ref.add StringRefAddr.new('password', '')
|
27
|
+
|
28
|
+
ic = InitialContext.new
|
29
|
+
ic.rebind("jdbc/derbydb", derby_ref)
|
30
|
+
|
data/test/db/logger.rb
ADDED
data/test/db/mysql.rb
ADDED
data/test/db/postgres.rb
ADDED
@@ -0,0 +1,12 @@
|
|
1
|
+
# To run this script, run the following in a mysql instance:
|
2
|
+
#
|
3
|
+
# drop database if exists weblog_development;
|
4
|
+
# create database weblog_development;
|
5
|
+
# grant all on weblog_development.* to blog@localhost;
|
6
|
+
|
7
|
+
require 'jdbc_common'
|
8
|
+
require 'db/derby'
|
9
|
+
|
10
|
+
class DerbyMultibyteTest < Test::Unit::TestCase
|
11
|
+
include MultibyteTestMethods
|
12
|
+
end
|
@@ -0,0 +1,12 @@
|
|
1
|
+
# To run this script, run the following in a mysql instance:
|
2
|
+
#
|
3
|
+
# drop database if exists weblog_development;
|
4
|
+
# create database weblog_development;
|
5
|
+
# grant all on weblog_development.* to blog@localhost;
|
6
|
+
|
7
|
+
require 'jdbc_common'
|
8
|
+
require 'db/derby'
|
9
|
+
|
10
|
+
class DerbySimpleTest < Test::Unit::TestCase
|
11
|
+
include SimpleTestMethods
|
12
|
+
end
|
@@ -0,0 +1,21 @@
|
|
1
|
+
require 'java'
|
2
|
+
require 'lib/jdbc_adapter/jdbc_db2'
|
3
|
+
require 'test/unit'
|
4
|
+
|
5
|
+
class JdbcSpec::DB2Test < Test::Unit::TestCase
|
6
|
+
def setup
|
7
|
+
@inst = Object.new
|
8
|
+
@inst.extend JdbcSpec::DB2
|
9
|
+
@column = Object.new
|
10
|
+
class <<@column
|
11
|
+
attr_accessor :type
|
12
|
+
end
|
13
|
+
end
|
14
|
+
|
15
|
+
def test_quote_decimal
|
16
|
+
assert_equal %q{'123.45'}, @inst.quote("123.45")
|
17
|
+
@column.type = :decimal
|
18
|
+
assert_equal %q{123.45}, @inst.quote("123.45", @column), "decimal columns should not have quotes"
|
19
|
+
end
|
20
|
+
|
21
|
+
end
|
data/test/jdbc_common.rb
ADDED
data/test/jndi_test.rb
ADDED
@@ -0,0 +1,37 @@
|
|
1
|
+
# In order to run these tests, you need to have a few things on your
|
2
|
+
# classpath. First, you're going to need the Sun File system
|
3
|
+
# context. You can get that here:
|
4
|
+
#
|
5
|
+
# http://java.sun.com/products/jndi/serviceproviders.html.
|
6
|
+
#
|
7
|
+
# Make sure that you put both the fscontext.jar and the
|
8
|
+
# providerutil.jar on your classpath.
|
9
|
+
#
|
10
|
+
# To support the connection pooling in the test, you'll need
|
11
|
+
# commons-dbcp, commons-pool, and commons-collections.
|
12
|
+
#
|
13
|
+
# Finally, you'll need the jdbc driver, which is derby, for this test.
|
14
|
+
|
15
|
+
require 'models/auto_id'
|
16
|
+
require 'models/entry'
|
17
|
+
require 'db/jndi_config'
|
18
|
+
require 'simple'
|
19
|
+
require 'test/unit'
|
20
|
+
require 'logger'
|
21
|
+
|
22
|
+
class DerbyJndiTest < Test::Unit::TestCase
|
23
|
+
include SimpleTestMethods
|
24
|
+
alias_method :setup_simple, :setup
|
25
|
+
def setup
|
26
|
+
ActiveRecord::Base.establish_connection({ :jndi => 'jdbc/derbydb', :adapter => 'jdbc'})
|
27
|
+
logger = Logger.new('jndi_test.log')
|
28
|
+
logger.level = Logger::DEBUG
|
29
|
+
ActiveRecord::Base.logger = logger
|
30
|
+
setup_simple
|
31
|
+
end
|
32
|
+
end
|
33
|
+
|
34
|
+
at_exit {
|
35
|
+
require 'fileutils'
|
36
|
+
FileUtils.rm_rf 'derby-testdb'
|
37
|
+
}
|
@@ -0,0 +1,195 @@
|
|
1
|
+
#!/usr/bin/env jruby
|
2
|
+
|
3
|
+
if ARGV.length < 2
|
4
|
+
$stderr.puts "syntax: #{__FILE__} [filename] [configuration-name]"
|
5
|
+
$stderr.puts " where filename points to a YAML database configuration file"
|
6
|
+
$stderr.puts " and the configuration name is in this file"
|
7
|
+
exit
|
8
|
+
end
|
9
|
+
|
10
|
+
$:.unshift File.join(File.dirname(__FILE__),'..','lib')
|
11
|
+
|
12
|
+
require 'yaml'
|
13
|
+
require 'rubygems'
|
14
|
+
RAILS_CONNECTION_ADAPTERS = ['mysql', 'jdbc']
|
15
|
+
require 'active_record'
|
16
|
+
|
17
|
+
cfg = (File.open(ARGV[0]) {|f| YAML.load(f) })[ARGV[1]]
|
18
|
+
|
19
|
+
ActiveRecord::Base.establish_connection(cfg)
|
20
|
+
|
21
|
+
ActiveRecord::Schema.define do
|
22
|
+
drop_table :authors rescue nil
|
23
|
+
drop_table :author rescue nil
|
24
|
+
|
25
|
+
create_table :author, :force => true do |t|
|
26
|
+
t.column :name, :string, :null => false
|
27
|
+
end
|
28
|
+
|
29
|
+
# Exercise all types, and add_column
|
30
|
+
add_column :author, :description, :text
|
31
|
+
add_column :author, :descr, :string, :limit => 50
|
32
|
+
add_column :author, :age, :integer, :null => false, :default => 17
|
33
|
+
add_column :author, :weight, :float
|
34
|
+
add_column :author, :born, :datetime
|
35
|
+
add_column :author, :died, :timestamp
|
36
|
+
add_column :author, :wakeup_time, :time
|
37
|
+
add_column :author, :birth_date, :date
|
38
|
+
add_column :author, :private_key, :binary
|
39
|
+
add_column :author, :female, :boolean, :default => true
|
40
|
+
|
41
|
+
change_column :author, :descr, :string, :limit => 100 if /db2|derby/ !~ ARGV[1]
|
42
|
+
change_column_default :author, :female, false if /db2|derby|mssql|firebird/ !~ ARGV[1]
|
43
|
+
remove_column :author, :died if /db2|derby/ !~ ARGV[1]
|
44
|
+
rename_column :author, :wakeup_time, :waking_time if /db2|derby|mimer/ !~ ARGV[1]
|
45
|
+
|
46
|
+
add_index :author, :name, :unique if /db2/ !~ ARGV[1]
|
47
|
+
add_index :author, [:age,:female], :name => :is_age_female if /db2/ !~ ARGV[1]
|
48
|
+
|
49
|
+
remove_index :author, :name if /db2/ !~ ARGV[1]
|
50
|
+
remove_index :author, :name => :is_age_female if /db2/ !~ ARGV[1]
|
51
|
+
|
52
|
+
rename_table :author, :authors if /db2|firebird|mimer/ !~ ARGV[1]
|
53
|
+
|
54
|
+
|
55
|
+
create_table :products, :force => true do |t|
|
56
|
+
t.column :title, :string
|
57
|
+
t.column :description, :text
|
58
|
+
t.column :image_url, :string
|
59
|
+
end
|
60
|
+
add_column :products, :price, :float, :default => 0.0
|
61
|
+
create_table :orders, :force => true do |t|
|
62
|
+
t.column :name, :string
|
63
|
+
t.column :address, :text
|
64
|
+
t.column :email, :string
|
65
|
+
t.column :pay_type, :string, :limit => 10
|
66
|
+
end
|
67
|
+
create_table :line_items, :force => true do |t|
|
68
|
+
t.column :product_id, :integer, :null => false
|
69
|
+
t.column :order_id, :integer, :null => false
|
70
|
+
t.column :quantity, :integer, :null => false
|
71
|
+
t.column :total_price, :float, :null => false
|
72
|
+
end
|
73
|
+
end
|
74
|
+
|
75
|
+
class Author < ActiveRecord::Base;
|
76
|
+
set_table_name "author" if /db2|firebird|mimer/ =~ ARGV[1]
|
77
|
+
end
|
78
|
+
|
79
|
+
class Order < ActiveRecord::Base
|
80
|
+
has_many :line_items
|
81
|
+
end
|
82
|
+
|
83
|
+
class Product < ActiveRecord::Base
|
84
|
+
has_many :orders, :through => :line_items
|
85
|
+
has_many :line_items
|
86
|
+
|
87
|
+
def self.find_products_for_sale
|
88
|
+
find(:all, :order => "title")
|
89
|
+
end
|
90
|
+
end
|
91
|
+
|
92
|
+
class LineItem < ActiveRecord::Base
|
93
|
+
belongs_to :order
|
94
|
+
belongs_to :product
|
95
|
+
end
|
96
|
+
|
97
|
+
Product.create(:title => 'Pragmatic Project Automation',
|
98
|
+
:description =>
|
99
|
+
%{<p>
|
100
|
+
<em>Pragmatic Project Automation</em> shows you how to improve the
|
101
|
+
consistency and repeatability of your project's procedures using
|
102
|
+
automation to reduce risk and errors.
|
103
|
+
</p>
|
104
|
+
<p>
|
105
|
+
Simply put, we're going to put this thing called a computer to work
|
106
|
+
for you doing the mundane (but important) project stuff. That means
|
107
|
+
you'll have more time and energy to do the really
|
108
|
+
exciting---and difficult---stuff, like writing quality code.
|
109
|
+
</p>},
|
110
|
+
:image_url => '/images/auto.jpg',
|
111
|
+
:price => 29.95)
|
112
|
+
|
113
|
+
|
114
|
+
Product.create(:title => 'Pragmatic Version Control',
|
115
|
+
:description =>
|
116
|
+
%{<p>
|
117
|
+
This book is a recipe-based approach to using Subversion that will
|
118
|
+
get you up and
|
119
|
+
running quickly---and correctly. All projects need version control:
|
120
|
+
it's a foundational piece of any project's infrastructure. Yet half
|
121
|
+
of all project teams in the U.S. don't use any version control at all.
|
122
|
+
Many others don't use it well, and end up experiencing time-consuming problems.
|
123
|
+
</p>},
|
124
|
+
:image_url => '/images/svn.jpg',
|
125
|
+
:price => 28.50)
|
126
|
+
|
127
|
+
# . . .
|
128
|
+
|
129
|
+
|
130
|
+
Product.create(:title => 'Pragmatic Unit Testing (C#)',
|
131
|
+
:description =>
|
132
|
+
%{<p>
|
133
|
+
Pragmatic programmers use feedback to drive their development and
|
134
|
+
personal processes. The most valuable feedback you can get while
|
135
|
+
coding comes from unit testing.
|
136
|
+
</p>
|
137
|
+
<p>
|
138
|
+
Without good tests in place, coding can become a frustrating game of
|
139
|
+
"whack-a-mole." That's the carnival game where the player strikes at a
|
140
|
+
mechanical mole; it retreats and another mole pops up on the opposite side
|
141
|
+
of the field. The moles pop up and down so fast that you end up flailing
|
142
|
+
your mallet helplessly as the moles continue to pop up where you least
|
143
|
+
expect them.
|
144
|
+
</p>},
|
145
|
+
:image_url => '/images/utc.jpg',
|
146
|
+
:price => 27.75)
|
147
|
+
|
148
|
+
|
149
|
+
|
150
|
+
|
151
|
+
1.times do
|
152
|
+
$stderr.print '.'
|
153
|
+
Author.destroy_all
|
154
|
+
Author.create(:name => "Arne Svensson", :age => 30)
|
155
|
+
if /db2|derby|mimer/ !~ ARGV[1]
|
156
|
+
Author.create(:name => "Pelle Gogolsson", :age => 15, :waking_time => Time.now, :private_key => "afbafddsfgsdfg")
|
157
|
+
else
|
158
|
+
Author.create(:name => "Pelle Gogolsson", :age => 15, :wakeup_time => Time.now, :private_key => "afbafddsfgsdfg")
|
159
|
+
end
|
160
|
+
Author.find(:first)
|
161
|
+
Author.find(:all)
|
162
|
+
arne = Author.find(:first)
|
163
|
+
arne.destroy
|
164
|
+
|
165
|
+
pelle = Author.find(:first)
|
166
|
+
pelle.name = "Pelle Sweitchon"
|
167
|
+
pelle.description = "dfsssdddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddd"
|
168
|
+
pelle.descr = "adsfasdf"
|
169
|
+
pelle.age = 79
|
170
|
+
pelle.weight = 57.6
|
171
|
+
pelle.born = Time.gm(1982,8,13,10,15,3,0)
|
172
|
+
pelle.female = false
|
173
|
+
pelle.save
|
174
|
+
|
175
|
+
|
176
|
+
|
177
|
+
|
178
|
+
|
179
|
+
prods = Product.find_all
|
180
|
+
order = Order.new(:name => "Dalai Lama", :address => "Great Road 32", :email => "abc@dot.com", :pay_type => "cash")
|
181
|
+
order.line_items << LineItem.new(:product => prods[0], :quantity => 3, :total_price => (prods[0].price * 3))
|
182
|
+
order.line_items << LineItem.new(:product => prods[2], :quantity => 1, :total_price => (prods[2].price))
|
183
|
+
order.save
|
184
|
+
|
185
|
+
puts "order: #{order.line_items.inspect}, with id: #{order.id} and name: #{order.name}"
|
186
|
+
end
|
187
|
+
|
188
|
+
ActiveRecord::Schema.define do
|
189
|
+
drop_table :line_items
|
190
|
+
drop_table :orders
|
191
|
+
drop_table :products
|
192
|
+
|
193
|
+
|
194
|
+
drop_table((/db2|firebird|mimer/=~ARGV[1]? :author : :authors ))
|
195
|
+
end
|