embulk-output-cdata 0.4.0 → 0.4.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 3b14cc46a35345c45de608f6c6992d382a2656fb
4
- data.tar.gz: a0a95f4243655c9bcb4971831f959ed24310ca71
3
+ metadata.gz: 2a79b482391f04bdffabfda7cadb2bf8fffdcaea
4
+ data.tar.gz: 805ef6119099703dfa3591cfb1038f3bd6718657
5
5
  SHA512:
6
- metadata.gz: c863615fe547ff4a20070217a1f46fa7516acdcfa5e99dec569ae4e34f6fd0454cc5a2bf620595c04bb101139cdb35afd2d4488009a339b6e1f6b4b7dea419fa
7
- data.tar.gz: 9b91c56f8bd54edc5f9c6d5175aa114c33aed1db2469e36b3309faa400385b541b8f232c0d23cbf1b327210ca95078001c09a68698d2e69d4fa4a1bfc9bc7bb8
6
+ metadata.gz: fa5485cffa86d2b25359d227a86dd7119ac06ecb1a4783acc21ee9db26c27411e279630f13fc67111c994945112956f8d85b0b83f6e62cdb3c63f994f79247e5
7
+ data.tar.gz: 06104206fdc4d697960b937dc92d0e26e8e23d520b9639ae5ad75358f2470626be8bc2f6237a8f98bf2098bd48f66747ebbaab2d2174daac6b9e4c130672114c
data/build.gradle CHANGED
@@ -13,7 +13,7 @@ configurations {
13
13
  provided
14
14
  }
15
15
 
16
- version = "0.4.0"
16
+ version = "0.4.1"
17
17
 
18
18
  sourceCompatibility = 1.8
19
19
  targetCompatibility = 1.8
@@ -43,6 +43,10 @@ public class CDataOutputPlugin
43
43
  @Config("default_primary_key")
44
44
  @ConfigDefault("\"id\"")
45
45
  String getDefaultPrimaryKey();
46
+
47
+ @Config("remove_csv_file")
48
+ @ConfigDefault("false")
49
+ Boolean getRemoveCsvFile();
46
50
  }
47
51
 
48
52
  @Override
@@ -85,7 +89,10 @@ public class CDataOutputPlugin
85
89
  PluginTask task = taskSource.loadTask(PluginTask.class);
86
90
 
87
91
  PageReader reader = new PageReader(schema);
