form 开发中的insert,update,delete,lock_row 举例

    在进行OA 开发中,FORM中的BLOCK 一般都是基于VIEW 的,所以无法使用FORM 提供的数据操作功能,对于INSERT/DELETE/UPDATE 操作均要编写代码来
实现。一般情况下,BLOCK 的HANDLER 包含以下几个PROCEDURE:

INSERT_ROW
UPDATE_ROW
DELETE_ROW
LOCK_ROW

其中LOCK_ROW 比较特别,它实现了一种锁机制,用于比较并保持FORM 中数据与数据库中数据的一致性。

CREATE OR REPLACE PACKAGE EMP IS

-- Author : YUAN
-- Created : 2013/8/15 11:40:49
-- Purpose : EMP TABLE HANDLER

PROCEDURE INSERT_ROW;
PROCEDURE LOCK_ROW;
PROCEDURE UPDATE_ROW;
PROCEDURE DELETE_ROW;
END EMP;

CREATE OR REPLACE PACKAGE BODY EMP IS

PROCEDURE INSERT_ROW IS
CURSOR C IS
SELECT ROWID FROM EMP WHERE EMPNO = :EMP.EMPNO;
BEGIN
INSERT INTO EMP
(EMPNO, ENAME, JOB, MGR, HIREDATE, SAL, COMM, DEPTNO)
VALUES
(:EMP.EMPNO,
:EMP.ENAME,
:EMP.JOB,
:EMP.MGR,
:EMP.HIREDATE,
:EMP.SAL,
:EMP.COMM,
:EMP.DEPTNO);
OPEN C;
FETCH C
INTO :EMP.ROW_ID;
IF (C%NOTFOUND) THEN
CLOSE C;
RAISE NO_DATA_FOUND;
END IF;
CLOSE C;
END INSERT_ROW;
/*锁记录*/
PROCEDURE LOCK_ROW IS
COUNTER NUMBER;
CURSOR C IS
SELECT EMPNO, ENAME, JOB, MGR, HIREDATE, SAL, COMM, DEPTNO
FROM EMP
WHERE ROWID = :EMP.ROW_ID
FOR UPDATE OF EMPNO NOWAIT;
RECINFO C%ROWTYPE;
BEGIN
COUNTER := 0;
LOOP
BEGIN
COUNTER := COUNTER + 1;
OPEN C;
FETCH C
INTO RECINFO;
IF (C%NOTFOUND) THEN
CLOSE C;
FND_MESSAGE.SET_NAME('FND', 'FORM_RECORD_DELETED');
FND_MESSAGE.ERROR;
RAISE FORM_TRIGGER_FAILURE;
END IF;
CLOSE C;
IF (
(RECINFO.EMPNO = :EMP.EMPNO)
AND
((RECINFO.ENAME = :EMP.ENAME) OR
((RECINFO.ENAME IS NULL) AND (:EMP.ENAME IS NULL)))
AND
((RECINFO.JOB = :EMP.JOB) OR
((RECINFO.JOB IS NULL) AND (:EMP.JOB IS NULL)))
AND
((RECINFO.MGR = :EMP.MGR) OR
((RECINFO.MGR IS NULL) AND (:EMP.MGR IS NULL)))
AND
((RECINFO.HIREDATE = :EMP.HIREDATE) OR
((RECINFO.HIREDATE IS NULL) AND (:EMP.HIREDATE IS NULL)))
AND
((RECINFO.SAL = :EMP.SAL) OR
((RECINFO.SAL IS NULL) AND (:EMP.SAL IS NULL)))
AND
((RECINFO.COMM = :EMP.COMM) OR
((RECINFO.COMM IS NULL) AND (:EMP.COMM IS NULL)))
AND
(RECINFO.DEPTNO = :EMP.DEPTNO)) THEN
RETURN;
ELSE
FND_MESSAGE.SET_NAME('FND', 'FORM_RECORD_CHANGED');
FND_MESSAGE.ERROR;
RAISE FORM_TRIGGER_FAILURE;
END IF;
EXCEPTION
WHEN APP_EXCEPTIONS.RECORD_LOCK_EXCEPTION THEN
IF (C% ISOPEN) THEN
CLOSE C;
END IF;
APP_EXCEPTION.RECORD_LOCK_ERROR(COUNTER);
END;
END LOOP;
END LOCK_ROW;


PROCEDURE UPDATE_ROW IS
BEGIN
UPDATE EMP
SET EMPNO = :EMP.EMPNO,
ENAME = :EMP.ENAME,
JOB = :EMP.JOB,
MGR = :EMP.MGR,
HIREDATE = :EMP.HIREDATE,
SAL = :EMP.SAL,
COMM = :EMP.COMM,
DEPTNO = :EMP.DEPTNO
WHERE ROWID = :EMP.ROW_ID;
IF (SQL%NOTFOUND) THEN
RAISE NO_DATA_FOUND;
END IF;
END UPDATE_ROW;
PROCEDURE DELETE_ROW IS
BEGIN
DELETE FROM EMP WHERE ROWID = :EMP.ROW_ID;
IF (SQL%NOTFOUND) THEN
RAISE NO_DATA_FOUND;
END IF;
END DELETE_ROW;
END EMP;

END EMP;

posted @ 2013-08-15 12:03  SanFrans  阅读(621)  评论(0编辑  收藏  举报