Skip to content

Commit b557397

Browse files
Aias00HandSonic
authored andcommitted
fix(snowflake): clear blank connection properties
1 parent b74b7ae commit b557397

2 files changed

Lines changed: 32 additions & 5 deletions

File tree

‎chat2db-community-server/chat2db-community-plugins/chat2db-community-snowflake/src/main/java/ai/chat2db/plugin/snowflake/SnowflakeDBManager.java‎

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,7 @@ static List<KeyValue> prepareExtendInfo(ConnectInfo connectInfo) {
4141
List<KeyValue> extendInfo = configuredExtendInfo == null
4242
? new ArrayList<>()
4343
: new ArrayList<>(configuredExtendInfo);
44-
replacePropertyIfConfigured(extendInfo, DATABASE_PROPERTY, connectInfo.getDatabaseName());
45-
replacePropertyIfConfigured(extendInfo, SCHEMA_PROPERTY, connectInfo.getSchemaName());
44+
replaceManagedContextProperties(extendInfo, connectInfo);
4645
replaceProperty(extendInfo, QUERY_RESULT_FORMAT_PROPERTY, QUERY_RESULT_FORMAT_JSON);
4746
return extendInfo;
4847
}
@@ -51,9 +50,14 @@ static List<KeyValue> prepareExtendInfo(ConnectInfo connectInfo) {
5150
// they are always dropped (including manually added entries) before the current value is
5251
// written back, so clearing a field cannot keep stale context. Configure the Snowflake
5352
// database/schema through the connection fields, not through extendInfo.
54-
private static void replacePropertyIfConfigured(List<KeyValue> extendInfo, String propertyName, String propertyValue) {
55-
// Always drop the managed entry first so a reused ConnectInfo cannot keep a stale value after it is cleared.
56-
removeProperty(extendInfo, propertyName);
53+
private static void replaceManagedContextProperties(List<KeyValue> extendInfo, ConnectInfo connectInfo) {
54+
removeProperty(extendInfo, DATABASE_PROPERTY);
55+
removeProperty(extendInfo, SCHEMA_PROPERTY);
56+
addPropertyIfConfigured(extendInfo, DATABASE_PROPERTY, connectInfo.getDatabaseName());
57+
addPropertyIfConfigured(extendInfo, SCHEMA_PROPERTY, connectInfo.getSchemaName());
58+
}
59+
60+
private static void addPropertyIfConfigured(List<KeyValue> extendInfo, String propertyName, String propertyValue) {
5761
if (StringUtils.isNotBlank(propertyValue)) {
5862
addProperty(extendInfo, propertyName, propertyValue);
5963
}

‎chat2db-community-server/chat2db-community-plugins/chat2db-community-snowflake/src/test/java/ai/chat2db/plugin/snowflake/SnowflakeDBManagerTest.java‎

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,29 @@ void reconnectWithClearedSchemaNameDropsStaleSchemaProperty() {
9393
assertFalse(connectInfo.getExtendMap().containsKey("schema"));
9494
}
9595

96+
@Test
97+
void reconnectWithClearedDatabaseAndSchemaDropsStaleManagedProperties() {
98+
ConnectInfo connectInfo = new ConnectInfo();
99+
connectInfo.setDatabaseName("analytics");
100+
connectInfo.setSchemaName("reporting");
101+
connectInfo.setConnection(openConnectionStub());
102+
connectInfo.setExtendInfo(List.of(keyValue("role", "analyst")));
103+
104+
assertSame(connectInfo.getConnection(), dbManager.getConnection(connectInfo));
105+
assertProperties(connectInfo.getExtendInfo(),
106+
List.of("role", "db", "schema", "JDBC_QUERY_RESULT_FORMAT"),
107+
List.of("analyst", "analytics", "reporting", "JSON"));
108+
109+
connectInfo.setDatabaseName(" ");
110+
connectInfo.setSchemaName("");
111+
assertSame(connectInfo.getConnection(), dbManager.getConnection(connectInfo));
112+
assertProperties(connectInfo.getExtendInfo(),
113+
List.of("role", "JDBC_QUERY_RESULT_FORMAT"),
114+
List.of("analyst", "JSON"));
115+
assertFalse(connectInfo.getExtendMap().containsKey("db"));
116+
assertFalse(connectInfo.getExtendMap().containsKey("schema"));
117+
}
118+
96119
@Test
97120
void passesDriverConfigPropertiesToNewConnectionWhenExtendInfoIsMissing() throws Exception {
98121
assertDriverConfigFallbackIsPassedToNewConnection(null);

0 commit comments

Comments
 (0)