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.
Files changed (191) hide show
  1. checksums.yaml +7 -0
  2. data/.gitignore +35 -0
  3. data/.travis.yml +462 -0
  4. data/.yardopts +4 -0
  5. data/Appraisals +36 -0
  6. data/CONTRIBUTING.md +49 -0
  7. data/Gemfile +68 -0
  8. data/History.md +1191 -0
  9. data/LICENSE.txt +25 -0
  10. data/README.md +277 -0
  11. data/RUNNING_TESTS.md +88 -0
  12. data/Rakefile +298 -0
  13. data/Rakefile.jdbc +20 -0
  14. data/activerecord-jdbc-adapter.gemspec +63 -0
  15. data/lib/active_record/connection_adapters/as400_adapter.rb +2 -0
  16. data/lib/active_record/connection_adapters/db2_adapter.rb +1 -0
  17. data/lib/active_record/connection_adapters/derby_adapter.rb +1 -0
  18. data/lib/active_record/connection_adapters/firebird_adapter.rb +1 -0
  19. data/lib/active_record/connection_adapters/h2_adapter.rb +1 -0
  20. data/lib/active_record/connection_adapters/hsqldb_adapter.rb +1 -0
  21. data/lib/active_record/connection_adapters/informix_adapter.rb +1 -0
  22. data/lib/active_record/connection_adapters/jdbc_adapter.rb +1 -0
  23. data/lib/active_record/connection_adapters/jndi_adapter.rb +1 -0
  24. data/lib/active_record/connection_adapters/mariadb_adapter.rb +1 -0
  25. data/lib/active_record/connection_adapters/mssql_adapter.rb +1 -0
  26. data/lib/active_record/connection_adapters/mysql2_adapter.rb +1 -0
  27. data/lib/active_record/connection_adapters/mysql_adapter.rb +1 -0
  28. data/lib/active_record/connection_adapters/oracle_adapter.rb +1 -0
  29. data/lib/active_record/connection_adapters/postgresql_adapter.rb +1 -0
  30. data/lib/active_record/connection_adapters/sqlite3_adapter.rb +1 -0
  31. data/lib/active_record/connection_adapters/sqlserver_adapter.rb +1 -0
  32. data/lib/activerecord-jdbc-adapter.rb +1 -0
  33. data/lib/arel/visitors/compat.rb +64 -0
  34. data/lib/arel/visitors/db2.rb +137 -0
  35. data/lib/arel/visitors/derby.rb +112 -0
  36. data/lib/arel/visitors/firebird.rb +79 -0
  37. data/lib/arel/visitors/h2.rb +25 -0
  38. data/lib/arel/visitors/hsqldb.rb +32 -0
  39. data/lib/arel/visitors/postgresql_jdbc.rb +6 -0
  40. data/lib/arel/visitors/sql_server.rb +225 -0
  41. data/lib/arel/visitors/sql_server/ng42.rb +293 -0
  42. data/lib/arjdbc.rb +22 -0
  43. data/lib/arjdbc/db2.rb +4 -0
  44. data/lib/arjdbc/db2/adapter.rb +802 -0
  45. data/lib/arjdbc/db2/as400.rb +137 -0
  46. data/lib/arjdbc/db2/column.rb +177 -0
  47. data/lib/arjdbc/db2/connection_methods.rb +45 -0
  48. data/lib/arjdbc/derby.rb +3 -0
  49. data/lib/arjdbc/derby/active_record_patch.rb +13 -0
  50. data/lib/arjdbc/derby/adapter.rb +567 -0
  51. data/lib/arjdbc/derby/connection_methods.rb +16 -0
  52. data/lib/arjdbc/derby/schema_creation.rb +15 -0
  53. data/lib/arjdbc/discover.rb +104 -0
  54. data/lib/arjdbc/firebird.rb +4 -0
  55. data/lib/arjdbc/firebird/adapter.rb +468 -0
  56. data/lib/arjdbc/firebird/connection_methods.rb +20 -0
  57. data/lib/arjdbc/h2.rb +3 -0
  58. data/lib/arjdbc/h2/adapter.rb +335 -0
  59. data/lib/arjdbc/h2/connection_methods.rb +22 -0
  60. data/lib/arjdbc/hsqldb.rb +3 -0
  61. data/lib/arjdbc/hsqldb/adapter.rb +304 -0
  62. data/lib/arjdbc/hsqldb/connection_methods.rb +23 -0
  63. data/lib/arjdbc/hsqldb/explain_support.rb +35 -0
  64. data/lib/arjdbc/hsqldb/schema_creation.rb +11 -0
  65. data/lib/arjdbc/informix.rb +5 -0
  66. data/lib/arjdbc/informix/adapter.rb +160 -0
  67. data/lib/arjdbc/informix/connection_methods.rb +9 -0
  68. data/lib/arjdbc/jdbc.rb +62 -0
  69. data/lib/arjdbc/jdbc/adapter.rb +997 -0
  70. data/lib/arjdbc/jdbc/adapter_require.rb +46 -0
  71. data/lib/arjdbc/jdbc/arel_support.rb +149 -0
  72. data/lib/arjdbc/jdbc/base_ext.rb +34 -0
  73. data/lib/arjdbc/jdbc/callbacks.rb +52 -0
  74. data/lib/arjdbc/jdbc/column.rb +83 -0
  75. data/lib/arjdbc/jdbc/connection.rb +26 -0
  76. data/lib/arjdbc/jdbc/connection_methods.rb +59 -0
  77. data/lib/arjdbc/jdbc/driver.rb +44 -0
  78. data/lib/arjdbc/jdbc/error.rb +75 -0
  79. data/lib/arjdbc/jdbc/extension.rb +69 -0
  80. data/lib/arjdbc/jdbc/java.rb +13 -0
  81. data/lib/arjdbc/jdbc/type_cast.rb +154 -0
  82. data/lib/arjdbc/jdbc/type_converter.rb +142 -0
  83. data/lib/arjdbc/mssql.rb +7 -0
  84. data/lib/arjdbc/mssql/adapter.rb +822 -0
  85. data/lib/arjdbc/mssql/column.rb +207 -0
  86. data/lib/arjdbc/mssql/connection_methods.rb +72 -0
  87. data/lib/arjdbc/mssql/explain_support.rb +99 -0
  88. data/lib/arjdbc/mssql/limit_helpers.rb +231 -0
  89. data/lib/arjdbc/mssql/lock_methods.rb +77 -0
  90. data/lib/arjdbc/mssql/types.rb +343 -0
  91. data/lib/arjdbc/mssql/utils.rb +82 -0
  92. data/lib/arjdbc/mysql.rb +3 -0
  93. data/lib/arjdbc/mysql/adapter.rb +998 -0
  94. data/lib/arjdbc/mysql/bulk_change_table.rb +150 -0
  95. data/lib/arjdbc/mysql/column.rb +167 -0
  96. data/lib/arjdbc/mysql/connection_methods.rb +137 -0
  97. data/lib/arjdbc/mysql/explain_support.rb +82 -0
  98. data/lib/arjdbc/mysql/schema_creation.rb +58 -0
  99. data/lib/arjdbc/oracle.rb +4 -0
  100. data/lib/arjdbc/oracle/adapter.rb +968 -0
  101. data/lib/arjdbc/oracle/column.rb +136 -0
  102. data/lib/arjdbc/oracle/connection_methods.rb +21 -0
  103. data/lib/arjdbc/postgresql.rb +3 -0
  104. data/lib/arjdbc/postgresql/_bc_time_cast_patch.rb +21 -0
  105. data/lib/arjdbc/postgresql/adapter.rb +1498 -0
  106. data/lib/arjdbc/postgresql/base/array_parser.rb +95 -0
  107. data/lib/arjdbc/postgresql/base/oid.rb +412 -0
  108. data/lib/arjdbc/postgresql/base/pgconn.rb +8 -0
  109. data/lib/arjdbc/postgresql/base/schema_definitions.rb +132 -0
  110. data/lib/arjdbc/postgresql/column.rb +640 -0
  111. data/lib/arjdbc/postgresql/connection_methods.rb +44 -0
  112. data/lib/arjdbc/postgresql/explain_support.rb +53 -0
  113. data/lib/arjdbc/postgresql/oid/bytea.rb +3 -0
  114. data/lib/arjdbc/postgresql/oid_types.rb +265 -0
  115. data/lib/arjdbc/postgresql/schema_creation.rb +60 -0
  116. data/lib/arjdbc/railtie.rb +11 -0
  117. data/lib/arjdbc/sqlite3.rb +3 -0
  118. data/lib/arjdbc/sqlite3/adapter.rb +654 -0
  119. data/lib/arjdbc/sqlite3/connection_methods.rb +36 -0
  120. data/lib/arjdbc/sqlite3/explain_support.rb +29 -0
  121. data/lib/arjdbc/sybase.rb +2 -0
  122. data/lib/arjdbc/sybase/adapter.rb +47 -0
  123. data/lib/arjdbc/tasks.rb +13 -0
  124. data/lib/arjdbc/tasks/database_tasks.rb +66 -0
  125. data/lib/arjdbc/tasks/databases.rake +91 -0
  126. data/lib/arjdbc/tasks/databases3.rake +239 -0
  127. data/lib/arjdbc/tasks/databases4.rake +39 -0
  128. data/lib/arjdbc/tasks/db2_database_tasks.rb +104 -0
  129. data/lib/arjdbc/tasks/derby_database_tasks.rb +95 -0
  130. data/lib/arjdbc/tasks/h2_database_tasks.rb +31 -0
  131. data/lib/arjdbc/tasks/hsqldb_database_tasks.rb +70 -0
  132. data/lib/arjdbc/tasks/jdbc_database_tasks.rb +169 -0
  133. data/lib/arjdbc/tasks/mssql_database_tasks.rb +46 -0
  134. data/lib/arjdbc/tasks/oracle/enhanced_structure_dump.rb +297 -0
  135. data/lib/arjdbc/tasks/oracle_database_tasks.rb +65 -0
  136. data/lib/arjdbc/util/quoted_cache.rb +60 -0
  137. data/lib/arjdbc/util/serialized_attributes.rb +98 -0
  138. data/lib/arjdbc/util/table_copier.rb +108 -0
  139. data/lib/arjdbc/version.rb +8 -0
  140. data/lib/generators/jdbc/USAGE +9 -0
  141. data/lib/generators/jdbc/jdbc_generator.rb +17 -0
  142. data/pom.xml +285 -0
  143. data/rails_generators/jdbc_generator.rb +15 -0
  144. data/rails_generators/templates/config/initializers/jdbc.rb +10 -0
  145. data/rails_generators/templates/lib/tasks/jdbc.rake +11 -0
  146. data/rakelib/01-tomcat.rake +51 -0
  147. data/rakelib/02-test.rake +151 -0
  148. data/rakelib/bundler_ext.rb +11 -0
  149. data/rakelib/db.rake +58 -0
  150. data/rakelib/rails.rake +77 -0
  151. data/src/java/arjdbc/ArJdbcModule.java +288 -0
  152. data/src/java/arjdbc/db2/DB2Module.java +77 -0
  153. data/src/java/arjdbc/db2/DB2RubyJdbcConnection.java +128 -0
  154. data/src/java/arjdbc/derby/DerbyModule.java +180 -0
  155. data/src/java/arjdbc/derby/DerbyRubyJdbcConnection.java +153 -0
  156. data/src/java/arjdbc/firebird/FirebirdRubyJdbcConnection.java +190 -0
  157. data/src/java/arjdbc/h2/H2Module.java +50 -0
  158. data/src/java/arjdbc/h2/H2RubyJdbcConnection.java +86 -0
  159. data/src/java/arjdbc/hsqldb/HSQLDBModule.java +74 -0
  160. data/src/java/arjdbc/informix/InformixRubyJdbcConnection.java +76 -0
  161. data/src/java/arjdbc/jdbc/AdapterJavaService.java +43 -0
  162. data/src/java/arjdbc/jdbc/Callable.java +44 -0
  163. data/src/java/arjdbc/jdbc/ConnectionFactory.java +77 -0
  164. data/src/java/arjdbc/jdbc/DataSourceConnectionFactory.java +156 -0
  165. data/src/java/arjdbc/jdbc/DriverConnectionFactory.java +63 -0
  166. data/src/java/arjdbc/jdbc/DriverWrapper.java +128 -0
  167. data/src/java/arjdbc/jdbc/JdbcConnectionFactory.java +32 -0
  168. data/src/java/arjdbc/jdbc/RubyJdbcConnection.java +4541 -0
  169. data/src/java/arjdbc/jdbc/SQLBlock.java +54 -0
  170. data/src/java/arjdbc/jdbc/WithResultSet.java +37 -0
  171. data/src/java/arjdbc/mssql/MSSQLModule.java +91 -0
  172. data/src/java/arjdbc/mssql/MSSQLRubyJdbcConnection.java +193 -0
  173. data/src/java/arjdbc/mysql/MySQLModule.java +140 -0
  174. data/src/java/arjdbc/mysql/MySQLRubyJdbcConnection.java +456 -0
  175. data/src/java/arjdbc/oracle/OracleModule.java +81 -0
  176. data/src/java/arjdbc/oracle/OracleRubyJdbcConnection.java +477 -0
  177. data/src/java/arjdbc/postgresql/ByteaUtils.java +171 -0
  178. data/src/java/arjdbc/postgresql/DriverImplementation.java +78 -0
  179. data/src/java/arjdbc/postgresql/PGDriverImplementation.java +535 -0
  180. data/src/java/arjdbc/postgresql/PostgreSQLModule.java +189 -0
  181. data/src/java/arjdbc/postgresql/PostgreSQLRubyJdbcConnection.java +489 -0
  182. data/src/java/arjdbc/sqlite3/SQLite3Module.java +93 -0
  183. data/src/java/arjdbc/sqlite3/SQLite3RubyJdbcConnection.java +405 -0
  184. data/src/java/arjdbc/util/CallResultSet.java +826 -0
  185. data/src/java/arjdbc/util/DateTimeUtils.java +517 -0
  186. data/src/java/arjdbc/util/NumberUtils.java +50 -0
  187. data/src/java/arjdbc/util/ObjectSupport.java +65 -0
  188. data/src/java/arjdbc/util/QuotingUtils.java +139 -0
  189. data/src/java/arjdbc/util/StringCache.java +60 -0
  190. data/src/java/arjdbc/util/StringHelper.java +155 -0
  191. metadata +288 -0
