Block plugin tables can use the XMLDB install & upgrade
[moodle.git] / lib / xmldb / classes / generators / oci8po / oci8po.class.php
CommitLineData
d7444bfc 1<?php // $Id$
2
3///////////////////////////////////////////////////////////////////////////
4// //
5// NOTICE OF COPYRIGHT //
6// //
7// Moodle - Modular Object-Oriented Dynamic Learning Environment //
8// http://moodle.com //
9// //
10// Copyright (C) 2001-3001 Martin Dougiamas http://dougiamas.com //
11// (C) 2001-3001 Eloy Lafuente (stronk7) http://contiento.com //
12// //
13// This program is free software; you can redistribute it and/or modify //
14// it under the terms of the GNU General Public License as published by //
15// the Free Software Foundation; either version 2 of the License, or //
16// (at your option) any later version. //
17// //
18// This program is distributed in the hope that it will be useful, //
19// but WITHOUT ANY WARRANTY; without even the implied warranty of //
20// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the //
21// GNU General Public License for more details: //
22// //
23// http://www.gnu.org/copyleft/gpl.html //
24// //
25///////////////////////////////////////////////////////////////////////////
26
27/// This class generate SQL code to be used against Oracle
28/// It extends XMLDBgenerator so everything can be
29/// overriden as needed to generate correct SQL.
30
31class XMLDBoci8po extends XMLDBgenerator {
32
33/// Only set values that are different from the defaults present in XMLDBgenerator
34
0513f3bf 35 var $statement_end = "\n/"; // String to be automatically added at the end of each statement
36 // Using "/" because the standard ";" isn't good for stored procedures (triggers)
37
d7444bfc 38 var $number_type = 'NUMBER'; // Proper type for NUMBER(x) in this DB
39
40 var $unsigned_allowed = false; // To define in the generator must handle unsigned information
2efaf3f8 41 var $default_for_char = ' '; // To define the default to set for NOT NULLs CHARs without default (null=do nothing)
42 // Using this whitespace here because Oracle doesn't distinguish empty and null! :-(
d7444bfc 43
f075bac4 44 var $default_after_null = false; //To decide if the default clause of each field must go after the null clause
45
d7444bfc 46 var $foreign_keys = false; // Does the generator build foreign keys
47
48 var $primary_index = false;// Does the generator need to build one index for primary keys
1bf63d33 49 var $unique_index = false; // Does the generator need to build one index for unique keys
d7444bfc 50 var $foreign_index = true; // Does the generator need to build one index for foreign keys
51
52 var $sequence_extra_code = true; //Does the generator need to add extra code to generate the sequence fields
53 var $sequence_name = ''; //Particular name for inline sequences in this generator
54
55 var $enum_inline_code = false; //Does the generator need to add inline code in the column definition
56
57 /**
58 * Creates one new XMLDBpostgres7
59 */
60 function XMLDBoci8po() {
61 parent::XMLDBgenerator();
62 $this->prefix = '';
63 $this->reserved_words = $this->getReservedWords();
64 }
65
66 /**
67 * Given one XMLDB Type, lenght and decimals, returns the DB proper SQL type
68 */
69 function getTypeSQL ($xmldb_type, $xmldb_length=null, $xmldb_decimals=null) {
70
71 switch ($xmldb_type) {
72 case XMLDB_TYPE_INTEGER: // From http://www.postgresql.org/docs/7.4/interactive/datatype.html
73 if (empty($xmldb_length)) {
74 $xmldb_length = 10;
75 }
76 $dbtype = 'NUMBER(' . $xmldb_length . ')';
77 break;
78 case XMLDB_TYPE_NUMBER:
79 $dbtype = $this->number_type;
80 if (!empty($xmldb_length)) {
81 $dbtype .= '(' . $xmldb_length;
82 if (!empty($xmldb_decimals)) {
83 $dbtype .= ',' . $xmldb_decimals;
84 }
85 $dbtype .= ')';
86 }
87 break;
88 case XMLDB_TYPE_FLOAT:
89 $dbtype = 'NUMBER';
90 break;
91 case XMLDB_TYPE_CHAR:
92 $dbtype = 'VARCHAR2';
93 if (empty($xmldb_length)) {
94 $xmldb_length='255';
95 }
96 $dbtype .= '(' . $xmldb_length . ')';
97 break;
98 case XMLDB_TYPE_TEXT:
99 $dbtype = 'CLOB';
100 break;
101 case XMLDB_TYPE_BINARY:
102 $dbtype = 'BLOB';
103 break;
104 case XMLDB_TYPE_DATETIME:
105 $dbtype = 'DATE';
106 break;
107 }
108 return $dbtype;
109 }
110
111 /**
112 * Returns the code needed to create one enum for the xmldb_table and xmldb_field passes
113 */
114 function getEnumExtraSQL ($xmldb_table, $xmldb_field) {
115
116 $sql = 'CONSTRAINT ' . $this->getNameForObject($xmldb_table->getName(), $xmldb_field->getName(), 'ck');
117 $sql.= ' CHECK (' . $this->getEncQuoted($xmldb_field->getName()) . ' IN (' . implode(', ', $xmldb_field->getEnumValues()) . ')),';
118
119 return $sql;
120 }
121
122 /**
123 * Returns the code needed to create one sequence for the xmldb_table and xmldb_field passes
124 */
125 function getCreateSequenceSQL ($xmldb_table, $xmldb_field) {
126
465a8029 127 $sequence_name = $this->getNameForObject($xmldb_table->getName(), $xmldb_field->getName(), 'seq');
128
129 $sequence = "CREATE SEQUENCE " . $sequence_name;
d7444bfc 130 $sequence.= "\n START WITH 1";
131 $sequence.= "\n INCREMENT BY 1";
9dcc6300 132 $sequence.= "\n NOMAXVALUE";
d7444bfc 133
134 $trigger_name = $this->getNameForObject($xmldb_table->getName(), $xmldb_field->getName(), 'trg');
135
9dcc6300 136 $trigger = "CREATE OR REPLACE TRIGGER " . $trigger_name;
d7444bfc 137 $trigger.= "\n BEFORE INSERT";
138 $trigger.= "\nON " . $this->getEncQuoted($this->prefix . $xmldb_table->getName());
139 $trigger.= "\n FOR EACH ROW";
140 $trigger.= "\nBEGIN";
b8851b80 141 $trigger.= "\n IF :old." . $this->getEncQuoted($xmldb_field->getName()) . ' IS NOT NULL THEN';
142 $trigger.= "\n SELECT " . $sequence_name . '.nextval INTO :new.' . $this->getEncQuoted($xmldb_field->getName()) . " FROM dual;";
143 $trigger.= "\n END IF;";
0513f3bf 144 $trigger.= "\nEND;";
9dcc6300 145 return array($sequence, $trigger);
d7444bfc 146 }
147
148 /**
9dcc6300 149 * Returns the code (in array) needed to add one comment to the table
d7444bfc 150 */
151 function getCommentSQL ($xmldb_table) {
152
9dcc6300 153 $comment = "COMMENT ON TABLE " . $this->getEncQuoted($this->prefix . $xmldb_table->getName());
d7444bfc 154 $comment.= " IS '" . substr($xmldb_table->getComment(), 0, 250) . "'";
155
9dcc6300 156 return array($comment);
d7444bfc 157 }
158
159 /**
160 * Returns an array of reserved words (lowercase) for this DB
161 */
162 function getReservedWords() {
6aa7885e 163 /// This file contains the reserved words for Oracle databases
164 /// from http://download-uk.oracle.com/docs/cd/B10501_01/server.920/a96540/ap_keywd.htm
d7444bfc 165 $reserved_words = array (
166 'access', 'add', 'all', 'alter', 'and', 'any',
167 'as', 'asc', 'audit', 'between', 'by', 'char',
168 'check', 'cluster', 'column', 'comment',
169 'compress', 'connect', 'create', 'current',
170 'date', 'decimal', 'default', 'delete', 'desc',
171 'distinct', 'drop', 'else', 'exclusive', 'exists',
172 'file', 'float', 'for', 'from', 'grant', 'group',
173 'having', 'identified', 'immediate', 'in',
174 'increment', 'index', 'initial', 'insert',
175 'integer', 'intersect', 'into', 'is', 'level',
176 'like', 'lock', 'long', 'maxextents', 'minus',
177 'mlslabel', 'mode', 'modify', 'noaudit',
178 'nocompress', 'not', 'nowait', 'null', 'number',
179 'of', 'offline', 'on', 'online', 'option', 'or',
180 'order', 'pctfree', 'prior', 'privileges',
181 'public', 'raw', 'rename', 'resource', 'revoke',
182 'row', 'rowid', 'rownum', 'rows', 'select',
183 'session', 'set', 'share', 'size', 'smallint',
184 'start', 'successful', 'synonym', 'sysdate',
185 'table', 'then', 'to', 'trigger', 'uid', 'union',
186 'unique', 'update', 'user', 'validate', 'values',
187 'varchar', 'varchar2', 'view', 'whenever',
188 'where', 'with'
189 );
190 return $reserved_words;
191 }
192}
193
194?>