Top
Symfoware Server V12.1.0 Application Development Guide
FUJITSU Software

B.6.2 Checking File Information

Oracle database

CREATE PROCEDURE read_file(fname VARCHAR2) AS


    v_file      UTL_FILE.FILE_TYPE;
    v_exists    BOOLEAN;
    v_length    NUMBER;
    v_bsize     INTEGER;
    v_rbuff     VARCHAR2(1024);
BEGIN


    UTL_FILE.FGETATTR('DIR', fname, v_exists, v_length, v_bsize); ...(2)


    IF v_exists <> true THEN
        DBMS_OUTPUT.PUT_LINE('-- FILE NOT FOUND --');
        RETURN;
    END IF;

    DBMS_OUTPUT.PUT_LINE('-- FILE DATA --');

    v_file := UTL_FILE.FOPEN('DIR', fname, 'r', 1024); ...(3)
    FOR i IN 1..3 LOOP
        UTL_FILE.GET_LINE(v_file, v_rbuff, 1024); ...(4)
        DBMS_OUTPUT.PUT_LINE(v_rbuff);
    END LOOP;
    DBMS_OUTPUT.PUT_LINE('... more');
    DBMS_OUTPUT.PUT_LINE('-- READ END --');

    UTL_FILE.FCLOSE(v_file); ...(5)
    RETURN;

EXCEPTION
    WHEN NO_DATA_FOUND THEN
        DBMS_OUTPUT.PUT_LINE('-- FILE END --');

        UTL_FILE.FCLOSE(v_file); 
        RETURN;
    WHEN OTHERS THEN
        DBMS_OUTPUT.PUT_LINE('-- SQL Error --');

        DBMS_OUTPUT.PUT_LINE('ERROR : ' || SQLERRM );
        UTL_FILE.FCLOSE_ALL; ...(6)
        RETURN;
END;
/

set serveroutput on

call read_file('file01.txt');

Symfoware Server

CREATE FUNCTION read_file(fname VARCHAR) RETURNS void AS $$
DECLARE
    v_file      UTL_FILE.FILE_TYPE;
    v_exists    BOOLEAN;
    v_length    NUMERIC;
    v_bsize     INTEGER;
    v_rbuff     VARCHAR(1024);
BEGIN
    PERFORM DBMS_OUTPUT.SERVEROUTPUT(TRUE);

    SELECT fexists, file_length, blocksize
       INTO v_exists, v_length, v_bsize
       FROM UTL_FILE.FGETATTR('/home/symfo', fname); ...(2)
    IF v_exists <> true THEN
        PERFORM DBMS_OUTPUT.PUT_LINE('-- FILE NOT FOUND --');
        RETURN;
    END IF;

    PERFORM DBMS_OUTPUT.PUT_LINE('-- FILE DATA --');
    v_file := UTL_FILE.FOPEN('/home/symfo', fname, 'w', 1024); ...(3)
    FOR i IN 1..3 LOOP
        v_rbuff := UTL_FILE.GET_LINE(v_file, 1024); ...(4)
        PERFORM DBMS_OUTPUT.PUT_LINE(v_rbuff);
    END LOOP;
    PERFORM DBMS_OUTPUT.PUT_LINE('... more');
    PERFORM DBMS_OUTPUT.PUT_LINE('-- READ END --');

    v_file := UTL_FILE.FCLOSE(v_file); ...(5)
    RETURN;

EXCEPTION
    WHEN NO_DATA_FOUND THEN
        PERFORM DBMS_OUTPUT.PUT_LINE('-- FILE END --');
        v_file := UTL_FILE.FCLOSE(v_file);
        RETURN;
    WHEN OTHERS THEN
        PERFORM DBMS_OUTPUT.PUT_LINE('-- SQL Error --');
        PERFORM DBMS_OUTPUT.PUT_LINE('ERROR : ' || SQLERRM );
        PERFORM UTL_FILE.FCLOSE_ALL(); ...(6)
        RETURN;
END;
$$ 
LANGUAGE plpgsql;

SELECT read_file('file01.txt');

(2) FGETATTR
Specification format for Oracle database

UTL_FILE.FGETATTR(firstArg, secondArg, thirdArg, fourthArg, fifthArg)

Feature differences
Oracle database

