Subscribe for automatic updates: RSS icon RSS

Login icon Sign in for full access | Help icon Help
Advanced search

Pages: [1]
  Reply  |  Print  
Author Topic: TEXT data type best practices  (Read 8398 times)
Huy H.
Posts: 45


« on: October 20, 2020, 08:35:35 pm »

We've been recently starting to use TEXT data type in our code.  I'm curious about any potential problem or if there's any best practices to follow when using TEXT or BYTE. The cautious side of me say I should only use these data types when I need to (field is defined as TEXT/BYTE in the database).  But would it make sense to start using TEXT to read text files:

Code
  1. DEFINE text_content TEXT
  2. DEFINE json_obj JSONObject
  3.  
  4. LOCATE text_content IN MEMORY
  5. CALL text_content.readFile("my_config.json")
  6. LET json_obj = util.JSONObject.parse(text_content)
  7. FREE text_content

Should we worry about performance impact?  In our older code, we would use the base.Channel class to read the file line-by-line to a string before parsing it with the JSONObject class.
Sebastien F.
Four Js
Posts: 545


« Reply #1 on: October 21, 2020, 10:03:38 am »

Hello!

(you have an error in your code sample: DEFINE json_obj util.JSONObject)

The main purpose of TEXT/BYTE types is to store LOB (Larg OBjects) from the database, from TEXT/BYTE columns in case of Informix, and CLOB/BLOB columns for other DB engines.

As you certainly know, TEXT is for simple text data, and BYTE is for media (images, sound, proprietary doc/text formats).

Obviously, don't replace all your STRING/CHAR/VARCHAR variables by TEXT!

But yes, you can use TEXT to load text files as shown in your sample code, it's not bad practice.
Just make sure that the charset of the text file and the application correspond:
There is no automatic conversion done (that would require some meta-data info to know what is the charset of the file).

Pay attention to TEXT/BYTE variable specifics:
These are references to LOB structures.
For ex when you assign to another variable you only copy the internal handle, not the data!

You must also properly LOCATE these variable before usage.
Use FREE to release allocated resources (take care it will delete the underlying file!)
LOCATE tx IN FILE uses a temp file that is automatically deleted when program ends.

If you just want to read a file without touching it, you can directly locate the TEXT variable to that file:

    LOCATE text_content IN FILE "my_config.json"
    LET json_obj = util.JSONObject.parse(text_content)
    DISPLAY json_obj.toString()
    --FREE text_content        -- FREE would delete the file!!!!

If not yet done, read carefully the documentation.

https://4js.com/online_documentation/fjs-fgl-manual-html/#fgl-topics/c_fgl_datatypes_TEXT.html
https://4js.com/online_documentation/fjs-fgl-manual-html/#fgl-topics/c_fgl_datatypes_BYTE.html
https://4js.com/online_documentation/fjs-fgl-manual-html/#fgl-topics/c_fgl_variables_LOCATE.html
https://4js.com/online_documentation/fjs-fgl-manual-html/#fgl-topics/c_fgl_variables_FREE.html

Seb
Pages: [1]
  Reply  |  Print  
 
Jump to:  

Powered by SMF 1.1.21 | SMF © 2015, Simple Machines