You can instantiate a user-defined type to define a constructor as follows:
NEW type-name( argument-list)
For example, a query could contain the following to instantiate two ST_Point values:
SELECT NEW ST_Point(), NEW ST_Point(3,4)
SQL Anywhere matches argument-list against defined constructors using the normal overload resolution rules. An error is returned in the following situations:
If NEW is used with a type that is not a user-defined type
If the user-defined type is not instantiable (for example, ST_Geometry is not an instantiable type).
If there is no overload that matches the supplied argument types
User defined types can have instance methods defined. Instance methods are invoked on a value of the type as follows:
value-expression.method-name( argument-list )
For example, the following selects the X coordinate of the myTable.centerpoint column:
SELECT centerpoint.ST_X() FROM myTable;
Note that if there was a user ID called centerpoint, the database server would find the construct
centerpoint.ST_X() to be ambiguous. This is because the statement could mean "call the user-defined function ST_X owned by user centerpoint"--the incorrect
intention of the statement--or it could mean "call the ST_X method on the myTable.centerpoint column" (the correct meaning).
The database server resolves such ambiguity by first performing a case-insensitive search for a user named centerpoint. If
a user named centerpoint is found, the database server proceeds as though a user-defined function called ST_X and owned by
user centerpoint is being called. If no user called centerpoint is found, the database server treats the construct as a method
call and calls the ST_X method on the myTable.centerpoint column.
An instance method invocation gives an error in the following cases:
If the declared type of the value-expression is not a user-defined type
If the named method is not defined in the declared type of value-expression or one of its supertypes
If argument-list does not match one of the defined overloads for the named method.
In addition to instance methods, the ANSI/SQL standard allows user-defined types to have static methods associated with them. These are invoked using the following syntax:
type-name::method-name( argument-list )
For example, the following instantiates an ST_Point by parsing text:
SELECT ST_Geometry::ST_GeomFromText('POINT( 5 6 )')
A static method invocation gives an error in the following cases:
If the declared type of value-expression is not a user-defined type
If the named method is not defined in the declared type of value expression or one of its supertypes
If argument-list does not match one of the defined overloads for the named method
As an extension to ANSI/SQL, SQL Anywhere supports static methods that implement user-defined aggregates. For example:
SELECT ST_Geometry::ST_AsSVGAggr(T.geo) FROM table T
All of the overloads for a static method must be aggregate or none of them may be aggregate.
A static aggregate method invocation gives an error in the following cases:
If a static method invocation would give an error
If a built-in aggregate function would give an error
If a WINDOW clause is specified
The ANSI/SQL standard defines type predicates that allow a statement to examine the dynamic type of a value. The syntax is as follows:
value IS [ NOT ] OF ( [ ONLY ] type-name,...)
If value is NULL, the predicate returns UNKNOWN. Otherwise, the dynamic type of value is compared to each of the elements in the type-name list. If ONLY is specified, there is a match if the dynamic type is exactly the specified type. Otherwise, there is a match if the dynamic type is the specified type or any derived type (sub-type).
If the dynamic type of value matches one of the elements in the list, TRUE is returned, otherwise FALSE.
For example, the following returns T:
SELECT IF DT.x IS OF ( ST_Point ) THEN 'T' ENDIF FROM ( SELECT ST_Geometry::ST_GeomFromText('POINT(5 6)') x ) DT
The ANSI/SQL standard defines a sub-type treatment expression that allows a cast from a base type to a sub-type (derived type). The syntax is as follows:
TREAT( value-expression AS target-subtype )
The following example casts ST_Geometry to sub-type ST_Point:
SELECT TREAT( DT.x AS ST_Point ) FROM ( SELECT ST_Geometry::ST_GeomFromText('POINT(5 6)') x ) DT
If no error condition is raised, the result is the value-expression with declared type of target-subtype.
The sub-type treatment expression gives an error in the following cases:
If value-expression is not a user-defined type
If target-subtype is not a sub-type of the declared type of value-expression
If the dynamic type of value-expression is not a sub-type of target-subtype
Discuss this page in DocCommentXchange.
|Copyright © 2010, iAnywhere Solutions, Inc. - SQL Anywhere 12.0.0|