activerecord-jdbc-adapter-ficoh 1.3.21-java
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +35 -0
- data/.travis.yml +462 -0
- data/.yardopts +4 -0
- data/Appraisals +36 -0
- data/CONTRIBUTING.md +49 -0
- data/Gemfile +68 -0
- data/History.md +1191 -0
- data/LICENSE.txt +25 -0
- data/README.md +277 -0
- data/RUNNING_TESTS.md +88 -0
- data/Rakefile +298 -0
- data/Rakefile.jdbc +20 -0
- data/activerecord-jdbc-adapter.gemspec +63 -0
- data/lib/active_record/connection_adapters/as400_adapter.rb +2 -0
- data/lib/active_record/connection_adapters/db2_adapter.rb +1 -0
- data/lib/active_record/connection_adapters/derby_adapter.rb +1 -0
- data/lib/active_record/connection_adapters/firebird_adapter.rb +1 -0
- data/lib/active_record/connection_adapters/h2_adapter.rb +1 -0
- data/lib/active_record/connection_adapters/hsqldb_adapter.rb +1 -0
- data/lib/active_record/connection_adapters/informix_adapter.rb +1 -0
- data/lib/active_record/connection_adapters/jdbc_adapter.rb +1 -0
- data/lib/active_record/connection_adapters/jndi_adapter.rb +1 -0
- data/lib/active_record/connection_adapters/mariadb_adapter.rb +1 -0
- data/lib/active_record/connection_adapters/mssql_adapter.rb +1 -0
- data/lib/active_record/connection_adapters/mysql2_adapter.rb +1 -0
- data/lib/active_record/connection_adapters/mysql_adapter.rb +1 -0
- data/lib/active_record/connection_adapters/oracle_adapter.rb +1 -0
- data/lib/active_record/connection_adapters/postgresql_adapter.rb +1 -0
- data/lib/active_record/connection_adapters/sqlite3_adapter.rb +1 -0
- data/lib/active_record/connection_adapters/sqlserver_adapter.rb +1 -0
- data/lib/activerecord-jdbc-adapter.rb +1 -0
- data/lib/arel/visitors/compat.rb +64 -0
- data/lib/arel/visitors/db2.rb +137 -0
- data/lib/arel/visitors/derby.rb +112 -0
- data/lib/arel/visitors/firebird.rb +79 -0
- data/lib/arel/visitors/h2.rb +25 -0
- data/lib/arel/visitors/hsqldb.rb +32 -0
- data/lib/arel/visitors/postgresql_jdbc.rb +6 -0
- data/lib/arel/visitors/sql_server.rb +225 -0
- data/lib/arel/visitors/sql_server/ng42.rb +293 -0
- data/lib/arjdbc.rb +22 -0
- data/lib/arjdbc/db2.rb +4 -0
- data/lib/arjdbc/db2/adapter.rb +802 -0
- data/lib/arjdbc/db2/as400.rb +137 -0
- data/lib/arjdbc/db2/column.rb +177 -0
- data/lib/arjdbc/db2/connection_methods.rb +45 -0
- data/lib/arjdbc/derby.rb +3 -0
- data/lib/arjdbc/derby/active_record_patch.rb +13 -0
- data/lib/arjdbc/derby/adapter.rb +567 -0
- data/lib/arjdbc/derby/connection_methods.rb +16 -0
- data/lib/arjdbc/derby/schema_creation.rb +15 -0
- data/lib/arjdbc/discover.rb +104 -0
- data/lib/arjdbc/firebird.rb +4 -0
- data/lib/arjdbc/firebird/adapter.rb +468 -0
- data/lib/arjdbc/firebird/connection_methods.rb +20 -0
- data/lib/arjdbc/h2.rb +3 -0
- data/lib/arjdbc/h2/adapter.rb +335 -0
- data/lib/arjdbc/h2/connection_methods.rb +22 -0
- data/lib/arjdbc/hsqldb.rb +3 -0
- data/lib/arjdbc/hsqldb/adapter.rb +304 -0
- data/lib/arjdbc/hsqldb/connection_methods.rb +23 -0
- data/lib/arjdbc/hsqldb/explain_support.rb +35 -0
- data/lib/arjdbc/hsqldb/schema_creation.rb +11 -0
- data/lib/arjdbc/informix.rb +5 -0
- data/lib/arjdbc/informix/adapter.rb +160 -0
- data/lib/arjdbc/informix/connection_methods.rb +9 -0
- data/lib/arjdbc/jdbc.rb +62 -0
- data/lib/arjdbc/jdbc/adapter.rb +997 -0
- data/lib/arjdbc/jdbc/adapter_require.rb +46 -0
- data/lib/arjdbc/jdbc/arel_support.rb +149 -0
- data/lib/arjdbc/jdbc/base_ext.rb +34 -0
- data/lib/arjdbc/jdbc/callbacks.rb +52 -0
- data/lib/arjdbc/jdbc/column.rb +83 -0
- data/lib/arjdbc/jdbc/connection.rb +26 -0
- data/lib/arjdbc/jdbc/connection_methods.rb +59 -0
- data/lib/arjdbc/jdbc/driver.rb +44 -0
- data/lib/arjdbc/jdbc/error.rb +75 -0
- data/lib/arjdbc/jdbc/extension.rb +69 -0
- data/lib/arjdbc/jdbc/java.rb +13 -0
- data/lib/arjdbc/jdbc/type_cast.rb +154 -0
- data/lib/arjdbc/jdbc/type_converter.rb +142 -0
- data/lib/arjdbc/mssql.rb +7 -0
- data/lib/arjdbc/mssql/adapter.rb +822 -0
- data/lib/arjdbc/mssql/column.rb +207 -0
- data/lib/arjdbc/mssql/connection_methods.rb +72 -0
- data/lib/arjdbc/mssql/explain_support.rb +99 -0
- data/lib/arjdbc/mssql/limit_helpers.rb +231 -0
- data/lib/arjdbc/mssql/lock_methods.rb +77 -0
- data/lib/arjdbc/mssql/types.rb +343 -0
- data/lib/arjdbc/mssql/utils.rb +82 -0
- data/lib/arjdbc/mysql.rb +3 -0
- data/lib/arjdbc/mysql/adapter.rb +998 -0
- data/lib/arjdbc/mysql/bulk_change_table.rb +150 -0
- data/lib/arjdbc/mysql/column.rb +167 -0
- data/lib/arjdbc/mysql/connection_methods.rb +137 -0
- data/lib/arjdbc/mysql/explain_support.rb +82 -0
- data/lib/arjdbc/mysql/schema_creation.rb +58 -0
- data/lib/arjdbc/oracle.rb +4 -0
- data/lib/arjdbc/oracle/adapter.rb +968 -0
- data/lib/arjdbc/oracle/column.rb +136 -0
- data/lib/arjdbc/oracle/connection_methods.rb +21 -0
- data/lib/arjdbc/postgresql.rb +3 -0
- data/lib/arjdbc/postgresql/_bc_time_cast_patch.rb +21 -0
- data/lib/arjdbc/postgresql/adapter.rb +1498 -0
- data/lib/arjdbc/postgresql/base/array_parser.rb +95 -0
- data/lib/arjdbc/postgresql/base/oid.rb +412 -0
- data/lib/arjdbc/postgresql/base/pgconn.rb +8 -0
- data/lib/arjdbc/postgresql/base/schema_definitions.rb +132 -0
- data/lib/arjdbc/postgresql/column.rb +640 -0
- data/lib/arjdbc/postgresql/connection_methods.rb +44 -0
- data/lib/arjdbc/postgresql/explain_support.rb +53 -0
- data/lib/arjdbc/postgresql/oid/bytea.rb +3 -0
- data/lib/arjdbc/postgresql/oid_types.rb +265 -0
- data/lib/arjdbc/postgresql/schema_creation.rb +60 -0
- data/lib/arjdbc/railtie.rb +11 -0
- data/lib/arjdbc/sqlite3.rb +3 -0
- data/lib/arjdbc/sqlite3/adapter.rb +654 -0
- data/lib/arjdbc/sqlite3/connection_methods.rb +36 -0
- data/lib/arjdbc/sqlite3/explain_support.rb +29 -0
- data/lib/arjdbc/sybase.rb +2 -0
- data/lib/arjdbc/sybase/adapter.rb +47 -0
- data/lib/arjdbc/tasks.rb +13 -0
- data/lib/arjdbc/tasks/database_tasks.rb +66 -0
- data/lib/arjdbc/tasks/databases.rake +91 -0
- data/lib/arjdbc/tasks/databases3.rake +239 -0
- data/lib/arjdbc/tasks/databases4.rake +39 -0
- data/lib/arjdbc/tasks/db2_database_tasks.rb +104 -0
- data/lib/arjdbc/tasks/derby_database_tasks.rb +95 -0
- data/lib/arjdbc/tasks/h2_database_tasks.rb +31 -0
- data/lib/arjdbc/tasks/hsqldb_database_tasks.rb +70 -0
- data/lib/arjdbc/tasks/jdbc_database_tasks.rb +169 -0
- data/lib/arjdbc/tasks/mssql_database_tasks.rb +46 -0
- data/lib/arjdbc/tasks/oracle/enhanced_structure_dump.rb +297 -0
- data/lib/arjdbc/tasks/oracle_database_tasks.rb +65 -0
- data/lib/arjdbc/util/quoted_cache.rb +60 -0
- data/lib/arjdbc/util/serialized_attributes.rb +98 -0
- data/lib/arjdbc/util/table_copier.rb +108 -0
- data/lib/arjdbc/version.rb +8 -0
- data/lib/generators/jdbc/USAGE +9 -0
- data/lib/generators/jdbc/jdbc_generator.rb +17 -0
- data/pom.xml +285 -0
- data/rails_generators/jdbc_generator.rb +15 -0
- data/rails_generators/templates/config/initializers/jdbc.rb +10 -0
- data/rails_generators/templates/lib/tasks/jdbc.rake +11 -0
- data/rakelib/01-tomcat.rake +51 -0
- data/rakelib/02-test.rake +151 -0
- data/rakelib/bundler_ext.rb +11 -0
- data/rakelib/db.rake +58 -0
- data/rakelib/rails.rake +77 -0
- data/src/java/arjdbc/ArJdbcModule.java +288 -0
- data/src/java/arjdbc/db2/DB2Module.java +77 -0
- data/src/java/arjdbc/db2/DB2RubyJdbcConnection.java +128 -0
- data/src/java/arjdbc/derby/DerbyModule.java +180 -0
- data/src/java/arjdbc/derby/DerbyRubyJdbcConnection.java +153 -0
- data/src/java/arjdbc/firebird/FirebirdRubyJdbcConnection.java +190 -0
- data/src/java/arjdbc/h2/H2Module.java +50 -0
- data/src/java/arjdbc/h2/H2RubyJdbcConnection.java +86 -0
- data/src/java/arjdbc/hsqldb/HSQLDBModule.java +74 -0
- data/src/java/arjdbc/informix/InformixRubyJdbcConnection.java +76 -0
- data/src/java/arjdbc/jdbc/AdapterJavaService.java +43 -0
- data/src/java/arjdbc/jdbc/Callable.java +44 -0
- data/src/java/arjdbc/jdbc/ConnectionFactory.java +77 -0
- data/src/java/arjdbc/jdbc/DataSourceConnectionFactory.java +156 -0
- data/src/java/arjdbc/jdbc/DriverConnectionFactory.java +63 -0
- data/src/java/arjdbc/jdbc/DriverWrapper.java +128 -0
- data/src/java/arjdbc/jdbc/JdbcConnectionFactory.java +32 -0
- data/src/java/arjdbc/jdbc/RubyJdbcConnection.java +4541 -0
- data/src/java/arjdbc/jdbc/SQLBlock.java +54 -0
- data/src/java/arjdbc/jdbc/WithResultSet.java +37 -0
- data/src/java/arjdbc/mssql/MSSQLModule.java +91 -0
- data/src/java/arjdbc/mssql/MSSQLRubyJdbcConnection.java +193 -0
- data/src/java/arjdbc/mysql/MySQLModule.java +140 -0
- data/src/java/arjdbc/mysql/MySQLRubyJdbcConnection.java +456 -0
- data/src/java/arjdbc/oracle/OracleModule.java +81 -0
- data/src/java/arjdbc/oracle/OracleRubyJdbcConnection.java +477 -0
- data/src/java/arjdbc/postgresql/ByteaUtils.java +171 -0
- data/src/java/arjdbc/postgresql/DriverImplementation.java +78 -0
- data/src/java/arjdbc/postgresql/PGDriverImplementation.java +535 -0
- data/src/java/arjdbc/postgresql/PostgreSQLModule.java +189 -0
- data/src/java/arjdbc/postgresql/PostgreSQLRubyJdbcConnection.java +489 -0
- data/src/java/arjdbc/sqlite3/SQLite3Module.java +93 -0
- data/src/java/arjdbc/sqlite3/SQLite3RubyJdbcConnection.java +405 -0
- data/src/java/arjdbc/util/CallResultSet.java +826 -0
- data/src/java/arjdbc/util/DateTimeUtils.java +517 -0
- data/src/java/arjdbc/util/NumberUtils.java +50 -0
- data/src/java/arjdbc/util/ObjectSupport.java +65 -0
- data/src/java/arjdbc/util/QuotingUtils.java +139 -0
- data/src/java/arjdbc/util/StringCache.java +60 -0
- data/src/java/arjdbc/util/StringHelper.java +155 -0
- metadata +288 -0
@@ -0,0 +1,43 @@
|
|
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
|
+
* Copyright (c) 2008-2009 Thomas E Enebo <enebo@acm.org>
|
6
|
+
*
|
7
|
+
* Permission is hereby granted, free of charge, to any person obtaining
|
8
|
+
* a copy of this software and associated documentation files (the
|
9
|
+
* "Software"), to deal in the Software without restriction, including
|
10
|
+
* without limitation the rights to use, copy, modify, merge, publish,
|
11
|
+
* distribute, sublicense, and/or sell copies of the Software, and to
|
12
|
+
* permit persons to whom the Software is furnished to do so, subject to
|
13
|
+
* the following conditions:
|
14
|
+
*
|
15
|
+
* The above copyright notice and this permission notice shall be
|
16
|
+
* included in all copies or substantial portions of the Software.
|
17
|
+
*
|
18
|
+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
19
|
+
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
20
|
+
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
21
|
+
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
22
|
+
* LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
23
|
+
* OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
24
|
+
* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
25
|
+
***** END LICENSE BLOCK *****/
|
26
|
+
|
27
|
+
package arjdbc.jdbc;
|
28
|
+
|
29
|
+
import java.io.IOException;
|
30
|
+
|
31
|
+
import org.jruby.Ruby;
|
32
|
+
import org.jruby.runtime.load.BasicLibraryService;
|
33
|
+
|
34
|
+
@Deprecated
|
35
|
+
public class AdapterJavaService implements BasicLibraryService {
|
36
|
+
|
37
|
+
public boolean basicLoad(final Ruby runtime) throws IOException {
|
38
|
+
RubyJdbcConnection.createJdbcConnectionClass(runtime);
|
39
|
+
arjdbc.ArJdbcModule.load(runtime);
|
40
|
+
return true;
|
41
|
+
}
|
42
|
+
|
43
|
+
}
|
@@ -0,0 +1,44 @@
|
|
1
|
+
/*
|
2
|
+
* The MIT License
|
3
|
+
*
|
4
|
+
* Copyright (c) 2013-2015 Karol Bucek <self@kares.org>
|
5
|
+
*
|
6
|
+
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
7
|
+
* of this software and associated documentation files (the "Software"), to deal
|
8
|
+
* in the Software without restriction, including without limitation the rights
|
9
|
+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
10
|
+
* copies of the Software, and to permit persons to whom the Software is
|
11
|
+
* furnished to do so, subject to the following conditions:
|
12
|
+
*
|
13
|
+
* The above copyright notice and this permission notice shall be included in
|
14
|
+
* all copies or substantial portions of the Software.
|
15
|
+
*
|
16
|
+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
17
|
+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
18
|
+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
19
|
+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
20
|
+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
21
|
+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
22
|
+
* THE SOFTWARE.
|
23
|
+
*/
|
24
|
+
package arjdbc.jdbc;
|
25
|
+
|
26
|
+
import java.sql.Connection;
|
27
|
+
import java.sql.SQLException;
|
28
|
+
|
29
|
+
/**
|
30
|
+
* A JDBC connection callable ("block") of code.
|
31
|
+
*
|
32
|
+
* @author kares
|
33
|
+
*/
|
34
|
+
public interface Callable<T> {
|
35
|
+
|
36
|
+
/**
|
37
|
+
* Perform an operation in the context of the given connection.
|
38
|
+
* @param connection
|
39
|
+
* @return
|
40
|
+
* @throws SQLException
|
41
|
+
*/
|
42
|
+
T call(final Connection connection) throws SQLException;
|
43
|
+
|
44
|
+
}
|
@@ -0,0 +1,77 @@
|
|
1
|
+
/***** BEGIN LICENSE BLOCK *****
|
2
|
+
* Copyright (c) 2012-2015 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
|
+
/**
|
37
|
+
* Interface to be implemented in Ruby for retrieving a new connection.
|
38
|
+
*
|
39
|
+
* @author nicksieger
|
40
|
+
*/
|
41
|
+
public interface ConnectionFactory {
|
42
|
+
|
43
|
+
/**
|
44
|
+
* Retrieve a (new) connection from the factory.
|
45
|
+
* @return a connection
|
46
|
+
* @throws SQLException
|
47
|
+
*/
|
48
|
+
Connection newConnection() throws SQLException;
|
49
|
+
|
50
|
+
}
|
51
|
+
|
52
|
+
// @legacy ActiveRecord::ConnectionAdapters::JdbcDriver
|
53
|
+
class RubyConnectionFactoryImpl implements ConnectionFactory {
|
54
|
+
|
55
|
+
private final IRubyObject driver;
|
56
|
+
final RubyString url;
|
57
|
+
final IRubyObject username, password; // null allowed
|
58
|
+
|
59
|
+
private final RubyObject contextProvider;
|
60
|
+
|
61
|
+
public RubyConnectionFactoryImpl(final IRubyObject driver, final RubyString url,
|
62
|
+
final IRubyObject username, final IRubyObject password) {
|
63
|
+
this.driver = driver; this.url = url;
|
64
|
+
this.username = username; this.password = password;
|
65
|
+
contextProvider = (RubyObject) driver;
|
66
|
+
}
|
67
|
+
|
68
|
+
@Override
|
69
|
+
public Connection newConnection() throws SQLException {
|
70
|
+
final ThreadContext context = contextProvider.getRuntime().getCurrentContext();
|
71
|
+
final IRubyObject connection = driver.callMethod(context, "connection", new IRubyObject[] { url, username, password });
|
72
|
+
return (Connection) connection.toJava(Connection.class);
|
73
|
+
}
|
74
|
+
|
75
|
+
IRubyObject getDriver() { return driver; } /* for tests */
|
76
|
+
|
77
|
+
}
|
@@ -0,0 +1,156 @@
|
|
1
|
+
/*
|
2
|
+
* The MIT License
|
3
|
+
*
|
4
|
+
* Copyright 2014 Karol Bucek.
|
5
|
+
*
|
6
|
+
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
7
|
+
* of this software and associated documentation files (the "Software"), to deal
|
8
|
+
* in the Software without restriction, including without limitation the rights
|
9
|
+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
10
|
+
* copies of the Software, and to permit persons to whom the Software is
|
11
|
+
* furnished to do so, subject to the following conditions:
|
12
|
+
*
|
13
|
+
* The above copyright notice and this permission notice shall be included in
|
14
|
+
* all copies or substantial portions of the Software.
|
15
|
+
*
|
16
|
+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
17
|
+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
18
|
+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
19
|
+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
20
|
+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
21
|
+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
22
|
+
* THE SOFTWARE.
|
23
|
+
*/
|
24
|
+
package arjdbc.jdbc;
|
25
|
+
|
26
|
+
import java.sql.Connection;
|
27
|
+
import java.sql.SQLException;
|
28
|
+
|
29
|
+
import javax.sql.DataSource;
|
30
|
+
import javax.naming.InitialContext;
|
31
|
+
import javax.naming.NameNotFoundException;
|
32
|
+
import javax.naming.NamingException;
|
33
|
+
|
34
|
+
import org.jruby.runtime.ThreadContext;
|
35
|
+
|
36
|
+
import static arjdbc.jdbc.RubyJdbcConnection.getConnectionNotEstablished;
|
37
|
+
import static arjdbc.jdbc.RubyJdbcConnection.wrapException;
|
38
|
+
|
39
|
+
/**
|
40
|
+
*
|
41
|
+
* @author kares
|
42
|
+
*/
|
43
|
+
final class DataSourceConnectionFactory implements ConnectionFactory {
|
44
|
+
|
45
|
+
private DataSource dataSource;
|
46
|
+
private final String lookupName;
|
47
|
+
String username;
|
48
|
+
String password; // optional
|
49
|
+
|
50
|
+
public DataSourceConnectionFactory(final DataSource dataSource) {
|
51
|
+
this.dataSource = dataSource;
|
52
|
+
this.lookupName = null;
|
53
|
+
}
|
54
|
+
|
55
|
+
DataSourceConnectionFactory(final DataSource dataSource, final String lookupName) {
|
56
|
+
this.dataSource = dataSource;
|
57
|
+
this.lookupName = lookupName;
|
58
|
+
}
|
59
|
+
|
60
|
+
public void setUsername(final String username) {
|
61
|
+
this.username = username;
|
62
|
+
}
|
63
|
+
|
64
|
+
public void setPassword(final String password) {
|
65
|
+
this.password = password;
|
66
|
+
}
|
67
|
+
|
68
|
+
@Override
|
69
|
+
public Connection newConnection() throws SQLException {
|
70
|
+
DataSource dataSource = this.dataSource;
|
71
|
+
// in case DS failed previously look it up again from JNDI :
|
72
|
+
if (dataSource == null) {
|
73
|
+
lookupDataSource();
|
74
|
+
}
|
75
|
+
try {
|
76
|
+
if (username != null) {
|
77
|
+
return dataSource.getConnection(username, password);
|
78
|
+
}
|
79
|
+
return dataSource.getConnection();
|
80
|
+
}
|
81
|
+
catch (SQLException e) {
|
82
|
+
// DS failed - maybe it's no longer a valid one
|
83
|
+
if (lookupName != null) {
|
84
|
+
this.dataSource = null;
|
85
|
+
}
|
86
|
+
throw e;
|
87
|
+
}
|
88
|
+
}
|
89
|
+
|
90
|
+
private DataSource lookupDataSource() throws SQLException {
|
91
|
+
try {
|
92
|
+
return dataSource = (DataSource) getInitialContext().lookup(lookupName);
|
93
|
+
}
|
94
|
+
catch (NamingException e) { throw new SQLException(e); }
|
95
|
+
}
|
96
|
+
|
97
|
+
DataSource getDataSource() { return dataSource; } /* for tests */
|
98
|
+
|
99
|
+
// NOTE: keep it here so that RubyJdbcConnection does not force loading of javax.naming classes
|
100
|
+
static DataSource lookupDataSource(final ThreadContext context, final String name) {
|
101
|
+
try {
|
102
|
+
final Object bound = getInitialContext().lookup(name);
|
103
|
+
if ( ! ( bound instanceof DataSource ) ) {
|
104
|
+
if ( bound == null ) throw new NameNotFoundException(); // unlikely to happen
|
105
|
+
final String msg = "bound object at '" + name + "' is not a " + DataSource.class.getName() + " but a " + bound.getClass().getName() + "\n" + bound;
|
106
|
+
throw wrapException(context, getConnectionNotEstablished(context.runtime), new ClassCastException(msg), msg);
|
107
|
+
}
|
108
|
+
return (DataSource) bound;
|
109
|
+
}
|
110
|
+
catch (NameNotFoundException e) {
|
111
|
+
final String message;
|
112
|
+
if ( name == null || name.isEmpty() ) {
|
113
|
+
message = "unable to lookup data source - no name given, please set jndi:";
|
114
|
+
}
|
115
|
+
else if ( name.indexOf("env", 0) == -1 ) {
|
116
|
+
final StringBuilder msg = new StringBuilder();
|
117
|
+
msg.append("name: '").append(name).append("' not found, ");
|
118
|
+
msg.append("try using full name (including env) e.g. ");
|
119
|
+
msg.append("java:/comp/env"); // e.g. java:/comp/env/jdbc/MyDS
|
120
|
+
if ( name.charAt(0) != '/' ) msg.append('/');
|
121
|
+
msg.append(name);
|
122
|
+
message = msg.toString();
|
123
|
+
}
|
124
|
+
else {
|
125
|
+
message = "unable to lookup data source - name: '" + name + "' not found";
|
126
|
+
}
|
127
|
+
throw wrapException(context, getConnectionNotEstablished(context.runtime), e, message);
|
128
|
+
}
|
129
|
+
catch (NamingException e) {
|
130
|
+
throw wrapException(context, context.runtime.getRuntimeError(), e);
|
131
|
+
}
|
132
|
+
}
|
133
|
+
|
134
|
+
// NamigHelper :
|
135
|
+
|
136
|
+
static final boolean contextCached = Boolean.getBoolean("arjdbc.jndi.context.cached");
|
137
|
+
|
138
|
+
private static InitialContext initialContext;
|
139
|
+
|
140
|
+
static InitialContext getInitialContext() throws NamingException {
|
141
|
+
if ( initialContext != null ) return initialContext;
|
142
|
+
if ( contextCached ) return initialContext = new InitialContext();
|
143
|
+
return new InitialContext();
|
144
|
+
}
|
145
|
+
|
146
|
+
/**
|
147
|
+
* Currently, the only way to bypass <code>new InitialContext()</code>
|
148
|
+
* without the environment being passed in.
|
149
|
+
* @param context
|
150
|
+
* @see #getInitialContext()
|
151
|
+
*/
|
152
|
+
static void setInitialContext(final InitialContext context) {
|
153
|
+
DataSourceConnectionFactory.initialContext = context;
|
154
|
+
}
|
155
|
+
|
156
|
+
}
|
@@ -0,0 +1,63 @@
|
|
1
|
+
/*
|
2
|
+
* The MIT License
|
3
|
+
*
|
4
|
+
* Copyright 2014 Karol Bucek.
|
5
|
+
*
|
6
|
+
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
7
|
+
* of this software and associated documentation files (the "Software"), to deal
|
8
|
+
* in the Software without restriction, including without limitation the rights
|
9
|
+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
10
|
+
* copies of the Software, and to permit persons to whom the Software is
|
11
|
+
* furnished to do so, subject to the following conditions:
|
12
|
+
*
|
13
|
+
* The above copyright notice and this permission notice shall be included in
|
14
|
+
* all copies or substantial portions of the Software.
|
15
|
+
*
|
16
|
+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
17
|
+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
18
|
+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
19
|
+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
20
|
+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
21
|
+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
22
|
+
* THE SOFTWARE.
|
23
|
+
*/
|
24
|
+
package arjdbc.jdbc;
|
25
|
+
|
26
|
+
import java.sql.Connection;
|
27
|
+
import java.sql.SQLException;
|
28
|
+
|
29
|
+
/**
|
30
|
+
*
|
31
|
+
* @author kares
|
32
|
+
*/
|
33
|
+
final class DriverConnectionFactory implements ConnectionFactory {
|
34
|
+
|
35
|
+
private final DriverWrapper driverWrapper;
|
36
|
+
final String url;
|
37
|
+
final String username;
|
38
|
+
final String password; // null allowed
|
39
|
+
|
40
|
+
public DriverConnectionFactory(final DriverWrapper driver, final String url) {
|
41
|
+
this.driverWrapper = driver;
|
42
|
+
this.url = url;
|
43
|
+
this.username = null;
|
44
|
+
this.password = null;
|
45
|
+
}
|
46
|
+
|
47
|
+
public DriverConnectionFactory(final DriverWrapper driver, final String url, final String username, final String password) {
|
48
|
+
this.driverWrapper = driver;
|
49
|
+
this.url = url;
|
50
|
+
this.username = username;
|
51
|
+
this.password = password;
|
52
|
+
}
|
53
|
+
|
54
|
+
@Override
|
55
|
+
public Connection newConnection() throws SQLException {
|
56
|
+
return driverWrapper.connect(url, username, password);
|
57
|
+
}
|
58
|
+
|
59
|
+
DriverWrapper getDriverWrapper() {
|
60
|
+
return driverWrapper;
|
61
|
+
} /* for tests */
|
62
|
+
|
63
|
+
}
|
@@ -0,0 +1,128 @@
|
|
1
|
+
/*
|
2
|
+
* The MIT License
|
3
|
+
*
|
4
|
+
* Copyright 2014 Karol Bucek.
|
5
|
+
*
|
6
|
+
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
7
|
+
* of this software and associated documentation files (the "Software"), to deal
|
8
|
+
* in the Software without restriction, including without limitation the rights
|
9
|
+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
10
|
+
* copies of the Software, and to permit persons to whom the Software is
|
11
|
+
* furnished to do so, subject to the following conditions:
|
12
|
+
*
|
13
|
+
* The above copyright notice and this permission notice shall be included in
|
14
|
+
* all copies or substantial portions of the Software.
|
15
|
+
*
|
16
|
+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
17
|
+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
18
|
+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
19
|
+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
20
|
+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
21
|
+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
22
|
+
* THE SOFTWARE.
|
23
|
+
*/
|
24
|
+
package arjdbc.jdbc;
|
25
|
+
|
26
|
+
import java.sql.Connection;
|
27
|
+
import java.sql.Driver;
|
28
|
+
import java.sql.SQLException;
|
29
|
+
import java.util.Map;
|
30
|
+
import java.util.Properties;
|
31
|
+
|
32
|
+
import org.jruby.Ruby;
|
33
|
+
|
34
|
+
|
35
|
+
/**
|
36
|
+
* A driver wrapper (we're avoiding DriverManager due possible leaks).
|
37
|
+
*
|
38
|
+
* When the driver is not loaded we're load it using JRuby's class-loader.
|
39
|
+
*
|
40
|
+
* @author kares
|
41
|
+
*/
|
42
|
+
public class DriverWrapper {
|
43
|
+
|
44
|
+
private final Driver driver;
|
45
|
+
private final Properties properties;
|
46
|
+
|
47
|
+
DriverWrapper(final Ruby runtime, final String name, final Properties properties)
|
48
|
+
throws ClassCastException, ClassNotFoundException, LinkageError, ExceptionInInitializerError,
|
49
|
+
InstantiationException, IllegalAccessException {
|
50
|
+
this.driver = allocateDriver( loadDriver(runtime, name) );
|
51
|
+
this.properties = properties == null ? new Properties() : properties;
|
52
|
+
}
|
53
|
+
|
54
|
+
public Properties getProperties() {
|
55
|
+
return properties;
|
56
|
+
}
|
57
|
+
|
58
|
+
public Driver getDriverInstance() {
|
59
|
+
return driver;
|
60
|
+
}
|
61
|
+
|
62
|
+
private Driver allocateDriver(final Class<? extends Driver> driverClass)
|
63
|
+
throws InstantiationException, IllegalAccessException {
|
64
|
+
try {
|
65
|
+
return driverClass.newInstance();
|
66
|
+
}
|
67
|
+
catch (InstantiationException e) {
|
68
|
+
throw e;
|
69
|
+
}
|
70
|
+
catch (IllegalAccessException e) {
|
71
|
+
throw e;
|
72
|
+
}
|
73
|
+
}
|
74
|
+
|
75
|
+
protected static Class<? extends Driver> loadDriver(final Ruby runtime, final String name)
|
76
|
+
throws ClassCastException, ClassNotFoundException, LinkageError, ExceptionInInitializerError {
|
77
|
+
@SuppressWarnings("unchecked")
|
78
|
+
Class<? extends Driver> klass = (Class<? extends Driver>) loadJavaClass(runtime, name);
|
79
|
+
if ( ! Driver.class.isAssignableFrom(klass) ) {
|
80
|
+
throw new ClassCastException(klass + " is not assignable from " + Driver.class);
|
81
|
+
}
|
82
|
+
return klass;
|
83
|
+
}
|
84
|
+
|
85
|
+
private static Class<?> loadJavaClass(final Ruby runtime, final String name)
|
86
|
+
throws ClassNotFoundException, LinkageError, ExceptionInInitializerError {
|
87
|
+
if ( ! Ruby.isSecurityRestricted() ) {
|
88
|
+
return Class.forName(name, true, runtime.getJRubyClassLoader());
|
89
|
+
}
|
90
|
+
return Class.forName(name);
|
91
|
+
}
|
92
|
+
|
93
|
+
public Connection connect(final String url, final String user, final String pass)
|
94
|
+
throws SQLException {
|
95
|
+
|
96
|
+
final Properties properties = (Properties) this.properties.clone();
|
97
|
+
if ( user != null ) properties.setProperty("user", user);
|
98
|
+
if ( pass != null ) properties.setProperty("password", pass);
|
99
|
+
|
100
|
+
return getDriverInstance().connect(url, properties);
|
101
|
+
}
|
102
|
+
|
103
|
+
static String buildURL(final Object url, final Map<?, ?> options) {
|
104
|
+
if ( options == null || options.isEmpty() ) {
|
105
|
+
return url.toString();
|
106
|
+
}
|
107
|
+
|
108
|
+
final StringBuilder urlWithOptions = new StringBuilder();
|
109
|
+
|
110
|
+
final String urlStr = url.toString();
|
111
|
+
if ( urlStr.indexOf('?') == -1 ) {
|
112
|
+
urlWithOptions.append(urlStr).append('?');
|
113
|
+
}
|
114
|
+
else {
|
115
|
+
urlWithOptions.append(urlStr).append('&');
|
116
|
+
}
|
117
|
+
|
118
|
+
boolean first = true;
|
119
|
+
for ( Map.Entry entry : options.entrySet() ) {
|
120
|
+
if ( ! first ) urlWithOptions.append('&');
|
121
|
+
urlWithOptions.append(entry.getKey()).append('=').append(entry.getValue());
|
122
|
+
first = false;
|
123
|
+
}
|
124
|
+
|
125
|
+
return urlWithOptions.toString();
|
126
|
+
}
|
127
|
+
|
128
|
+
}
|