Language basics / Data types |
The TEXT data type stores large text data.
TEXT
A BYTE or TEXT variable is a handle for a large object (LOB), that is stored in a file or in memory. Such data type is a complex type that cannot be used like INTEGER or CHAR basic types: It is designed to handle a large amount of data and has different semantics as simple types. The main difference with simple data types, is the fact that you must specify the storage with the LOCATE instruction, before using BYTE and TEXT variables.
The maximum size of data that can be handled by BYTE and TEXT variable is theoretically 2^31 bytes (~2.14 Gigabytes), but the practical limit depends from the disk or memory resources available to the process.
DEFINE t TEXT LET t = "aaaa" -- invalid, t is not located LOCATE t IN MEMORY LET t = "aaaa" -- valid, now t is located in memory
DEFINE t1, t2 TEXT ... CREATE TABLE mytable ( id INT, data TEXT ) ... LOCATE t1 IN MEMORY CALL t1.readFile("lob.4gl") INSERT INTO mytable VALUES ( 1, t1 ) LOCATE t2 IN FILE SELECT data INTO t2 FROM mytable WHERE id=1 ...
DEFINE t TEXT LOCATE t IN MEMORY CALL t.readFile("orig.txt") CALL t.writeFile("copy.txt")
DEFINE b BYTE LOCATE b IN FILE "picture.png" INITIALIZE b TO NULL -- The file "picture.png" is now empty.
DEFINE b BYTE LOCATE b IN FILE CALL b.readFile("picture.png") -- ok FREE b CALL b.readFile("picture.png") -- Invalid, b is not located. LOCATE b IN MEMORY CALL b.readFile("picture.png") -- ok
DEFINE b1, b2 BYTE -- Could be TEXT: same behavior LOCATE b1 IN FILE "mydata" -- reference file directly LOCATE b2 IN MEMORY -- use memory instead of file CALL b2.readFile("mydata") -- read file content into memory # FREE b2 -- this should be done to free memory before LET LET b2 = b1 -- Now b2 points directly to the file (like b1) INITIALIZE b1 TO NULL -- truncates reference file DISPLAY IIF( b2 IS NULL, "b2 is null", "b2 is not null") -- Displays "b2 is null"
-- WARNING: THIS IS AN INVALID CODE EXAMPLE DEFINE img, tmp BYTE LOCATE img IN MEMORY CALL img.readFile("picture1.png") LOCATE tmp IN MEMORY LET tmp = img -- Expecting to save the current data, but now -- both variables reference the same data... CALL img.readFile("picture2.png") LET img = tmp -- Does not restore the old value: Same data.
If you need to clone a large object, use the writeFile() / readFile() methods.
It is possible to assign TEXT variables to/from VARCHAR, CHAR and STRING variables.