对象类型
优点:
1) 更容易与Java, C++编写的对象应用程序交互
2) 获取便捷。一次对象类型请求就可以从多个关系表中获取信息,通过一次网络往复即可返回
语法:

Oracle 中的OOP概念CREATE [OR REPLACE] TYPE type_name
Oracle 中的OOP概念{{
AS| IS } OBJECT | UNDER super_type}
Oracle 中的OOP概念{
Oracle 中的OOP概念attribute_name datatype
[,attribute_name datatype]… ---成员变量
Oracle 中的OOP概念
[{MAP | ORDER} MEMBER function_name,] ---排序函数
Oracle 中的OOP概念
[{FINAL | NOT FINAL} MEMBER function_name,] ---可否继承的成员函数
Oracle 中的OOP概念
[{INSTANTIABLE | NOT INSTANTIABLE } MEMBER function_name,] ---可否实例化的成员函数
Oracle 中的OOP概念
[{MEMBER | STATIC } function_name,] ---静态、非静态成员函数
Oracle 中的OOP概念
}
Oracle 中的OOP概念
[{FINAL | NOT FINAL}] ---对象可否继承
Oracle 中的OOP概念
[{INSTANTIABLE | NOT INSTANTIABLE }] ---对象可否实例化
Oracle 中的OOP概念

对象类型的主体部分(即函数的实现部分,可选的):

Oracle 中的OOP概念CREATE [OR REPLACE] 
Oracle 中的OOP概念TYPE BODY type_name {
AS| IS } 
Oracle 中的OOP概念
[{MAP | ORDER} MEMBER function_body,] ---排序函数
Oracle 中的OOP概念
[{MEMBER | STATIC } function_name,] ---静态、非静态成员函数
Oracle 中的OOP概念
END;
Oracle 中的OOP概念

例如:

Oracle 中的OOP概念create or replace 
Oracle 中的OOP概念type person 
as object(
Oracle 中的OOP概念first_name 
varchar2(100),
Oracle 中的OOP概念last_name 
varchar2(100))
Oracle 中的OOP概念

属性类型可以是任何oracle 数据类型(包括自定义),除了如下:

Oracle 中的OOP概念LONG和LONG RAW
Oracle 中的OOP概念
NCHAR、NCLOB 、NVARCHAR2
Oracle 中的OOP概念ROWID、UROWID
Oracle 中的OOP概念PL
/SQL的特定类型:%TYPE %ROWTYPE

查看:

Oracle 中的OOP概念Desc person 

构造函数:

Oracle 中的OOP概念set serveroutput on
Oracle 中的OOP概念
declare
Oracle 中的OOP概念l_person person
Oracle 中的OOP概念
begin
Oracle 中的OOP概念l_person :
= person(‘Donny’,’Chen’);
Oracle 中的OOP概念dbms_output.putline(l_person.first_name);
Oracle 中的OOP概念
end;
Oracle 中的OOP概念

构造函数要接受对象类型的所有属性作为参数。因为这些参数没有默认值,即使是null,也要提供。
举例:
表中的对象类型:
对象类型可以作为数据库中的列,所以称为列对象

Oracle 中的OOP概念create table person_table
Oracle 中的OOP概念(
Oracle 中的OOP概念name person,
Oracle 中的OOP概念age 
number)
Oracle 中的OOP概念
/
Oracle 中的OOP概念
set desc depth all
Oracle 中的OOP概念
desc person_table
Oracle 中的OOP概念
set desc depth 1
Oracle 中的OOP概念

插入数据:

Oracle 中的OOP概念insert into person_table
Oracle 中的OOP概念
values(person(‘Donny’,’Chen’),30);
Oracle 中的OOP概念
declare
Oracle 中的OOP概念l_person person
Oracle 中的OOP概念
begin
Oracle 中的OOP概念l_person :
= person(‘Hua’,’Li’);
Oracle 中的OOP概念
insert into person_table values(l_person,33);
Oracle 中的OOP概念
end;
Oracle 中的OOP概念

查询数据:

Oracle 中的OOP概念select * from person_table


访问对象类型的各个属性:

Oracle 中的OOP概念select p.name.first_name
Oracle 中的OOP概念
from person_table p

