Updated on 2024-05-07 GMT+08:00

DBE_FILE

The DBE_FILE package provides the capabilities of reading and writing operating system text files for stored procedures.

Precautions

  • DBE_FILE requires that files opened using DBE_FILE.FOPEN be encoded using the database character set. If the opened files are not encoded using the expected character set, an encoding verification error occurs when DBE_FILE.READ_LINE is used to read files. DBE_FILE requires that files opened using DBE_FILE.FOPEN_NCHAR be encoded using the UTF-8 character set. If the opened files are not encoded using the expected character set, an encoding verification error occurs when DBE_FILE.READ_LINE_NCHAR is used to read files.
  • When DBE_OUTPUT.PUT_LINE is used to print the result obtained by the DBE_FILE.READ_LINE_NCHAR API, ensure that the UTF-8 character set encoding can be converted to the current database character set encoding. If the preceding conditions are met, the result can be properly output. DBE_OUTPUT.PRINT_LINE does not support this function.
  • DBE_FILE requires that the character set encoding of the client be the same as that of the database.
  • If the database character set encoding format is ASCII and the client character set encoding format is Chinese, when the client calls DBE_FILE.WRITE_NCHAR or DBE_FILE.WRITE_LINE_NCHAR to write Chinese content, the written content may be in UTF-8 encoding format. An error may be reported when the DBE_FILE.READ_LINE_NCHAR is used.

Data Types

  • DBE_FILE.FILE_TYPE

    Defines the representation of files in the DBE_FILE package. The fields in DBE_FILE.FILE_TYPE are private fields of the DBE_FILE package. Do not change the field value of the type defined in DBE_FILE.FILE_TYPE.

    1
    2
    3
    4
    5
    CREATE TYPE DBE_FILE.FILE_TYPE AS(
        id INTEGER,
        datatype INTEGER,
        byte_mode BOOLEAN
    );
    
    Table 1 DBE_FILE.FILE_TYPE columns

    Parameter

    Description

    id

    File handle

    datatype

    File data type (CHAR, NCHAR, or binary). Currently, only CHAR and NCHAR files are supported. For a CHAR file, 1 is returned. For an NCHAR file, 2 is returned.

    byte_mode

    Indicates that the file is opened in binary mode (TRUE) or text mode (FALSE).

API Description

Table 2 lists all APIs supported by the DBE_FILE package.

Table 2 DBE_FILE

API

Description

DBE_FILE.OPEN/DBE_FILE.F...

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

DBE_FILE.IS_CLOSE

Checks whether a file is closed.

DBE_FILE.IS_OPEN

Checks whether a file is opened.

DBE_FILE.READ_LINE

Reads a line of a specified length from an open file.

DBE_FILE.WRITE

Writes data to an open file.

DBE_FILE.NEW_LINE

Writes one or more line terminators to an open file.

DBE_FILE.WRITE_LINE

Writes data to an open file and automatically appends a line terminator.

DBE_FILE.FORMAT_WRITE

Writes formatted data to an open file. It is a DBE_FILE.WRITE API that allows formatting.

DBE_FILE.GET_RAW

Reads RAW data from an open file.

DBE_FILE.PUT_RAW

Writes RAW data to an open file.

DBE_FILE.FLUSH

Writes cached data to a physical file.

DBE_FILE.CLOSE

Closes an open file.

DBE_FILE.CLOSE_ALL

Closes all files opened in a session.

DBE_FILE.REMOVE

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

DBE_FILE.RENAME

Renames a disk file, which is similar to the mv command of 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

Returns the current offset of a file, in bytes.

DBE_FILE.FOPEN_NCHAR

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

DBE_FILE.WRITE_NCHAR

Writes data to an open file.

DBE_FILE.WRITE_LINE_NCHAR

Writes data to an open file and automatically appends a line terminator.

DBE_FILE.FORMAT_WRITE_NCHAR

Writes formatted data to an open file. It is a DBE_FILE.WRITE_NCHAR API that allows formatting.

DBE_FILE.READ_LINE_NCHAR

