Create the syntax of an object

Source: Internet
Author: User
Tags function definition

Description: At present, only the most commonly used definition syntax, and then slowly updated. Defines an object type that contains two parts, defines a type, and defines a type body. The CREATE Type statement defines the name, property, and method of the type. The Create TYPE body statement implementation method.

-------------------Definition Type---------------------------

CREATE [OR REPLACE] TYPE [schema.] Type_name

[Caller rights definition]

Type hierarchy

[

(Property declaration | element declaration [,,,])

]

[[NOT] FINAL] (note: for type)

[[NOT] instantiable] (note: for type)

;

The sections are described as follows:

OR REPLACE

If the type exists, change its definition, and the benefit of this clause is that you do not need to delete the existing type before you create it. Of course, using this clause can be an error due to dependencies between objects.

[Schema.] Type_name

The name of the object type. If schema is omitted, the type is created within the current schema.

Invoke a permission definition (this definition applies to its corresponding type body at the same time)

Calling a permission definition specifies three things:

(1) The execution permission of the method.

(2) The structure in which it resides.

(3) Oracle uses this definition to parse the external names referenced by queries, DML operations, and dynamic SQL statements in methods.

There are two kinds of invocation permission definitions:

Authid Current_User (caller rights type)

The execution permission of a method is determined by the caller. Also, the external name is resolved within the caller's schema.

Authid definer (definition of rights type)

The execution permission of a method is determined by the definition. Also, the external name is resolved within the schema of the defined person.

To use a restriction condition:

(1) This clause can only be used when defining an object type and cannot be used in nested tables and arrays.

(2) This clause can be used on subclasses, but the invocation permission definition of a subclass must be the same as the invocation permission definition on the superclass.

(3) If the superclass uses a defined-rights type, the subclass and superclass must be within the same schema.

Type hierarchy

Is|as OBJECT

Creates a top-level (root) object type.

UNDER [schema.] Supertype_name

Inherits a superclass.

[NOT] FINAL (note: for type)

Specifies whether subclasses can be created.

[NOT] Instantiable (note: for type)

Specifies whether it can be instantiated (the default is instantiation). You cannot define any constructors when it is not instantiable. If the type contains a virtual method, or does not contain any attributes (inherited or defined), you must use the not instantiable.

Property declaration

Attr_name datatype

-----------------The following section is the element declaration------------------------

The syntax is as follows:

[Inheritance sentence] {subroutine definition | constructor Definition | comparison method definition}

Inheritance sentence

An inherited sentence is part of an element declaration that describes the relationship between a superclass and a subclass. The syntax is as follows:

{[NOT] {overriding | FINAL | Instantiable}},,,

[NOT] overriding

This clause is valid only for member methods (the default is not overriding). Specifies the method that overrides the superclass, and you must use overriding if you want to redefine the method of the parent class. Note that this clause is not valid for static methods and Sqlj object types.

[NOT] FINAL

This clause indicates whether the method can be overridden by a quilt class. The default is not FINAL, which can be overwritten.

[NOT] Instantiable

This clause indicates whether the method is a virtual method and does not provide a method implementation. The default is Instantiable, which provides a method implementation. The class containing the virtual method is not instantiated.

Constructor definition

Use this clause to define a custom constructor. In addition to abstract classes, each class contains an implicit constructor whose parameter order is consistent with the property declaration in the type definition. You can define multiple constructors, as long as the parameters of those constructors are not the same in number, order, or data type. The syntax is as follows:

[FINAL] [Instantiable] Constructor FUNCTION datatype

[[SELF in Out datatype,] {parameter datatype},,,)]

return SELF as result [is | As calling convention]

Description

(1) Custom constructors are always final and instantiable, so you can omit final and instantiable.

(2) The datatype behind the function does not yet know what is useful, and the general type definition is the type_name for the type being defined.

(3) The user-defined constructor is always self in out, so it can be omitted.