If using a CREATE DIRECTORY statement (Oracle9.2i or later), specify a directory object name for the directory name.

Symfoware Server

A directory object name cannot be specified for the directory name.

Specification differences
Oracle database

Obtained values are received with variables specified for arguments.

Symfoware Server

Since obtained values are the search results for UTL_FILE.FGETATTR, they are received with variables specified for the INTO clause of the SELECT statement.

Conversion procedure

Convert using the following procedure. Refer to UTL_FILE_DIR/CREATE DIRECTORY for information on how to check if the directory object name corresponds to the actual directory name.

  1. Locate the places where the keyword "UTL_FILE.FOPEN" is used in the stored procedure.

  2. Check the actual directory name ('/home/symfo' in the example) that corresponds to the directory object name ('DIR' in the example).

  3. Replace the directory object name ('DIR' in the example) in firstArg with the actual directory name ('/home/symfo' in the example) verified in step 2.

  4. Replace the UTL_FILE.FGETATTR location called with a SELECT INTO statement.

    • Use the literal "fexists, file_length, blocksize" in the select list.

    • Specify thirdArg, fourthArg, and fifthArg (v_exists, v_length, v_bsize, in the example) specified for UTL_FILE.FGETATTR to the INTO clause in the same order as that of the arguments.

    • Use UTL_FILE.FGETATTR in the FROM clause. Specify only the actual directory name for firstArg ('/home/symfo' in the example) and secondArg (fname in the example) before modification for the arguments.


(3) FOPEN
Specification format for Oracle

UTL_FILE.FOPEN(firstArg, secondArg, thirdArg, fourthArg, fifthArg)

Feature differences
Oracle database

If using a CREATE DIRECTORY statement (Oracle9.2i or later), specify a directory object name for the directory name.

Symfoware Server

A directory object name cannot be specified for the directory name.

Conversion procedure

Convert using the following procedure. Refer to UTL_FILE_DIR/CREATE DIRECTORY for information on how to check if the directory object name corresponds to the actual directory name.

  1. Locate the places where the keyword "UTL_FILE.FOPEN" is used in the stored procedure.

  2. Check the actual directory name ('/home/symfo' in the example) that corresponds to the directory object name ('DIR' in the example).

  3. Replace the directory object name ('DIR' in the example) in firstArg with the actual directory name ('/home/symfo' in the example) checked in step 1.


(4) GET_LINE
Specification format for Oracle database

UTL_FILE.GET_LINE(firstArg, secondArg, thirdArg, fourthArg)

Specification differences
Oracle database

Obtained values are received with variables specified for arguments.

Symfoware Server

Since obtained values are the returned value of UTL_FILE.GET_LINE, they are received with variables specified for substitution statement.

Conversion procedure

Convert using the following procedure:

  1. Locate the places where the keyword "UTL_FILE.GET_LINE" is used in the stored procedure.

  2. Replace the UTL_FILE.GET_LINE location called with a value assignment (:=).

    • On the left-hand side, specify secondArg (v_rbuff in the example) specified for UTL_FILE.GET_LINE.

    • Use UTL_FILE.GET_LINE in the right-hand side. Specify only firstArg (v_file in the example) and thirdArg (1024 in the example) before modification.


(5) FCLOSE
Specification format for Oracle database

UTL_FILE.FCLOSE(firstArg)

Specification differences
Oracle database

After closing, the file handler specified for the argument becomes NULL.

Symfoware Server

After closing, set the file handler to NULL by assigning the return value of UTL_FILE.FCLOSE to it.

Conversion procedure

Convert using the following procedure:

  1. Locate the places where the keyword "UTL_FILE.FCLOSE" is used in the stored procedure.

  2. Replace the UTL_FILE.FCLOSE location called with a value assignment (:=) so that the file handler (v_file in the example) becomes NULL.

    • On the left-hand side, specify the argument (v_file in the example) specified for UTL_FILE.FCLOSE.

    • Use UTL_FILE.FCLOSE in the right-hand side. For the argument, specify the same value (v_file in the example) as before modification.


(6) FCLOSE_ALL

Same as NEW_LINE in the DBMS_OUTPUT package. Refer to NEW_LINE in the DBMS_OUTPUT for information on specification differences and conversion procedures associated with specification differences.