1 -- This file is part of Moodle - http://moodle.org/
3 -- Moodle is free software: you can redistribute it and/or modify
4 -- it under the terms of the GNU General Public License as published by
5 -- the Free Software Foundation, either version 3 of the License, or
6 -- (at your option) any later version.
8 -- Moodle is distributed in the hope that it will be useful,
9 -- but WITHOUT ANY WARRANTY; without even the implied warranty of
10 -- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 -- GNU General Public License for more details.
13 -- You should have received a copy of the GNU General Public License
14 -- along with Moodle. If not, see <http://www.gnu.org/licenses/>.
18 * @copyright 2009 onwards Eloy Lafuente (stronk7) {@link http://stronk7.com}
19 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
20 * @version 20091010 (plz, keep this updated for easier reference)
24 * This sql script generates various PL/SQL packages needed to provide
25 * cross-db compatibility in the Moodle 2.x DB API with some operations
26 * not natively supported by Oracle, namely:
27 * - locking: Application locks used by Moodle DB sessions. It uses
28 * the DBMS_LOCK package so execution must be granted
29 * to the Moodle DB user by SYS to work properly.
30 * - bit ops: To provide cross-db bitwise operations to be used by the
31 * sql_bitXXX() helper functions
32 * - one space hacks: One space empty string substitute hacks.
35 CREATE OR REPLACE PACKAGE MOODLELIB AS
37 FUNCTION BITOR (value1 IN INTEGER, value2 IN INTEGER) RETURN INTEGER;
38 FUNCTION BITXOR(value1 IN INTEGER, value2 IN INTEGER) RETURN INTEGER;
40 FUNCTION GET_HANDLE (lock_name IN VARCHAR2) RETURN VARCHAR2;
41 FUNCTION GET_LOCK (lock_name IN VARCHAR2, lock_timeout IN INTEGER) RETURN INTEGER;
42 FUNCTION RELEASE_LOCK(lock_name IN VARCHAR2) RETURN INTEGER;
44 FUNCTION UNDO_DIRTY_HACK(hackedstring IN VARCHAR2) RETURN VARCHAR2;
45 FUNCTION UNDO_MEGA_HACK(hackedstring IN VARCHAR2) RETURN VARCHAR2;
46 FUNCTION TRICONCAT(string1 IN VARCHAR2, string2 IN VARCHAR2, string3 IN VARCHAR2) RETURN VARCHAR2;
51 CREATE OR REPLACE PACKAGE BODY MOODLELIB AS
53 FUNCTION BITOR(value1 IN INTEGER, value2 IN INTEGER) RETURN INTEGER IS
56 RETURN value1 + value2 - BITAND(value1,value2);
59 FUNCTION BITXOR(value1 IN INTEGER, value2 IN INTEGER) RETURN INTEGER IS
62 RETURN MOODLELIB.BITOR(value1,value2) - BITAND(value1,value2);
65 FUNCTION GET_HANDLE(lock_name IN VARCHAR2) RETURN VARCHAR2 IS
66 PRAGMA AUTONOMOUS_TRANSACTION;
67 lock_handle VARCHAR2(128);
70 DBMS_LOCK.ALLOCATE_UNIQUE (
71 lockname => lock_name,
72 lockhandle => lock_handle,
73 expiration_secs => 864000);
77 FUNCTION GET_LOCK(lock_name IN VARCHAR2, lock_timeout IN INTEGER) RETURN INTEGER IS
80 lock_status := DBMS_LOCK.REQUEST(
81 lockhandle => GET_HANDLE(lock_name),
82 lockmode => DBMS_LOCK.X_MODE, -- eXclusive
83 timeout => lock_timeout,
84 release_on_commit => FALSE);
87 WHEN 2 THEN RAISE_APPLICATION_ERROR(-20000,'deadlock detected');
88 WHEN 4 THEN RAISE_APPLICATION_ERROR(-20000,'lock already obtained');
89 ELSE RAISE_APPLICATION_ERROR(-20000,'request lock failed - ' || lock_status);
94 FUNCTION RELEASE_LOCK(lock_name IN VARCHAR2) RETURN INTEGER IS
97 lock_status := DBMS_LOCK.RELEASE(
98 lockhandle => GET_HANDLE(lock_name));
99 IF lock_status > 0 THEN
100 RAISE_APPLICATION_ERROR(-20000,'release lock failed - ' || lock_status);
105 FUNCTION UNDO_DIRTY_HACK(hackedstring IN VARCHAR2) RETURN VARCHAR2 IS
108 IF hackedstring = ' ' THEN
114 FUNCTION UNDO_MEGA_HACK(hackedstring IN VARCHAR2) RETURN VARCHAR2 IS
117 IF hackedstring IS NULL THEN
120 RETURN REPLACE(hackedstring, '*OCISP*', ' ');
123 FUNCTION TRICONCAT(string1 IN VARCHAR2, string2 IN VARCHAR2, string3 IN VARCHAR2) RETURN VARCHAR2 IS
124 stringresult VARCHAR2(1333);
126 IF string1 IS NULL THEN
129 IF string2 IS NULL THEN
132 IF string3 IS NULL THEN
136 stringresult := CONCAT(CONCAT(MOODLELIB.UNDO_DIRTY_HACK(string1), MOODLELIB.UNDO_DIRTY_HACK(string2)), MOODLELIB.UNDO_DIRTY_HACK(string3));
138 IF stringresult IS NULL THEN