Initial commit of oci8po generator
[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
35 var $number_type = 'NUMBER'; // Proper type for NUMBER(x) in this DB
36
37 var $unsigned_allowed = false; // To define in the generator must handle unsigned information
38 var $default_for_char = ''; // To define the default to set for NOT NULLs CHARs without default (null=do nothing)
39
40 var $foreign_keys = false; // Does the generator build foreign keys
41
42 var $primary_index = false;// Does the generator need to build one index for primary keys
43 var $unique_index = true; // Does the generator need to build one index for unique keys
44 var $foreign_index = true; // Does the generator need to build one index for foreign keys
45
46 var $sequence_extra_code = true; //Does the generator need to add extra code to generate the sequence fields
47 var $sequence_name = ''; //Particular name for inline sequences in this generator
48
49 var $enum_inline_code = false; //Does the generator need to add inline code in the column definition
50
51 /**
52 * Creates one new XMLDBpostgres7
53 */
54 function XMLDBoci8po() {
55 parent::XMLDBgenerator();
56 $this->prefix = '';
57 $this->reserved_words = $this->getReservedWords();
58 }
59
60 /**
61 * Given one XMLDB Type, lenght and decimals, returns the DB proper SQL type
62 */
63 function getTypeSQL ($xmldb_type, $xmldb_length=null, $xmldb_decimals=null) {
64
65 switch ($xmldb_type) {
66 case XMLDB_TYPE_INTEGER: // From http://www.postgresql.org/docs/7.4/interactive/datatype.html
67 if (empty($xmldb_length)) {
68 $xmldb_length = 10;
69 }
70 $dbtype = 'NUMBER(' . $xmldb_length . ')';
71 break;
72 case XMLDB_TYPE_NUMBER:
73 $dbtype = $this->number_type;
74 if (!empty($xmldb_length)) {
75 $dbtype .= '(' . $xmldb_length;
76 if (!empty($xmldb_decimals)) {
77 $dbtype .= ',' . $xmldb_decimals;
78 }
79 $dbtype .= ')';
80 }
81 break;
82 case XMLDB_TYPE_FLOAT:
83 $dbtype = 'NUMBER';
84 break;
85 case XMLDB_TYPE_CHAR:
86 $dbtype = 'VARCHAR2';
87 if (empty($xmldb_length)) {
88 $xmldb_length='255';
89 }
90 $dbtype .= '(' . $xmldb_length . ')';
91 break;
92 case XMLDB_TYPE_TEXT:
93 $dbtype = 'CLOB';
94 break;
95 case XMLDB_TYPE_BINARY:
96 $dbtype = 'BLOB';
97 break;
98 case XMLDB_TYPE_DATETIME:
99 $dbtype = 'DATE';
100 break;
101 }
102 return $dbtype;
103 }
104
105 /**
106 * Returns the code needed to create one enum for the xmldb_table and xmldb_field passes
107 */
108 function getEnumExtraSQL ($xmldb_table, $xmldb_field) {
109
110 $sql = 'CONSTRAINT ' . $this->getNameForObject($xmldb_table->getName(), $xmldb_field->getName(), 'ck');
111 $sql.= ' CHECK (' . $this->getEncQuoted($xmldb_field->getName()) . ' IN (' . implode(', ', $xmldb_field->getEnumValues()) . ')),';
112
113 return $sql;
114 }
115
116 /**
117 * Returns the code needed to create one sequence for the xmldb_table and xmldb_field passes
118 */
119 function getCreateSequenceSQL ($xmldb_table, $xmldb_field) {
120
121 $sequence = "\nCREATE SEQUENCE ";
122 $sequence.= $this->getNameForObject($xmldb_table->getName(), $xmldb_field->getName(), 'seq');
123 $sequence.= "\n START WITH 1";
124 $sequence.= "\n INCREMENT BY 1";
125 $sequence.= "\n NOMAXVALUE;";
126
127 $trigger_name = $this->getNameForObject($xmldb_table->getName(), $xmldb_field->getName(), 'trg');
128
129 $trigger = "\nCREATE OR REPLACE TRIGGER " . $trigger_name;
130 $trigger.= "\n BEFORE INSERT";
131 $trigger.= "\nON " . $this->getEncQuoted($this->prefix . $xmldb_table->getName());
132 $trigger.= "\n FOR EACH ROW";
133 $trigger.= "\nBEGIN";
134 $trigger.= "\n SELECT " . $trigger_name . '.nextval INTO :new.' . $this->getEncQuoted($xmldb_field->getName()) . " FROM dual;";
135 $trigger.= "\nEND;";
136 return $sequence . "\n" . $trigger;
137 }
138
139 /**
140 * Returns the code needed to add one comment to the table
141 */
142 function getCommentSQL ($xmldb_table) {
143
144 $comment = ";\n\nCOMMENT ON TABLE " . $this->getEncQuoted($this->prefix . $xmldb_table->getName());
145 $comment.= " IS '" . substr($xmldb_table->getComment(), 0, 250) . "'";
146
147 return $comment;
148 }
149
150 /**
151 * Returns an array of reserved words (lowercase) for this DB
152 */
153 function getReservedWords() {
154 /// This file contains the reserved words for MySQL databases
155 /// from http://www.postgresql.org/docs/7.3/static/sql-keywords-appendix.html
156 $reserved_words = array (
157 'access', 'add', 'all', 'alter', 'and', 'any',
158 'as', 'asc', 'audit', 'between', 'by', 'char',
159 'check', 'cluster', 'column', 'comment',
160 'compress', 'connect', 'create', 'current',
161 'date', 'decimal', 'default', 'delete', 'desc',
162 'distinct', 'drop', 'else', 'exclusive', 'exists',
163 'file', 'float', 'for', 'from', 'grant', 'group',
164 'having', 'identified', 'immediate', 'in',
165 'increment', 'index', 'initial', 'insert',
166 'integer', 'intersect', 'into', 'is', 'level',
167 'like', 'lock', 'long', 'maxextents', 'minus',
168 'mlslabel', 'mode', 'modify', 'noaudit',
169 'nocompress', 'not', 'nowait', 'null', 'number',
170 'of', 'offline', 'on', 'online', 'option', 'or',
171 'order', 'pctfree', 'prior', 'privileges',
172 'public', 'raw', 'rename', 'resource', 'revoke',
173 'row', 'rowid', 'rownum', 'rows', 'select',
174 'session', 'set', 'share', 'size', 'smallint',
175 'start', 'successful', 'synonym', 'sysdate',
176 'table', 'then', 'to', 'trigger', 'uid', 'union',
177 'unique', 'update', 'user', 'validate', 'values',
178 'varchar', 'varchar2', 'view', 'whenever',
179 'where', 'with'
180 );
181 return $reserved_words;
182 }
183}
184
185?>