activerecord-jdbc-adapter 0.9.3-java

Sign up to get free protection for your applications and to get access to all the features.
Files changed (121) hide show
  1. data/History.txt +248 -0
  2. data/LICENSE.txt +21 -0
  3. data/Manifest.txt +125 -0
  4. data/README.txt +218 -0
  5. data/Rakefile +10 -0
  6. data/lib/active_record/connection_adapters/cachedb_adapter.rb +1 -0
  7. data/lib/active_record/connection_adapters/derby_adapter.rb +13 -0
  8. data/lib/active_record/connection_adapters/h2_adapter.rb +13 -0
  9. data/lib/active_record/connection_adapters/hsqldb_adapter.rb +13 -0
  10. data/lib/active_record/connection_adapters/informix_adapter.rb +1 -0
  11. data/lib/active_record/connection_adapters/jdbc_adapter.rb +640 -0
  12. data/lib/active_record/connection_adapters/jdbc_adapter_spec.rb +26 -0
  13. data/lib/active_record/connection_adapters/jndi_adapter.rb +1 -0
  14. data/lib/active_record/connection_adapters/mysql_adapter.rb +13 -0
  15. data/lib/active_record/connection_adapters/oracle_adapter.rb +1 -0
  16. data/lib/active_record/connection_adapters/postgresql_adapter.rb +13 -0
  17. data/lib/active_record/connection_adapters/sqlite3_adapter.rb +13 -0
  18. data/lib/generators/jdbc/jdbc_generator.rb +9 -0
  19. data/lib/jdbc_adapter.rb +27 -0
  20. data/lib/jdbc_adapter/jdbc.rake +121 -0
  21. data/lib/jdbc_adapter/jdbc_adapter_internal.jar +0 -0
  22. data/lib/jdbc_adapter/jdbc_cachedb.rb +33 -0
  23. data/lib/jdbc_adapter/jdbc_db2.rb +203 -0
  24. data/lib/jdbc_adapter/jdbc_derby.rb +430 -0
  25. data/lib/jdbc_adapter/jdbc_firebird.rb +109 -0
  26. data/lib/jdbc_adapter/jdbc_hsqldb.rb +218 -0
  27. data/lib/jdbc_adapter/jdbc_informix.rb +147 -0
  28. data/lib/jdbc_adapter/jdbc_mimer.rb +141 -0
  29. data/lib/jdbc_adapter/jdbc_mssql.rb +337 -0
  30. data/lib/jdbc_adapter/jdbc_mysql.rb +236 -0
  31. data/lib/jdbc_adapter/jdbc_oracle.rb +377 -0
  32. data/lib/jdbc_adapter/jdbc_postgre.rb +498 -0
  33. data/lib/jdbc_adapter/jdbc_sqlite3.rb +384 -0
  34. data/lib/jdbc_adapter/jdbc_sybase.rb +50 -0
  35. data/lib/jdbc_adapter/missing_functionality_helper.rb +87 -0
  36. data/lib/jdbc_adapter/rake_tasks.rb +10 -0
  37. data/lib/jdbc_adapter/tsql_helper.rb +60 -0
  38. data/lib/jdbc_adapter/version.rb +5 -0
  39. data/lib/pg.rb +4 -0
  40. data/rails_generators/jdbc_generator.rb +15 -0
  41. data/rails_generators/templates/config/initializers/jdbc.rb +7 -0
  42. data/rails_generators/templates/lib/tasks/jdbc.rake +8 -0
  43. data/rakelib/compile.rake +23 -0
  44. data/rakelib/package.rake +90 -0
  45. data/rakelib/rails.rake +41 -0
  46. data/rakelib/test.rake +76 -0
  47. data/src/java/jdbc_adapter/JdbcAdapterInternalService.java +53 -0
  48. data/src/java/jdbc_adapter/JdbcConnectionFactory.java +36 -0
  49. data/src/java/jdbc_adapter/JdbcDerbySpec.java +293 -0
  50. data/src/java/jdbc_adapter/JdbcMySQLSpec.java +134 -0
  51. data/src/java/jdbc_adapter/MssqlRubyJdbcConnection.java +71 -0
  52. data/src/java/jdbc_adapter/PostgresRubyJdbcConnection.java +55 -0
  53. data/src/java/jdbc_adapter/RubyJdbcConnection.java +1162 -0
  54. data/src/java/jdbc_adapter/SQLBlock.java +27 -0
  55. data/src/java/jdbc_adapter/Sqlite3RubyJdbcConnection.java +41 -0
  56. data/test/abstract_db_create.rb +107 -0
  57. data/test/activerecord/connection_adapters/type_conversion_test.rb +31 -0
  58. data/test/activerecord/connections/native_jdbc_mysql/connection.rb +25 -0
  59. data/test/cachedb_simple_test.rb +6 -0
  60. data/test/db/cachedb.rb +9 -0
  61. data/test/db/db2.rb +9 -0
  62. data/test/db/derby.rb +14 -0
  63. data/test/db/h2.rb +11 -0
  64. data/test/db/hsqldb.rb +12 -0
  65. data/test/db/informix.rb +11 -0
  66. data/test/db/jdbc.rb +11 -0
  67. data/test/db/jndi_config.rb +30 -0
  68. data/test/db/logger.rb +3 -0
  69. data/test/db/mssql.rb +9 -0
  70. data/test/db/mysql.rb +10 -0
  71. data/test/db/oracle.rb +34 -0
  72. data/test/db/postgres.rb +9 -0
  73. data/test/db/sqlite3.rb +15 -0
  74. data/test/db2_simple_test.rb +10 -0
  75. data/test/derby_migration_test.rb +21 -0
  76. data/test/derby_multibyte_test.rb +12 -0
  77. data/test/derby_simple_test.rb +21 -0
  78. data/test/generic_jdbc_connection_test.rb +9 -0
  79. data/test/h2_simple_test.rb +6 -0
  80. data/test/has_many_through.rb +79 -0
  81. data/test/helper.rb +5 -0
  82. data/test/hsqldb_simple_test.rb +6 -0
  83. data/test/informix_simple_test.rb +48 -0
  84. data/test/jdbc_adapter/jdbc_db2_test.rb +26 -0
  85. data/test/jdbc_adapter/jdbc_sybase_test.rb +33 -0
  86. data/test/jdbc_common.rb +25 -0
  87. data/test/jndi_callbacks_test.rb +38 -0
  88. data/test/jndi_test.rb +35 -0
  89. data/test/manualTestDatabase.rb +191 -0
  90. data/test/minirunit.rb +109 -0
  91. data/test/minirunit/testConnect.rb +14 -0
  92. data/test/minirunit/testH2.rb +73 -0
  93. data/test/minirunit/testHsqldb.rb +73 -0
  94. data/test/minirunit/testLoadActiveRecord.rb +3 -0
  95. data/test/minirunit/testMysql.rb +83 -0
  96. data/test/minirunit/testRawSelect.rb +24 -0
  97. data/test/models/add_not_null_column_to_table.rb +12 -0
  98. data/test/models/auto_id.rb +18 -0
  99. data/test/models/data_types.rb +28 -0
  100. data/test/models/entry.rb +23 -0
  101. data/test/models/mixed_case.rb +20 -0
  102. data/test/models/reserved_word.rb +18 -0
  103. data/test/models/string_id.rb +18 -0
  104. data/test/models/validates_uniqueness_of_string.rb +19 -0
  105. data/test/mssql_simple_test.rb +6 -0
  106. data/test/mysql_db_create_test.rb +25 -0
  107. data/test/mysql_multibyte_test.rb +10 -0
  108. data/test/mysql_nonstandard_primary_key_test.rb +42 -0
  109. data/test/mysql_simple_test.rb +32 -0
  110. data/test/oracle_simple_test.rb +29 -0
  111. data/test/pick_rails_version.rb +3 -0
  112. data/test/postgres_db_create_test.rb +21 -0
  113. data/test/postgres_mixed_case_test.rb +19 -0
  114. data/test/postgres_nonseq_pkey_test.rb +40 -0
  115. data/test/postgres_reserved_test.rb +22 -0
  116. data/test/postgres_schema_search_path_test.rb +46 -0
  117. data/test/postgres_simple_test.rb +13 -0
  118. data/test/simple.rb +475 -0
  119. data/test/sqlite3_simple_test.rb +233 -0
  120. data/test/sybase_jtds_simple_test.rb +6 -0
  121. metadata +188 -0
