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.
- checksums.yaml +4 -4
- data/README.md +155 -110
- data/build.gradle +6 -6
- data/classpath/embulk-output-jdbc-0.3.0.jar +0 -0
- data/classpath/embulk-output-oracle-0.3.0.jar +0 -0
- data/lib/embulk/output/oracle.rb +3 -3
- data/src/main/cpp/common/dir-path-load.cpp +424 -424
- data/src/main/cpp/common/dir-path-load.h +36 -36
- data/src/main/cpp/common/embulk-output-oracle.cpp +196 -196
- data/src/main/cpp/common/org_embulk_output_oracle_oci_OCI.h +77 -77
- data/src/main/cpp/linux/build.sh +21 -21
- data/src/main/cpp/win/build.bat +31 -31
- data/src/main/cpp/win/dllmain.cpp +25 -25
- data/src/main/cpp/win/embulk-output-oracle.sln +39 -39
- data/src/main/cpp/win/embulk-output-oracle.vcxproj +175 -175
- data/src/main/java/org/embulk/output/OracleOutputPlugin.java +22 -66
- data/src/main/java/org/embulk/output/oracle/DirectBatchInsert.java +289 -289
- data/src/main/java/org/embulk/output/oracle/InsertMethod.java +8 -8
- data/src/main/java/org/embulk/output/oracle/OracleCharset.java +32 -19
- data/src/main/java/org/embulk/output/oracle/OracleOutputConnection.java +165 -134
- data/src/main/java/org/embulk/output/oracle/OracleOutputConnector.java +49 -49
- data/src/main/java/org/embulk/output/oracle/TimestampFormat.java +37 -37
- data/src/main/java/org/embulk/output/oracle/oci/ColumnDefinition.java +26 -26
- data/src/main/java/org/embulk/output/oracle/oci/OCI.java +139 -139
- data/src/main/java/org/embulk/output/oracle/oci/OCIManager.java +64 -64
- data/src/main/java/org/embulk/output/oracle/oci/OCIWrapper.java +96 -96
- data/src/main/java/org/embulk/output/oracle/oci/RowBuffer.java +99 -99
- data/src/main/java/org/embulk/output/oracle/oci/TableDefinition.java +24 -24
- data/src/test/cpp/common/embulk-output-oracle-test.cpp +69 -69
- data/src/test/cpp/linux/build.sh +19 -19
- data/src/test/cpp/win/build.bat +28 -28
- data/src/test/cpp/win/embulk-output-oracle-test.vcxproj +154 -154
- data/src/test/java/org/embulk/input/filesplit/LocalFileSplitInputPlugin.java +187 -187
- data/src/test/java/org/embulk/input/filesplit/PartialFile.java +49 -49
- data/src/test/java/org/embulk/input/filesplit/PartialFileInputStream.java +154 -154
- data/src/test/java/org/embulk/output/oracle/ChildFirstClassLoader.java +42 -42
- data/src/test/java/org/embulk/output/oracle/EmbulkPluginTester.java +120 -120
- data/src/test/java/org/embulk/output/oracle/EmptyConfigSource.java +100 -100
- data/src/test/java/org/embulk/output/oracle/OracleOutputPluginTest.java +172 -161
- data/src/test/java/org/embulk/output/oracle/OracleOutputPluginTestImpl.java +445 -413
- data/src/test/java/org/embulk/output/oracle/TimestampFormatTest.java +57 -57
- data/src/test/resources/data/test1/test1.csv +3 -3
- data/src/test/resources/yml/test-insert-direct.yml +26 -26
- data/src/test/resources/yml/test-insert-oci-split.yml +26 -26
- data/src/test/resources/yml/test-insert-oci.yml +26 -26
- data/src/test/resources/yml/test-insert.yml +25 -25
- data/src/test/resources/yml/test-replace-long-name-multibyte.yml +25 -0
- data/src/test/resources/yml/test-replace-long-name.yml +25 -25
- data/src/test/resources/yml/test-replace.yml +25 -25
- data/src/test/resources/yml/test-string-timestamp.yml +25 -0
- data/src/test/resources/yml/test-url.yml +24 -24
- metadata +6 -5
- data/classpath/embulk-output-jdbc-0.2.4.jar +0 -0
- data/classpath/embulk-output-oracle-0.2.4.jar +0 -0
- data/src/main/java/org/embulk/output/oracle/setter/OracleColumnSetterFactory.java +0 -31
@@ -1,139 +1,139 @@
|
|
1
|
-
package org.embulk.output.oracle.oci;
|
2
|
-
|
3
|
-
import java.io.File;
|
4
|
-
import java.io.IOException;
|
5
|
-
import java.net.MalformedURLException;
|
6
|
-
import java.net.URISyntaxException;
|
7
|
-
import java.net.URL;
|
8
|
-
|
9
|
-
import org.embulk.spi.Exec;
|
10
|
-
import org.slf4j.Logger;
|
11
|
-
|
12
|
-
import com.google.common.io.Files;
|
13
|
-
|
14
|
-
|
15
|
-
public class OCI
|
16
|
-
{
|
17
|
-
private static final Logger logger = Exec.getLogger(OCI.class);
|
18
|
-
|
19
|
-
private static final String PLUGIN_NAME = "embulk-output-oracle";
|
20
|
-
|
21
|
-
static {
|
22
|
-
try {
|
23
|
-
loadLibrary();
|
24
|
-
} catch (URISyntaxException | IOException e) {
|
25
|
-
throw new RuntimeException(e);
|
26
|
-
}
|
27
|
-
}
|
28
|
-
|
29
|
-
public native byte[] createContext();
|
30
|
-
|
31
|
-
public native byte[] getLasetMessage(byte[] context);
|
32
|
-
|
33
|
-
public native boolean open(byte[] context, String dbName, String userName, String password);
|
34
|
-
|
35
|
-
public native boolean prepareLoad(byte[] context, TableDefinition tableDefinition);
|
36
|
-
|
37
|
-
public native boolean loadBuffer(byte[] context, byte[] buffer, int rowCount);
|
38
|
-
|
39
|
-
public native boolean commit(byte[] context);
|
40
|
-
|
41
|
-
public native boolean rollback(byte[] context);
|
42
|
-
|
43
|
-
public native void close(byte[] context);
|
44
|
-
|
45
|
-
private static void loadLibrary() throws URISyntaxException, IOException
|
46
|
-
{
|
47
|
-
loadLibrary(getPluginRoot());
|
48
|
-
}
|
49
|
-
|
50
|
-
private static File getPluginRoot() throws MalformedURLException, URISyntaxException
|
51
|
-
{
|
52
|
-
URL url = OCI.class.getResource("/" + OCI.class.getName().replace('.', '/') + ".class");
|
53
|
-
if (url.toString().startsWith("jar:")) {
|
54
|
-
url = new URL(url.toString().replaceAll("^jar:", "").replaceAll("![^!]*$", ""));
|
55
|
-
}
|
56
|
-
|
57
|
-
File folder = new File(url.toURI()).getParentFile();
|
58
|
-
for (;; folder = folder.getParentFile()) {
|
59
|
-
if (folder == null) {
|
60
|
-
String message = String.format("OCI : %s folder not found.", PLUGIN_NAME);
|
61
|
-
throw new RuntimeException(message);
|
62
|
-
}
|
63
|
-
|
64
|
-
if (folder.getName().startsWith(PLUGIN_NAME)) {
|
65
|
-
return folder;
|
66
|
-
}
|
67
|
-
}
|
68
|
-
}
|
69
|
-
|
70
|
-
private static void loadLibrary(File folder) throws IOException
|
71
|
-
{
|
72
|
-
File lib = new File(new File(folder, "lib"), "embulk");
|
73
|
-
|
74
|
-
String osName = System.getProperty("os.name");
|
75
|
-
String osArch = System.getProperty("os.arch");
|
76
|
-
|
77
|
-
String libraryName = System.mapLibraryName(PLUGIN_NAME);
|
78
|
-
File libFolder = null;
|
79
|
-
|
80
|
-
if (osName.startsWith("Windows")) {
|
81
|
-
if (osArch.endsWith("64")) {
|
82
|
-
libFolder = new File(lib, "win_x64");
|
83
|
-
} else if (osArch.equals("x86")) {
|
84
|
-
libFolder = new File(lib, "win_x86");
|
85
|
-
}
|
86
|
-
} else if (osName.equals("Linux")) {
|
87
|
-
if (osArch.endsWith("64")) {
|
88
|
-
libFolder = new File(lib, "linux_x64");
|
89
|
-
} else if (osArch.equals("x86")) {
|
90
|
-
libFolder = new File(lib, "linux_x86");
|
91
|
-
}
|
92
|
-
}
|
93
|
-
|
94
|
-
if (libFolder == null) {
|
95
|
-
logger.error(String.format("OCI : library \"%s\" for %s %s doesn't exist in lib folder.", libraryName, osName, osArch));
|
96
|
-
return;
|
97
|
-
}
|
98
|
-
|
99
|
-
File libFile = new File(libFolder, libraryName);
|
100
|
-
if (!libFile.exists()) {
|
101
|
-
logger.error(String.format("OCI : library \"%s\" doesn't exist.", libFile.getAbsolutePath()));
|
102
|
-
return;
|
103
|
-
}
|
104
|
-
|
105
|
-
logger.info(String.format("OCI : load library \"%s\".", libFile.getAbsolutePath()));
|
106
|
-
|
107
|
-
File tempFolder = new File(lib, "temp");
|
108
|
-
tempFolder.mkdirs();
|
109
|
-
|
110
|
-
long currentTime = System.currentTimeMillis();
|
111
|
-
File[] files = tempFolder.listFiles();
|
112
|
-
if (files != null) {
|
113
|
-
for (File file : files) {
|
114
|
-
// delete old temporary files.
|
115
|
-
// if another plugin is using a file, it cannot be deleted.
|
116
|
-
// don't delete a recent file because another plugin may intend to use it.
|
117
|
-
if (file.isFile() && file.getName().startsWith(PLUGIN_NAME) && file.lastModified() < currentTime - 60000) {
|
118
|
-
file.delete();
|
119
|
-
}
|
120
|
-
}
|
121
|
-
}
|
122
|
-
|
123
|
-
String extension = libraryName.replaceAll("^[^\\.]*", "");
|
124
|
-
for (int i = 0; i < 10; i++) {
|
125
|
-
File tempLibFile = new File(tempFolder, PLUGIN_NAME + "-" + currentTime + "-" + i + extension);
|
126
|
-
if (tempLibFile.createNewFile()) {
|
127
|
-
// copy and load the library because different plugins cannot load the same library.
|
128
|
-
logger.info(String.format("OCI : create temporary library \"%s\".", tempLibFile.getAbsolutePath()));
|
129
|
-
Files.copy(libFile, tempLibFile);
|
130
|
-
System.load(tempLibFile.getAbsolutePath());
|
131
|
-
tempLibFile.deleteOnExit();
|
132
|
-
// but may not be deleted because loaded as a library.
|
133
|
-
return;
|
134
|
-
}
|
135
|
-
}
|
136
|
-
|
137
|
-
logger.error("OCI : cannot create temporary library.");
|
138
|
-
}
|
139
|
-
}
|
1
|
+
package org.embulk.output.oracle.oci;
|
2
|
+
|
3
|
+
import java.io.File;
|
4
|
+
import java.io.IOException;
|
5
|
+
import java.net.MalformedURLException;
|
6
|
+
import java.net.URISyntaxException;
|
7
|
+
import java.net.URL;
|
8
|
+
|
9
|
+
import org.embulk.spi.Exec;
|
10
|
+
import org.slf4j.Logger;
|
11
|
+
|
12
|
+
import com.google.common.io.Files;
|
13
|
+
|
14
|
+
|
15
|
+
public class OCI
|
16
|
+
{
|
17
|
+
private static final Logger logger = Exec.getLogger(OCI.class);
|
18
|
+
|
19
|
+
private static final String PLUGIN_NAME = "embulk-output-oracle";
|
20
|
+
|
21
|
+
static {
|
22
|
+
try {
|
23
|
+
loadLibrary();
|
24
|
+
} catch (URISyntaxException | IOException e) {
|
25
|
+
throw new RuntimeException(e);
|
26
|
+
}
|
27
|
+
}
|
28
|
+
|
29
|
+
public native byte[] createContext();
|
30
|
+
|
31
|
+
public native byte[] getLasetMessage(byte[] context);
|
32
|
+
|
33
|
+
public native boolean open(byte[] context, String dbName, String userName, String password);
|
34
|
+
|
35
|
+
public native boolean prepareLoad(byte[] context, TableDefinition tableDefinition);
|
36
|
+
|
37
|
+
public native boolean loadBuffer(byte[] context, byte[] buffer, int rowCount);
|
38
|
+
|
39
|
+
public native boolean commit(byte[] context);
|
40
|
+
|
41
|
+
public native boolean rollback(byte[] context);
|
42
|
+
|
43
|
+
public native void close(byte[] context);
|
44
|
+
|
45
|
+
private static void loadLibrary() throws URISyntaxException, IOException
|
46
|
+
{
|
47
|
+
loadLibrary(getPluginRoot());
|
48
|
+
}
|
49
|
+
|
50
|
+
private static File getPluginRoot() throws MalformedURLException, URISyntaxException
|
51
|
+
{
|
52
|
+
URL url = OCI.class.getResource("/" + OCI.class.getName().replace('.', '/') + ".class");
|
53
|
+
if (url.toString().startsWith("jar:")) {
|
54
|
+
url = new URL(url.toString().replaceAll("^jar:", "").replaceAll("![^!]*$", ""));
|
55
|
+
}
|
56
|
+
|
57
|
+
File folder = new File(url.toURI()).getParentFile();
|
58
|
+
for (;; folder = folder.getParentFile()) {
|
59
|
+
if (folder == null) {
|
60
|
+
String message = String.format("OCI : %s folder not found.", PLUGIN_NAME);
|
61
|
+
throw new RuntimeException(message);
|
62
|
+
}
|
63
|
+
|
64
|
+
if (folder.getName().startsWith(PLUGIN_NAME)) {
|
65
|
+
return folder;
|
66
|
+
}
|
67
|
+
}
|
68
|
+
}
|
69
|
+
|
70
|
+
private static void loadLibrary(File folder) throws IOException
|
71
|
+
{
|
72
|
+
File lib = new File(new File(folder, "lib"), "embulk");
|
73
|
+
|
74
|
+
String osName = System.getProperty("os.name");
|
75
|
+
String osArch = System.getProperty("os.arch");
|
76
|
+
|
77
|
+
String libraryName = System.mapLibraryName(PLUGIN_NAME);
|
78
|
+
File libFolder = null;
|
79
|
+
|
80
|
+
if (osName.startsWith("Windows")) {
|
81
|
+
if (osArch.endsWith("64")) {
|
82
|
+
libFolder = new File(lib, "win_x64");
|
83
|
+
} else if (osArch.equals("x86")) {
|
84
|
+
libFolder = new File(lib, "win_x86");
|
85
|
+
}
|
86
|
+
} else if (osName.equals("Linux")) {
|
87
|
+
if (osArch.endsWith("64")) {
|
88
|
+
libFolder = new File(lib, "linux_x64");
|
89
|
+
} else if (osArch.equals("x86")) {
|
90
|
+
libFolder = new File(lib, "linux_x86");
|
91
|
+
}
|
92
|
+
}
|
93
|
+
|
94
|
+
if (libFolder == null) {
|
95
|
+
logger.error(String.format("OCI : library \"%s\" for %s %s doesn't exist in lib folder.", libraryName, osName, osArch));
|
96
|
+
return;
|
97
|
+
}
|
98
|
+
|
99
|
+
File libFile = new File(libFolder, libraryName);
|
100
|
+
if (!libFile.exists()) {
|
101
|
+
logger.error(String.format("OCI : library \"%s\" doesn't exist.", libFile.getAbsolutePath()));
|
102
|
+
return;
|
103
|
+
}
|
104
|
+
|
105
|
+
logger.info(String.format("OCI : load library \"%s\".", libFile.getAbsolutePath()));
|
106
|
+
|
107
|
+
File tempFolder = new File(lib, "temp");
|
108
|
+
tempFolder.mkdirs();
|
109
|
+
|
110
|
+
long currentTime = System.currentTimeMillis();
|
111
|
+
File[] files = tempFolder.listFiles();
|
112
|
+
if (files != null) {
|
113
|
+
for (File file : files) {
|
114
|
+
// delete old temporary files.
|
115
|
+
// if another plugin is using a file, it cannot be deleted.
|
116
|
+
// don't delete a recent file because another plugin may intend to use it.
|
117
|
+
if (file.isFile() && file.getName().startsWith(PLUGIN_NAME) && file.lastModified() < currentTime - 60000) {
|
118
|
+
file.delete();
|
119
|
+
}
|
120
|
+
}
|
121
|
+
}
|
122
|
+
|
123
|
+
String extension = libraryName.replaceAll("^[^\\.]*", "");
|
124
|
+
for (int i = 0; i < 10; i++) {
|
125
|
+
File tempLibFile = new File(tempFolder, PLUGIN_NAME + "-" + currentTime + "-" + i + extension);
|
126
|
+
if (tempLibFile.createNewFile()) {
|
127
|
+
// copy and load the library because different plugins cannot load the same library.
|
128
|
+
logger.info(String.format("OCI : create temporary library \"%s\".", tempLibFile.getAbsolutePath()));
|
129
|
+
Files.copy(libFile, tempLibFile);
|
130
|
+
System.load(tempLibFile.getAbsolutePath());
|
131
|
+
tempLibFile.deleteOnExit();
|
132
|
+
// but may not be deleted because loaded as a library.
|
133
|
+
return;
|
134
|
+
}
|
135
|
+
}
|
136
|
+
|
137
|
+
logger.error("OCI : cannot create temporary library.");
|
138
|
+
}
|
139
|
+
}
|
@@ -1,64 +1,64 @@
|
|
1
|
-
package org.embulk.output.oracle.oci;
|
2
|
-
|
3
|
-
import java.sql.SQLException;
|
4
|
-
import java.util.HashMap;
|
5
|
-
import java.util.Map;
|
6
|
-
|
7
|
-
import org.embulk.spi.Exec;
|
8
|
-
import org.slf4j.Logger;
|
9
|
-
|
10
|
-
public class OCIManager
|
11
|
-
{
|
12
|
-
private static class OCIWrapperAndCounter {
|
13
|
-
public OCIWrapper oci;
|
14
|
-
public int counter;
|
15
|
-
}
|
16
|
-
|
17
|
-
|
18
|
-
private final Logger logger = Exec.getLogger(getClass());
|
19
|
-
|
20
|
-
private Map<Object, OCIWrapperAndCounter> ociAndCounters = new HashMap<Object, OCIWrapperAndCounter>();
|
21
|
-
|
22
|
-
|
23
|
-
public OCIWrapper open(Object key, String dbName, String userName, String password, TableDefinition tableDefinition) throws SQLException
|
24
|
-
{
|
25
|
-
synchronized(ociAndCounters) {
|
26
|
-
OCIWrapperAndCounter ociAndCounter;
|
27
|
-
if (ociAndCounters.containsKey(key)) {
|
28
|
-
ociAndCounter = ociAndCounters.get(key);
|
29
|
-
} else {
|
30
|
-
logger.info(String.format("OCI : open for %s.", key));
|
31
|
-
ociAndCounter = new OCIWrapperAndCounter();
|
32
|
-
ociAndCounter.oci = new OCIWrapper();
|
33
|
-
ociAndCounter.oci.open(dbName, userName, password);
|
34
|
-
ociAndCounter.oci.prepareLoad(tableDefinition);
|
35
|
-
ociAndCounters.put(key, ociAndCounter);
|
36
|
-
}
|
37
|
-
ociAndCounter.counter++;
|
38
|
-
return ociAndCounter.oci;
|
39
|
-
}
|
40
|
-
}
|
41
|
-
|
42
|
-
public OCIWrapper get(Object key)
|
43
|
-
{
|
44
|
-
synchronized(ociAndCounters) {
|
45
|
-
return ociAndCounters.get(key).oci;
|
46
|
-
}
|
47
|
-
}
|
48
|
-
|
49
|
-
public void close(Object key) throws SQLException
|
50
|
-
{
|
51
|
-
synchronized(ociAndCounters) {
|
52
|
-
OCIWrapperAndCounter ociAndCounter = ociAndCounters.get(key);
|
53
|
-
if (ociAndCounter != null) {
|
54
|
-
ociAndCounter.counter--;
|
55
|
-
if (ociAndCounter.counter == 0) {
|
56
|
-
logger.info(String.format("OCI : close for %s.", key));
|
57
|
-
ociAndCounter.oci.close();
|
58
|
-
ociAndCounters.remove(key);
|
59
|
-
}
|
60
|
-
}
|
61
|
-
}
|
62
|
-
}
|
63
|
-
|
64
|
-
}
|
1
|
+
package org.embulk.output.oracle.oci;
|
2
|
+
|
3
|
+
import java.sql.SQLException;
|
4
|
+
import java.util.HashMap;
|
5
|
+
import java.util.Map;
|
6
|
+
|
7
|
+
import org.embulk.spi.Exec;
|
8
|
+
import org.slf4j.Logger;
|
9
|
+
|
10
|
+
public class OCIManager
|
11
|
+
{
|
12
|
+
private static class OCIWrapperAndCounter {
|
13
|
+
public OCIWrapper oci;
|
14
|
+
public int counter;
|
15
|
+
}
|
16
|
+
|
17
|
+
|
18
|
+
private final Logger logger = Exec.getLogger(getClass());
|
19
|
+
|
20
|
+
private Map<Object, OCIWrapperAndCounter> ociAndCounters = new HashMap<Object, OCIWrapperAndCounter>();
|
21
|
+
|
22
|
+
|
23
|
+
public OCIWrapper open(Object key, String dbName, String userName, String password, TableDefinition tableDefinition) throws SQLException
|
24
|
+
{
|
25
|
+
synchronized(ociAndCounters) {
|
26
|
+
OCIWrapperAndCounter ociAndCounter;
|
27
|
+
if (ociAndCounters.containsKey(key)) {
|
28
|
+
ociAndCounter = ociAndCounters.get(key);
|
29
|
+
} else {
|
30
|
+
logger.info(String.format("OCI : open for %s.", key));
|
31
|
+
ociAndCounter = new OCIWrapperAndCounter();
|
32
|
+
ociAndCounter.oci = new OCIWrapper();
|
33
|
+
ociAndCounter.oci.open(dbName, userName, password);
|
34
|
+
ociAndCounter.oci.prepareLoad(tableDefinition);
|
35
|
+
ociAndCounters.put(key, ociAndCounter);
|
36
|
+
}
|
37
|
+
ociAndCounter.counter++;
|
38
|
+
return ociAndCounter.oci;
|
39
|
+
}
|
40
|
+
}
|
41
|
+
|
42
|
+
public OCIWrapper get(Object key)
|
43
|
+
{
|
44
|
+
synchronized(ociAndCounters) {
|
45
|
+
return ociAndCounters.get(key).oci;
|
46
|
+
}
|
47
|
+
}
|
48
|
+
|
49
|
+
public void close(Object key) throws SQLException
|
50
|
+
{
|
51
|
+
synchronized(ociAndCounters) {
|
52
|
+
OCIWrapperAndCounter ociAndCounter = ociAndCounters.get(key);
|
53
|
+
if (ociAndCounter != null) {
|
54
|
+
ociAndCounter.counter--;
|
55
|
+
if (ociAndCounter.counter == 0) {
|
56
|
+
logger.info(String.format("OCI : close for %s.", key));
|
57
|
+
ociAndCounter.oci.close();
|
58
|
+
ociAndCounters.remove(key);
|
59
|
+
}
|
60
|
+
}
|
61
|
+
}
|
62
|
+
}
|
63
|
+
|
64
|
+
}
|
@@ -1,96 +1,96 @@
|
|
1
|
-
package org.embulk.output.oracle.oci;
|
2
|
-
|
3
|
-
import java.nio.charset.Charset;
|
4
|
-
import java.sql.SQLException;
|
5
|
-
|
6
|
-
import org.embulk.spi.Exec;
|
7
|
-
import org.slf4j.Logger;
|
8
|
-
|
9
|
-
|
10
|
-
public class OCIWrapper implements AutoCloseable
|
11
|
-
{
|
12
|
-
private final Logger logger = Exec.getLogger(getClass());
|
13
|
-
|
14
|
-
private final OCI oci = new OCI();
|
15
|
-
// used for messages
|
16
|
-
private final Charset defaultCharset;
|
17
|
-
private byte[] context;
|
18
|
-
private boolean errorOccured;
|
19
|
-
private boolean committedOrRollbacked;
|
20
|
-
|
21
|
-
|
22
|
-
public OCIWrapper()
|
23
|
-
{
|
24
|
-
// enable to change default encoding for test
|
25
|
-
defaultCharset = Charset.forName(System.getProperty("file.encoding"));
|
26
|
-
context = oci.createContext();
|
27
|
-
}
|
28
|
-
|
29
|
-
public void open(String dbName, String userName, String password) throws SQLException
|
30
|
-
{
|
31
|
-
if (!oci.open(context, dbName, userName, password)) {
|
32
|
-
throwException();
|
33
|
-
}
|
34
|
-
}
|
35
|
-
|
36
|
-
public void prepareLoad(TableDefinition tableDefinition) throws SQLException
|
37
|
-
{
|
38
|
-
if (!oci.prepareLoad(context, tableDefinition)) {
|
39
|
-
throwException();
|
40
|
-
}
|
41
|
-
}
|
42
|
-
|
43
|
-
public void loadBuffer(byte[] buffer, int rowCount) throws SQLException
|
44
|
-
{
|
45
|
-
if (!oci.loadBuffer(context, buffer, rowCount)) {
|
46
|
-
throwException();
|
47
|
-
}
|
48
|
-
}
|
49
|
-
|
50
|
-
public void commit() throws SQLException
|
51
|
-
{
|
52
|
-
committedOrRollbacked = true;
|
53
|
-
logger.info("OCI : start to commit.");
|
54
|
-
if (!oci.commit(context)) {
|
55
|
-
throwException();
|
56
|
-
}
|
57
|
-
}
|
58
|
-
|
59
|
-
public void rollback() throws SQLException
|
60
|
-
{
|
61
|
-
committedOrRollbacked = true;
|
62
|
-
logger.info("OCI : start to rollback.");
|
63
|
-
if (!oci.rollback(context)) {
|
64
|
-
throwException();
|
65
|
-
}
|
66
|
-
}
|
67
|
-
|
68
|
-
private void throwException() throws SQLException
|
69
|
-
{
|
70
|
-
errorOccured = true;
|
71
|
-
String message = new String(oci.getLasetMessage(context), defaultCharset);
|
72
|
-
logger.error(message);
|
73
|
-
throw new SQLException(message);
|
74
|
-
}
|
75
|
-
|
76
|
-
|
77
|
-
@Override
|
78
|
-
public void close() throws SQLException
|
79
|
-
{
|
80
|
-
if (context != null) {
|
81
|
-
try {
|
82
|
-
if (!committedOrRollbacked) {
|
83
|
-
if (errorOccured) {
|
84
|
-
rollback();
|
85
|
-
} else {
|
86
|
-
commit();
|
87
|
-
}
|
88
|
-
}
|
89
|
-
} finally {
|
90
|
-
oci.close(context);
|
91
|
-
context = null;
|
92
|
-
}
|
93
|
-
}
|
94
|
-
}
|
95
|
-
|
96
|
-
}
|
1
|
+
package org.embulk.output.oracle.oci;
|
2
|
+
|
3
|
+
import java.nio.charset.Charset;
|
4
|
+
import java.sql.SQLException;
|
5
|
+
|
6
|
+
import org.embulk.spi.Exec;
|
7
|
+
import org.slf4j.Logger;
|
8
|
+
|
9
|
+
|
10
|
+
public class OCIWrapper implements AutoCloseable
|
11
|
+
{
|
12
|
+
private final Logger logger = Exec.getLogger(getClass());
|
13
|
+
|
14
|
+
private final OCI oci = new OCI();
|
15
|
+
// used for messages
|
16
|
+
private final Charset defaultCharset;
|
17
|
+
private byte[] context;
|
18
|
+
private boolean errorOccured;
|
19
|
+
private boolean committedOrRollbacked;
|
20
|
+
|
21
|
+
|
22
|
+
public OCIWrapper()
|
23
|
+
{
|
24
|
+
// enable to change default encoding for test
|
25
|
+
defaultCharset = Charset.forName(System.getProperty("file.encoding"));
|
26
|
+
context = oci.createContext();
|
27
|
+
}
|
28
|
+
|
29
|
+
public void open(String dbName, String userName, String password) throws SQLException
|
30
|
+
{
|
31
|
+
if (!oci.open(context, dbName, userName, password)) {
|
32
|
+
throwException();
|
33
|
+
}
|
34
|
+
}
|
35
|
+
|
36
|
+
public void prepareLoad(TableDefinition tableDefinition) throws SQLException
|
37
|
+
{
|
38
|
+
if (!oci.prepareLoad(context, tableDefinition)) {
|
39
|
+
throwException();
|
40
|
+
}
|
41
|
+
}
|
42
|
+
|
43
|
+
public void loadBuffer(byte[] buffer, int rowCount) throws SQLException
|
44
|
+
{
|
45
|
+
if (!oci.loadBuffer(context, buffer, rowCount)) {
|
46
|
+
throwException();
|
47
|
+
}
|
48
|
+
}
|
49
|
+
|
50
|
+
public void commit() throws SQLException
|
51
|
+
{
|
52
|
+
committedOrRollbacked = true;
|
53
|
+
logger.info("OCI : start to commit.");
|
54
|
+
if (!oci.commit(context)) {
|
55
|
+
throwException();
|
56
|
+
}
|
57
|
+
}
|
58
|
+
|
59
|
+
public void rollback() throws SQLException
|
60
|
+
{
|
61
|
+
committedOrRollbacked = true;
|
62
|
+
logger.info("OCI : start to rollback.");
|
63
|
+
if (!oci.rollback(context)) {
|
64
|
+
throwException();
|
65
|
+
}
|
66
|
+
}
|
67
|
+
|
68
|
+
private void throwException() throws SQLException
|
69
|
+
{
|
70
|
+
errorOccured = true;
|
71
|
+
String message = new String(oci.getLasetMessage(context), defaultCharset);
|
72
|
+
logger.error(message);
|
73
|
+
throw new SQLException(message);
|
74
|
+
}
|
75
|
+
|
76
|
+
|
77
|
+
@Override
|
78
|
+
public void close() throws SQLException
|
79
|
+
{
|
80
|
+
if (context != null) {
|
81
|
+
try {
|
82
|
+
if (!committedOrRollbacked) {
|
83
|
+
if (errorOccured) {
|
84
|
+
rollback();
|
85
|
+
} else {
|
86
|
+
commit();
|
87
|
+
}
|
88
|
+
}
|
89
|
+
} finally {
|
90
|
+
oci.close(context);
|
91
|
+
context = null;
|
92
|
+
}
|
93
|
+
}
|
94
|
+
}
|
95
|
+
|
96
|
+
}
|