embulk-output-sqlserver 0.7.8 → 0.7.9

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: 786d49060396376e68bd28cf4e69a374cb9d30e2
4
- data.tar.gz: 9dd2c4b622a916271a964b3d042efce2de4cdfe1
3
+ metadata.gz: 760819b9d6fa3e94d1719d1730e4b5b7564b6134
4
+ data.tar.gz: 2867fe096261c86be930712a61becaa633bf30ac
5
5
  SHA512:
6
- metadata.gz: 4b193c07d2db4dc3b56e8ea55c2204766543ec557ca147db3b8e13bd232eb7889913cc744bc511af9127008be3e7f18e6bc890a47eeaddd9861684b41bbf312b
7
- data.tar.gz: 81e500f5d37ea191468130718abb6a75a70d7ea21dcb97721c6b853c2bf35e4e4d83f32fa9a7903f75ddee96d72cdffd71a5326d91b9eede5c07036f2671e488
6
+ metadata.gz: b40606a2db66bd7725dee518ff2d69041cccc59ddf3d0125d854060d49aab17e9db19d1192b8d6731092e3a6f49181e3f503cf5d3934d38ebccb8898e960d95d
7
+ data.tar.gz: c7f7bad7eddc9fafbdfa0e37bca3f4dfcf24d65eec2ebf19b1d9b277f60c9328492897fb0da07d6f06d9f1098f7df55498fc8b8943968909dfb227113c24e221
data/README.md CHANGED
@@ -22,6 +22,8 @@ embulk "-J-Djava.library.path=C:\drivers" run input-sqlserver.yml
22
22
  - **password**: database login password (string, default: "")
23
23
  - **instance**: destination instance name (string, default: use the default instance)
24
24
  - **database**: destination database name (string, default: use the default database)
25
+ - **schema**: destination schema name (string, optional)
26
+ - **temp_schema**: schema name for intermediate tables. by default, intermediate tables will be created in the same schema as destination table. (string, optional)
25
27
  - **url**: URL of the JDBC connection (string, optional)
26
28
  - **table**: destination table name (string, required)
27
29
  - **options**: extra connection properties (hash, default: {})
@@ -8,8 +8,10 @@ import org.embulk.config.ConfigDefault;
8
8
  import org.embulk.config.ConfigException;
9
9
  import org.embulk.output.jdbc.AbstractJdbcOutputPlugin;
10
10
  import org.embulk.output.jdbc.BatchInsert;
11
+ import org.embulk.output.jdbc.JdbcOutputConnection;
11
12
  import org.embulk.output.jdbc.MergeConfig;
12
13
  import org.embulk.output.jdbc.StandardBatchInsert;
14
+ import org.embulk.output.jdbc.TableIdentifier;
13
15
  import org.embulk.output.jdbc.setter.ColumnSetterFactory;
14
16
  import org.embulk.output.sqlserver.InsertMethod;
15
17
  import org.embulk.output.sqlserver.NativeBatchInsert;
@@ -67,6 +69,14 @@ public class SQLServerOutputPlugin
67
69
  @ConfigDefault("\"\"")
68
70
  public Optional<String> getPassword();
69
71
 
72
+ @Config("schema")
73
+ @ConfigDefault("null")
74
+ public Optional<String> getSchema();
75
+
76
+ @Config("temp_schema")
77
+ @ConfigDefault("null")
78
+ public Optional<String> getTempSchema();
79
+
70
80
  @Config("insert_method")
71
81
  @ConfigDefault("\"normal\"")
72
82
  public InsertMethod getInsertMethod();
@@ -139,7 +149,7 @@ public class SQLServerOutputPlugin
139
149
 
140
150
  UrlAndProperties urlProps = getUrlAndProperties(sqlServerTask, useJtdsDriver);
141
151
  logger.info("Connecting to {} options {}", urlProps.getUrl(), getPropsWithMaskedSecret(urlProps));
142
- return new SQLServerOutputConnector(urlProps.getUrl(), urlProps.getProps(), null);
152
+ return new SQLServerOutputConnector(urlProps.getUrl(), urlProps.getProps(), sqlServerTask.getSchema().orNull());
143
153
  }
144
154
 
145
155
  private UrlAndProperties getUrlAndProperties(SQLServerPluginTask sqlServerTask, boolean useJtdsDriver)
@@ -231,6 +241,15 @@ public class SQLServerOutputPlugin
231
241
  return new UrlAndProperties(url, props);
232
242
  }
233
243
 
244
+ @Override
245
+ protected TableIdentifier buildIntermediateTableId(JdbcOutputConnection con, PluginTask task, String tableName) {
246
+ SQLServerPluginTask sqlServerTask = (SQLServerPluginTask) task;
247
+ // replace mode doesn't support temp_schema because sp_rename cannot change schema of table
248
+ if (sqlServerTask.getTempSchema().isPresent() && sqlServerTask.getMode() != Mode.REPLACE) {
249
+ return new TableIdentifier(null, sqlServerTask.getTempSchema().get(), tableName);
250
+ }
251
+ return super.buildIntermediateTableId(con, task, tableName);
252
+ }
234
253
 
