Skip to content

Instantly share code, notes, and snippets.

@NielsLiisberg
Last active June 5, 2020 14:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save NielsLiisberg/c7d76c9e696161582c0e9a4acf8bd57a to your computer and use it in GitHub Desktop.
Save NielsLiisberg/c7d76c9e696161582c0e9a4acf8bd57a to your computer and use it in GitHub Desktop.
SQL Write a BLOB to IFS
-- Write a BLOB to IFS
-- Note1: This also works for save files
-- Note2: This also compiles on vanilla systems without QSYSINCL installed
-- Note3: I am using library QUSRSYS. I suggest you put it into your tool library
-- 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: Have fun :)
-- (C) Niels Liisberg 2020
----------------------------------------------------------------------------------------------
call qcmdexc ('crtsrcpf FILE(QTEMP/C) MBR(C)');
delete from qtemp.c;
insert into qtemp.c (srcdta) values
-- Prototype warnings are chekked and OK
-- ('#include <sys/types.h>')
-- ('#include <sys/stat.h>')
-- ('#include <fcntl.h>')
('#include <sqludf.h>')
,('int outFile;')
,('long pos = 1;')
,('long remains;')
,('long retlen;')
,('char buf [32760];')
,('long chunk = sizeof(buf); ')
,('int mode = 448; // mode_t mode = S_IRUSR | S_IWUSR | S_IXUSR;')
,('int opt = 106 ; // int opt = O_WRONLY | O_CREAT | O_TRUNC | O_CCSID ;')
,('int rc = sqludf_length(&MAIN.BUF, &remains);')
,('MAIN.NAME.DAT[MAIN.NAME.LEN] =0;')
,('outFile = open(MAIN.NAME.DAT, opt, mode, 1252);')
,('while (rc == 0 && remains > 0) {')
,(' rc = sqludf_substr(&MAIN.BUF, pos, chunk , buf , &retlen),')
,(' write (outFile , buf, retlen);')
,(' pos += chunk;')
,(' remains -= chunk;')
,('} ')
,('close(outFile);')
;
create or replace procedure qusrsys.ifs_write_blob
(
payload blob(2g),
tofile varchar(256)
)
external action
modifies sql data
set option output=*print, commit=*chg, dbgview = *list
main: begin
declare buf blob(2G);
declare name varchar(256) ;
-- "buf" and "name" are in the C-code as MAIN.BUF and MAIN.NAME
set buf = payload;
set name = tofile;
include qtemp/c(c);
end;
-- Usecases:
-- Simple get a stream file and put it on the IFS
call qusrsys.ifs_write_blob (
payload => SYSTOOLS.HTTPGETBLOB( url => 'http://webfiles.system-method.com/packages/Icebox2pkg/ip2.tar' , httpheader => null),
tofile => '/tmp/ip2.tar'
);
-- Savefile example
cl: crtsavf qgpl/icebox2;
call qusrsys.ifs_write_blob (
payload => SYSTOOLS.HTTPGETBLOB( url => 'http://webfiles.system-method.com/packages/Icebox2pkg/icebox2.savf' , httpheader => null),
tofile => '/qsys.lib/qgpl.lib/icebox2.file'
);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment