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