Skip to content

Commit

Permalink
Update raw template doc
Browse files Browse the repository at this point in the history
  • Loading branch information
heshanpadmasiri committed Oct 18, 2024
1 parent 7bf8e03 commit f98ab1c
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 47 deletions.
2 changes: 0 additions & 2 deletions examples/raw-templates/Ballerina.toml

This file was deleted.

36 changes: 10 additions & 26 deletions examples/raw-templates/raw_templates.bal
Original file line number Diff line number Diff line change
@@ -1,31 +1,15 @@
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");

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)`);
import ballerina/io;
import ballerina/lang.'object as 'object;

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();
}
function foo() returns boolean {
return false;
}

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}`);
public function main() {
int x = 5;
error y = error("foo");
'object:RawTemplate rawTemplate = `x is ${x}. y is ${y}. result of calling foo is ${foo()}`;
io:println(rawTemplate.strings);
io:println(rawTemplate.insertions);
}
19 changes: 2 additions & 17 deletions examples/raw-templates/raw_templates.md
Original file line number Diff line number Diff line change
@@ -1,22 +1,7 @@
# 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.

- 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.
>**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/).
Raw template is a backtick string without a tag (such as `string` or `xml`). Backtick string is a sequence of characters interleaved with interpolations (`${expression}`), wrapped by a pair of backticks. The result of evaluating such a raw template is a `RawTemplate` object that has two fields `(readonly & string[]) strings` and `(any|error)[] insertions`. `strings` array will have string literals in the backtick string broken at interpolations and `insertions` array will have the resultant value of evaluating each interpolation.

::: 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 :::
::: out raw_templates.out :::
4 changes: 2 additions & 2 deletions examples/raw-templates/raw_templates.out
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
$ bal run
{"ORDERID":1,"CUSTOMERID":1,"NOOFITEMS":20}
{"ORDERID":2,"CUSTOMERID":1,"NOOFITEMS":15}
["x is ",". y is ",". result of calling foo is ",""]
[5,error("foo"),false]

0 comments on commit f98ab1c

Please sign in to comment.