@@ -0,0 +1,81 @@
1
+ /*
2
+ * The MIT License
3
+ *
4
+ * Copyright 2013-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.oracle;
25
+
26
+ import static arjdbc.util.QuotingUtils.BYTES_0;
27
+ import static arjdbc.util.QuotingUtils.BYTES_1;
28
+ import static arjdbc.util.QuotingUtils.quoteCharWith;
29
+
30
+ import org.jruby.Ruby;
31
+ import org.jruby.RubyModule;
32
+ import org.jruby.RubyString;
33
+ import org.jruby.anno.JRubyMethod;
34
+ import org.jruby.runtime.ThreadContext;
35
+ import org.jruby.runtime.builtin.IRubyObject;
36
+
37
+ /**
38
+ * ArJdbc::Oracle
39
+ *
40
+ * @author kares
41
+ */
42
+ @org.jruby.anno.JRubyModule(name = "ArJdbc::Oracle")
43
+ public class OracleModule {
44
+
45
+ public static RubyModule load(final RubyModule arJdbc) {
46
+ RubyModule oracle = arJdbc.defineModuleUnder("Oracle");
47
+ oracle.defineAnnotatedMethods( OracleModule.class );
48
+ return oracle;
49
+ }
50
+
51
+ public static RubyModule load(final Ruby runtime) {
52
+ return load( arjdbc.ArJdbcModule.get(runtime) );
53
+ }
54
+
55
+ @JRubyMethod(name = "quote_string", required = 1)
56
+ public static IRubyObject quote_string(
57
+ final ThreadContext context,
58
+ final IRubyObject self,
59
+ final IRubyObject string) { // string.gsub("'", "''") :
60
+ final char single = '\'';
61
+ final RubyString quoted = quoteCharWith(
62
+ context, (RubyString) string, single, single
63
+ );
64
+ return quoted;
65
+ }
66
+
67
+ @JRubyMethod(name = "quoted_true", required = 0)
68
+ public static IRubyObject quoted_true(
69
+ final ThreadContext context,
70
+ final IRubyObject self) {
71
+ return RubyString.newString(context.runtime, BYTES_1);
72
+ }
73
+
74
+ @JRubyMethod(name = "quoted_false", required = 0)
75
+ public static IRubyObject quoted_false(
76
+ final ThreadContext context,
77
+ final IRubyObject self) {
78
+ return RubyString.newString(context.runtime, BYTES_0);
79
+ }
80
+
81
+ }
@@ -0,0 +1,477 @@
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
+ // NOTE: file contains code adapted from **oracle-enhanced** adapter, license follows
27
+ /*
28
+ Copyright (c) 2008-2011 Graham Jenkins, Michael Schoen, Raimonds Simanovskis
29
+
30
+ ... LICENSING TERMS ARE THE VERY SAME AS ACTIVERECORD-JDBC-ADAPTER'S ABOVE ...
31
+ */
32
+ package arjdbc.oracle;
33
+
34
+ import arjdbc.jdbc.Callable;
35
+ import arjdbc.jdbc.RubyJdbcConnection;
36
+ import arjdbc.util.CallResultSet;
37
+ import arjdbc.util.StringHelper;
38
+
39
+ import java.io.IOException;
40
+ import java.io.Reader;
41
+ import java.sql.CallableStatement;
42
+ import java.sql.Connection;
43
+ import java.sql.ResultSet;
44
+ import java.sql.SQLException;
45
+ import java.sql.PreparedStatement;
46
+ import java.sql.ResultSetMetaData;
47
+ import java.sql.Statement;
48
+ import java.sql.Types;
49
+ import java.util.Collections;
50
+ import java.util.List;
51
+ import java.util.regex.Pattern;
52
+
53
+ import org.jruby.Ruby;
54
+ import org.jruby.RubyArray;
55
+ import org.jruby.RubyClass;
56
+ import org.jruby.RubyFixnum;
57
+ import org.jruby.RubyString;
58
+ import org.jruby.anno.JRubyMethod;
59
+ import org.jruby.runtime.ObjectAllocator;
60
+ import org.jruby.runtime.ThreadContext;
61
+ import org.jruby.runtime.builtin.IRubyObject;
62
+
63
+ /**
64
+ *
65
+ * @author nicksieger
66
+ */
67
+ @org.jruby.anno.JRubyClass(name = "ActiveRecord::ConnectionAdapters::OracleJdbcConnection")
68
+ public class OracleRubyJdbcConnection extends RubyJdbcConnection {
69
+ private static final long serialVersionUID = -6469731781108431512L;
70
+
71
+ public OracleRubyJdbcConnection(Ruby runtime, RubyClass metaClass) {
72
+ super(runtime, metaClass);
73
+ }
74
+
75
+ public static RubyClass createOracleJdbcConnectionClass(Ruby runtime, RubyClass jdbcConnection) {
76
+ final RubyClass clazz = getConnectionAdapters(runtime).
77
+ defineClassUnder("OracleJdbcConnection", jdbcConnection, ALLOCATOR);
78
+ clazz.defineAnnotatedMethods(OracleRubyJdbcConnection.class);
79
+ return clazz;
80
+ }
81
+
82
+ public static RubyClass load(final Ruby runtime) {
83
+ RubyClass jdbcConnection = getJdbcConnection(runtime);
84
+ return createOracleJdbcConnectionClass(runtime, jdbcConnection);
85
+ }
86
+
87
+ protected static ObjectAllocator ALLOCATOR = new ObjectAllocator() {
88
+ public IRubyObject allocate(Ruby runtime, RubyClass klass) {
89
+ return new OracleRubyJdbcConnection(runtime, klass);
90
+ }
91
+ };
92
+
93
+ @JRubyMethod(name = "next_sequence_value", required = 1)
94
+ public IRubyObject next_sequence_value(final ThreadContext context, final IRubyObject sequence) {
95
+ return withConnection(context, new Callable<IRubyObject>() {
96
+ public IRubyObject call(final Connection connection) throws SQLException {
97
+ Statement statement = null; ResultSet value = null;
98
+ try {
99
+ statement = connection.createStatement();
100
+ value = statement.executeQuery("SELECT "+ sequence +".NEXTVAL id FROM dual");
101
+ if ( ! value.next() ) return context.nil;
102
+ return RubyFixnum.newFixnum(context.runtime, value.getLong(1));
103
+ }
104
+ catch (final SQLException e) {
105
+ debugMessage(context.runtime, "failed to get " + sequence + ".NEXTVAL : " + e.getMessage());
106
+ throw e;
107
+ }
108
+ finally { close(value); close(statement); }
109
+ }
110
+ });
111
+ }
112
+
113
+ @JRubyMethod(name = "execute_insert_returning", required = 2)
114
+ public IRubyObject execute_insert_returning(final ThreadContext context,
115
+ final IRubyObject sql, final IRubyObject binds) throws SQLException {
116
+ final String query = sqlString(sql);
117
+ final int outType = Types.VARCHAR;
118
+ if ( binds == null || binds.isNil() ) { // no prepared statements
119
+ return executePreparedCall(context, query, Collections.EMPTY_LIST, outType);
120
+ }
121
+ // allow prepared statements with empty binds parameters
122
+ return executePreparedCall(context, query, (List) binds, outType);
123
+ }
124
+
125
+ private IRubyObject executePreparedCall(final ThreadContext context, final String query,
126
+ final List<?> binds, final int outType) {
127
+ return withConnection(context, new Callable<IRubyObject>() {
128
+ public IRubyObject call(final Connection connection) throws SQLException {
129
+ CallableStatement statement = null;
130
+ final int outIndex = binds.size() + 1;
131
+ try {
132
+ statement = connection.prepareCall("{call " + query + " }");
133
+ setStatementParameters(context, connection, statement, binds);
134
+ statement.registerOutParameter(outIndex, outType);
135
+ statement.executeUpdate();
136
+ ResultSet resultSet = new CallResultSet(statement);
137
+ return jdbcToRuby(context, context.runtime, outIndex, outType, resultSet);
138
+ }
139
+ catch (final SQLException e) {
140
+ debugErrorSQL(context, query);
141
+ throw e;
142
+ }
143
+ finally { close(statement); }
144
+ }
145
+ });
146
+ }
147
+
148
+ protected static final boolean generatedKeys;
149
+ static {
150
+ final String genKeys = System.getProperty("arjdbc.oracle.generated_keys");
151
+ if ( genKeys == null ) {
152
+ generatedKeys = true; // by default
153
+ }
154
+ else {
155
+ generatedKeys = Boolean.parseBoolean(genKeys);
156
+ }
157
+ }
158
+
159
+ @Override
160
+ protected IRubyObject mapGeneratedKeys(
161
+ final Ruby runtime, final Connection connection,
162
+ final Statement statement, final Boolean singleResult)
163
+ throws SQLException {
164
+ if ( generatedKeys ) {
165
+ return super.mapGeneratedKeys(runtime, connection, statement, singleResult);
166
+ }
167
+ return null; // disabled using -Darjdbc.oracle.generated_keys=false
168
+ }
169
+
170
+ private static final boolean returnRowID = Boolean.getBoolean("arjdbc.oracle.generated_keys.rowid");
171
+
172
+ @Override // NOTE: Invalid column type:
173
+ // getLong not implemented for class oracle.jdbc.driver.T4CRowidAccessor
174
+ protected IRubyObject mapGeneratedKey(final Ruby runtime, final ResultSet genKeys)
175
+ throws SQLException {
176
+ // NOTE: it's likely a ROWID which we do not care about :
177
+ final String value = genKeys.getString(1); // "AAAsOjAAFAAABUlAAA"
178
+ if ( isPositiveInteger(value) ) {
179
+ return RubyFixnum.newFixnum( runtime, Long.parseLong(value) );
180
+ }
181
+ else {
182
+ return returnRowID ? RubyString.newString(runtime, value) : runtime.getNil();
183
+ }
184
+ }
185
+
186
+ private static boolean isPositiveInteger(final String value) {
187
+ for ( int i = 0; i < value.length(); i++ ) {
188
+ if ( ! Character.isDigit(value.charAt(i)) ) return false;
189
+ }
190
+ return true;
191
+ }
192
+
193
+ @Override // resultSet.wasNull() might be falsy for '' treated as null
194
+ protected IRubyObject stringToRuby(final ThreadContext context,
195
+ final Ruby runtime, final ResultSet resultSet, final int column)
196
+ throws SQLException {
197
+ final String value = resultSet.getString(column);
198
+ if ( value == null ) return context.nil;
199
+ return RubyString.newUnicodeString(runtime, value);
200
+ }
201
+
202
+ @Override
203
+ protected IRubyObject readerToRuby(final ThreadContext context,
204
+ final Ruby runtime, final ResultSet resultSet, final int column)
205
+ throws SQLException, IOException {
206
+ final Reader reader = resultSet.getCharacterStream(column);
207
+ try {
208
+ if ( reader == null || resultSet.wasNull() ) {
209
+ return RubyString.newEmptyString(runtime);
210
+ }
211
+
212
+ final int bufSize = streamBufferSize;
213
+ final StringBuilder string = new StringBuilder(bufSize);
214
+
215
+ final char[] buf = new char[ bufSize / 2 ];
216
+ for (int len = reader.read(buf); len != -1; len = reader.read(buf)) {
217
+ string.append(buf, 0, len);
218
+ }
219
+
220
+ return StringHelper.newUnicodeString(runtime, string);
221
+ }
222
+ finally { if ( reader != null ) reader.close(); }
223
+ }
224
+
225
+ @Override // booleans are emulated can not setNull(index, Types.BOOLEAN)
226
+ protected void setBooleanParameter(final ThreadContext context,
227
+ final Connection connection, final PreparedStatement statement,
228
+ final int index, final Object value,
229
+ final IRubyObject column, final int type) throws SQLException {
230
+ if ( value instanceof IRubyObject ) {
231
+ setBooleanParameter(context, connection, statement, index, (IRubyObject) value, column, type);
232
+ }
233
+ else {
234
+ if ( value == null ) statement.setNull(index, Types.TINYINT);
235
+ else {
236
+ statement.setBoolean(index, ((Boolean) value).booleanValue());
237
+ }
238
+ }
239
+ }
240
+
241
+ @Override // booleans are emulated can not setNull(index, Types.BOOLEAN)
242
+ protected void setBooleanParameter(final ThreadContext context,
243
+ final Connection connection, final PreparedStatement statement,
244
+ final int index, final IRubyObject value,
245
+ final IRubyObject column, final int type) throws SQLException {
246
+ if ( value.isNil() ) statement.setNull(index, Types.TINYINT);
247
+ else {
248
+ statement.setBoolean(index, value.isTrue());
249
+ }
250
+ }
251
+
252
+ /**
253
+ * Oracle needs this override to reconstruct NUMBER which is different
254
+ * from NUMBER(x) or NUMBER(x,y).
255
+ */
256
+ @Override
257
+ protected String typeFromResultSet(final ResultSet resultSet) throws SQLException {
258
+ int precision = intFromResultSet(resultSet, COLUMN_SIZE);
259
+ int scale = intFromResultSet(resultSet, DECIMAL_DIGITS);
260
+
261
+ // According to http://forums.oracle.com/forums/thread.jspa?threadID=658646
262
+ // Unadorned NUMBER reports scale == null, so we look for that here.
263
+ if ( scale < 0 && resultSet.getInt(DATA_TYPE) == java.sql.Types.DECIMAL ) {
264
+ precision = -1;
265
+ }
266
+
267
+ final String type = resultSet.getString(TYPE_NAME);
268
+ return formatTypeWithPrecisionAndScale(type, precision, scale);
269
+ }
270
+
271
+ @Override
272
+ protected RubyArray mapTables(final ThreadContext context, final Connection connection,
273
+ final String catalog, final String schemaPattern, final String tablePattern,
274
+ final ResultSet tablesSet) throws SQLException {
275
+ final RubyArray tables = RubyArray.newArray(context.runtime, 24);
276
+ while ( tablesSet.next() ) {
277
+ String name = tablesSet.getString(TABLES_TABLE_NAME);
278
+ // name = caseConvertIdentifierForRails(connection, name);
279
+ //if (name != null) {
280
+ name = name.toLowerCase();
281
+ if ( name.startsWith("bin$") ) continue; // Oracle 10g RecycleBin
282
+ //}
283
+ tables.append( cachedString(context, name) );
284
+ }
285
+ return tables;
286
+ }
287
+
288
+ @Override
289
+ protected ColumnData[] extractColumns(final Ruby runtime,
290
+ final Connection connection, final ResultSet resultSet,
291
+ final boolean downCase) throws SQLException {
292
+
293
+ final ResultSetMetaData resultMetaData = resultSet.getMetaData();
294
+
295
+ final int columnCount = resultMetaData.getColumnCount();
296
+ final ColumnData[] columns = new ColumnData[columnCount];
297
+
298
+ for ( int i = 1; i <= columnCount; i++ ) { // metadata is one-based
299
+ String name = resultMetaData.getColumnLabel(i);
300
+ if ( downCase ) {
301
+ name = name.toLowerCase();
302
+ } else {
303
+ name = caseConvertIdentifierForRails(connection, name);
304
+ }
305
+ final RubyString columnName = RubyString.newUnicodeString(runtime, name);
306
+
307
+ int columnType = resultMetaData.getColumnType(i);
308
+ if (columnType == Types.NUMERIC) {
309
+ // avoid extracting all NUMBER columns as BigDecimal :
310
+ if (resultMetaData.getScale(i) == 0) {
311
+ final int prec = resultMetaData.getPrecision(i);
312
+ if ( prec < 10 ) { // fits into int
313
+ columnType = Types.INTEGER;
314
+ }
315
+ else if ( prec < 19 ) { // fits into long
316
+ columnType = Types.BIGINT;
317
+ }
318
+ }
319
+ }
320
+
321
+ columns[i - 1] = new ColumnData(columnName, columnType, i);
322
+ }
323
+
324
+ return columns;
325
+ }
326
+
327
+ // storesMixedCaseIdentifiers() return false;
328
+ // storesLowerCaseIdentifiers() return false;
329
+ // storesUpperCaseIdentifiers() return true;
330
+
331
+ @Override
332
+ protected String caseConvertIdentifierForRails(final Connection connection, final String value) {
333
+ return value == null ? null : value.toLowerCase();
334
+ }
335
+
336
+ @Override
337
+ protected String caseConvertIdentifierForJdbc(final Connection connection, final String value) {
338
+ return value == null ? null : value.toUpperCase();
339
+ }
340
+
341
+ //@Override
342
+ //protected boolean useByteStrings() { return true; }
343
+
344
+ // based on OracleEnhanced's Ruby connection.describe
345
+ @JRubyMethod(name = "describe", required = 1)
346
+ public IRubyObject describe(final ThreadContext context, final IRubyObject name) {
347
+ final RubyArray desc = describe(context, name.toString(), null);
348
+ return desc == null ? context.nil : desc; // TODO raise instead of nil
349
+ }
350
+
351
+ @JRubyMethod(name = "describe", required = 2)
352
+ public IRubyObject describe(final ThreadContext context, final IRubyObject name, final IRubyObject owner) {
353
+ final RubyArray desc = describe(context, name.toString(), owner.isNil() ? null : owner.toString());
354
+ return desc == null ? context.nil : desc; // TODO raise instead of nil
355
+ }
356
+
357
+ private RubyArray describe(final ThreadContext context, final String name, final String owner) {
358
+ final String dbLink; String defaultOwner, theName = name; int delim;
359
+ if ( ( delim = theName.indexOf('@') ) > 0 ) {
360
+ dbLink = theName.substring(delim).toUpperCase(); // '@DBLINK'
361
+ theName = theName.substring(0, delim);
362
+ defaultOwner = null; // will SELECT username FROM all_dbLinks ...
363
+ }
364
+ else {
365
+ dbLink = ""; defaultOwner = owner; // config[:username] || meta_data.user_name
366
+ }
367
+
368
+ theName = isValidTableName(theName) ? theName.toUpperCase() : unquoteTableName(theName);
369
+
370
+ final String tableName; final String tableOwner;
371
+ if ( ( delim = theName.indexOf('.') ) > 0 ) {
372
+ tableOwner = theName.substring(0, delim);
373
+ tableName = theName.substring(delim + 1);
374
+ }
375
+ else {
376
+ tableName = theName;
377
+ tableOwner = (defaultOwner == null && dbLink.length() > 0) ? selectOwner(context, dbLink) : defaultOwner;
378
+ }
379
+
380
+ return withConnection(context, new Callable<RubyArray>() {
381
+ public RubyArray call(final Connection connection) throws SQLException {
382
+ String owner = tableOwner == null ? connection.getMetaData().getUserName() : tableOwner;
383
+ final String sql =
384
+ "SELECT owner, table_name, 'TABLE' name_type" +
385
+ " FROM all_tables" + dbLink +
386
+ " WHERE owner = '" + owner + "' AND table_name = '" + tableName + "'" +
387
+ " UNION ALL " +
388
+ "SELECT owner, view_name table_name, 'VIEW' name_type" +
389
+ " FROM all_views" + dbLink +
390
+ " WHERE owner = '" + owner + "' AND view_name = '" + tableName + "'" +
391
+ " UNION ALL " +
392
+ "SELECT table_owner, DECODE(db_link, NULL, table_name, table_name||'@'||db_link), 'SYNONYM' name_type" +
393
+ " FROM all_synonyms" + dbLink +
394
+ " WHERE owner = '" + owner + "' AND synonym_name = '" + tableName + "'" +
395
+ " UNION ALL " +
396
+ "SELECT table_owner, DECODE(db_link, NULL, table_name, table_name||'@'||db_link), 'SYNONYM' name_type" +
397
+ " FROM all_synonyms" + dbLink +
398
+ " WHERE owner = 'PUBLIC' AND synonym_name = '" + tableName + "'" ;
399
+
400
+ Statement statement = null; ResultSet result = null;
401
+ try {
402
+ statement = connection.createStatement();
403
+ result = statement.executeQuery(sql);
404
+
405
+ if ( ! result.next() ) return null; // NOTE: should raise
406
+
407
+ owner = result.getString("owner");
408
+ final String table_name = result.getString("table_name");
409
+ final String name_type = result.getString("name_type");
410
+
411
+ if ( "SYNONYM".equals(name_type) ) {
412
+ final StringBuilder name = new StringBuilder();
413
+ if ( owner != null && owner.length() > 0 ) {
414
+ name.append(owner).append('.');
415
+ }
416
+ name.append(table_name);
417
+ if ( dbLink != null ) name.append(dbLink);
418
+ return describe(context, name.toString(), owner);
419
+ }
420
+
421
+ final RubyArray arr = RubyArray.newArray(context.runtime, 3);
422
+ arr.append( context.runtime.newString(owner) );
423
+ arr.append( context.runtime.newString(table_name) );
424
+ if ( dbLink != null ) arr.append( context.runtime.newString(dbLink) );
425
+ return arr;
426
+ }
427
+ catch (final SQLException e) {
428
+ debugMessage(context, "failed to describe '" + name + "' : " + e.getMessage());
429
+ throw e;
430
+ }
431
+ finally { close(result); close(statement); }
432
+ }
433
+ });
434
+ }
435
+
436
+ private String selectOwner(final ThreadContext context, final String dbLink) {
437
+ return withConnection(context, new Callable<String>() {
438
+ public String call(final Connection connection) throws SQLException {
439
+ Statement statement = null; ResultSet result = null;
440
+ final String sql = "SELECT username FROM all_db_links WHERE db_link = '" + dbLink + "'";
441
+ try {
442
+ statement = connection.createStatement();
443
+ result = statement.executeQuery(sql);
444
+ // if ( ! result.next() ) return null;
445
+ return result.getString(1);
446
+ }
447
+ catch (final SQLException e) {
448
+ debugMessage(context, "\"" + sql + "\" failed : " + e.getMessage());
449
+ throw e;
450
+ }
451
+ finally { close(result); close(statement); }
452
+ }
453
+ });
454
+ }
455
+
456
+ private static final Pattern VALID_TABLE_NAME;
457
+ static {
458
+ final String NONQUOTED_OBJECT_NAME = "[A-Za-z][A-z0-9$#]{0,29}";
459
+ final String NONQUOTED_DATABASE_LINK = "[A-Za-z][A-z0-9$#\\.@]{0,127}";
460
+ VALID_TABLE_NAME = Pattern.compile(
461
+ "\\A(?:" + NONQUOTED_OBJECT_NAME + "\\.)?" + NONQUOTED_OBJECT_NAME + "(?:@" + NONQUOTED_DATABASE_LINK + ")?\\Z");
462
+ }
463
+
464
+ private static boolean isValidTableName(final String name) {
465
+ return VALID_TABLE_NAME.matcher(name).matches();
466
+ }
467
+
468
+ private static String unquoteTableName(String name) {
469
+ name = name.trim();
470
+ final int len = name.length();
471
+ if (len > 0 && name.charAt(0) == '"' && name.charAt(len - 1) == '"') {
472
+ return name.substring(1, len - 1);
473
+ }
474
+ return name;
475
+ }
476
+
477
+ }