From 28834579646146a4daab4542fa141effd2e8c736 Mon Sep 17 00:00:00 2001 From: qianmoQ Date: Sun, 30 Jul 2023 16:20:22 +0800 Subject: [PATCH] [Plugin] [MySQL] Remove adapter --- .../plugin/jdbc/mysql/MySQLAdapter.java | 124 +----------------- 1 file changed, 4 insertions(+), 120 deletions(-) diff --git a/plugin/datacap-jdbc-mysql/src/main/java/io/edurt/datacap/plugin/jdbc/mysql/MySQLAdapter.java b/plugin/datacap-jdbc-mysql/src/main/java/io/edurt/datacap/plugin/jdbc/mysql/MySQLAdapter.java index 0f37a4bf..557c7334 100644 --- a/plugin/datacap-jdbc-mysql/src/main/java/io/edurt/datacap/plugin/jdbc/mysql/MySQLAdapter.java +++ b/plugin/datacap-jdbc-mysql/src/main/java/io/edurt/datacap/plugin/jdbc/mysql/MySQLAdapter.java @@ -1,129 +1,13 @@ package io.edurt.datacap.plugin.jdbc.mysql; -import com.google.common.collect.Lists; import io.edurt.datacap.spi.adapter.JdbcAdapter; -import io.edurt.datacap.spi.column.JdbcColumn; -import io.edurt.datacap.spi.connection.JdbcConfigure; import io.edurt.datacap.spi.connection.JdbcConnection; -import io.edurt.datacap.spi.model.Response; -import io.edurt.datacap.spi.model.Time; -import lombok.extern.slf4j.Slf4j; -import java.sql.*; -import java.util.ArrayList; -import java.util.Arrays; -import java.util.Date; -import java.util.List; -import java.util.Objects; - - -@Slf4j public class MySQLAdapter - extends JdbcAdapter { - public MySQLAdapter(JdbcConnection jdbcConnection) { + extends JdbcAdapter +{ + public MySQLAdapter(JdbcConnection jdbcConnection) + { super(jdbcConnection); } - - @Override - public Response handlerExecute(String content) { - Time processorTime = new Time(); - processorTime.setStart(new Date().getTime()); - Response response = this.jdbcConnection.getResponse(); - Connection connection = (Connection) this.jdbcConnection.getConnection(); - JdbcConfigure configure = (JdbcConfigure) this.jdbcConnection.getConfigure(); - if (response.getIsConnected()) { - try (PreparedStatement statement = connection.prepareStatement(content)) { - List headers = new ArrayList<>(); - List types = new ArrayList<>(); - List columns = new ArrayList<>(); - // Split SQL - List setBuffer = Lists.newArrayList(); - List buffer = Lists.newArrayList(); - // TODO Each line of code is required to have a semicolon; this is different from scala syntax without semicolons - Arrays.stream(content.split(";")) - .forEach(line -> { - if (line.trim().toUpperCase().startsWith("SET")) { - setBuffer.add(line); - } else { - buffer.add(line); - } - }); - // Execute SET - for (String sql : setBuffer) { - statement.execute(sql); - } - Statement statement1 = connection.createStatement(); - Boolean isSelect = false; - ResultSet resultSet = null; - for (String sql : buffer) { - if (sql.trim().toUpperCase().startsWith("SELECT") || sql.trim().toUpperCase().startsWith("SHOW")) { - resultSet = statement1.executeQuery(sql); - isSelect = true; - // If it is multiple result sets, return the first result set - break; - } - // High-risk operations such as delete 、truncate are not allowed! - if (sql.trim().toUpperCase().startsWith("INSERT")) { - statement1.executeUpdate(sql); - isSelect = true; - // If it is multiple result sets, return the first result set - break; - } - statement1.addBatch(sql); - } - if (!isSelect) { - statement1.executeBatch(); - resultSet = statement1.getResultSet(); - } - statement1.close(); - try { - // Reset query SQL - boolean isPresent = true; - JdbcColumn jdbcColumn = new JdbcColumn(resultSet); - while (resultSet.next()) { - ResultSetMetaData metaData = resultSet.getMetaData(); - int columnCount = metaData.getColumnCount(); - List _columns = new ArrayList<>(); - for (int i = 1; i <= columnCount; i++) { - if (isPresent) { - headers.add(metaData.getColumnName(i)); - types.add(metaData.getColumnTypeName(i)); - } - _columns.add(jdbcColumn.convert(metaData.getColumnTypeName(i), i)); - } - isPresent = false; - columns.add(handlerFormatter(configure.getFormat(), headers, _columns)); - } - } catch (SQLException tryUpdateEx) { - if (Objects.equals(tryUpdateEx.getSQLState(), "C1999")) { - try { - headers.add("result"); - types.add(Integer.class.getSimpleName()); - List _columns = new ArrayList<>(); - _columns.add(statement.executeUpdate()); - columns.add(handlerFormatter(configure.getFormat(), headers, _columns)); - } catch (SQLException updateEx) { - throw new SQLException(updateEx); - } - } else { - throw new SQLException(tryUpdateEx); - } - } finally { - response.setHeaders(headers); - response.setTypes(types); - response.setColumns(columns); - response.setIsSuccessful(Boolean.TRUE); - } - } catch (SQLException ex) { - log.error("Execute content failed content {} exception ", content, ex); - response.setIsSuccessful(Boolean.FALSE); - response.setMessage(ex.getMessage()); - } - } - processorTime.setEnd(new Date().getTime()); - response.setProcessor(processorTime); - // It will be destroyed after the mission is closed - this.jdbcConnection.destroy(); - return response; - } }