mirror of
https://github.com/CodePhiliaX/Chat2DB.git
synced 2025-07-31 11:42:41 +08:00
feat(PostgreSQL): Get the PostgreSQL sequences
This commit is contained in:
@ -345,7 +345,7 @@ public class PostgreSQLMetaData extends DefaultMetaService implements MetaData {
|
|||||||
|
|
||||||
stringBuilder.append("CREATE SEQUENCE ").append(nspname).append(".").append(relname).append("\n ");
|
stringBuilder.append("CREATE SEQUENCE ").append(nspname).append(".").append(relname).append("\n ");
|
||||||
|
|
||||||
if (databaseProductVersion >= 10.0){
|
if (databaseProductVersion >= 10.0) {
|
||||||
stringBuilder.append(" AS ").append(typname).append("\n ");
|
stringBuilder.append(" AS ").append(typname).append("\n ");
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -379,4 +379,21 @@ public class PostgreSQLMetaData extends DefaultMetaService implements MetaData {
|
|||||||
},
|
},
|
||||||
sequenceName, schemaName);
|
sequenceName, schemaName);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public List<SimpleSequence> sequences(Connection connection, String databaseName, String schemaName) {
|
||||||
|
List<SimpleSequence> simpleSequences = new ArrayList<>();
|
||||||
|
return SQLExecutor.getInstance().preExecute(connection, EXPORT_SEQUENCES_SQL, resultSet -> {
|
||||||
|
while (resultSet.next()) {
|
||||||
|
String relname = resultSet.getString("relname");
|
||||||
|
String comment = resultSet.getString("comment");
|
||||||
|
simpleSequences.add(SimpleSequence.builder()
|
||||||
|
.name(relname)
|
||||||
|
.comment(comment)
|
||||||
|
.build());
|
||||||
|
}
|
||||||
|
return simpleSequences;
|
||||||
|
},
|
||||||
|
schemaName);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -665,4 +665,14 @@ public class SQLConst {
|
|||||||
WHERE c.relname = ?
|
WHERE c.relname = ?
|
||||||
and n.nspname = ?;
|
and n.nspname = ?;
|
||||||
""";
|
""";
|
||||||
|
|
||||||
|
public static final String EXPORT_SEQUENCES_SQL = """
|
||||||
|
SELECT c.relname, obj_description(c.oid, 'pg_class') AS comment
|
||||||
|
FROM pg_sequence s
|
||||||
|
JOIN
|
||||||
|
pg_class c ON c.oid = s.seqrelid
|
||||||
|
JOIN
|
||||||
|
pg_namespace n ON n.oid = c.relnamespace
|
||||||
|
WHERE n.nspname = ?;
|
||||||
|
""";
|
||||||
}
|
}
|
||||||
|
@ -0,0 +1,50 @@
|
|||||||
|
package ai.chat2db.server.domain.api.param;
|
||||||
|
|
||||||
|
|
||||||
|
import ai.chat2db.server.tools.base.wrapper.param.PageQueryParam;
|
||||||
|
import jakarta.validation.constraints.NotNull;
|
||||||
|
import lombok.AllArgsConstructor;
|
||||||
|
import lombok.Data;
|
||||||
|
import lombok.NoArgsConstructor;
|
||||||
|
import lombok.experimental.SuperBuilder;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Pagination query sequence information
|
||||||
|
*
|
||||||
|
* @author Sylphy
|
||||||
|
*/
|
||||||
|
@Data
|
||||||
|
@SuperBuilder
|
||||||
|
@NoArgsConstructor
|
||||||
|
@AllArgsConstructor
|
||||||
|
public class SequencePageQueryParam extends PageQueryParam {
|
||||||
|
private static final long serialVersionUID = 1364512325486354343L;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Corresponding source id stored in the database
|
||||||
|
*/
|
||||||
|
@NotNull
|
||||||
|
private Long dataSourceId;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Corresponding connection database name
|
||||||
|
*/
|
||||||
|
@NotNull
|
||||||
|
private String databaseName;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Sequence Name
|
||||||
|
*/
|
||||||
|
private String sequenceName;
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* schema
|
||||||
|
*/
|
||||||
|
private String schemaName;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* if true, refresh the cache
|
||||||
|
*/
|
||||||
|
private boolean refresh;
|
||||||
|
}
|
@ -1,7 +1,10 @@
|
|||||||
package ai.chat2db.server.domain.api.service;
|
package ai.chat2db.server.domain.api.service;
|
||||||
|
|
||||||
|
import ai.chat2db.server.domain.api.param.SequencePageQueryParam;
|
||||||
import ai.chat2db.server.domain.api.param.ShowCreateSequenceParam;
|
import ai.chat2db.server.domain.api.param.ShowCreateSequenceParam;
|
||||||
import ai.chat2db.server.tools.base.wrapper.result.DataResult;
|
import ai.chat2db.server.tools.base.wrapper.result.DataResult;
|
||||||
|
import ai.chat2db.server.tools.base.wrapper.result.ListResult;
|
||||||
|
import ai.chat2db.spi.model.SimpleSequence;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Sequence source management services
|
* Sequence source management services
|
||||||
@ -10,4 +13,6 @@ import ai.chat2db.server.tools.base.wrapper.result.DataResult;
|
|||||||
*/
|
*/
|
||||||
public interface SequenceService {
|
public interface SequenceService {
|
||||||
DataResult<String> showCreateSequence(ShowCreateSequenceParam request);
|
DataResult<String> showCreateSequence(ShowCreateSequenceParam request);
|
||||||
|
|
||||||
|
ListResult<SimpleSequence> pageQuery(SequencePageQueryParam request);
|
||||||
}
|
}
|
||||||
|
@ -1,18 +1,23 @@
|
|||||||
package ai.chat2db.server.domain.core.impl;
|
package ai.chat2db.server.domain.core.impl;
|
||||||
|
|
||||||
|
|
||||||
|
import ai.chat2db.server.domain.api.param.SequencePageQueryParam;
|
||||||
import ai.chat2db.server.domain.api.param.ShowCreateSequenceParam;
|
import ai.chat2db.server.domain.api.param.ShowCreateSequenceParam;
|
||||||
import ai.chat2db.server.domain.api.service.SequenceService;
|
import ai.chat2db.server.domain.api.service.SequenceService;
|
||||||
import ai.chat2db.server.tools.base.wrapper.result.DataResult;
|
import ai.chat2db.server.tools.base.wrapper.result.DataResult;
|
||||||
|
import ai.chat2db.server.tools.base.wrapper.result.ListResult;
|
||||||
import ai.chat2db.spi.MetaData;
|
import ai.chat2db.spi.MetaData;
|
||||||
|
import ai.chat2db.spi.model.SimpleSequence;
|
||||||
import ai.chat2db.spi.sql.Chat2DBContext;
|
import ai.chat2db.spi.sql.Chat2DBContext;
|
||||||
import lombok.extern.slf4j.Slf4j;
|
import lombok.extern.slf4j.Slf4j;
|
||||||
import org.springframework.stereotype.Service;
|
import org.springframework.stereotype.Service;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Sequence source management serviceImpl
|
* Sequence source management serviceImpl
|
||||||
*
|
*
|
||||||
* @author: Sylphy
|
* @author Sylphy
|
||||||
*/
|
*/
|
||||||
@Slf4j
|
@Slf4j
|
||||||
@Service
|
@Service
|
||||||
@ -23,4 +28,10 @@ public class SequenceServiceImpl implements SequenceService {
|
|||||||
String ddl = metaSchema.sequenceDDL(Chat2DBContext.getConnection(), param.getDatabaseName(), param.getSchemaName(), param.getSequenceName());
|
String ddl = metaSchema.sequenceDDL(Chat2DBContext.getConnection(), param.getDatabaseName(), param.getSchemaName(), param.getSequenceName());
|
||||||
return DataResult.of(ddl);
|
return DataResult.of(ddl);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public ListResult<SimpleSequence> pageQuery(SequencePageQueryParam request) {
|
||||||
|
MetaData metaSchema = Chat2DBContext.getMetaData();
|
||||||
|
List<SimpleSequence> sequences = metaSchema.sequences(Chat2DBContext.getConnection(), request.getDatabaseName(), request.getSchemaName());
|
||||||
|
return ListResult.of(sequences);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,13 +1,17 @@
|
|||||||
package ai.chat2db.server.web.api.controller.rdb;
|
package ai.chat2db.server.web.api.controller.rdb;
|
||||||
|
|
||||||
|
|
||||||
|
import ai.chat2db.server.domain.api.param.SequencePageQueryParam;
|
||||||
import ai.chat2db.server.domain.api.param.ShowCreateSequenceParam;
|
import ai.chat2db.server.domain.api.param.ShowCreateSequenceParam;
|
||||||
import ai.chat2db.server.domain.api.service.DatabaseService;
|
import ai.chat2db.server.domain.api.service.DatabaseService;
|
||||||
import ai.chat2db.server.domain.api.service.SequenceService;
|
import ai.chat2db.server.domain.api.service.SequenceService;
|
||||||
import ai.chat2db.server.tools.base.wrapper.result.DataResult;
|
import ai.chat2db.server.tools.base.wrapper.result.DataResult;
|
||||||
|
import ai.chat2db.server.tools.base.wrapper.result.ListResult;
|
||||||
import ai.chat2db.server.web.api.aspect.ConnectionInfoAspect;
|
import ai.chat2db.server.web.api.aspect.ConnectionInfoAspect;
|
||||||
import ai.chat2db.server.web.api.controller.rdb.converter.RdbWebConverter;
|
import ai.chat2db.server.web.api.controller.rdb.converter.RdbWebConverter;
|
||||||
import ai.chat2db.server.web.api.controller.rdb.request.DdlExportRequest;
|
import ai.chat2db.server.web.api.controller.rdb.request.DdlExportRequest;
|
||||||
|
import ai.chat2db.server.web.api.controller.rdb.request.SequenceBriefQueryRequest;
|
||||||
|
import ai.chat2db.spi.model.SimpleSequence;
|
||||||
import jakarta.validation.Valid;
|
import jakarta.validation.Valid;
|
||||||
import lombok.RequiredArgsConstructor;
|
import lombok.RequiredArgsConstructor;
|
||||||
import lombok.extern.slf4j.Slf4j;
|
import lombok.extern.slf4j.Slf4j;
|
||||||
@ -28,6 +32,18 @@ public class SequenceController {
|
|||||||
private final DatabaseService databaseService;
|
private final DatabaseService databaseService;
|
||||||
private final SequenceService sequenceService;
|
private final SequenceService sequenceService;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Query the sequence list under the current DB
|
||||||
|
*
|
||||||
|
* @param request
|
||||||
|
* @return
|
||||||
|
*/
|
||||||
|
@GetMapping("/list")
|
||||||
|
public ListResult<SimpleSequence> list(@Valid SequenceBriefQueryRequest request) {
|
||||||
|
SequencePageQueryParam queryParam = rdbWebConverter.sequencePageRequest2param(request);
|
||||||
|
return sequenceService.pageQuery(queryParam);
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Export sequence creation statement
|
* Export sequence creation statement
|
||||||
*
|
*
|
||||||
|
@ -5,22 +5,9 @@ import java.util.List;
|
|||||||
import ai.chat2db.server.domain.api.param.*;
|
import ai.chat2db.server.domain.api.param.*;
|
||||||
import ai.chat2db.server.web.api.controller.data.source.vo.DatabaseVO;
|
import ai.chat2db.server.web.api.controller.data.source.vo.DatabaseVO;
|
||||||
import ai.chat2db.server.web.api.controller.rdb.request.*;
|
import ai.chat2db.server.web.api.controller.rdb.request.*;
|
||||||
import ai.chat2db.server.web.api.controller.rdb.vo.ColumnVO;
|
import ai.chat2db.server.web.api.controller.rdb.vo.*;
|
||||||
import ai.chat2db.server.web.api.controller.rdb.vo.ExecuteResultVO;
|
|
||||||
import ai.chat2db.server.web.api.controller.rdb.vo.IndexVO;
|
|
||||||
import ai.chat2db.server.web.api.controller.rdb.vo.MetaSchemaVO;
|
|
||||||
import ai.chat2db.server.web.api.controller.rdb.vo.SchemaVO;
|
|
||||||
import ai.chat2db.server.web.api.controller.rdb.vo.SqlVO;
|
|
||||||
import ai.chat2db.server.web.api.controller.rdb.vo.TableVO;
|
|
||||||
import ai.chat2db.server.web.api.http.request.EsTableSchemaRequest;
|
import ai.chat2db.server.web.api.http.request.EsTableSchemaRequest;
|
||||||
import ai.chat2db.spi.model.Database;
|
import ai.chat2db.spi.model.*;
|
||||||
import ai.chat2db.spi.model.ExecuteResult;
|
|
||||||
import ai.chat2db.spi.model.MetaSchema;
|
|
||||||
import ai.chat2db.spi.model.Schema;
|
|
||||||
import ai.chat2db.spi.model.Sql;
|
|
||||||
import ai.chat2db.spi.model.Table;
|
|
||||||
import ai.chat2db.spi.model.TableColumn;
|
|
||||||
import ai.chat2db.spi.model.TableIndex;
|
|
||||||
import org.mapstruct.Mapper;
|
import org.mapstruct.Mapper;
|
||||||
import org.mapstruct.Mapping;
|
import org.mapstruct.Mapping;
|
||||||
import org.mapstruct.Mappings;
|
import org.mapstruct.Mappings;
|
||||||
@ -115,6 +102,13 @@ public abstract class RdbWebConverter {
|
|||||||
* @return
|
* @return
|
||||||
*/
|
*/
|
||||||
public abstract TablePageQueryParam tablePageRequest2param(DataExportRequest request);
|
public abstract TablePageQueryParam tablePageRequest2param(DataExportRequest request);
|
||||||
|
/**
|
||||||
|
* Parameter conversion
|
||||||
|
*
|
||||||
|
* @param request
|
||||||
|
* @return
|
||||||
|
*/
|
||||||
|
public abstract SequencePageQueryParam sequencePageRequest2param(SequenceBriefQueryRequest request);
|
||||||
/**
|
/**
|
||||||
* Parameter conversion
|
* Parameter conversion
|
||||||
*
|
*
|
||||||
@ -220,7 +214,20 @@ public abstract class RdbWebConverter {
|
|||||||
* @return
|
* @return
|
||||||
*/
|
*/
|
||||||
public abstract List<TableVO> tableDto2vo(List<Table> dtos);
|
public abstract List<TableVO> tableDto2vo(List<Table> dtos);
|
||||||
|
/**
|
||||||
|
* Model conversion
|
||||||
|
*
|
||||||
|
* @param dtos
|
||||||
|
* @return
|
||||||
|
*/
|
||||||
|
public abstract SimpleSequence SequenceDto2vo(Sequence dtos);
|
||||||
|
/**
|
||||||
|
* Model conversion
|
||||||
|
*
|
||||||
|
* @param dtos
|
||||||
|
* @return
|
||||||
|
*/
|
||||||
|
public abstract List<SimpleSequence> SequenceDto2vo(List<Sequence> dtos);
|
||||||
/**
|
/**
|
||||||
* Model conversion
|
* Model conversion
|
||||||
* @param tableColumns
|
* @param tableColumns
|
||||||
|
@ -0,0 +1,47 @@
|
|||||||
|
package ai.chat2db.server.web.api.controller.rdb.request;
|
||||||
|
|
||||||
|
|
||||||
|
import ai.chat2db.server.tools.base.wrapper.request.PageQueryRequest;
|
||||||
|
import ai.chat2db.server.web.api.controller.data.source.request.DataSourceBaseRequestInfo;
|
||||||
|
import jakarta.validation.constraints.NotNull;
|
||||||
|
import lombok.Data;
|
||||||
|
|
||||||
|
import java.io.Serial;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Query sequence brief request
|
||||||
|
*
|
||||||
|
* @author Sylphy
|
||||||
|
*/
|
||||||
|
@Data
|
||||||
|
public class SequenceBriefQueryRequest extends PageQueryRequest implements DataSourceBaseRequestInfo {
|
||||||
|
|
||||||
|
@Serial
|
||||||
|
private static final long serialVersionUID = -1324577112324436332L;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Data source id
|
||||||
|
*/
|
||||||
|
@NotNull
|
||||||
|
private Long dataSourceId;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* DB name
|
||||||
|
*/
|
||||||
|
private String databaseName;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The space where the sequence is located is required by pg and oracle, but not by mysql.
|
||||||
|
*/
|
||||||
|
private String schemaName;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Fuzzy search terms
|
||||||
|
*/
|
||||||
|
private String searchKey;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* if true, refresh the cache
|
||||||
|
*/
|
||||||
|
private boolean refresh;
|
||||||
|
}
|
@ -271,4 +271,13 @@ public interface MetaData {
|
|||||||
*/
|
*/
|
||||||
String sequenceDDL(Connection connection, @NotEmpty String databaseName, String schemaName,
|
String sequenceDDL(Connection connection, @NotEmpty String databaseName, String schemaName,
|
||||||
@NotEmpty String tableName);
|
@NotEmpty String tableName);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Querying sequences simple information
|
||||||
|
*
|
||||||
|
* @param connection
|
||||||
|
* @param databaseName
|
||||||
|
* @return
|
||||||
|
*/
|
||||||
|
List<SimpleSequence> sequences(Connection connection, String databaseName, String schemaName);
|
||||||
}
|
}
|
@ -193,4 +193,9 @@ public class DefaultMetaService implements MetaData {
|
|||||||
@NotEmpty String tableName){
|
@NotEmpty String tableName){
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public List<SimpleSequence> sequences(Connection connection, String databaseName, String schemaName){
|
||||||
|
return null;
|
||||||
|
}
|
||||||
}
|
}
|
@ -5,6 +5,8 @@ import lombok.Data;
|
|||||||
import lombok.NoArgsConstructor;
|
import lombok.NoArgsConstructor;
|
||||||
import lombok.experimental.SuperBuilder;
|
import lombok.experimental.SuperBuilder;
|
||||||
|
|
||||||
|
import java.io.Serializable;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Sequence information
|
* Sequence information
|
||||||
*
|
*
|
||||||
@ -14,7 +16,8 @@ import lombok.experimental.SuperBuilder;
|
|||||||
@SuperBuilder
|
@SuperBuilder
|
||||||
@NoArgsConstructor
|
@NoArgsConstructor
|
||||||
@AllArgsConstructor
|
@AllArgsConstructor
|
||||||
public class Sequence {
|
public class Sequence implements Serializable {
|
||||||
|
private static final long serialVersionUID = 1L;
|
||||||
private String nspname;
|
private String nspname;
|
||||||
private String relname;
|
private String relname;
|
||||||
private String typname;
|
private String typname;
|
||||||
|
@ -0,0 +1,30 @@
|
|||||||
|
package ai.chat2db.spi.model;
|
||||||
|
|
||||||
|
import lombok.AllArgsConstructor;
|
||||||
|
import lombok.Data;
|
||||||
|
import lombok.NoArgsConstructor;
|
||||||
|
import lombok.experimental.SuperBuilder;
|
||||||
|
|
||||||
|
import java.io.Serializable;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Simple sequence information
|
||||||
|
*
|
||||||
|
* @author Sylphy
|
||||||
|
*/
|
||||||
|
@Data
|
||||||
|
@SuperBuilder
|
||||||
|
@NoArgsConstructor
|
||||||
|
@AllArgsConstructor
|
||||||
|
public class SimpleSequence implements Serializable {
|
||||||
|
private static final long serialVersionUID = 1L;
|
||||||
|
/**
|
||||||
|
* Sequence Name
|
||||||
|
*/
|
||||||
|
private String name;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* description
|
||||||
|
*/
|
||||||
|
private String comment;
|
||||||
|
}
|
Reference in New Issue
Block a user