Skip to content
Open
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 @@ -85,6 +85,13 @@
<optional>true</optional>
</dependency>

<dependency>
<groupId>org.xerial</groupId>
<artifactId>sqlite-jdbc</artifactId>
<scope>test</scope>
<optional>true</optional>
</dependency>

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ static JdbcChatMemoryRepositoryDialect from(DataSource dataSource) {
case "MySQL", "MariaDB" -> new MysqlChatMemoryRepositoryDialect();
case "Microsoft SQL Server" -> new SqlServerChatMemoryRepositoryDialect();
case "HSQL Database Engine" -> new HsqldbChatMemoryRepositoryDialect();
case "SQLite" -> new SqliteChatMemoryRepositoryDialect();
default -> // Add more as needed
new PostgresChatMemoryRepositoryDialect();
};
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
/*
* Copyright 2024-2025 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

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

/**
* Sqlite dialect for chat memory repository.
*
* @author guan xu
* @since 1.1.0
*/
public class SqliteChatMemoryRepositoryDialect implements JdbcChatMemoryRepositoryDialect {

@Override
public String getSelectMessagesSql() {
return "SELECT content, type FROM SPRING_AI_CHAT_MEMORY WHERE conversation_id = ? ORDER BY timestamp";
}

@Override
public String getInsertMessageSql() {
return "INSERT INTO SPRING_AI_CHAT_MEMORY (conversation_id, content, type, timestamp) VALUES (?, ?, ?, ?)";
}

@Override
public String getSelectConversationIdsSql() {
return "SELECT DISTINCT conversation_id FROM SPRING_AI_CHAT_MEMORY";
}

@Override
public String getDeleteMessagesSql() {
return "DELETE FROM SPRING_AI_CHAT_MEMORY WHERE conversation_id = ?";
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
CREATE TABLE IF NOT EXISTS SPRING_AI_CHAT_MEMORY (
conversation_id TEXT NOT NULL,
content TEXT NOT NULL,
type TEXT NOT NULL,
timestamp INTEGER NOT NULL,
CHECK (type IN ('USER', 'ASSISTANT', 'SYSTEM', 'TOOL'))
);

CREATE INDEX IF NOT EXISTS SPRING_AI_CHAT_MEMORY_CONVERSATION_ID_TIMESTAMP_IDX
ON SPRING_AI_CHAT_MEMORY(conversation_id, timestamp);
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@

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

import java.sql.Timestamp;
import java.util.List;
import java.util.UUID;
import java.util.stream.Collectors;
Expand Down Expand Up @@ -84,7 +83,7 @@ void saveMessagesSingleMessage(String content, MessageType messageType) {
assertThat(result.get("conversation_id")).isEqualTo(conversationId);
assertThat(result.get("content")).isEqualTo(message.getText());
assertThat(result.get("type")).isEqualTo(messageType.name());
assertThat(result.get("timestamp")).isInstanceOf(Timestamp.class);
assertThat(result.get("timestamp")).isNotNull();
}

@Test
Expand Down Expand Up @@ -114,7 +113,7 @@ void saveMessagesMultipleMessages() {
assertThat(result.get("conversation_id")).isEqualTo(conversationId);
assertThat(result.get("content")).isEqualTo(message.getText());
assertThat(result.get("type")).isEqualTo(message.getMessageType().name());
assertThat(result.get("timestamp")).isInstanceOf(Timestamp.class);
assertThat(result.get("timestamp")).isNotNull();
}

var count = this.chatMemoryRepository.findByConversationId(conversationId).size();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/*
* Copyright 2023-2025 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

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

import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.TestPropertySource;
import org.springframework.test.context.jdbc.Sql;

/**
* Integration tests for {@link JdbcChatMemoryRepository} with Sqlite.
*
* @author guan xu
*/
@SpringBootTest
@TestPropertySource(properties = { "spring.datasource.url=jdbc:sqlite::memory:",
"spring.datasource.driver-class-name=org.sqlite.JDBC",
"spring.ai.chat.memory.repository.jdbc.initialize-schema=always" })
@Sql(scripts = "classpath:org/springframework/ai/chat/memory/repository/jdbc/schema-sqlite.sql")
class JdbcChatMemoryRepositorySqliteIT extends AbstractJdbcChatMemoryRepositoryIT {

}