Updated on 2024-10-14 GMT+08:00

DBE_FILE

Interface Description

Table 1 lists all interfaces supported by the DBE_FILE package.

Table 1 DBE_FILE

Interface

Description

DBE_FILE.OPEN

Opens a file based on the specified directory and file name.

DBE_FILE.IS_CLOSE

Checks whether a file handle is opened.

DBE_FILE.READ_LINE

Reads a line of data from an open file handle based on the specified length.

DBE_FILE.WRITE

Writes the data specified in the buffer to a file.

DBE_FILE.NEW_LINE

Writes one or more line terminators to an open file.

DBE_FILE.WRITE_LINE

Writes a string from the buffer to an open file.

DBE_FILE.FORMAT_WRITE

This is a formatted PUT stored procedure similar to printf().

DBE_FILE.GET_RAW

Reads binary data from an open file handle.

DBE_FILE.PUT_RAW

Writes the input binary data to the file.

DBE_FILE.FLUSH

Writes data from a file handle to a physical file.

DBE_FILE.CLOSE

Closes an open file handle.

DBE_FILE.CLOSE_ALL

Closes all file handles opened in a session.

DBE_FILE.REMOVE

Deletes a disk file. To perform this operation, you must have required permissions.

DBE_FILE.RENAME

Renames files on the disk, similar to mv in Unix.

DBE_FILE.COPY

Copies data in a continuous area to a new file. If start_line and end_line are omitted, the entire file is copied.

DBE_FILE.GET_ATTR

Reads and returns the attributes of a disk file.

DBE_FILE.SEEK

Adjusts the position of a file pointer forward or backward based on the specified number of bytes.

DBE_FILE.GET_POS

