Skip to content

Commit 0b0024e

Browse files
quaffericbottard
authored andcommitted
Introduce H2ChatMemoryRepositoryDialect
Signed-off-by: Yanming Zhou <[email protected]> Signed-off-by: Eric Bottard <[email protected]>
1 parent db9ac2f commit 0b0024e

File tree

5 files changed

+98
-4
lines changed

5 files changed

+98
-4
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
@@ -92,6 +92,13 @@
9292
<optional>true</optional>
9393
</dependency>
9494

95+
<dependency>
96+
<groupId>com.h2database</groupId>
97+
<artifactId>h2</artifactId>
98+
<scope>test</scope>
99+
<optional>true</optional>
100+
</dependency>
101+
95102
<dependency>
96103
<groupId>org.springframework.boot</groupId>
97104
<artifactId>spring-boot-starter-test</artifactId>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
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+
* H2-specific SQL dialect for chat memory repository.
21+
*
22+
* @author Yanming Zhou
23+
*/
24+
public class H2ChatMemoryRepositoryDialect implements JdbcChatMemoryRepositoryDialect {
25+
26+
@Override
27+
public String getSelectMessagesSql() {
28+
return "SELECT content, type FROM SPRING_AI_CHAT_MEMORY WHERE conversation_id = ? ORDER BY timestamp ASC";
29+
}
30+
31+
@Override
32+
public String getInsertMessageSql() {
33+
return "INSERT INTO SPRING_AI_CHAT_MEMORY (conversation_id, content, type, timestamp) VALUES (?, ?, ?, ?)";
34+
}
35+
36+
@Override
37+
public String getDeleteMessagesSql() {
38+
return "DELETE FROM SPRING_AI_CHAT_MEMORY WHERE conversation_id = ?";
39+
}
40+
41+
@Override
42+
public String getSelectConversationIdsSql() {
43+
return "SELECT DISTINCT conversation_id FROM SPRING_AI_CHAT_MEMORY";
44+
}
45+
46+
}

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 & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -53,10 +53,6 @@ public interface JdbcChatMemoryRepositoryDialect {
5353
*/
5454
String getDeleteMessagesSql();
5555

56-
/**
57-
* Optionally, dialect can provide more advanced SQL as needed.
58-
*/
59-
6056
/**
6157
* Detects the dialect from the DataSource.
6258
*/
@@ -79,6 +75,7 @@ static JdbcChatMemoryRepositoryDialect from(DataSource dataSource) {
7975
case "Microsoft SQL Server" -> new SqlServerChatMemoryRepositoryDialect();
8076
case "HSQL Database Engine" -> new HsqldbChatMemoryRepositoryDialect();
8177
case "SQLite" -> new SqliteChatMemoryRepositoryDialect();
78+
case "H2" -> new H2ChatMemoryRepositoryDialect();
8279
default -> // Add more as needed
8380
new PostgresChatMemoryRepositoryDialect();
8481
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
CREATE TABLE SPRING_AI_CHAT_MEMORY (
2+
conversation_id VARCHAR(36) NOT NULL,
3+
content LONGVARCHAR NOT NULL,
4+
type VARCHAR(10) NOT NULL,
5+
timestamp TIMESTAMP DEFAULT CURRENT_TIMESTAMP NOT NULL
6+
);
7+
8+
CREATE INDEX SPRING_AI_CHAT_MEMORY_CONVERSATION_ID_TIMESTAMP_IDX ON SPRING_AI_CHAT_MEMORY(conversation_id, timestamp DESC);
9+
10+
ALTER TABLE SPRING_AI_CHAT_MEMORY ADD CONSTRAINT TYPE_CHECK CHECK (type IN ('USER', 'ASSISTANT', 'SYSTEM', 'TOOL'));
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
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 H2.
25+
*
26+
* @author Yanming Zhou
27+
*/
28+
@SpringBootTest
29+
@TestPropertySource(properties = { "spring.datasource.url=jdbc:h2:mem:mydb" })
30+
@Sql(scripts = "classpath:org/springframework/ai/chat/memory/repository/jdbc/schema-h2.sql",
31+
executionPhase = Sql.ExecutionPhase.BEFORE_TEST_CLASS)
32+
class JdbcChatMemoryRepositoryH2IT extends AbstractJdbcChatMemoryRepositoryIT {
33+
34+
}

0 commit comments

Comments
 (0)