Skip to content

Commit

Permalink
refactor(metadata): refactoring drop column
Browse files Browse the repository at this point in the history
  • Loading branch information
qianmoQ committed Jan 15, 2025
1 parent d5bdde0 commit f94576f
Show file tree
Hide file tree
Showing 7 changed files with 108 additions and 45 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -176,4 +176,15 @@ public CommonResponse<Response> createColumn(
{
return this.service.createColumn(code, database, table, configure);
}

@DeleteMapping(value = "{code}/{database}/{table}/drop-column")
public CommonResponse<Response> dropColumn(
@PathVariable String code,
@PathVariable String database,
@PathVariable String table,
@RequestBody TableDefinition configure
)
{
return this.service.dropColumn(code, database, table, configure);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -37,4 +37,6 @@ public interface MetadataService
CommonResponse<Response> exportData(String code, String database, String table, TableDefinition configure);

CommonResponse<Response> createColumn(String code, String database, String table, TableDefinition configure);

CommonResponse<Response> dropColumn(String code, String database, String table, TableDefinition configure);
}
Original file line number Diff line number Diff line change
Expand Up @@ -250,4 +250,19 @@ public CommonResponse<Response> createColumn(String code, String database, Strin
.orElseGet(() -> CommonResponse.failure(String.format("Plugin [ %s ] not found", entity.getType()))))
.orElseGet(() -> CommonResponse.failure(String.format("Resource [ %s ] not found", code)));
}

@Override
public CommonResponse<Response> dropColumn(String code, String database, String table, TableDefinition configure)
{
return repository.findByCode(code)
.map(entity -> pluginManager.getPlugin(entity.getType())
.map(plugin -> {
PluginService service = plugin.getService(PluginService.class);
configure.setDatabase(database);
configure.setName(table);
return CommonResponse.success(service.dropColumn(entity.toConfigure(pluginManager, plugin), configure));
})
.orElseGet(() -> CommonResponse.failure(String.format("Plugin [ %s ] not found", entity.getType()))))
.orElseGet(() -> CommonResponse.failure(String.format("Resource [ %s ] not found", code)));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -935,6 +935,27 @@ default Response createColumn(Configure configure, TableDefinition definition)
);
}

/**
* 删除列
* Drop column
*
* @param configure 配置信息 | Configuration information
* @param definition 表配置定义 | Table configuration definition
* @return 执行结果 | Execution result
*/
default Response dropColumn(Configure configure, TableDefinition definition)
{
AlterTable tableDefinition = AlterTable.create(definition.getDatabase(), definition.getName());

definition.getColumns().forEach(col -> tableDefinition.dropColumn(col.getName()));

return this.getResponse(
tableDefinition.build(),
configure,
definition
);
}

private Response getResponse(String sql, Configure configure, BaseDefinition definition)
{
Response response;
Expand Down
5 changes: 5 additions & 0 deletions core/datacap-ui/src/services/metadata.ts
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,11 @@ class MetadataService
{
return HttpUtils.post(`${ DEFAULT_PATH }/${ code }/${ database }/${ table }/create-column`, configure)
}

dropColumn(code: string, database: string, table: string, configure: any): Promise<ResponseModel>
{
return HttpUtils.delete(`${ DEFAULT_PATH }/${ code }/${ database }/${ table }/drop-column`, configure)
}
}

export default new MetadataService()
Original file line number Diff line number Diff line change
Expand Up @@ -138,10 +138,11 @@
:info="dataInfo as any"
@close="visibleChangeColumn(false)"/>

<ColumnDrop v-if="columnDropVisible"
<ColumnDrop v-if="columnDropVisible && dataInfo"
:is-visible="columnDropVisible"
:info="dataInfo as any"
@close="visibleDropColumn(false)"/>
:column="dataInfo?.code"
@close="visibleDropColumn(false)">
</ColumnDrop>
</template>

<script lang="ts">
Expand Down Expand Up @@ -299,7 +300,7 @@ export default defineComponent({
},
onNodeClick(node: any)
{
if (node.level === StructureEnum.TYPE) {
if (node.level === StructureEnum.TYPE || node.level === StructureEnum.COLUMN) {
return
}
Expand Down Expand Up @@ -363,6 +364,10 @@ export default defineComponent({
visibleDropColumn(opened: boolean)
{
this.columnDropVisible = opened
if (!opened) {
this.onChangeDatabase()
}
},
visibleContextMenu(event: any, node: any)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,7 @@

<script lang="ts">
import { defineComponent } from 'vue'
import { StructureModel } from '@/model/structure.ts'
import MetadataService from '@/services/metadata'
import AceEditor from '@/views/components/editor/AceEditor.vue'
export default defineComponent({
Expand All @@ -59,8 +58,8 @@ export default defineComponent({
isVisible: {
type: Boolean
},
info: {
type: Object as () => StructureModel | null
column: {
type: String
}
},
data()
Expand All @@ -69,61 +68,66 @@ export default defineComponent({
loading: false,
submitting: false,
title: null as string | null,
formState: null as unknown as TableFilter
formState: null as any
}
},
created()
{
this.formState = TableFilterRequest.of()
this.formState.type = SqlType.DROP
if (this.info) {
this.formState.columnId = Number(this.info.applyId)
this.title = this.$t('source.common.dropColumnInfo').replace('$VALUE', this.info.title as string)
this.formState = {
statement: null
}
if (this.column) {
this.title = this.$t('source.common.dropColumnInfo').replace('$VALUE', this.column)
this.onSubmit(true)
}
},
methods: {
onSubmit(preview: boolean)
{
if (this.info && this.formState) {
const code = this.$route.params?.source
const database = this.$route.params?.database
const table = this.$route.params?.table
if (code && database && table) {
if (preview) {
this.loading = true
}
else {
this.submitting = true
}
this.formState.preview = preview
TableService.getData(String(this.info.tableId), this.formState)
.then(response => {
if (response.status) {
if (preview) {
this.formState.statement = response.data.content
}
else {
this.$Message.success({
content: this.$t('source.tip.dropColumnSuccess').replace('$VALUE', String(this.info?.title)),
showIcon: true
})
this.onCancel()
}
}
else {
this.$Message.error({
content: response.message,
showIcon: true
})
}
})
.finally(() => {
if (preview) {
this.loading = false
}
else {
this.submitting = false
}
})
MetadataService.dropColumn(code, database, table, { preview, columns: [{ name: this.column }] })
.then(response => {
if (response.status) {
if (preview) {
this.formState.statement = response.data.content
}
else {
this.$Message.success({
content: this.$t('source.tip.dropColumnSuccess').replace('$VALUE', String(this.column)),
showIcon: true
})
this.onCancel()
}
}
else {
this.$Message.error({
content: response.message,
showIcon: true
})
}
})
.finally(() => {
if (preview) {
this.loading = false
}
else {
this.submitting = false
}
})
}
},
onCancel()
Expand Down

0 comments on commit f94576f

Please sign in to comment.