235
254
  @Override
236
255
  protected BatchInsert newBatchInsert(PluginTask task, Optional<MergeConfig> mergeConfig) throws IOException, SQLException
@@ -12,6 +12,7 @@ import org.embulk.output.jdbc.BatchInsert;
12
12
  import org.embulk.output.jdbc.JdbcColumn;
13
13
  import org.embulk.output.jdbc.JdbcSchema;
14
14
  import org.embulk.output.jdbc.StandardBatchInsert;
15
+ import org.embulk.output.jdbc.TableIdentifier;
15
16
  import org.embulk.output.jdbc.TimestampFormat;
16
17
  import org.embulk.output.sqlserver.nativeclient.NativeClientWrapper;
17
18
  import org.embulk.spi.Exec;
@@ -58,10 +59,10 @@ public class NativeBatchInsert implements BatchInsert
58
59
 
59
60
 
60
61
  @Override
61
- public void prepare(String loadTable, JdbcSchema insertSchema) throws SQLException
62
+ public void prepare(TableIdentifier loadTable, JdbcSchema insertSchema) throws SQLException
62
63
  {
63
64
  columnCount = insertSchema.getCount();
64
- client.open(server, port, instance, database, user, password, loadTable);
65
+ client.open(server, port, instance, database, user, password, loadTable.getTableName());
65
66
 
66
67
  formats = new DateFormat[insertSchema.getCount()];
67
68
  for (int i = 0; i < insertSchema.getCount(); i++) {
@@ -10,6 +10,7 @@ import org.embulk.output.jdbc.JdbcColumn;
10
10
  import org.embulk.output.jdbc.JdbcOutputConnection;
11
11
  import org.embulk.output.jdbc.JdbcSchema;
12
12
  import org.embulk.output.jdbc.MergeConfig;
13
+ import org.embulk.output.jdbc.TableIdentifier;
13
14
 
14
15
  public class SQLServerOutputConnection
15
16
  extends JdbcOutputConnection
@@ -22,13 +23,18 @@ public class SQLServerOutputConnection
22
23
  }
23
24
 
24
25
  @Override
25
- protected String buildRenameTableSql(String fromTable, String toTable)
26
+ protected String buildRenameTableSql(TableIdentifier fromTable, TableIdentifier toTable)
26
27
  {
28
+ // sp_rename cannot change schema of table
27
29
  StringBuilder sb = new StringBuilder();
28
30
  sb.append("EXEC sp_rename ");
29
- sb.append(quoteIdentifierString(fromTable));
31
+ if (fromTable.getSchemaName() == null) {
32
+ sb.append(quoteIdentifierString(fromTable.getTableName()));
33
+ } else {
34
+ sb.append(quoteIdentifierString(fromTable.getSchemaName() + "." + fromTable.getTableName()));
35
+ }
30
36
  sb.append(", ");
31
- sb.append(quoteIdentifierString(toTable));
37
+ sb.append(quoteIdentifierString(toTable.getTableName()));
32
38
  sb.append(", 'OBJECT'");
33
39
  return sb.toString();
34
40
  }
@@ -55,53 +61,29 @@ public class SQLServerOutputConnection
55
61
  }
56
62
 
57
63
  @Override
