Skip to content

Commit 673f483

Browse files
guanxucericbottard
authored andcommitted
Add Oracle and Sqlite support for JDBC chat memory
- Add Oracle and Sqlite JdbcChatMemoryRepositoryDialect classes. - Modify the JdbcChatMemoryRepositoryDialect interface to support identifying the correct dialect for Oracle and SQLite by their database product names. - Add Oracle and Sqlite JdbcChatMemoryRepository integration test classes. Signed-off-by: guanxu <[email protected]> Signed-off-by: Eric Bottard <[email protected]>
1 parent bdb7ea5 commit 673f483

File tree

6 files changed

+102
-3
lines changed

6 files changed

+102
-3
lines changed

memory/repository/spring-ai-model-chat-memory-repository-jdbc/pom.xml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,13 @@
8585
<optional>true</optional>
8686
</dependency>
8787

88+
<dependency>
89+
<groupId>org.xerial</groupId>
90+
<artifactId>sqlite-jdbc</artifactId>
91+
<scope>test</scope>
92+
<optional>true</optional>
93+
</dependency>
94+
8895
<dependency>
8996
<groupId>org.springframework.boot</groupId>
9097
<artifactId>spring-boot-starter-test</artifactId>

memory/repository/spring-ai-model-chat-memory-repository-jdbc/src/main/java/org/springframework/ai/chat/memory/repository/jdbc/JdbcChatMemoryRepositoryDialect.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,7 @@ static JdbcChatMemoryRepositoryDialect from(DataSource dataSource) {
7878
case "MySQL", "MariaDB" -> new MysqlChatMemoryRepositoryDialect();
7979
case "Microsoft SQL Server" -> new SqlServerChatMemoryRepositoryDialect();
8080
case "HSQL Database Engine" -> new HsqldbChatMemoryRepositoryDialect();
81+
case "SQLite" -> new SqliteChatMemoryRepositoryDialect();
8182
default -> // Add more as needed
8283
new PostgresChatMemoryRepositoryDialect();
8384
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
/*
2+
* Copyright 2024-2025 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.ai.chat.memory.repository.jdbc;
18+
19+
/**
20+
* Sqlite dialect for chat memory repository.
21+
*
22+
* @author guan xu
23+
* @since 1.1.0
24+
*/
25+
public class SqliteChatMemoryRepositoryDialect implements JdbcChatMemoryRepositoryDialect {
26+
27+
@Override
28+
public String getSelectMessagesSql() {
29+
return "SELECT content, type FROM SPRING_AI_CHAT_MEMORY WHERE conversation_id = ? ORDER BY timestamp";
30+
}
31+
32+
@Override
33+
public String getInsertMessageSql() {
34+
return "INSERT INTO SPRING_AI_CHAT_MEMORY (conversation_id, content, type, timestamp) VALUES (?, ?, ?, ?)";
35+
}
36+
37+
@Override
38+
public String getSelectConversationIdsSql() {
39+
return "SELECT DISTINCT conversation_id FROM SPRING_AI_CHAT_MEMORY";
40+
}
41+
42+
@Override
43+
public String getDeleteMessagesSql() {
44+
return "DELETE FROM SPRING_AI_CHAT_MEMORY WHERE conversation_id = ?";
45+
}
46+
47+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
CREATE TABLE IF NOT EXISTS SPRING_AI_CHAT_MEMORY (
2+
conversation_id TEXT NOT NULL,
3+
content TEXT NOT NULL,
4+
type TEXT NOT NULL,
5+
timestamp INTEGER NOT NULL,
6+
CHECK (type IN ('USER', 'ASSISTANT', 'SYSTEM', 'TOOL'))
7+
);
8+
9+
CREATE INDEX IF NOT EXISTS SPRING_AI_CHAT_MEMORY_CONVERSATION_ID_TIMESTAMP_IDX
10+
ON SPRING_AI_CHAT_MEMORY(conversation_id, timestamp);

memory/repository/spring-ai-model-chat-memory-repository-jdbc/src/test/java/org/springframework/ai/chat/memory/repository/jdbc/AbstractJdbcChatMemoryRepositoryIT.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@
1616

1717
package org.springframework.ai.chat.memory.repository.jdbc;
1818

19-
import java.sql.Timestamp;
2019
import java.util.List;
2120
import java.util.UUID;
2221
import java.util.stream.Collectors;
@@ -84,7 +83,7 @@ void saveMessagesSingleMessage(String content, MessageType messageType) {
8483
assertThat(result.get("conversation_id")).isEqualTo(conversationId);
8584
assertThat(result.get("content")).isEqualTo(message.getText());
8685
assertThat(result.get("type")).isEqualTo(messageType.name());
87-
assertThat(result.get("timestamp")).isInstanceOf(Timestamp.class);
86+
assertThat(result.get("timestamp")).isNotNull();
8887
}
8988

9089
@Test
@@ -114,7 +113,7 @@ void saveMessagesMultipleMessages() {
114113
assertThat(result.get("conversation_id")).isEqualTo(conversationId);
115114
assertThat(result.get("content")).isEqualTo(message.getText());
116115
assertThat(result.get("type")).isEqualTo(message.getMessageType().name());
117-
assertThat(result.get("timestamp")).isInstanceOf(Timestamp.class);
116+
assertThat(result.get("timestamp")).isNotNull();
118117
}
119118

120119
var count = this.chatMemoryRepository.findByConversationId(conversationId).size();
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
/*
2+
* Copyright 2023-2025 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.ai.chat.memory.repository.jdbc;
18+
19+
import org.springframework.boot.test.context.SpringBootTest;
20+
import org.springframework.test.context.TestPropertySource;
21+
import org.springframework.test.context.jdbc.Sql;
22+
23+
/**
24+
* Integration tests for {@link JdbcChatMemoryRepository} with Sqlite.
25+
*
26+
* @author guan xu
27+
*/
28+
@SpringBootTest
29+
@TestPropertySource(properties = { "spring.datasource.url=jdbc:sqlite::memory:",
30+
"spring.datasource.driver-class-name=org.sqlite.JDBC",
31+
"spring.ai.chat.memory.repository.jdbc.initialize-schema=always" })
32+
@Sql(scripts = "classpath:org/springframework/ai/chat/memory/repository/jdbc/schema-sqlite.sql")
33+
class JdbcChatMemoryRepositorySqliteIT extends AbstractJdbcChatMemoryRepositoryIT {
34+
35+
}

0 commit comments

Comments
 (0)