neo-activerecord-jdbc-adapter 5.0.pre2

Sign up to get free protection for your applications and to get access to all the features.
Files changed (187) hide show
  1. checksums.yaml +7 -0
  2. data/.gitignore +33 -0
  3. data/.travis.yml +438 -0
  4. data/.yardopts +4 -0
  5. data/Appraisals +41 -0
  6. data/CONTRIBUTING.md +44 -0
  7. data/Gemfile +62 -0
  8. data/History.md +1191 -0
  9. data/LICENSE.txt +25 -0
  10. data/README.md +266 -0
  11. data/RUNNING_TESTS.md +88 -0
  12. data/Rakefile +100 -0
  13. data/Rakefile.jdbc +20 -0
  14. data/activerecord-jdbc-adapter.gemspec +42 -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 +60 -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 +19 -0
  43. data/lib/arjdbc/abstract/database_statements.rb +92 -0
  44. data/lib/arjdbc/abstract/transaction_support.rb +86 -0
  45. data/lib/arjdbc/common_jdbc_methods.rb +13 -0
  46. data/lib/arjdbc/db2.rb +4 -0
  47. data/lib/arjdbc/db2/adapter.rb +789 -0
  48. data/lib/arjdbc/db2/as400.rb +130 -0
  49. data/lib/arjdbc/db2/column.rb +167 -0
  50. data/lib/arjdbc/db2/connection_methods.rb +44 -0
  51. data/lib/arjdbc/derby.rb +3 -0
  52. data/lib/arjdbc/derby/active_record_patch.rb +13 -0
  53. data/lib/arjdbc/derby/adapter.rb +556 -0
  54. data/lib/arjdbc/derby/connection_methods.rb +20 -0
  55. data/lib/arjdbc/derby/schema_creation.rb +15 -0
  56. data/lib/arjdbc/discover.rb +115 -0
  57. data/lib/arjdbc/firebird.rb +4 -0
  58. data/lib/arjdbc/firebird/adapter.rb +434 -0
  59. data/lib/arjdbc/firebird/connection_methods.rb +23 -0
  60. data/lib/arjdbc/h2.rb +3 -0
  61. data/lib/arjdbc/h2/adapter.rb +303 -0
  62. data/lib/arjdbc/h2/connection_methods.rb +27 -0
  63. data/lib/arjdbc/hsqldb.rb +3 -0
  64. data/lib/arjdbc/hsqldb/adapter.rb +297 -0
  65. data/lib/arjdbc/hsqldb/connection_methods.rb +28 -0
  66. data/lib/arjdbc/hsqldb/explain_support.rb +35 -0
  67. data/lib/arjdbc/hsqldb/schema_creation.rb +11 -0
  68. data/lib/arjdbc/informix.rb +5 -0
  69. data/lib/arjdbc/informix/adapter.rb +162 -0
  70. data/lib/arjdbc/informix/connection_methods.rb +9 -0
  71. data/lib/arjdbc/jdbc.rb +59 -0
  72. data/lib/arjdbc/jdbc/adapter.rb +899 -0
  73. data/lib/arjdbc/jdbc/adapter_java.jar +0 -0
  74. data/lib/arjdbc/jdbc/adapter_require.rb +46 -0
  75. data/lib/arjdbc/jdbc/base_ext.rb +44 -0
  76. data/lib/arjdbc/jdbc/callbacks.rb +51 -0
  77. data/lib/arjdbc/jdbc/column.rb +97 -0
  78. data/lib/arjdbc/jdbc/connection.rb +133 -0
  79. data/lib/arjdbc/jdbc/connection_methods.rb +36 -0
  80. data/lib/arjdbc/jdbc/driver.rb +43 -0
  81. data/lib/arjdbc/jdbc/extension.rb +59 -0
  82. data/lib/arjdbc/jdbc/java.rb +15 -0
  83. data/lib/arjdbc/jdbc/jdbc.rake +4 -0
  84. data/lib/arjdbc/jdbc/quoted_primary_key.rb +28 -0
  85. data/lib/arjdbc/jdbc/railtie.rb +2 -0
  86. data/lib/arjdbc/jdbc/rake_tasks.rb +3 -0
  87. data/lib/arjdbc/jdbc/serialized_attributes_helper.rb +3 -0
  88. data/lib/arjdbc/jdbc/type_cast.rb +166 -0
  89. data/lib/arjdbc/jdbc/type_converter.rb +142 -0
  90. data/lib/arjdbc/mimer.rb +3 -0
  91. data/lib/arjdbc/mimer/adapter.rb +142 -0
  92. data/lib/arjdbc/mssql.rb +7 -0
  93. data/lib/arjdbc/mssql/adapter.rb +808 -0
  94. data/lib/arjdbc/mssql/column.rb +200 -0
  95. data/lib/arjdbc/mssql/connection_methods.rb +79 -0
  96. data/lib/arjdbc/mssql/explain_support.rb +99 -0
  97. data/lib/arjdbc/mssql/limit_helpers.rb +231 -0
  98. data/lib/arjdbc/mssql/lock_methods.rb +77 -0
  99. data/lib/arjdbc/mssql/types.rb +343 -0
  100. data/lib/arjdbc/mssql/utils.rb +82 -0
  101. data/lib/arjdbc/mysql.rb +3 -0
  102. data/lib/arjdbc/mysql/adapter.rb +1006 -0
  103. data/lib/arjdbc/mysql/bulk_change_table.rb +150 -0
  104. data/lib/arjdbc/mysql/column.rb +162 -0
  105. data/lib/arjdbc/mysql/connection_methods.rb +145 -0
  106. data/lib/arjdbc/mysql/explain_support.rb +82 -0
  107. data/lib/arjdbc/mysql/schema_creation.rb +58 -0
  108. data/lib/arjdbc/oracle.rb +4 -0
  109. data/lib/arjdbc/oracle/adapter.rb +952 -0
  110. data/lib/arjdbc/oracle/column.rb +126 -0
  111. data/lib/arjdbc/oracle/connection_methods.rb +21 -0
  112. data/lib/arjdbc/postgresql.rb +3 -0
  113. data/lib/arjdbc/postgresql/_bc_time_cast_patch.rb +21 -0
  114. data/lib/arjdbc/postgresql/adapter.rb +825 -0
  115. data/lib/arjdbc/postgresql/base/array_decoder.rb +26 -0
  116. data/lib/arjdbc/postgresql/base/array_encoder.rb +25 -0
  117. data/lib/arjdbc/postgresql/base/array_parser.rb +95 -0
  118. data/lib/arjdbc/postgresql/base/pgconn.rb +11 -0
  119. data/lib/arjdbc/postgresql/column.rb +51 -0
  120. data/lib/arjdbc/postgresql/connection_methods.rb +54 -0
  121. data/lib/arjdbc/postgresql/name.rb +24 -0
  122. data/lib/arjdbc/postgresql/oid_types.rb +178 -0
  123. data/lib/arjdbc/railtie.rb +11 -0
  124. data/lib/arjdbc/sqlite3.rb +3 -0
  125. data/lib/arjdbc/sqlite3/adapter.rb +703 -0
  126. data/lib/arjdbc/sqlite3/connection_methods.rb +40 -0
  127. data/lib/arjdbc/sybase.rb +2 -0
  128. data/lib/arjdbc/sybase/adapter.rb +47 -0
  129. data/lib/arjdbc/tasks.rb +13 -0
  130. data/lib/arjdbc/tasks/database_tasks.rb +54 -0
  131. data/lib/arjdbc/tasks/databases.rake +91 -0
  132. data/lib/arjdbc/tasks/databases3.rake +215 -0
  133. data/lib/arjdbc/tasks/databases4.rake +39 -0
  134. data/lib/arjdbc/tasks/db2_database_tasks.rb +104 -0
  135. data/lib/arjdbc/tasks/derby_database_tasks.rb +95 -0
  136. data/lib/arjdbc/tasks/h2_database_tasks.rb +31 -0
  137. data/lib/arjdbc/tasks/hsqldb_database_tasks.rb +70 -0
  138. data/lib/arjdbc/tasks/jdbc_database_tasks.rb +169 -0
  139. data/lib/arjdbc/tasks/mssql_database_tasks.rb +46 -0
  140. data/lib/arjdbc/tasks/oracle/enhanced_structure_dump.rb +297 -0
  141. data/lib/arjdbc/tasks/oracle_database_tasks.rb +65 -0
  142. data/lib/arjdbc/util/quoted_cache.rb +60 -0
  143. data/lib/arjdbc/util/serialized_attributes.rb +98 -0
  144. data/lib/arjdbc/util/table_copier.rb +110 -0
  145. data/lib/arjdbc/version.rb +8 -0
  146. data/lib/generators/jdbc/USAGE +9 -0
  147. data/lib/generators/jdbc/jdbc_generator.rb +17 -0
  148. data/lib/jdbc_adapter.rb +2 -0
  149. data/lib/jdbc_adapter/rake_tasks.rb +4 -0
  150. data/lib/jdbc_adapter/version.rb +4 -0
  151. data/pom.xml +114 -0
  152. data/rails_generators/jdbc_generator.rb +15 -0
  153. data/rails_generators/templates/config/initializers/jdbc.rb +10 -0
  154. data/rails_generators/templates/lib/tasks/jdbc.rake +11 -0
  155. data/rakelib/01-tomcat.rake +51 -0
  156. data/rakelib/02-test.rake +121 -0
  157. data/rakelib/bundler_ext.rb +11 -0
  158. data/rakelib/compile.rake +62 -0
  159. data/rakelib/db.rake +58 -0
  160. data/rakelib/rails.rake +75 -0
  161. data/src/java/arjdbc/ArJdbcModule.java +178 -0
  162. data/src/java/arjdbc/db2/DB2Module.java +71 -0
  163. data/src/java/arjdbc/db2/DB2RubyJdbcConnection.java +142 -0
  164. data/src/java/arjdbc/derby/DerbyModule.java +179 -0
  165. data/src/java/arjdbc/derby/DerbyRubyJdbcConnection.java +164 -0
  166. data/src/java/arjdbc/firebird/FirebirdRubyJdbcConnection.java +190 -0
  167. data/src/java/arjdbc/h2/H2Module.java +44 -0
  168. data/src/java/arjdbc/h2/H2RubyJdbcConnection.java +67 -0
  169. data/src/java/arjdbc/hsqldb/HSQLDBModule.java +68 -0
  170. data/src/java/arjdbc/informix/InformixRubyJdbcConnection.java +75 -0
  171. data/src/java/arjdbc/jdbc/AdapterJavaService.java +45 -0
  172. data/src/java/arjdbc/jdbc/Callable.java +44 -0
  173. data/src/java/arjdbc/jdbc/JdbcConnectionFactory.java +45 -0
  174. data/src/java/arjdbc/jdbc/RubyJdbcConnection.java +3616 -0
  175. data/src/java/arjdbc/jdbc/SQLBlock.java +54 -0
  176. data/src/java/arjdbc/mssql/MSSQLModule.java +102 -0
  177. data/src/java/arjdbc/mssql/MSSQLRubyJdbcConnection.java +195 -0
  178. data/src/java/arjdbc/mysql/MySQLModule.java +147 -0
  179. data/src/java/arjdbc/mysql/MySQLRubyJdbcConnection.java +397 -0
  180. data/src/java/arjdbc/oracle/OracleModule.java +75 -0
  181. data/src/java/arjdbc/oracle/OracleRubyJdbcConnection.java +465 -0
  182. data/src/java/arjdbc/postgresql/PostgreSQLRubyJdbcConnection.java +752 -0
  183. data/src/java/arjdbc/sqlite3/SQLite3Module.java +78 -0
  184. data/src/java/arjdbc/sqlite3/SQLite3RubyJdbcConnection.java +351 -0
  185. data/src/java/arjdbc/util/CallResultSet.java +826 -0
  186. data/src/java/arjdbc/util/QuotingUtils.java +111 -0
  187. metadata +255 -0
