Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

DM class data type like how to use

2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >

Share

Shulou(Shulou.com)05/31 Report--

This article mainly explains "how to use DM data types". The content of the explanation is simple and clear, and it is easy to learn and understand. Please follow the editor's train of thought to study and learn how to use DM data types.

Class type

DM7 implements object-oriented programming support in DMSQL programs through class types. Class encapsulates structured data and the processes or functions that operate on it. Allows users to model real-world objects without having to abstract them into relational data.

The class types of DM7 are divided into normal class types and JAVA CLASS types. The examples in the DM documentation use normal class types except for specifically declaring that they use the JAVA CLASS type.

Normal CLASS type

The definition of DM7 class is divided into two parts: the class header and the class body. The class header completes the declaration of the class; the class body completes the implementation of the class. The following can be included in the class

1. Type definition

Data types such as cursors, exceptions, record types, array types, and memory index tables can be defined in classes, and these data types can be used in class declarations and implementations; cursors and exceptions cannot be declared in class declarations, but can be defined and used in implementation.

two。 Attribute

A member variable in a class, the data type can be a standard data type, or a special data type customized in the class.

3. Member method

A function or procedure in a class is declared in the head of the class; in fact, it is now done in the body of the class

The member method and the later constructor contain an implicit parameter, that is, its own object, which can be accessed through this or self in the method implementation, and self is equivalent to this. If there is no duplicate name problem, you can also use the properties and methods of the object directly. This and self can only be called in package or object scripts.

4. Constructor function

Constructor is a special function defined and implemented within a class. This kind of function is used to instantiate the object of the class. The constructor satisfies the following conditions:

1) the function name is the same as the class name

2) the return value type of the function is its own class.

The constructor has the following constraints:

1) the system provides two default constructors for each class, which are the constructor of 0 parameter and the constructor of all parameters.

2) the number of parameters of the 0 parameter constructor is 0, and the initialization values of all properties in the object of the instance are NULL

3) the number and type of parameters of the all-parameter constructor are the same as those of the attributes in the class, and the values of the parameters are read and assigned to the attributes according to the order of the attributes.

4) users can customize constructors. A class can have multiple constructors, but the number of parameters of each constructor must be different.

5) if the user defines a constructor with 0 parameters or the same number of parameters as the number of attributes, the corresponding default constructor will be overridden.

Following from the declaration of the class, the implementation of the class, the deletion of the class, the deletion of the body of the class and the use of the class to introduce the implementation process of the class type in detail.

The declaration of the class is done in the class header. The class header definition is done through the CREATE CLASS statement, and its syntax is as follows:

Grammatical format

CREATE [OR REPLACE] CLASS [

< 模式名>

.]

< 类名>

[WITH ENCRYPTION] [UNDER [

< 模式名>

.]

< 父类名>

] [[NOT] FINAL] [[NOT]

INSTANTIABLE] [AUTHID DEFINER | AUTHID CURRENT_USER] AS | IS

< 类内声明列表>

END [class name]

< 类内声明列表>

:: =

< 类内声明>

; {

< 类内声明>

;}

< 类内声明>

:: =

< 变量定义>

| |

< 过程定义>

| |

< 函数定义>

| |

< 类型声名>

< 变量定义>

:: =

< 变量名列表>

< 数据类型>

[default value definition]

< 过程定义>

: = [

< 方法继承属性>

] [STATIC | MEMBER] PROCEDURE

< 过程名>

< 参数列表>

< 函数定义>

: = [

< 方法继承属性>

] [MAP] [STATIC | MEMBER] FUNCTION

< 函数名>

< 参数列表>

RETURN

< 返回值数据类型>

[DETERMINISTIC]

[PIPELINED]

< 方法继承属性>

:: =

< 重载属性>

| | |

< 重载属性>

< 重载属性>

:: = [NOT] OVERRDING

:: = FINAL | NOT FINAL | INSTANTIABLE | NOT INSTANTIABLE

< 类型声名>

:: = TYPE

