Skip to content

Commit 2867034

Browse files
committed
Introduce default methods for JdbcChatMemoryRepositoryDialect
Signed-off-by: Yanming Zhou <[email protected]>
1 parent b219c21 commit 2867034

File tree

5 files changed

+36
-75
lines changed

5 files changed

+36
-75
lines changed

memory/repository/spring-ai-model-chat-memory-repository-jdbc/src/main/java/org/springframework/ai/chat/memory/repository/jdbc/HsqldbChatMemoryRepositoryDialect.java

Lines changed: 3 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -18,27 +18,10 @@
1818

1919
/**
2020
* HSQLDB-specific SQL dialect for chat memory repository.
21+
*
22+
* @author Mark Pollack
23+
* @author Yanming Zhou
2124
*/
2225
public class HsqldbChatMemoryRepositoryDialect implements JdbcChatMemoryRepositoryDialect {
2326

24-
@Override
25-
public String getSelectMessagesSql() {
26-
return "SELECT content, type FROM SPRING_AI_CHAT_MEMORY WHERE conversation_id = ? ORDER BY timestamp ASC";
27-
}
28-
29-
@Override
30-
public String getInsertMessageSql() {
31-
return "INSERT INTO SPRING_AI_CHAT_MEMORY (conversation_id, content, type, timestamp) VALUES (?, ?, ?, ?)";
32-
}
33-
34-
@Override
35-
public String getDeleteMessagesSql() {
36-
return "DELETE FROM SPRING_AI_CHAT_MEMORY WHERE conversation_id = ?";
37-
}
38-
39-
@Override
40-
public String getSelectConversationIdsSql() {
41-
return "SELECT DISTINCT conversation_id FROM SPRING_AI_CHAT_MEMORY";
42-
}
43-
4427
}

memory/repository/spring-ai-model-chat-memory-repository-jdbc/src/main/java/org/springframework/ai/chat/memory/repository/jdbc/JdbcChatMemoryRepositoryDialect.java

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,29 +20,49 @@
2020