@@ -0,0 +1,71 @@
1
+ /*
2
+ * The MIT License
3
+ *
4
+ * Copyright 2013 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.db2;
25
+
26
+ import static arjdbc.util.QuotingUtils.BYTES_0;
27
+ import static arjdbc.util.QuotingUtils.BYTES_1;
28
+ import static arjdbc.util.QuotingUtils.quoteSingleQuotesWithFallback;
29
+
30
+ import org.jruby.RubyModule;
31
+ import org.jruby.RubyString;
32
+ import org.jruby.anno.JRubyMethod;
33
+ import org.jruby.runtime.ThreadContext;
34
+ import org.jruby.runtime.builtin.IRubyObject;
35
+
36
+ /**
37
+ * ArJdbc::DB2
38
+ *
39
+ * @author kares
40
+ */
41
+ public class DB2Module {
42
+
43
+ public static RubyModule load(final RubyModule arJdbc) {
44
+ RubyModule db2 = arJdbc.defineModuleUnder("DB2");
45
+ db2.defineAnnotatedMethods( DB2Module.class );
46
+ return db2;
47
+ }
48
+
49
+ @JRubyMethod(name = "quote_string", required = 1, frame = false)
50
+ public static IRubyObject quote_string(
51
+ final ThreadContext context,
52
+ final IRubyObject self,
53
+ final IRubyObject string) {
54
+ return quoteSingleQuotesWithFallback(context, string);
55
+ }
56
+
57
+ @JRubyMethod(name = "quoted_true", required = 0, frame = false)
58
+ public static IRubyObject quoted_true(
59
+ final ThreadContext context,
60
+ final IRubyObject self) {
61
+ return RubyString.newString(context.getRuntime(), BYTES_1);
62
+ }
63
+
64
+ @JRubyMethod(name = "quoted_false", required = 0, frame = false)
65
+ public static IRubyObject quoted_false(
66
+ final ThreadContext context,
67
+ final IRubyObject self) {
68
+ return RubyString.newString(context.getRuntime(), BYTES_0);
69
+ }
70
+
71
+ }
@@ -0,0 +1,142 @@
1
+ /***** BEGIN LICENSE BLOCK *****
2
+ * Copyright (c) 2012-2013 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
+ package arjdbc.db2;
27
+
28
+ import arjdbc.jdbc.Callable;
29
+ import arjdbc.jdbc.RubyJdbcConnection;
30
+
31
+ import java.sql.Connection;
32
+ import java.sql.PreparedStatement;
33
+ import java.sql.ResultSet;
34
+ import java.sql.SQLException;
35
+ import java.sql.Statement;
36
+
37
+ import org.jruby.Ruby;
38
+ import org.jruby.RubyClass;
39
+ import org.jruby.RubyString;
40
+ import org.jruby.anno.JRubyMethod;
41
+ import org.jruby.runtime.ObjectAllocator;
42
+ import org.jruby.runtime.ThreadContext;
43
+ import org.jruby.runtime.builtin.IRubyObject;
44
+ import org.jruby.util.ByteList;
45
+
46
+ /**
47
+ *
48
+ * @author mikestone
49
+ */
50
+ public class DB2RubyJdbcConnection extends RubyJdbcConnection {
51
+
52
+ protected DB2RubyJdbcConnection(Ruby runtime, RubyClass metaClass) {
53
+ super(runtime, metaClass);
54
+ }
55
+
56
+ public static RubyClass createDB2JdbcConnectionClass(Ruby runtime, RubyClass jdbcConnection) {
57
+ RubyClass clazz = RubyJdbcConnection.getConnectionAdapters(runtime).defineClassUnder("DB2JdbcConnection",
58
+ jdbcConnection, DB2_JDBCCONNECTION_ALLOCATOR);
59
+ clazz.defineAnnotatedMethods(DB2RubyJdbcConnection.class);
60
+
61
+ return clazz;
62
+ }
63
+
64
+ private static ObjectAllocator DB2_JDBCCONNECTION_ALLOCATOR = new ObjectAllocator() {
65
+ public IRubyObject allocate(Ruby runtime, RubyClass klass) {
66
+ return new DB2RubyJdbcConnection(runtime, klass);
67
+ }
68
+ };
69
+
70
+ @JRubyMethod(name = "select?", required = 1, meta = true, frame = false)
71
+ public static IRubyObject select_p(final ThreadContext context,
72
+ final IRubyObject self, final IRubyObject sql) {
73
+ if ( isValues(sql.convertToString()) ) {
74
+ return context.getRuntime().newBoolean( true );
75
+ }
76
+ return arjdbc.jdbc.RubyJdbcConnection.select_p(context, self, sql);
77
+ }
78
+
79
+ // DB2 supports 'stand-alone' VALUES expressions
80
+ private static final byte[] VALUES = new byte[]{ 'v','a','l','u', 'e', 's' };
81
+
82
+ private static boolean isValues(final RubyString sql) {
83
+ final ByteList sqlBytes = sql.getByteList();
84
+ return startsWithIgnoreCase(sqlBytes, VALUES);
85
+ }
86
+
87
+ private static final String[] TABLE_TYPES = new String[] {
88
+ "TABLE", "VIEW", "SYNONYM", "MATERIALIZED QUERY TABLE", "ALIAS"
89
+ };
90
+
91
+ @Override
92
+ protected String[] getTableTypes() {
93
+ return TABLE_TYPES;
94
+ }
95
+
96
+ @Override
97
+ protected boolean databaseSupportsSchemas() {
98
+ return true;
99
+ }
100
+
101
+ @JRubyMethod(name = {"identity_val_local", "last_insert_id"})
102
+ public IRubyObject identity_val_local(final ThreadContext context)
103
+ throws SQLException {
104
+ return withConnection(context, new Callable<IRubyObject>() {
105
+ public IRubyObject call(final Connection connection) throws SQLException {
106
+ PreparedStatement statement = null; ResultSet genKeys = null;
107
+ try {
108
+ statement = connection.prepareStatement("VALUES IDENTITY_VAL_LOCAL()");
109
+ genKeys = statement.executeQuery();
110
+ return doMapGeneratedKeys(context.getRuntime(), genKeys, true);
111
+ }
112
+ catch (final SQLException e) {
113
+ debugMessage(context, "failed to get generated keys: " + e.getMessage());
114
+ throw e;
115
+ }
116
+ finally { close(genKeys); close(statement); }
117
+ }
118
+ });
119
+ }
120
+
121
+ // NOTE: this is non-sense or DB2 - but it has been originally implemented this way !
122
+ //@JRubyMethod(name = {"identity_val_local", "last_insert_id"}, required = 1)
123
+ private IRubyObject identity_val_local(final ThreadContext context, final IRubyObject table)
124
+ throws SQLException {
125
+ return withConnection(context, new Callable<IRubyObject>() {
126
+ public IRubyObject call(final Connection connection) throws SQLException {
127
+ Statement statement = null; ResultSet genKeys = null;
128
+ try {
129
+ statement = connection.createStatement();
130
+ genKeys = statement.executeQuery("SELECT IDENTITY_VAL_LOCAL() FROM " + table);
131
+ return doMapGeneratedKeys(context.getRuntime(), genKeys, true);
132
+ }
133
+ catch (final SQLException e) {
134
+ debugMessage(context, "failed to get generated keys: " + e.getMessage());
135
+ throw e;
136
+ }
137
+ finally { close(genKeys); close(statement); }
138
+ }
139
+ });
140
+ }
141
+
142
+ }
@@ -0,0 +1,179 @@
1
+ /***** BEGIN LICENSE BLOCK *****
2
+ * Copyright (c) 2012-2013 Karol Bucek <self@kares.org>
3
+ * Copyright (c) 2006-2011 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.derby;
27
+
28
+ import static arjdbc.util.QuotingUtils.BYTES_0;
29
+ import static arjdbc.util.QuotingUtils.BYTES_1;
30
+ import static arjdbc.util.QuotingUtils.BYTES_SINGLE_Q_x2;
31
+
32
+ import org.jruby.Ruby;
33
+ import org.jruby.RubyModule;
34
+ import org.jruby.RubyString;
35
+ import org.jruby.anno.JRubyMethod;
36
+ import org.jruby.runtime.ThreadContext;
37
+ import org.jruby.runtime.builtin.IRubyObject;
38
+ import org.jruby.util.ByteList;
39
+
40
+ public class DerbyModule {
41
+
42
+ public static RubyModule load(final RubyModule arJdbc) {
43
+ RubyModule derby = arJdbc.defineModuleUnder("Derby");
44
+ derby.defineAnnotatedMethods( DerbyModule.class );
45
+ RubyModule column = derby.defineModuleUnder("Column");
46
+ column.defineAnnotatedMethods(Column.class);
47
+ return derby;
48
+ }
49
+
50
+ public static class Column {
51
+
52
+ @JRubyMethod(name = "type_cast", required = 1)
53
+ public static IRubyObject type_cast(final ThreadContext context,
54
+ final IRubyObject self, final IRubyObject value) {
55
+
56
+ if ( value.isNil() ||
57
+ ( (value instanceof RubyString) && value.toString().trim().equalsIgnoreCase("null") ) ) {
58
+ return context.getRuntime().getNil();
59
+ }
60
+
61
+ final String type = self.getInstanceVariables().getInstanceVariable("@type").toString();
62
+
63
+ switch (type.charAt(0)) {
64
+ case 's': //string
65
+ return value;
66
+ case 't': //text, timestamp, time
67
+ if ( type.equals("time") ) {
68
+ return self.getMetaClass().callMethod(context, "string_to_dummy_time", value);
69
+ }
70
+ if ( type.equals("timestamp") ) {
71
+ return self.getMetaClass().callMethod(context, "string_to_time", value);
72
+ }
73
+ return value; // text
74
+ case 'i': //integer
75
+ case 'p': //primary key
76
+ if ( value.respondsTo("to_i") ) {
77
+ return value.callMethod(context, "to_i");
78
+ }
79
+ return context.getRuntime().newFixnum( value.isTrue() ? 1 : 0 );
80
+ case 'd': //decimal, datetime, date
81
+ if ( type.equals("datetime") ) {
82
+ return self.getMetaClass().callMethod(context, "string_to_time", value);
83
+ }
84
+ if ( type.equals("date") ) {
85
+ return self.getMetaClass().callMethod(context, "string_to_date", value);
86
+ }
87
+ return self.getMetaClass().callMethod(context, "value_to_decimal", value);
88
+ case 'f': //float
89
+ return value.callMethod(context, "to_f");
90
+ case 'b': //binary, boolean
91
+ return type.equals("binary") ?
92
+ self.getMetaClass().callMethod(context, "binary_to_string", value) :
93
+ self.getMetaClass().callMethod(context, "value_to_boolean", value) ;
94
+ }
95
+ return value;
96
+ }
97
+
98
+ }
99
+
100
+ @JRubyMethod(name = "quote_binary", required = 1)
101
+ public static IRubyObject quote_binary(final ThreadContext context,
102
+ final IRubyObject self, final IRubyObject string) {
103
+ return quoteStringHex(context.getRuntime(), "", string, "");
104
+ }
105
+
106
+ private final static byte[] HEX = {'0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F'};
107
+
108
+ private static IRubyObject quoteStringHex(final Ruby runtime,
109
+ final String before, final IRubyObject string, final String after) {
110
+
111
+ final ByteList input = ((RubyString) string).getByteList();
112
+ final int size = input.getRealSize();
113
+ final ByteList output = new ByteList( before.length() + size * 2 + after.length() );
114
+ output.append( before.getBytes() );
115
+
116
+ final byte[] inputBytes = input.unsafeBytes();
117
+
118
+ int written = 0;
119
+ for (int i = input.getBegin(); i < input.getBegin() + size; i++) {
120
+ final byte b = inputBytes[i];
121
+ byte h = HEX[ ( ((char) b) >> 4 ) % 16 ];
122
+ byte l = HEX[ ( (char) b ) % 16 ];
123
+ output.append(h).append(l);
124
+ written += 2;
125
+ if ( written >= 16334 ) { // max hex length = 16334
126
+ output.append("'||X'".getBytes());
127
+ written = 0;
128
+ }
129
+ }
130
+
131
+ output.append( after.getBytes() );
132
+ return RubyString.newStringShared(runtime, output);
133
+ }
134
+
135
+ @JRubyMethod(name = "quote_string", required = 1)
136
+ public static IRubyObject quote_string(final IRubyObject self, IRubyObject string) {
137
+ if ( ! ( string instanceof RubyString ) ) {
138
+ string = string.asString(); // e.g. Multibyte::Chars
139
+ }
140
+
141
+ ByteList bytes = ((RubyString) string).getByteList();
142
+
143
+ boolean replacement = false;
144
+ for ( int i = 0; i < bytes.length(); i++ ) {
145
+ switch ( bytes.get(i) ) {
146
+ case '\'': break;
147
+ default: continue;
148
+ }
149
+ // on first replacement allocate so we don't manip original
150
+ if ( ! replacement ) {
151
+ bytes = new ByteList(bytes);
152
+ replacement = true;
153
+ }
154
+
155
+ bytes.replace(i, 1, BYTES_SINGLE_Q_x2);
156
+ i += 1;
157
+ }
158
+
159
+ return replacement ? RubyString.newStringShared(self.getRuntime(), bytes) : string;
160
+ }
161
+
162
+ @JRubyMethod(name = "quoted_true", required = 0, frame = false)
163
+ public static IRubyObject quoted_true(
164
+ final ThreadContext context, final IRubyObject self) {
165
+ return RubyString.newString(context.getRuntime(), BYTES_1);
166
+ }
167
+
168
+ @JRubyMethod(name = "quoted_false", required = 0, frame = false)
169
+ public static IRubyObject quoted_false(
170
+ final ThreadContext context, final IRubyObject self) {
171
+ return RubyString.newString(context.getRuntime(), BYTES_0);
172
+ }
173
+
174
+ private static RubyModule getMultibyteChars(final Ruby runtime) {
175
+ return (RubyModule) ((RubyModule) runtime.getModule("ActiveSupport").
176
+ getConstant("Multibyte")).getConstantAt("Chars");
177
+ }
178
+
179
+ }
@@ -0,0 +1,164 @@
1
+ /*
2
+ * The MIT License
3
+ *
4
+ * Copyright 2013 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.derby;
25
+
26
+ import arjdbc.jdbc.Callable;
27
+ import arjdbc.jdbc.RubyJdbcConnection;
28
+
29
+ import java.sql.Connection;
30
+ import java.sql.PreparedStatement;
31
+ import java.sql.ResultSet;
32
+ import java.sql.SQLException;
33
+
34
+ import org.jruby.Ruby;
35
+ import org.jruby.RubyClass;
36
+ import org.jruby.RubyString;
37
+ import org.jruby.anno.JRubyMethod;
38
+ import org.jruby.runtime.ObjectAllocator;
39
+ import org.jruby.runtime.ThreadContext;
40
+ import org.jruby.runtime.builtin.IRubyObject;
41
+ import org.jruby.util.ByteList;
42
+
43
+ /**
44
+ * ArJdbc::DerbyJdbcConnection
45
+ *
46
+ * @author kares
47
+ */
48
+ public class DerbyRubyJdbcConnection extends RubyJdbcConnection {
49
+ private static final long serialVersionUID = 4809475910953623325L;
50
+
51
+ protected DerbyRubyJdbcConnection(Ruby runtime, RubyClass metaClass) {
52
+ super(runtime, metaClass);
53
+ }
54
+
55
+ public static RubyClass createDerbyJdbcConnectionClass(Ruby runtime, RubyClass jdbcConnection) {
56
+ final RubyClass clazz = getConnectionAdapters(runtime). // ActiveRecord::ConnectionAdapters
57
+ defineClassUnder("DerbyJdbcConnection", jdbcConnection, DERBY_JDBCCONNECTION_ALLOCATOR);
58
+ clazz.defineAnnotatedMethods(DerbyRubyJdbcConnection.class);
59
+ return clazz;
60
+ }
61
+
62
+ private static ObjectAllocator DERBY_JDBCCONNECTION_ALLOCATOR = new ObjectAllocator() {
63
+ public IRubyObject allocate(Ruby runtime, RubyClass klass) {
64
+ return new DerbyRubyJdbcConnection(runtime, klass);
65
+ }
66
+ };
67
+
68
+ @JRubyMethod(name = "select?", required = 1, meta = true, frame = false)
69
+ public static IRubyObject select_p(final ThreadContext context,
70
+ final IRubyObject self, final IRubyObject sql) {
71
+ if ( isValues(sql.convertToString()) ) {
72
+ return context.getRuntime().newBoolean( true );
73
+ }
74
+ return arjdbc.jdbc.RubyJdbcConnection.select_p(context, self, sql);
75
+ }
76
+
77
+ // Derby supports 'stand-alone' VALUES expressions
78
+ private static final byte[] VALUES = new byte[]{ 'v','a','l','u', 'e', 's' };
79
+
80
+ private static boolean isValues(final RubyString sql) {
81
+ final ByteList sqlBytes = sql.getByteList();
82
+ return startsWithIgnoreCase(sqlBytes, VALUES);
83
+ }
84
+
85
+ @JRubyMethod(name = {"identity_val_local", "last_insert_id"})
86
+ public IRubyObject identity_val_local(final ThreadContext context)
87
+ throws SQLException {
88
+ return withConnection(context, new Callable<IRubyObject>() {
89
+ public IRubyObject call(final Connection connection) throws SQLException {
90
+ PreparedStatement statement = null; ResultSet genKeys = null;
91
+ try {
92
+ statement = connection.prepareStatement("values IDENTITY_VAL_LOCAL()");
93
+ genKeys = statement.executeQuery();
94
+ return doMapGeneratedKeys(context.getRuntime(), genKeys, true);
95
+ }
96
+ catch (final SQLException e) {
97
+ debugMessage(context, "failed to get generated keys: " + e.getMessage());
98
+ throw e;
99
+ }
100
+ finally { close(genKeys); close(statement); }
101
+ }
102
+ });
103
+ }
104
+
105
+ @Override
106
+ protected boolean supportsGeneratedKeys(final Connection connection) throws SQLException {
107
+ return true; // driver reports false from supportsGetGeneratedKeys()
108
+ }
109
+
110
+ @Override
111
+ protected IRubyObject matchTables(final Ruby runtime,
112
+ final Connection connection,
113
+ final String catalog, String schemaPattern,
114
+ final String tablePattern, final String[] types,
115
+ final boolean checkExistsOnly) throws SQLException {
116
+ if (schemaPattern != null && schemaPattern.equals("")) {
117
+ schemaPattern = null; // Derby doesn't like empty-string schema name
118
+ }
119
+ return super.matchTables(runtime, connection, catalog, schemaPattern, tablePattern, types, checkExistsOnly);
120
+ }
121
+
122
+ @JRubyMethod(name = "transaction_isolation", alias = "get_transaction_isolation")
123
+ public IRubyObject get_transaction_isolation(final ThreadContext context) {
124
+ return withConnection(context, new Callable<IRubyObject>() {
125
+ public IRubyObject call(final Connection connection) throws SQLException {
126
+ final int level = connection.getTransactionIsolation();
127
+ final String isolationSymbol = formatTransactionIsolationLevel(level);
128
+ if ( isolationSymbol == null ) return context.getRuntime().getNil();
129
+ return context.getRuntime().newSymbol(isolationSymbol);
130
+ }
131
+ });
132
+ }
133
+
134
+ @JRubyMethod(name = "transaction_isolation=", alias = "set_transaction_isolation")
135
+ public IRubyObject set_transaction_isolation(final ThreadContext context, final IRubyObject isolation) {
136
+ return withConnection(context, new Callable<IRubyObject>() {
137
+ public IRubyObject call(final Connection connection) throws SQLException {
138
+ final int level;
139
+ if ( isolation.isNil() ) {
140
+ level = connection.getMetaData().getDefaultTransactionIsolation();
141
+ }
142
+ else {
143
+ level = mapTransactionIsolationLevel(isolation);
144
+ }
145
+
146
+ connection.setTransactionIsolation(level);
147
+
148
+ final String isolationSymbol = formatTransactionIsolationLevel(level);
149
+ if ( isolationSymbol == null ) return context.getRuntime().getNil();
150
+ return context.getRuntime().newSymbol(isolationSymbol);
151
+ }
152
+ });
153
+ }
154
+
155
+ public static String formatTransactionIsolationLevel(final int level) {
156
+ if ( level == Connection.TRANSACTION_READ_UNCOMMITTED ) return "read_uncommitted"; // 1
157
+ if ( level == Connection.TRANSACTION_READ_COMMITTED ) return "read_committed"; // 2
158
+ if ( level == Connection.TRANSACTION_REPEATABLE_READ ) return "repeatable_read"; // 4
159
+ if ( level == Connection.TRANSACTION_SERIALIZABLE ) return "serializable"; // 8
160
+ if ( level == 0 ) return null;
161
+ throw new IllegalArgumentException("unexpected transaction isolation level: " + level);
162
+ }
163
+
164
+ }