Documentation TYPO3 par Ameos

class.ux_t3lib_sqlparser.php

00001 <?php
00002 /***************************************************************
00003 *  Copyright notice
00004 *
00005 *  (c) 2004 Kasper Skaarhoj (kasperYYYY@typo3.com)
00006 *  (c) 2004-2006 Karsten Dambekalns <karsten@typo3.org>
00007 *  All rights reserved
00008 *
00009 *  This script is part of the TYPO3 project. The TYPO3 project is
00010 *  free software; you can redistribute it and/or modify
00011 *  it under the terms of the GNU General Public License as published by
00012 *  the Free Software Foundation; either version 2 of the License, or
00013 *  (at your option) any later version.
00014 *
00015 *  The GNU General Public License can be found at
00016 *  http://www.gnu.org/copyleft/gpl.html.
00017 *  A copy is found in the textfile GPL.txt and important notices to the license
00018 *  from the author is found in LICENSE.txt distributed with these scripts.
00019 *
00020 *
00021 *  This script is distributed in the hope that it will be useful,
00022 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
00023 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00024 *  GNU General Public License for more details.
00025 *
00026 *  This copyright notice MUST APPEAR in all copies of the script!
00027 ***************************************************************/
00046 class ux_t3lib_sqlparser extends t3lib_sqlparser {
00047 
00048         /*************************
00049          *
00050          * Compiling queries
00051          *
00052          *************************/
00053 
00061         function compileSQL($components)        {
00062 
00063                 switch($components['type'])     {
00064                         case 'SELECT':
00065                                 $query = $this->compileSELECT($components);
00066                                 break;
00067                         case 'UPDATE':
00068                                 $query = $this->compileUPDATE($components);
00069                                 break;
00070                         case 'INSERT':
00071                                 $query = $this->compileINSERT($components);
00072                                 break;
00073                         case 'DELETE':
00074                                 $query = $this->compileDELETE($components);
00075                                 break;
00076                         case 'EXPLAIN':
00077                                 $query = 'EXPLAIN '.$this->compileSELECT($components);
00078                                 break;
00079                         case 'DROPTABLE':
00080                                 $query = $this->compileDROPTABLE($components);
00081                                 break;
00082                         case 'CREATETABLE':
00083                                 $query = $this->compileCREATETABLE($components);
00084                                 break;
00085                         case 'ALTERTABLE':
00086                                 $query = $this->compileALTERTABLE($components);
00087                                 break;
00088                 }
00089 
00090                 return $query;
00091         }
00092 
00093 
00094         function compileINSERT($components)     {
00095                 switch((string)$GLOBALS['TYPO3_DB']->handlerCfg[$GLOBALS['TYPO3_DB']->lastHandlerKey]['type'])  {
00096                         case 'native':
00097                                 parent::compileINSERT($components);
00098                                 break;
00099                         case 'adodb':
00100                                 if(isset($components['VALUES_ONLY']) && is_array($components['VALUES_ONLY'])) {
00101                                         $fields = $GLOBALS['TYPO3_DB']->cache_fieldType[$components['TABLE']];
00102                                         $fc = 0;
00103                                         foreach($fields as $fn => $fd) {
00104                                                 $query[$fn] = $components['VALUES_ONLY'][$fc++][0];
00105                                         }
00106                                 }
00107                                 break;
00108                 }
00109 
00110                 return $query;
00111         }
00112 
00113         function compileDROPTABLE($components)  {
00114                 switch((string)$GLOBALS['TYPO3_DB']->handlerCfg[$GLOBALS['TYPO3_DB']->lastHandlerKey]['type'])  {
00115                         case 'native':
00116                                 $query = 'DROP TABLE'.($components['ifExists']?' IF EXISTS':'').' '.$components['TABLE'];
00117                                 break;
00118                         case 'adodb':
00119                                 $query = $GLOBALS['TYPO3_DB']->handlerInstance[$GLOBALS['TYPO3_DB']->handler_getFromTableList($components['TABLE'])]->DataDictionary->DropTableSQL('`'.$components['TABLE'].'`');
00120                                 break;
00121                 }
00122 
00123                 return $query;
00124         }
00125 
00133         function compileCREATETABLE($components)        {
00134                         // Execute query (based on handler derived from the TABLE name which we actually know for once!)
00135                 switch((string)$GLOBALS['TYPO3_DB']->handlerCfg[$GLOBALS['TYPO3_DB']->handler_getFromTableList($components['TABLE'])]['type'])  {
00136                         case 'native':
00137                                 $query[] = parent::compileCREATETABLE($components);
00138                                 break;
00139                         case 'adodb':
00140                                         // Create fields and keys:
00141                                 $fieldsKeys = array();
00142                                 $indexKeys = array();
00143 
00144                                 foreach($components['FIELDS'] as $fN => $fCfg)  {
00145                                                 // the backticks get converted to the correct quote char automatically
00146                                         $fieldsKeys[$fN] = '`'.$fN.'` '.$this->compileFieldCfg($fCfg['definition'],$fN);
00147                                 }
00148 
00149                                 if(isset($components['KEYS']) && is_array($components['KEYS'])) {
00150                                         foreach($components['KEYS'] as $kN => $kCfg)    {
00151                                                 if ($kN == 'PRIMARYKEY')        {
00152                                                         foreach($kCfg as $n => $field)  {
00153                                                                 $fieldsKeys[$field] .= ' PRIMARY';
00154                                                         }
00155                                                 } elseif ($kN == 'UNIQUE')      {
00156                                                         foreach($kCfg as $n => $field)  {
00157                                                                 $indexKeys = array_merge($indexKeys, $GLOBALS['TYPO3_DB']->handlerInstance[$GLOBALS['TYPO3_DB']->handler_getFromTableList($components['TABLE'])]->DataDictionary->CreateIndexSQL($n, $components['TABLE'], $field, array('UNIQUE')));
00158                                                         }
00159                                                 } else {
00160                                                         $indexKeys = array_merge($indexKeys, $GLOBALS['TYPO3_DB']->handlerInstance[$GLOBALS['TYPO3_DB']->handler_getFromTableList($components['TABLE'])]->DataDictionary->CreateIndexSQL($components['TABLE'].'_'.$kN, $components['TABLE'], $kCfg));
00161                                                 }
00162                                         }
00163                                 }
00164 
00165                                         // Fetch table/index generation query:
00166                                 $query = array_merge($GLOBALS['TYPO3_DB']->handlerInstance[$GLOBALS['TYPO3_DB']->lastHandlerKey]->DataDictionary->CreateTableSQL('`'.$components['TABLE'].'`',implode(','.chr(10), $fieldsKeys)), $indexKeys);
00167                                 break;
00168                 }
00169 
00170                 return $query;
00171         }
00172 
00173         function compileALTERTABLE($components) {
00174                         // Execute query (based on handler derived from the TABLE name which we actually know for once!)
00175                 switch((string)$GLOBALS['TYPO3_DB']->handlerCfg[$GLOBALS['TYPO3_DB']->lastHandlerKey]['type'])  {
00176                         case 'native':
00177                                 $query[] = parent::compileALTERTABLE($components);
00178                                 break;
00179                         case 'adodb':
00180                                 switch(strtoupper(str_replace(array(" ","\n","\r","\t"),'',$components['action'])))     {
00181                                         case 'ADD':
00182                                                 $query = $GLOBALS['TYPO3_DB']->handlerInstance[$GLOBALS['TYPO3_DB']->lastHandlerKey]->DataDictionary->AddColumnSQL('`'.$components['TABLE'].'`','`'.$components['FIELD'].'` '.$this->compileFieldCfg($components['definition']));
00183                                                 break;
00184                                         case 'CHANGE':
00185                                                 $query = $GLOBALS['TYPO3_DB']->handlerInstance[$GLOBALS['TYPO3_DB']->lastHandlerKey]->DataDictionary->AlterColumnSQL('`'.$components['TABLE'].'`','`'.$components['FIELD'].'` '.$this->compileFieldCfg($components['definition']));
00186                                                 break;
00187                                         case 'DROP':
00188                                         case 'DROPKEY':
00189                                                 break;
00190                                         case 'ADDKEY':
00191                                         case 'ADDPRIMARYKEY':
00192                                                 $query.=' ('.implode(',',$components['fields']).')';
00193                                                 break;
00194                                 }
00195                                 break;
00196                 }
00197 
00198                 return $query;
00199         }
00200 
00207         function compileFieldCfg($fieldCfg,$fN='')      {
00208                 switch((string)$GLOBALS['TYPO3_DB']->handlerCfg[$GLOBALS['TYPO3_DB']->lastHandlerKey]['type'])  {
00209                         case 'native':
00210                                 $cfg = parent::compileFieldCfg($fieldCfg,$fN);
00211                                 break;
00212                         case 'adodb':
00213                                 // Set type:
00214                                 $cfg = $GLOBALS['TYPO3_DB']->MySQLMetaType($fieldCfg['fieldType']);
00215 
00216                                         // Add value, if any:
00217                                 if (strlen($fieldCfg['value']) && (in_array($cfg, array('C','C2'))))    {
00218                                         $cfg.=' '.$fieldCfg['value'];
00219                                 } elseif (!isset($fieldCfg['value']) && (in_array($cfg, array('C','C2')))) {
00220                                         $cfg .= ' 255'; // add 255 as length for varchar without specified length (e.g. coming from tinytext, tinyblob)
00221                                 }
00222 
00223                                         // Add additional features:
00224                                 if (is_array($fieldCfg['featureIndex']))        {
00225 
00226                                                 // MySQL assigns DEFAULT value automatically if NOT NULL, fake this here
00227                                         if(isset($fieldCfg['featureIndex']['NOTNULL']) && !isset($fieldCfg['featureIndex']['DEFAULT']) && !isset($fieldCfg['featureIndex']['AUTO_INCREMENT'])) {
00228                                                 $fieldCfg['featureIndex']['DEFAULT'] = array('keyword' => 'DEFAULT', 'value' => array('','\''));
00229                                         }
00230 
00231                                         foreach($fieldCfg['featureIndex'] as $featureDef)       {
00232                                                         // unsigned only for mysql, as it is mysql specific
00233                                                 if($featureDef['keyword'] == 'unsigned' && !strstr($GLOBALS['TYPO3_DB']->handlerCfg[$GLOBALS['TYPO3_DB']->lastHandlerKey]['config']['driver'],'mysql')) {
00234                                                         continue;
00235                                                 }
00236                                                         // auto_increment is removed, it is handled by (emulated) sequences
00237                                                 if($featureDef['keyword'] == 'auto_increment') {
00238                                                         continue;
00239                                                 }
00240                                                         // NOT NULL only if there is no default value, as this conflicts
00241                                                 if($featureDef['keyword'] == 'NOT NULL') {
00242                                                         if($this->checkEmptyDefaultValue($fieldCfg['featureIndex'])) continue; // we do not have a default value or it is an empty string
00243                                                         else $cfg.=' NOTNULL';
00244                                                 }
00245 
00246                                                 $cfg.=' '.$featureDef['keyword'];
00247 
00248                                                         // Add value if found:
00249                                                 if (is_array($featureDef['value']))     {
00250                                                         if(!is_numeric($featureDef['value'][0]) && empty($featureDef['value'][0])) {
00251                                                                 $cfg .= ' "\'\'"';
00252                                                         } else {
00253                                                                 $cfg.=' '.$featureDef['value'][1].$this->compileAddslashes($featureDef['value'][0]).$featureDef['value'][1];
00254                                                         }
00255                                                 }
00256                                         }
00257                                 }
00258                                 $cfg .= ' NOQUOTE';
00259                                 break;
00260                 }
00261 
00262                         // Return field definition string:
00263                 return $cfg;
00264         }
00265 
00266         function checkEmptyDefaultValue($featureIndex) {
00267                 if (is_array($featureIndex['DEFAULT']['value']))        {
00268                         if(!is_numeric($featureIndex['DEFAULT']['value'][0]) && empty($featureIndex['DEFAULT']['value'][0])) {
00269                                 return true;
00270                         } else {
00271                                 return false;
00272                         }
00273                 }
00274                 return true;
00275         }
00276 }
00277 
00278 
00279 if (defined('TYPO3_MODE') && $TYPO3_CONF_VARS[TYPO3_MODE]['XCLASS']['ext/dbal/class.ux_t3lib_sqlparser.php'])   {
00280         include_once($TYPO3_CONF_VARS[TYPO3_MODE]['XCLASS']['ext/dbal/class.ux_t3lib_sqlparser.php']);
00281 }
00282 ?>


Généré par L'expert TYPO3 avec  doxygen 1.4.6