các stored procedure trong các Package khiến công việc trở nên modul hoá, che dấu thông tin, dễ dàng đợc mở rộng khi thêm nhiều chức năng mà không bị ảnh h- ởng đến chơng trình cũ. Chúng ta sẽ dùng OleDb để gọi các stored procedure trong đâỵ
Create or replace package body PackCategory AS
Procedure Getallcategory(c_cursor out cat_cursor) as
begin
Open c_cursor for
select * From Categories; End Getallcategory;
Procedure Getsubcategoriesbycat(icategoryid in number, c_cursor out cat_cursor)
as begin
Open c_cursor for
Select c.subcategoryid,c.subcategoryName from subcategories c
where c.categoryid=icategoryid ; End Getsubcategoriesbycat;
End PackCategory;
Create or replace package body PackMail As
Procedure Getmaildetails(imailid in number, m_cursor OUT mailcursor)
As
Begin
Open m_cursor for
Select * from Maildetails where Mailitemid=imailid; End Getmaildetails;
Procedure Getmailbyproduct(iProductid In Number, m_cursor Out mailcursor)
As
Begin
Open m_cursor for
Select * from Maildetails where ProductID=iProductid; End GetmailbyProduct;
Procedure GetmailbyUser(iUserid In number, m_cursor Out mailcursor)
As
Begin
Open m_cursor for
Select m.mailitemid, m.Productid,m.subject,m.RCPT, m.mbody,m.Datesent
From maildetails m, Mailsent s Where
m.mailitemid=s.mailitemid and s.userid=iuserid; End GetmailbyUser;
Procedure Getmailtype (imailtype In number, m_cursor out mailcursor)
Begin
Open m_cursor for
Select * from Emailtemplate; End Getmailtype;
End Packmails;
Create or replace package body PackProduct As
Procedure GetProductbycat(icategoryid in number,i_isforsale In varchar2,p_cursor out procursor)
As begin
if i_isforsale='both' then open p_cursor for
select p.productid,p.productmodel,p.imageurl,p.totalview, p.price,p.isforsale,ụuserid,ụUSERNAME,p.subcategoryid from products p,users u
where p.subcategoryid in (select subcategoryid from subcategories where
categoryid=icategoryid) and p.userid=ụuserid ; else
if i_isforsale='salộ then open p_cursor for
select p.productid,p.productmodel,p.imageurl,p.totalview, p.price,p.isforsale,ụuserid,ụUSERNAME,p.subcategoryid from products p,users u
where p.subcategoryid in (select subcategoryid from subcategories where
categoryid=icategoryid) and p.userid=ụuserid and p.isforsale='For sale onlý;
else
open p_cursor for
select p.productid,p.productmodel,p.imageurl,p.totalview, p.price,p.isforsale,ụuserid,ụUSERNAME,p.subcategoryid from products p,users u
where p.subcategoryid in (select subcategoryid from subcategories where
categoryid=icategoryid) and p.userid=ụuserid and p.Isforsale ='Wanted';
end if; end if;
end GetProductbycat;
procedure GetProductbysubcat(isubcategoryid in Number, p_cursor out procursor)
As begin
open p_cursor for select
p.productid,p.productmodel,p.production,p.imageurl,p.totalview, p.price,p.isforsale,ụuserid from products p,users u
where p.subcategoryid=isubcategoryid and p.userid=ụuserid; end GetProductbysubcat;
iPRODUCTION IN VARCHAR2, iCOUNTRY IN VARCHAR2,iPRICE IN FLOAT, iIMAGEURL IN VARCHAR2,
iDESCRIPTION IN VARCHAR2, iQUALITY IN NUMBER,iAMOUNT IN NUMBER,iTOTALVIEW IN NUMBER,
iDATECREATED IN DATE, iISFORSALE IN VARCHAR2) IS
Begin
Insert into Post values( (post_seq.nextval),iSUBCATEGORYID,iUSERID,iPRODUCTMODEL,iPRODUCT ION,iCOUNTRY,iPRICE, iIMAGEURL,iDESCRIPTION, iQUALITY,iAMOUNT,iTOTALVIEW,iDATECREATED,iISFORSALE); end PostNewProduct;
Procedure SearchProducts(search IN Varchar2,pageindex In number,pagesize in number,p_cursor Out procursor)
As
pagelow number; pagehight number;
Cursor resultcursor is Select ProductID from products Where ProductModel like '%search'
OR Production like '%search' OR Description like '%search'; cnt Integer; productlow integer; producthight integer; begin cnt:=0; pagelow:=pageindex * pagesize; pagehight:=pagelow + pagesize + 1; /* Ok, now paging */
For countrec In resultcursor Loop if cnt = pagelow then productlow:=countrec.Productid; End if; if cnt = pagehight then producthight:=countrec.Productid; cnt:=cnt+1; End if; End loop;
Open p_cursor for Select
P.ProductId,P.ProductModel,P.ImageUrl,P.Price,P.Totalview,ỤUserName From Products P,Users U
Where P.ProductModel like '%search' OR P.Production like '%search' OR P.Description like '%search'
and p.userid=ụuserid and P.productid >=Productlow and P.productid <= producthight;
End SearchProducts;
Procedure GetProductDetails(i_ProductID In Number,p_cursor Out procursor)
As Begin
Select
P.SUBCATEGORYID,P.USERID,P.PRODUCTMODEL,P.PRODUCTION,P.COUNTRY,P.PRI CE,
P.IMAGEURL,P.DESCRIPTION,P.QUALITY,P.AMOUNT,P.TOTALVIEW,P.DATECR EATED,P.ISFORSALE,
ỤUserid From Products P,Users U Where
P.Productid=i_Productid and P.Userid=ỤUserid; End GetProductDetails;
Procedure AđPostProduct(Productid In Number) As
Begin
Insert Into Products select * From Post; End AđPostProduct;
Procedure GetHotProduct (PrevWeek In Date, p_cursor OUT procursor)
As Begin
Open p_cursor for Select
P.SUBCATEGORYID,P.USERID,P.PRODUCTMODEL,P.PRODUCTION,P.COUNTRY,P.PRI CE,
P.IMAGEURL,P.DESCRIPTION,P.QUALITY,P.AMOUNT,P.TOTALVIEW,P.DATECR EATED,P.ISFORSALE,
ỤUserid From Products P,Users U Where
P.Userid=ỤUserid and P.Datecreated >=PrevWeek; End GetHotProduct;
Procedure Updateview (iProductid IN Number) As
Begin
Update Products set totalview= totalview +1 Where ProductId =iProductid;
end Updateview; End packproduct;
Create or replace package body PackUser As
Procedure CheckEmail(iEmailađ in varchar2,U_cursor Out UserCursor)
As Begin
Open U_cursor for
Select Email from Users where Email=iEmailađ; End CheckEmail;
Procedure CheckLogin (iadmin In varchar2,iEmailađ In Varchar2,iPassword In varchar2, U_cursor Out UserCursor)
As Begin
If iadmin ='%normal' then Open U_cursor for
Select Userid,UserName,password from Users Where Email=iEmailađ
and Password=iPassword; Else
password=ipassword; End if;
End CheckLogin;
Procedure AđUser(USERNAME IN Varchar2,TEL IN Varchar2, AĐRESS IN Varchar2,
EMAIL IN Varchar2,PASSWORD In Varchar2) As
Begin
Insert Into Users Values(
(User_seq.Nextval), USERNAME, TEL,AĐRESS, EMAIL,PASSWORD); End AđUser;
Procedure GetUserDetails(iuserid In Number, u_cursor out usercursor)
As Begin
Open u_cursor for
Select UserName,Tel,Ađress,Email from users where userid=iuserid;
End GetUserDetails; end packUser;