< 类型名称>

IS

< 数据类型>

instructions

1. The elements in the class can appear in any order, and the objects must be declared before they are referenced.

2. The declarations of procedures and functions are forward declarations, and no implementation code is included in the class declaration.

3. Support object static method declaration and invocation. You can add a static reserved word before the PROCEDURE/FUNCTION keyword to indicate that the method is static. Static methods can only be called with the object name prefixed, not in the object instance.

4. Support object member method declaration and invocation. You can add MEMBER before the PROCEDURE/FUNCTION keyword to indicate that the method is a member method. MEMBER and STATIC cannot be used at the same time, and non-constructor methods of non-STATIC types default to member methods. MAP means to map an instance of an object type to a scalar value and can only be used for FUNCTION of a member type

5. With regard to class inheritance, there are the following usage restrictions:

1) the class definition defaults to FINAL, which means that the object type cannot be inherited. You must specify the NOT FINAL option when defining the parent class.

2) UNDER option must be specified when defining subclasses

3) NOT INSTANTIABLE object cannot be FINAL

4) NOT INSTANTIABLE objects cannot be instantiated, but can be assigned with their subclasses

5) when an object is instantiated, the member variables of both the parent class and the subclass must be assigned, one by one from the parent class to the subclass.

6) cyclic inheritance of objects is not supported

7) multiple inheritance of objects is not supported, that is, a class has multiple parent classes

6) cyclic inheritance of objects is not supported

7) multiple inheritance of objects is not supported, that is, a class has multiple parent classes

8) parent and subclasses are not supported to contain variables with the same name

9) the parent class and the subclass can have the same name and parameter, and the subclass must specify OVERRIDING

10) the default method is that NOT OVERRIDING,OVERRIDING cannot be used with static

11) parent and subclasses support methods with different parameters of the same name (different number of parameters, same number of parameters but different types)

12) when the same name and the same number of parameters but different types, select the method to be used according to the parameter type

13) the method defaults to INSTANTIABLE. If declared as NOT INSTANTIABLE, it cannot be used with FINAL or STATIC.

14) if the parent class has more than one NOT INSTANTIABLE method, the subclass can only be partially overridden, but the subclass must be defined as NOT FINAL NOT INSTANTIABLE

15) the NOT INSTANTIABLE method cannot have a body

16) the method defaults to NOT FINAL. If declared as FINAL, it cannot be overridden by subclasses.

17) subclasses can be assigned to parent classes

18) if the instance corresponding to the parent class is a subclass or a child of the subclass, the parent class can be assigned to the subclass

19) the NOT INSTANTIABLE parent class can be assigned with the Instantiable subclass

20) after the subclass instance is assigned to the parent class, it is called using the parent class method instead of the subclass method

21) support the use of as statements to convert to parent classes.

Required permissions

1. The user using this statement must be DBA or a user with CREATE CLASS database permissions

2. You can use the keyword AUTHID DEFINER | AUTHID CURRENT_USER to specify the caller permission of the class. If DEFINER, the class definer permission is used. If CURRENT_USER, the current user permission is used. By default, the class definer permission is used.

The implementation of the class is accomplished through the class body. The definition of the class body is done through the CREATE CLASS BODY statement, and its syntax is:

Grammatical format

CREATE [OR REPLACE] CLASS BODY [

< 模式名>

.]

< 类名>

[WITH ENCRYPTION] AS | IS

< 类体部分>

END [class name]

< 类体部分>

:: =

< 过程/函数列表>

[

< 初始化代码>

]

< 过程/函数列表>

:: =

< 过程实现|函数实现>

{

< 过程实现|函数实现>

}

< 过程实现>

: = [

< 方法继承属性>

] [STATIC | MEMBER] PROCEDURE

< 过程名>

< 参数列表>

AS | IS BEGIN

< 实现体>

END [procedure name]

< 函数实现>

: = [

< 方法继承属性>

] [MAP] [STATIC | MEMBER] FUNCTION

< 函数名>

< 参数列表>

