Created
January 7, 2025 14:59
-
-
Save NielsLiisberg/d180d490a1e778a4a9530749498325ae to your computer and use it in GitHub Desktop.
SQL IFS_WRITE_BLOB function
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
-- Write a BLOB to IFS function | |
-- 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 own tool library | |
-- Simply paste this gist into ACS SQL and step through the example. | |
-- | |
-- | |
-- It is a cool example how far you can go with SQL: Have fun - | |
-- (C) Niels Liisberg 2025 | |
-- | |
-- This gist is distributed on an "as is" basis, without warranties | |
-- or conditions of any kind, either express or implied. | |
---------------------------------------------------------------------------------------------- | |
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);') | |
,('MAIN.RC = rc;') | |
; | |
create or replace function qusrsys.ifs_write_blob | |
( | |
payload blob(2g), | |
tofile varchar(256) | |
) | |
returns int | |
external action | |
modifies sql data | |
set option output=*print, commit=*ur, dbgview = *list | |
main: begin | |
declare buf blob(2G); | |
declare name varchar(256) ; | |
declare rc int default 0; | |
-- "buf" and "name" are in the C-code as MAIN.BUF and MAIN.NAME | |
set buf = payload; | |
set name = tofile; | |
include qtemp/c(c); | |
return rc; | |
end; | |
-- Usecase: | |
-- returns 0 on ok | |
values qusrsys.ifs_write_blob ( | |
payload => SYSTOOLS.HTTPGETBLOB( url => 'http://google.com' , httpheader => null), | |
tofile => '/tmp/google.html' | |
); | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment