mirror of
https://github.com/WeiYe-Jing/datax-web.git
synced 2026-07-03 21:08:58 +08:00
update: 优化代码,去掉一些没用的方法;
This commit is contained in:
parent
6899247591
commit
8dbecfc43e
@ -12,7 +12,7 @@ public abstract class BaseDatabaseMeta implements DatabaseInterface {
|
||||
|
||||
@Override
|
||||
public String getSQLQueryFields(String tableName) {
|
||||
return "SELECT * FROM " + tableName;
|
||||
return "SELECT * FROM " + tableName + " where 1=0";
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
@ -10,13 +10,6 @@ public interface DatabaseInterface {
|
||||
*/
|
||||
public String getSQLQueryFields(String tableName);
|
||||
|
||||
/**
|
||||
* 获取表和字段注释的sql语句
|
||||
*
|
||||
* @return The SQL to launch.
|
||||
*/
|
||||
public String getSQLQueryComment(String schemaName, String tableName, String columnName);
|
||||
|
||||
/**
|
||||
* 获取主键字段
|
||||
*
|
||||
@ -24,20 +17,31 @@ public interface DatabaseInterface {
|
||||
*/
|
||||
public String getSQLQueryPrimaryKey();
|
||||
|
||||
public String getSQLQueryTableNameComment();
|
||||
|
||||
/**
|
||||
* 根据schemaName获取所有的表名和注释
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
public String getSQLQueryTablesNameComments();
|
||||
|
||||
/**
|
||||
* 根据schemaName tableName 获取所有的表名和注释
|
||||
* 获取所有表名的sql
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
public String getSQLQueryTableNameComment();
|
||||
public String getSQLQueryTables(String... args);
|
||||
|
||||
|
||||
/**
|
||||
* 获取所有的字段的sql
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
public String getSQLQueryColumns(String... args);
|
||||
|
||||
/**
|
||||
* 获取表和字段注释的sql语句
|
||||
*
|
||||
* @return The SQL to launch.
|
||||
*/
|
||||
public String getSQLQueryComment(String schemaName, String tableName, String columnName);
|
||||
|
||||
// /**
|
||||
// * 查询表名所有字段信息
|
||||
|
||||
@ -23,12 +23,6 @@ public class MySQLDatabaseMeta extends BaseDatabaseMeta implements DatabaseInter
|
||||
return single;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getSQLQueryFields(String tableName) {
|
||||
return "SELECT * FROM " + tableName + " LIMIT 0";
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public String getSQLQueryComment(String schemaName, String tableName, String columnName) {
|
||||
return String.format("SELECT COLUMN_COMMENT FROM information_schema.COLUMNS where TABLE_SCHEMA = '%s' and TABLE_NAME = '%s' and COLUMN_NAME = '%s'", schemaName, tableName, columnName);
|
||||
@ -38,4 +32,14 @@ public class MySQLDatabaseMeta extends BaseDatabaseMeta implements DatabaseInter
|
||||
public String getSQLQueryPrimaryKey() {
|
||||
return "select column_name from information_schema.columns where table_schema=? and table_name=? and column_key = 'PRI'";
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getSQLQueryTables(String... args) {
|
||||
return "select table_name from information_schema.tables where table_schema=?";
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getSQLQueryColumns(String... args) {
|
||||
return "select column_name from information_schema.columns where table_schema=? and table_name=?";
|
||||
}
|
||||
}
|
||||
|
||||
@ -23,11 +23,6 @@ public class OracleDatabaseMeta extends BaseDatabaseMeta implements DatabaseInte
|
||||
return single;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getSQLQueryFields(String tableName) {
|
||||
return "SELECT * FROM " + tableName + " where 1=0";
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public String getSQLQueryComment(String schemaName, String tableName, String columnName) {
|
||||
@ -53,4 +48,14 @@ public class OracleDatabaseMeta extends BaseDatabaseMeta implements DatabaseInte
|
||||
public String getSQLQueryTableNameComment() {
|
||||
return "select table_name,comments from user_tab_comments where table_name = ?";
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getSQLQueryTables(String... args) {
|
||||
return "select table_name from user_tab_comments";
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getSQLQueryColumns(String... args) {
|
||||
return "select table_name,comments from user_tab_comments where table_name = ?";
|
||||
}
|
||||
}
|
||||
|
||||
@ -152,11 +152,11 @@ public abstract class BaseQueryTool implements QueryToolInterface {
|
||||
|
||||
@Override
|
||||
public List<Map<String, Object>> getTables() {
|
||||
String sqlQueryTablesNameComments = sqlBuilder.getSQLQueryTablesNameComments();
|
||||
logger.info(sqlQueryTablesNameComments);
|
||||
String sqlQueryTables = sqlBuilder.getSQLQueryTables();
|
||||
logger.info(sqlQueryTables);
|
||||
List<Map<String, Object>> res = null;
|
||||
try {
|
||||
res = JdbcUtils.executeQuery(connection, sqlQueryTablesNameComments, ImmutableList.of(currentSchema));
|
||||
res = JdbcUtils.executeQuery(connection, sqlQueryTables, ImmutableList.of(currentSchema));
|
||||
} catch (SQLException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
@ -251,42 +251,30 @@ public abstract class BaseQueryTool implements QueryToolInterface {
|
||||
return res;
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public List<String> getColumnNames(String tableName) {
|
||||
|
||||
List<String> columns = Lists.newArrayList();
|
||||
|
||||
//获取查询指定表所有字段的sql语句
|
||||
String querySql = sqlBuilder.getSQLQueryFields(tableName);
|
||||
logger.info("querySql: {}", querySql);
|
||||
|
||||
List<String> res = Lists.newArrayList();
|
||||
//获取指定表的所有字段
|
||||
try {
|
||||
//获取查询指定表所有字段的sql语句
|
||||
String querySql = sqlBuilder.getSQLQueryFields(tableName);
|
||||
logger.info("querySql: {}", querySql);
|
||||
|
||||
//获取所有字段
|
||||
Statement statement = connection.createStatement();
|
||||
ResultSet resultSet = statement.executeQuery(querySql);
|
||||
ResultSetMetaData metaData = resultSet.getMetaData();
|
||||
|
||||
int columnCount = metaData.getColumnCount();
|
||||
for (int i = 1; i <= columnCount; i++) {
|
||||
columns.add(metaData.getColumnName(i));
|
||||
res.add(metaData.getColumnName(i));
|
||||
}
|
||||
// logger.info("res: ");
|
||||
// res.forEach(e -> logger.info(e.toString()));
|
||||
} catch (SQLException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
return columns;
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<String> getTableNames() {
|
||||
List<String> res = Lists.newArrayList();
|
||||
List<Map<String, Object>> tables = getTables();
|
||||
//这里只取表名
|
||||
tables.forEach(e -> {
|
||||
//表名,注释
|
||||
List tValues = new ArrayList(e.values());
|
||||
//第一个总是表名
|
||||
res.add((String) tValues.get(0));
|
||||
});
|
||||
return res;
|
||||
}
|
||||
}
|
||||
|
||||
@ -1,8 +1,14 @@
|
||||
package com.wugui.tool.query;
|
||||
|
||||
import com.alibaba.druid.util.JdbcUtils;
|
||||
import com.google.common.collect.ImmutableList;
|
||||
import com.google.common.collect.Lists;
|
||||
import com.wugui.dataxweb.entity.JobJdbcDatasource;
|
||||
|
||||
import java.sql.SQLException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* mysql数据库使用的查询工具
|
||||
@ -18,4 +24,23 @@ public class MySQLQueryTool extends BaseQueryTool implements QueryToolInterface
|
||||
super(codeJdbcDatasource);
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<String> getTableNames() {
|
||||
List<String> res = Lists.newArrayList();
|
||||
//获取sql
|
||||
String sqlQueryTables = sqlBuilder.getSQLQueryTables();
|
||||
logger.info(sqlQueryTables);
|
||||
//查询
|
||||
try {
|
||||
List<Map<String, Object>> maps = JdbcUtils.executeQuery(connection, sqlQueryTables, ImmutableList.of(getSchema()));
|
||||
// // 只取value即可
|
||||
maps.forEach((k) -> {
|
||||
String tName = (String) new ArrayList<>(k.values()).get(0);
|
||||
res.add(tName);
|
||||
});
|
||||
} catch (SQLException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
return res;
|
||||
}
|
||||
}
|
||||
|
||||
@ -6,6 +6,7 @@ import com.google.common.collect.Lists;
|
||||
import com.wugui.dataxweb.entity.JobJdbcDatasource;
|
||||
|
||||
import java.sql.SQLException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
@ -48,4 +49,27 @@ public class OracleQueryTool extends BaseQueryTool implements QueryToolInterface
|
||||
}
|
||||
return res;
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<String> getTableNames() {
|
||||
{
|
||||
List<String> res = Lists.newArrayList();
|
||||
//获取sql
|
||||
String sqlQueryTables = sqlBuilder.getSQLQueryTables();
|
||||
logger.info(sqlQueryTables);
|
||||
//查询
|
||||
try {
|
||||
List<Map<String, Object>> maps = JdbcUtils.executeQuery(connection, sqlQueryTables, new ArrayList<>());
|
||||
// logger.info(maps.toString());
|
||||
// // 只取value即可
|
||||
maps.forEach((k) -> {
|
||||
String tName = (String) new ArrayList<>(k.values()).get(0);
|
||||
res.add(tName);
|
||||
});
|
||||
} catch (SQLException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
return res;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@ -1,13 +1,11 @@
|
||||
package com.wugui.tool.query;
|
||||
|
||||
import com.wugui.dataxweb.entity.JobJdbcDatasource;
|
||||
import com.wugui.tool.database.TableInfo;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
@Slf4j
|
||||
public class MySQLQueryToolTest {
|
||||
@ -30,35 +28,15 @@ public class MySQLQueryToolTest {
|
||||
jdbcDatasource.setJdbcDriverClass("com.mysql.jdbc.Driver");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void getTableInfo() {
|
||||
List<Map<String, Object>> tableInfo = queryTool.getTableInfo("datax_plugin");
|
||||
tableInfo.forEach(e -> {
|
||||
log.info(e.toString());
|
||||
});
|
||||
}
|
||||
|
||||
@Test
|
||||
public void buildTableInfo() {
|
||||
TableInfo tableInfo = queryTool.buildTableInfo("datax_plugin");
|
||||
log.info(tableInfo.toString());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void getTables() {
|
||||
List<Map<String, Object>> tables = queryTool.getTables();
|
||||
tables.forEach(e -> log.info(e.toString()));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void getColumns() {
|
||||
List<String> columns = queryTool.getColumnNames("datax_plugin");
|
||||
log.info(columns.toString());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void getTableNames() {
|
||||
List<String> tableNames = queryTool.getTableNames();
|
||||
tableNames.forEach(System.out::println);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void getColumnNames() {
|
||||
List<String> columns = queryTool.getColumnNames("datax_plugin");
|
||||
log.info(columns.toString());
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,42 @@
|
||||
package com.wugui.tool.query;
|
||||
|
||||
import com.wugui.dataxweb.entity.JobJdbcDatasource;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
@Slf4j
|
||||
public class OracleQueryToolTest {
|
||||
|
||||
private BaseQueryTool queryTool;
|
||||
private JobJdbcDatasource jdbcDatasource;
|
||||
|
||||
@Before
|
||||
public void before() {
|
||||
genMysqlDemo();
|
||||
queryTool = QueryToolFactory.getByDbType(jdbcDatasource);
|
||||
}
|
||||
|
||||
private void genMysqlDemo() {
|
||||
jdbcDatasource = new JobJdbcDatasource();
|
||||
jdbcDatasource.setDatasourceName("test");
|
||||
jdbcDatasource.setJdbcUsername("scott");
|
||||
jdbcDatasource.setJdbcPassword("tiger");
|
||||
jdbcDatasource.setJdbcUrl("jdbc:oracle:thin:@localhost:1521/orcl");
|
||||
jdbcDatasource.setJdbcDriverClass("oracle.jdbc.OracleDriver");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void getTableNames() {
|
||||
List<String> tableNames = queryTool.getTableNames();
|
||||
tableNames.forEach(System.out::println);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void getColumnNames() {
|
||||
List<String> columns = queryTool.getColumnNames("EMP");
|
||||
log.info(columns.toString());
|
||||
}
|
||||
}
|
||||
Loading…
Reference in New Issue
Block a user