Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
SQL write IFS file
-- Simple way to write a stream file to the IFS, by using C runtime as inline code
-- This will produce stream files UTF-8 encoded
-- I doubt it is a good idea to build huge applications this way, however it
-- is a cool example how far you can go with SQL:
----------------------------------------------------------------------------------------------
call qcmdexc ('addlible qsysinc');
call qcmdexc ('crtsrcpf FILE(QTEMP/C) MBR(C)');
delete from qtemp.c;
insert into qtemp.c (srcdta) values
('{'),
('#include <sys/types.h>'),
('#include <sys/stat.h>'),
('#include <fcntl.h>'),
('int f,l,o;'),
('mode_t mode = S_IRUSR | S_IWUSR | S_IXUSR;'),
('o = O_WRONLY | O_CREAT | O_TRUNC | O_CCSID ;'),
('IFS_WRITE.NAME.DAT[IFS_WRITE.NAME.LEN] =0;'),
('f = open(IFS_WRITE.NAME.DAT, o, mode, 1208);'),
('l = write(f, IFS_WRITE.BUF.DAT,IFS_WRITE.BUF.LEN);'),
('close (f);'),
('}')
;
create or replace procedure qusrsys.ifs_write(name varchar(256), buf varchar(32700) ccsid 1208 )
external action
modifies sql data
set option output=*print, commit=*none, dbgview = *source
begin
if buf is not null then
include qtemp/c(c);
end if;
end;
-- Test it like this:
call qusrsys.ifs_write('/tmp/test.txt' , 'Hello');
-- And a binary version:
delete from qtemp.c;
insert into qtemp.c (srcdta) values
('{'),
('#include <sys/types.h>'),
('#include <sys/stat.h>'),
('#include <fcntl.h>'),
('int f,l,o;'),
('mode_t mode = S_IRUSR | S_IWUSR | S_IXUSR;'),
('o = O_WRONLY | O_CREAT | O_TRUNC | O_CCSID ;'),
('IFS_WRITE_BIN.NAME.DAT[IFS_WRITE_BIN.NAME.LEN] =0;'),
('f = open(IFS_WRITE_BIN.NAME.DAT, o, mode, 1208);'),
('l = write(f, IFS_WRITE_BIN.BUF.DAT,IFS_WRITE_BIN.BUF.LEN);'),
('close (f);'),
('}')
;
create or replace procedure qusrsys.ifs_write_bin(name varchar(256), buf varchar(32700))
external action
modifies sql data
set option output=*print, commit=*none, dbgview = *source
begin
if buf is not null then
include qtemp/c(c);
end if;
end;
-- Test it like this ex place the bom code:
call qusrsys.ifs_write_bin('/tmp/test.txt' , x'EFBBBF' );
@daframe

This comment has been minimized.

Copy link

@daframe daframe commented Dec 5, 2019

Hi Niels ...
Thank you for this tool.
Please, verify line #10.

('#includ<sys/stat.h>'),

A letter is missed.

('#include <sys/stat.h>'),
@NielsLiisberg

This comment has been minimized.

Copy link
Owner Author

@NielsLiisberg NielsLiisberg commented Dec 9, 2019

Thanx for the feedback - Yes and also QSYSINC need to be installed and added to library list. In early version this dependency was not required.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment