Skip to content
Closed
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 @@ -50,13 +50,7 @@
<artifactId>spring-jdbc</artifactId>
</dependency>

<dependency>
<groupId>com.zaxxer</groupId>
<artifactId>HikariCP</artifactId>
</dependency>

<!-- TESTING -->

<dependency>
<groupId>org.postgresql</groupId>
<artifactId>postgresql</artifactId>
Expand Down Expand Up @@ -85,6 +79,20 @@
<optional>true</optional>
</dependency>

<!-- Oracle JDBC Driver -->
<dependency>
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since we prefer to use the test containers this dependency isn't required. Will remove when merging.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Meant to post it for the test container dependency not the JDBC driver one. Will keep this in.

<groupId>com.oracle.database.jdbc</groupId>
<artifactId>ojdbc11</artifactId>
<version>23.9.0.25.07</version>
<scope>test</scope>
</dependency>

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
<scope>test</scope>
</dependency>

<dependency>
<groupId>org.xerial</groupId>
<artifactId>sqlite-jdbc</artifactId>
Expand All @@ -111,6 +119,14 @@
<scope>test</scope>
</dependency>

<!-- Testcontainers for Oracle -->
<dependency>
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ran into issues when dropping table using this test container. Switched to use the latest testcontainers-oracle-free which works fine. Will update when merging.

<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 @@ -74,6 +74,7 @@ static JdbcChatMemoryRepositoryDialect from(DataSource dataSource) {
case "MySQL", "MariaDB" -> new MysqlChatMemoryRepositoryDialect();
case "Microsoft SQL Server" -> new SqlServerChatMemoryRepositoryDialect();
case "HSQL Database Engine" -> new HsqldbChatMemoryRepositoryDialect();
case "Oracle" -> new OracleChatMemoryRepositoryDialect();
case "SQLite" -> new SqliteChatMemoryRepositoryDialect();
case "H2" -> new H2ChatMemoryRepositoryDialect();
default -> // Add more as needed
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
/*
* 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 = ?";
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
DROP TABLE SPRING_AI_CHAT_MEMORY;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Need to use DROP TABLE IF EXISTS SPRING_AI_CHAT_MEMORY


CREATE TABLE SPRING_AI_CHAT_MEMORY (
CONVERSATION_ID VARCHAR2(36 CHAR) NOT NULL,
CONTENT CLOB NOT NULL,
"TYPE" VARCHAR2(10 CHAR) NOT NULL CHECK (
"TYPE" IN (
'USER',
'ASSISTANT',
'SYSTEM',
'TOOL'
)
),
"TIMESTAMP" TIMESTAMP NOT NULL
);

CREATE INDEX 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 @@ -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,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 Oracle.
*
* @author Xiaotong Fan
*/

@SpringBootTest
@TestPropertySource(properties = { "spring.datasource.url=jdbc:oracle:thin:@localhost:1521/FREEPDB1",
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Need to use test containers for the test. Will fix when merging.

"spring.datasource.username=scott", "spring.datasource.password=tiger" })
@Sql(scripts = "classpath:org/springframework/ai/chat/memory/repository/jdbc/schema-oracle.sql")
public class JdbcChatMemoryRepositoryOracleIT extends AbstractJdbcChatMemoryRepositoryIT {

}
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,7 @@ Spring AI supports multiple relational databases via a dialect abstraction. The
- MySQL / MariaDB
- SQL Server
- HSQLDB
- Oracle Database

The correct dialect can be auto-detected from the JDBC URL when using `JdbcChatMemoryRepositoryDialect.from(DataSource)`. You can extend support for other databases by implementing the `JdbcChatMemoryRepositoryDialect` interface.

Expand Down