db-data-export

This commit is contained in:
zgq
2024-03-28 13:13:09 +08:00
parent fc9ce84773
commit f425604b15
27 changed files with 520 additions and 20 deletions

View File

@ -122,4 +122,6 @@ public interface DBManage {
void updateProcedure(Connection connection, @NotEmpty String databaseName, String schemaName, @NotNull Procedure procedure) throws SQLException;
String exportDatabase(Connection connection, String databaseName, String schemaName,boolean containData) throws SQLException;
String exportDatabaseData(Connection connection, String databaseName, String schemaName,String tableName) throws SQLException;
}

View File

@ -54,6 +54,16 @@ public interface MetaData {
*/
List<Table> tables(Connection connection, @NotEmpty String databaseName, String schemaName, String tableName);
/**
* Querying all table name under a schema.
* @param connection
* @param databaseName
* @param schemaName
* @param tableName
* @return
*/
List<String> tableNames(Connection connection, @NotEmpty String databaseName, String schemaName, String tableName);
/**
* Querying all table under a schema.
@ -79,6 +89,14 @@ public interface MetaData {
Table view(Connection connection, @NotEmpty String databaseName, String schemaName, String viewName);
/** query view names
* @param connection
* @param databaseName
* @param schemaName
* @return
*/
List<String> viewNames(Connection connection, @NotEmpty String databaseName, String schemaName);
/**
* Querying all views under a schema.
*

View File

@ -142,7 +142,10 @@ public class DefaultDBManage implements DBManage {
public String exportDatabase(Connection connection, String databaseName, String schemaName, boolean containData) throws SQLException {
return null;
}
@Override
public String exportDatabaseData(Connection connection, String databaseName, String schemaName,String tableName) throws SQLException {
return null;
}
@Override
public void dropTable(Connection connection, String databaseName, String schemaName, String tableName) {

View File

@ -1,10 +1,5 @@
package ai.chat2db.spi.jdbc;
import java.sql.Connection;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;
import ai.chat2db.server.tools.base.wrapper.result.PageResult;
import ai.chat2db.spi.CommandExecutor;
import ai.chat2db.spi.MetaData;
@ -16,6 +11,11 @@ import com.google.common.collect.Lists;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import java.sql.Connection;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;
/**
* @author jipengfei
* @version : DefaultMetaService.java
@ -29,9 +29,9 @@ public class DefaultMetaService implements MetaData {
@Override
public List<Schema> schemas(Connection connection, String databaseName) {
List<Schema> schemas = SQLExecutor.getInstance().schemas(connection, databaseName, null);
if(StringUtils.isNotBlank(databaseName) && CollectionUtils.isNotEmpty(schemas)){
for ( Schema schema : schemas) {
if(StringUtils.isBlank(schema.getDatabaseName())){
if (StringUtils.isNotBlank(databaseName) && CollectionUtils.isNotEmpty(schemas)) {
for (Schema schema : schemas) {
if (StringUtils.isBlank(schema.getDatabaseName())) {
schema.setDatabaseName(databaseName);
}
}
@ -46,14 +46,19 @@ public class DefaultMetaService implements MetaData {
@Override
public List<Table> tables(Connection connection, String databaseName, String schemaName, String tableName) {
return SQLExecutor.getInstance().tables(connection, StringUtils.isEmpty(databaseName) ? null : databaseName, StringUtils.isEmpty(schemaName) ? null : schemaName, tableName, new String[]{"TABLE","SYSTEM TABLE"});
return SQLExecutor.getInstance().tables(connection, StringUtils.isEmpty(databaseName) ? null : databaseName, StringUtils.isEmpty(schemaName) ? null : schemaName, tableName, new String[]{"TABLE", "SYSTEM TABLE"});
}
@Override
public List<String> tableNames(Connection connection, String databaseName, String schemaName, String tableName) {
return SQLExecutor.getInstance().tableNames(connection, StringUtils.isEmpty(databaseName) ? null : databaseName, StringUtils.isEmpty(schemaName) ? null : schemaName, tableName, new String[]{"TABLE", "SYSTEM TABLE"});
}
@Override
public PageResult<Table> tables(Connection connection, String databaseName, String schemaName, String tableNamePattern, int pageNo, int pageSize) {
List<Table> tables = tables(connection, databaseName, schemaName, tableNamePattern);
if(CollectionUtils.isEmpty(tables)){
return PageResult.of(tables,0L,pageNo, pageSize);
if (CollectionUtils.isEmpty(tables)) {
return PageResult.of(tables, 0L, pageNo, pageSize);
}
List result = tables.stream().skip((pageNo - 1) * pageSize).limit(pageSize).collect(Collectors.toList());
return PageResult.of(result, (long) tables.size(), pageNo, pageSize);
@ -69,10 +74,15 @@ public class DefaultMetaService implements MetaData {
return SQLExecutor.getInstance().tables(connection, StringUtils.isEmpty(databaseName) ? null : databaseName, StringUtils.isEmpty(schemaName) ? null : schemaName, null, new String[]{"VIEW"});
}
@Override
public List<String> viewNames(Connection connection, String databaseName, String schemaName) {
return SQLExecutor.getInstance().tableNames(connection, StringUtils.isEmpty(databaseName) ? null : databaseName, StringUtils.isEmpty(schemaName) ? null : schemaName, null, new String[]{"VIEW"});
}
@Override
public List<Function> functions(Connection connection, String databaseName, String schemaName) {
List<Function> functions = SQLExecutor.getInstance().functions(connection, StringUtils.isEmpty(databaseName) ? null : databaseName, StringUtils.isEmpty(schemaName) ? null : schemaName);
if(CollectionUtils.isEmpty(functions)){
if (CollectionUtils.isEmpty(functions)) {
return functions;
}
return functions.stream().filter(function -> StringUtils.isNotBlank(function.getFunctionName())).map(function -> {
@ -89,9 +99,9 @@ public class DefaultMetaService implements MetaData {
@Override
public List<Procedure> procedures(Connection connection, String databaseName, String schemaName) {
List<Procedure> procedures = SQLExecutor.getInstance().procedures(connection, StringUtils.isEmpty(databaseName) ? null : databaseName, StringUtils.isEmpty(schemaName) ? null : schemaName);
List<Procedure> procedures = SQLExecutor.getInstance().procedures(connection, StringUtils.isEmpty(databaseName) ? null : databaseName, StringUtils.isEmpty(schemaName) ? null : schemaName);
if(CollectionUtils.isEmpty(procedures)){
if (CollectionUtils.isEmpty(procedures)) {
return procedures;
}
return procedures.stream().filter(function -> StringUtils.isNotBlank(function.getProcedureName())).map(procedure -> {

View File

@ -440,6 +440,26 @@ public class SQLExecutor implements CommandExecutor {
}
}
/** query table names
* @param connection
* @param databaseName
* @param schemaName
* @param tableName
* @param types
* @return
*/
public List<String> tableNames(Connection connection, String databaseName, String schemaName, String tableName, String[] types) {
List<String> tableNames = new ArrayList<>();
try (ResultSet resultSet = connection.getMetaData().getTables(databaseName, schemaName, tableName, types)) {
while (resultSet.next()) {
tableNames.add(resultSet.getString("TABLE_NAME"));
}
} catch (Exception e) {
throw new RuntimeException(e);
}
return tableNames;
}
/**
* Get all database table columns
*

View File

@ -23,7 +23,7 @@ public class ResultSetUtils {
private static List<String> getRsHeader(ResultSet rs) {
public static List<String> getRsHeader(ResultSet rs) {
try {
ResultSetMetaData resultSetMetaData = rs.getMetaData();
int col = resultSetMetaData.getColumnCount();