Skip to content

Introduce default methods for JdbcChatMemoryRepositoryDialect #3204

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 1 commit 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 @@ -18,27 +18,10 @@

/**
* HSQLDB-specific SQL dialect for chat memory repository.
*
* @author Mark Pollack
* @author Yanming Zhou
*/
public class HsqldbChatMemoryRepositoryDialect implements JdbcChatMemoryRepositoryDialect {

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

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

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

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

}
Original file line number Diff line number Diff line change
Expand Up @@ -20,29 +20,49 @@

/**
* Abstraction for database-specific SQL for chat memory repository.
*
* @author Mark Pollack
* @author Yanming Zhou
*/
public interface JdbcChatMemoryRepositoryDialect {

/**
* Returns the SQL to fetch messages for a conversation, ordered by timestamp, with
* limit.
*/
String getSelectMessagesSql();
default String getSelectMessagesSql() {
return "SELECT content, type FROM SPRING_AI_CHAT_MEMORY WHERE conversation_id = ? ORDER BY "
+ escape("timestamp") + " ASC";
}

/**
* Returns the SQL to insert a message.
*/
String getInsertMessageSql();
default String getInsertMessageSql() {
return "INSERT INTO SPRING_AI_CHAT_MEMORY (conversation_id, content, type, " + escape("timestamp")
+ ") VALUES (?, ?, ?, ?)";
}

/**
* Returns the SQL to fetch conversation IDs.
*/
String getSelectConversationIdsSql();
default String getSelectConversationIdsSql() {
return "SELECT DISTINCT conversation_id FROM SPRING_AI_CHAT_MEMORY";
}

/**
* Returns the SQL to delete all messages for a conversation.
*/
String getDeleteMessagesSql();
default String getDeleteMessagesSql() {
return "DELETE FROM SPRING_AI_CHAT_MEMORY WHERE conversation_id = ?";
}

/**
* Escape keywords as column name.
*/
default String escape(String identifier) {
return identifier;
}

/**
* Optionally, dialect can provide more advanced SQL as needed.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,28 +20,14 @@
* MySQL dialect for chat memory repository.
*
* @author Mark Pollack
* @author Yanming Zhou
* @since 1.0.0
*/
public class MysqlChatMemoryRepositoryDialect 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 = ?";
public String escape(String identifier) {
return "`" + identifier + "`";
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -20,28 +20,14 @@
* Dialect for Postgres.
*
* @author Mark Pollack
* @author Yanming Zhou
* @since 1.0.0
*/
public class PostgresChatMemoryRepositoryDialect 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 = ?";
public String escape(String identifier) {
return "\"" + identifier + "\"";
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -20,28 +20,14 @@
* Dialect for SQL Server.
*
* @author Mark Pollack
* @author Yanming Zhou
* @since 1.0.0
*/
public class SqlServerChatMemoryRepositoryDialect 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 = ?";
public String escape(String identifier) {
return "[" + identifier + "]";
}

}