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.
34 * Moodle will not parse this file correctly if it uses Windows line endings.
37 CREATE OR REPLACE PACKAGE MOODLELIB AS
39 FUNCTION BITOR (value1 IN INTEGER, value2 IN INTEGER) RETURN INTEGER;
40 FUNCTION BITXOR(value1 IN INTEGER, value2 IN INTEGER) RETURN INTEGER;
42 FUNCTION GET_HANDLE (lock_name IN VARCHAR2) RETURN VARCHAR2;
43 FUNCTION GET_LOCK (lock_name IN VARCHAR2, lock_timeout IN INTEGER) RETURN INTEGER;
44 FUNCTION RELEASE_LOCK(lock_name IN VARCHAR2) RETURN INTEGER;
46 FUNCTION UNDO_DIRTY_HACK(hackedstring IN VARCHAR2) RETURN VARCHAR2;
47 FUNCTION UNDO_MEGA_HACK(hackedstring IN VARCHAR2) RETURN VARCHAR2;
48 FUNCTION TRICONCAT(string1 IN VARCHAR2, string2 IN VARCHAR2, string3 IN VARCHAR2) RETURN VARCHAR2;
53 CREATE OR REPLACE PACKAGE BODY MOODLELIB AS
55 FUNCTION BITOR(value1 IN INTEGER, value2 IN INTEGER) RETURN INTEGER IS
58 RETURN value1 + value2 - BITAND(value1,value2);
61 FUNCTION BITXOR(value1 IN INTEGER, value2 IN INTEGER) RETURN INTEGER IS
64 RETURN MOODLELIB.BITOR(value1,value2) - BITAND(value1,value2);
67 FUNCTION GET_HANDLE(lock_name IN VARCHAR2) RETURN VARCHAR2 IS
68 PRAGMA AUTONOMOUS_TRANSACTION;
69 lock_handle VARCHAR2(128);
72 DBMS_LOCK.ALLOCATE_UNIQUE (
73 lockname => lock_name,
74 lockhandle => lock_handle,
75 expiration_secs => 864000);
79 FUNCTION GET_LOCK(lock_name IN VARCHAR2, lock_timeout IN INTEGER) RETURN INTEGER IS
82 lock_status := DBMS_LOCK.REQUEST(
83 lockhandle => GET_HANDLE(lock_name),
84 lockmode => DBMS_LOCK.X_MODE, -- eXclusive
85 timeout => lock_timeout,
86 release_on_commit => FALSE);
89 WHEN 2 THEN RAISE_APPLICATION_ERROR(-20000,'deadlock detected');
90 WHEN 4 THEN RAISE_APPLICATION_ERROR(-20000,'lock already obtained');
91 ELSE RAISE_APPLICATION_ERROR(-20000,'request lock failed - ' || lock_status);
96 FUNCTION RELEASE_LOCK(lock_name IN VARCHAR2) RETURN INTEGER IS
99 lock_status := DBMS_LOCK.RELEASE(
100 lockhandle => GET_HANDLE(lock_name));
101 IF lock_status > 0 THEN
102 RAISE_APPLICATION_ERROR(-20000,'release lock failed - ' || lock_status);
107 FUNCTION UNDO_DIRTY_HACK(hackedstring IN VARCHAR2) RETURN VARCHAR2 IS
110 IF hackedstring = ' ' THEN
116 FUNCTION UNDO_MEGA_HACK(hackedstring IN VARCHAR2) RETURN VARCHAR2 IS
119 IF hackedstring IS NULL THEN
122 RETURN REPLACE(hackedstring, '*OCISP*', ' ');
125 FUNCTION TRICONCAT(string1 IN VARCHAR2, string2 IN VARCHAR2, string3 IN VARCHAR2) RETURN VARCHAR2 IS
126 stringresult VARCHAR2(1333);
128 IF string1 IS NULL THEN
131 IF string2 IS NULL THEN
134 IF string3 IS NULL THEN
138 stringresult := CONCAT(CONCAT(MOODLELIB.UNDO_DIRTY_HACK(string1), MOODLELIB.UNDO_DIRTY_HACK(string2)), MOODLELIB.UNDO_DIRTY_HACK(string3));
140 IF stringresult IS NULL THEN