mirror of
https://github.com/CodePhiliaX/Chat2DB.git
synced 2025-08-02 21:50:43 +08:00
support PinService
This commit is contained in:
@ -60,5 +60,9 @@
|
||||
<groupId>com.baomidou</groupId>
|
||||
<artifactId>mybatis-plus-boot-starter</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>commons-beanutils</groupId>
|
||||
<artifactId>commons-beanutils</artifactId>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
</project>
|
@ -6,12 +6,7 @@ package ai.chat2db.spi;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import ai.chat2db.spi.model.Function;
|
||||
import ai.chat2db.spi.model.Procedure;
|
||||
import ai.chat2db.spi.model.Table;
|
||||
import ai.chat2db.spi.model.TableColumn;
|
||||
import ai.chat2db.spi.model.TableIndex;
|
||||
import ai.chat2db.spi.model.Trigger;
|
||||
import ai.chat2db.spi.model.*;
|
||||
import jakarta.validation.constraints.NotEmpty;
|
||||
|
||||
/**
|
||||
@ -26,7 +21,7 @@ public interface MetaData {
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
List<String> databases();
|
||||
List<Database> databases();
|
||||
|
||||
/**
|
||||
* Querying all schemas under a database
|
||||
@ -34,7 +29,7 @@ public interface MetaData {
|
||||
* @param databaseName
|
||||
* @return
|
||||
*/
|
||||
List<String> schemas(String databaseName);
|
||||
List<Schema> schemas(String databaseName);
|
||||
|
||||
/**
|
||||
* Querying DDL information
|
||||
|
@ -4,16 +4,18 @@
|
||||
*/
|
||||
package ai.chat2db.spi.jdbc;
|
||||
|
||||
import java.lang.reflect.InvocationTargetException;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
import ai.chat2db.spi.MetaData;
|
||||
import ai.chat2db.spi.model.Function;
|
||||
import ai.chat2db.spi.model.Procedure;
|
||||
import ai.chat2db.spi.model.Table;
|
||||
import ai.chat2db.spi.model.TableColumn;
|
||||
import ai.chat2db.spi.model.TableIndex;
|
||||
import ai.chat2db.spi.model.Trigger;
|
||||
import ai.chat2db.spi.model.*;
|
||||
import ai.chat2db.spi.sql.SQLExecutor;
|
||||
import cn.hutool.json.JSON;
|
||||
import com.google.common.collect.Lists;
|
||||
import org.apache.commons.beanutils.BeanUtils;
|
||||
import org.springframework.util.CollectionUtils;
|
||||
|
||||
/**
|
||||
* @author jipengfei
|
||||
@ -21,13 +23,32 @@ import ai.chat2db.spi.sql.SQLExecutor;
|
||||
*/
|
||||
public class DefaultMetaService implements MetaData {
|
||||
@Override
|
||||
public List<String> databases() {
|
||||
return SQLExecutor.getInstance().databases();
|
||||
public List<Database> databases() {
|
||||
List<String> dataBases = SQLExecutor.getInstance().databases();
|
||||
if (CollectionUtils.isEmpty(dataBases)) {
|
||||
return Lists.newArrayList();
|
||||
}
|
||||
return dataBases.stream().map(str -> Database.builder().name(str).build()).collect(Collectors.toList());
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<String> schemas(String databaseName) {
|
||||
return SQLExecutor.getInstance().schemas(databaseName, null);
|
||||
public List<Schema> schemas(String databaseName) {
|
||||
List<Map<String, String>> maps = SQLExecutor.getInstance().schemas(databaseName, null);
|
||||
if (CollectionUtils.isEmpty(maps)) {
|
||||
return Lists.newArrayList();
|
||||
}
|
||||
return maps.stream().map(map -> map2Schema(map)).collect(Collectors.toList());
|
||||
|
||||
}
|
||||
|
||||
private Schema map2Schema(Map<String, String> map) {
|
||||
Schema schema = new Schema();
|
||||
try {
|
||||
BeanUtils.populate(schema, map);
|
||||
} catch (Exception e) {
|
||||
}
|
||||
return schema;
|
||||
}
|
||||
|
||||
@Override
|
||||
@ -37,12 +58,12 @@ public class DefaultMetaService implements MetaData {
|
||||
|
||||
@Override
|
||||
public List<Table> tables(String databaseName, String schemaName, String tableName) {
|
||||
return SQLExecutor.getInstance().tables(databaseName, schemaName, tableName, new String[] {"TABLE"});
|
||||
return SQLExecutor.getInstance().tables(databaseName, schemaName, tableName, new String[]{"TABLE"});
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<? extends Table> views(String databaseName, String schemaName) {
|
||||
return SQLExecutor.getInstance().tables(databaseName, schemaName, null, new String[] {"VIEW"});
|
||||
return SQLExecutor.getInstance().tables(databaseName, schemaName, null, new String[]{"VIEW"});
|
||||
}
|
||||
|
||||
@Override
|
||||
@ -67,7 +88,7 @@ public class DefaultMetaService implements MetaData {
|
||||
|
||||
@Override
|
||||
public List<TableColumn> columns(String databaseName, String schemaName, String tableName,
|
||||
String columnName) {
|
||||
String columnName) {
|
||||
return SQLExecutor.getInstance().columns(databaseName, schemaName, tableName, columnName);
|
||||
}
|
||||
|
||||
|
@ -5,6 +5,8 @@ import lombok.Data;
|
||||
import lombok.NoArgsConstructor;
|
||||
import lombok.experimental.SuperBuilder;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* 数据库
|
||||
*
|
||||
@ -16,7 +18,12 @@ import lombok.experimental.SuperBuilder;
|
||||
@AllArgsConstructor
|
||||
public class Database {
|
||||
/**
|
||||
* 数据名字
|
||||
* 数据库名字
|
||||
*/
|
||||
private String name;
|
||||
|
||||
/**
|
||||
* schema name
|
||||
*/
|
||||
private List<Schema> schemas;
|
||||
}
|
||||
|
@ -0,0 +1,25 @@
|
||||
package ai.chat2db.spi.model;
|
||||
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Data;
|
||||
import lombok.NoArgsConstructor;
|
||||
import lombok.experimental.SuperBuilder;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
@Data
|
||||
@SuperBuilder
|
||||
@NoArgsConstructor
|
||||
@AllArgsConstructor
|
||||
public class MetaSchema {
|
||||
|
||||
/**
|
||||
* database list
|
||||
*/
|
||||
private List<Database> databases;
|
||||
|
||||
/**
|
||||
* schema list
|
||||
*/
|
||||
private List<Schema> schemas;
|
||||
}
|
@ -18,6 +18,11 @@ import lombok.experimental.SuperBuilder;
|
||||
@NoArgsConstructor
|
||||
@AllArgsConstructor
|
||||
public class Schema {
|
||||
|
||||
/**
|
||||
* databaseName
|
||||
*/
|
||||
private String databaseName;
|
||||
/**
|
||||
* 数据名字
|
||||
*/
|
||||
|
@ -59,6 +59,9 @@ public class Table {
|
||||
*/
|
||||
private String type;
|
||||
|
||||
|
||||
/**
|
||||
* 是否置顶
|
||||
*/
|
||||
private boolean pinned;
|
||||
}
|
||||
|
||||
|
@ -9,7 +9,9 @@ import java.sql.ResultSet;
|
||||
import java.sql.ResultSetMetaData;
|
||||
import java.sql.SQLException;
|
||||
import java.sql.Statement;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.function.Function;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
@ -203,13 +205,16 @@ public class SQLExecutor {
|
||||
* @param schemaName
|
||||
* @return
|
||||
*/
|
||||
public List<String> schemas(String databaseName, String schemaName) {
|
||||
List<String> schemaList = Lists.newArrayList();
|
||||
public List<Map<String,String>> schemas(String databaseName, String schemaName) {
|
||||
List<Map<String,String>> schemaList = Lists.newArrayList();
|
||||
try {
|
||||
ResultSet resultSet = getConnection().getMetaData().getSchemas(databaseName, schemaName);
|
||||
if (resultSet != null) {
|
||||
while (resultSet.next()) {
|
||||
schemaList.add(resultSet.getString("TABLE_SCHEM"));
|
||||
Map<String,String> map = new HashMap<>();
|
||||
map.put("name",resultSet.getString("TABLE_SCHEM"));
|
||||
map.put("databaseName",resultSet.getString("TABLE_CATALOG"));
|
||||
schemaList.add(map);
|
||||
}
|
||||
}
|
||||
} catch (SQLException e) {
|
||||
|
Reference in New Issue
Block a user