Skip to content

Commit 5c70a99

Browse files
committed
[refactor] clean up ConnectionFactory and implementations
Move the used RubyConnectionFactoryImpl to own file, delete the two unused *Impl classes in the same file.
1 parent 17cccdc commit 5c70a99

File tree

3 files changed

+62
-88
lines changed

3 files changed

+62
-88
lines changed

src/java/arjdbc/jdbc/ConnectionFactory.java

Lines changed: 0 additions & 87 deletions
Original file line numberDiff line numberDiff line change
@@ -27,12 +27,6 @@
2727

2828
import java.sql.Connection;
2929
import java.sql.SQLException;
30-
import javax.sql.DataSource;
31-
32-
import org.jruby.RubyObject;
33-
import org.jruby.RubyString;
34-
import org.jruby.runtime.ThreadContext;
35-
import org.jruby.runtime.builtin.IRubyObject;
3630

3731
/**
3832
* Interface to be implemented in Ruby for retrieving a new connection.
@@ -49,84 +43,3 @@ public interface ConnectionFactory {
4943
Connection newConnection() throws SQLException;
5044

5145
}
52-
53-
class DataSourceConnectionFactoryImpl implements ConnectionFactory {
54-
55-
private final DataSource dataSource;
56-
final String username, password; // optional
57-
58-
public DataSourceConnectionFactoryImpl(final DataSource dataSource) {
59-
this.dataSource = dataSource;
60-
this.username = null; this.password = null;
61-
}
62-
63-
public DataSourceConnectionFactoryImpl(final DataSource dataSource,
64-
final String username, final String password) {
65-
this.dataSource = dataSource;
66-
this.username = username; this.password = password;
67-
}
68-
69-
@Override
70-
public Connection newConnection() throws SQLException {
71-
if ( username != null ) {
72-
dataSource.getConnection(username, password);
73-
}
74-
return dataSource.getConnection();
75-
}
76-
77-
DataSource getDataSource() { return dataSource; } /* for tests */
78-
79-
}
80-
81-
class DriverConnectionFactoryImpl implements ConnectionFactory {
82-
83-
private final DriverWrapper driverWrapper;
84-
final String url;
85-
final String username, password; // null allowed
86-
87-
public DriverConnectionFactoryImpl(final DriverWrapper driver, final String url) {
88-
this.driverWrapper = driver; this.url = url;
89-
this.username = null; this.password = null;
90-
}
91-
92-
public DriverConnectionFactoryImpl(final DriverWrapper driver, final String url,
93-
final String username, final String password) {
94-
this.driverWrapper = driver; this.url = url;
95-
this.username = username; this.password = password;
96-
}
97-
98-
@Override
99-
public Connection newConnection() throws SQLException {
100-
return driverWrapper.connect(url, username, password);
101-
}
102-
103-
DriverWrapper getDriverWrapper() { return driverWrapper; } /* for tests */
104-
105-
}
106-
107-
// @legacy ActiveRecord::ConnectionAdapters::JdbcDriver
108-
class RubyConnectionFactoryImpl implements ConnectionFactory {
109-
110-
private final IRubyObject driver;
111-
final RubyString url;
112-
final IRubyObject username, password; // null allowed
113-
114-
private final RubyObject contextProvider;
115-
116-
public RubyConnectionFactoryImpl(final IRubyObject driver, final RubyString url,
117-
final IRubyObject username, final IRubyObject password) {
118-
this.driver = driver; this.url = url;
119-
this.username = username; this.password = password;
120-
contextProvider = (RubyObject) driver;
121-
}
122-
123-
@Override
124-
public Connection newConnection() throws SQLException {
125-
final ThreadContext context = contextProvider.getRuntime().getCurrentContext();
126-
final IRubyObject connection = driver.callMethod(context, "connection", new IRubyObject[] { url, username, password });
127-
return (Connection) connection.toJava(Connection.class);
128-
}
129-
130-
IRubyObject getDriver() { return driver; } /* for tests */
131-
132-
}
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
/***** BEGIN LICENSE BLOCK *****
2+
* Copyright (c) 2012-2014 Karol Bucek <self@kares.org>
3+
* Copyright (c) 2006-2010 Nick Sieger <nick@nicksieger.com>
4+
* Copyright (c) 2006-2007 Ola Bini <ola.bini@gmail.com>
5+
*
6+
* Permission is hereby granted, free of charge, to any person obtaining
7+
* a copy of this software and associated documentation files (the
8+
* "Software"), to deal in the Software without restriction, including
9+
* without limitation the rights to use, copy, modify, merge, publish,
10+
* distribute, sublicense, and/or sell copies of the Software, and to
11+
* permit persons to whom the Software is furnished to do so, subject to
12+
* the following conditions:
13+
*
14+
* The above copyright notice and this permission notice shall be
15+
* included in all copies or substantial portions of the Software.
16+
*
17+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
18+
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
19+
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
20+
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
21+
* LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
22+
* OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
23+
* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
24+
***** END LICENSE BLOCK *****/
25+
26+
package arjdbc.jdbc;
27+
28+
import java.sql.Connection;
29+
import java.sql.SQLException;
30+
31+
import org.jruby.RubyObject;
32+
import org.jruby.RubyString;
33+
import org.jruby.runtime.ThreadContext;
34+
import org.jruby.runtime.builtin.IRubyObject;
35+
36+
// @legacy ActiveRecord::ConnectionAdapters::JdbcDriver
37+
class RubyConnectionFactory implements ConnectionFactory {
38+
39+
private final IRubyObject driver;
40+
final RubyString url;
41+
final IRubyObject username, password; // null allowed
42+
43+
private final RubyObject contextProvider;
44+
45+
public RubyConnectionFactory(final IRubyObject driver, final RubyString url,
46+
final IRubyObject username, final IRubyObject password) {
47+
this.driver = driver; this.url = url;
48+
this.username = username; this.password = password;
49+
contextProvider = (RubyObject) driver;
50+
}
51+
52+
@Override
53+
public Connection newConnection() throws SQLException {
54+
final ThreadContext context = contextProvider.getRuntime().getCurrentContext();
55+
final IRubyObject connection = driver.callMethod(context, "connection", new IRubyObject[] { url, username, password });
56+
return (Connection) connection.toJava(Connection.class);
57+
}
58+
59+
IRubyObject getDriver() { return driver; } /* for tests */
60+
61+
}

src/java/arjdbc/jdbc/RubyJdbcConnection.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1824,7 +1824,7 @@ private ConnectionFactory setDriverFactory(final ThreadContext context) {
18241824
return factory;
18251825
}
18261826
else {
1827-
setConnectionFactory(factory = new RubyConnectionFactoryImpl(
1827+
setConnectionFactory(factory = new RubyConnectionFactory(
18281828
driver_instance, context.runtime.newString(jdbcURL),
18291829
( username.isNil() ? username : username.asString() ),
18301830
( password.isNil() ? password : password.asString() )

0 commit comments

Comments
 (0)