Specifies the offset of a returned file, in bytes.

  • DBE_FILE.OPEN

    This function opens a file. You can specify the maximum number of characters in each line. A maximum of 50 files can be opened at a time. This function returns a handle of the INTEGER type.

    The prototype of the DBE_FILE.OPEN function is as follows:

    1
    2
    3
    4
    5
    6
    DBE_FILE.OPEN (
    dir             IN    VARCHAR2,
    file_name       IN   VARCHAR2,
    open_mode       IN    VARCHAR2,
    max_line_size   IN    INTEGER DEFAULT 1024)
    RETURN INTEGER;
    
    Table 2 DBE_FILE.OPEN interface parameters

    Parameter

    Description

    dir

    Directory of a file. It is a string, indicating an object name.

    NOTE:
    • File directories need to be added to the system catalog PG_DIRECTORY. If the input path does not match the path in PG_DIRECTORY, an error indicating that the path does not exist will be reported. Functions that involve location as parameters also comply with this rule.
    • When the GUC parameter safe_data_path is enabled, you can only use the advanced package to read and write files in the file path specified by safe_data_path.

    file_name

    File name with an extension (file type), excluding the path name. A path contained in a file name is ignored in the OPEN function. In Unix, the file name cannot end with the combination of a slash and a dot (/.).

    open_mode

    File opening mode, including r (read), w (write), and a (append).

    NOTE:

    For the write operation, the system checks the file type. If the ELF file is written, an error is reported and the system exits.

    max_line_size

    Maximum number of characters in each line, including newline characters. The minimum value is 1 and the maximum is 32767. If this parameter is not specified, the default value 1024 is used.

  • DBE_FILE.IS_CLOSE

    This function detects a file handle to check whether the file is opened. A Boolean value is returned. If an invalid file handle is detected, the INVALID_FILEHANDLE exception is thrown.

    The prototype of the DBE_FILE.IS_CLOSE function is as follows:

    1
    2
    3
    DBE_FILE.IS_CLOSE (
    file      IN     INTEGER)
    RETURN BOOLEAN;
    
    Table 3 DBE_FILE.IS_CLOSE interface parameters

    Parameter

    Description

    file IN INTEGER

    File handle to be detected

  • DBE_FILE.READ_LINE

    This stored procedure reads text from an open file handle and stores the result in the buffer. The procedure reads data until a line end (excluding line terminator), file end, or the value specified by the len parameter. The length of the read data cannot exceed the value of the max_line_size parameter in the OPEN function.

    The prototype of the DBE_FILE.READ_LINE function is as follows:

    1
    2
    3
    4
    DBE_FILE.READ_LINE (
    file      IN   INTEGER,
    buffer    OUT  VARCHAR2,
    len       IN   INTEGER DEFAULT NULL)
    
    Table 4 DBE_FILE.READ_LINE interface parameters

    Parameter

    Description

    file

    File handle opened by calling the OPEN function. The file must be opened in read mode. Otherwise, the INVALID_OPERATION exception is thrown.

    buffer

    Buffer used to receive data

    len

    Number of bytes read from a file. The default value is NULL. If the default value NULL is used, max_linesize is used to specify the line size.

  • DBE_FILE.WRITE

    This stored procedure writes data in the buffer to a file. The file must be opened in write mode. Line terminators are not written.

    The prototype of the DBE_FILE.WRITE function is as follows:

    1
    2
    3
    DBE_FILE.WRITE (
    file     IN     INTEGER,
    buffer   IN     TEXT);
    
    Table 5 DBE_FILE.WRITE interface parameters

    Parameter

    Description

    file

    This stored procedure writes data in the buffer to a file. The file must be opened in write mode. Line terminators are not written.

    buffer

    Text data to be written to a file. The maximum buffer size is 32767 bytes. If no value is specified in the open state, the default value is 1024 bytes. Before the writing is performed, the buffer occupied by WRITE operations cannot exceed 32767 bytes.

    NOTE:

    For the write operation, the system checks the file type. If the ELF file is written, an error is reported and the system exits.

  • DBE_FILE.NEW_LINE

    This stored procedure writes one or more line terminators to an open file. The procedure is split from the WRITE function because line terminators are related to platforms.

    The prototype of the DBE_FILE.NEW_LINE function is as follows:

    1
    2
    3
    DBE_FILE.NEW_LINE (
    file         IN     INTEGER,
    line_nums    IN     INTEGER := 1);
    
    Table 6 DBE_FILE.NEW_LINE interface parameters

    Parameter

    Description

    file

    Opened file handle

    line_nums

    Number of terminators written to a file

  • DBE_FILE.WRITE_LINE

    This stored procedure writes strings in the buffer to an open file. The file must be opened in write mode.

    The prototype of the DBE_FILE.WRITE_LINE function is as follows:

    1
    2
    3
    4
    DBE_FILE.WRITE_LINE(
    file       IN     INTEGER,
    buffer     IN     TEXT,
    flush      IN     BOOLEAN DEFAULT FALSE);
    
    Table 7 DBE_FILE.WRITE_LINE interface parameters

    Parameter

    Description

    file

    Opened file handle

    buffer

    Text data to be written to a file. The maximum buffer size is 32767 bytes. If no value is specified in the open state, the default value is 1024 bytes. Before the writing is performed, the buffer occupied by PUT operations cannot exceed 32767 bytes.

    NOTE:

    For the write operation, the system checks the file type. If the ELF file is written, an error is reported and the system exits.

    flush

    Whether to flush data to the disk after the writing

  • DBE_FILE.FORMAT_WRITE

    This is a formatted PUT stored procedure similar to printf().

    The prototype of the DBE_FILE.FORMAT_WRITE function is as follows:

    1
    2
    3
    4
    5
    6
    DBE_FILE.FORMAT_WRITE (
    file   IN INTEGER,
    format IN VARCHAR2,
    arg1  IN VARCHAR2 DEFAULT NULL,
    . . .
    arg6 IN VARCHAR2 DEFAULT NULL]);
    
    Table 8 DBE_FILE.FORMAT_WRITE interface parameters

    Parameter

    Description

    file

    Opened file handle

    format

    A string to be formatted, containing the text and format characters \n and %s

    [arg1. . .arg6]

    Six optional parameters. The parameters and the positions of characters to be formatted are in one-to-one correspondence. If the parameter corresponding to a character to be formatted is not provided, an empty string is used to replace %s.

  • DBE_FILE.GET_RAW

    This function reads binary data from the opened file descriptor and returns the data using r.

    The prototype of the DBE_FILE.GET_RAW function is as follows:

    1
    2
    3
    4
    DBE_FILE.GET_RAW (
    file    IN   INTEGER,
    r       OUT  RAW,
    length  IN   INTEGER DEFAULT NULL);
    
    Table 9 DBE_FILE.GET_RAW interface parameters

    Parameter

    Description

    file

    Opened file handle

    r

    Output binary data

    length

    Length of the file to be read. The default value is NULL. All data in the file is read. The maximum length is 1 GB.

  • DBE_FILE.PUT_RAW

    This function writes binary data to a file.

    The prototype of the DBE_FILE.PUT_RAW function is as follows:

    1
    2
    3
    4
    DBE_FILE.PUT_RAW (
    file    IN  INTEGER,
    r       IN  RAW,
    flush   IN  BOOLEAN DEFAULT FALSE);
    
    Table 10 DBE_FILE.PUT_RAW interface parameters

    Parameter

    Description

    file

    Opened file handle

    r

    Output binary data

    NOTE:

    For the write operation, the system checks the file type. If the ELF file is written, an error is reported and the system exits.

    flush

    Specifies whether to flush data to a file. The default value is false.

  • DBE_FILE.FLUSH

    Data in a file handle must be written into a physical file. Data in the buffer must have a line terminator. Refresh is important if a file must be read when it is opened. For example, debugging information can be refreshed to a file so that it can be read immediately.

    The prototype of the DBE_FILE.FLUSH function is as follows:

    1
    2
    DBE_FILE.FLUSH (
    file     IN     INTEGER);
    
    Table 11 DBE_FILE.FLUSH interface parameters

    Parameter

    Description

    file

    Opened file handle

  • DBE_FILE.CLOSE

    This stored procedure closes an open file handle. When the stored procedure is called, exception is thrown if there is data to be written into the buffer.

    The prototype of the DBE_FILE.CLOSE function is as follows:

    1
    2
    3
    DBE_FILE.CLOSE (
    file IN INTEGER
    )RETURN INTEGER;
    
    Table 12 DBE_FILE.CLOSE interface parameters

    Parameter

    Description

    file

    Opened file handle

  • DBE_FILE.CLOSE_ALL

    This stored procedure closes all file handles opened in a session and can be used for emergency cleanup.

    The prototype of the DBE_FILE.CLOSE_ALL function is as follows:

    1
    DBE_FILE.CLOSE_ALL;
    
    Table 13 DBE_FILE.CLOSE_ALL interface parameters

    Parameter

    Description

    None

    None

  • DBE_FILE.REMOVE

    This stored procedure deletes a disk file. To perform this operation, you must have required permissions for the directories and files.

    The prototype of the DBE_FILE.REMOVE function is as follows:

    1
    2
    3
    DBE_FILE.REMOVE (
    dir           IN     VARCHAR2,
    file_name     IN     VARCHAR2);
    
    Table 14 DBE_FILE.REMOVE interface parameters

    Parameter

    Description

    dir

    File directory

    NOTE:
    • File directories need to be added to the system catalog PG_DIRECTORY. If the input path does not match the path in PG_DIRECTORY, an error indicating that the path does not exist will be reported. Functions that involve location as parameters also comply with this rule.
    • When the GUC parameter safe_data_path is enabled, you can only use the advanced package to read and write files in the file path specified by safe_data_path.

    file_name

    File to be deleted

  • DBE_FILE.RENAME

    This function renames files on the disk, similar to mv in Unix.

    The prototype of the DBE_FILE.RENAME function is as follows:

    1
    2
    3
    4
    5
    6
    DBE_FILE.RENAME (
    src_dir        IN   VARCHAR2,
    src_file_name  IN   VARCHAR2,
    dest_dir       IN   VARCHAR2,
    dest_file_name IN   VARCHAR2,
    overwrite      IN   BOOLEAN DEFAULT FALSE);
    
    Table 15 DBE_FILE.RENAME interface parameters

    Parameter

    Description

    src_dir

    Directory of the original file (case-sensitive)

    NOTE:
    • File directories need to be added to the system catalog PG_DIRECTORY. If the input path does not match the path in PG_DIRECTORY, an error indicating that the path does not exist will be reported. Functions that involve location as parameters also comply with this rule.
    • When the GUC parameter safe_data_path is enabled, you can only use the advanced package to read and write files in the file path specified by safe_data_path.

    src_file_name

    Original file to be renamed

    dest_dir

    Destination directory (case-sensitive)

    NOTE:
    • File directories need to be added to the system catalog PG_DIRECTORY. If the input path does not match the path in PG_DIRECTORY, an error indicating that the path does not exist will be reported. Functions that involve location as parameters also comply with this rule.
    • When the GUC parameter safe_data_path is enabled, you can only use the advanced package to read and write files in the file path specified by safe_data_path.

    dest_file_name

    New file name

    overwrite

    The default value is false. If a file with the same name exists in the destination directory, the file will not be rewritten.

  • DBE_FILE.COPY

    This stored procedure copies data in a continuous area to a new file. If start_line and end_line are omitted, the entire file is copied.

    The prototype of the DBE_FILE.COPY function is as follows:

    1
    2
    3
    4
    5
    6
    7
    DBE_FILE.COPY (
    src_dir           IN     VARCHAR2,
    src_file_name     IN     VARCHAR2,
    dest_dir          IN     VARCHAR2,
    dest_file_name    IN     VARCHAR2,
    start_line        IN     INTEGER DEFAULT 1,
    end_line          IN     INTEGER DEFAULT NULL);
    
    Table 16 DBE_FILE.COPY interface parameters

    Parameter

    Description

    src_dir

    Directory of the original file

    NOTE:
    • File directories need to be added to the system catalog PG_DIRECTORY. If the input path does not match the path in PG_DIRECTORY, an error indicating that the path does not exist will be reported. Functions that involve location as parameters also comply with this rule.
    • When the GUC parameter safe_data_path is enabled, you can only use the advanced package to read and write files in the file path specified by safe_data_path.

    src_file_name

    File to be copied

    dest_dir

    Directory of the destination file

    NOTE:
    • File directories need to be added to the system catalog PG_DIRECTORY. If the input path does not match the path in PG_DIRECTORY, an error indicating that the path does not exist will be reported. Functions that involve location as parameters also comply with this rule.
    • When the GUC parameter safe_data_path is enabled, you can only use the advanced package to read and write files in the file path specified by safe_data_path.

    dest_file_name

    Destination file to which data is to be written

    NOTE:

    For the write operation, the system checks the file type. If the ELF file is written, an error is reported and the system exits.

    start_line

    Number of the line where the copy starts. The default value is 1.

    end_line

    Number of the line where the copy ends. The default value is NULL, indicating the end of the file.

  • DBE_FILE.GET_ATTR

    This stored procedure reads and returns the attributes of a disk file.

    The prototype of the DBE_FILE.GET_ATTR function is as follows:

    1
    2
    3
    4
    5
    6
    DBE_FILE.GET_ATTR(
    location IN text,
    filename IN text,
    OUT fexists boolean,
    OUT file_length bigint,
    OUT block_size integer);
    
    Table 17 DBE_FILE.GET_ATTR interface parameters

    Parameter

    Description

    location

    File directory

    NOTE:
    • File directories need to be added to the system catalog PG_DIRECTORY. If the input path does not match the path in PG_DIRECTORY, an error indicating that the path does not exist will be reported. Functions that involve location as parameters also comply with this rule.
    • When the GUC parameter safe_data_path is enabled, you can only use the advanced package to read and write files in the file path specified by safe_data_path.

    filename

    Name of the file to be checked

    fexists

    Whether the file exists

    file_length

    File length (unit: bytes). If the file does not exist, NULL is returned.

    block_size

    Block size of the file system (unit: byte). If the file does not exist, NULL is returned.

  • DBE_FILE.SEEK

    This stored procedure adjusts the position of a file pointer forward or backward based on the specified number of bytes.

    The prototype of the DBE_FILE.SEEK function is as follows:

    1
    2
    3
    4
    DBE_FILE.SEEK (
    file            IN INTEGER,
    absolute_start  IN     BIGINT DEFAULT NULL,
    relative_start  IN     BIGINT DEFAULT NULL);
    
    Table 18 DBE_FILE.SEEK interface parameters

    Parameter

    Description

    file

    Opened file handle

    absolute_start

    Absolute offset of a file. The default value is NULL.

    relative_start

    Relative offset of a file. A positive number indicates forward offset and a negative number indicates backward offset. The default value is NULL. If both absolute_start and this parameter are specified, the absolute_start parameter is used.

  • DBE_FILE.GET_POS

    This function returns file offset in bytes.

    The prototype of the DBE_FILE.FGETPOS function is as follows:

    1
    2
    3
    DBE_FILE.GET_POS (
    file    IN     INTEGER)
    RETURN BIGINT;
    
    Table 19 DBE_FILE.GET_POS interface parameters

    Parameter

    Description

    file

    Opened file handle

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
-- Add the /temp/ directory to the PG_DIRECTORY system catalog as a system administrator.
CREATE OR REPLACE DIRECTORY dir AS '/tmp/';
-- Open a file and write data into the file.
DECLARE
  f integer;
  dir text := 'dir';
