embulk-output-oracle 0.2.4 → 0.3.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (55) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +155 -110
  3. data/build.gradle +6 -6
  4. data/classpath/embulk-output-jdbc-0.3.0.jar +0 -0
  5. data/classpath/embulk-output-oracle-0.3.0.jar +0 -0
  6. data/lib/embulk/output/oracle.rb +3 -3
  7. data/src/main/cpp/common/dir-path-load.cpp +424 -424
  8. data/src/main/cpp/common/dir-path-load.h +36 -36
  9. data/src/main/cpp/common/embulk-output-oracle.cpp +196 -196
  10. data/src/main/cpp/common/org_embulk_output_oracle_oci_OCI.h +77 -77
  11. data/src/main/cpp/linux/build.sh +21 -21
  12. data/src/main/cpp/win/build.bat +31 -31
  13. data/src/main/cpp/win/dllmain.cpp +25 -25
  14. data/src/main/cpp/win/embulk-output-oracle.sln +39 -39
  15. data/src/main/cpp/win/embulk-output-oracle.vcxproj +175 -175
  16. data/src/main/java/org/embulk/output/OracleOutputPlugin.java +22 -66
  17. data/src/main/java/org/embulk/output/oracle/DirectBatchInsert.java +289 -289
  18. data/src/main/java/org/embulk/output/oracle/InsertMethod.java +8 -8
  19. data/src/main/java/org/embulk/output/oracle/OracleCharset.java +32 -19
  20. data/src/main/java/org/embulk/output/oracle/OracleOutputConnection.java +165 -134
  21. data/src/main/java/org/embulk/output/oracle/OracleOutputConnector.java +49 -49
  22. data/src/main/java/org/embulk/output/oracle/TimestampFormat.java +37 -37
  23. data/src/main/java/org/embulk/output/oracle/oci/ColumnDefinition.java +26 -26
  24. data/src/main/java/org/embulk/output/oracle/oci/OCI.java +139 -139
  25. data/src/main/java/org/embulk/output/oracle/oci/OCIManager.java +64 -64
  26. data/src/main/java/org/embulk/output/oracle/oci/OCIWrapper.java +96 -96
  27. data/src/main/java/org/embulk/output/oracle/oci/RowBuffer.java +99 -99
  28. data/src/main/java/org/embulk/output/oracle/oci/TableDefinition.java +24 -24
  29. data/src/test/cpp/common/embulk-output-oracle-test.cpp +69 -69
  30. data/src/test/cpp/linux/build.sh +19 -19
  31. data/src/test/cpp/win/build.bat +28 -28
  32. data/src/test/cpp/win/embulk-output-oracle-test.vcxproj +154 -154
  33. data/src/test/java/org/embulk/input/filesplit/LocalFileSplitInputPlugin.java +187 -187
  34. data/src/test/java/org/embulk/input/filesplit/PartialFile.java +49 -49
  35. data/src/test/java/org/embulk/input/filesplit/PartialFileInputStream.java +154 -154
  36. data/src/test/java/org/embulk/output/oracle/ChildFirstClassLoader.java +42 -42
  37. data/src/test/java/org/embulk/output/oracle/EmbulkPluginTester.java +120 -120
  38. data/src/test/java/org/embulk/output/oracle/EmptyConfigSource.java +100 -100
  39. data/src/test/java/org/embulk/output/oracle/OracleOutputPluginTest.java +172 -161
  40. data/src/test/java/org/embulk/output/oracle/OracleOutputPluginTestImpl.java +445 -413
  41. data/src/test/java/org/embulk/output/oracle/TimestampFormatTest.java +57 -57
  42. data/src/test/resources/data/test1/test1.csv +3 -3
  43. data/src/test/resources/yml/test-insert-direct.yml +26 -26
  44. data/src/test/resources/yml/test-insert-oci-split.yml +26 -26
  45. data/src/test/resources/yml/test-insert-oci.yml +26 -26
  46. data/src/test/resources/yml/test-insert.yml +25 -25
  47. data/src/test/resources/yml/test-replace-long-name-multibyte.yml +25 -0
  48. data/src/test/resources/yml/test-replace-long-name.yml +25 -25
  49. data/src/test/resources/yml/test-replace.yml +25 -25
  50. data/src/test/resources/yml/test-string-timestamp.yml +25 -0
  51. data/src/test/resources/yml/test-url.yml +24 -24
  52. metadata +6 -5
  53. data/classpath/embulk-output-jdbc-0.2.4.jar +0 -0
  54. data/classpath/embulk-output-oracle-0.2.4.jar +0 -0
  55. data/src/main/java/org/embulk/output/oracle/setter/OracleColumnSetterFactory.java +0 -31
