Initial support for DROP TABLE adedd.
[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;
4782a1f8 80 /// 38 is the max allowed
81 if ($xmldb_length > 38) {
82 $xmldb_length = 38;
83 }
d7444bfc 84 if (!empty($xmldb_length)) {
85 $dbtype .= '(' . $xmldb_length;
86 if (!empty($xmldb_decimals)) {
87 $dbtype .= ',' . $xmldb_decimals;
88 }
89 $dbtype .= ')';
90 }
91 break;
92 case XMLDB_TYPE_FLOAT:
93 $dbtype = 'NUMBER';
94 break;
95 case XMLDB_TYPE_CHAR:
96 $dbtype = 'VARCHAR2';
97 if (empty($xmldb_length)) {
98 $xmldb_length='255';
99 }
100 $dbtype .= '(' . $xmldb_length . ')';
101 break;
102 case XMLDB_TYPE_TEXT:
103 $dbtype = 'CLOB';
104 break;
105 case XMLDB_TYPE_BINARY:
106 $dbtype = 'BLOB';
107 break;
108 case XMLDB_TYPE_DATETIME:
109 $dbtype = 'DATE';
110 break;
111 }
112 return $dbtype;
113 }
114
115 /**
116 * Returns the code needed to create one enum for the xmldb_table and xmldb_field passes
117 */
118 function getEnumExtraSQL ($xmldb_table, $xmldb_field) {
119
120 $sql = 'CONSTRAINT ' . $this->getNameForObject($xmldb_table->getName(), $xmldb_field->getName(), 'ck');
121 $sql.= ' CHECK (' . $this->getEncQuoted($xmldb_field->getName()) . ' IN (' . implode(', ', $xmldb_field->getEnumValues()) . ')),';
122
123 return $sql;
124 }
125
126 /**
127 * Returns the code needed to create one sequence for the xmldb_table and xmldb_field passes
128 */
129 function getCreateSequenceSQL ($xmldb_table, $xmldb_field) {
130
465a8029 131 $sequence_name = $this->getNameForObject($xmldb_table->getName(), $xmldb_field->getName(), 'seq');
132
133 $sequence = "CREATE SEQUENCE " . $sequence_name;
d7444bfc 134 $sequence.= "\n START WITH 1";
135 $sequence.= "\n INCREMENT BY 1";
9dcc6300 136 $sequence.= "\n NOMAXVALUE";
d7444bfc 137
138 $trigger_name = $this->getNameForObject($xmldb_table->getName(), $xmldb_field->getName(), 'trg');
139
9dcc6300 140 $trigger = "CREATE OR REPLACE TRIGGER " . $trigger_name;
d7444bfc 141 $trigger.= "\n BEFORE INSERT";
142 $trigger.= "\nON " . $this->getEncQuoted($this->prefix . $xmldb_table->getName());
143 $trigger.= "\n FOR EACH ROW";
144 $trigger.= "\nBEGIN";
4782a1f8 145 $trigger.= "\n IF :new." . $this->getEncQuoted($xmldb_field->getName()) . ' IS NULL THEN';
b8851b80 146 $trigger.= "\n SELECT " . $sequence_name . '.nextval INTO :new.' . $this->getEncQuoted($xmldb_field->getName()) . " FROM dual;";
147 $trigger.= "\n END IF;";
0513f3bf 148 $trigger.= "\nEND;";
9dcc6300 149 return array($sequence, $trigger);
d7444bfc 150 }
151
152 /**
9dcc6300 153 * Returns the code (in array) needed to add one comment to the table
d7444bfc 154 */
155 function getCommentSQL ($xmldb_table) {
156
9dcc6300 157 $comment = "COMMENT ON TABLE " . $this->getEncQuoted($this->prefix . $xmldb_table->getName());
d7444bfc 158 $comment.= " IS '" . substr($xmldb_table->getComment(), 0, 250) . "'";
159
9dcc6300 160 return array($comment);
d7444bfc 161 }
162
163 /**
164 * Returns an array of reserved words (lowercase) for this DB
165 */
166 function getReservedWords() {
6aa7885e 167 /// This file contains the reserved words for Oracle databases
168 /// from http://download-uk.oracle.com/docs/cd/B10501_01/server.920/a96540/ap_keywd.htm
d7444bfc 169 $reserved_words = array (
170 'access', 'add', 'all', 'alter', 'and', 'any',
171 'as', 'asc', 'audit', 'between', 'by', 'char',
172 'check', 'cluster', 'column', 'comment',
173 'compress', 'connect', 'create', 'current',
174 'date', 'decimal', 'default', 'delete', 'desc',
175 'distinct', 'drop', 'else', 'exclusive', 'exists',
176 'file', 'float', 'for', 'from', 'grant', 'group',
177 'having', 'identified', 'immediate', 'in',
178 'increment', 'index', 'initial', 'insert',
179 'integer', 'intersect', 'into', 'is', 'level',
180 'like', 'lock', 'long', 'maxextents', 'minus',
181 'mlslabel', 'mode', 'modify', 'noaudit',
182 'nocompress', 'not', 'nowait', 'null', 'number',
183 'of', 'offline', 'on', 'online', 'option', 'or',
184 'order', 'pctfree', 'prior', 'privileges',
185 'public', 'raw', 'rename', 'resource', 'revoke',
186 'row', 'rowid', 'rownum', 'rows', 'select',
187 'session', 'set', 'share', 'size', 'smallint',
188 'start', 'successful', 'synonym', 'sysdate',
189 'table', 'then', 'to', 'trigger', 'uid', 'union',
190 'unique', 'update', 'user', 'validate', 'values',
191 'varchar', 'varchar2', 'view', 'whenever',
192 'where', 'with'
193 );
194 return $reserved_words;
195 }
196}
197
198?>