BirgittaHauser / Write-to-IFS-with-SQL

Generate XML and JSON data and write them directly to the IFS with SQL

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Write-to-IFS-with-SQL

Character and XML data (data type XML) directly to the IFS with SQL

Description

This tool includes:

Program TypeProgram/
Procedure Name
Description
Service Program SNDMSG contains the (RPG) prodecures for sending escape messages
SQLRPGLE Program WRT2IFS for writing any character data to the IFS
SQLRPGLE Program WRTXML2IFSfor writing XML data (Data type XML) to the IFS
SQL Stored Procedures WRT2IFSxxxxfor writing any character data into the IFS.
xxxx = File Operation
i.e. Create/CreateReplace/Append
SQL Stored Procedures WRTXML2IFSxxxxfor writing XML data (data type XML) into the IFS.
xxxx = File Operation
i.e. Create/CreateReplace/Append

Author

Birgitta Hauser is Software and Database Engineer, focusing on RPG, SQL and Web development on IBM i at Toolmaker Advanced Efficiency GmbH in Germany. She also works in consulting with regard to modernizaing legacy IBM i applications IBM i as well as in education as a trainer for RPG and SQL developers.

Since 2002 she has frequently spoken at the COMMON User Groups and other IBM i and Power Conferences in Germany, other European Countries, USA and Canada.

In addition, she is co-author of two IBM Redbooks and also the author of several articles and papers focusing on RPG and SQL for a German publisher, IBM DeveloperWorks and IT Jungle.

Prerequisites

Minimum IBM i Release: 7.2 TR7 (or 7.3 TR3)

Compile

SNDMSG Service Program

  • Create a module with the same name using the CRTRPGMOD CL command
    CRTRPGMOD MODULE(YOURSCHEMA/SNDMSG)       
                         SRCFILE(YOURSCHEMA/QSRVPGMSRC)  
                         SRCMBR(SNDMSG)      
          
  • Create the service Program with the CRTSRVPGM
    CRTSRVPGM SRVPGM(YOURSCHEMA/SNDMSG)                                                   
                         MODULE(SNDMSG)                                                            
                         SRCFILE(YOURSCHEMA/QSRVSRC)
                         SRCMBR(SNDMSG)
          
  • Delete the module after having successfully created the service program.

RPG Programs

If you create a binder directory with the name HSBNDDIR in your schema and add the SNDMSG service program to this binder directory, the RPG programs can be compiled with the the CRTSQLRPGI command.

CRTSQLRPGI OBJ(YOURSCHEMA/WRTUT82IFS)     
                SRCFILE(YOURSCHEMA/QRPGLESRC)    
                SRCMBR(WRTUT82IFS)             
                OBJTYPE(*PGM)               

SQL Stored Procedures and User Defined Functions

The SQL Scripts containing the source code for the stored procedures, can be run with the RUNSQLSTM command:

RUNSQLSTM SRCFILE(YOURSCHEMA/QSQLSRC)   
               SRCMBR(WRT2IFS)          
               COMMIT(*NONE)               
               NAMING(*SYS)                
               MARGINS(132)                
               DFTRDBCOL(YOURSCHEMA)

It is also possible to run the SQL scripts from the b>RUN SQL SCRIPTING facility in Client Access or (even better) ACS (Access Client Solution).

Attention:The database objects are not qualified in the SQL script,
so you need to add YOURSCHEMA to the script by yourself.

Program and Procedure Descriptions:

SNDMSG (RPG Service Program) – Sending Program Messages from within RPG

Function NameDescription
SndEscMsg Send Escape message from within an RPG internal or exported procedure.
SndEscMsgLinMainSend Escape message from within an RPG linear main procedure

Both procedures are needed for signaling errors in the WRT2IFS and WRTXML2IFS RPG Programs.

WRT2IFS (RPG Program) – Write Character Data to the IFS

Parameter:

Parameter NameData Type/LengthDescription
ParText VarChar(16000000)Text to be written into the IFS
ParIFSFile VarChar(1024) IFS File to be written, replaced or appended
ParOperationInteger 8=Create / 16=Replace / 32=Append

With the WRT2IFS program any character data can be written to the IFS into the passed IFS file. Depening on the file operation a new file will be created, or replaced or the data is added to an existing IFS file. The program WRT2IFS is registered as SQL Stored Procedure.

WRTXML2IFS (RPG Program) – Write XML Data to the IFS

Parameter:

Parameter NameData Type/LengthDescription
ParXML VarChar(16000000)Serialized XML data to be written into the IFS
ParIFSFile VarChar(1024) IFS File to be written, replaced or appended
ParOperation Integer 8=Create / 16=Replace / 32=Append

With the WRTXML2IFS program wellformed XML data can be written to the IFS. epening on the file operation a new file will be created, or replaced or the data is added to an existing IFS file. The program WRTXML2IFS is registered as SQL Stored Procedure.

WRT2IFSxxxxx (Stored Procedures) – Write Character Data into the IFS

WRT2IFS - Write Data to the IFS

Parameter:

