Add SQL_SKEL3 using procedures
This commit is contained in:
parent
0279066993
commit
7c3704a9bf
2
.vscode/actions.json
vendored
2
.vscode/actions.json
vendored
@ -19,7 +19,7 @@
|
||||
},
|
||||
{
|
||||
"name": "Create SQLRPGLE Program",
|
||||
"command": "CRTSQLRPGI OBJ(&CURLIB/&NAME) SRCSTMF('&RELATIVEPATH') OPTION(*EVENTF) DBGVIEW(*SOURCE) CLOSQLCSR(*ENDMOD) CVTCCSID(*JOB) COMPILEOPT('TGTCCSID(*JOB)') RPGPPOPT(*LVL2)",
|
||||
"command": "CRTSQLRPGI OBJ(&CURLIB/&NAME) SRCSTMF('&RELATIVEPATH') OPTION(*EVENTF *SECLVL) DBGVIEW(*SOURCE) CLOSQLCSR(*ENDMOD) CVTCCSID(*JOB) COMPILEOPT('TGTCCSID(*JOB)') RPGPPOPT(*LVL2)",
|
||||
"deployFirst": true,
|
||||
"environment": "ile",
|
||||
"extensions": [
|
||||
|
||||
18
.vscode/rpglint.json
vendored
Normal file
18
.vscode/rpglint.json
vendored
Normal file
@ -0,0 +1,18 @@
|
||||
{
|
||||
"indent": 2,
|
||||
"PrototypeCheck": true,
|
||||
"NoOCCURS": true,
|
||||
"NoSELECTAll": true,
|
||||
"UppercaseConstants": true,
|
||||
"IncorrectVariableCase": true,
|
||||
"NoSQLJoins": true,
|
||||
"PrettyComments": true,
|
||||
"NoGlobalSubroutines": true,
|
||||
"NoExternalTo": [
|
||||
"QCMD",
|
||||
"QP2TERM",
|
||||
"QSH",
|
||||
"SYSTEM",
|
||||
"QCMDEXC"
|
||||
]
|
||||
}
|
||||
@ -1,6 +1,6 @@
|
||||
### SQL_SKELETON
|
||||
|
||||
Batch program with SQL embedded in RPGLE. Includes error checking and handling. It could be the starting point for any embedded SQL program. There are versions in completely free format and in not free format where the D-Specs are in fixed format.
|
||||
Batch program with SQL embedded in RPGLE. Includes error checking and handling. It could be the starting point for any embedded SQL program. There are various evolutionary versions. *SQL_SKEL3* is the most current one.
|
||||
|
||||
*SQL_SKEL* is completely free format, i.e., code begins in column 1.
|
||||
|
||||
@ -8,8 +8,12 @@ Batch program with SQL embedded in RPGLE. Includes error checking and handling.
|
||||
|
||||
*SQL_SKEL2* is an upgrade to SQL_SKEL. The SQLProblem procedure is now in the SRV_SQL service program, so the code is much shorter. This is completely free format.
|
||||
|
||||
*SQL_SKEL3* is an upgrade to SQL_SKEL2. It uses procedures instead of subroutines. It is interesting getting the SQL pre-compiler to co-operate when trying to minimize global variables. Seems like the `declare ... cursor` needs to be global. If anyone has a better technique I'd like to know.
|
||||
|
||||
I also did some renaming of variables so that code assist works more conveniently in the Code for i extension.
|
||||
|
||||
**NOTE:** There is a deliberate duplicated line in the SQL_SKEL2 code so that it triggers the SQLProblem handling procedure.
|
||||
|
||||
You should be able to compile any version of the program without any changes and run it on virtually any IBM i with a reasonably current OS release. I developed it on a V7R4 machine. If you are at an older release then the not totally free version will probably compile.
|
||||
You should be able to compile any version of the program without any changes and run it on virtually any IBM i with a reasonably current OS release. I developed on V7R4 and V7R5 machines. If you are at an older release then the not totally free version will probably compile.
|
||||
|
||||
*SQLC and SQLC2* are simple CL program that call SQL_SKEL and SQL_SKEL2, so that you can see the difference in how SQL errors are reported. Call them from QCMD or the Main Menu.
|
||||
|
||||
131
SQL_SKELETON/SQL_SKEL3.SQLRPGLE
Normal file
131
SQL_SKELETON/SQL_SKEL3.SQLRPGLE
Normal file
@ -0,0 +1,131 @@
|
||||
**free
|
||||
// Sample Skeleton SQL RPG program. This is a linear main program
|
||||
// so there is no RPG cycle logic included. There is no RPG file
|
||||
// IO. Data is read with SQL. It uses an SQL Cursor, but this is
|
||||
// just one way to retrieve data with SQL.
|
||||
//
|
||||
// After *executable* SQL statements I check for the SQL States
|
||||
// that I expect. Any unexpected state causes the program to dump
|
||||
// and crash, because this requires investigation.
|
||||
// Common states are defined below in SQL State Constants.
|
||||
//
|
||||
// Your shop error handling standards may be different and you
|
||||
// may want to rewrite the SQLProblem procedure.
|
||||
|
||||
// Program reads table QIWS/QCUSTCDT which is on all(?) machines.
|
||||
// Pass a two character State parameter, e.g. PARM('NY').
|
||||
// -----------------------------------------------------------------
|
||||
// 10/--/2021 Lennon. This is a revised version of the original
|
||||
// SQL_SKEL program.
|
||||
// I trimmed down the code by moving the SQLProblem
|
||||
// logic into the SRV_SQL service program. The SQLProblem
|
||||
// messaging is also improved.
|
||||
// 05/--/2024 Lennon. This is a revised version of SQL_SKEL2 and uses
|
||||
// procedures instead of subroutines and attempts to avoid
|
||||
// global variables.
|
||||
// I have also changed the naming approach.
|
||||
|
||||
ctl-opt option(*nodebugio:*srcstmt) dftactgrp(*no) actgrp(*caller)
|
||||
bnddir('SQL_BND')
|
||||
main(SQL_SKEL);
|
||||
|
||||
// === Prototypes ======================================
|
||||
/include ../Copy_Mbrs/SRV_SQL_P.RPGLE
|
||||
|
||||
// === SQL State Constants =============================
|
||||
dcl-c SQLSUCCESS '00000';
|
||||
dcl-c SQLNODATA '02000';
|
||||
dcl-c SQLNOMOREDATA '02000';
|
||||
dcl-c SQLDUPRECD '23505';
|
||||
dcl-c SQLROWLOCKED '57033';
|
||||
|
||||
// === Templates =======================================
|
||||
dcl-ds InTbl extname('QIWS/QCUSTCDT') template
|
||||
end-ds;
|
||||
|
||||
// The data returned from balanceCursor
|
||||
dcl-ds t_balancesData qualified template;
|
||||
LstNam like(lstnam);
|
||||
Init like(init);
|
||||
BalDue like(baldue);
|
||||
end-ds;
|
||||
|
||||
// =+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=
|
||||
// === Main Program === =
|
||||
// =+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=
|
||||
dcl-proc SQL_SKEL;
|
||||
dcl-pi *n;
|
||||
piState char(2);
|
||||
end-pi;
|
||||
|
||||
// === Global variables ==============================
|
||||
// SQL balanceCursor returns data here.
|
||||
dcl-ds balanceData likeds(t_balancesData);
|
||||
|
||||
dcl-s recordsRead packed(7) inz(0);
|
||||
|
||||
// === Set SQL Options ===============================
|
||||
// Note: Compile time only. Not executable.
|
||||
exec sql set option datfmt=*iso,
|
||||
closqlcsr=*endmod,
|
||||
commit=*none;
|
||||
// === Cursor ========================================
|
||||
exec sql declare balanceCursor cursor for
|
||||
select
|
||||
LSTNAM,
|
||||
INIT,
|
||||
BALDUE
|
||||
from QIWS/QCUSTCDT
|
||||
where STATE = :piState
|
||||
order by BALDUE desc
|
||||
;
|
||||
|
||||
// === Initialization ================================
|
||||
exec sql open balanceCursor;
|
||||
if SQLSTT <> SQLSUCCESS;
|
||||
SQLProblem('open balanceCursor');
|
||||
endif;
|
||||
|
||||
// === Main Logic loop ===============================
|
||||
balanceFetch(balanceData);
|
||||
dow SQLSTT = SQLSUCCESS;
|
||||
recordsRead += 1;
|
||||
// >>>>>>>> Real program logic goes here <<<<<<<<<<
|
||||
// Demo code just to prove data is returned
|
||||
snd-msg *INFO 'Balance for ' +
|
||||
balanceData.Init + ' ' +
|
||||
balanceData.LstNam + ': ' +
|
||||
%char(balanceData.BalDue);
|
||||
balanceFetch(balanceData);
|
||||
enddo;
|
||||
|
||||
// === Termination ===================================
|
||||
snd-msg *INFO (piState + ' records read: ' + %char(recordsRead) );
|
||||
exec sql close balanceCursor;
|
||||
if SQLSTT <> SQLSUCCESS;
|
||||
SQLProblem('close balanceCursor');
|
||||
endif;
|
||||
*inlr = *on;
|
||||
// =+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=
|
||||
// === End of Main Program === =
|
||||
// =+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=
|
||||
end-proc;
|
||||
|
||||
// =+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=
|
||||
// === Procedures === =
|
||||
// =+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=
|
||||
|
||||
// --- balanceFetch(target DS) -----------------------
|
||||
// Populates a data structure defined like t_balancesData
|
||||
// with the next row from balanceCursor.
|
||||
// Returns: SQLSUCCESS, with data
|
||||
// SQLNoMoreData, no data returned
|
||||
dcl-proc balanceFetch;
|
||||
dcl-pi balanceFetch ;
|
||||
balDS likeds(t_balancesData);
|
||||
end-pi;
|
||||
exec sql fetch balanceCursor into :balDS;
|
||||
if SQLSTT <> SQLSUCCESS and SQLSTT <> SQLNOMOREDATA;
|
||||
SQLProblem('fetch balanceCursor');
|
||||
endif;
|
||||
end-proc ;
|
||||
Loading…
x
Reference in New Issue
Block a user