@@ -1,413 +1,445 @@
1
- package org.embulk.output.oracle;
2
-
3
- import static org.junit.Assert.assertEquals;
4
-
5
- import java.io.BufferedReader;
6
- import java.io.BufferedWriter;
7
- import java.io.File;
8
- import java.io.FileReader;
9
- import java.io.FileWriter;
10
- import java.io.IOException;
11
- import java.lang.reflect.Constructor;
12
- import java.math.BigDecimal;
13
- import java.net.URISyntaxException;
14
- import java.sql.Connection;
15
- import java.sql.DriverManager;
16
- import java.sql.ResultSet;
17
- import java.sql.SQLException;
18
- import java.sql.Statement;
19
- import java.sql.Timestamp;
20
- import java.text.DateFormat;
21
- import java.text.ParseException;
22
- import java.text.SimpleDateFormat;
23
- import java.util.ArrayList;
24
- import java.util.Collections;
25
- import java.util.Comparator;
26
- import java.util.Date;
27
- import java.util.Iterator;
28
- import java.util.List;
29
- import java.util.TimeZone;
30
- import java.util.regex.Matcher;
31
- import java.util.regex.Pattern;
32
-
33
- import org.embulk.input.filesplit.LocalFileSplitInputPlugin;
34
- import org.embulk.output.OracleOutputPlugin;
35
- import org.embulk.spi.InputPlugin;
36
- import org.embulk.spi.OutputPlugin;
37
-
38
-
39
- public class OracleOutputPluginTestImpl
40
- {
41
- private EmbulkPluginTester tester = new EmbulkPluginTester(OutputPlugin.class, "oracle", OracleOutputPlugin.class);
42
-
43
-
44
- public String beforeClass()
45
- {
46
- try {
47
- Class.forName("oracle.jdbc.OracleDriver");
48
-
49
- try (Connection connection = connect()) {
50
- String version = connection.getMetaData().getDriverVersion();
51
- System.out.println("Driver version = " + version);
52
- return version;
53
- }
54
-
55
- } catch (ClassNotFoundException | NoClassDefFoundError e) {
56
- //throw new RuntimeException("You should put Oracle JDBC driver on 'driver' directory.");
57
- System.err.println("Warning: put Oracle JDBC driver on 'driver' directory in order to test embulk-output-oracle plugin.");
58
-
59
- } catch (SQLException e) {
60
- System.err.println(e);
61
- //throw new RuntimeException("You should prepare a schema on Oracle (database = 'TESTDB', user = 'TEST_USER', password = 'test_pw').");
62
- System.err.println("Warning: prepare a schema on Oracle (database = 'TESTDB', user = 'TEST_USER', password = 'test_pw').");
63
- // for example
64
- // CREATE USER EMBULK_USER IDENTIFIED BY "embulk_pw";
65
- // GRANT DBA TO EMBULK_USER;
66
- }
67
-
68
- return null;
69
- }
70
-
71
-
72
- public void testInsert() throws Exception
73
- {
74
- String table = "TEST1";
75
-
76
- dropTable(table);
77
- createTable(table);
78
-
79
- run("/yml/test-insert.yml");
80
-
81
- assertTable(table);
82
- }
83
-
84
- public void testInsertCreate() throws Exception
85
- {
86
- String table = "TEST1";
87
-
88
- dropTable(table);
89
-
90
- run("/yml/test-insert.yml");
91
-
92
- assertGeneratedTable(table);
93
- }
94
-
95
- public void testInsertDirect() throws Exception
96
- {
97
- String table = "TEST1";
98
-
99
- dropTable(table);
100
- createTable(table);
101
-
102
- run("/yml/test-insert-direct.yml");
103
-
104
- assertTable(table);
105
- }
106
-
107
- public void testInsertOCI() throws Exception
108
- {
109
- String table = "TEST1";
110
-
111
- dropTable(table);
112
- createTable(table);
113
-
114
- run("/yml/test-insert-oci.yml");
115
-
116
- assertTable(table);
117
- }
118
-
119
- public void testInsertOCISplit() throws Exception
120
- {
121
- tester.addPlugin(InputPlugin.class, "filesplit", LocalFileSplitInputPlugin.class);
122
-
123
- String table = "TEST1";
124
-
125
- dropTable(table);
126
- createTable(table);
127
-
128
- run("/yml/test-insert-oci-split.yml");
129
-
130
- assertTable(table);
131
- }
132
-
133
- public void testUrl() throws Exception
134
- {
135
- String table = "TEST1";
136
-
137
- dropTable(table);
138
- createTable(table);
139
-
140
- run("/yml/test-url.yml");
141
-
142
- assertTable(table);
143
- }
144
-
145
- public void testReplace() throws Exception
146
- {
147
- String table = "TEST1";
148
-
149
- dropTable(table);
150
- createTable(table);
151
-
152
- run("/yml/test-replace.yml");
153
-
154
- assertGeneratedTable(table);
155
- }
156
-
157
- public void testReplaceCreate() throws Exception
158
- {
159
- String table = "TEST1";
160
-
161
- dropTable(table);
162
-
163
- run("/yml/test-replace.yml");
164
-
165
- assertGeneratedTable(table);
166
- }
167
-
168
-
169
- public void testReplaceLongName() throws Exception
170
- {
171
- String table = "TEST12345678901234567890123456";
172
-
173
- dropTable(table);
174
- createTable(table);
175
-
176
- run("/yml/test-replace-long-name.yml");
177
-
178
- assertGeneratedTable(table);
179
- }
180
-
181
- private void dropTable(String table) throws SQLException
182
- {
183
- String sql = String.format("DROP TABLE %s", table);
184
- executeSQL(sql, true);
185
- }
186
-
187
- private void createTable(String table) throws SQLException
188
- {
189
- String sql = String.format("CREATE TABLE %s ("
190
- + "ID CHAR(4),"
191
- + "VARCHAR2_ITEM VARCHAR2(40),"
192
- + "INTEGER_ITEM NUMBER(4,0),"
193
- + "NUMBER_ITEM NUMBER(10,2),"
194
- + "DATE_ITEM DATE,"
195
- + "TIMESTAMP_ITEM TIMESTAMP,"
196
- + "PRIMARY KEY (ID))", table);
197
- executeSQL(sql);
198
- }
199
-
200
- private void assertTable(String table) throws Exception
201
- {
202
- List<List<Object>> rows = select(table);
203
-
204
- /*
205
- A001,ABCDE,0,123.45,2015/03/05,2015/03/05 12:34:56
206
- A002,あいうえお,-9999,-99999999.99,2015/03/06,2015/03/06 23:59:59
207
- A003,,,,,
208
- */
209
-
210
- assertEquals(3, rows.size());
211
- Iterator<List<Object>> i1 = rows.iterator();
212
- {
213
- Iterator<Object> i2 = i1.next().iterator();
214
- assertEquals("A001", i2.next());
215
- assertEquals("ABCDE", i2.next());
216
- assertEquals(new BigDecimal("0"), i2.next());
217
- assertEquals(new BigDecimal("123.45"), i2.next());
218
- assertEquals(toTimestamp("2015/03/05 00:00:00"), i2.next());
219
- assertEquals(toOracleTimestamp("2015/03/05 12:34:56"), i2.next());
220
- }
221
- {
222
- Iterator<Object> i2 = i1.next().iterator();
223
- assertEquals("A002", i2.next());
224
- assertEquals("あいうえお", i2.next());
225
- assertEquals(new BigDecimal("-9999"), i2.next());
226
- assertEquals(new BigDecimal("-99999999.99"), i2.next());
227
- assertEquals(toTimestamp("2015/03/06 00:00:00"), i2.next());
228
- assertEquals(toOracleTimestamp("2015/03/06 23:59:59"), i2.next());
229
- }
230
- {
231
- Iterator<Object> i2 = i1.next().iterator();
232
- assertEquals("A003", i2.next());
233
- assertEquals(null, i2.next());
234
- assertEquals(null, i2.next());
235
- assertEquals(null, i2.next());
236
- assertEquals(null, i2.next());
237
- assertEquals(null, i2.next());
238
- }
239
- }
240
-
241
- private void assertGeneratedTable(String table) throws Exception
242
- {
243
- List<List<Object>> rows = select(table);
244
-
245
- /*
246
- A001,ABCDE,0,123.45,2015/03/05,2015/03/05 12:34:56
247
- A002,あいうえお,-9999,-99999999.99,2015/03/06,2015/03/06 23:59:59
248
- A003,,,,,
249
- */
250
-
251
- assertEquals(3, rows.size());
252
- Iterator<List<Object>> i1 = rows.iterator();
253
- {
254
- Iterator<Object> i2 = i1.next().iterator();
255
- assertEquals("A001", i2.next());
256
- assertEquals("ABCDE", i2.next());
257
- assertEquals(new BigDecimal("0"), i2.next());
258
- assertEquals("123.45", i2.next());
259
- assertEquals(toOracleTimestamp("2015/03/05 00:00:00"), i2.next());
260
- assertEquals(toOracleTimestamp("2015/03/05 12:34:56"), i2.next());
261
- }
262
- {
263
- Iterator<Object> i2 = i1.next().iterator();
264
- assertEquals("A002", i2.next());
265
- assertEquals("あいうえお", i2.next());
266
- assertEquals(new BigDecimal("-9999"), i2.next());
267
- assertEquals("-99999999.99", i2.next());
268
- assertEquals(toOracleTimestamp("2015/03/06 00:00:00"), i2.next());
269
- assertEquals(toOracleTimestamp("2015/03/06 23:59:59"), i2.next());
270
- }
271
- {
272
- Iterator<Object> i2 = i1.next().iterator();
273
- assertEquals("A003", i2.next());
274
- assertEquals(null, i2.next());
275
- assertEquals(null, i2.next());
276
- assertEquals(null, i2.next());
277
- assertEquals(null, i2.next());
278
- assertEquals(null, i2.next());
279
- }
280
- }
281
-
282
-
283
- private Timestamp toTimestamp(String s)
284
- {
285
- for (String formatString : new String[]{"yyyy/MM/dd HH:mm:ss", "yyyy/MM/dd"}) {
286
- DateFormat dateFormat = new SimpleDateFormat(formatString);
287
- dateFormat.setTimeZone(TimeZone.getTimeZone("GMT"));
288
- try {
289
- Date date = dateFormat.parse(s);
290
- return new Timestamp(date.getTime());
291
- } catch (ParseException e) {
292
- // NOP
293
- }
294
- }
295
- throw new IllegalArgumentException(s);
296
- }
297
-
298
- private Object toOracleTimestamp(String s) throws Exception
299
- {
300
- Class<?> timestampClass = Class.forName("oracle.sql.TIMESTAMP");
301
- Constructor<?> constructor = timestampClass.getConstructor(Timestamp.class);
302
- return constructor.newInstance(toTimestamp(s));
303
- }
304
-
305
-
306
- private List<List<Object>> select(String table) throws SQLException
307
- {
308
- try (Connection connection = connect()) {
309
- try (Statement statement = connection.createStatement()) {
310
- List<List<Object>> rows = new ArrayList<List<Object>>();
311
- String sql = "SELECT * FROM " + table;
312
- System.out.println(sql);
313
- try (ResultSet resultSet = statement.executeQuery(sql)) {
314
- while (resultSet.next()) {
315
- List<Object> row = new ArrayList<Object>();
316
- for (int i = 1; i <= resultSet.getMetaData().getColumnCount(); i++) {
317
- Object value = resultSet.getObject(i);
318
- if (value != null && value.getClass().getName().equals("oracle.sql.CLOB")) {
319
- value = resultSet.getString(i);
320
- }
321
- row.add(value);
322
- }
323
- rows.add(row);
324
- }
325
- }
326
- // cannot sort by CLOB, so sort by Java
327
- Collections.sort(rows, new Comparator<List<Object>>() {
328
- @Override
329
- public int compare(List<Object> o1, List<Object> o2) {
330
- return o1.toString().compareTo(o2.toString());
331
- }
332
- });
333
- return rows;
334
- }
335
- }
336
-
337
- }
338
-
339
-
340
- private void executeSQL(String sql) throws SQLException
341
- {
342
- executeSQL(sql, false);
343
- }
344
-
345
- private void executeSQL(String sql, boolean ignoreError) throws SQLException
346
- {
347
- try (Connection connection = connect()) {
348
- try {
349
- connection.setAutoCommit(true);
350
-
351
- try (Statement statement = connection.createStatement()) {
352
- System.out.println(String.format("Execute SQL : \"%s\".", sql));
353
- statement.execute(sql);
354
- }
355
-
356
- } catch (SQLException e) {
357
- if (!ignoreError) {
358
- throw e;
359
- }
360
- }
361
- }
362
- }
363
-
364
- private static Connection connect() throws SQLException
365
- {
366
- return DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:TESTDB", "TEST_USER", "test_pw");
367
- }
368
-
369
- private void run(String ymlName) throws Exception
370
- {
371
- tester.run(convertYml(ymlName));
372
- }
373
-
374
- private String convertYml(String ymlName)
375
- {
376
- try {
377
- File ymlPath = convertPath(ymlName);
378
- File tempYmlPath = new File(ymlPath.getParentFile(), "temp-" + ymlPath.getName());
379
- Pattern pathPrefixPattern = Pattern.compile("^ *path(_prefix)?: '(.*)'$");
380
- try (BufferedReader reader = new BufferedReader(new FileReader(ymlPath))) {
381
- try (BufferedWriter writer = new BufferedWriter(new FileWriter(tempYmlPath))) {
382
- String line;
383
- while ((line = reader.readLine()) != null) {
384
- Matcher matcher = pathPrefixPattern.matcher(line);
385
- if (matcher.matches()) {
386
- int group = 2;
387
- writer.write(line.substring(0, matcher.start(group)));
388
- writer.write(convertPath(matcher.group(group)).getAbsolutePath());
389
- writer.write(line.substring(matcher.end(group)));
390
- } else {
391
- writer.write(line);
392
- }
393
- writer.newLine();
394
- }
395
- }
396
- }
397
- return tempYmlPath.getAbsolutePath();
398
-
399
- } catch (IOException e) {
400
- throw new RuntimeException(e);
401
- } catch (URISyntaxException e) {
402
- throw new RuntimeException(e);
403
- }
404
- }
405
-
406
- private File convertPath(String name) throws URISyntaxException
407
- {
408
- if (getClass().getResource(name) == null)
409
- return new File(name);
410
- return new File(getClass().getResource(name).toURI());
411
- }
412
-
413
- }
1
+ package org.embulk.output.oracle;
2
+
3
+ import static org.junit.Assert.assertEquals;
4
+
5
+ import java.io.BufferedReader;
6
+ import java.io.BufferedWriter;
7
+ import java.io.File;
8
+ import java.io.FileReader;
9
+ import java.io.FileWriter;
10
+ import java.io.IOException;
11
+ import java.lang.reflect.Constructor;
12
+ import java.math.BigDecimal;
13
+ import java.net.URISyntaxException;
14
+ import java.sql.Connection;
15
+ import java.sql.DriverManager;
16
+ import java.sql.ResultSet;
17
+ import java.sql.SQLException;
18
+ import java.sql.Statement;
19
+ import java.sql.Timestamp;
20
+ import java.text.DateFormat;
21
+ import java.text.ParseException;
22
+ import java.text.SimpleDateFormat;
23
+ import java.util.ArrayList;
24
+ import java.util.Collections;
25
+ import java.util.Comparator;
26
+ import java.util.Date;
27
+ import java.util.Iterator;
28
+ import java.util.List;
29
+ import java.util.TimeZone;
30
+ import java.util.regex.Matcher;
31
+ import java.util.regex.Pattern;
32
+
33
+ import org.embulk.input.filesplit.LocalFileSplitInputPlugin;
34
+ import org.embulk.output.OracleOutputPlugin;
35
+ import org.embulk.spi.InputPlugin;
36
+ import org.embulk.spi.OutputPlugin;
37
+
38
+
39
+ public class OracleOutputPluginTestImpl
40
+ {
41
+ private EmbulkPluginTester tester = new EmbulkPluginTester(OutputPlugin.class, "oracle", OracleOutputPlugin.class);
42
+
43
+
44
+ public String beforeClass()
45
+ {
46
+ try {
47
+ Class.forName("oracle.jdbc.OracleDriver");
48
+
49
+ try (Connection connection = connect()) {
50
+ String version = connection.getMetaData().getDriverVersion();
51
+ System.out.println("Driver version = " + version);
52
+ return version;
53
+ }
54
+
55
+ } catch (ClassNotFoundException | NoClassDefFoundError e) {
56
+ //throw new RuntimeException("You should put Oracle JDBC driver on 'driver' directory.");
57
+ System.err.println("Warning: put Oracle JDBC driver on 'driver' directory in order to test embulk-output-oracle plugin.");
58
+
59
+ } catch (SQLException e) {
60
+ System.err.println(e);
61
+ //throw new RuntimeException("You should prepare a schema on Oracle (database = 'TESTDB', user = 'TEST_USER', password = 'test_pw').");
62
+ System.err.println("Warning: prepare a schema on Oracle (database = 'TESTDB', user = 'TEST_USER', password = 'test_pw').");
63
+ // for example
64
+ // CREATE USER EMBULK_USER IDENTIFIED BY "embulk_pw";
65
+ // GRANT DBA TO EMBULK_USER;
66
+ }
67
+
68
+ return null;
69
+ }
70
+
71
+
72
+ public void testInsert() throws Exception
73
+ {
74
+ String table = "TEST1";
75
+
76
+ dropTable(table);
77
+ createTable(table);
78
+
79
+ run("/yml/test-insert.yml");
80
+
81
+ assertTable(table);
82
+ }
83
+
84
+ public void testInsertCreate() throws Exception
85
+ {
86
+ String table = "TEST1";
87
+
88
+ dropTable(table);
89
+
90
+ run("/yml/test-insert.yml");
91
+
92
+ assertGeneratedTable(table);
93
+ }
94
+
95
+ public void testInsertDirect() throws Exception
96
+ {
97
+ String table = "TEST1";
98
+
99
+ dropTable(table);
100
+ createTable(table);
101
+
102
+ run("/yml/test-insert-direct.yml");
103
+
104
+ assertTable(table);
105
+ }
106
+
107
+ public void testInsertOCI() throws Exception
108
+ {
109
+ String table = "TEST1";
110
+
111
+ dropTable(table);
112
+ createTable(table);
113
+
114
+ run("/yml/test-insert-oci.yml");
115
+
116
+ assertTable(table);
117
+ }
118
+
119
+ public void testInsertOCISplit() throws Exception
120
+ {
121
+ tester.addPlugin(InputPlugin.class, "filesplit", LocalFileSplitInputPlugin.class);
122
+
123
+ String table = "TEST1";
124
+
125
+ dropTable(table);
126
+ createTable(table);
127
+
128
+ run("/yml/test-insert-oci-split.yml");
129
+
130
+ assertTable(table);
131
+ }
132
+
133
+ public void testUrl() throws Exception
134
+ {
135
+ String table = "TEST1";
136
+
137
+ dropTable(table);
138
+ createTable(table);
139
+
140
+ run("/yml/test-url.yml");
141
+
142
+ assertTable(table);
143
+ }
144
+
145
+ public void testReplace() throws Exception
146
+ {
147
+ String table = "TEST1";
148
+
149
+ dropTable(table);
150
+ createTable(table);
151
+
152
+ run("/yml/test-replace.yml");
153
+
154
+ assertGeneratedTable(table);
155
+ }
156
+
157
+ public void testReplaceCreate() throws Exception
158
+ {
159
+ String table = "TEST1";
160
+
161
+ dropTable(table);
162
+
163
+ run("/yml/test-replace.yml");
164
+
165
+ assertGeneratedTable(table);
166
+ }
167
+
168
+
169
+ public void testReplaceLongName() throws Exception
170
+ {
171
+ String table = "TEST12345678901234567890123456";
172
+
173
+ dropTable(table);
174
+ createTable(table);
175
+
176
+ run("/yml/test-replace-long-name.yml");
177
+
178
+ assertGeneratedTable(table);
179
+ }
180
+
181
+ public void testReplaceLongNameMultibyte() throws Exception
182
+ {
183
+ String table = "TEST12345678901234567890";
184
+
185
+ run("/yml/test-replace-long-name-multibyte.yml");
186
+
187
+ assertGeneratedTable(table);
188
+ }
189
+
190
+ public void testStringTimestamp() throws Exception
191
+ {
192
+ String table = "TEST1";
193
+
194
+ dropTable(table);
195
+ createTable(table);
196
+
197
+ run("/yml/test-string-timestamp.yml");
198
+
199
+ assertTable(table, TimeZone.getDefault());
200
+ }
201
+
202
+ private void dropTable(String table) throws SQLException
203
+ {
204
+ String sql = String.format("DROP TABLE %s", table);
205
+ executeSQL(sql, true);
206
+ }
207
+
208
+ private void createTable(String table) throws SQLException
209
+ {
210
+ String sql = String.format("CREATE TABLE %s ("
211
+ + "ID CHAR(4),"
212
+ + "VARCHAR2_ITEM VARCHAR2(40),"
213
+ + "INTEGER_ITEM NUMBER(4,0),"
214
+ + "NUMBER_ITEM NUMBER(10,2),"
215
+ + "DATE_ITEM DATE,"
216
+ + "TIMESTAMP_ITEM TIMESTAMP,"
217
+ + "PRIMARY KEY (ID))", table);
218
+ executeSQL(sql);
219
+ }
220
+
221
+ private void assertTable(String table) throws Exception
222
+ {
223
+ assertTable(table, TimeZone.getTimeZone("GMT"));
224
+ }
225
+
226
+
227
+ private void assertTable(String table, TimeZone timeZone) throws Exception
228
+ {
229
+ List<List<Object>> rows = select(table);
230
+
231
+ /*
232
+ A001,ABCDE,0,123.45,2015/03/05,2015/03/05 12:34:56
233
+ A002,あいうえお,-9999,-99999999.99,2015/03/06,2015/03/06 23:59:59
234
+ A003,,,,,
235
+ */
236
+
237
+ assertEquals(3, rows.size());
238
+ Iterator<List<Object>> i1 = rows.iterator();
239
+ {
240
+ Iterator<Object> i2 = i1.next().iterator();
241
+ assertEquals("A001", i2.next());
242
+ assertEquals("ABCDE", i2.next());
243
+ assertEquals(new BigDecimal("0"), i2.next());
244
+ assertEquals(new BigDecimal("123.45"), i2.next());
245
+ assertEquals(toTimestamp("2015/03/05 00:00:00", timeZone), i2.next());
246
+ assertEquals(toOracleTimestamp("2015/03/05 12:34:56", timeZone), i2.next());
247
+ }
248
+ {
249
+ Iterator<Object> i2 = i1.next().iterator();
250
+ assertEquals("A002", i2.next());
251
+ assertEquals("あいうえお", i2.next());
252
+ assertEquals(new BigDecimal("-9999"), i2.next());
253
+ assertEquals(new BigDecimal("-99999999.99"), i2.next());
254
+ assertEquals(toTimestamp("2015/03/06 00:00:00", timeZone), i2.next());
255
+ assertEquals(toOracleTimestamp("2015/03/06 23:59:59", timeZone), i2.next());
256
+ }
257
+ {
258
+ Iterator<Object> i2 = i1.next().iterator();
259
+ assertEquals("A003", i2.next());
260
+ assertEquals(null, i2.next());
261
+ assertEquals(null, i2.next());
262
+ assertEquals(null, i2.next());
263
+ assertEquals(null, i2.next());
264
+ assertEquals(null, i2.next());
265
+ }
266
+ }
267
+
268
+ private void assertGeneratedTable(String table) throws Exception
269
+ {
270
+ assertGeneratedTable(table, TimeZone.getTimeZone("GMT"));
271
+ }
272
+
273
+ private void assertGeneratedTable(String table, TimeZone timeZone) throws Exception
274
+ {
275
+ List<List<Object>> rows = select(table);
276
+
277
+ /*
278
+ A001,ABCDE,0,123.45,2015/03/05,2015/03/05 12:34:56
279
+ A002,あいうえお,-9999,-99999999.99,2015/03/06,2015/03/06 23:59:59
280
+ A003,,,,,
281
+ */
282
+
283
+ assertEquals(3, rows.size());
284
+ Iterator<List<Object>> i1 = rows.iterator();
285
+ {
286
+ Iterator<Object> i2 = i1.next().iterator();
287
+ assertEquals("A001", i2.next());
288
+ assertEquals("ABCDE", i2.next());
289
+ assertEquals(new BigDecimal("0"), i2.next());
290
+ assertEquals("123.45", i2.next());
291
+ assertEquals(toOracleTimestamp("2015/03/05 00:00:00", timeZone), i2.next());
292
+ assertEquals(toOracleTimestamp("2015/03/05 12:34:56", timeZone), i2.next());
293
+ }
294
+ {
295
+ Iterator<Object> i2 = i1.next().iterator();
296
+ assertEquals("A002", i2.next());
297
+ assertEquals("あいうえお", i2.next());
298
+ assertEquals(new BigDecimal("-9999"), i2.next());
299
+ assertEquals("-99999999.99", i2.next());
300
+ assertEquals(toOracleTimestamp("2015/03/06 00:00:00", timeZone), i2.next());
301
+ assertEquals(toOracleTimestamp("2015/03/06 23:59:59", timeZone), i2.next());
302
+ }
303
+ {
304
+ Iterator<Object> i2 = i1.next().iterator();
305
+ assertEquals("A003", i2.next());
306
+ assertEquals(null, i2.next());
307
+ assertEquals(null, i2.next());
308
+ assertEquals(null, i2.next());
309
+ assertEquals(null, i2.next());
310
+ assertEquals(null, i2.next());
311
+ }
312
+ }
313
+
314
+
315
+ private Timestamp toTimestamp(String s, TimeZone timeZone)
316
+ {
317
+ for (String formatString : new String[]{"yyyy/MM/dd HH:mm:ss", "yyyy/MM/dd"}) {
318
+ DateFormat dateFormat = new SimpleDateFormat(formatString);
319
+ dateFormat.setTimeZone(timeZone);
320
+ try {
321
+ Date date = dateFormat.parse(s);
322
+ return new Timestamp(date.getTime());
323
+ } catch (ParseException e) {
324
+ // NOP
325
+ }
326
+ }
327
+ throw new IllegalArgumentException(s);
328
+ }
329
+
330
+ private Object toOracleTimestamp(String s, TimeZone timeZone) throws Exception
331
+ {
332
+ Class<?> timestampClass = Class.forName("oracle.sql.TIMESTAMP");
333
+ Constructor<?> constructor = timestampClass.getConstructor(Timestamp.class);
334
+ return constructor.newInstance(toTimestamp(s, timeZone));
335
+ }
336
+
337
+
338
+ private List<List<Object>> select(String table) throws SQLException
339
+ {
340
+ try (Connection connection = connect()) {
341
+ try (Statement statement = connection.createStatement()) {
342
+ List<List<Object>> rows = new ArrayList<List<Object>>();
343
+ String sql = "SELECT * FROM " + table;
344
+ System.out.println(sql);
345
+ try (ResultSet resultSet = statement.executeQuery(sql)) {
346
+ while (resultSet.next()) {
347
+ List<Object> row = new ArrayList<Object>();
348
+ for (int i = 1; i <= resultSet.getMetaData().getColumnCount(); i++) {
349
+ Object value = resultSet.getObject(i);
350
+ if (value != null && value.getClass().getName().equals("oracle.sql.CLOB")) {
351
+ value = resultSet.getString(i);
352
+ }
353
+ row.add(value);
354
+ }
355
+ rows.add(row);
356
+ }
357
+ }
358
+ // cannot sort by CLOB, so sort by Java
359
+ Collections.sort(rows, new Comparator<List<Object>>() {
360
+ @Override
361
+ public int compare(List<Object> o1, List<Object> o2) {
362
+ return o1.toString().compareTo(o2.toString());
363
+ }
364
+ });
365
+ return rows;
366
+ }
367
+ }
368
+
369
+ }
370
+
371
+
372
+ private void executeSQL(String sql) throws SQLException
373
+ {
374
+ executeSQL(sql, false);
375
+ }
376
+
377
+ private void executeSQL(String sql, boolean ignoreError) throws SQLException
378
+ {
379
+ try (Connection connection = connect()) {
380
+ try {
381
+ connection.setAutoCommit(true);
382
+
383
+ try (Statement statement = connection.createStatement()) {
384
+ System.out.println(String.format("Execute SQL : \"%s\".", sql));
385
+ statement.execute(sql);
386
+ }
387
+
388
+ } catch (SQLException e) {
389
+ if (!ignoreError) {
390
+ throw e;
391
+ }
392
+ }
393
+ }
394
+ }
395
+
396
+ private static Connection connect() throws SQLException
397
+ {
398
+ return DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:TESTDB", "TEST_USER", "test_pw");
399
+ }
400
+
401
+ private void run(String ymlName) throws Exception
402
+ {
403
+ tester.run(convertYml(ymlName));
404
+ }
405
+
406
+ private String convertYml(String ymlName)
407
+ {
408
+ try {
409
+ File ymlPath = convertPath(ymlName);
410
+ File tempYmlPath = new File(ymlPath.getParentFile(), "temp-" + ymlPath.getName());
411
+ Pattern pathPrefixPattern = Pattern.compile("^ *path(_prefix)?: '(.*)'$");
412
+ try (BufferedReader reader = new BufferedReader(new FileReader(ymlPath))) {
413
+ try (BufferedWriter writer = new BufferedWriter(new FileWriter(tempYmlPath))) {
414
+ String line;
415
+ while ((line = reader.readLine()) != null) {
416
+ Matcher matcher = pathPrefixPattern.matcher(line);
417
+ if (matcher.matches()) {
418
+ int group = 2;
419
+ writer.write(line.substring(0, matcher.start(group)));
420
+ writer.write(convertPath(matcher.group(group)).getAbsolutePath());
421
+ writer.write(line.substring(matcher.end(group)));
422
+ } else {
423
+ writer.write(line);
424
+ }
425
+ writer.newLine();
426
+ }
427
+ }
428
+ }
429
+ return tempYmlPath.getAbsolutePath();
430
+
431
+ } catch (IOException e) {
432
+ throw new RuntimeException(e);
433
+ } catch (URISyntaxException e) {
434
+ throw new RuntimeException(e);
435
+ }
436
+ }
437
+
438
+ private File convertPath(String name) throws URISyntaxException
439
+ {
440
+ if (getClass().getResource(name) == null)
441
+ return new File(name);
442
+ return new File(getClass().getResource(name).toURI());
443
+ }
444
+
445
+ }