为避免名称解析问题,要求查询对象类型的属性的时候,使用表别名。否则报错,举例:
对象中的对象(合成):

Oracle 中的OOP概念create or replace
Oracle 中的OOP概念type employee 
as object(
Oracle 中的OOP概念name person,
Oracle 中的OOP概念empno 
number,
Oracle 中的OOP概念hiredate date)

 修改和删除对象:
9i之前,当建立的对象类型,以及依赖于此类型的对象或表之后,就无法再修改此对象类型了(增加删除属性和成员函数)。唯一的办法是撤销所有以来,即删除依赖于此类型的对象或表。
9i新特性,可以修改被以来的对象类型,成为类型演化。有两种方法:
INVALIDATE 和 CASCADE
INVALIDATE比如:

Oracle 中的OOP概念desc person_table

改变person类型,增加新属性ssn

Oracle 中的OOP概念alter type person
Oracle 中的OOP概念
add attribute ssn varchar2(11) INVALIDATE;
Oracle 中的OOP概念
desc person (bug可能需要新开一个session)
Oracle 中的OOP概念

INVALIDATE选项使的所有依赖于person类型的对象和表标记为INVALID,比如:

Oracle 中的OOP概念Desc person_table


需要手工验证person_table:

Oracle 中的OOP概念alter table person_table upgrade including data;
Oracle 中的OOP概念
desc person_table
Oracle 中的OOP概念

upgrade including data表示根据新类型,物理上更新现有的数据的结构,ssn 置为null。
也可以upgrade not including data,不更新原有数据的结构。Dml访问person实例数据的时候再更新。

Oracle 中的OOP概念Select * from person_table


CASCADE比如:

Oracle 中的OOP概念alter type person
Oracle 中的OOP概念
add attribute dob date
Oracle 中的OOP概念
cascade not including table data
Oracle 中的OOP概念

不用手工验证依赖此对象类型的表,由数据库自动验证。

Oracle 中的OOP概念Desc person
Oracle 中的OOP概念
Desc person_table

因为not including table data,没有更新原有数据:

Oracle 中的OOP概念select * from person_table


删除类型:
force
方法:
即对象中的过程和函数,3种类型:
STATIC: 只能够在对象类型上调用,不专属于某个实例。
MEMBER: 专属于某个特定的实例
CONSTRUCTOR: 构造函数

Oracle 中的OOP概念create or replace
Oracle 中的OOP概念type employee 
as object(
Oracle 中的OOP概念name person,
Oracle 中的OOP概念empno 
number,
Oracle 中的OOP概念hiredate date,
Oracle 中的OOP概念sal 
number,
Oracle 中的OOP概念commission 
number,
Oracle 中的OOP概念member 
function total_compensation return number,
Oracle 中的OOP概念static 
function new(p_empno number,
Oracle 中的OOP概念p_person person) 
return employee)
Oracle 中的OOP概念
/
Oracle 中的OOP概念
desc employee


在类型主体实现这两个方法:

Oracle 中的OOP概念create or replace
Oracle 中的OOP概念type body employee 
as 
Oracle 中的OOP概念member 
function total_compensation return number is
Oracle 中的OOP概念
begin 
Oracle 中的OOP概念
return nvl(self.sal,0+ nvl(self.commission, 0);
Oracle 中的OOP概念
end;
Oracle 中的OOP概念static 
function new(p_empno number,
Oracle 中的OOP概念p_person person) 
return employee is 
Oracle 中的OOP概念
begin 
Oracle 中的OOP概念
return employee(p_person,p_empno,sysdate,10000,null);
Oracle 中的OOP概念
end;
Oracle 中的OOP概念
end;
Oracle 中的OOP概念
/
Oracle 中的OOP概念

比较抽象数据类型的数据:

Oracle 中的OOP概念declare
Oracle 中的OOP概念l_employee1 employee;
Oracle 中的OOP概念l_employee2 employee;
Oracle 中的OOP概念
begin
Oracle 中的OOP概念l_employee1 :
=employee.new(12345,null);
Oracle 中的OOP概念l_employee2 :
=employee.new(67890,null);
Oracle 中的OOP概念
if l_employee1= l_employee2 then
Oracle 中的OOP概念dbms_output.line_put(“They are equal”);
Oracle 中的OOP概念
end if;
Oracle 中的OOP概念
end;
Oracle 中的OOP概念

使用map指定具体比较哪些属性:

Oracle 中的OOP概念create or replace
Oracle 中的OOP概念type employee 
as object(
Oracle 中的OOP概念name person,
Oracle 中的OOP概念empno 
number,
Oracle 中的OOP概念hiredate date,
Oracle 中的OOP概念sal 
number,
Oracle 中的OOP概念commission 
number,
Oracle 中的OOP概念map member 
function convert return number)
Oracle 中的OOP概念
/
Oracle 中的OOP概念
create or replace
Oracle 中的OOP概念type body employee 
as 
Oracle 中的OOP概念map member 
function convert return number is
Oracle 中的OOP概念
begin 
Oracle 中的OOP概念
return self.empno;
Oracle 中的OOP概念
end;
Oracle 中的OOP概念
end;
Oracle 中的OOP概念

再比较:

Oracle 中的OOP概念declare
Oracle 中的OOP概念l_employee1 employee;
Oracle 中的OOP概念l_employee2 employee;
Oracle 中的OOP概念
begin
Oracle 中的OOP概念l_employee1 :
=employee.new(12345,null);
Oracle 中的OOP概念l_employee2 :
=employee.new(67890,null);
Oracle 中的OOP概念
if l_employee1= l_employee2 then
Oracle 中的OOP概念dbms_output.line_put(“They are equal”);
Oracle 中的OOP概念
end if;
Oracle 中的OOP概念
if l_employee1> l_employee2 then
Oracle 中的OOP概念dbms_output.line_put(“employee1 
is greater”);
Oracle 中的OOP概念
end if;
Oracle 中的OOP概念
if l_employee1< l_employee2 then
Oracle 中的OOP概念dbms_output.line_put(“employee2 
is greater”);
Oracle 中的OOP概念
end if;
Oracle 中的OOP概念
end;
Oracle 中的OOP概念

Order 方法:

Oracle 中的OOP概念create or replace
Oracle 中的OOP概念type employee 
as object(
Oracle 中的OOP概念name person,
Oracle 中的OOP概念empno 
number,
Oracle 中的OOP概念hiredate date,
Oracle 中的OOP概念sal 
number,
Oracle 中的OOP概念commission 
number,
Oracle 中的OOP概念
order member function match(p_employee employee) return integer)
Oracle 中的OOP概念
/
Oracle 中的OOP概念
create or replace
Oracle 中的OOP概念type body employee 
as 
Oracle 中的OOP概念
order member function match(p_employee employee) return integer is
Oracle 中的OOP概念
begin 
Oracle 中的OOP概念
if self.empno> p_employee.empno then
Oracle 中的OOP概念
return 1;
Oracle 中的OOP概念elseif self.empno< p_employee.empno 
then
Oracle 中的OOP概念
return -1;
Oracle 中的OOP概念
else 
Oracle 中的OOP概念
return 0;
Oracle 中的OOP概念
end if;
Oracle 中的OOP概念
end;
Oracle 中的OOP概念
end;
Oracle 中的OOP概念

继承:

Oracle 中的OOP概念FINAL / NOT FINAL
Oracle 中的OOP概念

对象默认FINAL,表示不可以被继承;
MEMBER方法也能指定是否FINAL,表示能否在子类中对他进行覆写。默认NOT FINAL

Oracle 中的OOP概念Create or replace type super_type as object(
Oracle 中的OOP概念
number,
Oracle 中的OOP概念Final member 
procedure cannot_override
Oracle 中的OOP概念)
Oracle 中的OOP概念
not final
Oracle 中的OOP概念


 

相关文章:

  • 2021-05-28
  • 2022-12-23
  • 2021-10-06
  • 2022-02-01
  • 2022-01-28
  • 2022-12-23
  • 2021-11-28
  • 2019-06-11
猜你喜欢
  • 2022-01-19
  • 2022-01-19
  • 2021-12-17
  • 2021-08-01
  • 2022-12-23
  • 2021-08-23
  • 2021-11-24
相关资源
相似解决方案