Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: increase query table data (issue #82) #149

Merged
merged 1 commit into from
Sep 2, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,15 @@

package com.cloudorc.solidui.plugin.jdbc;

import java.sql.*;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;

public class DorisClient extends BaseJdbcClient {

Expand All @@ -29,7 +35,12 @@ public DorisClient(Connection conn) {
}

@Override
public List<String> getAllDatabase() throws SQLException{
public String generateSelectAllDataSql(String database, String tableName) {
return "SELECT * FROM " + tableName;
}

@Override
public List<String> getAllDatabase() throws SQLException {
List<String> dataBaseName = new ArrayList<>();
Statement stmt = null;
ResultSet rs = null;
Expand All @@ -47,7 +58,7 @@ public List<String> getAllDatabase() throws SQLException{
}

@Override
public List<String> getAllTables(String database) throws SQLException{
public List<String> getAllTables(String database) throws SQLException {
List<String> tableNames = new ArrayList<>();
Statement stmt = null;
ResultSet rs = null;
Expand All @@ -64,7 +75,7 @@ public List<String> getAllTables(String database) throws SQLException{
}

@Override
public List<List<String>> getSelectResult(String sql) throws SQLException{
public List<List<String>> getSelectResult(String sql) throws SQLException {
PreparedStatement pstmt = null;
ResultSet rs = null;
List<List<String>> lists = new ArrayList<>();
Expand All @@ -83,7 +94,7 @@ public List<List<String>> getSelectResult(String sql) throws SQLException{
while (rs.next()) {
List<String> stringArrayList = new ArrayList<>();
for (int i = 1; i < columnCount + 1; i++) {
stringArrayList.add(rs.getString(i).trim());
stringArrayList.add(Optional.ofNullable(rs.getString(i)).map(String::trim).orElse(null));
}
lists.add(stringArrayList);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
import java.sql.Statement;


public abstract class BaseJdbcClient implements JdbcClient{
public abstract class BaseJdbcClient implements JdbcClient {

public final Logger logger = LoggerFactory.getLogger(this.getClass());

Expand Down Expand Up @@ -55,8 +55,9 @@ public void closeResource(Connection connection, Statement statement, ResultSet

@Override
public void close() throws IOException {
closeResource(conn,null,null);
closeResource(conn, null, null);
}

@Override
public Connection getConn() {
return conn;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,11 @@

public interface JdbcClient {

/**
* Generates a SQL query string to select all data from a specified table.
*/
String generateSelectAllDataSql(String database, String tableName);

List<String> getAllDatabase() throws SQLException;

List<String> getAllTables(String database) throws SQLException ;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,15 @@

package com.cloudorc.solidui.plugin.jdbc;

import java.sql.*;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;

public class MysqlClient extends BaseJdbcClient {

Expand All @@ -29,7 +35,12 @@ public MysqlClient(Connection conn) {
}

@Override
public List<String> getAllDatabase() throws SQLException{
public String generateSelectAllDataSql(String database, String tableName) {
return "SELECT * FROM " + tableName;
}

@Override
public List<String> getAllDatabase() throws SQLException {
List<String> dataBaseName = new ArrayList<>();
Statement stmt = null;
ResultSet rs = null;
Expand All @@ -47,7 +58,7 @@ public List<String> getAllDatabase() throws SQLException{
}

@Override
public List<String> getAllTables(String database) throws SQLException{
public List<String> getAllTables(String database) throws SQLException {
List<String> tableNames = new ArrayList<>();
Statement stmt = null;
ResultSet rs = null;
Expand All @@ -64,7 +75,7 @@ public List<String> getAllTables(String database) throws SQLException{
}

@Override
public List<List<String>> getSelectResult(String sql) throws SQLException{
public List<List<String>> getSelectResult(String sql) throws SQLException {
PreparedStatement pstmt = null;
ResultSet rs = null;
List<List<String>> lists = new ArrayList<>();
Expand All @@ -82,7 +93,7 @@ public List<List<String>> getSelectResult(String sql) throws SQLException{
while (rs.next()) {
List<String> stringArrayList = new ArrayList<>();
for (int i = 1; i < columnCount + 1; i++) {
stringArrayList.add(rs.getString(i).trim());
stringArrayList.add(Optional.ofNullable(rs.getString(i)).map(String::trim).orElse(null));
}
lists.add(stringArrayList);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,18 @@
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;

import static com.cloudorc.solidui.entrance.enums.Status.*;
import static com.cloudorc.solidui.entrance.enums.Status.QUERY_METADATA_DB_ERROR;
import static com.cloudorc.solidui.entrance.enums.Status.QUERY_METADATA_SQL_ERROR;
import static com.cloudorc.solidui.entrance.enums.Status.QUERY_METADATA_TABLE_ERROR;

@Api(tags = "metadata_query")
@RestController
Expand All @@ -51,7 +58,7 @@ public Result getDatabases(
@RequestParam("dataSourceName") String dataSourceName,
@RequestParam("typeName") String typeName,
HttpServletRequest request) {
return metadataQueryService.queryDatabasesByDsName(dataSourceName,typeName);
return metadataQueryService.queryDatabasesByDsName(dataSourceName, typeName);

}

Expand All @@ -69,7 +76,25 @@ public Result getTables(
@RequestParam("database") String database,
@RequestParam("typeName") String typeName,
HttpServletRequest request) {
return metadataQueryService.queryTablesByDsName(dataSourceName,database,typeName);
return metadataQueryService.queryTablesByDsName(dataSourceName, database, typeName);
}

@ApiOperation(value = "query table data")
@ApiImplicitParams({
@ApiImplicitParam(name = "dataSourceName", required = true, dataType = "String", value = "data source name"),
@ApiImplicitParam(name = "typeName", required = true, dataType = "String", value = "typeName"),
@ApiImplicitParam(name = "database", required = true, dataType = "String", value = "database"),
@ApiImplicitParam(name = "tableName", required = true, dataType = "String", value = "tableName")
})
@ResponseStatus(HttpStatus.OK)
@ApiException(QUERY_METADATA_SQL_ERROR)
@GetMapping(value = "/queryTableData")
public Result getTables(
@RequestParam String dataSourceName,
@RequestParam String database,
@RequestParam String typeName,
@RequestParam String tableName) {
return metadataQueryService.queryTableData(dataSourceName, database, typeName, tableName);
}

@ApiOperation(value = "querySql", notes = "query sql")
Expand All @@ -86,10 +111,9 @@ public Result querySelectSql(
@RequestParam("sql") String sql,
@RequestParam("typeName") String typeName,
HttpServletRequest request) {
return metadataQueryService.queryBySql(dataSourceName,sql,typeName);
return metadataQueryService.queryBySql(dataSourceName, sql, typeName);
}


@ApiOperation(value = "querySql/id", notes = "query sql")
@ApiImplicitParams({
@ApiImplicitParam(name = "dataSourceId", required = true, dataType = "Long", value = "data source id"),
Expand All @@ -104,7 +128,7 @@ public Result querySelectSql(
@RequestParam("sql") String sql,
@RequestParam("typeId") Long typeId,
HttpServletRequest request) {
return metadataQueryService.queryBySql(dataSourceId,sql,typeId);
return metadataQueryService.queryBySql(dataSourceId, sql, typeId);
}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,4 +31,5 @@ public interface MetadataQueryService {

Result queryConnection(String dataSourceName,String typeName);

Result queryTableData(String dataSourceName, String database, String typeName, String tableName);
}
Loading
Loading