Reads a line of a specified length from an open file.

  • DBE_FILE.OPEN/DBE_FILE.FOPEN

    Opens a file. You can specify the maximum line size. A maximum of 50 files can be opened in a session. This function returns a file handle of the INTEGER type. The function of DBE_FILE.FOPEN is similar to that of DBE_FILE.OPEN. It returns an object of the type defined in DBE_FILE.FILE_TYPE.

    The prototype of the DBE_FILE.OPEN function is as follows:
     1
     2
     3
     4
     5
     6
     7
     8
     9
    10
    11
    12
    13
    DBE_FILE.OPEN(
        dir           IN TEXT,
        file_name     IN TEXT,
        open_mode     IN TEXT,
        max_line_size IN INTEGER DEFAULT 1024)
    RETURN INTEGER;
    
    DBE_FILE.FOPEN(
        dir           IN TEXT,
        file_name     IN TEXT,
        open_mode     IN TEXT,
        max_line_size IN INTEGER DEFAULT 1024)
    RETURN DBE_FILE.FILE_TYPE;
    
    Table 3 DBE_FILE.OPEN/DBE_FILE.FOPEN parameters

    Parameter

    Description

    dir

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

    NOTE:
    • Location of the file directory, which needs to be added to 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 is reported.
    • When the GUC parameter safe_data_path is enabled, you can only use an 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

    Open mode of a specified file, including r (read text), w (write text), a (append text), rb (read byte), wb (write byte), and ab (append byte).

    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

    Checks whether a file is closed. 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
    4
    5
    6
    7
    DBE_FILE.IS_CLOSE(
        file IN INTEGER)
    RETURN BOOLEAN;
    
    DBE_FILE.IS_CLOSE(
        file IN DBE_FILE.FILE_TYPE)
    RETURN BOOLEAN;
    
    Table 4 DBE_FILE.IS_CLOSE parameters

    Parameter

    Description

    file

    File handle or DBE_FILE.FILE_TYPE object to be checked

  • DBE_FILE.IS_OPEN

    Checks whether a 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_OPEN function is as follows:

    1
    2
    3
    4
    5
    6
    7
    DBE_FILE.IS_OPEN(
        file IN INTEGER)
    RETURN BOOLEAN;
    
    DBE_FILE.IS_OPEN(
        file IN DBE_FILE.FILE_TYPE)
    RETURN BOOLEAN;
    
    Table 5 DBE_FILE.IS_OPEN parameters

    Parameter

    Description

    file

    File handle or DBE_FILE.FILE_TYPE object to be checked

  • DBE_FILE.READ_LINE

    Reads data from an open file and stores the read result to the buffer. It reads data to the end of each line excluding the line terminator, to the end of the file, or to the size specified by the len parameter. The length of the data to be read cannot exceed the value of max_line_size specified when the file is opened.

    The prototype of the DBE_FILE.READ_LINE stored procedure is as follows:
    1
    2
    3
    4
    5
    6
    7
    8
    9
    DBE_FILE.READ_LINE(
        file   IN  INTEGER,
        buffer OUT TEXT,
        len    IN  INTEGER DEFAULT NULL);
    
    DBE_FILE.READ_LINE(
        file   IN  DBE_FILE.FILE_TYPE,
        buffer OUT TEXT,
        len    IN  INTEGER DEFAULT NULL);
    
    Table 6 DBE_FILE.READ_LINE parameters

    Parameter

    Description

    file

    File handle opened using OPEN or object of the DBE_FILE.FILE_TYPE type opened using FOPEN. The file must be opened in read mode. Otherwise, the INVALID_OPERATION exception is thrown.

    buffer

    Buffer for receiving data.

    len

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

  • DBE_FILE.WRITE

    Writes data in the buffer to a file. The file must be opened in write mode. This operation does not write a line terminator, and the return value is always TRUE.

    The prototype of the DBE_FILE.WRITE function is as follows:
    1
    2
    3
    4
    5
    6
    7
    8
    9
    DBE_FILE.WRITE(
        file   IN INTEGER,
        buffer IN TEXT)
    RETURN BOOLEAN;
    
    DBE_FILE.WRITE(
        file   IN DBE_FILE.FILE_TYPE,
        buffer IN TEXT)
    RETURN VOID;
    
    Table 7 DBE_FILE.WRITE parameters

    Parameter

    Description

    file

    File handle opened using OPEN or object of the DBE_FILE.FILE_TYPE type opened using FOPEN. The file must be opened in write mode. This operation does not write line terminators.

    buffer

    Text data to be written to the file. The accumulated write length of each line cannot be greater than or equal to the value of max_line_size specified when OPEN or FOPEN is used. Otherwise, an error is reported when the file is refreshed.

    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

    Writes one or more line terminators to an open file. The line terminators are related to the platform used, and the return value is always TRUE.

    The prototype of the DBE_FILE.NEW_LINE function is as follows:
    1
    2
    3
    4
    5
    6
    7
    8
    9
    DBE_FILE.NEW_LINE(
        file      IN INTEGER,
        line_nums IN INTEGER DEFAULT 1)
    RETURN BOOLEAN;
    
    DBE_FILE.NEW_LINE(
        file      IN DBE_FILE.FILE_TYPE,
        line_nums IN INTEGER DEFAULT 1)
    RETURN VOID;
    
    Table 8 DBE_FILE.NEW_LINE parameters

    Parameter

    Description

    file

    File handle opened using OPEN or object of the DBE_FILE.FILE_TYPE type opened using FOPEN.

    line_nums

    Number of line terminators written to a file.

  • DBE_FILE.WRITE_LINE

    Writes data in the buffer to a file. The file must be opened in write mode. This operation automatically appends line terminators, and the return value is always TRUE.

    The prototype of the DBE_FILE.WRITE_LINE function is as follows:
     1
     2
     3
     4
     5
     6
     7
     8
     9
    10
    11
    DBE_FILE.WRITE_LINE(
        file   IN INTEGER,
        buffer IN TEXT,
        flush  IN BOOLEAN DEFAULT FALSE)
    RETURN BOOLEAN;
    
    DBE_FILE.WRITE_LINE(
        file   IN DBE_FILE.FILE_TYPE,
        buffer IN TEXT,
        flush  IN BOOLEAN DEFAULT FALSE)
    RETURN VOID;
    
    Table 9 DBE_FILE.WRITE_LINE parameters

    Parameter

    Description

    file

    File handle opened using OPEN or object of the DBE_FILE.FILE_TYPE type opened using FOPEN.

    buffer

    Text data to be written to the file. The length of each line (including the newline character) cannot be greater than the value of max_line_size specified when OPEN or FOPEN is executed or the default value. Otherwise, an error is reported when the file is refreshed.

    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 disks after WRITE_LINE. The default value is FALSE.

  • DBE_FILE.FORMAT_WRITE

    Writes formatted data to an open file. It is a DBE_FILE.WRITE API that allows formatting. The return value is always TRUE.

    The prototype of the DBE_FILE.FORMAT_WRITE function is as follows:
     1
     2
     3
     4
     5
     6
     7
     8
     9
    10
    11
    12
    13
    14
    15
    DBE_FILE.FORMAT_WRITE(
        file   IN INTEGER,
        format IN TEXT,
        arg1   IN TEXT DEFAULT NULL,
        . . .
        arg6   IN TEXT DEFAULT NULL)
    RETURN BOOLEAN;
    
    DBE_FILE.FORMAT_WRITE(
        file   IN DBE_FILE.FILE_TYPE,
        format IN TEXT,
        arg1   IN TEXT DEFAULT NULL,
        . . .
        arg6   IN TEXT DEFAULT NULL)
    RETURN VOID;
    
    Table 10 DBE_FILE.FORMAT_WRITE parameters

    Parameter

    Description

    file

    File handle opened using OPEN or object of the DBE_FILE.FILE_TYPE type opened using FOPEN.

    format

    Formatted string, 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

    Reads RAW data from an open file, stores the read result in the buffer, and returns the result from r.

    The prototype of the DBE_FILE.GET_RAW stored procedure is as follows:
    1
    2
    3
    4
    5
    6
    7
    8
    9
    DBE_FILE.GET_RAW(
        file   IN  INTEGER,
        r      OUT RAW,
        length IN  INTEGER DEFAULT NULL);
    
    DBE_FILE.GET_RAW(
        file   IN  DBE_FILE.FILE_TYPE,
        r      OUT RAW,
        length IN  INTEGER DEFAULT NULL);
    
    Table 11 DBE_FILE.GET_RAW parameters

    Parameter

    Description

    file

    File handle opened using OPEN or object of the DBE_FILE.FILE_TYPE type opened using FOPEN.

    r

    Buffer for receiving RAW data.

    length

    Number of bytes read from the file. The default value is NULL. If the value is NULL, the maximum length of the RAW type is used to specify the size.

  • DBE_FILE.PUT_RAW

    Writes RAW data to a file. The return value is always TRUE.

    The prototype of the DBE_FILE.PUT_RAW function is as follows:
     1
     2
     3
     4
     5
     6
     7
     8
     9
    10
    11
    DBE_FILE.PUT_RAW (
        file  IN INTEGER,
        r     IN RAW,
        flush IN BOOLEAN DEFAULT FALSE)
    RETURN BOOLEAN;
    
    DBE_FILE.PUT_RAW (
        file  IN DBE_FILE.FILE_TYPE,
        r     IN RAW,
        flush IN BOOLEAN DEFAULT FALSE)
    RETURN VOID;
    
    Table 12 DBE_FILE.PUT_RAW parameters

    Parameter

    Description

    file

    File handle opened using OPEN or object of the DBE_FILE.FILE_TYPE type opened using FOPEN.

    r

    RAW data to be written to a file.

    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 disks after PUT_RAW. The default value is FALSE.

  • DBE_FILE.FLUSH

    Writes cached data to a physical file. The cached data must have a line terminator.

    The prototype of the DBE_FILE.FLUSH function is as follows:
    1
    2
    3
    4
    5
    6
    7
    DBE_FILE.FLUSH(
        file IN INTEGER)
    RETURN VOID;
    
    DBE_FILE.FLUSH(
        file IN DBE_FILE.FILE_TYPE)
    RETURN VOID;
    
    Table 13 DBE_FILE.FLUSH parameters

    Parameter

    Description

    file

    File handle opened using OPEN or object of the DBE_FILE.FILE_TYPE type opened using FOPEN.

  • DBE_FILE.CLOSE

    Closes an open file. When this function is called, if there is cached data to be written, exception information may be received and the return value is always TRUE.

    The prototype of the DBE_FILE.CLOSE function is as follows:
    1
    2
    3
    4
    5
    6
    7
    DBE_FILE.CLOSE(
        file IN INTEGER)
    RETURN BOOLEAN;
    
    DBE_FILE.CLOSE(
        file IN DBE_FILE.FILE_TYPE)
    RETURN BOOLEAN;
    
    Table 14 DBE_FILE.CLOSE parameters

    Parameter

    Description

    file

    File handle opened using OPEN or object of the DBE_FILE.FILE_TYPE type opened using FOPEN.

  • DBE_FILE.CLOSE_ALL

    Closes all file handles opened in a session. This function can be used for emergency cleanup.

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

    1
    2
    DBE_FILE.CLOSE_ALL()
    RETRUN VOID;
    
    Table 15 DBE_FILE.CLOSE_ALL parameters

    Parameter

    Description

    None

    None

  • DBE_FILE.REMOVE

    Deletes a disk file. To use this function, you must have the required permission.

    The prototype of the DBE_FILE.REMOVE function is as follows:
    1
    2
    3
    4
    DBE_FILE.REMOVE(
        dir       IN TEXT,
        file_name IN TEXT)
    RETURN VOID;
    
    Table 16 DBE_FILE.REMOVE parameters

    Parameter

    Description

    dir

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

    NOTE:
    • Location of the file directory, which needs to be added to 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 is reported.
    • When the GUC parameter safe_data_path is enabled, you can only use an advanced package to operate files in the file path specified by safe_data_path.

    file_name

    File name.

  • DBE_FILE.RENAME

    Renames a disk file. This function is similar to the mv command of Unix.

    The prototype of the DBE_FILE.RENAME function is as follows:
    1
    2
    3
    4
    5
    6
    7
    DBE_FILE.RENAME(
        src_dir        IN TEXT,
        src_file_name  IN TEXT,
        dest_dir       IN TEXT,
        dest_file_name IN TEXT,
        overwrite      IN BOOLEAN DEFAULT FALSE)
    RETURN VOID;
    
    Table 17 DBE_FILE.RENAME parameters

    Parameter

    Description

    src_dir

    Directory of the original file (case-sensitive)

    NOTE:
    • Location of the file directory, which needs to be added to 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 is reported.
    • When the GUC parameter safe_data_path is enabled, you can only use an advanced package to operate files in the file path specified by safe_data_path.

    src_file_name

    Original file to be renamed

    dest_dir

    Target directory (case-sensitive).

    NOTE:
    • Location of the file directory, which needs to be added to 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 is reported.
    • When the GUC parameter safe_data_path is enabled, you can only use an advanced package to operate files in the file path specified by safe_data_path.

    dest_file_name

    New file name

    overwrite

    Whether to overwrite the file. The default value is FALSE. If a file with the same name exists in the target directory, the file will not be overwritten.

  • 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.

    The prototype of the DBE_FILE.COPY function is as follows:
    1
    2
    3
    4
    5
    6
    7
    8
    DBE_FILE.COPY(
        src_dir        IN TEXT,
        src_file_name  IN TEXT,
        dest_dir       IN TEXT,
        dest_file_name IN TEXT,
        start_line     IN INTEGER DEFAULT 1,
        end_line       IN INTEGER DEFAULT NULL)
    RETURN VOID;
    
    Table 18 DBE_FILE.COPY parameters

    Parameter

    Description

    src_dir

    Directory of the original file

    NOTE:
    • Location of the file directory, which needs to be added to 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 is reported.
    • When the GUC parameter safe_data_path is enabled, you can only use an advanced package to operate files in the file path specified by safe_data_path.

    src_file_name

    Original file

    dest_dir

    Directory of the destination file

    NOTE:
    • Location of the file directory, which needs to be added to 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 is reported.
    • When the GUC parameter safe_data_path is enabled, you can only use an advanced package to operate files in the file path specified by safe_data_path.

    dest_file_name

    Target file

    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

    Reads and returns the attributes of a disk file.

    The prototype of the DBE_FILE.GET_ATTR stored procedure is as follows:
    1
    2
    3
    4
    5
    6
    DBE_FILE.GET_ATTR(
        location    IN  TEXT,
        filename    IN  TEXT,
        fexists     OUT BOOLEAN,
        file_length OUT BIGINT,
        block_size  OUT INTEGER);
    
    Table 19 DBE_FILE.GET_ATTR parameters

    Parameter

    Description

    location

    File directory

    NOTE:
    • Location of the file directory, which needs to be added to 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 is reported.
    • When the GUC parameter safe_data_path is enabled, you can only use an advanced package to operate files in the file path specified by safe_data_path.

    filename

    File name.

    fexists

    Whether the file exists.

    file_length

    File length (unit: byte). 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

    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
     5
     6
     7
     8
     9
    10
    11
    DBE_FILE.SEEK(
        file           IN INTEGER,
        absolute_start IN BIGINT DEFAULT NULL,
        relative_start IN BIGINT DEFAULT NULL)
    RETURN VOID;
    
    DBE_FILE.SEEK(
        file           IN DBE_FILE.FILE_TYPE,
        absolute_start IN BIGINT DEFAULT NULL,
        relative_start IN BIGINT DEFAULT NULL)
    RETURN VOID;
    
    Table 20 DBE_FILE.SEEK parameters

    Parameter

    Description

    file

    File handle opened using OPEN or object of the DBE_FILE.FILE_TYPE type opened using FOPEN.

    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

    Returns the current offset of the file in bytes.

    The prototype of the DBE_FILE.FGETPOS function is as follows:
    1
    2
    3
    4
    5
    6
    7
    DBE_FILE.GET_POS(
        file IN INTEGER)
    RETURN BIGINT;
    
    DBE_FILE.GET_POS(
        file IN DBE_FILE.FILE_TYPE)
    RETURN BIGINT;
    
    Table 21 DBE_FILE.GET_POS parameters

    Parameter

    Description

    file

    File handle opened using OPEN or object of the DBE_FILE.FILE_TYPE type opened using FOPEN.

  • DBE_FILE.FOPEN_NCHAR

    Opens a file. You can specify the maximum line size. A maximum of 50 files can be opened in a session. This function returns a file handle of the DBE_FILE.FILE_TYPE type. This function opens a file in national character set mode for input or output.

    The prototype of the DBE_FILE.FOPEN_NCHAR function is as follows:
    1
    2
    3
    4
    5
    6
    DBE_FILE.FOPEN_NCHAR(
        dir           IN TEXT,
        file_name     IN TEXT,
        open_mode     IN TEXT,
        max_line_size IN INTEGER DEFAULT 1024)
    RETURN DBE_FILE.FILE_TYPE;
    
    Table 22 DBE_FILE.FOPEN_NCHAR parameters

    Parameter

    Description

    dir

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

    NOTE:
    • Location of the file directory, which needs to be added to 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 is reported.
    • When the GUC parameter safe_data_path is enabled, you can only use an 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 FOPEN_NCHAR function. In Unix, the file name cannot end with the combination of a slash and a dot (/.).

    open_mode

    Open mode of a specified file, including r (read text), w (write text), a (append text), rb (read byte), wb (write byte), and ab (append byte).

    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.WRITE_NCHAR

    Writes data in the buffer to a file. The file must be opened in national character set or write mode. This operation does not write a line terminator, and the return value is always TRUE. The text string is written in the UTF8 character set format.

    The prototype of the DBE_FILE.WRITE_NCHAR function is as follows:
    1
    2
    3
    4
    DBE_FILE.WRITE_NCHAR(
        file   IN DBE_FILE.FILE_TYPE,
        buffer IN NVARCHAR2)
    RETURN VOID;
    
    Table 23 DBE_FILE.WRITE parameters

    Parameter

    Description

    file

    Object of the DBE_FILE.FILE_TYPE type opened using FOPEN_NCHAR. The file must be opened in write mode. This operation does not write line terminators.

    buffer

    Text data to be written to the file. The accumulated write length of each line cannot be greater than or equal to the value of max_line_size specified by FOPEN_NCHAR. Otherwise, an error is reported when the file is refreshed.

    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.WRITE_LINE_NCHAR

    Writes data in the buffer to a file. The file must be opened in national character set or write mode. This operation automatically appends line terminators, and the return value is always TRUE. The text string is written in the UTF8 character set format.

    The prototype of the DBE_FILE.WRITE_LINE_NCHAR function is as follows:
    1
    2
    3
    4
    DBE_FILE.WRITE_LINE_NCHAR(
        file   IN DBE_FILE.FILE_TYPE,
        buffer IN NVARCHAR2)
    RETURN VOID;
    
    Table 24 DBE_FILE.WRITE_LINE parameters

    Parameter

    Description

    file

    Object of the DBE_FILE.FILE_TYPE type opened using FOPEN_NCHAR.

    buffer

    Text data to be written to the file. The length of each line (including the newline character) cannot be greater than the value of max_line_size specified by FOPEN_NCHAR or the default value. Otherwise, an error is reported when the file is refreshed.

    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.FORMAT_WRITE_NCHAR

    Writes formatted data to an open file. It is a DBE_FILE.WRITE_NCHAR API that allows formatting. The return value is always TRUE.

    The prototype of the DBE_FILE.FORMAT_WRITE_NCHAR function is as follows:
    1
    2
    3
    4
    5
    6
    7
    DBE_FILE.FORMAT_WRITE_NCHAR(
        file   IN DBE_FILE.FILE_TYPE,
        format IN NVARCHAR2,
        arg1   IN NVARCHAR2 DEFAULT NULL,
        . . .
        arg5   IN NVARCHAR2 DEFAULT NULL)
    RETURN VOID;
    
    Table 25 DBE_FILE.FORMAT_WRITE_NCHAR parameters

    Parameter

    Description

    file

    Object of the DBE_FILE.FILE_TYPE type opened using FOPEN_NCHAR.

    format

    Formatted string, containing the text and format characters \n and %s.

    [arg1. . .arg5]

    Five 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.READ_LINE_NCHAR

    Reads data from an open file and stores the read result to the buffer. It reads data to the end of each line excluding the line terminator, to the end of the file, or to the size specified by the len parameter. The length of the data to be read cannot exceed the value of max_line_size specified by FOPEN_NCHAR.

    The prototype of the DBE_FILE.READ_LINE_NCHAR stored procedure is as follows:
    1
    2
    3
    4
    DBE_FILE.READ_LINE_NCHAR(
        file   IN  DBE_FILE.FILE_TYPE,
        buffer OUT NVARCHAR2,
        len    IN  INTEGER DEFAULT NULL);
    
    Table 26 DBE_FILE.READ_LINE parameters

    Parameter

    Description

    file

    Object of the DBE_FILE.FILE_TYPE type opened using FOPEN_NCHAR. The file must be opened in read mode. Otherwise, the INVALID_OPERATION exception is thrown.

    buffer

    Buffer for receiving data.

    len

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

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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
-- 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 DBE_FILE.FILE_TYPE;
buffer VARCHAR2;
BEGIN
-- Write a file.
f := DBE_FILE.FOPEN('dir', 'sample.txt', 'w');
DBE_FILE.WRITE(f, 'A');
DBE_FILE.WRITE(f, 'B');
DBE_FILE.WRITE(f, 'C');
DBE_FILE.NEW_LINE(f);
DBE_FILE.WRITE_LINE(f, 'ABC');
DBE_FILE.FORMAT_WRITE(f, '[1 -> %s, 2 -> %s]\n', 'GaussDB', 'DBE_FILE');
DBE_FILE.CLOSE(f);
-- Read a file.
f := DBE_FILE.FOPEN('dir', 'sample.txt', 'r');
DBE_FILE.READ_LINE(f, buffer); -- ABC
DBE_FILE.READ_LINE(f, buffer); -- ABC
DBE_FILE.READ_LINE(f, buffer); -- [1 -> GaussDB, 2 -> DBE_FILE]
DBE_OUTPUT.PRINT_LINE(buffer); 
DBE_FILE.CLOSE(f);
END;
/

