|
| 1 | +/* |
| 2 | + * Copyright © 2020 Cask Data, Inc. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); you may not |
| 5 | + * use this file except in compliance with the License. You may obtain a copy of |
| 6 | + * the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 12 | + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the |
| 13 | + * License for the specific language governing permissions and limitations under |
| 14 | + * the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package io.cdap.plugin.db.batch.action; |
| 18 | + |
| 19 | +import io.cdap.cdap.etl.api.FailureCollector; |
| 20 | +import io.cdap.cdap.etl.api.PipelineConfigurer; |
| 21 | +import io.cdap.cdap.etl.api.StageConfigurer; |
| 22 | +import io.cdap.cdap.etl.api.action.Action; |
| 23 | +import io.cdap.cdap.etl.api.action.ActionContext; |
| 24 | +import io.cdap.cdap.etl.api.action.SettableArguments; |
| 25 | +import io.cdap.plugin.db.ConnectionConfig; |
| 26 | +import io.cdap.plugin.util.DBUtils; |
| 27 | +import io.cdap.plugin.util.DriverCleanup; |
| 28 | + |
| 29 | +import java.sql.Connection; |
| 30 | +import java.sql.Driver; |
| 31 | +import java.sql.DriverManager; |
| 32 | +import java.sql.ResultSet; |
| 33 | +import java.sql.SQLException; |
| 34 | +import java.sql.Statement; |
| 35 | +import java.util.Properties; |
| 36 | + |
| 37 | +/** |
| 38 | + * Action that converts db column into pipeline argument. |
| 39 | + */ |
| 40 | +public class AbstractDBArgumentSetter extends Action { |
| 41 | + |
| 42 | + private static final String JDBC_PLUGIN_ID = "driver"; |
| 43 | + private final ArgumentSetterConfig config; |
| 44 | + |
| 45 | + public AbstractDBArgumentSetter(ArgumentSetterConfig config) { |
| 46 | + this.config = config; |
| 47 | + } |
| 48 | + |
| 49 | + @Override |
| 50 | + public void run(ActionContext context) throws Exception { |
| 51 | + Class<? extends Driver> driverClass = context.loadPluginClass(JDBC_PLUGIN_ID); |
| 52 | + FailureCollector failureCollector = context.getFailureCollector(); |
| 53 | + SettableArguments settableArguments = context.getArguments(); |
| 54 | + processArguments(driverClass, failureCollector, settableArguments); |
| 55 | + } |
| 56 | + |
| 57 | + @Override |
| 58 | + public void configurePipeline(PipelineConfigurer pipelineConfigurer) |
| 59 | + throws IllegalArgumentException { |
| 60 | + DBUtils.validateJDBCPluginPipeline(pipelineConfigurer, config, JDBC_PLUGIN_ID); |
| 61 | + Class<? extends Driver> driverClass = DBUtils.getDriverClass( |
| 62 | + pipelineConfigurer, config, ConnectionConfig.JDBC_PLUGIN_TYPE); |
| 63 | + StageConfigurer stageConfigurer = pipelineConfigurer.getStageConfigurer(); |
| 64 | + FailureCollector collector = stageConfigurer.getFailureCollector(); |
| 65 | + config.validate(collector); |
| 66 | + try { |
| 67 | + processArguments(driverClass, collector, null); |
| 68 | + } catch (SQLException e) { |
| 69 | + collector.addFailure("SQL error while executing query: " + e.getMessage(), null) |
| 70 | + .withStacktrace(e.getStackTrace()); |
| 71 | + } catch (IllegalAccessException | InstantiationException e) { |
| 72 | + collector.addFailure("Unable to instantiate JDBC driver: " + e.getMessage(), null) |
| 73 | + .withStacktrace(e.getStackTrace()); |
| 74 | + } catch (Exception e) { |
| 75 | + collector.addFailure(e.getMessage(), null).withStacktrace(e.getStackTrace()); |
| 76 | + } |
| 77 | + } |
| 78 | + |
| 79 | + /** |
| 80 | + * Creates connection to database. Reads row from database based on selection conditions and |
| 81 | + * depending on whether settable arguments is provided or not set the argument from row columns. |
| 82 | + * |
| 83 | + * @param driverClass {@link Class<? extends Driver>} |
| 84 | + * @param failureCollector {@link FailureCollector} |
| 85 | + * @param settableArguments {@link SettableArguments} |
| 86 | + * @throws SQLException is raised when there is sql related exception |
| 87 | + * @throws IllegalAccessException is raised when there is access related exception |
| 88 | + * @throws InstantiationException is raised when there is class/driver issue |
| 89 | + */ |
| 90 | + private void processArguments(Class<? extends Driver> driverClass, |
| 91 | + FailureCollector failureCollector, SettableArguments settableArguments) |
| 92 | + throws SQLException, IllegalAccessException, InstantiationException { |
| 93 | + DriverCleanup driverCleanup; |
| 94 | + |
| 95 | + driverCleanup = DBUtils.ensureJDBCDriverIsAvailable(driverClass, config.getConnectionString(), |
| 96 | + config.jdbcPluginName); |
| 97 | + Properties connectionProperties = new Properties(); |
| 98 | + connectionProperties.putAll(config.getConnectionArguments()); |
| 99 | + try { |
| 100 | + Connection connection = DriverManager |
| 101 | + .getConnection(config.getConnectionString(), connectionProperties); |
| 102 | + Statement statement = connection.createStatement(); |
| 103 | + ResultSet resultSet = statement.executeQuery(config.getQuery()); |
| 104 | + boolean hasRecord = resultSet.next(); |
| 105 | + if (!hasRecord) { |
| 106 | + failureCollector.addFailure("No record found.", |
| 107 | + "The argument selection conditions must match only one record."); |
| 108 | + return; |
| 109 | + } |
| 110 | + if (settableArguments != null) { |
| 111 | + setArguments(resultSet, failureCollector, settableArguments); |
| 112 | + } |
| 113 | + if (resultSet.next()) { |
| 114 | + failureCollector |
| 115 | + .addFailure("More than one records found.", |
| 116 | + "The argument selection conditions must match only one record."); |
| 117 | + } |
| 118 | + } finally { |
| 119 | + driverCleanup.destroy(); |
| 120 | + } |
| 121 | + } |
| 122 | + |
| 123 | + /** |
| 124 | + * Converts column from jdbc results set into pipeline arguments |
| 125 | + * |
| 126 | + * @param resultSet - result set from db {@link ResultSet} |
| 127 | + * @param failureCollector - context failure collector @{link FailureCollector} |
| 128 | + * @param arguments - context argument setter {@link SettableArguments} |
| 129 | + * @throws SQLException - raises {@link SQLException} when configuration is not valid |
| 130 | + */ |
| 131 | + private void setArguments(ResultSet resultSet, FailureCollector failureCollector, |
| 132 | + SettableArguments arguments) throws SQLException { |
| 133 | + String[] columns = config.getArgumentsColumns().split(","); |
| 134 | + for (String column : columns) { |
| 135 | + arguments.set(column, resultSet.getString(column)); |
| 136 | + } |
| 137 | + } |
| 138 | +} |
0 commit comments