forked from moodle/moodle
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathddllib.php
393 lines (320 loc) · 14.4 KB
/
ddllib.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
<?php // $Id$
///////////////////////////////////////////////////////////////////////////
// //
// NOTICE OF COPYRIGHT //
// //
// Moodle - Modular Object-Oriented Dynamic Learning Environment //
// http://moodle.com //
// //
// Copyright (C) 2001-3001 Martin Dougiamas http://dougiamas.com //
// (C) 2001-3001 Eloy Lafuente (stronk7) http://contiento.com //
// //
// This program is free software; you can redistribute it and/or modify //
// it under the terms of the GNU General Public License as published by //
// the Free Software Foundation; either version 2 of the License, or //
// (at your option) any later version. //
// //
// This program is distributed in the hope that it will be useful, //
// but WITHOUT ANY WARRANTY; without even the implied warranty of //
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the //
// GNU General Public License for more details: //
// //
// http://www.gnu.org/copyleft/gpl.html //
// //
///////////////////////////////////////////////////////////////////////////
// This library includes all the required functions used to handle the DB
// structure (DDL) independently of the underlying RDBMS in use. All the functions
// rely on the XMLDBDriver classes to be able to generate the correct SQL
// syntax needed by each DB.
//
// To define any structure to be created we'll use the schema defined
// by the XMLDB classes, for tables, fields, indexes, keys and other
// statements instead of direct handling of SQL sentences.
//
// This library should be used, exclusively, by the installation and
// upgrade process of Moodle.
//
// For further documentation, visit http://docs.moodle.org/en/DDL_functions
/// Add required XMLDB constants
require_once($CFG->libdir . '/xmldb/classes/XMLDBConstants.php');
/// Add main XMLDB Generator
require_once($CFG->libdir . '/xmldb/classes/generators/XMLDBGenerator.class.php');
/// Add required XMLDB DB classes
require_once($CFG->libdir . '/xmldb/classes/XMLDBObject.class.php');
require_once($CFG->libdir . '/xmldb/classes/XMLDBFile.class.php');
require_once($CFG->libdir . '/xmldb/classes/XMLDBStructure.class.php');
require_once($CFG->libdir . '/xmldb/classes/XMLDBTable.class.php');
require_once($CFG->libdir . '/xmldb/classes/XMLDBField.class.php');
require_once($CFG->libdir . '/xmldb/classes/XMLDBKey.class.php');
require_once($CFG->libdir . '/xmldb/classes/XMLDBIndex.class.php');
require_once($CFG->libdir . '/xmldb/classes/XMLDBStatement.class.php');
/// Based on $CFG->dbtype, add the proper generator class
require_once($CFG->libdir . '/xmldb/classes/generators/' . $CFG->dbtype . '/' . $CFG->dbtype . '.class.php');
/// Add other libraries
require_once($CFG->libdir . '/xmlize.php');
/**
* Add a new field to a table, or modify an existing one (if oldfield is defined).
* Warning: Please be careful on primary keys, as this function will eat auto_increments
*
* @uses $CFG
* @uses $db
* @param string $table the name of the table to modify. (Without the prefix.)
* @param string $oldfield If changing an existing column, the name of that column.
* @param string $field The name of the column at the end of the operation.
* @param string $type The type of the column at the end of the operation. TEXT, VARCHAR, CHAR, INTEGER, REAL, or TINYINT
* @param string $size The size of that column type. As in VARCHAR($size), or INTEGER($size).
* @param string $signed For numeric column types, whether that column is 'signed' or 'unsigned'.
* @param string $default The new default value for the column.
* @param string $null 'not null', or '' to allow nulls.
* @param string $after Which column to insert this one after. Not supported on Postgres.
*
* @return boolean Wheter the operation succeeded.
*/
function table_column($table, $oldfield, $field, $type='integer', $size='10',
$signed='unsigned', $default='0', $null='not null', $after='') {
global $CFG, $db, $empty_rs_cache;
if (!empty($empty_rs_cache[$table])) { // Clear the recordset cache because it's out of date
unset($empty_rs_cache[$table]);
}
switch (strtolower($CFG->dbtype)) {
case 'mysql':
case 'mysqlt':
switch (strtolower($type)) {
case 'text':
$type = 'TEXT';
$signed = '';
break;
case 'integer':
$type = 'INTEGER('. $size .')';
break;
case 'varchar':
$type = 'VARCHAR('. $size .')';
$signed = '';
break;
case 'char':
$type = 'CHAR('. $size .')';
$signed = '';
break;
}
if (!empty($oldfield)) {
$operation = 'CHANGE '. $oldfield .' '. $field;
} else {
$operation = 'ADD '. $field;
}
$default = 'DEFAULT \''. $default .'\'';
if (!empty($after)) {
$after = 'AFTER `'. $after .'`';
}
return execute_sql('ALTER TABLE '. $CFG->prefix . $table .' '. $operation .' '. $type .' '. $signed .' '. $default .' '. $null .' '. $after);
case 'postgres7': // From Petri Asikainen
//Check db-version
$dbinfo = $db->ServerInfo();
$dbver = substr($dbinfo['version'],0,3);
//to prevent conflicts with reserved words
$realfield = '"'. $field .'"';
$field = '"'. $field .'_alter_column_tmp"';
$oldfield = '"'. $oldfield .'"';
switch (strtolower($type)) {
case 'tinyint':
case 'integer':
if ($size <= 4) {
$type = 'INT2';
}
if ($size <= 10) {
$type = 'INT';
}
if ($size > 10) {
$type = 'INT8';
}
break;
case 'varchar':
$type = 'VARCHAR('. $size .')';
break;
case 'char':
$type = 'CHAR('. $size .')';
$signed = '';
break;
}
$default = '\''. $default .'\'';
//After is not implemented in postgesql
//if (!empty($after)) {
// $after = "AFTER '$after'";
//}
//Use transactions
execute_sql('BEGIN');
//Always use temporary column
execute_sql('ALTER TABLE '. $CFG->prefix . $table .' ADD COLUMN '. $field .' '. $type);
//Add default values
execute_sql('UPDATE '. $CFG->prefix . $table .' SET '. $field .'='. $default);
if ($dbver >= '7.3') {
// modifying 'not null' is posible before 7.3
//update default values to table
if (strtoupper($null) == 'NOT NULL') {
execute_sql('UPDATE '. $CFG->prefix . $table .' SET '. $field .'='. $default .' WHERE '. $field .' IS NULL');
execute_sql('ALTER TABLE '. $CFG->prefix . $table .' ALTER COLUMN '. $field .' SET '. $null);
} else {
execute_sql('ALTER TABLE '. $CFG->prefix . $table .' ALTER COLUMN '. $field .' DROP NOT NULL');
}
}
execute_sql('ALTER TABLE '. $CFG->prefix . $table .' ALTER COLUMN '. $field .' SET DEFAULT '. $default);
if ( $oldfield != '""' ) {
// We are changing the type of a column. This may require doing some casts...
$casting = '';
$oldtype = column_type($table, $oldfield);
$newtype = column_type($table, $field);
// Do we need a cast?
if($newtype == 'N' && $oldtype == 'C') {
$casting = 'CAST(CAST('.$oldfield.' AS TEXT) AS REAL)';
}
else if($newtype == 'I' && $oldtype == 'C') {
$casting = 'CAST(CAST('.$oldfield.' AS TEXT) AS INTEGER)';
}
else {
$casting = $oldfield;
}
// Run the update query, casting as necessary
execute_sql('UPDATE '. $CFG->prefix . $table .' SET '. $field .' = '. $casting);
execute_sql('ALTER TABLE '. $CFG->prefix . $table .' DROP COLUMN '. $oldfield);
}
execute_sql('ALTER TABLE '. $CFG->prefix . $table .' RENAME COLUMN '. $field .' TO '. $realfield);
return execute_sql('COMMIT');
default:
switch (strtolower($type)) {
case 'integer':
$type = 'INTEGER';
break;
case 'varchar':
$type = 'VARCHAR';
break;
}
$default = 'DEFAULT \''. $default .'\'';
if (!empty($after)) {
$after = 'AFTER '. $after;
}
if (!empty($oldfield)) {
execute_sql('ALTER TABLE '. $CFG->prefix . $table .' RENAME COLUMN '. $oldfield .' '. $field);
} else {
execute_sql('ALTER TABLE '. $CFG->prefix . $table .' ADD COLUMN '. $field .' '. $type);
}
execute_sql('ALTER TABLE '. $CFG->prefix . $table .' ALTER COLUMN '. $field .' SET '. $null);
return execute_sql('ALTER TABLE '. $CFG->prefix . $table .' ALTER COLUMN '. $field .' SET '. $default);
}
}
/**
* This function will load one entire XMLDB file, generating all the needed
* SQL statements, specific for each RDBMS ($CFG->dbtype) and, finally, it
* will execute all those statements against the DB.
*
* @uses $CFG, $db
* @param $file full path to the XML file to be used
* @return boolean (true on success, false on error)
*/
function install_from_xmldb_file($file) {
global $CFG, $db;
$status = true;
$xmldb_file = new XMLDBFile($file);
if (!$xmldb_file->fileExists()) {
return false;
}
$loaded = $xmldb_file->loadXMLStructure();
if (!$loaded || !$xmldb_file->isLoaded()) {
return false;
}
$structure = $xmldb_file->getStructure();
if (!$sqlarr = $structure->getCreateStructureSQL($CFG->dbtype, $CFG->prefix, false)) {
return false;
}
return execute_sql_arr($sqlarr);
}
/**
* This function will create the table passed as argument with all its
* fields/keys/indexes/sequences, everything based in the XMLDB object
*
* @uses $CFG, $db
* @param XMLDBTable table object (full specs are required)
* @param boolean continue to specify if must continue on error (true) or stop (false)
* @param boolean feedback to specify to show status info (true) or not (false)
* @return boolean true on success, false on error
*/
function create_table($table, $continue=true, $feedback=true) {
global $CFG, $db;
$status = true;
if (strtolower(get_class($table)) != 'xmldbtable') {
return false;
}
if(!$sqlarr = $table->getCreateTableSQL($CFG->dbtype, $CFG->prefix, false)) {
return false;
}
return execute_sql_arr($sqlarr, $continue, $feedback);
}
/**
* This function will drop the table passed as argument
* and all the associated objects (keys, indexes, constaints, sequences, triggers)
* will be dropped too.
*
* @uses $CFG, $db
* @param XMLDBTable table object (just the name is mandatory)
* @param boolean continue to specify if must continue on error (true) or stop (false)
* @param boolean feedback to specify to show status info (true) or not (false)
* @return boolean true on success, false on error
*/
function drop_table($table, $continue=true, $feedback=true) {
global $CFG, $db;
$status = true;
if (strtolower(get_class($table)) != 'xmldbtable') {
return false;
}
if(!$sqlarr = $table->getDropTableSQL($CFG->dbtype, $CFG->prefix, false)) {
return false;
}
return execute_sql_arr($sqlarr, $continue, $feedback);
}
/**
* This function will add the field to the table passed as arguments
*
* @uses $CFG, $db
* @param XMLDBTable table object (just the name is mandatory)
* @param XMLDBField field object (full specs are required)
* @param boolean continue to specify if must continue on error (true) or stop (false)
* @param boolean feedback to specify to show status info (true) or not (false)
* @return boolean true on success, false on error
*/
function add_field($table, $field, $continue=true, $feedback=true) {
global $CFG, $db;
$status = true;
if (strtolower(get_class($table)) != 'xmldbtable') {
return false;
}
if (strtolower(get_class($field)) != 'xmldbfield') {
return false;
}
if(!$sqlarr = $table->getAddFieldSQL($CFG->dbtype, $CFG->prefix, $field, false)) {
return false;
}
return execute_sql_arr($sqlarr, $continue, $feedback);
}
/**
* This function will drop the field from the table passed as arguments
*
* @uses $CFG, $db
* @param XMLDBTable table object (just the name is mandatory)
* @param XMLDBField field object (just the name is mandatory)
* @param boolean continue to specify if must continue on error (true) or stop (false)
* @param boolean feedback to specify to show status info (true) or not (false)
* @return boolean true on success, false on error
*/
function drop_field($table, $field, $continue=true, $feedback=true) {
global $CFG, $db;
$status = true;
if (strtolower(get_class($table)) != 'xmldbtable') {
return false;
}
if (strtolower(get_class($field)) != 'xmldbfield') {
return false;
}
if(!$sqlarr = $table->getDropFieldSQL($CFG->dbtype, $CFG->prefix, $field, false)) {
return false;
}
return execute_sql_arr($sqlarr, $continue, $feedback);
}
?>