-
Notifications
You must be signed in to change notification settings - Fork 192
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
Update raw-template BBE to remove external dependency #5749
Merged
MaryamZi
merged 10 commits into
ballerina-platform:master
from
heshanpadmasiri:feat/raw-template
Oct 30, 2024
Merged
Changes from 6 commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
f98ab1c
Update raw template doc
heshanpadmasiri a941a78
Address review suggestions
heshanpadmasiri 1e62858
Add a better example
heshanpadmasiri 71a8d44
Change output
heshanpadmasiri a099fce
Update examples/raw-templates/raw_templates.md
heshanpadmasiri 451fb42
Fix comments
heshanpadmasiri 45aaeb7
Apply suggestions from code review
heshanpadmasiri 5ac3aeb
Apply suggestions from code review
heshanpadmasiri 7bea092
Describe common use case for raw templates
heshanpadmasiri 26e37b0
Apply suggestions from code review
heshanpadmasiri File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,31 +1,26 @@ | ||
import ballerina/io; | ||
import ballerinax/java.jdbc; | ||
import ballerina/sql; | ||
|
||
jdbc:Client dbClient = check new (url = "jdbc:h2:file:./master/orderdb", | ||
user = "test", password = "test"); | ||
function col3() returns boolean { | ||
return false; | ||
} | ||
|
||
public function main() returns error? { | ||
// Uses a raw template to create the `Orders` table. | ||
_ = check dbClient->execute(`CREATE TABLE IF NOT EXISTS Orders | ||
(orderId INTEGER NOT NULL, customerId INTEGER, noOfItems INTEGER, | ||
PRIMARY KEY (orderId))`); | ||
// Uses a raw template to insert values to the `Orders` table. | ||
_ = check dbClient->execute(`INSERT INTO Orders (orderId, customerId, noOfItems) | ||
VALUES (1, 1, 20)`); | ||
_ = check dbClient->execute(`INSERT INTO Orders (orderId, customerId, noOfItems) | ||
VALUES (2, 1, 15)`); | ||
type MyCSVRawTemplate object { | ||
*object:RawTemplate; | ||
public (string[] & readonly) strings; | ||
public [int, int, boolean] insertions; | ||
}; | ||
|
||
stream<record {| anydata...; |}, sql:Error?> strm = getOrders(1); | ||
record {|record {} value;|}|sql:Error? v = strm.next(); | ||
while (v is record {|record {} value;|}) { | ||
record {} value = v.value; | ||
io:println(value); | ||
v = strm.next(); | ||
} | ||
} | ||
public function main() { | ||
int col1 = 5; | ||
int col2 = 10; | ||
|
||
// Allow any value as interpolations since object:RawTemplate has any|error[] as insertions type. | ||
heshanpadmasiri marked this conversation as resolved.
Show resolved
Hide resolved
|
||
object:RawTemplate rawTemplate = `${col1}, fixed_string1, ${col2}, ${col3()}, fixed_string3`; | ||
io:println(rawTemplate.strings); | ||
heshanpadmasiri marked this conversation as resolved.
Show resolved
Hide resolved
|
||
io:println(rawTemplate.insertions); | ||
heshanpadmasiri marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
function getOrders(int customerId) returns stream<record {| anydata...; |}, sql:Error?> { | ||
// In this raw template, the `customerId` variable is interpolated in the literal. | ||
return dbClient->query(`SELECT * FROM orders WHERE customerId = ${customerId}`); | ||
// Ensure we have 2 ints followed by a boolean value as interpolations based on insertions type. | ||
heshanpadmasiri marked this conversation as resolved.
Show resolved
Hide resolved
|
||
MyCSVRawTemplate myCSVRawTemplate = `fixed_string4, ${col1}, ${col2}, fixed_string_5, ${col3()}`; | ||
io:println(myCSVRawTemplate.strings); | ||
io:println(myCSVRawTemplate.insertions); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,22 +1,13 @@ | ||
# Raw templates | ||
|
||
A raw template is a backtick template without a tag. Exposes result of phase 1 without further processing. Raw template is evaluated by evaluating each expression and creating an object containing. | ||
Raw template expressions are backtick templates without a tag (such as `string` or `xml`). This is a sequence of characters interleaved with interpolations within a pair of backticks (in the form `${expression}`). The result of evaluating such a raw template is a `RawTemplate` object that has two fields `(readonly & string[]) strings` and `(any|error)[] insertions`. The `strings` array will have string literals in the backtick string broken at interpolations and the `insertions` array will have the resultant values of evaluating each interpolation. | ||
heshanpadmasiri marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
- an `array` of the `strings` separated by insertions | ||
- an `array` of the results of expression evaluation and an `array` of `strings` separating | ||
|
||
>**Important use case:** SQL parameters. | ||
heshanpadmasiri marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
>**Note:** The relevant database driver JAR should be defined in the `Ballerina.toml` file as a dependency. This sample is based on an H2 database and the H2 database driver JAR need to be added to `Ballerina.toml` file. This sample is written using H2 2.1.210 and it is recommended to use H2 JAR with versions higher than 2.1.210. | ||
|
||
For a sample configuration and more information on the underlying module, see the [`jdbc` module](https://lib.ballerina.io/ballerinax/java.jdbc/latest/). | ||
If you want to control the type of values used for interpolation more precisely, you can define an object type that includes the `RawTemplate` type and give a narrower type for the `insertions` fields. Then, the compiler will statically verify that the corresponding values used for interpolation belong to the desired type. | ||
heshanpadmasiri marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
::: code raw_templates.bal ::: | ||
|
||
Add the relevant database driver JAR details to the `Ballerina.toml` file. | ||
|
||
::: code Ballerina.toml ::: | ||
|
||
Build and run the project using the `bal run` command. | ||
::: out raw_templates.out ::: | ||
heshanpadmasiri marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
::: out raw_templates.out ::: | ||
## Related links | ||
- [Backtick templates](https://ballerina.io/learn/by-example/backtick-templates/) | ||
- [Object type inclusion](https://ballerina.io/learn/by-example/object-type-inclusion/) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,5 @@ | ||
$ bal run | ||
{"ORDERID":1,"CUSTOMERID":1,"NOOFITEMS":20} | ||
{"ORDERID":2,"CUSTOMERID":1,"NOOFITEMS":15} | ||
["",", fixed_string1, ",", ",", fixed_string3"] | ||
[5,10,false] | ||
["fixed_string4, ",", ",", fixed_string_5, ",""] | ||
[5,10,false] |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this field is needed due to ballerina-platform/ballerina-lang#43501