diff --git a/memory/repository/spring-ai-model-chat-memory-repository-jdbc/pom.xml b/memory/repository/spring-ai-model-chat-memory-repository-jdbc/pom.xml index 75db46a8e1a..8b57633f92e 100644 --- a/memory/repository/spring-ai-model-chat-memory-repository-jdbc/pom.xml +++ b/memory/repository/spring-ai-model-chat-memory-repository-jdbc/pom.xml @@ -88,6 +88,14 @@ true + + + com.oracle.database.jdbc + ojdbc8 + 21.13.0.0 + test + + org.springframework.boot spring-boot-starter-test @@ -100,6 +108,14 @@ test + + + org.testcontainers + oracle-xe + 1.19.0 + test + + org.testcontainers postgresql diff --git a/memory/repository/spring-ai-model-chat-memory-repository-jdbc/src/main/java/org/springframework/ai/chat/memory/repository/jdbc/JdbcChatMemoryRepositoryDialect.java b/memory/repository/spring-ai-model-chat-memory-repository-jdbc/src/main/java/org/springframework/ai/chat/memory/repository/jdbc/JdbcChatMemoryRepositoryDialect.java index 526c0908c77..6de842bd01d 100644 --- a/memory/repository/spring-ai-model-chat-memory-repository-jdbc/src/main/java/org/springframework/ai/chat/memory/repository/jdbc/JdbcChatMemoryRepositoryDialect.java +++ b/memory/repository/spring-ai-model-chat-memory-repository-jdbc/src/main/java/org/springframework/ai/chat/memory/repository/jdbc/JdbcChatMemoryRepositoryDialect.java @@ -71,6 +71,9 @@ static JdbcChatMemoryRepositoryDialect from(DataSource dataSource) { if (url.contains("hsqldb")) { return new HsqldbChatMemoryRepositoryDialect(); } + if (url.contains("oracle")) { + return new OracleChatMemoryRepositoryDialect(); + } // Add more as needed } catch (Exception ignored) { diff --git a/memory/repository/spring-ai-model-chat-memory-repository-jdbc/src/main/java/org/springframework/ai/chat/memory/repository/jdbc/OracleChatMemoryRepositoryDialect.java b/memory/repository/spring-ai-model-chat-memory-repository-jdbc/src/main/java/org/springframework/ai/chat/memory/repository/jdbc/OracleChatMemoryRepositoryDialect.java new file mode 100644 index 00000000000..71708c21d63 --- /dev/null +++ b/memory/repository/spring-ai-model-chat-memory-repository-jdbc/src/main/java/org/springframework/ai/chat/memory/repository/jdbc/OracleChatMemoryRepositoryDialect.java @@ -0,0 +1,57 @@ +/* + * 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; + +/** + * Dialect for Oracle. + * + * @author Xiaotong Fan + * @since 1.1.0 + */ + +public class OracleChatMemoryRepositoryDialect 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 = ?"; + } + + /** + * This method requires uncommenting the Override annotation after the interface + * defines the method + */ + // @Override + public String getDeleteAllMessageSql() { + return "DELETE FROM SPRING_AI_CHAT_MEMORY"; + } + +} diff --git a/memory/repository/spring-ai-model-chat-memory-repository-jdbc/src/main/resources/org/springframework/ai/chat/memory/repository/jdbc/schema-oracle.sql b/memory/repository/spring-ai-model-chat-memory-repository-jdbc/src/main/resources/org/springframework/ai/chat/memory/repository/jdbc/schema-oracle.sql new file mode 100644 index 00000000000..d245ea65b48 --- /dev/null +++ b/memory/repository/spring-ai-model-chat-memory-repository-jdbc/src/main/resources/org/springframework/ai/chat/memory/repository/jdbc/schema-oracle.sql @@ -0,0 +1,9 @@ +CREATE TABLE SPRING_AI_CHAT_MEMORY ( + conversation_id VARCHAR2(36) NOT NULL, + content CLOB NOT NULL, + type VARCHAR2(10) NOT NULL CHECK (type IN ('USER', 'ASSISTANT', 'SYSTEM', 'TOOL')), + "timestamp" TIMESTAMP NOT NULL +); + +CREATE INDEX idx_conversation_id ON SPRING_AI_CHAT_MEMORY(conversation_id); +CREATE INDEX idx_conversation_id_timestamp ON SPRING_AI_CHAT_MEMORY(conversation_id, "timestamp"); \ No newline at end of file diff --git a/memory/repository/spring-ai-model-chat-memory-repository-jdbc/src/test/java/org/springframework/ai/chat/memory/repository/jdbc/JdbcChatMemoryRepositoryBuilderTests.java b/memory/repository/spring-ai-model-chat-memory-repository-jdbc/src/test/java/org/springframework/ai/chat/memory/repository/jdbc/JdbcChatMemoryRepositoryBuilderTests.java index 87dbfa5f03b..80f6a97f156 100644 --- a/memory/repository/spring-ai-model-chat-memory-repository-jdbc/src/test/java/org/springframework/ai/chat/memory/repository/jdbc/JdbcChatMemoryRepositoryBuilderTests.java +++ b/memory/repository/spring-ai-model-chat-memory-repository-jdbc/src/test/java/org/springframework/ai/chat/memory/repository/jdbc/JdbcChatMemoryRepositoryBuilderTests.java @@ -37,6 +37,7 @@ * * @author Mark Pollack * @author Yanming Zhou + * @author Xiaotong Fan */ public class JdbcChatMemoryRepositoryBuilderTests { @@ -136,6 +137,23 @@ void testBuilderWithHsqldbDialectFromDataSource() throws SQLException { assertThat(repository).isNotNull(); } + @Test + void testBuilderWithOracleDialectFromDataSource() throws SQLException { + // Setup mocks for Oracle + DataSource dataSource = mock(DataSource.class); + Connection connection = mock(Connection.class); + DatabaseMetaData metaData = mock(DatabaseMetaData.class); + + when(dataSource.getConnection()).thenReturn(connection); + when(connection.getMetaData()).thenReturn(metaData); + when(metaData.getURL()).thenReturn("jdbc:oracle:thin:@//192.168.19.129:1521/ORCL"); + + // Test with dialect from datasource + JdbcChatMemoryRepository repository = JdbcChatMemoryRepository.builder().dataSource(dataSource).build(); + + assertThat(repository).isNotNull(); + } + @Test void testBuilderWithUnknownDialectFromDataSource() throws SQLException { // Setup mocks for unknown database diff --git a/memory/repository/spring-ai-model-chat-memory-repository-jdbc/src/test/java/org/springframework/ai/chat/memory/repository/jdbc/JdbcChatMemoryRepositoryOracleIT.java b/memory/repository/spring-ai-model-chat-memory-repository-jdbc/src/test/java/org/springframework/ai/chat/memory/repository/jdbc/JdbcChatMemoryRepositoryOracleIT.java new file mode 100644 index 00000000000..7ed889c7430 --- /dev/null +++ b/memory/repository/spring-ai-model-chat-memory-repository-jdbc/src/test/java/org/springframework/ai/chat/memory/repository/jdbc/JdbcChatMemoryRepositoryOracleIT.java @@ -0,0 +1,34 @@ +/* + * 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 Oracle. + * + * @author Xiaotong Fan + */ + +@SpringBootTest +@TestPropertySource(properties = { "spring.datasource.url=jdbc:tc:oracle:21.3.0.0:///" }) +@Sql(scripts = "classpath:org/springframework/ai/chat/memory/repository/jdbc/schema-oracle.sql") +public class JdbcChatMemoryRepositoryOracleIT extends AbstractJdbcChatMemoryRepositoryIT { + +}