Skip to content
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 @@ -126,7 +126,9 @@ public String buildModifyColumn(TableColumn tableColumn) {
}
if (EditStatusEnum.MODIFY.name().equals(tableColumn.getEditStatus())) {
if (!StringUtils.equalsIgnoreCase(tableColumn.getOldName(), tableColumn.getName())) {
return StringUtils.join("CHANGE COLUMN ", SnowflakeIdentifierProcessor.INSTANCE.quoteIdentifierAlways(tableColumn.getOldName()), " ", buildCreateColumnSql(tableColumn));
return StringUtils.join("RENAME COLUMN ",
SnowflakeIdentifierProcessor.INSTANCE.quoteIdentifierAlways(tableColumn.getOldName()),
" TO ", SnowflakeIdentifierProcessor.INSTANCE.quoteIdentifierAlways(tableColumn.getName()));
} else {
return StringUtils.join("MODIFY COLUMN ", buildCreateColumnSql(tableColumn));
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
package ai.chat2db.plugin.snowflake.builder;

import ai.chat2db.community.domain.api.config.TableBuilderConfig;
import ai.chat2db.community.domain.api.enums.plugin.EditStatusEnum;
import ai.chat2db.community.domain.api.model.metadata.Table;
import ai.chat2db.community.domain.api.model.metadata.TableColumn;
import ai.chat2db.community.domain.api.model.metadata.TableIndex;
import org.junit.jupiter.api.Test;

import java.util.Collections;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;
Expand Down Expand Up @@ -52,6 +54,44 @@ void buildAlterTableDoesNotAlterEqualBoxedIncrementValues() {
assertFalse(sql.contains("AUTOINCREMENT"));
}

@Test
void buildCreateTableQuotesSchemaAndTableWithEmbeddedDelimiters() {
Table table = tableWithColumn("VARCHAR");
table.setSchemaName("Sales\"Ops");
table.setName("Order\"Items");

String sql = builder.buildCreateTable(table, new TableBuilderConfig());

assertTrue(sql.startsWith("CREATE TABLE \"Sales\"\"Ops\".\"Order\"\"Items\" (\n"), sql);
}

@Test
void buildAlterTableQuotesDroppedColumnWithEmbeddedDelimiter() {
Table oldTable = tableWithColumn("VARCHAR");
Table newTable = tableWithColumn("VARCHAR");
TableColumn droppedColumn = newTable.getColumnList().get(0);
droppedColumn.setName("Mixed\"Case");
droppedColumn.setEditStatus(EditStatusEnum.DELETE.name());

String sql = builder.buildAlterTable(oldTable, newTable);

assertEquals("ALTER TABLE \"users\"\n\tDROP COLUMN \"Mixed\"\"Case\";", sql);
}

@Test
void buildAlterTableUsesSnowflakeRenameSyntaxAndQuotesBothNames() {
Table oldTable = tableWithColumn("VARCHAR");
Table newTable = tableWithColumn("VARCHAR");
TableColumn renamedColumn = newTable.getColumnList().get(0);
renamedColumn.setOldName("Old\"Name");
renamedColumn.setName("New\"Name");
renamedColumn.setEditStatus(EditStatusEnum.MODIFY.name());

String sql = builder.buildAlterTable(oldTable, newTable);

assertEquals("ALTER TABLE \"users\"\n\tRENAME COLUMN \"Old\"\"Name\" TO \"New\"\"Name\";", sql);
}

private Table tableWithColumn(String columnType) {
TableColumn column = new TableColumn();
column.setName("name");
Expand Down
Loading