-- Offset the position of the file handle and obtain the current position of the file.
DECLARE
f DBE_FILE.FILE_TYPE;
buffer VARCHAR2;
pos BIGINT;
BEGIN
-- Read a file.
f := DBE_FILE.FOPEN('dir', 'sample.txt', 'r');
DBE_FILE.SEEK(f, 8);
DBE_FILE.READ_LINE(f, buffer); -- [1 -> GaussDB, 2 -> DBE_FILE]
DBE_OUTPUT.PRINT_LINE(buffer);
pos := DBE_FILE.GET_POS(f); -- 38
DBE_OUTPUT.PRINT_LINE(pos); 
DBE_FILE.CLOSE(f);
END;
/
-- NCHAR read/write API cases
DECLARE
f DBE_FILE.FILE_TYPE;
buffer NVARCHAR2;
BEGIN
-- Write a file.
f := DBE_FILE.FOPEN_NCHAR('dir', 'sample02.txt', 'w');
DBE_FILE.WRITE_NCHAR(f, 'A');
DBE_FILE.WRITE_NCHAR(f, 'B');
DBE_FILE.WRITE_NCHAR(f, 'C');
DBE_FILE.NEW_LINE(f);
DBE_FILE.WRITE_LINE_NCHAR(f, 'ABC');
DBE_FILE.FORMAT_WRITE_NCHAR(f, '[1 -> %s, 2 -> %s]\n', 'GaussDB', 'DBE_FILE');
DBE_FILE.CLOSE(f);
-- Read a file.
f := DBE_FILE.FOPEN_NCHAR('dir', 'sample02.txt', 'r');
DBE_FILE.READ_LINE_NCHAR(f, buffer); -- ABC
DBE_FILE.READ_LINE_NCHAR(f, buffer); -- ABC
DBE_FILE.READ_LINE_NCHAR(f, buffer); -- [1 -> GaussDB, 2 -> DBE_FILE]
DBE_OUTPUT.PRINT_LINE(buffer); 
DBE_FILE.CLOSE(f);
END;
/