dcsimg
DBAsupport.com Forums - Powered by vBulletin
Results 1 to 2 of 2

Thread: User defined Constructors

  1. #1
    Join Date
    Feb 2002
    Posts
    1

    Exclamation

    Recently I have started to work with user defined datatypes.
    I came across a problem: How do I overload the default constructor Oracle provides me?

    Say I have tName which consists of:
    FirstName VARCHAR(10);
    LastName VARCHAR(10);

    And I want my constructor to, first evaluate the length of the names given, trim them if neccessery, and only then return the object.

    Something of the such:
    create or replace function tName(fname in VARCHAR, lname in VARCHAR)
    begin
    return tName(substr(fname,0,10),substr(lname,0,10));
    -- Although by calling the constructor of tName, I enter an enfinite loop!!!
    end;

    Please help!

    Thanks in advance,
    Really.

  2. #2
    Join Date
    Dec 2001
    Posts
    19
    Hi,
    The first problem I see is that you are creating a function named tName and calling that function from inside. That will cause the infinite loop.
    ___________________________________
    Something of the such:
    create or replace function tName(fname in VARCHAR, lname in VARCHAR)
    begin
    return tName(substr(fname,0,10),substr(lname,0,10));
    -- Although by calling the constructor of tName, I enter an enfinite loop!!!
    end;
    ___________________________________

    I am not sure whether you require a construct or simply a function to measure and trim your data.

    It's been a long time since I have done anything like this but I did find some info that might help you.

    Good Luck.

    If you have metalink access.

    http://metalink.oracle.com/metalink/...id=1013450.102

    or

    When using objects in PL/SQL it is important to remember that they are not
    ordinary primitive datatypes. They must be implemented in such a manner
    that all of the components of each object are properly initialized. This is
    accomplished via the invocation of appropriate 'constructors', which ensure
    acceptable settings for each of the attributes or data members of the
    respective object.


    Procedure
    ---------

    The following actions should be taken to ensure each object is ready, before
    it is used in a PL/SQL process:

    1. Define each object with the CREATE [OR REPLACE] TYPE statement.
    This includes creation of the specification and body for member
    attributes and methods.

    Example Specification:

    CREATE OR REPLACE TYPE Employee_Obj AS OBJECT (
    Name VARCHAR2(20),
    Id NUMBER(5),
    Sal NUMBER(5),

    MEMBER PROCEDURE Change_Salary (p_New_Sal IN NUMBER),
    PRAGMA RESTRICT_REFERENCES (Change_Salary,
    RNDS, WNDS, RNPS, WNPS)
    );

    Example Body:

    CREATE OR REPLACE TYPE BODY Employee_Obj AS
    MEMBER PROCEDURE Change_Salary (p_New_Sal IN NUMBER) IS
    BEGIN
    Sal := p_New_Sal;
    END Change_Salary;
    END;

    2. Declare the object as you would any variable using the variable
    name and the type name.

    Example:

    DECLARE
    v_employee Employee_Obj;

    OR

    v_employee Employee_Obj := Employee_Obj ('Tony King', 12345, 54321);

    OR

    v_employee Employee_Obj := Employee_Obj (NULL,NULL,NULL);

    3. Initialize the object using an available constructor for that object.
    Default constructors are implicitly provided by PL/SQL, but these
    constructors must be explicitly invoked.

    The default constructor contains an argument for each of the attributes
    listed in the object definition, in the order they are defined. Until
    the constructor is invoked, the object is initialized to NULL, and cannot
    be used or accessed.

    Example:

    DECLARE
    v_employee := Employee_Obj ('Tony King', 12345, 54321);

    OR

    v_employee := Employee_Obj (NULL,NULL,NULL);


    4. In first example below, an error occurs because an attempt is made to
    invoke the method Change_Salary on the object v_employee, before a
    constructor is executed.

    The solution to this problem is to always explicitly call a constructor
    for any object you create. This not only permits access, it ensures that
    internal object attributes are set to known values prior to activation of
    the object.

    In the second example, even though one of the values passed to the
    constructor for an attribute was NULL, this was sufficient to initialize
    the object and allow it to be accessed.

    Example:

    DECLARE
    v_employee Employee_Obj;
    v_new_salary NUMBER(5);
    BEGIN
    v_new_salary := 75000;
    v_employee.Change_Salary (v_new_salary);
    END;

    DECLARE
    *
    ERROR at line 1:
    ORA-06530: Reference to uninitialized composite
    ORA-06512: at "SCOTT.EMPLOYEE_OBJ", line 4
    ORA-06512: at line 6

    Example:

    DECLARE
    v_employee Employee_Obj := Employee_Obj ('KING', 12345, NULL);
    v_new_salary NUMBER(5);
    BEGIN
    v_new_salary := 75000;
    v_employee.Change_Salary (v_new_salary);
    END;

    PL/SQL procedure successfully completed.


Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Click Here to Expand Forum to Full Width