RETURN

< 返回值数据类型>

[DETERMINISTIC] [PIPELINED] AS | IS BEGIN

< 实现体>

END [function name]

< 方法继承属性>

:: =

< 重载属性>

| | |

< 重载属性>

< 重载属性>

:: = [NOT] OVERRDING

:: = FINAL | NOT FINAL | INSTANTIABLE | NOT INSTANTIABLE

< 初始化代码>

: = [

< 说明部分>

] BEGIN

< 执行部分>

[

< 异常处理部分>

]]

< 说明部分>

: = [DECLARE]

< 说明定义>

{

< 说明定义>

}

< 说明定义>

:: =

< 变量说明>

| |

< 异常变量说明>

| |

< 游标定义>

| |

< 子过程定义>

| |

< 子函数定义>

< 变量说明>

:: =

< 变量名>

{

< 变量名>

}

< 变量类型>

[DEFAULT | ASSIGN |: =

< 表达式>

]

< 变量类型>

:: = |

< [模式名.]表名.列名%TYPE>

| |

< [模式名.]表名%ROWTYPE>

| |

< 记录类型>

< 记录类型>

:: = RECORD (

< 变量名>

{

< 变量名>

})

< 异常变量说明>

:: =

< 异常变量名>

EXCEPTION [FOR

< 错误号>

]

< 异常处理语句>

:: = WHEN

< 异常名>

THEN

< SQL过程语句序列>

instructions

1. The objects defined in the class declaration are visible to the class body and can be referenced directly without declaration. These objects include variables, cursors, exception definitions, and type definitions

2. The procedure and function definition in the class body must be exactly the same as the declaration in the class declaration. Including the name of the procedure, the parameter name of the parameter definition list, and the data type definition

3. There can be member methods with duplicate names in the class, requiring different parameter definition lists. The system will overload (OVERLOAD) according to the call of the user.

4. When declaring a class and implementing a class, the specified logic for the deterministic function is the same as the function in the package. Currently, deterministic functions of classes are not supported for use in functional indexes.

Required permissions

The user using this statement must be the owner of DBA or this type of object and have CREATE CLASS database permissions.

The complete class header and class body are created as follows:

-creation of class headers

SQL > create or replace class mycls2 as3 type rec_type is record (C1 int, c2 int);-- Type declaration 4 id int;-member variable 5 r rec_type;-member variable 6 function F1 (an int, b int) return rec_type;-member function 7 function mycls (id int, r_c1 int, r_c2 int) return mycls;8-user-defined constructor 9 end;10 / executed successfullyused time: 14.032 (ms). Execute id is 106.

-creation of class body

SQL > create or replace class body mycls2 as3 function F1 (an int, b int) return rec_type4 as5 begin6 r.c1 = an end;10 function mycls 7 r.c2 = b end;10 function mycls 8 return r TX 9 end;10 function mycls (id int, r_c1 int, r_c2 int) return mycls11 as12 begin13 this.id = id;-- this can be used. To access its own member 14 r.c1 = rroomc1;-- this can also omit 15 r.c2 = rhombc2 / 16 return this;-- use return this to return this object 17 end;18 end;19 / executed successfullyused time: 61.783 (ms). Execute id is 107.

Recompiling class

Recompile the class, or if the recompilation fails, set the class to the disabled state.

The reprogramming function is mainly used to verify the correctness of the class.

Grammatical format

ALTER CLASS [

< 模式名>

.]

< 类名>

COMPILE [DEBUG]

Parameters.

1.

< 模式名>

Indicates the pattern to which the recompiled class belongs

two。

< 类名>

Indicates the name of the recompiled class

3. [DEBUG] can be ignored.

Required permissions

The user performing this operation must be the creator of the class or have DBA permissions.

Give examples to illustrate

For example, recompiling a class

SQL > ALTER CLASS mycls COMPILE;executed successfullyused time: 8.867 (ms). Execute id is 108.

Delete CLA

