activerecord-jdbc-adapter 0.9.2 → 1.1.0
Sign up to get free protection for your applications and to get access to all the features.
- data/History.txt +163 -0
- data/Manifest.txt +130 -65
- data/README.txt +64 -72
- data/lib/active_record/connection_adapters/derby_adapter.rb +1 -13
- data/lib/active_record/connection_adapters/h2_adapter.rb +1 -13
- data/lib/active_record/connection_adapters/hsqldb_adapter.rb +1 -13
- data/lib/active_record/connection_adapters/informix_adapter.rb +1 -1
- data/lib/active_record/connection_adapters/jdbc_adapter.rb +1 -633
- data/lib/active_record/connection_adapters/jndi_adapter.rb +1 -1
- 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 -13
- data/lib/active_record/connection_adapters/oracle_adapter.rb +1 -1
- data/lib/active_record/connection_adapters/postgresql_adapter.rb +1 -13
- data/lib/active_record/connection_adapters/sqlite3_adapter.rb +1 -13
- data/lib/activerecord-jdbc-adapter.rb +6 -0
- data/lib/arel/engines/sql/compilers/db2_compiler.rb +9 -0
- data/lib/arel/engines/sql/compilers/derby_compiler.rb +6 -0
- data/lib/arel/engines/sql/compilers/h2_compiler.rb +6 -0
- data/lib/arel/engines/sql/compilers/hsqldb_compiler.rb +15 -0
- data/lib/arel/engines/sql/compilers/jdbc_compiler.rb +6 -0
- data/lib/arel/engines/sql/compilers/mssql_compiler.rb +46 -0
- data/lib/arel/visitors/db2.rb +15 -0
- data/lib/arel/visitors/derby.rb +19 -0
- data/lib/arel/visitors/hsqldb.rb +24 -0
- data/lib/arel/visitors/mssql.rb +44 -0
- data/lib/arjdbc.rb +29 -0
- data/lib/arjdbc/db2.rb +2 -0
- data/lib/arjdbc/db2/adapter.rb +413 -0
- data/lib/arjdbc/derby.rb +7 -0
- data/lib/{jdbc_adapter/jdbc_derby.rb → arjdbc/derby/adapter.rb} +58 -132
- data/lib/arjdbc/derby/connection_methods.rb +18 -0
- data/lib/arjdbc/discover.rb +92 -0
- data/lib/arjdbc/firebird.rb +2 -0
- data/lib/{jdbc_adapter/jdbc_firebird.rb → arjdbc/firebird/adapter.rb} +36 -14
- data/lib/arjdbc/h2.rb +4 -0
- data/lib/arjdbc/h2/adapter.rb +36 -0
- data/lib/arjdbc/h2/connection_methods.rb +12 -0
- data/lib/arjdbc/hsqldb.rb +4 -0
- data/lib/{jdbc_adapter/jdbc_hsqldb.rb → arjdbc/hsqldb/adapter.rb} +34 -62
- data/lib/arjdbc/hsqldb/connection_methods.rb +14 -0
- data/lib/arjdbc/informix.rb +3 -0
- data/lib/{jdbc_adapter/jdbc_informix.rb → arjdbc/informix/adapter.rb} +10 -19
- data/lib/arjdbc/informix/connection_methods.rb +10 -0
- data/lib/arjdbc/jdbc.rb +2 -0
- data/lib/arjdbc/jdbc/adapter.rb +285 -0
- data/lib/arjdbc/jdbc/adapter_java.jar +0 -0
- data/lib/arjdbc/jdbc/callbacks.rb +44 -0
- data/lib/arjdbc/jdbc/column.rb +38 -0
- data/lib/arjdbc/jdbc/compatibility.rb +51 -0
- data/lib/arjdbc/jdbc/connection.rb +128 -0
- data/lib/arjdbc/jdbc/connection_methods.rb +16 -0
- data/lib/arjdbc/jdbc/core_ext.rb +24 -0
- data/lib/arjdbc/jdbc/discover.rb +18 -0
- data/lib/arjdbc/jdbc/driver.rb +44 -0
- data/lib/arjdbc/jdbc/extension.rb +47 -0
- data/lib/arjdbc/jdbc/java.rb +14 -0
- data/lib/{jdbc_adapter → arjdbc/jdbc}/jdbc.rake +36 -26
- data/lib/{jdbc_adapter → arjdbc/jdbc}/missing_functionality_helper.rb +5 -5
- data/lib/arjdbc/jdbc/quoted_primary_key.rb +28 -0
- data/lib/arjdbc/jdbc/railtie.rb +9 -0
- data/lib/arjdbc/jdbc/rake_tasks.rb +10 -0
- data/lib/arjdbc/jdbc/require_driver.rb +16 -0
- data/lib/arjdbc/jdbc/type_converter.rb +126 -0
- data/lib/arjdbc/mimer.rb +2 -0
- data/lib/{jdbc_adapter/jdbc_mimer.rb → arjdbc/mimer/adapter.rb} +17 -16
- data/lib/arjdbc/mssql.rb +4 -0
- data/lib/arjdbc/mssql/adapter.rb +472 -0
- data/lib/arjdbc/mssql/connection_methods.rb +30 -0
- data/lib/arjdbc/mssql/limit_helpers.rb +92 -0
- data/lib/{jdbc_adapter → arjdbc/mssql}/tsql_helper.rb +2 -1
- data/lib/arjdbc/mysql.rb +4 -0
- data/lib/arjdbc/mysql/adapter.rb +416 -0
- data/lib/arjdbc/mysql/connection_methods.rb +27 -0
- data/lib/arjdbc/oracle.rb +3 -0
- data/lib/{jdbc_adapter/jdbc_oracle.rb → arjdbc/oracle/adapter.rb} +68 -33
- data/lib/arjdbc/oracle/connection_methods.rb +11 -0
- data/lib/arjdbc/postgresql.rb +4 -0
- data/lib/{jdbc_adapter/jdbc_postgre.rb → arjdbc/postgresql/adapter.rb} +147 -72
- data/lib/arjdbc/postgresql/connection_methods.rb +21 -0
- data/lib/arjdbc/sqlite3.rb +4 -0
- data/lib/arjdbc/sqlite3/adapter.rb +381 -0
- data/lib/arjdbc/sqlite3/connection_methods.rb +34 -0
- data/lib/arjdbc/sybase.rb +2 -0
- data/lib/arjdbc/sybase/adapter.rb +46 -0
- data/lib/arjdbc/version.rb +8 -0
- data/lib/generators/jdbc/jdbc_generator.rb +9 -0
- data/lib/jdbc_adapter.rb +2 -27
- data/lib/jdbc_adapter/rake_tasks.rb +3 -10
- data/lib/jdbc_adapter/version.rb +3 -5
- data/lib/pg.rb +23 -1
- data/rails_generators/jdbc_generator.rb +2 -2
- data/rails_generators/templates/{jdbc.rb → config/initializers/jdbc.rb} +1 -1
- data/rails_generators/templates/{jdbc.rake → lib/tasks/jdbc.rake} +2 -2
- data/rakelib/compile.rake +5 -3
- data/rakelib/db.rake +19 -0
- data/rakelib/package.rake +12 -6
- data/rakelib/test.rake +24 -14
- data/src/java/{jdbc_adapter/JdbcDerbySpec.java → arjdbc/derby/DerbyModule.java} +63 -34
- data/src/java/arjdbc/h2/H2RubyJdbcConnection.java +70 -0
- data/src/java/{jdbc_adapter/MssqlRubyJdbcConnection.java → arjdbc/informix/InformixRubyJdbcConnection.java} +15 -12
- data/src/java/{jdbc_adapter/JdbcAdapterInternalService.java → arjdbc/jdbc/AdapterJavaService.java} +21 -8
- data/src/java/{jdbc_adapter → arjdbc/jdbc}/JdbcConnectionFactory.java +6 -6
- data/src/java/{jdbc_adapter → arjdbc/jdbc}/RubyJdbcConnection.java +210 -67
- data/src/java/arjdbc/jdbc/SQLBlock.java +48 -0
- data/src/java/arjdbc/mssql/MssqlRubyJdbcConnection.java +127 -0
- data/src/java/{jdbc_adapter/JdbcMySQLSpec.java → arjdbc/mysql/MySQLModule.java} +12 -12
- data/src/java/arjdbc/mysql/MySQLRubyJdbcConnection.java +87 -0
- data/src/java/arjdbc/oracle/OracleRubyJdbcConnection.java +85 -0
- data/src/java/{jdbc_adapter/PostgresRubyJdbcConnection.java → arjdbc/postgresql/PostgresqlRubyJdbcConnection.java} +11 -9
- data/src/java/arjdbc/sqlite3/Sqlite3RubyJdbcConnection.java +64 -0
- data/test/abstract_db_create.rb +98 -19
- data/test/activerecord/connection_adapters/type_conversion_test.rb +1 -1
- data/test/db/db2.rb +4 -2
- data/test/db/derby.rb +12 -14
- data/test/db/hsqldb.rb +3 -2
- data/test/db/jndi_config.rb +22 -12
- data/test/db/mssql.rb +5 -5
- data/test/db/sqlite3.rb +2 -6
- data/test/db2_simple_test.rb +56 -0
- data/test/derby_migration_test.rb +50 -3
- data/test/derby_simple_test.rb +87 -0
- data/test/generic_jdbc_connection_test.rb +21 -1
- data/test/h2_simple_test.rb +35 -0
- data/test/jdbc_common.rb +4 -5
- data/test/jndi_callbacks_test.rb +3 -1
- data/test/jndi_test.rb +1 -11
- data/test/models/entry.rb +20 -0
- data/test/models/mixed_case.rb +7 -2
- data/test/models/string_id.rb +18 -0
- data/test/mssql_db_create_test.rb +26 -0
- data/test/mssql_identity_insert_test.rb +19 -0
- data/test/mssql_legacy_types_test.rb +58 -0
- data/test/mssql_limit_offset_test.rb +136 -0
- data/test/mssql_multibyte_test.rb +18 -0
- data/test/mssql_simple_test.rb +49 -0
- data/test/mysql_db_create_test.rb +8 -6
- data/test/mysql_info_test.rb +123 -0
- data/test/mysql_simple_test.rb +19 -1
- data/test/oracle_simple_test.rb +9 -20
- data/test/oracle_specific_test.rb +83 -0
- data/test/pick_rails_version.rb +3 -0
- data/test/postgres_db_create_test.rb +11 -0
- data/test/postgres_drop_db_test.rb +16 -0
- data/test/postgres_mixed_case_test.rb +11 -1
- data/test/postgres_nonseq_pkey_test.rb +0 -2
- data/test/postgres_schema_search_path_test.rb +44 -0
- data/test/postgres_simple_test.rb +53 -0
- data/test/postgres_table_alias_length_test.rb +15 -0
- data/test/simple.rb +103 -9
- data/test/sqlite3_simple_test.rb +1 -0
- data/test/sybase_jtds_simple_test.rb +22 -0
- metadata +239 -137
- data/lib/active_record/connection_adapters/cachedb_adapter.rb +0 -1
- data/lib/active_record/connection_adapters/jdbc_adapter_spec.rb +0 -26
- data/lib/jdbc_adapter/jdbc_adapter_internal.jar +0 -0
- data/lib/jdbc_adapter/jdbc_cachedb.rb +0 -33
- data/lib/jdbc_adapter/jdbc_db2.rb +0 -191
- data/lib/jdbc_adapter/jdbc_mssql.rb +0 -333
- data/lib/jdbc_adapter/jdbc_mysql.rb +0 -236
- data/lib/jdbc_adapter/jdbc_sqlite3.rb +0 -379
- data/lib/jdbc_adapter/jdbc_sybase.rb +0 -50
- data/src/java/jdbc_adapter/SQLBlock.java +0 -27
- data/src/java/jdbc_adapter/Sqlite3RubyJdbcConnection.java +0 -41
- data/test/cachedb_simple_test.rb +0 -6
- data/test/db/cachedb.rb +0 -9
- data/test/jdbc_adapter/jdbc_db2_test.rb +0 -26
- data/test/jdbc_adapter/jdbc_sybase_test.rb +0 -33
- data/test/minirunit.rb +0 -109
- data/test/minirunit/testConnect.rb +0 -14
- data/test/minirunit/testH2.rb +0 -73
- data/test/minirunit/testHsqldb.rb +0 -73
- data/test/minirunit/testLoadActiveRecord.rb +0 -3
- data/test/minirunit/testMysql.rb +0 -83
- data/test/minirunit/testRawSelect.rb +0 -24
@@ -0,0 +1,70 @@
|
|
1
|
+
/*
|
2
|
+
**** BEGIN LICENSE BLOCK *****
|
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.h2;
|
28
|
+
|
29
|
+
import java.sql.ResultSet;
|
30
|
+
import java.sql.SQLException;
|
31
|
+
import java.sql.Types;
|
32
|
+
|
33
|
+
import arjdbc.jdbc.RubyJdbcConnection;
|
34
|
+
|
35
|
+
import org.jruby.Ruby;
|
36
|
+
import org.jruby.RubyClass;
|
37
|
+
import org.jruby.runtime.ObjectAllocator;
|
38
|
+
import org.jruby.runtime.builtin.IRubyObject;
|
39
|
+
|
40
|
+
/**
|
41
|
+
*
|
42
|
+
* @author nicksieger
|
43
|
+
*/
|
44
|
+
public class H2RubyJdbcConnection extends RubyJdbcConnection {
|
45
|
+
protected H2RubyJdbcConnection(Ruby runtime, RubyClass metaClass) {
|
46
|
+
super(runtime, metaClass);
|
47
|
+
}
|
48
|
+
|
49
|
+
public static RubyClass createH2JdbcConnectionClass(Ruby runtime, RubyClass jdbcConnection) {
|
50
|
+
RubyClass clazz = RubyJdbcConnection.getConnectionAdapters(runtime).defineClassUnder("H2JdbcConnection",
|
51
|
+
jdbcConnection, H2_JDBCCONNECTION_ALLOCATOR);
|
52
|
+
clazz.defineAnnotatedMethods(H2RubyJdbcConnection.class);
|
53
|
+
|
54
|
+
return clazz;
|
55
|
+
}
|
56
|
+
|
57
|
+
private static ObjectAllocator H2_JDBCCONNECTION_ALLOCATOR = new ObjectAllocator() {
|
58
|
+
public IRubyObject allocate(Ruby runtime, RubyClass klass) {
|
59
|
+
return new H2RubyJdbcConnection(runtime, klass);
|
60
|
+
}
|
61
|
+
};
|
62
|
+
|
63
|
+
/**
|
64
|
+
* H2 supports schemas.
|
65
|
+
*/
|
66
|
+
@Override
|
67
|
+
protected boolean databaseSupportsSchemas() {
|
68
|
+
return true;
|
69
|
+
}
|
70
|
+
}
|
@@ -1,6 +1,6 @@
|
|
1
1
|
/*
|
2
2
|
**** BEGIN LICENSE BLOCK *****
|
3
|
-
* Copyright (c) 2006-
|
3
|
+
* Copyright (c) 2006-2010 Nick Sieger <nick@nicksieger.com>
|
4
4
|
* Copyright (c) 2006-2007 Ola Bini <ola.bini@gmail.com>
|
5
5
|
* Copyright (c) 2008-2009 Thomas E Enebo <enebo@acm.org>
|
6
6
|
*
|
@@ -23,11 +23,15 @@
|
|
23
23
|
* OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
24
24
|
* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
25
25
|
***** END LICENSE BLOCK *****/
|
26
|
-
|
26
|
+
|
27
|
+
package arjdbc.informix;
|
27
28
|
|
28
29
|
import java.sql.ResultSet;
|
29
30
|
import java.sql.SQLException;
|
30
31
|
import java.sql.Types;
|
32
|
+
|
33
|
+
import arjdbc.jdbc.RubyJdbcConnection;
|
34
|
+
|
31
35
|
import org.jruby.Ruby;
|
32
36
|
import org.jruby.RubyClass;
|
33
37
|
import org.jruby.runtime.ObjectAllocator;
|
@@ -37,28 +41,27 @@ import org.jruby.runtime.builtin.IRubyObject;
|
|
37
41
|
*
|
38
42
|
* @author nicksieger
|
39
43
|
*/
|
40
|
-
public class
|
41
|
-
|
42
|
-
protected MssqlRubyJdbcConnection(Ruby runtime, RubyClass metaClass) {
|
44
|
+
public class InformixRubyJdbcConnection extends RubyJdbcConnection {
|
45
|
+
protected InformixRubyJdbcConnection(Ruby runtime, RubyClass metaClass) {
|
43
46
|
super(runtime, metaClass);
|
44
47
|
}
|
45
48
|
|
46
|
-
public static RubyClass
|
47
|
-
RubyClass clazz = RubyJdbcConnection.getConnectionAdapters(runtime).defineClassUnder("
|
48
|
-
jdbcConnection,
|
49
|
-
clazz.defineAnnotatedMethods(
|
49
|
+
public static RubyClass createInformixJdbcConnectionClass(Ruby runtime, RubyClass jdbcConnection) {
|
50
|
+
RubyClass clazz = RubyJdbcConnection.getConnectionAdapters(runtime).defineClassUnder("InformixJdbcConnection",
|
51
|
+
jdbcConnection, INFORMIX_JDBCCONNECTION_ALLOCATOR);
|
52
|
+
clazz.defineAnnotatedMethods(InformixRubyJdbcConnection.class);
|
50
53
|
|
51
54
|
return clazz;
|
52
55
|
}
|
53
|
-
private static ObjectAllocator MSSQL_JDBCCONNECTION_ALLOCATOR = new ObjectAllocator() {
|
54
56
|
|
57
|
+
private static ObjectAllocator INFORMIX_JDBCCONNECTION_ALLOCATOR = new ObjectAllocator() {
|
55
58
|
public IRubyObject allocate(Ruby runtime, RubyClass klass) {
|
56
|
-
return new
|
59
|
+
return new InformixRubyJdbcConnection(runtime, klass);
|
57
60
|
}
|
58
61
|
};
|
59
62
|
|
60
63
|
/**
|
61
|
-
* Treat LONGVARCHAR as CLOB on
|
64
|
+
* Treat LONGVARCHAR as CLOB on Informix for purposes of converting a JDBC value to Ruby.
|
62
65
|
*/
|
63
66
|
@Override
|
64
67
|
protected IRubyObject jdbcToRuby(Ruby runtime, int column, int type, ResultSet resultSet)
|
data/src/java/{jdbc_adapter/JdbcAdapterInternalService.java → arjdbc/jdbc/AdapterJavaService.java}
RENAMED
@@ -1,6 +1,6 @@
|
|
1
1
|
/*
|
2
2
|
**** BEGIN LICENSE BLOCK *****
|
3
|
-
* Copyright (c) 2006-
|
3
|
+
* Copyright (c) 2006-2010 Nick Sieger <nick@nicksieger.com>
|
4
4
|
* Copyright (c) 2006-2007 Ola Bini <ola.bini@gmail.com>
|
5
5
|
* Copyright (c) 2008-2009 Thomas E Enebo <enebo@acm.org>
|
6
6
|
*
|
@@ -24,10 +24,20 @@
|
|
24
24
|
* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
25
25
|
***** END LICENSE BLOCK *****/
|
26
26
|
|
27
|
-
package
|
27
|
+
package arjdbc.jdbc;
|
28
28
|
|
29
29
|
import java.io.IOException;
|
30
30
|
|
31
|
+
import arjdbc.derby.DerbyModule;
|
32
|
+
import arjdbc.h2.H2RubyJdbcConnection;
|
33
|
+
import arjdbc.informix.InformixRubyJdbcConnection;
|
34
|
+
import arjdbc.mssql.MssqlRubyJdbcConnection;
|
35
|
+
import arjdbc.mysql.MySQLModule;
|
36
|
+
import arjdbc.mysql.MySQLRubyJdbcConnection;
|
37
|
+
import arjdbc.oracle.OracleRubyJdbcConnection;
|
38
|
+
import arjdbc.postgresql.PostgresqlRubyJdbcConnection;
|
39
|
+
import arjdbc.sqlite3.Sqlite3RubyJdbcConnection;
|
40
|
+
|
31
41
|
import org.jruby.Ruby;
|
32
42
|
import org.jruby.RubyClass;
|
33
43
|
import org.jruby.RubyModule;
|
@@ -35,19 +45,22 @@ import org.jruby.RubyObjectAdapter;
|
|
35
45
|
import org.jruby.javasupport.JavaEmbedUtils;
|
36
46
|
import org.jruby.runtime.load.BasicLibraryService;
|
37
47
|
|
38
|
-
public class
|
48
|
+
public class AdapterJavaService implements BasicLibraryService {
|
39
49
|
private static RubyObjectAdapter rubyApi;
|
40
50
|
|
41
51
|
public boolean basicLoad(final Ruby runtime) throws IOException {
|
42
52
|
RubyClass jdbcConnection = RubyJdbcConnection.createJdbcConnectionClass(runtime);
|
43
|
-
|
53
|
+
PostgresqlRubyJdbcConnection.createPostgresqlJdbcConnectionClass(runtime, jdbcConnection);
|
44
54
|
MssqlRubyJdbcConnection.createMssqlJdbcConnectionClass(runtime, jdbcConnection);
|
55
|
+
InformixRubyJdbcConnection.createInformixJdbcConnectionClass(runtime, jdbcConnection);
|
56
|
+
OracleRubyJdbcConnection.createOracleJdbcConnectionClass(runtime, jdbcConnection);
|
45
57
|
Sqlite3RubyJdbcConnection.createSqlite3JdbcConnectionClass(runtime, jdbcConnection);
|
46
|
-
|
47
|
-
|
58
|
+
H2RubyJdbcConnection.createH2JdbcConnectionClass(runtime, jdbcConnection);
|
59
|
+
MySQLRubyJdbcConnection.createMySQLJdbcConnectionClass(runtime, jdbcConnection);
|
60
|
+
RubyModule arJdbc = runtime.getOrCreateModule("ArJdbc");
|
48
61
|
rubyApi = JavaEmbedUtils.newObjectAdapter();
|
49
|
-
|
50
|
-
|
62
|
+
MySQLModule.load(arJdbc);
|
63
|
+
DerbyModule.load(arJdbc, rubyApi);
|
51
64
|
return true;
|
52
65
|
}
|
53
66
|
}
|
@@ -1,7 +1,7 @@
|
|
1
1
|
/***** BEGIN LICENSE BLOCK *****
|
2
|
-
* Copyright (c) 2006-
|
2
|
+
* Copyright (c) 2006-2010 Nick Sieger <nick@nicksieger.com>
|
3
3
|
* Copyright (c) 2006-2007 Ola Bini <ola.bini@gmail.com>
|
4
|
-
*
|
4
|
+
*
|
5
5
|
* Permission is hereby granted, free of charge, to any person obtaining
|
6
6
|
* a copy of this software and associated documentation files (the
|
7
7
|
* "Software"), to deal in the Software without restriction, including
|
@@ -9,10 +9,10 @@
|
|
9
9
|
* distribute, sublicense, and/or sell copies of the Software, and to
|
10
10
|
* permit persons to whom the Software is furnished to do so, subject to
|
11
11
|
* the following conditions:
|
12
|
-
*
|
12
|
+
*
|
13
13
|
* The above copyright notice and this permission notice shall be
|
14
14
|
* included in all copies or substantial portions of the Software.
|
15
|
-
*
|
15
|
+
*
|
16
16
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
17
17
|
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
18
18
|
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
@@ -22,13 +22,13 @@
|
|
22
22
|
* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
23
23
|
***** END LICENSE BLOCK *****/
|
24
24
|
|
25
|
-
package
|
25
|
+
package arjdbc.jdbc;
|
26
26
|
|
27
27
|
import java.sql.Connection;
|
28
28
|
|
29
29
|
/**
|
30
30
|
* Interface to be implemented in Ruby for retrieving a new connection
|
31
|
-
*
|
31
|
+
*
|
32
32
|
* @author nicksieger
|
33
33
|
*/
|
34
34
|
public interface JdbcConnectionFactory {
|
@@ -1,6 +1,6 @@
|
|
1
1
|
/*
|
2
2
|
**** BEGIN LICENSE BLOCK *****
|
3
|
-
* Copyright (c) 2006-
|
3
|
+
* Copyright (c) 2006-2010 Nick Sieger <nick@nicksieger.com>
|
4
4
|
* Copyright (c) 2006-2007 Ola Bini <ola.bini@gmail.com>
|
5
5
|
* Copyright (c) 2008-2009 Thomas E Enebo <enebo@acm.org>
|
6
6
|
*
|
@@ -23,25 +23,14 @@
|
|
23
23
|
* OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
24
24
|
* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
25
25
|
***** END LICENSE BLOCK *****/
|
26
|
-
package
|
26
|
+
package arjdbc.jdbc;
|
27
27
|
|
28
28
|
import java.io.ByteArrayInputStream;
|
29
29
|
import java.io.IOException;
|
30
30
|
import java.io.InputStream;
|
31
31
|
import java.io.Reader;
|
32
32
|
import java.io.StringReader;
|
33
|
-
import
|
34
|
-
import org.jruby.RubyClass;
|
35
|
-
import org.jruby.RubyModule;
|
36
|
-
import org.jruby.RubyObject;
|
37
|
-
import org.jruby.RubyObjectAdapter;
|
38
|
-
import org.jruby.anno.JRubyMethod;
|
39
|
-
import org.jruby.javasupport.JavaEmbedUtils;
|
40
|
-
import org.jruby.runtime.ObjectAllocator;
|
41
|
-
import org.jruby.runtime.ThreadContext;
|
42
|
-
import org.jruby.runtime.builtin.IRubyObject;
|
43
|
-
import org.jruby.util.ByteList;
|
44
|
-
|
33
|
+
import java.math.BigInteger;
|
45
34
|
import java.sql.Connection;
|
46
35
|
import java.sql.DatabaseMetaData;
|
47
36
|
import java.sql.PreparedStatement;
|
@@ -57,17 +46,31 @@ import java.util.ArrayList;
|
|
57
46
|
import java.util.Calendar;
|
58
47
|
import java.util.Date;
|
59
48
|
import java.util.List;
|
49
|
+
|
50
|
+
import org.jruby.Ruby;
|
60
51
|
import org.jruby.RubyArray;
|
52
|
+
import org.jruby.RubyBignum;
|
53
|
+
import org.jruby.RubyClass;
|
61
54
|
import org.jruby.RubyHash;
|
55
|
+
import org.jruby.RubyModule;
|
62
56
|
import org.jruby.RubyNumeric;
|
57
|
+
import org.jruby.RubyObject;
|
58
|
+
import org.jruby.RubyObjectAdapter;
|
63
59
|
import org.jruby.RubyString;
|
64
60
|
import org.jruby.RubySymbol;
|
65
61
|
import org.jruby.RubyTime;
|
62
|
+
import org.jruby.anno.JRubyMethod;
|
66
63
|
import org.jruby.exceptions.RaiseException;
|
67
64
|
import org.jruby.javasupport.Java;
|
65
|
+
import org.jruby.javasupport.JavaEmbedUtils;
|
68
66
|
import org.jruby.javasupport.JavaObject;
|
67
|
+
import org.jruby.javasupport.util.RuntimeHelpers;
|
69
68
|
import org.jruby.runtime.Arity;
|
70
69
|
import org.jruby.runtime.Block;
|
70
|
+
import org.jruby.runtime.ObjectAllocator;
|
71
|
+
import org.jruby.runtime.ThreadContext;
|
72
|
+
import org.jruby.runtime.builtin.IRubyObject;
|
73
|
+
import org.jruby.util.ByteList;
|
71
74
|
|
72
75
|
/**
|
73
76
|
* Part of our ActiveRecord::ConnectionAdapters::Connection impl.
|
@@ -117,35 +120,51 @@ public class RubyJdbcConnection extends RubyObject {
|
|
117
120
|
throws SQLException, IOException {
|
118
121
|
return (IRubyObject) withConnectionAndRetry(context, new SQLBlock() {
|
119
122
|
public Object call(Connection c) throws SQLException {
|
120
|
-
ResultSet results = null;
|
123
|
+
ResultSet results = null, pkeys = null;
|
121
124
|
try {
|
122
125
|
String table_name = rubyApi.convertToRubyString(args[0]).getUnicodeValue();
|
123
126
|
String schemaName = null;
|
124
127
|
|
125
|
-
|
126
|
-
if(
|
127
|
-
|
128
|
-
table_name = table_name.substring(index + 1);
|
128
|
+
final String[] name_parts = table_name.split( "\\." );
|
129
|
+
if ( name_parts.length > 3 ) {
|
130
|
+
throw new SQLException("Table name '" + table_name + "' should not contain more than 2 '.'");
|
129
131
|
}
|
130
132
|
|
131
133
|
DatabaseMetaData metadata = c.getMetaData();
|
134
|
+
String clzName = metadata.getClass().getName().toLowerCase();
|
135
|
+
boolean isDB2 = clzName.indexOf("db2") != -1 || clzName.indexOf("as400") != -1;
|
136
|
+
|
137
|
+
String catalog = c.getCatalog();
|
138
|
+
if( name_parts.length == 2 ) {
|
139
|
+
schemaName = name_parts[0];
|
140
|
+
table_name = name_parts[1];
|
141
|
+
}
|
142
|
+
else if ( name_parts.length == 3 ) {
|
143
|
+
catalog = name_parts[0];
|
144
|
+
schemaName = name_parts[1];
|
145
|
+
table_name = name_parts[2];
|
146
|
+
}
|
132
147
|
|
133
148
|
if(args.length > 2 && schemaName == null) schemaName = toStringOrNull(args[2]);
|
134
149
|
|
135
150
|
if (schemaName != null) schemaName = caseConvertIdentifierForJdbc(metadata, schemaName);
|
136
151
|
table_name = caseConvertIdentifierForJdbc(metadata, table_name);
|
137
152
|
|
153
|
+
if (schemaName != null && !isDB2 && !databaseSupportsSchemas()) { catalog = schemaName; }
|
154
|
+
|
138
155
|
String[] tableTypes = new String[]{"TABLE","VIEW","SYNONYM"};
|
139
156
|
RubyArray matchingTables = (RubyArray) tableLookupBlock(context.getRuntime(),
|
140
|
-
|
157
|
+
catalog, schemaName, table_name, tableTypes, false).call(c);
|
141
158
|
if (matchingTables.isEmpty()) {
|
142
159
|
throw new SQLException("Table " + table_name + " does not exist");
|
143
160
|
}
|
144
161
|
|
145
|
-
results = metadata.getColumns(
|
146
|
-
|
162
|
+
results = metadata.getColumns(catalog,schemaName,table_name,null);
|
163
|
+
pkeys = metadata.getPrimaryKeys(catalog,schemaName,table_name);
|
164
|
+
return unmarshal_columns(context, metadata, results, pkeys);
|
147
165
|
} finally {
|
148
166
|
close(results);
|
167
|
+
close(pkeys);
|
149
168
|
}
|
150
169
|
}
|
151
170
|
});
|
@@ -192,15 +211,62 @@ public class RubyJdbcConnection extends RubyObject {
|
|
192
211
|
return setConnection(null);
|
193
212
|
}
|
194
213
|
|
214
|
+
@JRubyMethod
|
215
|
+
public IRubyObject execute(final ThreadContext context, final IRubyObject sql) {
|
216
|
+
return (IRubyObject) withConnectionAndRetry(context, new SQLBlock() {
|
217
|
+
public Object call(Connection c) throws SQLException {
|
218
|
+
Statement stmt = null;
|
219
|
+
String query = rubyApi.convertToRubyString(sql).getUnicodeValue();
|
220
|
+
try {
|
221
|
+
stmt = c.createStatement();
|
222
|
+
if (genericExecute(stmt, query)) {
|
223
|
+
return unmarshalResults(context, c.getMetaData(), stmt, false);
|
224
|
+
} else {
|
225
|
+
return unmarshalKeysOrUpdateCount(context, c, stmt);
|
226
|
+
}
|
227
|
+
} catch (SQLException sqe) {
|
228
|
+
if (context.getRuntime().isDebug()) {
|
229
|
+
System.out.println("Error SQL: " + query);
|
230
|
+
}
|
231
|
+
throw sqe;
|
232
|
+
} finally {
|
233
|
+
close(stmt);
|
234
|
+
}
|
235
|
+
}
|
236
|
+
});
|
237
|
+
}
|
238
|
+
|
239
|
+
protected boolean genericExecute(Statement stmt, String query) throws SQLException {
|
240
|
+
return stmt.execute(query);
|
241
|
+
}
|
242
|
+
|
243
|
+
protected IRubyObject unmarshalKeysOrUpdateCount(ThreadContext context, Connection c, Statement stmt) throws SQLException {
|
244
|
+
IRubyObject key = context.getRuntime().getNil();
|
245
|
+
if (c.getMetaData().supportsGetGeneratedKeys()) {
|
246
|
+
key = unmarshal_id_result(context.getRuntime(), stmt.getGeneratedKeys());
|
247
|
+
}
|
248
|
+
if (key.isNil()) {
|
249
|
+
return context.getRuntime().newFixnum(stmt.getUpdateCount());
|
250
|
+
} else {
|
251
|
+
return key;
|
252
|
+
}
|
253
|
+
}
|
254
|
+
|
195
255
|
@JRubyMethod(name = "execute_id_insert", required = 2)
|
196
|
-
public IRubyObject execute_id_insert(ThreadContext context, final IRubyObject sql,
|
256
|
+
public IRubyObject execute_id_insert(final ThreadContext context, final IRubyObject sql,
|
197
257
|
final IRubyObject id) throws SQLException {
|
198
258
|
return (IRubyObject) withConnectionAndRetry(context, new SQLBlock() {
|
199
259
|
public Object call(Connection c) throws SQLException {
|
200
|
-
|
260
|
+
String insert = rubyApi.convertToRubyString(sql).getUnicodeValue();
|
261
|
+
PreparedStatement ps = c.prepareStatement(insert);
|
201
262
|
try {
|
202
263
|
ps.setLong(1, RubyNumeric.fix2long(id));
|
203
264
|
ps.executeUpdate();
|
265
|
+
} catch (SQLException sqe) {
|
266
|
+
if (context.getRuntime().isDebug()) {
|
267
|
+
System.out.println("Error SQL: " + insert);
|
268
|
+
}
|
269
|
+
throw sqe;
|
204
270
|
} finally {
|
205
271
|
close(ps);
|
206
272
|
}
|
@@ -215,10 +281,16 @@ public class RubyJdbcConnection extends RubyObject {
|
|
215
281
|
return (IRubyObject) withConnectionAndRetry(context, new SQLBlock() {
|
216
282
|
public Object call(Connection c) throws SQLException {
|
217
283
|
Statement stmt = null;
|
284
|
+
String insert = rubyApi.convertToRubyString(sql).getUnicodeValue();
|
218
285
|
try {
|
219
286
|
stmt = c.createStatement();
|
220
|
-
stmt.executeUpdate(
|
287
|
+
stmt.executeUpdate(insert, Statement.RETURN_GENERATED_KEYS);
|
221
288
|
return unmarshal_id_result(context.getRuntime(), stmt.getGeneratedKeys());
|
289
|
+
} catch (SQLException sqe) {
|
290
|
+
if (context.getRuntime().isDebug()) {
|
291
|
+
System.out.println("Error SQL: " + insert);
|
292
|
+
}
|
293
|
+
throw sqe;
|
222
294
|
} finally {
|
223
295
|
close(stmt);
|
224
296
|
}
|
@@ -252,6 +324,11 @@ public class RubyJdbcConnection extends RubyObject {
|
|
252
324
|
stmt = c.createStatement();
|
253
325
|
stmt.setMaxRows(maxRows);
|
254
326
|
return unmarshalResult(context, metadata, stmt.executeQuery(query), false);
|
327
|
+
} catch (SQLException sqe) {
|
328
|
+
if (context.getRuntime().isDebug()) {
|
329
|
+
System.out.println("Error SQL: " + query);
|
330
|
+
}
|
331
|
+
throw sqe;
|
255
332
|
} finally {
|
256
333
|
close(stmt);
|
257
334
|
}
|
@@ -265,9 +342,15 @@ public class RubyJdbcConnection extends RubyObject {
|
|
265
342
|
return (IRubyObject) withConnectionAndRetry(context, new SQLBlock() {
|
266
343
|
public Object call(Connection c) throws SQLException {
|
267
344
|
Statement stmt = null;
|
345
|
+
String update = rubyApi.convertToRubyString(sql).getUnicodeValue();
|
268
346
|
try {
|
269
347
|
stmt = c.createStatement();
|
270
|
-
return context.getRuntime().newFixnum((long)stmt.executeUpdate(
|
348
|
+
return context.getRuntime().newFixnum((long)stmt.executeUpdate(update));
|
349
|
+
} catch (SQLException sqe) {
|
350
|
+
if (context.getRuntime().isDebug()) {
|
351
|
+
System.out.println("Error SQL: " + update);
|
352
|
+
}
|
353
|
+
throw sqe;
|
271
354
|
} finally {
|
272
355
|
close(stmt);
|
273
356
|
}
|
@@ -583,10 +666,11 @@ public class RubyJdbcConnection extends RubyObject {
|
|
583
666
|
public static String caseConvertIdentifierForJdbc(DatabaseMetaData metadata, String value)
|
584
667
|
throws SQLException {
|
585
668
|
if (value == null) return null;
|
669
|
+
boolean isPostgres = metadata.getDatabaseProductName().equals("PostgreSQL");
|
586
670
|
|
587
671
|
if (metadata.storesUpperCaseIdentifiers()) {
|
588
672
|
return value.toUpperCase();
|
589
|
-
} else if (metadata.storesLowerCaseIdentifiers()) {
|
673
|
+
} else if (metadata.storesLowerCaseIdentifiers() && ! isPostgres) {
|
590
674
|
return value.toLowerCase();
|
591
675
|
}
|
592
676
|
|
@@ -647,6 +731,14 @@ public class RubyJdbcConnection extends RubyObject {
|
|
647
731
|
return conn;
|
648
732
|
}
|
649
733
|
|
734
|
+
protected IRubyObject getAdapter(ThreadContext context) {
|
735
|
+
return callMethod(context, "adapter");
|
736
|
+
}
|
737
|
+
|
738
|
+
protected IRubyObject getJdbcColumnClass(ThreadContext context) {
|
739
|
+
return getAdapter(context).callMethod(context, "jdbc_column_class");
|
740
|
+
}
|
741
|
+
|
650
742
|
protected JdbcConnectionFactory getConnectionFactory() throws RaiseException {
|
651
743
|
IRubyObject connection_factory = getInstanceVariable("@connection_factory");
|
652
744
|
JdbcConnectionFactory factory = null;
|
@@ -729,12 +821,18 @@ public class RubyJdbcConnection extends RubyObject {
|
|
729
821
|
}
|
730
822
|
|
731
823
|
protected IRubyObject integerToRuby(Ruby runtime, ResultSet resultSet, long longValue)
|
732
|
-
throws SQLException
|
824
|
+
throws SQLException {
|
733
825
|
if (longValue == 0 && resultSet.wasNull()) return runtime.getNil();
|
734
826
|
|
735
827
|
return runtime.newFixnum(longValue);
|
736
828
|
}
|
737
829
|
|
830
|
+
protected IRubyObject bigIntegerToRuby(Ruby runtime, ResultSet resultSet, String bigint) throws SQLException {
|
831
|
+
if (bigint == null && resultSet.wasNull()) return runtime.getNil();
|
832
|
+
|
833
|
+
return RubyBignum.bignorm(runtime, new BigInteger(bigint));
|
834
|
+
}
|
835
|
+
|
738
836
|
protected IRubyObject jdbcToRuby(Ruby runtime, int column, int type, ResultSet resultSet)
|
739
837
|
throws SQLException {
|
740
838
|
try {
|
@@ -749,10 +847,14 @@ public class RubyJdbcConnection extends RubyObject {
|
|
749
847
|
return readerToRuby(runtime, resultSet, resultSet.getCharacterStream(column));
|
750
848
|
case Types.TIMESTAMP:
|
751
849
|
return timestampToRuby(runtime, resultSet, resultSet.getTimestamp(column));
|
752
|
-
case Types.INTEGER:
|
850
|
+
case Types.INTEGER:
|
851
|
+
case Types.SMALLINT:
|
852
|
+
case Types.TINYINT:
|
753
853
|
return integerToRuby(runtime, resultSet, resultSet.getLong(column));
|
754
854
|
case Types.REAL:
|
755
855
|
return doubleToRuby(runtime, resultSet, resultSet.getDouble(column));
|
856
|
+
case Types.BIGINT:
|
857
|
+
return bigIntegerToRuby(runtime, resultSet, resultSet.getString(column));
|
756
858
|
default:
|
757
859
|
return stringToRuby(runtime, resultSet, resultSet.getString(column));
|
758
860
|
}
|
@@ -769,8 +871,9 @@ public class RubyJdbcConnection extends RubyObject {
|
|
769
871
|
RubyHash row = RubyHash.newHash(runtime);
|
770
872
|
|
771
873
|
for (int i = 0; i < columnCount; i++) {
|
772
|
-
row.op_aset(context, columns[i].name, jdbcToRuby(runtime, i
|
874
|
+
row.op_aset(context, columns[i].name, jdbcToRuby(runtime, columns[i].index, columns[i].type, resultSet));
|
773
875
|
}
|
876
|
+
|
774
877
|
results.add(row);
|
775
878
|
}
|
776
879
|
}
|
@@ -900,10 +1003,12 @@ public class RubyJdbcConnection extends RubyObject {
|
|
900
1003
|
DatabaseMetaData metadata = c.getMetaData();
|
901
1004
|
String clzName = metadata.getClass().getName().toLowerCase();
|
902
1005
|
boolean isOracle = clzName.indexOf("oracle") != -1 || clzName.indexOf("oci") != -1;
|
1006
|
+
boolean isDerby = clzName.indexOf("derby") != 1;
|
903
1007
|
|
904
1008
|
String realschema = schemapat;
|
905
1009
|
String realtablepat = tablepat;
|
906
1010
|
|
1011
|
+
if (isDerby && realschema != null && realschema.equals("")) realschema = null; // Derby doesn't like empty-string schema name
|
907
1012
|
if (realtablepat != null) realtablepat = caseConvertIdentifierForJdbc(metadata, realtablepat);
|
908
1013
|
if (realschema != null) realschema = caseConvertIdentifierForJdbc(metadata, realschema);
|
909
1014
|
|
@@ -931,26 +1036,29 @@ public class RubyJdbcConnection extends RubyObject {
|
|
931
1036
|
}
|
932
1037
|
|
933
1038
|
protected IRubyObject timestampToRuby(Ruby runtime, ResultSet resultSet, Timestamp time)
|
934
|
-
throws SQLException
|
1039
|
+
throws SQLException {
|
935
1040
|
if (time == null && resultSet.wasNull()) return runtime.getNil();
|
936
1041
|
|
937
1042
|
String str = time.toString();
|
938
1043
|
if (str.endsWith(" 00:00:00.0")) {
|
939
1044
|
str = str.substring(0, str.length() - (" 00:00:00.0".length()));
|
940
1045
|
}
|
1046
|
+
if (str.endsWith(".0")) {
|
1047
|
+
str = str.substring(0, str.length() - (".0".length()));
|
1048
|
+
}
|
941
1049
|
|
942
1050
|
return RubyString.newUnicodeString(runtime, str);
|
943
1051
|
}
|
944
1052
|
|
945
|
-
|
946
|
-
|
947
|
-
|
948
|
-
|
949
|
-
|
950
|
-
|
951
|
-
|
1053
|
+
protected static final int COLUMN_NAME = 4;
|
1054
|
+
protected static final int DATA_TYPE = 5;
|
1055
|
+
protected static final int TYPE_NAME = 6;
|
1056
|
+
protected static final int COLUMN_SIZE = 7;
|
1057
|
+
protected static final int DECIMAL_DIGITS = 9;
|
1058
|
+
protected static final int COLUMN_DEF = 13;
|
1059
|
+
protected static final int IS_NULLABLE = 18;
|
952
1060
|
|
953
|
-
|
1061
|
+
protected int intFromResultSet(ResultSet resultSet, int column) throws SQLException {
|
954
1062
|
int precision = resultSet.getInt(column);
|
955
1063
|
|
956
1064
|
return precision == 0 && resultSet.wasNull() ? -1 : precision;
|
@@ -959,20 +1067,11 @@ public class RubyJdbcConnection extends RubyObject {
|
|
959
1067
|
/**
|
960
1068
|
* Create a string which represents a sql type usable by Rails from the resultSet column
|
961
1069
|
* metadata object.
|
962
|
-
*
|
963
|
-
* @param numberAsBoolean the database uses decimal as a boolean data type
|
964
|
-
* because it does not support optional SQL92 type or mandatory SQL99
|
965
|
-
* booleans.
|
966
1070
|
*/
|
967
|
-
|
1071
|
+
protected String typeFromResultSet(ResultSet resultSet) throws SQLException {
|
968
1072
|
int precision = intFromResultSet(resultSet, COLUMN_SIZE);
|
969
1073
|
int scale = intFromResultSet(resultSet, DECIMAL_DIGITS);
|
970
1074
|
|
971
|
-
// Assume db's which use decimal for boolean will not also specify a
|
972
|
-
// valid precision 1 decimal also. Seems sketchy to me...
|
973
|
-
if (numberAsBoolean && precision != 1 &&
|
974
|
-
resultSet.getInt(DATA_TYPE) == java.sql.Types.DECIMAL) precision = -1;
|
975
|
-
|
976
1075
|
String type = resultSet.getString(TYPE_NAME);
|
977
1076
|
if (precision > 0) {
|
978
1077
|
type += "(" + precision;
|
@@ -991,34 +1090,35 @@ public class RubyJdbcConnection extends RubyObject {
|
|
991
1090
|
}
|
992
1091
|
|
993
1092
|
private IRubyObject unmarshal_columns(ThreadContext context, DatabaseMetaData metadata,
|
994
|
-
|
1093
|
+
ResultSet rs, ResultSet pkeys) throws SQLException {
|
995
1094
|
try {
|
996
1095
|
Ruby runtime = context.getRuntime();
|
997
1096
|
List columns = new ArrayList();
|
1097
|
+
List pkeyNames = new ArrayList();
|
998
1098
|
String clzName = metadata.getClass().getName().toLowerCase();
|
999
|
-
boolean isOracle = clzName.indexOf("oracle") != -1 || clzName.indexOf("oci") != -1;
|
1000
1099
|
|
1001
1100
|
RubyHash types = (RubyHash) native_database_types();
|
1002
|
-
IRubyObject jdbcCol =
|
1101
|
+
IRubyObject jdbcCol = getJdbcColumnClass(context);
|
1102
|
+
|
1103
|
+
while (pkeys.next()) {
|
1104
|
+
pkeyNames.add(pkeys.getString(COLUMN_NAME));
|
1105
|
+
}
|
1003
1106
|
|
1004
|
-
while(rs.next()) {
|
1107
|
+
while (rs.next()) {
|
1108
|
+
String colName = rs.getString(COLUMN_NAME);
|
1005
1109
|
IRubyObject column = jdbcCol.callMethod(context, "new",
|
1006
1110
|
new IRubyObject[] {
|
1007
1111
|
getInstanceVariable("@config"),
|
1008
1112
|
RubyString.newUnicodeString(runtime,
|
1009
|
-
caseConvertIdentifierForRails(metadata,
|
1113
|
+
caseConvertIdentifierForRails(metadata, colName)),
|
1010
1114
|
defaultValueFromResultSet(runtime, rs),
|
1011
|
-
RubyString.newUnicodeString(runtime, typeFromResultSet(rs
|
1115
|
+
RubyString.newUnicodeString(runtime, typeFromResultSet(rs)),
|
1012
1116
|
runtime.newBoolean(!rs.getString(IS_NULLABLE).trim().equals("NO"))
|
1013
1117
|
});
|
1014
1118
|
columns.add(column);
|
1015
1119
|
|
1016
|
-
|
1017
|
-
|
1018
|
-
column.callMethod(context, "limit=", runtime.getNil());
|
1019
|
-
if(!column.callMethod(context, "type").equals(runtime.newSymbol("decimal"))) {
|
1020
|
-
column.callMethod(context, "precision=", runtime.getNil());
|
1021
|
-
}
|
1120
|
+
if (pkeyNames.contains(colName)) {
|
1121
|
+
column.callMethod(context, "primary=", runtime.getTrue());
|
1022
1122
|
}
|
1023
1123
|
}
|
1024
1124
|
return runtime.newArray(columns);
|
@@ -1039,13 +1139,32 @@ public class RubyJdbcConnection extends RubyObject {
|
|
1039
1139
|
}
|
1040
1140
|
}
|
1041
1141
|
|
1142
|
+
protected IRubyObject unmarshalResults(ThreadContext context, DatabaseMetaData metadata,
|
1143
|
+
Statement stmt, boolean downCase) throws SQLException {
|
1144
|
+
Ruby runtime = context.getRuntime();
|
1145
|
+
List<IRubyObject> sets = new ArrayList<IRubyObject>();
|
1146
|
+
|
1147
|
+
while (true) {
|
1148
|
+
sets.add(unmarshalResult(context, metadata, stmt.getResultSet(), downCase));
|
1149
|
+
if (!stmt.getMoreResults()) {
|
1150
|
+
break;
|
1151
|
+
}
|
1152
|
+
}
|
1153
|
+
|
1154
|
+
if (sets.size() > 1) {
|
1155
|
+
return runtime.newArray(sets);
|
1156
|
+
} else {
|
1157
|
+
return sets.get(0);
|
1158
|
+
}
|
1159
|
+
}
|
1160
|
+
|
1042
1161
|
/**
|
1043
1162
|
* Converts a jdbc resultset into an array (rows) of hashes (row) that AR expects.
|
1044
1163
|
*
|
1045
1164
|
* @param downCase should column names only be in lower case?
|
1046
1165
|
*/
|
1047
1166
|
protected IRubyObject unmarshalResult(ThreadContext context, DatabaseMetaData metadata,
|
1048
|
-
|
1167
|
+
ResultSet resultSet, boolean downCase) throws SQLException {
|
1049
1168
|
Ruby runtime = context.getRuntime();
|
1050
1169
|
List results = new ArrayList();
|
1051
1170
|
|
@@ -1075,6 +1194,11 @@ public class RubyJdbcConnection extends RubyObject {
|
|
1075
1194
|
while (toWrap.getCause() != null && toWrap.getCause() != toWrap) {
|
1076
1195
|
toWrap = toWrap.getCause();
|
1077
1196
|
}
|
1197
|
+
|
1198
|
+
if (context.getRuntime().isDebug()) {
|
1199
|
+
toWrap.printStackTrace(System.out);
|
1200
|
+
}
|
1201
|
+
|
1078
1202
|
i++;
|
1079
1203
|
if (autoCommit) {
|
1080
1204
|
if (i == 1) {
|
@@ -1094,15 +1218,32 @@ public class RubyJdbcConnection extends RubyObject {
|
|
1094
1218
|
throw wrap(context, toWrap);
|
1095
1219
|
}
|
1096
1220
|
|
1097
|
-
|
1098
|
-
|
1099
|
-
|
1221
|
+
protected RuntimeException wrap(ThreadContext context, Throwable exception) {
|
1222
|
+
Ruby runtime = context.getRuntime();
|
1223
|
+
RaiseException arError = new RaiseException(runtime, runtime.getModule("ActiveRecord").getClass("JDBCError"),
|
1224
|
+
exception.getMessage(), true);
|
1225
|
+
arError.initCause(exception);
|
1226
|
+
if (exception instanceof SQLException) {
|
1227
|
+
RuntimeHelpers.invoke(context, arError.getException(),
|
1228
|
+
"errno=", runtime.newFixnum(((SQLException) exception).getErrorCode()));
|
1229
|
+
RuntimeHelpers.invoke(context, arError.getException(),
|
1230
|
+
"sql_exception=", JavaEmbedUtils.javaToRuby(runtime, exception));
|
1231
|
+
}
|
1232
|
+
return (RuntimeException) arError;
|
1100
1233
|
}
|
1101
1234
|
|
1102
1235
|
private IRubyObject wrappedConnection(Connection c) {
|
1103
1236
|
return Java.java_to_ruby(this, JavaObject.wrap(getRuntime(), c), Block.NULL_BLOCK);
|
1104
1237
|
}
|
1105
1238
|
|
1239
|
+
/**
|
1240
|
+
* Some databases support schemas and others do not.
|
1241
|
+
* For ones which do this method should return true, aiding in decisions regarding schema vs database determination.
|
1242
|
+
*/
|
1243
|
+
protected boolean databaseSupportsSchemas() {
|
1244
|
+
return false;
|
1245
|
+
}
|
1246
|
+
|
1106
1247
|
private static int whitespace(int start, ByteList bl) {
|
1107
1248
|
int end = bl.begin + bl.realSize;
|
1108
1249
|
|
@@ -1133,11 +1274,13 @@ public class RubyJdbcConnection extends RubyObject {
|
|
1133
1274
|
|
1134
1275
|
public static class ColumnData {
|
1135
1276
|
public IRubyObject name;
|
1277
|
+
public int index;
|
1136
1278
|
public int type;
|
1137
1279
|
|
1138
|
-
public ColumnData(IRubyObject name, int type) {
|
1280
|
+
public ColumnData(IRubyObject name, int type, int idx) {
|
1139
1281
|
this.name = name;
|
1140
1282
|
this.type = type;
|
1283
|
+
this.index = idx;
|
1141
1284
|
}
|
1142
1285
|
|
1143
1286
|
public static ColumnData[] setup(Ruby runtime, DatabaseMetaData databaseMetadata,
|
@@ -1153,7 +1296,7 @@ public class RubyJdbcConnection extends RubyObject {
|
|
1153
1296
|
name = RubyJdbcConnection.caseConvertIdentifierForRails(databaseMetadata, metadata.getColumnLabel(i));
|
1154
1297
|
}
|
1155
1298
|
|
1156
|
-
columns[i - 1] = new ColumnData(RubyString.newUnicodeString(runtime, name), metadata.getColumnType(i));
|
1299
|
+
columns[i - 1] = new ColumnData(RubyString.newUnicodeString(runtime, name), metadata.getColumnType(i), i);
|
1157
1300
|
}
|
1158
1301
|
|
1159
1302
|
return columns;
|