|
1 | 1 | package org.embulk.output; |
2 | 2 |
|
3 | 3 | import java.io.IOException; |
4 | | -import java.sql.DatabaseMetaData; |
5 | | -import java.sql.ResultSet; |
6 | 4 | import java.sql.SQLException; |
7 | 5 | import java.util.*; |
8 | 6 | import org.embulk.config.ConfigDiff; |
@@ -171,86 +169,10 @@ protected void logConnectionProperties(String url, Properties props) { |
171 | 169 | super.logConnectionProperties(url, maskedProps); |
172 | 170 | } |
173 | 171 |
|
174 | | - // This is almost copy from AbstractJdbcOutputPlugin excepting validation of table exists in |
175 | | - // current schema |
176 | 172 | public Optional<JdbcSchema> newJdbcSchemaFromTableIfExists( |
177 | 173 | JdbcOutputConnection connection, TableIdentifier table) throws SQLException { |
178 | | - if (!connection.tableExists(table)) { |
179 | | - // DatabaseMetaData.getPrimaryKeys fails if table does not exist |
180 | | - return Optional.empty(); |
181 | | - } |
182 | | - |
183 | | - DatabricksOutputConnection conn = (DatabricksOutputConnection) connection; |
184 | | - DatabaseMetaData dbm = connection.getMetaData(); |
185 | | - String escape = dbm.getSearchStringEscape(); |
186 | | - |
187 | | - ResultSet rs = |
188 | | - dbm.getPrimaryKeys(conn.getCatalogName(), table.getSchemaName(), table.getTableName()); |
189 | | - final HashSet<String> primaryKeysBuilder = new HashSet<>(); |
190 | | - try { |
191 | | - while (rs.next()) { |
192 | | - if (!((DatabricksOutputConnection) connection) |
193 | | - .isAvailableTableMetadataInConnection(rs, table)) { |
194 | | - continue; |
195 | | - } |
196 | | - primaryKeysBuilder.add(rs.getString("COLUMN_NAME")); |
197 | | - } |
198 | | - } finally { |
199 | | - rs.close(); |
200 | | - } |
201 | | - final Set<String> primaryKeys = Collections.unmodifiableSet(primaryKeysBuilder); |
202 | | - |
203 | | - final ArrayList<JdbcColumn> builder = new ArrayList<>(); |
204 | | - // NOTE: Columns of TIMESTAMP_NTZ, INTERVAL are not included in getColumns result. |
205 | | - // This cause runtime sql exception when copy into. |
206 | | - // (probably because of unsupported in databricks jdbc) |
207 | | - // https://docs.databricks.com/en/sql/language-manual/data-types/interval-type.html |
208 | | - // https://docs.databricks.com/en/sql/language-manual/data-types/timestamp-ntz-type.html#notes |
209 | | - rs = |
210 | | - dbm.getColumns( |
211 | | - JdbcUtils.escapeSearchString(conn.getCatalogName(), escape), |
212 | | - JdbcUtils.escapeSearchString(table.getSchemaName(), escape), |
213 | | - JdbcUtils.escapeSearchString(table.getTableName(), escape), |
214 | | - null); |
215 | | - try { |
216 | | - while (rs.next()) { |
217 | | - if (!((DatabricksOutputConnection) connection) |
218 | | - .isAvailableTableMetadataInConnection(rs, table)) { |
219 | | - continue; |
220 | | - } |
221 | | - String columnName = rs.getString("COLUMN_NAME"); |
222 | | - String simpleTypeName = rs.getString("TYPE_NAME").toUpperCase(Locale.ENGLISH); |
223 | | - boolean isUniqueKey = primaryKeys.contains(columnName); |
224 | | - int sqlType = rs.getInt("DATA_TYPE"); |
225 | | - int colSize = rs.getInt("COLUMN_SIZE"); |
226 | | - int decDigit = rs.getInt("DECIMAL_DIGITS"); |
227 | | - if (rs.wasNull()) { |
228 | | - decDigit = -1; |
229 | | - } |
230 | | - int charOctetLength = rs.getInt("CHAR_OCTET_LENGTH"); |
231 | | - boolean isNotNull = "NO".equals(rs.getString("IS_NULLABLE")); |
232 | | - // rs.getString("COLUMN_DEF") // or null // TODO |
233 | | - builder.add( |
234 | | - JdbcColumn.newGenericTypeColumn( |
235 | | - columnName, |
236 | | - sqlType, |
237 | | - simpleTypeName, |
238 | | - colSize, |
239 | | - decDigit, |
240 | | - charOctetLength, |
241 | | - isNotNull, |
242 | | - isUniqueKey)); |
243 | | - // We can't get declared column name using JDBC API. |
244 | | - // Subclasses need to overwrite it. |
245 | | - } |
246 | | - } finally { |
247 | | - rs.close(); |
248 | | - } |
249 | | - final List<JdbcColumn> columns = Collections.unmodifiableList(builder); |
250 | | - if (columns.isEmpty()) { |
251 | | - return Optional.empty(); |
252 | | - } else { |
253 | | - return Optional.of(new JdbcSchema(columns)); |
254 | | - } |
| 174 | + return super.newJdbcSchemaFromTableIfExists( |
| 175 | + connection, |
| 176 | + ((DatabricksOutputConnection) connection).currentConnectionTableIdentifier(table)); |
255 | 177 | } |
256 | 178 | } |
0 commit comments