BEGIN
  f := dbe_file.open(dir, 'sample.txt', 'w');
  PERFORM dbe_file.write_line(f, 'ABC');
  PERFORM dbe_file.write_line(f, '123'::numeric);
  PERFORM dbe_file.write_line(f, '-----');
  PERFORM dbe_file.new_line(f);
  PERFORM dbe_file.write_line(f, '*******');
  PERFORM dbe_file.new_line(f, 0);
  PERFORM dbe_file.write_line(f, '++++++++');
  PERFORM dbe_file.new_line(f, 2);
  PERFORM dbe_file.write_line(f, '#######');
  PERFORM dbe_file.write(f, 'A');
  PERFORM dbe_file.write(f, 'B');
  PERFORM dbe_file.new_line(f);
  PERFORM dbe_file.format_write(f, '[1 -> %s, 2 -> %s, 3 -> %s, 4 -> %s, 5 -> %s]', 'gaussdb', 'dbe', 'file', 'get', 'line');
  PERFORM dbe_file.new_line(f);
  PERFORM dbe_file.write_line(f, '1234567890');
  f := dbe_file.close(f);
END;
/
-- Read data from the file mentioned above.
DECLARE
  f integer;
  dir text := 'dir';
BEGIN
  f := dbe_file.open(dir, 'sample.txt', 'r');
  FOR i IN 1..11 LOOP
       RAISE INFO '[%] : %', i, dbe_file.read_line(f);
  END LOOP;
END;
/
-- Offset the file handle and obtain the current file location.
DECLARE
      l_file   integer;
      l_buffer VARCHAR2(32767);
      dir text := 'dir';
      abs_offset number := 100;
      rel_offset number := NULL;
BEGIN
      l_file := dbe_file.open(dir  => dir,  file_name  => 'sample.txt',open_mode => 'R');
      dbe_output.print_line('before seek: current position is ' || dbe_file.get_pos(file => l_file)); -- before seek: current position is 0
      dbe_file.seek(file => l_file, absolute_start=>abs_offset, relative_start=>rel_offset);
      dbe_output.print_line('fseek: current position is ' || dbe_file.get_pos(file => l_file)); -- seek: current position is 100
      l_file := dbe_file.close(file => l_file);
END;
/