@@ -0,0 +1,293 @@
1
+ /***** BEGIN LICENSE BLOCK *****
2
+ * Copyright (c) 2006-2007 Nick Sieger <nick@nicksieger.com>
3
+ * Copyright (c) 2006-2007 Ola Bini <ola.bini@gmail.com>
4
+ *
5
+ * Permission is hereby granted, free of charge, to any person obtaining
6
+ * a copy of this software and associated documentation files (the
7
+ * "Software"), to deal in the Software without restriction, including
8
+ * without limitation the rights to use, copy, modify, merge, publish,
9
+ * distribute, sublicense, and/or sell copies of the Software, and to
10
+ * permit persons to whom the Software is furnished to do so, subject to
11
+ * the following conditions:
12
+ *
13
+ * The above copyright notice and this permission notice shall be
14
+ * included in all copies or substantial portions of the Software.
15
+ *
16
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23
+ ***** END LICENSE BLOCK *****/
24
+
25
+ package jdbc_adapter;
26
+
27
+ import org.jruby.Ruby;
28
+ import org.jruby.RubyModule;
29
+ import org.jruby.RubyString;
30
+ import org.jruby.RubyFloat;
31
+ import org.jruby.RubyFixnum;
32
+ import org.jruby.RubyBignum;
33
+ import org.jruby.RubyBoolean;
34
+ import org.jruby.RubyBigDecimal;
35
+ import org.jruby.RubyRange;
36
+ import org.jruby.RubyNumeric;
37
+
38
+ import org.jruby.runtime.builtin.IRubyObject;
39
+
40
+ import org.jruby.util.ByteList;
41
+
42
+ import java.sql.SQLException;
43
+ import org.jruby.RubyObjectAdapter;
44
+ import org.jruby.anno.JRubyMethod;
45
+ import org.jruby.runtime.ThreadContext;
46
+
47
+ public class JdbcDerbySpec {
48
+ private static RubyObjectAdapter rubyApi;
49
+ public static void load(RubyModule jdbcSpec, RubyObjectAdapter adapter) {
50
+ RubyModule derby = jdbcSpec.defineModuleUnder("Derby");
51
+ derby.defineAnnotatedMethods(JdbcDerbySpec.class);
52
+ RubyModule column = derby.defineModuleUnder("Column");
53
+ column.defineAnnotatedMethods(Column.class);
54
+ rubyApi = adapter;
55
+ }
56
+
57
+ public static class Column {
58
+ @JRubyMethod(name = "type_cast", required = 1)
59
+ public static IRubyObject type_cast(IRubyObject recv, IRubyObject value) {
60
+ Ruby runtime = recv.getRuntime();
61
+
62
+ if (value.isNil() || ((value instanceof RubyString) && value.toString().trim().equalsIgnoreCase("null"))) {
63
+ return runtime.getNil();
64
+ }
65
+
66
+ String type = rubyApi.getInstanceVariable(recv, "@type").toString();
67
+
68
+ switch (type.charAt(0)) {
69
+ case 's': //string
70
+ return value;
71
+ case 't': //text, timestamp, time
72
+ if (type.equals("text")) {
73
+ return value;
74
+ } else if (type.equals("timestamp")) {
75
+ return rubyApi.callMethod(recv.getMetaClass(), "string_to_time", value);
76
+ } else { //time
77
+ return rubyApi.callMethod(recv.getMetaClass(), "string_to_dummy_time", value);
78
+ }
79
+ case 'i': //integer
80
+ case 'p': //primary key
81
+ if (value.respondsTo("to_i")) {
82
+ return rubyApi.callMethod(value, "to_i");
83
+ } else {
84
+ return runtime.newFixnum(value.isTrue() ? 1 : 0);
85
+ }
86
+ case 'd': //decimal, datetime, date
87
+ if (type.equals("datetime")) {
88
+ return rubyApi.callMethod(recv.getMetaClass(), "string_to_time", value);
89
+ } else if (type.equals("date")) {
90
+ return rubyApi.callMethod(recv.getMetaClass(), "string_to_date", value);
91
+ } else {
92
+ return rubyApi.callMethod(recv.getMetaClass(), "value_to_decimal", value);
93
+ }
94
+ case 'f': //float
95
+ return rubyApi.callMethod(value, "to_f");
96
+ case 'b': //binary, boolean
97
+ if (type.equals("binary")) {
98
+ return rubyApi.callMethod(recv.getMetaClass(), "binary_to_string", value);
99
+ } else {
100
+ return rubyApi.callMethod(recv.getMetaClass(), "value_to_boolean", value);
101
+ }
102
+ }
103
+ return value;
104
+ }
105
+ }
106
+
107
+ @JRubyMethod(name = "quote", required = 1, optional = 1)
108
+ public static IRubyObject quote(ThreadContext context, IRubyObject recv, IRubyObject[] args) {
109
+ Ruby runtime = recv.getRuntime();
110
+ IRubyObject value = args[0];
111
+ if (args.length > 1) {
112
+ IRubyObject col = args[1];
113
+ String type = rubyApi.callMethod(col, "type").toString();
114
+ if (value instanceof RubyString) {
115
+ if (type.equals("string")) {
116
+ return quote_string_with_surround(runtime, "'", (RubyString)value, "'");
117
+ } else if (type.equals("text")) {
118
+ return quote_string_with_surround(runtime, "CAST('", (RubyString)value, "' AS CLOB)");
119
+ } else if (type.equals("binary")) {
120
+ return hexquote_string_with_surround(runtime, "CAST(X'", (RubyString)value, "' AS BLOB)");
121
+ } else {
122
+ // column type :integer or other numeric or date version
123
+ if (only_digits((RubyString)value)) {
124
+ return value;
125
+ } else {
126
+ return super_quote(context, recv, runtime, value, col);
127
+ }
128
+ }
129
+ } else if ((value instanceof RubyFloat) || (value instanceof RubyFixnum) || (value instanceof RubyBignum)) {
130
+ if (type.equals("string")) {
131
+ return quote_string_with_surround(runtime, "'", RubyString.objAsString(context, value), "'");
132
+ }
133
+ }
134
+ }
135
+ return super_quote(context, recv, runtime, value, runtime.getNil());
136
+ }
137
+
138
+ private final static ByteList NULL = new ByteList("NULL".getBytes());
139
+
140
+ private static IRubyObject super_quote(ThreadContext context, IRubyObject recv, Ruby runtime, IRubyObject value, IRubyObject col) {
141
+ if (value.respondsTo("quoted_id")) {
142
+ return rubyApi.callMethod(value, "quoted_id");
143
+ }
144
+
145
+ IRubyObject type = (col.isNil()) ? col : rubyApi.callMethod(col, "type");
146
+ RubyModule multibyteChars = (RubyModule)
147
+ ((RubyModule) ((RubyModule) runtime.getModule("ActiveSupport")).getConstant("Multibyte")).getConstantAt("Chars");
148
+ if (value instanceof RubyString || rubyApi.isKindOf(value, multibyteChars)) {
149
+ RubyString svalue = RubyString.objAsString(context, value);
150
+ if (type == runtime.newSymbol("binary") && col.getType().respondsTo("string_to_binary")) {
151
+ return quote_string_with_surround(runtime, "'", (RubyString)(rubyApi.callMethod(col.getType(), "string_to_binary", svalue)), "'");
152
+ } else if (type == runtime.newSymbol("integer") || type == runtime.newSymbol("float")) {
153
+ return RubyString.objAsString(context, ((type == runtime.newSymbol("integer")) ?
154
+ rubyApi.callMethod(svalue, "to_i") :
155
+ rubyApi.callMethod(svalue, "to_f")));
156
+ } else {
157
+ return quote_string_with_surround(runtime, "'", svalue, "'");
158
+ }
159
+ } else if (value.isNil()) {
160
+ return runtime.newString(NULL);
161
+ } else if (value instanceof RubyBoolean) {
162
+ return (value.isTrue() ?
163
+ (type == runtime.newSymbol(":integer")) ? runtime.newString("1") : rubyApi.callMethod(recv, "quoted_true") :
164
+ (type == runtime.newSymbol(":integer")) ? runtime.newString("0") : rubyApi.callMethod(recv, "quoted_false"));
165
+ } else if((value instanceof RubyFloat) || (value instanceof RubyFixnum) || (value instanceof RubyBignum)) {
166
+ return RubyString.objAsString(context, value);
167
+ } else if(value instanceof RubyBigDecimal) {
168
+ return rubyApi.callMethod(value, "to_s", runtime.newString("F"));
169
+ } else if (rubyApi.callMethod(value, "acts_like?", runtime.newString("date")).isTrue() || rubyApi.callMethod(value, "acts_like?", runtime.newString("time")).isTrue()) {
170
+ return quote_string_with_surround(runtime, "'", (RubyString)(rubyApi.callMethod(recv, "quoted_date", value)), "'");
171
+ } else {
172
+ return quote_string_with_surround(runtime, "'", (RubyString)(rubyApi.callMethod(value, "to_yaml")), "'");
173
+ }
174
+ }
175
+
176
+ private final static ByteList TWO_SINGLE = new ByteList(new byte[]{'\'','\''});
177
+
178
+ private static IRubyObject quote_string_with_surround(Ruby runtime, String before, RubyString string, String after) {
179
+ ByteList input = string.getByteList();
180
+ ByteList output = new ByteList(before.getBytes());
181
+ for(int i = input.begin; i< input.begin + input.realSize; i++) {
182
+ switch(input.bytes[i]) {
183
+ case '\'':
184
+ output.append(input.bytes[i]);
185
+ //FALLTHROUGH
186
+ default:
187
+ output.append(input.bytes[i]);
188
+ }
189
+
190
+ }
191
+
192
+ output.append(after.getBytes());
193
+
194
+ return runtime.newString(output);
195
+ }
196
+
197
+ private final static byte[] HEX = {'0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F'};
198
+
199
+ private static IRubyObject hexquote_string_with_surround(Ruby runtime, String before, RubyString string, String after) {
200
+ ByteList input = string.getByteList();
201
+ ByteList output = new ByteList(before.getBytes());
202
+ int written = 0;
203
+ for(int i = input.begin; i< input.begin + input.realSize; i++) {
204
+ byte b1 = input.bytes[i];
205
+ byte higher = HEX[(((char)b1)>>4)%16];
206
+ byte lower = HEX[((char)b1)%16];
207
+ output.append(higher);
208
+ output.append(lower);
209
+ written += 2;
210
+ if(written >= 16334) { // max hex length = 16334
211
+ output.append("'||X'".getBytes());
212
+ written = 0;
213
+ }
214
+ }
215
+
216
+ output.append(after.getBytes());
217
+ return runtime.newStringShared(output);
218
+ }
219
+
220
+ private static boolean only_digits(RubyString inp) {
221
+ ByteList input = inp.getByteList();
222
+ for(int i = input.begin; i< input.begin + input.realSize; i++) {
223
+ if(input.bytes[i] < '0' || input.bytes[i] > '9') {
224
+ return false;
225
+ }
226
+ }
227
+ return true;
228
+ }
229
+
230
+ @JRubyMethod(name = "quote_string", required = 1)
231
+ public static IRubyObject quote_string(IRubyObject recv, IRubyObject string) {
232
+ boolean replacementFound = false;
233
+ ByteList bl = ((RubyString) string).getByteList();
234
+
235
+ for(int i = bl.begin; i < bl.begin + bl.realSize; i++) {
236
+ switch (bl.bytes[i]) {
237
+ case '\'': break;
238
+ default: continue;
239
+ }
240
+
241
+ // On first replacement allocate a different bytelist so we don't manip original
242
+ if(!replacementFound) {
243
+ i-= bl.begin;
244
+ bl = new ByteList(bl);
245
+ replacementFound = true;
246
+ }
247
+
248
+ bl.replace(i, 1, TWO_SINGLE);
249
+ i+=1;
250
+ }
251
+ if(replacementFound) {
252
+ return recv.getRuntime().newStringShared(bl);
253
+ } else {
254
+ return string;
255
+ }
256
+ }
257
+
258
+ @JRubyMethod(name = "select_all", rest = true)
259
+ public static IRubyObject select_all(IRubyObject recv, IRubyObject[] args) {
260
+ return rubyApi.callMethod(recv, "execute", args);
261
+ }
262
+
263
+ @JRubyMethod(name = "select_one", rest = true)
264
+ public static IRubyObject select_one(IRubyObject recv, IRubyObject[] args) {
265
+ IRubyObject limit = rubyApi.getInstanceVariable(recv, "@limit");
266
+ if (limit == null || limit.isNil()) {
267
+ rubyApi.setInstanceVariable(recv, "@limit", recv.getRuntime().newFixnum(1));
268
+ }
269
+ try {
270
+ IRubyObject result = rubyApi.callMethod(recv, "execute", args);
271
+ return rubyApi.callMethod(result, "first");
272
+ } finally {
273
+ rubyApi.setInstanceVariable(recv, "@limit", recv.getRuntime().getNil());
274
+ }
275
+ }
276
+
277
+ @JRubyMethod(name = "_execute", required = 1, optional = 1)
278
+ public static IRubyObject _execute(ThreadContext context, IRubyObject recv, IRubyObject[] args) throws SQLException, java.io.IOException {
279
+ Ruby runtime = recv.getRuntime();
280
+ RubyJdbcConnection conn = (RubyJdbcConnection) rubyApi.getInstanceVariable(recv, "@connection");
281
+ String sql = args[0].toString().trim().toLowerCase();
282
+ if (sql.charAt(0) == '(') {
283
+ sql = sql.substring(1).trim();
284
+ }
285
+ if (sql.startsWith("insert")) {
286
+ return conn.execute_insert(context, args[0]);
287
+ } else if (sql.startsWith("select") || sql.startsWith("show") || sql.startsWith("values")) {
288
+ return conn.execute_query(context, args[0]);
289
+ } else {
290
+ return conn.execute_update(context, args[0]);
291
+ }
292
+ }
293
+ }
@@ -0,0 +1,134 @@
1
+ /***** BEGIN LICENSE BLOCK *****
2
+ * Copyright (c) 2006-2009 Nick Sieger <nick@nicksieger.com>
3
+ * Copyright (c) 2006-2007 Ola Bini <ola.bini@gmail.com>
4
+ *
5
+ * Permission is hereby granted, free of charge, to any person obtaining
6
+ * a copy of this software and associated documentation files (the
7
+ * "Software"), to deal in the Software without restriction, including
8
+ * without limitation the rights to use, copy, modify, merge, publish,
9
+ * distribute, sublicense, and/or sell copies of the Software, and to
10
+ * permit persons to whom the Software is furnished to do so, subject to
11
+ * the following conditions:
12
+ *
13
+ * The above copyright notice and this permission notice shall be
14
+ * included in all copies or substantial portions of the Software.
15
+ *
16
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23
+ ***** END LICENSE BLOCK *****/
24
+
25
+ package jdbc_adapter;
26
+
27
+ import java.sql.Connection;
28
+
29
+ import org.jruby.RubyModule;
30
+ import org.jruby.RubyString;
31
+
32
+ import org.jruby.anno.JRubyMethod;
33
+ import org.jruby.runtime.ThreadContext;
34
+ import org.jruby.runtime.builtin.IRubyObject;
35
+
36
+ import org.jruby.util.ByteList;
37
+
38
+ public class JdbcMySQLSpec {
39
+ public static void load(RubyModule jdbcSpec) {
40
+ RubyModule mysql = jdbcSpec.defineModuleUnder("MySQL");
41
+ mysql.defineAnnotatedMethods(JdbcMySQLSpec.class);
42
+ }
43
+
44
+ private final static byte BACKQUOTE = '`';
45
+ private final static byte[] QUOTED_DOT = new byte[] {'`', '.', '`'};
46
+
47
+ private final static byte[] ZERO = new byte[] {'\\','0'};
48
+ private final static byte[] NEWLINE = new byte[] {'\\','n'};
49
+ private final static byte[] CARRIAGE = new byte[] {'\\','r'};
50
+ private final static byte[] ZED = new byte[] {'\\','Z'};
51
+ private final static byte[] DBL = new byte[] {'\\','"'};
52
+ private final static byte[] SINGLE = new byte[] {'\\','\''};
53
+ private final static byte[] ESCAPE = new byte[] {'\\','\\'};
54
+
55
+ @JRubyMethod(name = "quote_string", required = 1, frame=false)
56
+ public static IRubyObject quote_string(ThreadContext context, IRubyObject recv, IRubyObject string) {
57
+ ByteList bytes = ((RubyString) string).getByteList();
58
+ ByteList newBytes = new ByteList();
59
+
60
+ newBytes.append(bytes);
61
+
62
+ for(int i = newBytes.begin; i < newBytes.begin + newBytes.realSize; i++) {
63
+ byte[] rep = null;
64
+ switch (newBytes.bytes[i]) {
65
+ case 0: rep = ZERO; break;
66
+ case '\n': rep = NEWLINE; break;
67
+ case '\r': rep = CARRIAGE; break;
68
+ case 26: rep = ZED; break;
69
+ case '"': rep = DBL; break;
70
+ case '\'': rep = SINGLE; break;
71
+ case '\\': rep = ESCAPE; break;
72
+ }
73
+
74
+ if (rep != null) {
75
+ newBytes.replace(i, 1, rep);
76
+ i += rep.length - 1; // We subtract one since for loop already adds one
77
+ }
78
+ }
79
+
80
+ // Nothing changed, can return original
81
+ if (newBytes.length() == bytes.length()) return string;
82
+
83
+ return context.getRuntime().newString(newBytes);
84
+ }
85
+
86
+ @JRubyMethod(name = "quote_column_name", frame=false)
87
+ public static IRubyObject quote_column_name(ThreadContext context, IRubyObject recv, IRubyObject arg) {
88
+ ByteList bytes = arg.asString().getByteList();
89
+ ByteList newBytes = new ByteList();
90
+
91
+ newBytes.insert(0, BACKQUOTE);
92
+ newBytes.append(bytes);
93
+ newBytes.append(BACKQUOTE);
94
+
95
+ return context.getRuntime().newString(newBytes);
96
+ }
97
+
98
+ @JRubyMethod(name = "quote_table_name", frame=false)
99
+ public static IRubyObject quote_table_name(ThreadContext context, IRubyObject recv, IRubyObject arg) {
100
+ ByteList bytes = arg.asString().getByteList();
101
+ ByteList newBytes = new ByteList();
102
+
103
+ newBytes.insert(0, BACKQUOTE);
104
+ newBytes.append(bytes);
105
+ int i = 0, j = 0;
106
+ while ((i = newBytes.indexOf('.', j)) != -1) {
107
+ newBytes.replace(i, 1, QUOTED_DOT);
108
+ j = i+3;
109
+ }
110
+ newBytes.append(BACKQUOTE);
111
+
112
+ return context.getRuntime().newString(newBytes);
113
+ }
114
+
115
+ /**
116
+ * HACK HACK HACK See http://bugs.mysql.com/bug.php?id=36565
117
+ * MySQL's statement cancel timer can cause memory leaks, so cancel it
118
+ * if we loaded MySQL classes from the same classloader as JRuby
119
+ */
120
+ @JRubyMethod(module = true, frame = false)
121
+ public static IRubyObject kill_cancel_timer(ThreadContext context, IRubyObject recv, IRubyObject raw_connection) {
122
+ Connection conn = (Connection) raw_connection.dataGetStruct();
123
+ if (conn != null && conn.getClass().getClassLoader() == recv.getRuntime().getJRubyClassLoader()) {
124
+ try {
125
+ java.lang.reflect.Field f = conn.getClass().getDeclaredField("cancelTimer");
126
+ f.setAccessible(true);
127
+ java.util.Timer timer = (java.util.Timer) f.get(null);
128
+ timer.cancel();
129
+ } catch (Exception e) {
130
+ }
131
+ }
132
+ return recv.getRuntime().getNil();
133
+ }
134
+ }
@@ -0,0 +1,71 @@
1
+ /*
2
+ **** BEGIN LICENSE BLOCK *****
3
+ * Copyright (c) 2006-2009 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 jdbc_adapter;
27
+
28
+ import java.sql.ResultSet;
29
+ import java.sql.SQLException;
30
+ import java.sql.Types;
31
+ import org.jruby.Ruby;
32
+ import org.jruby.RubyClass;
33
+ import org.jruby.runtime.ObjectAllocator;
34
+ import org.jruby.runtime.builtin.IRubyObject;
35
+
36
+ /**
37
+ *
38
+ * @author nicksieger
39
+ */
40
+ public class MssqlRubyJdbcConnection extends RubyJdbcConnection {
41
+
42
+ protected MssqlRubyJdbcConnection(Ruby runtime, RubyClass metaClass) {
43
+ super(runtime, metaClass);
44
+ }
45
+
46
+ public static RubyClass createMssqlJdbcConnectionClass(Ruby runtime, RubyClass jdbcConnection) {
47
+ RubyClass clazz = RubyJdbcConnection.getConnectionAdapters(runtime).defineClassUnder("MssqlJdbcConnection",
48
+ jdbcConnection, MSSQL_JDBCCONNECTION_ALLOCATOR);
49
+ clazz.defineAnnotatedMethods(MssqlRubyJdbcConnection.class);
50
+
51
+ return clazz;
52
+ }
53
+ private static ObjectAllocator MSSQL_JDBCCONNECTION_ALLOCATOR = new ObjectAllocator() {
54
+
55
+ public IRubyObject allocate(Ruby runtime, RubyClass klass) {
56
+ return new MssqlRubyJdbcConnection(runtime, klass);
57
+ }
58
+ };
59
+
60
+ /**
61
+ * Treat LONGVARCHAR as CLOB on Mssql for purposes of converting a JDBC value to Ruby.
62
+ */
63
+ @Override
64
+ protected IRubyObject jdbcToRuby(Ruby runtime, int column, int type, ResultSet resultSet)
65
+ throws SQLException {
66
+ if (type == Types.LONGVARCHAR) {
67
+ type = Types.CLOB;
68
+ }
69
+ return super.jdbcToRuby(runtime, column, type, resultSet);
70
+ }
71
+ }