00001 <?php
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00046 class ux_t3lib_sqlengine extends t3lib_sqlengine {
00047
00048
00049
00050
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
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
00141 $fieldsKeys = array();
00142 $indexKeys = array();
00143
00144 foreach($components['FIELDS'] as $fN => $fCfg) {
00145
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
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
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
00214 $cfg = $GLOBALS['TYPO3_DB']->MySQLMetaType($fieldCfg['fieldType']);
00215
00216
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';
00221 }
00222
00223
00224 if (is_array($fieldCfg['featureIndex'])) {
00225
00226
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
00233 if($featureDef['keyword'] == 'unsigned' && !strstr($GLOBALS['TYPO3_DB']->handlerCfg[$GLOBALS['TYPO3_DB']->lastHandlerKey]['config']['driver'],'mysql')) {
00234 continue;
00235 }
00236
00237 if($featureDef['keyword'] == 'auto_increment') {
00238 continue;
00239 }
00240
00241 if($featureDef['keyword'] == 'NOT NULL') {
00242 if($this->checkEmptyDefaultValue($fieldCfg['featureIndex'])) continue;
00243 else $cfg.=' NOTNULL';
00244 }
00245
00246 $cfg.=' '.$featureDef['keyword'];
00247
00248
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
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_sqlengine.php']) {
00280 include_once($TYPO3_CONF_VARS[TYPO3_MODE]['XCLASS']['ext/dbal/class.ux_t3lib_sqlengine.php']);
00281 }
00282 ?>