Insert Object data in Oracle 12c

63 views Asked by At

I am trying to insert data in my table where one of the columns is of Object type, but I am getting the following error:

RA-06553: PLS-306: wrong number or types of arguments in call to 'SET_DATUME'

Table definition is:

CREATE TABLE Trebovanje (
BrDokumenta NUMBER,
Datumi DATUMI_T,
SifRJ NUMBER,
BrRadKnjiz NUMBER,
CONSTRAINT trebovanje_pk PRIMARY KEY (BrDokumenta),
CONSTRAINT treb_rj_fk FOREIGN KEY (SifRJ) REFERENCES RadnaJedinica (SifRJ),
CONSTRAINT trebovanje_zap_fk FOREIGN KEY (BrRadKnjiz) REFERENCES Zaposleni (BrRadKnjiz)
);

DATUMI_T user defined type:

create or replace TYPE DATUMI_T AS OBJECT  ( 
DatPrijema DATE,   
DatStampanja DATE,
MEMBER FUNCTION get_dat_prijema RETURN DATE,
MEMBER FUNCTION get_dat_stampanja RETURN DATE,
MEMBER PROCEDURE set_datume(p_dat_prijema DATE, p_dat_stampanja DATE),
MEMBER FUNCTION proveri_dat_stampanja(p_dat_stampanja DATE) RETURN BOOLEAN
);

create or replace TYPE BODY DATUMI_T AS

  MEMBER FUNCTION get_dat_prijema RETURN DATE AS   
  BEGIN
    RETURN self.DatPrijema;   
  END get_dat_prijema;

  MEMBER FUNCTION get_dat_stampanja RETURN DATE AS   
  BEGIN
    RETURN self.DatStampanja;   
  END get_dat_stampanja;

  MEMBER PROCEDURE set_datume(p_dat_prijema DATE, p_dat_stampanja DATE) AS   
  BEGIN
    self.DatPrijema := p_dat_prijema;

    IF proveri_dat_stampanja(p_dat_stampanja)
    THEN
      self.DatStampanja := p_dat_stampanja;
    ELSE
      self.DatStampanja := SYSDATE;
    END IF;   
  END set_datUME;

  MEMBER FUNCTION proveri_dat_stampanja(p_dat_stampanja DATE) RETURN BOOLEAN AS   
  ok BOOLEAN;   
  BEGIN    
    ok := self.DatPrijema < p_dat_stampanja;
    RETURN ok;   
  END proveri_dat_stampanja;
END;

And my insert statement:

insert into trebovanje values (
  1, 
  DATUMI_T.set_datume(
    TO_DATE('2017-01-02 17:44:33', 'YYYY-MM-DD HH24:MI:SS'), 
    TO_DATE('2017-01-02 17:55:33', 'YYYY-MM-DD HH24:MI:SS')
    ),
  1,
  1234);

Any thoughts on this?

1

There are 1 answers

0
Aleksej On BEST ANSWER

What you seem to need is define a constructor that "hides" the default constructor.

CREATE OR REPLACE TYPE DATUMI_T AS OBJECT
(
    DatPrijema DATE,
    DatStampanja DATE,
    MEMBER FUNCTION get_dat_prijema
        RETURN DATE,
    MEMBER FUNCTION get_dat_stampanja
        RETURN DATE,
    MEMBER PROCEDURE set_datume(p_dat_prijema DATE, p_dat_stampanja DATE),
    MEMBER FUNCTION proveri_dat_stampanja(p_dat_stampanja DATE)
        RETURN BOOLEAN,
    /* the declaration of your own constructor ( same name of the type ) */
    CONSTRUCTOR FUNCTION DATUMI_T(DatPrijema DATE, DatStampanja DATE)
        RETURN SELF AS RESULT
);
create or replace TYPE BODY DATUMI_T AS

  MEMBER FUNCTION get_dat_prijema RETURN DATE AS   
  BEGIN
    RETURN self.DatPrijema;   
  END get_dat_prijema;

  MEMBER FUNCTION get_dat_stampanja RETURN DATE AS   
  BEGIN
    RETURN self.DatStampanja;   
  END get_dat_stampanja;

  MEMBER PROCEDURE set_datume(p_dat_prijema DATE, p_dat_stampanja DATE) AS   
  BEGIN
    self.DatPrijema := p_dat_prijema;

    IF proveri_dat_stampanja(p_dat_stampanja)
    THEN
      self.DatStampanja := p_dat_stampanja;
    ELSE
      self.DatStampanja := SYSDATE;
    END IF;   
  END set_datUME;

  /* the body of your constructor */
  CONSTRUCTOR FUNCTION DATUMI_T(DatPrijema DATE, DatStampanja DATE)
    RETURN SELF AS RESULT as  
  BEGIN
    self.DatPrijema := DatPrijema;

    IF proveri_dat_stampanja(DatStampanja)
    THEN
      self.DatStampanja := DatStampanja;
    ELSE
      self.DatStampanja := SYSDATE;
    END IF;   
    return;
  END DATUMI_T;

  MEMBER FUNCTION proveri_dat_stampanja(p_dat_stampanja DATE) RETURN BOOLEAN AS   
  ok BOOLEAN;   
  BEGIN    
    ok := self.DatPrijema < p_dat_stampanja;
    RETURN ok;   
  END proveri_dat_stampanja;
END;