File

File is used to declare the tables that can be used in a script.

Syntax

   Local File FILEDEC_LIST
   File FILEDEC_LIST

Examples

   # Declaration of two tables: SALESORDER with its default abbreviation, and CUSTOMER with [CST] as abbreviation.
   Local File SALESORDER, CUSTOMER [CST]

   # Use of 2 account tables from 2 accounting folder ACCOUNTING1 and ACCOUNTING2. 
   # my_server.my_domain is the network name of the server where ACCOUNTING1 folder is installed,
   # the second folder is installed on the current application server.
    Local File "[email protected]" [ACC1], "ACCOUNTING2.ACCCOUNT" [ACC2]

   # Declaration of one table twice with different abbreviations.
   # This can be useful to perform joins within the same table
    Local File ACCOUNT [GACC1], ACCOUNT [GACC2]

   # Declaration of 5 already opened tables.
    Local File [A1],[A2],[A3],[A4],[A5]

   # ITEMS table with a filer and an order by clause
    Local File ITEMS [ITM] Where [ITM]ITMKEY >= "fzzz" Order By Key A = [ITM]ITMCATEG Desc

   # Let's compute the global size of files belonging to the "sage" group present in a directory on UNIX
    Local Integer TOTAL_SIZE
    Local File (D,L,P,G,T) From System "ls -l" As [SYS] Where [SYS]G = "sage"
    For [SYS] : [L]TOTAL_SIZE += val([F:SYS]T) : Next

   # Let's create a table in memory and use it
   Local Char MY_KEY(20)(1..200)
   Local Integer MY_VALUE(1..200)
   Local Date MY_DATE(1..200)
   Local Integer NB_REC
   Gosub FILL_TABLE : # Fills MY_KEY, MY_VALUE, MY_DATE arrays and returns NB_REC as the number of records
   Local File (Char THEKEY(20), Integer AMOUNT, Date POSTING_DATE)
&  From Variable MY_KEY(1..NB_REC), MY_VALUE(1..NB_REC), MY_DATE(1..NB_REC)
&  As [MEM] Order By POSTING_DATE
   For [MEM]
     # For every loop, we have [MEM]THEKEY, [MEM]AMOUNT, [MEM]POSTING_DATE available (found in MY_* arrays)
     # This type of table is usable only in read mode
   Next

Description and comments

File is used to declare the tables used in a routine. A table can be:

The result of the execution of this instruction is the following:
* It creates variable classes [F] and [G] and updates the variables [S]fileabre and [S]filename.
* It updates the list of tables opened. The table that is first in the command becomes the default table. This can be changed by the Default File instruction.
* The maximum number of tables that can be opened simultaneously is limited by the system variable [S]adxmto. The value of this variable is normally defaulted by the supervisor (assigned in APL.ini configuration file). A common value for adxmto is 200.

A File command closes all the tables opened by an earlier File (or Trbegin) instruction. The use of this syntax is therefore deprecated in the application code. You must use Local File to open the tables in a routine to avoid conflicts on the calling context.

The Local File instruction is used to open files locally for a routine or subprogram and temporarily. This declaration does not close files previously opened by a File or Local File command, but adds to the list of open tables. The first table opened this way becomes the new default table. After the Close Local File command (or the end of the subprogram), the list of tables is as it was before the instruction, as are the current records.

A locality level is attached to each Local file. Therefore, using Local File and the same abbreviation, you can reopen a file already opened elsewhere, but this cannot be done in the same level of the nesting call.

Comments

You can open a file with Local File within a transaction, but files opened in write mode by Trbegin will no longer be accessible – even if they are not physically closed before the end of the transaction.

In version 6, opening a table is costly if the table has not been opened, or if a Close instruction closed the table previously. This is the reason that in version 6 programming style, tests are performed by using clalev to open a table only if it is not previously opened. The main issue with this programming technique is that it creates conflicts (for example, filters can be inherited from a previous declaration). Therefore, a new instruction called LogicClose file has been implemented in version 7. It performs as a Close instruction, but does not release all the resources and is therefore cost effective when the table is reopened. As these instructions are managed by the supervisor layers, the best practice for version 7 is to declare systematically with the Local File all the tables used in a routine called by Call, Func, or fmet instructions.

Temporary files created for a File command (sort file or System file) are stored in the '/tmp' folder, unless the environment variable TMPDIR contains the name of another folder. Their names are randomly generated. They are only accessible from the current procedure and are physically deleted at the end of their use or at the end of the procedure.

A Close Local File or LogicClose Local File can be done within a transaction, but it will be taken into account only at the end of the transaction (instructions Commit or Rollback).

The nbrecord function is available to perform a count(*) on a table. It assumes that the table has been previously declared by File, and uses the abbreviation of the table as an argument. This count does not consider the filter that can be added by any Where clause. The instruction rowcount performs the count on the filtered lines.

A table can be opened several times with different locality levels, using a new [Where](../4gl/where.md) clause each time. It is not added to the previous ones, but it is attached to a locality level and thus deleted at the end of the subprogram. For example, consider the following script:
  Local File CUSTOMER [CUST] Where COUNTRY="USA"
  Call MY_ROUTINE
  Read [CUST]CODE First : # This will return the first customer having COUNTRY equal to the USA
  Call MY_OTHER_ROUTINE
  ...
End

Subprog MY_ROUTINE
  Read [CUST]CODE First : # Table has not been declared, previous condition applies: the first US customer code is read
  Local File CUSTOMER Where COUNTRY<>"USA" : # The opposite condition
  Read [CUST]CODE First : # Previous condition applies, now the first non US customer code is read
  Filter [CUST] Where CITY<>"PARIS"
  Read [CUST]CODE First : # Filter added to previous condition: first non US customer with a city not equal to PARIS is read
End

Subprog MY_OTHER_ROUTINE
  Local File [CUST] Where COUNTRY<>"USA" : # The abbreviation refers to the previous file instruction, the condition is added
  Read [CUST]CODE First : # No record will be returned, because the condition is COUNTRY="USA" and CONTRY<>"USA"
End

Associated errors

CodeDescription
7Abbreviation not found.
10The expression that contains the table name does not return a character string.
20Table not found.
27Table access error.
28Table opened twice.
29Too many tables opened.

See also

Trbegin, Where, Order By, adxmto, clalev, Default, Close, Local, LogicClose, nbrecord, Filter, For, Link, filename, fileabre.