Skip to content

Commit 02ce162

Browse files
committed
Introduce H2ChatMemoryRepositoryDialect
It seems that H2 is more populate than HSQLDB. Signed-off-by: Yanming Zhou <[email protected]>
1 parent 144ae1e commit 02ce162

File tree

5 files changed

+101
-4
lines changed

5 files changed

+101
-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
@@ -80,6 +80,13 @@
8080
<optional>true</optional>
8181
</dependency>
8282

83+
<dependency>
84+
<groupId>com.h2database</groupId>
85+
<artifactId>h2</artifactId>
86+
<scope>test</scope>
87+
<optional>true</optional>
88+
</dependency>
89+
8390
<dependency>
8491
<groupId>org.springframework.boot</groupId>
8592
<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: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -58,10 +58,7 @@ static JdbcChatMemoryRepositoryDialect from(DataSource dataSource) {
5858
if (url.contains("postgresql")) {
5959
return new PostgresChatMemoryRepositoryDialect();
6060
}
61-
if (url.contains("mysql")) {
62-
return new MysqlChatMemoryRepositoryDialect();
63-
}
64-
if (url.contains("mariadb")) {
61+
if (url.contains("mysql") || url.contains("mariadb")) {
6562
return new MysqlChatMemoryRepositoryDialect();
6663
}
6764
if (url.contains("sqlserver")) {
@@ -70,6 +67,9 @@ static JdbcChatMemoryRepositoryDialect from(DataSource dataSource) {
7067
if (url.contains("hsqldb")) {
7168
return new HsqldbChatMemoryRepositoryDialect();
7269
}
70+
if (url.contains("h2")) {
71+
return new H2ChatMemoryRepositoryDialect();
72+
}
7373
// Add more as needed
7474
}
7575
catch (Exception ignored) {
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)