达梦class用法

说实话我对class也不熟,之前从来没用过,这次有客户问我我才去研究了一下,客户的需求是,把oracle中定义的class迁移到达梦,我看到客户提供的demo,找研发咨询了下,研发说达梦class不支持import之类的写法,目前无法把oracle中定义的class迁移到达梦。

我想,这种复杂的class不支持,简单的总可以吧,所以就自己去研究了下class,结合手册+百度学到的。

先抄袭个demo:

create class mycls
as
    type rec_type is record (c1 int, c2 int); --类型声明
    id int; --成员变量
    r rec_type; --成员变量
        --成员函数
    function f1(a int, b int) return rec_type;
    --用户自定义构造函数
    function mycls(id int , r_c1 int, r_c2 int) return mycls;
end;
create or replace class body mycls
as
    function f1(a int, b int) return rec_type
    as
    begin
        r.c1 = a;
        r.c2 = b;
        return r;
    end;
    
    function mycls(id int, r_c1 int, r_c2 int) return mycls
    as
    begin
        this.id = id; --可以使用 this.来访问自身的成员
        r.c1 = r_c1; --this 也可以省略
        r.c2 = r_c2;
        return this; --使用 return this 返回本对象
    end;

end;

用法如下:

declare
    type ex_rec_t is record (a int, b int); --使用一个同结构的类型代替类定义的类型
    rec ex_rec_t;
    o1 mycls;
    o2 mycls;
begin
    o1 = new mycls(1,2,3);
    o2 = o1; --对象引用
    rec = o2.r; --变量对象的成员变量访问
    print rec.a; 
    print rec.b;
    rec = o1.f1(4,5); --成员函数调用
    print rec.a; 
    print rec.b;
    print o1.id; --成员变量访问
end;

注意:我是在达梦数据库中测试的,因为相对而言,还是对达梦管理工具比较熟。

类也可以分开定义,先定义类头:

--类头创建
create class mycls
as
    type rec_type is record (c1 int, c2 int); --类型声明
    id int; --成员变量
    r rec_type; --成员变量
    --成员函数
    function f1(a int, b int) return rec_type;
    --用户自定义构造函数
    function mycls(id int , r_c1 int, r_c2 int) return mycls;
end;
/

再定义类体:

--类体创建
create or replace class body mycls as function f1(a int, b int) return rec_type as begin r.c1 = a; r.c2 = b; return r; end; function mycls(id int, r_c1 int, r_c2 int) return mycls as begin this.id = id; --可以使用 this.来访问自身的成员 r.c1 = r_c1; --this 也可以省略 r.c2 = r_c2; return this; --使用 return this 返回本对象 end; end;

 使用类类型同普通的数据类型一样,可以作为表中列的数据类型,DMSQL 程序语句块中变量的数据类型或过程及函数参数的数据类型。

create table testclass (id int,name mycls);

 更多class用法可以参考《DM8_SQL程序设计》,安装完达梦数据库自带的手册,章节为:基于C、JAVA语法的DMSQL程序。

 

 

 

更多资讯请上达梦技术社区了解: https://eco.dameng.com

posted @ 2021-09-23 14:23  xiaowu222  阅读(196)  评论(0编辑  收藏  举报