Parameter NameData Type/LengthDescription
ParText CLOB(16 M) Text to be written into the IFS
ParIFSFile VarChar(1024) IFS File to be written, replaced or appended
ParOperation Integer 8=Create / 16=Replace / 32=Append

The WRT2IFS stored procedure is a wrapper around the WRT2IFS RPG Program. The WRT2IFSxxxxx stored procedures call the WRTIFS stored procedure with a specific File Operation.

Example:

Call wrt2IFS('This is a test for checking whether data is written to the IFS', 
             '/home/Hauser/Test20180224', 8);

WRT2IFS_CREATE – Write Character Data to the IFS – Create a New File

Parameter:

Parameter NameData Type/LengthDescription
ParText CLOB(16 M) Text to be written into the IFS
ParIFSFile VarChar(1024) IFS File to be written, replaced or appended

Calls the WRT2IFS Procedure, the File Operation is passed fix with 8. A new IFS file will be created. If the file already exists an error will be returned.

Example:

Call Wrt2IFS_Create(Cast('{"root": {"Name": "Hauser", 
                                    "FirstName": "Birgitta"}' as VarChar(256) CCSID 1208), 
                         '/home/Hauser/Tst20180224');

WRT2IFS_CREATEREPLACE – Write Character Data to the IFS – Replace an existing files

Parameter:

Parameter NameData Type/LengthDescription
ParText CLOB(16 M) Text to be written into the IFS
ParIFSFile VarChar(1024) IFS File to be written, replaced or appended

Calls the WRT2IFS Procedure, the File Operation is passed fix with 16. A new IFS file will be created. If the file already exists the existing one is replaced.

Example:

Call Wrt2IFS_CreateReplace(Cast('{"root": {"Name": "Hauser", 
                                           "FirstName": "Birgitta", 
                                           "City": "Kaufering"}' as VarChar(256) CCSID 1208), 
                           '/home/Hauser/Tst20180224');

WRT2IFS_APPEND – Write Character Data to the IFS – Append data to an existing one

Parameter:

Parameter NameData Type/LengthDescription
ParText CLOB(16 M) Text to be written into the IFS
ParIFSFile VarChar(1024) IFS File to be written, replaced or appended

Calls the WRT2IFS Procedure, the File Operation is passed fix with 32 A new IFS file will be created. If the file already exists the text is appended at the end.

Example:

Call Wrt2IFS_Append(Cast(', {"Street": "Dr.-Gerbl-Str.", 
                             "HausNr": 20}}' as VarChar(256) CCSID 1208), 
                    '/home/Hauser/Tst20180224');

WRTXML2IFSxxxx (Stored Procedures) – Write XML Data to the IFS

WRTXML2IFS

Parameter:

Parameter NameData Type/LengthDescription
ParText XML XML data to be written into the IFS
ParIFSFile VarChar(1024) IFS File to be written, replaced or appended
ParOperation Integer 8=Create / 16=Replace / 32=Append

The WRTXML2IFS stored procedure is a Wrapper around the WRTXML2IFS RPG Program. The WRTXML2IFSxxxxx stored procedure call the WRTXML2IFS stored procedure with a specific File Operation.

Example:

Call WrtXML2IFS(XMLElement(Name "root", XMLElement(Name "Name", 'Hauser')), 
                           '/home/Hauser/TstXML20180224.xml', 16);

WRTXML2IFS_CREATE – Write XML Data to the IFS – Create a New File

Parameter:

Parameter NameData Type/LengthDescription
ParText XML XML Data to be written into the IFS
ParIFSFile VarChar(1024) IFS File to be written, replaced or appended

Calls the WRTXML2IFS Procedure, the File Operation is passed fix with 8 A new IFS file will be created. If the file already exists an error will be returned

Example:

Call WrtXML2IFS_Create(XMLElement(Name "root", XMLElement(Name "Name", 'Hauser')), 
                      '/home/Hauser/TstXML20180224.xml');

WRT2IFS_CREATEREPLACE – Write XML Data to the IFS – Replace an existing file

Parameter:

Parameter NameData Type/LengthDescription
ParText XML XML Data to be written into the IFS
ParIFSFile VarChar(1024) IFS File to be written, replaced or appended

Calls the WRTXML2IFS Procedure, the File Operation is passed fix with 16 A new IFS file will be created. If the file already exists the existing one is replaced.

Example:

Call WrtXML2IFS_CreateReplace(XMLElement(Name "root", 
                                         XMLElement(Name "Name", 'Hauser'), 
                                         XMLElement(Name "FirstName", 'Birgitta')), 
                              '/home/Hauser/TstXML20180224.xml');```

WRT2IFS_APPEND – Write XML Data to the IFS – Append data to an existing file

Parameter:

Parameter NameData Type/LengthDescription
ParText XML XML Data to be written into the IFS
ParIFSFile VarChar(1024) IFS File to be written, replaced or appended

Calls the WRTXML2IFS Procedure, the File Operation is passed fix with 32 A new IFS file will be created. If the file already exists the text is appended at the end.

About

Generate XML and JSON data and write them directly to the IFS with SQL

License:MIT License


Languages

Language:SQLPL 100.0%