Moodle  2.2.1
http://www.collinsharper.com
C:/xampp/htdocs/moodle/lib/xmldb/xmldb_index.php
Go to the documentation of this file.
00001 <?php
00002 
00004 //                                                                       //
00005 // NOTICE OF COPYRIGHT                                                   //
00006 //                                                                       //
00007 // Moodle - Modular Object-Oriented Dynamic Learning Environment         //
00008 //          http://moodle.com                                            //
00009 //                                                                       //
00010 // Copyright (C) 1999 onwards Martin Dougiamas     http://dougiamas.com  //
00011 //           (C) 2001-3001 Eloy Lafuente (stronk7) http://contiento.com  //
00012 //                                                                       //
00013 // This program is free software; you can redistribute it and/or modify  //
00014 // it under the terms of the GNU General Public License as published by  //
00015 // the Free Software Foundation; either version 2 of the License, or     //
00016 // (at your option) any later version.                                   //
00017 //                                                                       //
00018 // This program is distributed in the hope that it will be useful,       //
00019 // but WITHOUT ANY WARRANTY; without even the implied warranty of        //
00020 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         //
00021 // GNU General Public License for more details:                          //
00022 //                                                                       //
00023 //          http://www.gnu.org/copyleft/gpl.html                         //
00024 //                                                                       //
00026 
00028 
00029 class xmldb_index extends xmldb_object {
00030 
00031     var $unique;
00032     var $fields;
00033 
00040     const INDEX_COMPOSED_MAX_BYTES = 999;
00041 
00048     const INDEX_MAX_BYTES = 765;
00049 
00053     function __construct($name, $type=null, $fields=array()) {
00054         $this->unique = false;
00055         $this->fields = array();
00056         parent::__construct($name);
00057         return $this->set_attributes($type, $fields);
00058     }
00059 
00062     function setAttributes($type, $fields) {
00063 
00064         debugging('XMLDBIndex->setAttributes() has been deprecated in Moodle 2.0. Will be out in Moodle 2.1. Please use xmldb_index->set_attributes() instead.', DEBUG_DEVELOPER);
00065 
00066         return $this->set_attributes($type, $fields);
00067     }
00069 
00076     function set_attributes($type, $fields) {
00077         $this->unique = !empty($type) ? true : false;
00078         $this->fields = $fields;
00079     }
00080 
00084     function getUnique() {
00085         return $this->unique;
00086     }
00087 
00091     function setUnique($unique = true) {
00092         $this->unique = $unique;
00093     }
00094 
00098     function setFields($fields) {
00099         $this->fields = $fields;
00100     }
00101 
00105     function &getFields() {
00106         return $this->fields;
00107     }
00108 
00112     function arr2xmldb_index($xmlarr) {
00113 
00114         $result = true;
00115 
00120 
00122         if (isset($xmlarr['@']['NAME'])) {
00123             $this->name = trim($xmlarr['@']['NAME']);
00124         } else {
00125             $this->errormsg = 'Missing NAME attribute';
00126             $this->debug($this->errormsg);
00127             $result = false;
00128         }
00129 
00130         if (isset($xmlarr['@']['UNIQUE'])) {
00131             $unique = strtolower(trim($xmlarr['@']['UNIQUE']));
00132             if ($unique == 'true') {
00133                 $this->unique = true;
00134             } else if ($unique == 'false') {
00135                 $this->unique = false;
00136             } else {
00137                 $this->errormsg = 'Incorrect UNIQUE attribute (true/false allowed)';
00138                 $this->debug($this->errormsg);
00139                 $result = false;
00140             }
00141         } else {
00142                 $this->errormsg = 'Undefined UNIQUE attribute';
00143                 $this->debug($this->errormsg);
00144                 $result = false;
00145         }
00146 
00147         if (isset($xmlarr['@']['FIELDS'])) {
00148             $fields = strtolower(trim($xmlarr['@']['FIELDS']));
00149             if ($fields) {
00150                 $fieldsarr = explode(',',$fields);
00151                 if ($fieldsarr) {
00152                     foreach ($fieldsarr as $key => $element) {
00153                         $fieldsarr [$key] = trim($element);
00154                     }
00155                 } else {
00156                     $this->errormsg = 'Incorrect FIELDS attribute (comma separated of fields)';
00157                     $this->debug($this->errormsg);
00158                     $result = false;
00159                 }
00160             } else {
00161                 $this->errormsg = 'Empty FIELDS attribute';
00162                 $this->debug($this->errormsg);
00163                 $result = false;
00164             }
00165         } else {
00166             $this->errormsg = 'Missing FIELDS attribute';
00167             $this->debug($this->errormsg);
00168             $result = false;
00169         }
00171         $this->fields = $fieldsarr;
00172 
00173         if (isset($xmlarr['@']['COMMENT'])) {
00174             $this->comment = trim($xmlarr['@']['COMMENT']);
00175         }
00176 
00177         if (isset($xmlarr['@']['PREVIOUS'])) {
00178             $this->previous = trim($xmlarr['@']['PREVIOUS']);
00179         }
00180 
00181         if (isset($xmlarr['@']['NEXT'])) {
00182             $this->next = trim($xmlarr['@']['NEXT']);
00183         }
00184 
00186         if ($result) {
00187             $this->loaded = true;
00188         }
00189         $this->calculateHash();
00190         return $result;
00191     }
00192 
00196      function calculateHash($recursive = false) {
00197         if (!$this->loaded) {
00198             $this->hash = NULL;
00199         } else {
00200             $key = $this->unique . implode (', ', $this->fields);
00201             $this->hash = md5($key);
00202         }
00203     }
00204 
00208     function xmlOutput() {
00209         $o = '';
00210         $o.= '        <INDEX NAME="' . $this->name . '"';
00211         if ($this->unique) {
00212             $unique = 'true';
00213         } else {
00214             $unique = 'false';
00215         }
00216         $o.= ' UNIQUE="' . $unique . '"';
00217         $o.= ' FIELDS="' . implode(', ', $this->fields) . '"';
00218         if ($this->comment) {
00219             $o.= ' COMMENT="' . htmlspecialchars($this->comment) . '"';
00220         }
00221         if ($this->previous) {
00222             $o.= ' PREVIOUS="' . $this->previous . '"';
00223         }
00224         if ($this->next) {
00225             $o.= ' NEXT="' . $this->next . '"';
00226         }
00227         $o.= '/>' . "\n";
00228 
00229         return $o;
00230     }
00231 
00236     function setFromADOIndex($adoindex) {
00237 
00239         $this->unique = false;
00241         $fields = array_flip(array_change_key_case(array_flip($adoindex['columns'])));
00242         $this->fields = $fields;
00244         $this->loaded = true;
00245         $this->changed = true;
00246     }
00247 
00251     function getPHP() {
00252 
00253         $result = '';
00254 
00256         $unique = $this->getUnique();
00257         if (!empty($unique)) {
00258             $result .= 'XMLDB_INDEX_UNIQUE, ';
00259         } else {
00260             $result .= 'XMLDB_INDEX_NOTUNIQUE, ';
00261         }
00263         $indexfields = $this->getFields();
00264         if (!empty($indexfields)) {
00265             $result .= 'array(' . "'".  implode("', '", $indexfields) . "')";
00266         } else {
00267             $result .= 'null';
00268         }
00270         return $result;
00271     }
00272 
00276     function readableInfo() {
00277         $o = '';
00279         if ($this->unique) {
00280             $o .= 'unique';
00281         } else {
00282             $o .= 'not unique';
00283         }
00285         $o .= ' (' . implode(', ', $this->fields) . ')';
00286 
00287         return $o;
00288     }
00289 
00299     function validateDefinition(xmldb_table $xmldb_table=null) {
00300         if (!$xmldb_table) {
00301             return 'Invalid xmldb_index->validateDefinition() call, $xmldb_table si required.';
00302         }
00303 
00304         $total = 0;
00305         foreach ($this->getFields() as $fieldname) {
00306             if (!$field = $xmldb_table->getField($fieldname)) {
00307                 // argh, we do not have the fields loaded yet, this should not happen during install
00308                 continue;
00309             }
00310 
00311             switch ($field->getType()) {
00312                 case XMLDB_TYPE_INTEGER:
00313                     $total += 8; // big int
00314                     break;
00315 
00316                 case XMLDB_TYPE_NUMBER:
00317                     $total += 12; // this is just a guess
00318                     break;
00319 
00320                 case XMLDB_TYPE_FLOAT:
00321                     $total += 8; // double precision
00322                     break;
00323 
00324                 case XMLDB_TYPE_CHAR:
00325                     if ($field->getLength() > self::INDEX_MAX_BYTES / 3) {
00326                         return 'Invalid index definition in table {'.$xmldb_table->getName(). '}: XMLDB_TYPE_CHAR field "'.$field->getName().'" can not be indexed because it is too long.'
00327                                 .' Limit is '.(self::INDEX_MAX_BYTES/3).' chars.';
00328                     }
00329                     $total += ($field->getLength() * 3); // the most complex utf-8 chars have 3 bytes
00330                     break;
00331 
00332                 case XMLDB_TYPE_TEXT:
00333                     return 'Invalid index definition in table {'.$xmldb_table->getName(). '}: XMLDB_TYPE_TEXT field "'.$field->getName().'" can not be indexed';
00334                     break;
00335 
00336                 case XMLDB_TYPE_BINARY:
00337                     return 'Invalid index definition in table {'.$xmldb_table->getName(). '}: XMLDB_TYPE_BINARY field "'.$field->getName().'" can not be indexed';
00338                     break;
00339 
00340                 case XMLDB_TYPE_DATETIME:
00341                     $total += 8; // this is just a guess
00342                     break;
00343 
00344                 case XMLDB_TYPE_TIMESTAMP:
00345                     $total += 8; // this is just a guess
00346                     break;
00347             }
00348         }
00349 
00350         if ($total > self::INDEX_COMPOSED_MAX_BYTES) {
00351             return 'Invalid index definition in table {'.$xmldb_table->getName(). '}: the composed index on fields "'.implode(',', $this->getFields()).'" is too long.'
00352                     .' Limit is '.self::INDEX_COMPOSED_MAX_BYTES.' bytes / '.(self::INDEX_COMPOSED_MAX_BYTES/3).' chars.';
00353         }
00354 
00355         return null;
00356     }
00357 
00358 }
00359 
00362 class XMLDBIndex extends xmldb_index {
00363 
00364     function __construct($name) {
00365         parent::__construct($name);
00366     }
00367 
00368 }
 All Data Structures Namespaces Files Functions Variables Enumerations