2121
/**
2222
* Abstraction for database-specific SQL for chat memory repository.
23+
*
24+
* @author Mark Pollack
25+
* @author Yanming Zhou
2326
*/
2427
public interface JdbcChatMemoryRepositoryDialect {
2528

2629
/**
2730
* Returns the SQL to fetch messages for a conversation, ordered by timestamp, with
2831
* limit.
2932
*/
30-
String getSelectMessagesSql();
33+
default String getSelectMessagesSql() {
34+
return "SELECT content, type FROM SPRING_AI_CHAT_MEMORY WHERE conversation_id = ? ORDER BY "
35+
+ escape("timestamp") + " ASC";
36+
}
3137

3238
/**
3339
* Returns the SQL to insert a message.
3440
*/
35-
String getInsertMessageSql();
41+
default String getInsertMessageSql() {
42+
return "INSERT INTO SPRING_AI_CHAT_MEMORY (conversation_id, content, type, " + escape("timestamp")
43+
+ ") VALUES (?, ?, ?, ?)";
44+
}
3645

3746
/**
3847
* Returns the SQL to fetch conversation IDs.
3948
*/
40-
String getSelectConversationIdsSql();
49+
default String getSelectConversationIdsSql() {
50+
return "SELECT DISTINCT conversation_id FROM SPRING_AI_CHAT_MEMORY";
51+
}
4152

4253
/**
4354
* Returns the SQL to delete all messages for a conversation.
4455
*/
45-
String getDeleteMessagesSql();
56+
default String getDeleteMessagesSql() {
57+
return "DELETE FROM SPRING_AI_CHAT_MEMORY WHERE conversation_id = ?";
58+
}
59+
60+
/**
61+
* Escape keywords as column name.
62+
*/
63+
default String escape(String identifier) {
64+
return identifier;
65+
}
4666

4767
/**
4868
* Optionally, dialect can provide more advanced SQL as needed.

memory/repository/spring-ai-model-chat-memory-repository-jdbc/src/main/java/org/springframework/ai/chat/memory/repository/jdbc/MysqlChatMemoryRepositoryDialect.java

Lines changed: 3 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -20,28 +20,14 @@
2020
* MySQL dialect for chat memory repository.
2121
*
2222
* @author Mark Pollack
23+
* @author Yanming Zhou
2324
* @since 1.0.0
2425
*/
2526
public class MysqlChatMemoryRepositoryDialect implements JdbcChatMemoryRepositoryDialect {
2627

2728
@Override
28-
public String getSelectMessagesSql() {
29-
return "SELECT content, type FROM SPRING_AI_CHAT_MEMORY WHERE conversation_id = ? ORDER BY `timestamp`";
30-
}
31-
32-
@Override
33-
public String getInsertMessageSql() {
34-
return "INSERT INTO SPRING_AI_CHAT_MEMORY (conversation_id, content, type, `timestamp`) VALUES (?, ?, ?, ?)";
35-
}
36-
37-
@Override
38-
public String getSelectConversationIdsSql() {
39-
return "SELECT DISTINCT conversation_id FROM SPRING_AI_CHAT_MEMORY";
40-
}
41-
42-
@Override
43-
public String getDeleteMessagesSql() {
44-
return "DELETE FROM SPRING_AI_CHAT_MEMORY WHERE conversation_id = ?";
29+
public String escape(String identifier) {
30+
return "`" + identifier + "`";
4531
}
4632

4733
}

memory/repository/spring-ai-model-chat-memory-repository-jdbc/src/main/java/org/springframework/ai/chat/memory/repository/jdbc/PostgresChatMemoryRepositoryDialect.java

Lines changed: 3 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -20,28 +20,14 @@
2020
* Dialect for Postgres.
2121
*
2222
* @author Mark Pollack
23+
* @author Yanming Zhou
2324
* @since 1.0.0
2425
*/
2526
public class PostgresChatMemoryRepositoryDialect implements JdbcChatMemoryRepositoryDialect {
2627

2728
@Override
28-
public String getSelectMessagesSql() {
29-
return "SELECT content, type FROM SPRING_AI_CHAT_MEMORY WHERE conversation_id = ? ORDER BY \"timestamp\"";
30-
}
31-
32-
@Override
33-
public String getInsertMessageSql() {
34-
return "INSERT INTO SPRING_AI_CHAT_MEMORY (conversation_id, content, type, \"timestamp\") VALUES (?, ?, ?, ?)";
35-
}
36-
37-
@Override
38-
public String getSelectConversationIdsSql() {
39-
return "SELECT DISTINCT conversation_id FROM SPRING_AI_CHAT_MEMORY";
40-
}
41-
42-
@Override
43-
public String getDeleteMessagesSql() {
44-
return "DELETE FROM SPRING_AI_CHAT_MEMORY WHERE conversation_id = ?";
29+
public String escape(String identifier) {
30+
return "\"" + identifier + "\"";
4531
}
4632

4733
}

memory/repository/spring-ai-model-chat-memory-repository-jdbc/src/main/java/org/springframework/ai/chat/memory/repository/jdbc/SqlServerChatMemoryRepositoryDialect.java

Lines changed: 3 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -20,28 +20,14 @@
2020
* Dialect for SQL Server.
2121
*
2222
* @author Mark Pollack
23+
* @author Yanming Zhou
2324
* @since 1.0.0
2425
*/
2526
public class SqlServerChatMemoryRepositoryDialect implements JdbcChatMemoryRepositoryDialect {
2627

2728
@Override
28-
public String getSelectMessagesSql() {
29-
return "SELECT content, type FROM SPRING_AI_CHAT_MEMORY WHERE conversation_id = ? ORDER BY [timestamp]";
30-
}
31-
32-
@Override
33-
public String getInsertMessageSql() {
34-
return "INSERT INTO SPRING_AI_CHAT_MEMORY (conversation_id, content, type, [timestamp]) VALUES (?, ?, ?, ?)";
35-
}
36-
37-
@Override
38-
public String getSelectConversationIdsSql() {
39-
return "SELECT DISTINCT conversation_id FROM SPRING_AI_CHAT_MEMORY";
40-
}
41-
42-
@Override
43-
public String getDeleteMessagesSql() {
44-
return "DELETE FROM SPRING_AI_CHAT_MEMORY WHERE conversation_id = ?";
29+
public String escape(String identifier) {
30+
return "[" + identifier + "]";
4531
}
4632

4733
}

0 commit comments

Comments
 (0)