There are two ways to delete a class: one is the deletion of the class header, and the deletion of the class header will delete the class body together; the other is the deletion of the class body, which can only delete the class body, but the class header is still

Exist.

Delete class header

The deletion of the class is done through DROP CLASS, that is, the deletion of the class header. When the class header is deleted, the class body is also deleted.

Grammatical format

DROP CLASS [

< 模式名>

.]

< 类名>

[RESTRICT | CASCADE]; instructions for use

1. If the deleted class does not belong to the current schema, you must indicate the schema name in the statement

2. If the declaration of a class is deleted, the corresponding class body is automatically deleted.

Required permissions

The user performing the operation must be the owner of the class or have DBA permissions.

Delete class body

Removes the implementation principal object of a class from the database.

Grammatical format

DROP CLASS BODY [

< 模式名>

.]

< 类名>

[RESTRICT | CASCADE]; instructions for use

If the deleted class does not belong to the current schema, you must indicate the schema name in the statement.

Authority

The user performing the operation must be the owner of the class or have DBA permissions.

The use of classes

Like ordinary data types, class types can be used as data types of columns in tables, variables in DMSQL program statement blocks, or data types of procedure and function parameters.

Specific rules of use

1. Classes that are attributes of column types or other class member variables in the table cannot be modified. When deleting, you need to specify the data type defined in the CASCADE cascade delete class, and its name is only valid in the declaration and implementation of the class. If the parameter or return value of a function in a class is a data type in the class, or if you copy the member variables in the class, you need to define a type with the same structure in the DMSQL program.

According to the way classes are used, objects can be divided into variable objects and column objects. A variable object refers to a variable of class type declared in a block of DMSQL program statements; a column object refers to a column of class type in a table. A variable object can modify the value of its properties while a column object cannot.

2. Instantiation of variable objects

Class instantiation is accomplished by NEW expression calling the constructor.

3. Reference to variable object

The assignment between class type variables through'='is a reference to the object and does not copy a new object.

4. Variable object property access

You can access properties in the following ways.

< 对象名>

.

< 属性名>

5. Variable object member method call

The call to the member method is called in the following ways:

< 对象名>

.

< 成员方法名>

(

< 参数>

{

< 参数>

})

If the value of the property within the object is modified within the function, the change takes effect.

6. Insertion of column objects

The creation of the column object is accomplished by inserting data into the table through the INSERT statement. The values in the insert statement are variable objects, and the data stored in the table after insertion is the column object.

7. Replication of column objects

The object stored in the table does not allow the modification of the member variables in the object. The column-to-variable assignment through the into query or'='is the object assignment, resulting in a copy of the column object data, and the changes made on this copy do not affect the value of the column object in the table.

8. Property access of column object

Access the properties in the following ways:

< 列名>

.

< 属性名>

9. Method calls to column objects

< 列名>

.

< 成员方法名>

(

< 参数>

{

< 参数>

})

Changes to properties within a type during a method call to a column object are made on a copy of the column object and do not affect the value of the column object.

Application example

1. An application example of variable object

SQL > declare2 type ex_rec_t is record (an int, b int);-- replace the class-defined type 3 rec ex_rec_t;4 o1 mycls;5 o2 mycls;6 begin7 o1 with a type with the same structure; 8 o2 = o1; 8 O2 = o1;-- object reference 9 rec = o2.r;-- member variable access to the variable object 10 print rec.a; print rec.b 11 rec = o1.f1 (415);-- member function call 12 print rec.a; print rec.b;13 print o1.id;-- member variable access 14 end;15 / 23451DMSQL executed successfullyused time: 3.129 (ms). Execute id is 109.

two。 Application example of column object

Creation of the table.

SQL > create table tt1 (C1 int, c2 mycls); executed successfullyused time: 28.302 (ms). Execute id is 112.

Creation of column objects-insert data.

SQL > insert into tt1 values (1, mycls); affect rows 1used time: 22.639 (ms). Execute id is 113.SQL > commit;executed successfullyused time: 17.285 (ms). Execute id is 114.

