|
| 1 | +/* |
| 2 | + * Copyright © 2021 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; |
| 18 | + |
| 19 | +import io.cdap.e2e.utils.PluginPropertyUtils; |
| 20 | + |
| 21 | +import java.sql.Connection; |
| 22 | +import java.sql.DriverManager; |
| 23 | +import java.sql.ResultSet; |
| 24 | +import java.sql.SQLException; |
| 25 | +import java.sql.Statement; |
| 26 | + |
| 27 | +/** |
| 28 | + * MySQL client. |
| 29 | + */ |
| 30 | +public class MysqlClient { |
| 31 | + private static final String host = PluginPropertyUtils.pluginProp("host"); |
| 32 | + private static final int port = Integer.parseInt(PluginPropertyUtils.pluginProp("port")); |
| 33 | + private static final String database = PluginPropertyUtils.pluginProp("database"); |
| 34 | + |
| 35 | + private static Connection getMysqlConnection() throws SQLException, ClassNotFoundException { |
| 36 | + Class.forName("com.mysql.cj.jdbc.Driver"); |
| 37 | + String username = PluginPropertyUtils.pluginProp("username"); |
| 38 | + String password = PluginPropertyUtils.pluginProp("password"); |
| 39 | + return DriverManager.getConnection("jdbc:mysql://" + host + ":" + port + "/" + database, username, password); |
| 40 | + } |
| 41 | + |
| 42 | + public static int countRecord(String table) throws SQLException, ClassNotFoundException { |
| 43 | + String countQuery = "SELECT COUNT(*) as total FROM " + table; |
| 44 | + try (Connection connect = getMysqlConnection(); Statement statement = connect.createStatement(); |
| 45 | + ResultSet rs = statement.executeQuery(countQuery)) { |
| 46 | + int num = 0; |
| 47 | + while (rs.next()) { |
| 48 | + num = (rs.getInt(1)); |
| 49 | + } |
| 50 | + return num; |
| 51 | + } |
| 52 | + } |
| 53 | + |
| 54 | + public static void createSourceTable(String sourceTable) throws SQLException, ClassNotFoundException { |
| 55 | + try (Connection connect = getMysqlConnection(); Statement statement = connect.createStatement()) { |
| 56 | + String createSourceTableQuery = "CREATE TABLE IF NOT EXISTS " + sourceTable + |
| 57 | + "(id int, lastName varchar(255), PRIMARY KEY (id))"; |
| 58 | + statement.executeUpdate(createSourceTableQuery); |
| 59 | + |
| 60 | + // Truncate table to clean the data of last failure run. |
| 61 | + String truncateSourceTableQuery = "TRUNCATE TABLE " + sourceTable; |
| 62 | + statement.executeUpdate(truncateSourceTableQuery); |
| 63 | + |
| 64 | + // Insert dummy data. |
| 65 | + statement.executeUpdate("INSERT INTO " + sourceTable + " (id, lastName)" + |
| 66 | + "VALUES (1, 'Simpson')"); |
| 67 | + statement.executeUpdate("INSERT INTO " + sourceTable + " (id, lastName)" + |
| 68 | + "VALUES (2, 'McBeal')"); |
| 69 | + statement.executeUpdate("INSERT INTO " + sourceTable + " (id, lastName)" + |
| 70 | + "VALUES (3, 'Flinstone')"); |
| 71 | + } |
| 72 | + } |
| 73 | + |
| 74 | + public static void createTargetTable(String targetTable) throws SQLException, ClassNotFoundException { |
| 75 | + try (Connection connect = getMysqlConnection(); Statement statement = connect.createStatement()) { |
| 76 | + String createTargetTableQuery = "CREATE TABLE IF NOT EXISTS " + targetTable + |
| 77 | + "(id int, lastName varchar(255), PRIMARY KEY (id))"; |
| 78 | + statement.executeUpdate(createTargetTableQuery); |
| 79 | + // Truncate table to clean the data of last failure run. |
| 80 | + String truncateTargetTableQuery = "TRUNCATE TABLE " + targetTable; |
| 81 | + statement.executeUpdate(truncateTargetTableQuery); |
| 82 | + } |
| 83 | + } |
| 84 | + |
| 85 | + public static void dropTables(String[] tables) throws SQLException, ClassNotFoundException { |
| 86 | + try (Connection connect = getMysqlConnection(); Statement statement = connect.createStatement()) { |
| 87 | + for (String table : tables) { |
| 88 | + String dropTableQuery = "Drop Table " + table; |
| 89 | + statement.executeUpdate(dropTableQuery); |
| 90 | + } |
| 91 | + } |
| 92 | + } |
| 93 | +} |
0 commit comments