(4) Return SELF as result indicates that the type of the value returned by the constructor is the same as the run-time type of the implicit parameter SELF.

Comparison method definition

Only a map method or an order method can be defined in the top-level class, and subclasses may override the map method of the superclass, but not the order method.

If a class does not define a comparison method, only equality comparisons can be made, and objects cannot be sorted at this time, only if all corresponding properties of two objects of the same type definition are equal, the two objects are considered equivalent.

The map method is typically used for sorting or hashing connections for a large number of object instances. The map method returns a system-predefined scalar value (DATE,NUMBER,VARCHAR2, or ANSI SQL type such as character or real). When a query that involves sorting (such as order by,distinct, or a UNION clause) refers to the type name, Oracle can use the map method for parallel queries.

The order method is less efficient than the map method, it directly compares two objects and returns an integral type, greater than 0, equal to 0, and less than 0 representing greater than, equal to, and less than. The order method can also be used for sorting, but not for hash joins. If an order method is defined, Oracle automatically calls this method when comparing. The order method is not as efficient as the map, but it is used primarily to represent complex comparisons.

All two methods can be invoked by Oracle, so you typically only define one.

Map method Definition:

MAP member FUNCTION func_name return {DATE | number | VARCHAR2 | CHARACTER | Real}

Order method Definition:

Order member FUNCTION func_name (parameter type_name) return INTEGER (Note: type_name must be of the same type)

Child program Definition

A subroutine definition contains a procedure definition and a function definition. Its general syntax is as follows:

Member | STATIC {procedure definition | function definition}

Member

Represents a defined member method.

STATIC

Represents the definition of a static method.

Procedure definition Syntax:

PROCEDURE Proc_name [({parameter datatype},,,)] [is | As {calling convention}]

function definition Syntax:

FUNCTION Func_name [({parameter datatype},,,)] return datatype [is | As {calling convention}]

-------------------Definition type Body---------------------------

Any method that does not specify the calling convention when creating the type, in the type body, defines the implementation body.

CREATE [OR REPLACE] TYPE body [schema.] Type_name Is|as

{Sub-Program implementation | Constructor Implementation | Comparison method implementation},,,

End;

The description is as follows:

(1) Define the type body don't forget to use end;

(2) The implementation body is either a pl/sql block or a calling convention.

(3) For a constructor, be sure to return it with returns.

----------------A complete example of-------------------------------

--A complete type definition

CREATE OR REPLACE TYPE myrectangle as OBJECT (
--Attribute definition
Length integer,
Width integer,
--Constructor definition
Constructor FUNCTION MyRectangle (var_length number) return to SELF as result,
--Process definition
Member PROCEDURE INC (var_length integer,var_width integer),
Member PROCEDURE Mini,
--Function definition
Member FUNCTION getlength return integer,
Member FUNCTION getwidth return integer,
Member FUNCTION Scale (var_length integer,var_width integer) return integer,
--Comparison method definition
MAP member FUNCTION Compare return integer
);
--/
CREATE OR REPLACE TYPE body MyRectangle is
--Constructor definition
Constructor FUNCTION MyRectangle (var_length number) return SELF as
BEGIN
Length: = Var_length;
Width: = 2;
return;
End;
--Process definition
Member PROCEDURE Inc. (var_length integer,var_width Integer) is
BEGIN
Length: = length + var_length;
Width: = width + var_width;
End;
Member PROCEDURE the Mini is
BEGIN
Length: = length-1;
Width: = width-1;
End;
--Function definition
Member FUNCTION getlength return integer is
BEGIN
return length;
End;
Member FUNCTION getwidth return integer is
BEGIN
return width;
End;
Member FUNCTION Scale (var_length integer,var_width integer) return to Integer is
BEGIN
Return (length + var_length) * (width + var_width);
End;

--Comparison method definition
MAP member FUNCTION Compare return integer is
BEGIN
return length * width;
End;
End;

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.