Mimer SQL Documentation TOC PREV NEXT INDEX

Mimer SQL Developer Site


Methods


Given a user-defined type it is possible to create methods for that type. A method is very similar to a function, it can only have in parameters and returns a value. What differs is the syntax for invoking the method and that most methods are used together with an instance of a user-defined type. There are three different types of methods - constructor, instance, and static methods.

Constructor methods

A constructor method is used to assign values to attributes when creating a new instance of a user-defined type.

A constructor method can only be defined for a structured user-defined type.

Constructor methods have an implicit parameter SELF that represents the actual value instance used when invoking the method.

Static methods

A static method does not have the connection with an instance of a user-defined type (like a constructor method has), but works almost the same as a function. The only difference is how they are invoked.

Instance methods

An instance method can only be used with an actual instance of a user-defined type.

Instance methods have an implicit parameter SELF that represents the actual value instance used when invoking the method.

Creating Methods

Creating a method is done in two steps, the first is the creation of a method specification and the second is the actual creation.

There can be multiple methods with the same name as long as they either differ by the number of parameters or the type of the parameters.

The method specification can either be given when creating the type or it can be added by using an ALTER TYPE statement.

Examples
 CREATE TYPE bool AS boolean 
     CAST(distinct AS source) WITH bool
     METHOD asChar() RETURNS varchar(5);
 CREATE METHOD asChar() FOR bool
     RETURN CASE WHEN self THEN 'TRUE' ELSE 'FALSE' END;
 
 CREATE TYPE ymd AS (y int, m int, d int)
     INSTANCE METHOD compare(y int, m int, d int) RETURNS bool,
     STATIC METHOD add(iv interval day(4)) RETURNS date READS SQL DATA;
 
 ALTER TYPE ymd ADD CONSTRUCTOR METHOD ymd(d date) RETURNS ymd;
 
 CREATE INSTANCE METHOD compare(y int, m int, d int) FOR ymd
     RETURN b((self.y, self.m, self.d) = (y,m,d));
 
 CREATE STATIC METHOD add(iv interval day(4)) FOR ymd
 BEGIN
     DECLARE d date;
     SELECT MAX(release_date) INTO d FROM coming;
     SET d = current_date + iv;
     RETURN d;
 END
 
 CREATE CONSTRUCTOR METHOD example(d date) FOR ymd
 BEGIN
     SET self.y = year(d);
     SET self.m = month(d);
     SET self.d = day(d);
     RETURN self;
 END
 

As can be seen, the type of method is specified in the method specification and the CREATE METHOD statement. (The method type can be omitted if an instance method is created.) It is possible to use all PSM statements when creating a method. The for clause with the type name is needed since it is possible to have methods with the same name and parameters for different user-defined types.

There are some specific rules for constructor methods, the method name must be the same as the name of the user-defined type and the return type must be the user-defined type.

A method specification can be dropped from a type by using a variant of the alter type statement. If there are any method defined using that method specification, that method will be dropped if cascade is specified.

Example
 ALTER TYPE ymd DROP CONSTRUCTOR METHOD ymd(date) CASCADE;
 ALTER TYPE ymd DROP STATIC METHOD add RESTRICT;
 

If neither RESTRICT nor CASCADE is specified, RESTRICT is default. Note that it is necessary to specify the data type of the parameters if there are multiple methods with the same name and method type for the user-defined type. The second example will only be successful if there is only one static method named add for the type ymd. To specify a method with no parameters an empty pair of parenthesis () can be used.

See Mimer SQL Reference Manual, CREATE METHOD for additional information.

Invoking Methods

Methods are invoked differently depending on the type of the method.

Invoking a constructor method

A constructor method is used with the NEW operator when creating a new instance of a user-defined type.



When using the NEW operator, the constructor function will be invoked first. This function assigns default values to all attributes. The constructor function returns the instance and this will be passed as an implicit parameter to the constructor method together with the explicit parameter. The constructor method modifies the attributes and returns the instance. It is possible to have a constructor method without parameters.

Example
 BEGIN
     DECLARE a ymd;
 
     SET a = NEW ymd(CURRENT_DATE);
 END
 

Invoking an instance method

An instance method is invoked by using dot notation on a expression that evaluates to a user-defined type.



Example
 BEGIN
     DECLARE a ymd;
     DECLARE b int;
 
     SET a = NEW ymd(DATE'2010-04-23');
 --
 -- invoke the implicitly created instance method m
 -- for retrieving the value of the attribute m
 --
     SET b = a.m()
 
     ...
 --
 -- as the instance method compare returns a user-defined type,
 -- the method asChar for this user-defined type can be invoked on that result
 --
     IF a.compare(2010,11,2).asChar() = 'TRUE' THEN
     ...
     END IF;
 END

Invoking a static method

A static method is invoked with a double-colon syntax.



Example
 SELECT ymd::add(c) FROM t;

Dropping Methods

Dropping a method will have effects on objects using it. The DROP statement may either have a restrict or cascade option. Restrict means that if there are any objects depending on the method the drop will not be done. If cascade is specified all such objects will be dropped.

Example
 DROP STATIC METHOD add FOR ymd CASCADE;
 

See Mimer SQL Reference Manual, DROP for details.


Mimer
Mimer Information Technology AB
Voice: +46 18 780 92 00
Fax: +46 18 780 92 40
info@mimer.se
Mimer SQL Documentation TOC PREV NEXT INDEX