Skip to content

Oracle: add OracleChatMemory support #3601

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 7 commits into
base: main
Choose a base branch
from
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 @@ -88,6 +88,14 @@
<optional>true</optional>
</dependency>

<!-- Oracle JDBC Driver -->
<dependency>
<groupId>com.oracle.database.jdbc</groupId>
<artifactId>ojdbc8</artifactId>
<version>21.13.0.0</version>
<scope>test</scope>
</dependency>

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
Expand All @@ -100,6 +108,14 @@
<scope>test</scope>
</dependency>

<!-- Testcontainers for Oracle -->
<dependency>
<groupId>org.testcontainers</groupId>
<artifactId>oracle-xe</artifactId>
<version>1.19.0</version>
<scope>test</scope>
</dependency>

<dependency>
<groupId>org.testcontainers</groupId>
<artifactId>postgresql</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
@@ -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";
}

}
Original file line number Diff line number Diff line change
@@ -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");
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
*
* @author Mark Pollack
* @author Yanming Zhou
* @author Xiaotong Fan
*/
public class JdbcChatMemoryRepositoryBuilderTests {

Expand Down Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
@@ -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 {

}