88
- if (Objects.equals(task.getMode(), "insert_direct")) {
92
+ if (Objects.equals(task.getMode(), "insert")) {
93
+ return new CDataPageOutputForInsert(reader, conn, task);
94
+ }
95
+ else if (Objects.equals(task.getMode(), "upsert")) {
89
96
  if (Objects.equals(task.getDriverName(), "cdata.jdbc.salesforce.SalesforceDriver")) {
90
97
  return new CDataPageOutputForUpsert(reader, conn, task);
91
98
  } else {
@@ -0,0 +1,177 @@
1
+ package org.embulk.output.cdata;
2
+
3
+ import org.embulk.config.TaskReport;
4
+ import org.embulk.output.cdata.procedures.DeleteFile;
5
+ import org.embulk.spi.*;
6
+ import org.slf4j.Logger;
7
+ import org.slf4j.LoggerFactory;
8
+
9
+ import java.sql.*;
10
+ import java.util.ArrayList;
11
+ import java.util.stream.Collectors;
12
+
13
+ public class CDataPageOutputForInsert implements TransactionalPageOutput {
14
+
15
+ private static final Logger logger = LoggerFactory.getLogger(CDataPageOutputForInsert.class);
16
+
17
+ private final PageReader pageReader;
18
+ private final Connection conn;
19
+ private final CDataOutputPlugin.PluginTask task;
20
+ private PreparedStatement preparedStatement;
21
+
22
+ public CDataPageOutputForInsert(final PageReader reader, Connection conn, CDataOutputPlugin.PluginTask task) {
23
+ this.pageReader = reader;
24
+ this.conn = conn;
25
+ this.task = task;
26
+ this.preparedStatement = null;
27
+ }
28
+
29
+ @Override
30
+ public void add(Page page) {
31
+ pageReader.setPage(page);
32
+
33
+ if (task.getRemoveCsvFile()) {
34
+ try {
35
+ DeleteFile.execute(conn, task.getTable());
36
+ } catch (SQLException e) {
37
+ throw new RuntimeException(e);
38
+ }
39
+ }
40
+
41
+ ArrayList<String> columnNames = pageReader.getSchema().getColumns().stream()
42
+ .map(Column::getName).collect(Collectors.toCollection(ArrayList::new));
43
+
44
+ ArrayList<String> preparedValues = pageReader.getSchema().getColumns().stream()
45
+ .map(it -> "?").collect(Collectors.toCollection(ArrayList::new));
46
+
47
+ String insertStatement = "INSERT INTO " + task.getTable() + "(" +
48
+ String.join(", ", columnNames) +
49
+ ") VALUES (" +
50
+ String.join(", ", preparedValues) + ")";
51
+ logger.info(insertStatement);
52
+
53
+ while (pageReader.nextRecord()) {
54
+ try {
55
+ this.preparedStatement = conn.prepareStatement(insertStatement, Statement.RETURN_GENERATED_KEYS);
56
+ pageReader.getSchema().visitColumns(createColumnVisitor(preparedStatement));
57
+ preparedStatement.executeUpdate();
58
+ } catch (SQLException e) {
59
+ throw new RuntimeException(e);
60
+ }
61
+ }
62
+ }
63
+
64
+ @Override
65
+ public void finish() {
66
+ try {
67
+ conn.close();
68
+ } catch (SQLException e) {
69
+ throw new RuntimeException(e);
70
+ }
71
+ }
72
+
73
+ @Override
74
+ public void close() {
75
+ try {
76
+ conn.close();
77
+ } catch (SQLException e) {
78
+ throw new RuntimeException(e);
79
+ }
80
+ }
81
+
82
+ @Override
83
+ public void abort() {
84
+ try {
85
+ conn.close();
86
+ } catch (SQLException e) {
87
+ throw new RuntimeException(e);
88
+ }
89
+ }
90
+
91
+ @Override
92
+ public TaskReport commit() {
93
+ return null;
94
+ }
95
+
96
+ private ColumnVisitor createColumnVisitor(PreparedStatement preparedStatement) {
97
+ return new ColumnVisitor() {
98
+ @Override
99
+ public void booleanColumn(Column column) {
100
+ try {
101
+ if (pageReader.isNull(column)) {
102
+ preparedStatement.setObject(column.getIndex() + 1, null);
103
+ } else {
104
+ preparedStatement.setBoolean(column.getIndex() + 1, pageReader.getBoolean(column));
105
+ }
106
+ } catch (SQLException e) {
107
+ throw new RuntimeException(e);
108
+ }
109
+ }
110
+
111
+ @Override
112
+ public void longColumn(Column column) {
113
+ try {
114
+ if (pageReader.isNull(column)) {
115
+ preparedStatement.setObject(column.getIndex() + 1, null);
116
+ } else {
117
+ preparedStatement.setLong(column.getIndex() + 1, pageReader.getLong(column));
118
+ }
119
+ } catch (SQLException e) {
120
+ throw new RuntimeException(e);
121
+ }
122
+ }
123
+
124
+ @Override
125
+ public void doubleColumn(Column column) {
126
+ try {
127
+ if (pageReader.isNull(column)) {
128
+ preparedStatement.setObject(column.getIndex() + 1, null);
129
+ } else {
130
+ preparedStatement.setDouble(column.getIndex() + 1, pageReader.getDouble(column));
131
+ }
132
+ } catch (SQLException e) {
133
+ throw new RuntimeException(e);
134
+ }
135
+ }
136
+
137
+ @Override
138
+ public void stringColumn(Column column) {
139
+ try {
140
+ if (pageReader.isNull(column)) {
141
+ preparedStatement.setObject(column.getIndex() + 1, null);
142
+ } else {
143
+ preparedStatement.setString(column.getIndex() + 1, pageReader.getString(column));
144
+ }
145
+ } catch (SQLException e) {
146
+ throw new RuntimeException(e);
147
+ }
148
+ }
149
+
150
+ @Override
151
+ public void timestampColumn(Column column) {
152
+ try {
153
+ if (pageReader.isNull(column)) {
154
+ preparedStatement.setObject(column.getIndex() + 1, null);
155
+ } else {
156
+ preparedStatement.setTimestamp(column.getIndex() + 1, Timestamp.from(pageReader.getTimestampInstant(column)));
157
+ }
158
+ } catch (SQLException e) {
159
+ throw new RuntimeException(e);
160
+ }
161
+ }
162
+
163
+ @Override
164
+ public void jsonColumn(Column column) {
165
+ try {
166
+ if (pageReader.isNull(column)) {
167
+ preparedStatement.setObject(column.getIndex() + 1, null);
168
+ } else {
169
+ preparedStatement.setString(column.getIndex() + 1, pageReader.getString(column));
170
+ }
171
+ } catch (SQLException e) {
172
+ throw new RuntimeException(e);
173
+ }
174
+ }
175
+ };
176
+ }
177
+ }
@@ -0,0 +1,34 @@
1
+ package org.embulk.output.cdata.procedures;
2
+
3
+ import org.slf4j.Logger;
4
+ import org.slf4j.LoggerFactory;
5
+
6
+ import java.sql.Connection;
7
+ import java.sql.SQLException;
8
+ import java.sql.Statement;
9
+ import java.util.Arrays;
10
+
11
+ public class DeleteFile {
12
+
13
+ private static final Logger logger = LoggerFactory.getLogger(DeleteFile.class);
14
+ public static String[] SUPPOERTED_CLASS_NAMES = { "cdata.jdbc.csv.CSVDriver" };
15
+
16
+ public static void execute(Connection conn, String fileName) throws SQLException {
17
+ String className = conn.getMetaData().getDriverName();
18
+ if (Arrays.asList(SUPPOERTED_CLASS_NAMES).contains(className)) {
19
+ logger.info("not supported delete file className, skipped delete file: " + className);
20
+ return;
21
+ }
22
+ Statement stmt = conn.createStatement();
23
+ String source = fileName + ".csv";
24
+ String sql = "EXEC DELETEFILE @PATH='" + source + "'";
25
+ try {
26
+ boolean ret = stmt.execute(sql);
27
+ if (!ret) {
28
+ logger.info("failed to delete file: " + source);
29
+ }
30
+ } catch (Exception e) {
31
+ logger.error(e.getMessage());
32
+ }
33
+ }
34
+ }
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: embulk-output-cdata
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.4.0
4
+ version: 0.4.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - sampo02
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2022-10-20 00:00:00.000000000 Z
11
+ date: 2022-10-25 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  requirement: !ruby/object:Gem::Requirement
@@ -49,7 +49,7 @@ files:
49
49
  - LICENSE.txt
50
50
  - README.md
51
51
  - build.gradle
52
- - classpath/embulk-output-cdata-0.4.0.jar
52
+ - classpath/embulk-output-cdata-0.4.1.jar
53
53
  - config/checkstyle/checkstyle.xml
54
54
  - config/checkstyle/default.xml
55
55
  - gradle/wrapper/gradle-wrapper.jar
@@ -58,10 +58,12 @@ files:
58
58
  - gradlew.bat
59
59
  - lib/embulk/output/cdata.rb
60
60
  - src/main/java/org/embulk/output/cdata/CDataOutputPlugin.java
61
+ - src/main/java/org/embulk/output/cdata/CDataPageOutputForInsert.java
61
62
  - src/main/java/org/embulk/output/cdata/CDataPageOutputForManualUpsert.java
62
63
  - src/main/java/org/embulk/output/cdata/CDataPageOutputForUpdate.java
63
64
  - src/main/java/org/embulk/output/cdata/CDataPageOutputForUpsert.java
64
65
  - src/main/java/org/embulk/output/cdata/CDataPageOutputForUpsertBase.java
66
+ - src/main/java/org/embulk/output/cdata/procedures/DeleteFile.java
65
67
  - src/test/java/org/embulk/output/cdata/TestCDataOutputPlugin.java
66
68
  homepage: https://github.com/datable-inc/embulk-output-cdata
67
69
  licenses: