Oracle 中的OOP概念:對象類型,可變數組,嵌套表,對象表,對象視圖。
對象類型:
優點:
1) 更容易與Java, C++編寫的對象應用程序交互
2) 獲取便捷。一次對象類型請求就可以從多個關系表中獲取信息,通過一次網絡往復即可返回
語法:
CREATE [OR REPLACE] TYPE type_name
{{AS| IS } OBJECT | UNDER super_type}
{
attribute_name datatype[,attribute_name datatype]… ---成員變量
[{MAP | ORDER} MEMBER function_name,] ---排序函數
[{FINAL | NOT FINAL} MEMBER function_name,] ---可否繼承的成員函數
[{INSTANTIABLE | NOT INSTANTIABLE } MEMBER function_name,] ---可否實例化的成員函數
[{MEMBER | STATIC } function_name,] ---靜態、非靜態成員函數
}[{FINAL | NOT FINAL}] ---對象可否繼承
[{INSTANTIABLE | NOT INSTANTIABLE }] ---對象可否實例化
/
對象類型的主體部分(即函數的實現部分,可選的):
CREATE [OR REPLACE]
TYPE BODY type_name {AS| IS }
[{MAP | ORDER} MEMBER function_body,] ---排序函數
[{MEMBER | STATIC } function_name,] ---靜態、非靜態成員函數
END;
/
例如:
create or replace
type person as object(
first_name varchar2(100),
last_name varchar2(100))
/
屬性類型可以是任何Oracle 數據類型(包括自定義),除了如下:
LONG和LONG RAW
NCHAR、NCLOB 、NVARCHAR2
ROWID、UROWID
PL/SQL的特定類型:%TYPE %ROWTYPE
查看:
Desc person
構造函數:
set serveroutput on
declare
l_person person
begin
l_person := person(‘Donny’,’Chen’);
dbms_output.putline(l_person.first_name);
end;
/
構造函數要接受對象類型的所有屬性作為參數。因為這些參數沒有默認值,即使是null,也要提供。
舉例:
表中的對象類型:
對象類型可以作為數據庫中的列,所以稱為列對象
create table person_table
(
name person,
age number)
/
set desc depth all
desc person_table
set desc depth 1
插入數據:
insert into person_table
values(person(‘Donny’,’Chen’),30);
declare
l_person person
begin
l_person := person(‘Hua’,’Li’);
insert into person_table values(l_person,33);
end;
/
查詢數據:
select * from person_table
訪問對象類型的各個屬性:
select p.name.first_name
from person_table p
/
為避免名稱解析問題,要求查詢對象類型的屬性的時候,使用表別名。否則報錯,
舉例:對象中的對象(合成):create or replace
type employee as object(
name person,
empno number,
hiredate date)
修改和刪除對象:
9i之前,當建立的對象類型,以及依賴於此類型的對象或表之後,就無法再修改此對象類型了(增加刪除屬性和成員函數)。唯一的辦法是撤銷所有以來,即刪除依賴於此類型的對象或表。9i新特性,可以修改被以來的對象類型,成為類型演化。
有兩種方法:INVALIDATE 和 CASCADE
INVALIDATE比如:desc person_table
改變person類型,增加新屬性ssn
alter type person
add attribute ssn varchar2(11) INVALIDATE;
desc person (bug可能需要新開一個session)
INVALIDATE選項使的所有依賴於person類型的對象和表標記為INVALID,比如:
Desc person_table
需要手工驗證person_table:
alter table person_table upgrade including data;
desc person_table
upgrade including data表示根據新類型,物理上更新現有的數據的結構,ssn 置為null。
也可以upgrade not including data,不更新原有數據的結構。Dml訪問person實例數據的時候再更新。
Select * from person_table
CASCADE比如:
alter type person
add attribute dob date
cascade not including table data
/不用手工驗證依賴此對象類型的表,由數據庫自動驗證。
Desc person
Desc person_table
因為not including table data,沒有更新原有數據:
select * from person_table
刪除類型:force
方法:即對象中的過程和函數,3種類型:
STATIC: 只能夠在對象類型上調用,不專屬於某個實例。
MEMBER: 專屬於某個特定的實例
CONSTRUCTOR: 構造函數
create or replace
type employee as object(
name person,
empno number,
hiredate date,
sal number,
commission number,
member function total_compensation return number,
static function new(p_empno number,
p_person person) return employee)
/
desc employee
在類型主體實現這兩個方法:
create or replace
type body employee as
member function total_compensation return number is
begin
return nvl(self.sal,0) + nvl(self.commission, 0);
end;
static function new(p_empno number,
p_person person) return employee is
begin
return employee(p_person,p_empno,sysdate,10000,null);
end;
end;
/
比較抽象數據類型的數據:
declare
l_employee1 employee;
l_employee2 employee;
begin
l_employee1 :=employee.new(12345,null);
l_employee2 :=employee.new(67890,null);
if l_employee1= l_employee2 then
dbms_output.line_put(“They are equal”);
end if;
end;
/
使用map指定具體比較哪些屬性:
create or replace
type employee as object(
name person,
empno number,
hiredate date,
sal number,
commission number,
map member function convert return number)
/
create or replace
type body employee as
map member function convert return number is
begin
return self.empno;
end;
end;
/
再比較:
declare
l_employee1 employee;
l_employee2 employee;
begin
l_employee1 :=employee.new(12345,null);
l_employee2 :=employee.new(67890,null);
if l_employee1= l_employee2 then
dbms_output.line_put(“They are equal”);
end if;
if l_employee1> l_employee2 then
dbms_output.line_put(“employee1 is greater”);
end if;
if l_employee1< l_employee2 then
dbms_output.line_put(“employee2 is greater”);
end if;
end;
Order 方法:
create or replace
type employee as object(
name person,
empno number,
hiredate date,
sal number,
commission number,
order member function match(p_employee employee) return integer)
/
create or replace
type body employee as
order member function match(p_employee employee) return integer is
begin
if self.empno> p_employee.empno then
return 1;
elseif self.empno< p_employee.empno then
return -1;
else
return 0;
end if;
end;
end;
/
繼承:
FINAL / NOT FINAL
對象默認FINAL,表示不可以被繼承;
MEMBER方法也能指定是否FINAL,表示能否在子類中對他進行覆寫。默認NOT FINAL
Create or replace type super_type as object(
N number,
Final member procedure cannot_override
)
not final
/create or replace type sub_type under super_type(
overriding member procedure cannot_override
)
/
show error
若super_type 改成final
INSTANTIABLE / NOT INSTANTIABLE
可否被實例化,後者類似於抽象類
create or replace type shape as object(
number_of_sides number,
not instantiable member function calculate_area return number
)
not instantiable not final
/
實例化該類型對象:
declare
l_shape shape;
begin
l_shape:=shape(2);
end;
/
可變數組(VARRAYS):
create type employee_type as object(
employee_id number,
first_name varchar2(30),
last_name varchar2(30)
)
/
create type employee_list_type as varray(50) of employee_type
/
create table departments(
department_id number,
department_name varchar2(30),
manager employee_type,
employees employee_list_type)
/
insert into departments values
(10,
‘HR’,
employee_type(1,’Dony’,’Chen’),
employee_list_type(
employee_type(2,’Hua’,’Li’)
employee_type(3,’Wu’,’Wang’)
employee_type(4,’San’,’Zhang’))
)
/
column department_name format a13
column employee_type format a63 Word_wrapped
select * from departments
/
嵌套表(Nested table):
create type order_item_type as object(
line_item_id number(3),
product_id number(6),
unit_price numbe(8,2),
quantity number(4)
)
/
create type order_item_list_type as table of order_item_type
/
create table orders(
order_id number(12) not null,
order_date date,
customer_id number(6),
order_items order_item_list_type)
nested table order_items store as order_items_tab
/
insert into orders values(
(1, sysdate, 10,
order_item_list_type(
order_item_type(1,2,3,4),
order_item_type(2,3,4,5)
))
/
多少個order_item_type ,無限制。
對象表:每行都代表一個對象,行對象。
創建對象:
create or replace
type address as object(
id number,
street varchar2(100),
state varchar2(2),
zipcode varchar2(11)
)
/
創建對象表:
create table address_table of address
/
desc address_table
插入數據:
可以像關系表一樣插入
insert into address_table values(1,’Oracle way’,’CA’,’90001’)
/也可以用默認構造函數插入對象:
insert into address_table
values(address(2,’Oracle way2’,’CA’,’90011’)
select * from address_ table
/VALUE()
以對象表別名作為參數,返回對象實例:
select value(a) from address_table a
/
REF數據類型:
在關系表中關聯對象
create table employee_location(
empno number,
loc_ref ref address scope is address_table)
/
loc_ref是個列,類型是指向address對象類型的ref, 即引用,或者指向address實例的指針。
scope is address_table 是可選的,表示ref指向的對象實例的位置,即只能指向address_table對象表中的address對象實例。
REF():
REF()函數可以建立指向對象表中對象實例的REF對象數據類型,以對象表的別名作為參數
插入數據:
insert into employee_location
select 12345, ref(a)
from address_table a
where id=1
/
insert into employee_location
select 45678, ref(a)
from address_table a
where id=2/
OID:
對象表中每一行對象都對應一個唯一的OID,對象標示符
Select * from employee_location
DEREF()
解析REF數據類型,返回真正指向的實例。以REF數據類型作為參數
select empno, deref(loc_ref)
from employee_location
懸空REF:
REF指向的對象實例被刪掉了,此時稱REF懸空(dangling),說明ref指向不存在的實例
Delete from address_table where id=1;
Select * from employee_location;
Select empno, deref(rec_loc) from employee_location;
懸空的ref會返回null,使用is dangling 確定哪些ref懸空:
select empno from employee_location
where loc_ref is dangling;
清除懸空的ref, 將ref更新為null:
update employee_location
set loc_ref =null
where loc_ref is dangling;
Select * from employee_location;
對象視圖:
在已有的關系型表上,建立對象模型:
關系表:
create table item
( itemcode varchar2(10),
item_on_hand number(10),
item_sode number(10)
);建立對象,使用相同的列:
create or replace type item_type as object
( itemcode varchar2(10),
item_on_hand number(10),
item_sode number(10)
);
建立對象視圖:
create view item_vIEw of item_type
with object oid (itemcode)
as
select * from item
/
of item_type 說明基於對象
with object oid (itemcode) 明確生成OID
通過視圖操作數據:
insert into item_vIEw values(item_type(‘i102’,15,50));
MAKE_REF()
關系主表1:
create table itmefile(
itemcode varchar2(5) primary key,
itemdesc varchar2(20),
p_category varchar2(20),
qty_hand number(5),
re_level number(5),
max_level number(5),
itemrate number(9,2));
關系從表2:
create table order_detail(
orderno varchar2(5),
itemcode varchar2(5) foreign key references itemfile(itemcode)
qty_ord number(5),
qty_deld number(5)
)