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                                 } else {
00107                                                 // Initialize:
00108                                         foreach($components['FIELDS'] as $fN => $fV)    {
00109                                                 $query[$fN]=$fV[0];
00110                                         }
00111                                 }
00112                                 break;
00113                 }
00114 
00115                 return $query;
00116         }
00117 
00118         function compileDROPTABLE($components)  {
00119                 switch((string)$GLOBALS['TYPO3_DB']->handlerCfg[$GLOBALS['TYPO3_DB']->lastHandlerKey]['type'])  {
00120                         case 'native':
00121                                 $query = 'DROP TABLE'.($components['ifExists']?' IF EXISTS':'').' '.$components['TABLE'];
00122                                 break;
00123                         case 'adodb':
00124                                 $query = $GLOBALS['TYPO3_DB']->handlerInstance[$GLOBALS['TYPO3_DB']->handler_getFromTableList($components['TABLE'])]->DataDictionary->DropTableSQL('`'.$components['TABLE'].'`');
00125                                 break;
00126                 }
00127 
00128                 return $query;
00129         }
00130 
00138         function compileCREATETABLE($components)        {
00139                         // Execute query (based on handler derived from the TABLE name which we actually know for once!)
00140                 switch((string)$GLOBALS['TYPO3_DB']->handlerCfg[$GLOBALS['TYPO3_DB']->handler_getFromTableList($components['TABLE'])]['type'])  {
00141                         case 'native':
00142                                 $query[] = parent::compileCREATETABLE($components);
00143                                 break;
00144                         case 'adodb':
00145                                         // Create fields and keys:
00146                                 $fieldsKeys = array();
00147                                 $indexKeys = array();
00148 
00149                                 foreach($components['FIELDS'] as $fN => $fCfg)  {
00150                                                 // the backticks get converted to the correct quote char automatically
00151                                         $fieldsKeys[$fN] = $GLOBALS['TYPO3_DB']->handlerInstance[$GLOBALS['TYPO3_DB']->handler_getFromTableList($components['TABLE'])]->DataDictionary->nameQuote('`'.$fN.'`').' '.$this->compileFieldCfg($fCfg['definition']);
00152                                 }
00153 
00154                                 if(isset($components['KEYS']) && is_array($components['KEYS'])) {
00155                                         foreach($components['KEYS'] as $kN => $kCfg)    {
00156                                                 if ($kN == 'PRIMARYKEY')        {
00157                                                         foreach($kCfg as $n => $field)  {
00158                                                                 $fieldsKeys[$field] .= ' PRIMARY';
00159                                                         }
00160                                                 } elseif ($kN == 'UNIQUE')      {
00161                                                         foreach($kCfg as $n => $field)  {
00162                                                                 $indexKeys = array_merge($indexKeys, $GLOBALS['TYPO3_DB']->handlerInstance[$GLOBALS['TYPO3_DB']->handler_getFromTableList($components['TABLE'])]->DataDictionary->CreateIndexSQL($n, $components['TABLE'], $field, array('UNIQUE')));
00163                                                         }
00164                                                 } else {
00165                                                         $indexKeys = array_merge($indexKeys, $GLOBALS['TYPO3_DB']->handlerInstance[$GLOBALS['TYPO3_DB']->handler_getFromTableList($components['TABLE'])]->DataDictionary->CreateIndexSQL($components['TABLE'].'_'.$kN, $components['TABLE'], $kCfg));
00166                                                 }
00167                                         }
00168                                 }
00169 
00170                                         // Fetch table/index generation query:
00171                                 $query = array_merge($GLOBALS['TYPO3_DB']->handlerInstance[$GLOBALS['TYPO3_DB']->lastHandlerKey]->DataDictionary->CreateTableSQL('`'.$components['TABLE'].'`',implode(','.chr(10), $fieldsKeys)), $indexKeys);
00172                                 break;
00173                 }
00174 
00175                 return $query;
00176         }
00177 
00178         function compileALTERTABLE($components) {
00179                         // Execute query (based on handler derived from the TABLE name which we actually know for once!)
00180                 switch((string)$GLOBALS['TYPO3_DB']->handlerCfg[$GLOBALS['TYPO3_DB']->lastHandlerKey]['type'])  {
00181                         case 'native':
00182                                 $query[] = parent::compileALTERTABLE($components);
00183                                 break;
00184                         case 'adodb':
00185                                 switch(strtoupper(str_replace(array(" ","\n","\r","\t"),'',$components['action'])))     {
00186                                         case 'ADD':
00187                                                 $query = $GLOBALS['TYPO3_DB']->handlerInstance[$GLOBALS['TYPO3_DB']->lastHandlerKey]->DataDictionary->AddColumnSQL('`'.$components['TABLE'].'`','`'.$components['FIELD'].'` '.$this->compileFieldCfg($components['definition']));
00188                                                 break;
00189                                         case 'CHANGE':
00190                                                 $query = $GLOBALS['TYPO3_DB']->handlerInstance[$GLOBALS['TYPO3_DB']->lastHandlerKey]->DataDictionary->AlterColumnSQL('`'.$components['TABLE'].'`','`'.$components['FIELD'].'` '.$this->compileFieldCfg($components['definition']));
00191                                                 break;
00192                                         case 'DROP':
00193                                         case 'DROPKEY':
00194                                                 break;
00195                                         case 'ADDKEY':
00196                                         case 'ADDPRIMARYKEY':
00197                                                 $query.=' ('.implode(',',$components['fields']).')';
00198                                                 break;
00199                                 }
00200                                 break;
00201                 }
00202 
00203                 return $query;
00204         }
00205 
00212         function compileFieldCfg($fieldCfg)     {
00213 
00214                 switch((string)$GLOBALS['TYPO3_DB']->handlerCfg[$GLOBALS['TYPO3_DB']->lastHandlerKey]['type'])  {
00215                         case 'native':
00216                                 $cfg = parent::compileFieldCfg($fieldCfg);
00217                                 break;
00218                         case 'adodb':
00219                                         // Set type:
00220                                 $type = $GLOBALS['TYPO3_DB']->MySQLMetaType($fieldCfg['fieldType']);
00221                                 $cfg = $type;
00222 
00223                                         // Add value, if any:
00224                                 if (strlen($fieldCfg['value']) && (in_array($type, array('C','C2'))))   {
00225                                         $cfg .= ' '.$fieldCfg['value'];
00226                                 } elseif (!isset($fieldCfg['value']) && (in_array($type, array('C','C2')))) {
00227                                         $cfg .= ' 255'; // add 255 as length for varchar without specified length (e.g. coming from tinytext, tinyblob)
00228                                 }
00229 
00230                                         // Add additional features:
00231                                 if (is_array($fieldCfg['featureIndex']))        {
00232 
00233                                                 // MySQL assigns DEFAULT value automatically if NOT NULL, fake this here
00234                                                 // numeric fields get 0 as default, other fields an empty string
00235                                         if(isset($fieldCfg['featureIndex']['NOTNULL']) && !isset($fieldCfg['featureIndex']['DEFAULT']) && !isset($fieldCfg['featureIndex']['AUTO_INCREMENT'])) {
00236                                                 switch($type) {
00237                                                         case 'I8':
00238                                                         case 'F':
00239                                                         case 'N':
00240                                                                 $fieldCfg['featureIndex']['DEFAULT'] = array('keyword' => 'DEFAULT', 'value' => array('0',''));
00241                                                                 break;
00242                                                         default:
00243                                                                 $fieldCfg['featureIndex']['DEFAULT'] = array('keyword' => 'DEFAULT', 'value' => array('','\''));
00244                                                 }
00245                                         }
00246 
00247                                         foreach($fieldCfg['featureIndex'] as $feature => $featureDef)   {
00248                                                 switch(true) {
00249                                                                 // unsigned only for mysql, as it is mysql specific
00250                                                         case ($feature == 'UNSIGNED' && !$GLOBALS['TYPO3_DB']->runningADOdbDriver('mysql')) :
00251                                                                 // auto_increment is removed, it is handled by (emulated) sequences
00252                                                         case ($feature == 'AUTO_INCREMENT') :
00253                                                                 // never add NOT NULL if running on Oracle and we have an empty string as default
00254                                                         case ($feature == 'NOTNULL' && $GLOBALS['TYPO3_DB']->runningADOdbDriver('oci8')) :
00255                                                                 continue;
00256                                                         case ($feature == 'NOTNULL') :
00257                                                         $cfg.=' NOTNULL';
00258                                                                 break;
00259                                                         default :
00260                                                         $cfg.=' '.$featureDef['keyword'];
00261                                                 }
00262 
00263                                                         // Add value if found:
00264                                                 if (is_array($featureDef['value']))     {
00265                                                         if($featureDef['value'][0]==='') {
00266                                                                 $cfg .= ' "\'\'"';
00267                                                         } else {
00268                                                                 $cfg.=' '.$featureDef['value'][1].$this->compileAddslashes($featureDef['value'][0]).$featureDef['value'][1];
00269                                                         }
00270                                                 }
00271                                         }
00272                                 }
00273                                 $cfg .= ' NOQUOTE';
00274                                 break;
00275                 }
00276 
00277                         // Return field definition string:
00278                 return $cfg;
00279         }
00280 
00281         function checkEmptyDefaultValue($featureIndex) {
00282                 if (is_array($featureIndex['DEFAULT']['value']))        {
00283                         if(!is_numeric($featureIndex['DEFAULT']['value'][0]) && empty($featureIndex['DEFAULT']['value'][0])) {
00284                                 return true;
00285                         } else {
00286                                 return false;
00287                         }
00288                 }
00289                 return true;
00290         }
00291 }
00292 
00293 
00294 if (defined('TYPO3_MODE') && $TYPO3_CONF_VARS[TYPO3_MODE]['XCLASS']['ext/dbal/class.ux_t3lib_sqlparser.php'])   {
00295         include_once($TYPO3_CONF_VARS[TYPO3_MODE]['XCLASS']['ext/dbal/class.ux_t3lib_sqlparser.php']);
00296 }
00297 ?>


Généré par Les experts TYPO3 avec  doxygen 1.4.6