Replication and access of column objects.

SQL > declare2 o mycls;3 id int;4 begin5 select top 1 c2 into o from tt1;-replication of column objects 6 select top 1 c2.id into id from tt1;-access to column object members 7 end;8 / DMSQL executed successfullyused time: 33.518 (ms). Execute id is 115.

3. An application example of class inheritance

SQL > CREATE OR REPLACE CLASS cls01 NOT FINAL IS2 name VARCHAR2 (10); 3 MEMBER FUNCTION get_info RETURN VARCHAR2;4 END;5 / executed successfullyused time: 22.220 (ms). Execute id is 116.SQL > CREATE OR REPLACE CLASS cls02 UNDER cls01 IS2 ID INT;3 OVERRIDING MEMBER FUNCTION get_info RETURN VARCHAR2;4 END;5 / executed successfullyused time: 14.072 (ms). Execute id is 117.

JAVA CLASS Typ

The definition of the JAVA class is similar to the JAVA language syntax and can be defined in the class.

The following can be included in the JAVA class:

1. Type definition

Cursors, exceptions can be defined in classes, and data type variables such as record types, array types, struct types, and memory index tables can be declared.

two。 Attribute

A member variable in a class, the data type can be a standard data type, or a special data type that is customized outside the class.

3. Member method

The member method in the JAVA class and the later constructor contain an implicit parameter, that is, its own object, which can be accessed through this or self in the method implementation, and self is equivalent to this. If there is no duplicate name problem, you can also use the properties and methods of the object directly.

4. Constructor function

Constructor is a special function defined and implemented within a class. This kind of function is used to instantiate the object of the class. The constructor satisfies the following conditions:

1) the function name is the same as the class name

2) the function does not return a value type.

The constructor has the following constraints:

1) the system provides two default constructors for each class, which are the constructor of 0 parameter and the constructor of all parameters.

2) the number of parameters of the 0 parameter constructor is 0, and the initialization values of all properties in the object of the instance are NULL

3) the number and type of parameters of the all-parameter constructor are the same as those of the attributes in the class, and the values of the parameters are read and assigned to the attributes according to the order of the attributes.

4) users can customize constructors. A class can have multiple constructors, but the number of parameters of each constructor must be different.

5) if the user defines a constructor with 0 parameters or the same number of parameters as the number of attributes, the corresponding default constructor will be overridden.

Define the JAVA class

The definition is done through the CREATE JAVA CLASS statement, and its syntax is:

Grammatical format

CREATE [OR REPLACE] JAVA [PUBLIC] [ABSTRACT] [FINAL] CLASS

< 类名>

[EXTENDS [

< 模式名>

.]

< 父类名>

] {

< 类内定义部分>

}

< 类内定义部分>

:: =

< 类内定义列表>

< 类内定义列表>

:: =

< 类内定义>

; {

< 类内定义>

;}

< 类内定义>

:: = [PUBLIC | PRIVATE]

< 变量定义>

| |

< 方法定义>

< 变量定义>

:: =

< 变量属性>

< 数据类型>

< 变量名列表>

[default value definition]

< 变量属性>

: = [STATIC]

< 方法定义>

:: = [PUBLIC | PRIVATE] [

< 方法继承属性>

] [STATIC]

< 返回类型>

< 函数名>

< 参数列表>

{

< 实现体>

}

< 方法继承属性>

:: =

< 重载属性>

| | |

:: = ABSTRACT

:: = FINAL

< 重载属性>

:: = OVERRIDE

instructions

1. The elements in the class can appear in any order, and the objects must be declared before they are referenced.

2. Support object static method declaration and invocation. You can indicate that the method is static by adding a static reserved word before the method. Static methods can only be called with the object name prefixed, not in the object instance.

3. Object member method declarations and calls are supported. Non-constructor methods of non-STATIC types default to member methods. When a member method is called, it needs to be instantiated first, and the instantiation parameter value defaults to null.

4. Variable definitions also include cursors and exception definitions.

