文章目录
- 背景
- 介绍
- 语法
- 示例
背景
在信创适配中,从Oracle迁移过来的程序使用STATIC函数。为此LightDB从24.1版本开始,对该功能进行了支持。
介绍
LightDB 24.1版本中对象方法包括下列几种:
-
MEMBER方法:MEMBER方法可以被对象实例调用。通过调用MEMBER方法,可以访问对象实例的数据,通过SELF隐式参数可以访问对象的数据。
-
STATIC方法:STATIC方法可以被对象类型调用。没有隐式参数,不能通过隐式参数访问到对象的数据。典型调用方式是type_name.method()。
语法
CREATE [ OR REPLACE ] TYPE name as_is OBJECT ( [ object_type_element_list ] )where as_is is:AS | ISand object_type_element_list is:TableFuncElementList [ object_type_func_list ]and TableFuncElementList is:attribute_name data_type [ COLLATE collation ] [, ... ]and object_type_func_list is:{ type_function_spec | type_procedure_spec } [, ... ]and type_function_spec is:MEMBER FUNCTION func_name ( [ [ argmode ] [ argname ] argtype [ { DEFAULT | = } default_expr ] [, ...] ] ) RETURN rettypeSTATIC FUNCTION func_name ( [ [ argmode ] [ argname ] argtype [ { DEFAULT | = } default_expr ] [, ...] ] ) RETURN rettypeand type_procedure_spec is:MEMBER PROCEDURE func_name ( [ [ argmode ] [ argname ] argtype [ { DEFAULT | = } default_expr ] [, ...] ] )STATIC PROCEDURE func_name ( [ [ argmode ] [ argname ] argtype [ { DEFAULT | = } default_expr ] [, ...] ] )CREATE [ OR REPLACE ] TYPE BODY name as_is subprog_decl_in_type_list ENDwhere subprog_decl_in_type_list is:{ func_decl_in_type | proc_decl_in_type } [ ... ]and func_decl_in_type is:MEMBER FUNCTION func_name ( [ [ argmode ] [ argname ] argtype [ { DEFAULT | = } default_expr ] [, ...] ] ) RETURN rettype as_is pl_block ;STATIC FUNCTION func_name ( [ [ argmode ] [ argname ] argtype [ { DEFAULT | = } default_expr ] [, ...] ] ) RETURN rettype as_is pl_block ;and proc_decl_in_type is:MEMBER PROCEDURE func_name ( [ [ argmode ] [ argname ] argtype [ { DEFAULT | = } default_expr ] [, ...] ] ) as_is pl_block ;STATIC PROCEDURE func_name ( [ [ argmode ] [ argname ] argtype [ { DEFAULT | = } default_expr ] [, ...] ] ) as_is pl_block ;and as_is is:AS | ISand pl_block is:[ DECLARE decl_stmts ] BEGIN statement [ , ... ] END
示例
CREATE OR REPLACE TYPE tp_point as object (x int,y int,member procedure plus_x(v number),member procedure plus_y(v number),static procedure p_display(p tp_point),static function f_display(p tp_point) return varchar
);CREATE OR REPLACE TYPE BODY tp_point AS member procedure plus_x(v number) is BEGINself.x := self.x + v;end;member procedure plus_y(v number) is BEGINself.y := self.y + v;end;static procedure p_display(p tp_point) is BEGINdbms_output.put_line('x=' || p.x || ',' || 'y=' || p.y);end;static function f_display(p tp_point) return varchar is BEGINreturn 'x=' || p.x || ',' || 'y=' || p.y;end;
END;
/declarep tp_point := tp_point(1.0, 2.0);
BEGINtp_point.p_display(p);p.plus_x(1.0);p.plus_y(2.0);dbms_output.put_line(tp_point.f_display(p));
end;
/
x=1,y=2
x=2,y=4
DO