This is a demo of the experimental formslib. It is used here to print and process...
[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
b0ec41af 46 var $unique_keys = false; // Does the generator build unique keys
d7444bfc 47 var $foreign_keys = false; // Does the generator build foreign keys
48
49 var $primary_index = false;// Does the generator need to build one index for primary keys
b0ec41af 50 var $unique_index = true; // Does the generator need to build one index for unique keys
d7444bfc 51 var $foreign_index = true; // Does the generator need to build one index for foreign keys
52
53 var $sequence_extra_code = true; //Does the generator need to add extra code to generate the sequence fields
54 var $sequence_name = ''; //Particular name for inline sequences in this generator
55
3a8c55c3 56 var $drop_table_extra_code = true; //Does the generatos need to add code after table drop
57
d7444bfc 58 var $enum_inline_code = false; //Does the generator need to add inline code in the column definition
59
19c8321e 60 var $alter_column_sql = 'ALTER TABLE TABLENAME MODIFY (COLUMNSPECS)'; //The SQL template to alter columns
61
d7444bfc 62 /**
63 * Creates one new XMLDBpostgres7
64 */
65 function XMLDBoci8po() {
66 parent::XMLDBgenerator();
67 $this->prefix = '';
68 $this->reserved_words = $this->getReservedWords();
69 }
70
71 /**
72 * Given one XMLDB Type, lenght and decimals, returns the DB proper SQL type
73 */
74 function getTypeSQL ($xmldb_type, $xmldb_length=null, $xmldb_decimals=null) {
75
76 switch ($xmldb_type) {
77 case XMLDB_TYPE_INTEGER: // From http://www.postgresql.org/docs/7.4/interactive/datatype.html
78 if (empty($xmldb_length)) {
79 $xmldb_length = 10;
80 }
81 $dbtype = 'NUMBER(' . $xmldb_length . ')';
82 break;
83 case XMLDB_TYPE_NUMBER:
84 $dbtype = $this->number_type;
4782a1f8 85 /// 38 is the max allowed
86 if ($xmldb_length > 38) {
87 $xmldb_length = 38;
88 }
d7444bfc 89 if (!empty($xmldb_length)) {
90 $dbtype .= '(' . $xmldb_length;
91 if (!empty($xmldb_decimals)) {
92 $dbtype .= ',' . $xmldb_decimals;
93 }
94 $dbtype .= ')';
95 }
96 break;
97 case XMLDB_TYPE_FLOAT:
98 $dbtype = 'NUMBER';
99 break;
100 case XMLDB_TYPE_CHAR:
101 $dbtype = 'VARCHAR2';
102 if (empty($xmldb_length)) {
103 $xmldb_length='255';
104 }
105 $dbtype .= '(' . $xmldb_length . ')';
106 break;
107 case XMLDB_TYPE_TEXT:
108 $dbtype = 'CLOB';
109 break;
110 case XMLDB_TYPE_BINARY:
111 $dbtype = 'BLOB';
112 break;
113 case XMLDB_TYPE_DATETIME:
114 $dbtype = 'DATE';
115 break;
116 }
117 return $dbtype;
118 }
119
eef868d1 120 /**
d7444bfc 121 * Returns the code needed to create one enum for the xmldb_table and xmldb_field passes
eef868d1 122 */
d7444bfc 123 function getEnumExtraSQL ($xmldb_table, $xmldb_field) {
eef868d1 124
d7444bfc 125 $sql = 'CONSTRAINT ' . $this->getNameForObject($xmldb_table->getName(), $xmldb_field->getName(), 'ck');
0dd87cfa 126 $sql.= ' CHECK (' . $this->getEncQuoted($xmldb_field->getName()) . ' IN (' . implode(', ', $xmldb_field->getEnumValues()) . '))';
d7444bfc 127
128 return $sql;
129 }
130
131 /**
132 * Returns the code needed to create one sequence for the xmldb_table and xmldb_field passes
133 */
134 function getCreateSequenceSQL ($xmldb_table, $xmldb_field) {
135
465a8029 136 $sequence_name = $this->getNameForObject($xmldb_table->getName(), $xmldb_field->getName(), 'seq');
eef868d1 137
465a8029 138 $sequence = "CREATE SEQUENCE " . $sequence_name;
d7444bfc 139 $sequence.= "\n START WITH 1";
140 $sequence.= "\n INCREMENT BY 1";
9dcc6300 141 $sequence.= "\n NOMAXVALUE";
d7444bfc 142
143 $trigger_name = $this->getNameForObject($xmldb_table->getName(), $xmldb_field->getName(), 'trg');
eef868d1 144
9dcc6300 145 $trigger = "CREATE OR REPLACE TRIGGER " . $trigger_name;
d7444bfc 146 $trigger.= "\n BEFORE INSERT";
147 $trigger.= "\nON " . $this->getEncQuoted($this->prefix . $xmldb_table->getName());
148 $trigger.= "\n FOR EACH ROW";
149 $trigger.= "\nBEGIN";
4782a1f8 150 $trigger.= "\n IF :new." . $this->getEncQuoted($xmldb_field->getName()) . ' IS NULL THEN';
b8851b80 151 $trigger.= "\n SELECT " . $sequence_name . '.nextval INTO :new.' . $this->getEncQuoted($xmldb_field->getName()) . " FROM dual;";
152 $trigger.= "\n END IF;";
0513f3bf 153 $trigger.= "\nEND;";
9dcc6300 154 return array($sequence, $trigger);
d7444bfc 155 }
156
3a8c55c3 157 /**
158 * Returns the code needed to drop one sequence for the xmldb_table and xmldb_field passed
159 * Can, optionally, specify if the underlying trigger will be also dropped
160 */
161 function getDropSequenceSQL ($xmldb_table, $xmldb_field, $include_trigger=false) {
162
163 $sequence_name = $this->getNameForObject($xmldb_table->getName(), $xmldb_field->getName(), 'seq');
eef868d1 164
3a8c55c3 165 $sequence = "DROP SEQUENCE " . $sequence_name;
166
167 $trigger_name = $this->getNameForObject($xmldb_table->getName(), $xmldb_field->getName(), 'trg');
eef868d1 168
3a8c55c3 169 $trigger = "DROP TRIGGER " . $trigger_name;
170
171 if ($include_trigger) {
172 $result = array($sequence, $trigger);
173 } else {
174 $result = array($sequence);
175 }
176 return $result;
177 }
178
179 /**
180 * Returns the code (in array) needed to add one comment to the table
181 */
182 function getCommentSQL ($xmldb_table) {
183
184 $comment = "COMMENT ON TABLE " . $this->getEncQuoted($this->prefix . $xmldb_table->getName());
185 $comment.= " IS '" . substr($xmldb_table->getComment(), 0, 250) . "'";
d7444bfc 186
3a8c55c3 187 return array($comment);
188 }
d7444bfc 189
3a8c55c3 190 /**
191 * Returns the code (array of statements) needed to execute extra statements on table drop
192 */
193 function getDropTableExtraSQL ($xmldb_table) {
194 $xmldb_field = new XMLDBField('id'); // Fields having sequences should be exclusively, id.
195 return $this->getDropSequenceSQL($xmldb_table, $xmldb_field, false);
196 }
d7444bfc 197
19c8321e 198 /**
199 * Given one XMLDBTable and one XMLDBField, return the SQL statements needded to alter the field in the table
200 * Oracle has some severe limits:
201 * - clob and blob fields doesn't allow type to be specified
202 * - error is dropped if the null/not null clause is specified and hasn't changed
203 */
204 function getAlterFieldSQL($xmldb_table, $xmldb_field) {
205
206 global $db;
207
208 /// Get the quoted name of the table and field
209 $tablename = $this->getEncQuoted($this->prefix . $xmldb_table->getName());
210 $fieldname = $this->getEncQuoted($xmldb_field->getName());
211
212 /// Take a look to field metadata
213 $meta = array_change_key_case($db->MetaColumns($tablename));
214 $metac = $meta[$fieldname];
215 $oldtype = strtolower($metac->type);
216 $oldlength = $metac->max_length;
217 $olddecimals = empty($metac->scale) ? null : $metac->scale;
218 $oldnotnull = empty($metac->not_null) ? false : $metac->not_null;
219 $olddefault = empty($metac->default_value) ? null : $metac->default_value;
220
221 $islob = false;
222
223 /// If field is CLOB and new one is also XMLDB_TYPE_TEXT or
224 /// if fiels is BLOB and new one is also XMLDB_TYPE_BINARY
225 /// prevent type to be specified, so only NULL and DEFAULT clauses are allowed
226 if (($oldtype = 'clob' && $xmldb_field->getType() == XMLDB_TYPE_TEXT) ||
227 ($oldtype = 'blob' && $xmldb_field->getType() == XMLDB_TYPE_BINARY)) {
228 $this->alter_column_skip_type = true;
229 $islob = true;
230 }
231
232 /// If field is NOT NULL and the new one too or
233 /// if field is NULL and the new one too
234 /// prevent null clause to be specified
235 if (($oldnotnull && $xmldb_field->getNotnull()) ||
236 (!$oldnotnull && !$xmldb_field->getNotnull())) {
237 $this->alter_column_skip_notnull = true;
238 }
239
240 /// In the rest of cases, use the general generator
241 return parent::getAlterFieldSQL($xmldb_table, $xmldb_field);
242 }
243
d7444bfc 244 /**
245 * Returns an array of reserved words (lowercase) for this DB
246 */
247 function getReservedWords() {
6aa7885e 248 /// This file contains the reserved words for Oracle databases
249 /// from http://download-uk.oracle.com/docs/cd/B10501_01/server.920/a96540/ap_keywd.htm
d7444bfc 250 $reserved_words = array (
251 'access', 'add', 'all', 'alter', 'and', 'any',
252 'as', 'asc', 'audit', 'between', 'by', 'char',
253 'check', 'cluster', 'column', 'comment',
254 'compress', 'connect', 'create', 'current',
255 'date', 'decimal', 'default', 'delete', 'desc',
256 'distinct', 'drop', 'else', 'exclusive', 'exists',
257 'file', 'float', 'for', 'from', 'grant', 'group',
258 'having', 'identified', 'immediate', 'in',
259 'increment', 'index', 'initial', 'insert',
260 'integer', 'intersect', 'into', 'is', 'level',
261 'like', 'lock', 'long', 'maxextents', 'minus',
262 'mlslabel', 'mode', 'modify', 'noaudit',
263 'nocompress', 'not', 'nowait', 'null', 'number',
264 'of', 'offline', 'on', 'online', 'option', 'or',
265 'order', 'pctfree', 'prior', 'privileges',
266 'public', 'raw', 'rename', 'resource', 'revoke',
267 'row', 'rowid', 'rownum', 'rows', 'select',
268 'session', 'set', 'share', 'size', 'smallint',
269 'start', 'successful', 'synonym', 'sysdate',
270 'table', 'then', 'to', 'trigger', 'uid', 'union',
271 'unique', 'update', 'user', 'validate', 'values',
272 'varchar', 'varchar2', 'view', 'whenever',
273 'where', 'with'
eef868d1 274 );
d7444bfc 275 return $reserved_words;
276 }
277}
278
279?>