Jariko can execute the SQL embedded in RPG programs, through the connections of the Reload configuration
(Configuration.reloadConfig, see File access). The code is in
interpreter/sql/ and in ExecSqlStmt (parsing/ast/statements.kt).
Embedded SQL is executed only if both are true:
- the feature flag
EmbeddedSqlFlagis on (it is by default; turn it off with the system propertyjariko.features.EmbeddedSqlFlag=false, or withConfiguration.jarikoCallback.featureFlagIsOn); Configuration.reloadConfigis set.
Otherwise EXEC SQL stays a mock statement: the onMockStatement callback is invoked and the program
gets SQLCODE/SQLCOD 100 (DECLARE leaves them as they are). This is the behaviour of the previous releases.
Fixed format only (C/EXEC SQL ... C+ ... C/END-EXEC), in the main body and in subroutines.
| Statement | Notes |
|---|---|
SELECT ... INTO :a, :b ... |
one row; no row: SQLCODE 100; more than one row: -811, the first row is assigned (as DB2 does) |
INSERT, UPDATE, DELETE |
UPDATE/DELETE affecting no row: SQLCODE 100 |
PREPARE name FROM :var / FROM text |
the database validates the statement now, as DB2 does |
EXECUTE name [USING :a, :b], EXECUTE IMMEDIATE :var |
? markers are filled by USING |
DECLARE c [SCROLL] CURSOR [WITH HOLD] FOR name / FOR select |
a cursor opened before its DECLARE is executed is found in the program |
OPEN c [USING :a], CLOSE c |
open cursor: -502, not open cursor: -501 |
FETCH [NEXT|PRIOR|FIRST|LAST|BEFORE|AFTER|CURRENT|ABSOLUTE n|RELATIVE n] [FROM] c [INTO :a, :b] |
no row: SQLCODE 100 |
COMMIT, ROLLBACK, SET OPTION |
SET OPTION is ignored, except CLOSQLCSR (see below). COMMIT/ROLLBACK with no connection used yet do nothing |
VALUES expr INTO :a, WITH ... SELECT ... INTO :a |
handled like SELECT INTO |
| any other statement without result set | executed as it is |
Not supported: free format exec sql ... ;, WHENEVER, DESCRIBE/SQLDA, USING DESCRIPTOR,
qualified (ds.field) host variables, FETCH ... FOR n ROWS, multiple row INSERT from arrays.
An unsupported statement gives SQLCODE -84.
DB2 for i resolves unquoted names in upper case, PostgreSQL in lower case, while the tables created through Reload on
PostgreSQL are quoted upper case ("MUDB010F"). On a PostgreSQL connection the unquoted identifiers of the statement
(tables, columns, aliases) are therefore quoted in upper case, as DB2 would resolve them. Keywords, functions,
literals, quoted names, numbers and :: casts are left as they are, and RRN(t) becomes "T"."__RNN". The
rewritten statement is cached.
DELETE FROM t WHERE CURRENT OF cursor deletes the row the cursor is on; the cursor then goes before the next row,
so the next FETCH returns the following one. The cursor must be declared FOR UPDATE (it is opened updatable). The
codes were verified on IBM i: cursor not open -507, not positioned on a row -508, not FOR UPDATE -510. Positioned
UPDATE is not supported.
After every statement the program finds the result in SQLCODE, SQLSTATE and the RPG III names SQLCOD and
SQLERM (message). They are implicitly defined by EXEC SQL; a program which declares them itself keeps its
own declaration. Errors never stop the program: they are reported there.
The database error is mapped by SQLSTATE class, so it works for any database:
| SQLCODE | When |
|---|---|
| 0 | success |
| 100 | no row (SELECT INTO, FETCH, UPDATE, DELETE) |
| -104 | syntax error (SQLSTATE class 42) |
| -204 | table not found |
| -206 | host variable not found |
| -302 | data conversion error (class 22) |
| -407, -530, -545, -803 | null / foreign key / check / unique violations |
| -501, -502, -504 | cursor not open / already open / not defined |
| -518 | EXECUTE of a statement which was not prepared |
| -811 | SELECT INTO with more than one row |
| -911 | any other database error |
| -30080 | no usable connection |
The SQLSTATE is the one reported by the database.
:name in the statement is replaced by a ? parameter and bound by the type of its current value
(character, number, indicator). Names may contain £ § @ # $ _. Quoted literals and PostgreSQL's :: casts
are not touched. Fields of a data structure can be used as they are in the RPG source.
-
A data structure as the only host variable of
INSERT INTO t VALUES(:ds),UPDATE t SET ROW = :ds WHERE ...,FETCH c INTO :dsandSELECT ... INTO :dsstands for its subfields, in declaration order, one per column (as on IBM i). ForINSERTthe statement gets an explicit column list and forUPDATE ... SET ROWan explicitSET "c1" = ?, ...list: the columns of the table, read once per table, without the hidden ones Reload adds (__RNN,__SID,__TSTAMP); a different number of subfields and columns is SQLCODE -206. ForFETCH/SELECT INTOthe hidden columns are skipped too. Other uses of a structure (inWHERE, among several host variables) are not supported. -
Trailing blanks of an alphanumeric variable are removed when it is bound.
-
Indicator variables (
:host:indicator, inINTOandUSING, and in any statement text) are supported. Reading aNULLcolumn leaves the host variable as it is and sets the indicator to -1, otherwise the indicator is 0. Writing, an indicator below zero sendsNULL. -
A
NULLcolumn read into a variable without indicator assigns blanks / zero (DB2 would give SQLCODE -305): this is on purpose, so that data migrated from IBM i with default values and NULLs keeps working.
Embedded SQL has no file name, but the Reload configuration chooses the connection by file name. Jariko uses
the first table the statement names (after FROM, INTO, UPDATE, JOIN; library qualifiers and quotes
removed) as the name to resolve, so the fileName patterns of the connection configuration work as they do
for RPG files, and a * entry matches everything. For PREPARE it is the table of the prepared text.
COMMIT/ROLLBACK and the statements naming no table use the connection of the previous statement.
A cursor, once opened, always stays on its own connection.
If a Reload scope is active (ConnectionProvider.withScope), its connection is used, so the embedder can
share its own transaction. Otherwise Jariko opens the connection itself and closes it with the execution context.
Only JDBC connections (jdbc: URLs, SQLDBMManager) are supported. Another kind (for example as400:)
gives SQLCODE -30080.
They belong to the program which created them (a called program has its own C1). Like on IBM i, how long they
live depends on the CLOSQLCSR option of the program:
*ENDACTGRP(the default, and whatSET OPTION CloSqlCsr = *ENDACTGRPsays): they live until the execution context ends, when open cursors are closed. A program which ends withoutCLOSEand is called again finds the cursor still open (SQLCODE -502);*ENDMOD/*ENDPGM(SET OPTION CloSqlCsr = *ENDMOD, as inQILEGEN/£INIZSQ2): when the program ends, its cursors are closed and its prepared statements forgotten.
The option is read when its SET OPTION statement is executed. The *COP* CLOSQLCSR(...) compile comments
of the sources are not read.
A runtime failure of the SQL layer which is not an SQLException (for example while connecting) is reported
as SQLCODE -911; the program never gets a Kotlin exception from EXEC SQL.
EmbeddedSqlTestBase (src/test/kotlin/.../db) holds the scenarios, written once. They run on the HSQLDB
of the DB tests (EmbeddedSqlTest) and on a real PostgreSQL (EmbeddedSqlPostgresTest, skipped if none is
reachable: the Postgres tests expect localhost:5432, user and password root, or PG_USER/PG_PASSWORD):
docker run -d --rm -p 5432:5432 -e POSTGRES_USER=root -e POSTGRES_PASSWORD=root postgres:16
./gradlew :rpgJavaInterpreter-core:test --tests "*EmbeddedSql*"