Skip to content

Comments

Add support for specifying a custom table name for storing chat messages#5

Merged
SasinduDilshara merged 4 commits intoballerina-platform:mainfrom
MohamedSabthar:main
Nov 26, 2025
Merged

Add support for specifying a custom table name for storing chat messages#5
SasinduDilshara merged 4 commits intoballerina-platform:mainfrom
MohamedSabthar:main

Conversation

@MohamedSabthar
Copy link
Member

@MohamedSabthar MohamedSabthar commented Nov 20, 2025

Purpose

Fixes: wso2/product-ballerina-integrator#1976

Examples

Checklist

  • Linked to an issue
  • Added tests
  • Checked native-image compatibility

@MohamedSabthar MohamedSabthar marked this pull request as draft November 20, 2025 16:16
@MohamedSabthar MohamedSabthar marked this pull request as ready for review November 21, 2025 04:48
Copy link

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull Request Overview

This PR adds support for specifying a custom table name for storing chat messages in the MS SQL-backed short-term memory store, addressing issue #1976. The implementation includes proper input validation to prevent SQL injection vulnerabilities while maintaining backward compatibility through a default table name value.

Key Changes:

  • Added a tableName parameter to ShortTermMemoryStore.init() with validation and a default value of "ChatMessages"
  • Implemented a placeholder-based table name replacement mechanism for all SQL queries
  • Version bumped from 1.0.x to 1.1.0, reflecting a minor feature addition

Reviewed Changes

Copilot reviewed 4 out of 4 changed files in this pull request and generated 2 comments.

File Description
gradle.properties Version bump to 1.1.0-SNAPSHOT for the new feature
ballerina/Ballerina.toml Package version updated to 1.1.0
ballerina/Dependencies.toml Added lang.regexp dependency and updated package version; also includes transitive dependency updates (http 2.14.8, mime 2.12.1)
ballerina/store.bal Core implementation: added tableName parameter with regex validation, introduced replaceTableNamePlaceholder function, and updated all SQL queries to use the dynamic table name

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.


if tableExists is sql:Error {
return error("Failed to check existence of the ChatMessages table: " + tableExists.message(),
return error(string `Failed to check existence of the $_tableName_$ table: ` + tableExists.message(),
Copy link
Member

Choose a reason for hiding this comment

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

Invalid change?

Copy link
Member Author

Choose a reason for hiding this comment

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

Already fixed it via: 4470a7c

}
}

isolated function replaceTableNamePlaceholder(sql:ParameterizedQuery query, string tableName) returns sql:ParameterizedQuery {
Copy link
Member

Choose a reason for hiding this comment

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

Can fix later, but technically we are creating two objects for each DB query. Also the table name is fixed at initialization. Wonder if we can absorb some pain and improve this, given that having to change the table name may not be common.

Copy link
Member Author

Choose a reason for hiding this comment

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

Are we creating two objects for each query? I believe we're creating a single object and updating the same one.
I agree with the second point, but I don't think we have a cleaner way to handle this since the SQL library doesn’t provide functionality for this kind of scenario, AFAIK. we’ll proceed with this approach for now.

Copy link
Member

Choose a reason for hiding this comment

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

Yeah, my bad. We can still avoid the repeated array creation though. We can do this separately.

Copy link
Member

Choose a reason for hiding this comment

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

One approach would be to use function pointers (with a closure for when the table name is a variable).

}
}

isolated function replaceTableNamePlaceholder(sql:ParameterizedQuery query, string tableName) returns sql:ParameterizedQuery {
Copy link
Member

Choose a reason for hiding this comment

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

Yeah, my bad. We can still avoid the repeated array creation though. We can do this separately.


if tableExists is sql:Error {
return error("Failed to check existence of the ChatMessages table: " + tableExists.message(),
return error(string `Failed to check existence of the ${self.tableName} table: ` + tableExists.message(),
Copy link
Member

Choose a reason for hiding this comment

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

tableExists.message() can also be an interpolation?

Copy link
Member

Choose a reason for hiding this comment

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

Applicable elsewhere too.

Copy link
Member Author

@MohamedSabthar MohamedSabthar Nov 21, 2025

Choose a reason for hiding this comment

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

Fixed with 37c2a6c

}
}

isolated function replaceTableNamePlaceholder(sql:ParameterizedQuery query, string tableName) returns sql:ParameterizedQuery {
Copy link
Member

Choose a reason for hiding this comment

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

One approach would be to use function pointers (with a closure for when the table name is a variable).

@SasinduDilshara SasinduDilshara merged commit 46e9b4a into ballerina-platform:main Nov 26, 2025
2 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Allow AI Agent MsSQL Short Term memory to support two agents in one database

4 participants