5. If the method property is PUBLIC, it can be accessed when the class is accessed. If it is the PRIVATE property, the method cannot be accessed when accessing the class.

6. With regard to JAVA class inheritance, there are the following usage restrictions:

1) the JAVA CLASS definition is inheritable by default, and FINAL indicates that the class cannot be inherited.

2) EXTENDS option must be specified when defining subclasses

3) ABSTRACT object cannot be FINAL

4) ABSTRACT objects cannot be instantiated, but can be assigned with their subclasses

5) when the subclass object is instantiated, the member variables of both the parent class and the subclass must be assigned, one by one from the parent class to the subclass.

6) cyclic inheritance of objects is not supported

7) multiple inheritance of objects is not supported, that is, a class can only have one parent class

8) parent and subclasses are not supported to contain variables with the same name

9) the parent class and the subclass can have the same name and parameter, and the subclass must specify OVERRIDE

10) the default method is that NOT OVERRIDING,OVERRIDING cannot be used with static

11) parent and subclasses support methods with different parameters of the same name (different number of parameters, same number of parameters but different types)

12) when the same name and the same number of parameters but different types, select the method to be used according to the parameter type

13) if the method is declared as ABSTRACT, it cannot be used with FINAL or STATIC

14) if the parent class has more than one ABSTRACT method, the subclass can only be partially overridden, but the subclass must be defined as ABSTRACT

15) the ABSTRACT method cannot have a body

16) the method is inheritable by default, and if declared as FINAL, it cannot be overridden by subclasses

17) subclasses can be assigned to parent classes

18) if the instance corresponding to the parent class is a subclass or a child of the subclass, the parent class can be assigned to the subclass

19) you can use ABSTRACT subclass to assign values to non-ABSTRACT parent classes

20) after the subclass instance is assigned to the parent class, it is called using the parent class method instead of the subclass method

21) support using super no-parameter method to convert to parent class reference

22) support using this () to call this class constructor, and super () to call parent class constructor

23) the subclass must have new members or methods and cannot be completely empty.

Recompile the JAVA class

Recompile the JAVA class, or set the JAVA class to the disabled state if the recompilation fails.

The reprogramming function is mainly used to verify the correctness of the JAVA class.

Grammatical format

ALTER JAVA CLASS [

< 模式名>

.] COMPILE [DEBUG]

Parameters.

1.

< 模式名>

Indicates the schema to which the recompiled JAVA class belongs

2. Indicate the name of the recompiled JAVA class

3. [DEBUG] can be ignored.

Required permissions

The user performing this operation must be the creator of the JAVA class or have DBA permissions.

12.2.3 deleting the JAVA class

The deletion of the JAVA class is done through DROP CLASS.

Grammatical format

DROP CLASS

< 类名>

[RESTRICT | CASCADE]

The use of classes

Here is a simple application example. How to use JAVA CLASS on column objects.

1. Create a JAVA CLASS.

SQL > create or replace java class jcls2 {3 int an int 4 public static int testAdd2 (int a, int b) 5 {/ / static STATIC method 6 return a + b int 7} 8 public int testAdd3 (int a, int b, int c) 9 {/ / the member method 10 return a + b + c × 11} 12}; 13 / executed successfullyused time: 16.964 (ms). Execute id is 123.

two。 Use JAVA CLASS in column objects.

SQL > create table tt2 (C1 int, c2 jcls); executed successfullyused time: 9.261 (ms). Execute id is 124.SQL > insert into tt2 values (jcls.testadd2 (1), jcls (1)); / / static method call 2 / affect rows 1used time: 1.255 (ms). Execute id is 125.SQL > insert into tt2 values (jcls (). Testadd3 (1 affect rows 1used time 2 (3), jcls (2)); / / member method calls must be instantiated 2 / affect rows 1used time: 1.023 (ms). Execute id is 126. Thank you for your reading, the above is the content of "how to use DM data types". After the study of this article, I believe you have a deeper understanding of how to use DM data types, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Database

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report