58
- public void dropTableIfExists(String tableName) throws SQLException
64
+ public void dropTableIfExists(TableIdentifier table) throws SQLException
59
65
  {
60
- if (tableExists(tableName)) {
61
- dropTable(tableName);
66
+ if (tableExists(table)) {
67
+ dropTable(table);
62
68
  }
63
69
  }
64
70
 
65
71
  @Override
66
- protected void dropTableIfExists(Statement stmt, String tableName) throws SQLException
72
+ protected void dropTableIfExists(Statement stmt, TableIdentifier table) throws SQLException
67
73
  {
68
- if (tableExists(tableName)) {
69
- dropTable(stmt, tableName);
74
+ if (tableExists(table)) {
75
+ dropTable(stmt, table);
70
76
  }
71
77
  }
72
78
 
73
79
  @Override
74
- public void createTableIfNotExists(String tableName, JdbcSchema schema) throws SQLException
75
- {
76
- if (!tableExists(tableName)) {
77
- createTable(tableName, schema);
78
- }
79
- }
80
-
81
- public void createTable(String tableName, JdbcSchema schema) throws SQLException
80
+ public void createTableIfNotExists(TableIdentifier table, JdbcSchema schema) throws SQLException
82
81
  {
83
- Statement stmt = connection.createStatement();
84
- try {
85
- String sql = buildCreateTableSql(tableName, schema);
86
- executeUpdate(stmt, sql);
87
- commitIfNecessary(connection);
88
- } catch (SQLException ex) {
89
- throw safeRollback(connection, ex);
90
- } finally {
91
- stmt.close();
82
+ if (!tableExists(table)) {
83
+ createTable(table, schema);
92
84
  }
93
85
  }
94
86
 
95
- protected String buildCreateTableSql(String name, JdbcSchema schema)
96
- {
97
- StringBuilder sb = new StringBuilder();
98
-
99
- sb.append("CREATE TABLE ");
100
- quoteIdentifierString(sb, name);
101
- sb.append(buildCreateTableSchemaSql(schema));
102
- return sb.toString();
103
- }
104
-
105
87
  private static final String[] SIMPLE_TYPE_NAMES = {
106
88
  "BIT", "FLOAT",
107
89
  };
@@ -116,12 +98,12 @@ public class SQLServerOutputConnection
116
98
  }
117
99
 
118
100
  @Override
119
- protected String buildCollectMergeSql(List<String> fromTables, JdbcSchema schema, String toTable, MergeConfig mergeConfig) throws SQLException
101
+ protected String buildCollectMergeSql(List<TableIdentifier> fromTables, JdbcSchema schema, TableIdentifier toTable, MergeConfig mergeConfig) throws SQLException
120
102
  {
121
103
  StringBuilder sb = new StringBuilder();
122
104
 
123
105
  sb.append("MERGE INTO ");
124
- sb.append(quoteIdentifierString(toTable));
106
+ sb.append(quoteTableIdentifier(toTable));
125
107
  sb.append(" AS T");
126
108
  sb.append(" USING (");
127
109
  for (int i = 0; i < fromTables.size(); i++) {
@@ -129,7 +111,7 @@ public class SQLServerOutputConnection
129
111
  sb.append(" SELECT ");
130
112
  sb.append(buildColumns(schema, ""));
131
113
  sb.append(" FROM ");
132
- sb.append(quoteIdentifierString(fromTables.get(i)));
114
+ sb.append(quoteTableIdentifier(fromTables.get(i)));
133
115
  }
134
116
  sb.append(") AS S");
135
117
  sb.append(" ON (");
@@ -1,15 +1,22 @@
1
1
  package org.embulk.output.sqlserver;
2
2
 
3
- import org.embulk.output.jdbc.JdbcOutputConnector;
4
-
5
3
  import java.sql.Connection;
6
4
  import java.sql.DriverManager;
5
+ import java.sql.PreparedStatement;
6
+ import java.sql.ResultSet;
7
7
  import java.sql.SQLException;
8
8
  import java.util.Properties;
9
9
 
10
+ import org.embulk.output.jdbc.JdbcOutputConnector;
11
+ import org.embulk.spi.Exec;
12
+ import org.slf4j.Logger;
13
+
14
+
10
15
  public class SQLServerOutputConnector
11
16
  implements JdbcOutputConnector
12
17
  {
18
+ private final Logger logger = Exec.getLogger(getClass());
19
+
13
20
  private final String url;
14
21
  private final Properties properties;
15
22
  private final String schemaName;
@@ -30,6 +37,23 @@ public class SQLServerOutputConnector
30
37
  throw new SQLException("Invalid url : " + url);
31
38
  }
32
39
 
40
+ String schemaName = this.schemaName;
41
+ if (schemaName == null) {
42
+ // get default schema name for user (Connection#getSchema won't work)
43
+ try {
44
+ try (PreparedStatement statement = c.prepareStatement("SELECT default_schema_name FROM sys.database_principals WHERE name = ?")) {
45
+ statement.setString(1, properties.getProperty("user"));
46
+ try (ResultSet rs = statement.executeQuery()) {
47
+ if (rs.next()) {
48
+ schemaName = rs.getString(1);
49
+ }
50
+ }
51
+ }
52
+ } catch (SQLException e) {
53
+ logger.warn("Cannot specify default schema : " + e);
54
+ }
55
+ }
56
+
33
57
  try {
34
58
  SQLServerOutputConnection con = new SQLServerOutputConnection(c, schemaName, autoCommit);
35
59
  c = null;
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: embulk-output-sqlserver
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.7.8
4
+ version: 0.7.9
5
5
  platform: ruby
6
6
  authors:
7
7
  - Sadayuki Furuhashi
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2017-05-10 00:00:00.000000000 Z
11
+ date: 2017-06-23 00:00:00.000000000 Z
12
12
  dependencies: []
13
13
  description: Inserts or updates records to a table.
14
14
  email:
@@ -19,8 +19,8 @@ extra_rdoc_files: []
19
19
  files:
20
20
  - README.md
21
21
  - build.gradle
22
- - classpath/embulk-output-jdbc-0.7.8.jar
23
- - classpath/embulk-output-sqlserver-0.7.8.jar
22
+ - classpath/embulk-output-jdbc-0.7.9.jar
23
+ - classpath/embulk-output-sqlserver-0.7.9.jar
24
24
  - classpath/jtds-1.3.1.jar
25
25
  - lib/embulk/output/sqlserver.rb
26
26
  - src/main/java/org/embulk/output/SQLServerOutputPlugin.java
Binary file