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
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043
00044
00045
00046
00047
00048
00049
00050
00051
00052
00053
00054
00055
00056
00057
00058
00059
00060
00061
00062
00063
00064
00065
00066
00067
00068
00069
00070
00071
00072
00073
00074
00075
00076
00077
00078
00079
00080
00081
00082
00083
00084
00085
00086
00087
00088
00089
00090
00091
00092
00093
00094
00095
00096
00097
00098
00099
00100
00101
00102
00103
00104
00105
00106
00107
00108
00109
00110
00111
00112
00113
00114 class ux_t3lib_DB extends t3lib_DB {
00115
00116
00117 var $printErrors = false;
00118 var $debug = false;
00119 var $conf = array();
00120
00121 var $mapping = array();
00122 var $table2handlerKeys = array();
00123 var $handlerCfg = array (
00124 '_DEFAULT' => array (
00125 'type' => 'native',
00126 'config' => array(
00127 'username' => '',
00128 'password' => '',
00129 'host' => '',
00130 'database' => '',
00131 'driver' => '',
00132 'sequenceStart' => 1
00133 )
00134 ),
00135 );
00136
00137
00138
00139 var $handlerInstance = array();
00140 var $lastHandlerKey = '';
00141 var $lastQuery = '';
00142 var $lastParsedAndMappedQueryArray = array();
00143
00144 var $resourceIdToTableNameMap = array();
00145
00146
00147 var $cache_handlerKeyFromTableList = array();
00148 var $cache_mappingFromTableList = array();
00149 var $cache_autoIncFields = array();
00150 var $cache_fieldType = array();
00151 var $cache_primaryKeys = array();
00152
00153
00154
00155
00156
00157
00158 var $SQLparser;
00159
00160
00161
00162
00163
00164
00165 var $Installer;
00166
00167
00168
00169
00170
00171
00172
00173
00174 function ux_t3lib_DB() {
00175
00176
00177 $this->SQLparser = t3lib_div::makeInstance('t3lib_sqlengine');
00178 $this->Installer = t3lib_div::makeInstance('t3lib_install');
00179
00180
00181 $this->conf = $GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['dbal'];
00182 $this->initInternalVariables();
00183 }
00184
00185
00186
00187
00188
00189
00190 function initInternalVariables() {
00191
00192
00193 if (isset($this->conf['mapping'])) $this->mapping = $this->conf['mapping'];
00194 if (isset($this->conf['table2handlerKeys'])) $this->table2handlerKeys = $this->conf['table2handlerKeys'];
00195 if (isset($this->conf['handlerCfg'])) $this->handlerCfg = $this->conf['handlerCfg'];
00196
00197 $this->cacheFieldInfo();
00198
00199 $this->printErrors = $this->conf['debugOptions']['printErrors'] ? TRUE : FALSE;
00200 $this->debug = $this->conf['debugOptions']['enabled'] ? TRUE : FALSE;
00201 }
00202
00203 function clearCachedFieldInfo() {
00204 if(file_exists(PATH_typo3conf.'temp_fieldInfo.php'))
00205 unlink(PATH_typo3conf.'temp_fieldInfo.php');
00206 }
00207
00208 function cacheFieldInfo() {
00209 global $TYPO3_LOADED_EXT;
00210 $extSQL = '';
00211 $parsedExtSQL = array();
00212
00213
00214
00215 if(file_exists(PATH_typo3conf.'temp_fieldInfo.php')) {
00216 $fdata = unserialize(t3lib_div::getUrl(PATH_typo3conf.'temp_fieldInfo.php'));
00217 $this->cache_autoIncFields = $fdata['incFields'];
00218 $this->cache_fieldType = $fdata['fieldTypes'];
00219 $this->cache_primaryKeys = $fdata['primaryKeys'];
00220 }
00221 else {
00222
00223 $extSQL = t3lib_div::getUrl(PATH_site.'t3lib/stddb/tables.sql');
00224 $parsedExtSQL = $this->Installer->getFieldDefinitions_fileContent($extSQL);
00225 $this->analyzeFields($parsedExtSQL);
00226
00227
00228 foreach($TYPO3_LOADED_EXT as $ext => $v) {
00229 if(!is_array($v) || !isset($v['ext_tables.sql']))
00230 continue;
00231
00232
00233 $extSQL = t3lib_div::getUrl($v['ext_tables.sql']);
00234 $parsedExtSQL = $this->Installer->getFieldDefinitions_fileContent($extSQL);
00235 $this->analyzeFields($parsedExtSQL);
00236 }
00237
00238 $cachedFieldInfo = array('incFields' => $this->cache_autoIncFields, 'fieldTypes' => $this->cache_fieldType, 'primaryKeys' => $this->cache_primaryKeys);
00239 $cachedFieldInfo = serialize($this->mapCachedFieldInfo($cachedFieldInfo));
00240
00241
00242 t3lib_div::writeFile(PATH_typo3conf."temp_fieldInfo.php", $cachedFieldInfo);
00243
00244 if (strcmp(t3lib_div::getUrl(PATH_typo3conf."temp_fieldInfo.php"), $cachedFieldInfo)) {
00245 die('typo3temp/temp_incfields.php was NOT updated properly (written content didn\'t match file content) - maybe write access problem?');
00246 }
00247 }
00248 }
00249
00250
00251
00252
00253
00254
00255
00256
00257 function analyzeFields($parsedExtSQL) {
00258 foreach($parsedExtSQL as $table => $tdef) {
00259 if (is_array($tdef['fields'])) {
00260 foreach($tdef['fields'] as $field => $fdef) {
00261 $fdef = $this->SQLparser->parseFieldDef($fdef);
00262 $this->cache_fieldType[$table][$field]['type'] = $fdef['fieldType'];
00263 $this->cache_fieldType[$table][$field]['metaType'] = $this->MySQLMetaType($fdef['fieldType']);
00264 $this->cache_fieldType[$table][$field]['notnull'] = (isset($fdef['featureIndex']['NOTNULL']) && !$this->SQLparser->checkEmptyDefaultValue($fdef['featureIndex'])) ? 1 : 0;
00265 if(isset($fdef['featureIndex']['DEFAULT'])) {
00266 $default = $fdef['featureIndex']['DEFAULT']['value'][0];
00267 if(isset($fdef['featureIndex']['DEFAULT']['value'][1])) {
00268 $default = $fdef['featureIndex']['DEFAULT']['value'][1].$default.$fdef['featureIndex']['DEFAULT']['value'][1];
00269 }
00270 $this->cache_fieldType[$table][$field]['default'] = $default;
00271 }
00272 if(isset($fdef['featureIndex']['AUTO_INCREMENT'])) {
00273 $this->cache_autoIncFields[$table] = $field;
00274 }
00275 if(isset($tdef['keys']['PRIMARY'])) {
00276 $this->cache_primaryKeys[$table] = substr($tdef['keys']['PRIMARY'], 13, -1);
00277 }
00278 }
00279 }
00280 }
00281 }
00282
00283
00284
00285
00286
00287 function mapCachedFieldInfo($fieldInfo){
00288 global $TYPO3_CONF_VARS;
00289
00290 if(is_array($TYPO3_CONF_VARS['EXTCONF']['dbal']['mapping'])) {
00291 foreach($TYPO3_CONF_VARS['EXTCONF']['dbal']['mapping'] as $mappedTable => $mappedConf){
00292 if(array_key_exists($mappedTable, $fieldInfo['incFields'])) {
00293 $mappedTableAlias = $mappedConf['mapTableName'];
00294 $fieldInfo['incFields'][$mappedTableAlias] = isset($mappedConf['mapFieldNames'][$fieldInfo['incFields'][$mappedTable]]) ? $mappedConf['mapFieldNames'][$fieldInfo['incFields'][$mappedTable]] : $fieldInfo['incFields'][$mappedTable];
00295 }
00296
00297 if(array_key_exists($mappedTable, $fieldInfo['fieldTypes'])) {
00298 foreach($fieldInfo['fieldTypes'][$mappedTable] as $field => $fieldConf){
00299 $tempMappedFieldConf[$mappedConf['mapFieldNames'][$field]] = $fieldConf;
00300 }
00301
00302 $fieldInfo['fieldTypes'][$mappedConf['mapTableName']] = $tempMappedFieldConf;
00303 }
00304
00305 if(array_key_exists($mappedTable, $fieldInfo['primaryKeys'])) {
00306 $mappedTableAlias = $mappedConf['mapTableName'];
00307 $fieldInfo['primaryKeys'][$mappedTableAlias] = isset($mappedConf['mapFieldNames'][$fieldInfo['primaryKeys'][$mappedTable]]) ? $mappedConf['mapFieldNames'][$fieldInfo['primaryKeys'][$mappedTable]] : $fieldInfo['primaryKeys'][$mappedTable];
00308 }
00309
00310 }
00311 }
00312
00313 return $fieldInfo;
00314 }
00315
00316
00317
00318
00319
00320
00321
00322
00323
00324
00325
00326
00327
00328
00329
00330
00331
00332
00333
00334
00335
00336
00337
00338
00339
00340
00341
00342
00343
00344
00345 function exec_INSERTquery($table,$fields_values,$no_quote_fields='') {
00346
00347 if ($this->debug) $pt = t3lib_div::milliseconds();
00348
00349
00350 $ORIG_tableName = $table;
00351 if ($tableArray = $this->map_needMapping($table)) {
00352
00353
00354 $fields_values = $this->map_assocArray($fields_values,$tableArray);
00355
00356
00357 if ($this->mapping[$table]['mapTableName']) {
00358 $table = $this->mapping[$table]['mapTableName'];
00359 }
00360 }
00361
00362 $this->lastHandlerKey = $this->handler_getFromTableList($ORIG_tableName);
00363 switch((string)$this->handlerCfg[$this->lastHandlerKey]['type']) {
00364 case 'native':
00365 $this->lastQuery = $this->INSERTquery($table,$fields_values,$no_quote_fields);
00366 if(is_string($this->lastQuery)) {
00367 $sqlResult = mysql_query($this->lastQuery, $this->handlerInstance[$this->lastHandlerKey]['link']);
00368 }
00369 else {
00370 $sqlResult = mysql_query($this->lastQuery[0], $this->handlerInstance[$this->lastHandlerKey]['link']);
00371 foreach($this->lastQuery[1] as $field => $content) {
00372 mysql_query('UPDATE '.$this->quoteFromTables($table).' SET '.$this->quoteFromTables($field).'='.$this->fullQuoteStr($content,$table).' WHERE '.$this->quoteWhereClause($where), $this->handlerInstance[$this->lastHandlerKey]['link']);
00373 }
00374 }
00375 break;
00376 case 'adodb':
00377
00378
00379 if(isset($this->cache_autoIncFields[$table])) {
00380 if(isset($fields_values[$this->cache_autoIncFields[$table]])) {
00381 $new_id = $fields_values[$this->cache_autoIncFields[$table]];
00382 if($table !== 'tx_dbal_debuglog') {
00383 $this->handlerInstance[$this->lastHandlerKey]->last_insert_id = $new_id;
00384 }
00385 } else {
00386 $new_id = $this->handlerInstance[$this->lastHandlerKey]->GenID($table.'_'.$this->cache_autoIncFields[$table], $this->handlerInstance[$this->lastHandlerKey]->sequenceStart);
00387 $fields_values[$this->cache_autoIncFields[$table]] = $new_id;
00388 if($table !== 'tx_dbal_debuglog') {
00389 $this->handlerInstance[$this->lastHandlerKey]->last_insert_id = $new_id;
00390 }
00391 }
00392 }
00393
00394 $this->lastQuery = $this->INSERTquery($table,$fields_values,$no_quote_fields);
00395 if(is_string($this->lastQuery)) {
00396 $sqlResult = $this->handlerInstance[$this->lastHandlerKey]->_query($this->lastQuery,false);
00397 } else {
00398 $this->handlerInstance[$this->lastHandlerKey]->StartTrans();
00399 if(strlen($this->lastQuery[0])) {
00400 $sqlResult = $this->handlerInstance[$this->lastHandlerKey]->_query($this->lastQuery[0],false);
00401 }
00402 if(is_array($this->lastQuery[1])) {
00403 foreach($this->lastQuery[1] as $field => $content) {
00404 if(empty($content)) continue;
00405
00406 if(isset($this->cache_autoIncFields[$table]) && isset($new_id)) {
00407 $this->handlerInstance[$this->lastHandlerKey]->UpdateBlob($this->quoteFromTables($table),$field,$content,$this->quoteWhereClause($this->cache_autoIncFields[$table].'='.$new_id));
00408 } elseif(isset($this->cache_primaryKeys[$table])) {
00409 $where = '';
00410 $pks = explode(',', $this->cache_primaryKeys[$table]);
00411 foreach ($pks as $pk) {
00412 if(isset($fields_values[$pk]))
00413 $where .= $pk.'='.$this->fullQuoteStr($fields_values[$pk], $table).' AND ';
00414 }
00415 $where = $this->quoteWhereClause($where.'1=1');
00416 $this->handlerInstance[$this->lastHandlerKey]->UpdateBlob($this->quoteFromTables($table),$field,$content,$where);
00417 } else {
00418 $this->handlerInstance[$this->lastHandlerKey]->CompleteTrans(false);
00419 die('Could not update BLOB >>>> no WHERE clause found!');
00420 }
00421 }
00422 }
00423 if(is_array($this->lastQuery[2])) {
00424 foreach($this->lastQuery[2] as $field => $content) {
00425 if(empty($content)) continue;
00426
00427 if(isset($this->cache_autoIncFields[$table]) && isset($new_id)) {
00428 $this->handlerInstance[$this->lastHandlerKey]->UpdateClob($this->quoteFromTables($table),$field,$content,$this->quoteWhereClause($this->cache_autoIncFields[$table].'='.$new_id));
00429 } elseif(isset($this->cache_primaryKeys[$table])) {
00430 $where = '';
00431 $pks = explode(',', $this->cache_primaryKeys[$table]);
00432 foreach ($pks as $pk) {
00433 if(isset($fields_values[$pk]))
00434 $where .= $pk.'='.$this->fullQuoteStr($fields_values[$pk], $table).' AND ';
00435 }
00436 $where = $this->quoteWhereClause($where.'1=1');
00437 $this->handlerInstance[$this->lastHandlerKey]->UpdateClob($this->quoteFromTables($table),$field,$content,$where);
00438 } else {
00439 $this->handlerInstance[$this->lastHandlerKey]->CompleteTrans(false);
00440 die('Could not update CLOB >>>> no WHERE clause found!');
00441 }
00442 }
00443 }
00444 $this->handlerInstance[$this->lastHandlerKey]->CompleteTrans();
00445 }
00446 break;
00447 case 'userdefined':
00448 $sqlResult = $this->handlerInstance[$this->lastHandlerKey]->exec_INSERTquery($table,$fields_values,$no_quote_fields);
00449 break;
00450 }
00451
00452 if ($this->printErrors && $this->sql_error()) {
00453 debug(array($this->lastQuery, $this->sql_error()));
00454 }
00455
00456 if ($this->debug) {
00457 $this->debugHandler(
00458 'exec_INSERTquery',
00459 t3lib_div::milliseconds()-$pt,
00460 array(
00461 'handlerType' => $hType,
00462 'args' => array($table,$fields_values),
00463 'ORIG_tablename' => $ORIG_tableName
00464 )
00465 );
00466 }
00467
00468 return $sqlResult;
00469 }
00470
00471
00472
00473
00474
00475
00476
00477
00478
00479
00480 function exec_UPDATEquery($table,$where,$fields_values,$no_quote_fields='') {
00481
00482 if ($this->debug) $pt = t3lib_div::milliseconds();
00483
00484
00485 $ORIG_tableName = $table;
00486 if ($tableArray = $this->map_needMapping($table)) {
00487
00488
00489 $fields_values = $this->map_assocArray($fields_values,$tableArray);
00490
00491
00492 $whereParts = $this->SQLparser->parseWhereClause($where);
00493 $this->map_sqlParts($whereParts,$tableArray[0]['table']);
00494 $where = $this->SQLparser->compileWhereClause($whereParts, false);
00495
00496
00497 if ($this->mapping[$table]['mapTableName']) {
00498 $table = $this->mapping[$table]['mapTableName'];
00499 }
00500 }
00501
00502
00503 $this->lastHandlerKey = $this->handler_getFromTableList($ORIG_tableName);
00504 switch((string)$this->handlerCfg[$this->lastHandlerKey]['type']) {
00505 case 'native':
00506 $this->lastQuery = $this->UPDATEquery($table,$where,$fields_values,$no_quote_fields);
00507 if(is_string($this->lastQuery)) {
00508 $sqlResult = mysql_query($this->lastQuery, $this->handlerInstance[$this->lastHandlerKey]['link']);
00509 }
00510 else {
00511 $sqlResult = mysql_query($this->lastQuery[0], $this->handlerInstance[$this->lastHandlerKey]['link']);
00512 foreach($this->lastQuery[1] as $field => $content) {
00513 mysql_query('UPDATE '.$this->quoteFromTables($table).' SET '.$this->quoteFromTables($field).'='.$this->fullQuoteStr($content,$table).' WHERE '.$this->quoteWhereClause($where), $this->handlerInstance[$this->lastHandlerKey]['link']);
00514 }
00515 }
00516 break;
00517 case 'adodb':
00518 $this->lastQuery = $this->UPDATEquery($table,$where,$fields_values,$no_quote_fields);
00519 if(is_string($this->lastQuery)) {
00520 $sqlResult = $this->handlerInstance[$this->lastHandlerKey]->_query($this->lastQuery,false);
00521 } else {
00522 $this->handlerInstance[$this->lastHandlerKey]->StartTrans();
00523 if(strlen($this->lastQuery[0])) {
00524 $sqlResult = $this->handlerInstance[$this->lastHandlerKey]->_query($this->lastQuery[0],false);
00525 }
00526 if(is_array($this->lastQuery[1])) {
00527 foreach($this->lastQuery[1] as $field => $content) {
00528 $this->handlerInstance[$this->lastHandlerKey]->UpdateBlob($this->quoteFromTables($table),$field,$content,$this->quoteWhereClause($where));
00529 }
00530 }
00531 if(is_array($this->lastQuery[2])) {
00532 foreach($this->lastQuery[2] as $field => $content) {
00533 $this->handlerInstance[$this->lastHandlerKey]->UpdateClob($this->quoteFromTables($table),$field,$content,$this->quoteWhereClause($where));
00534 }
00535 }
00536 $this->handlerInstance[$this->lastHandlerKey]->CompleteTrans();
00537 }
00538 break;
00539 case 'userdefined':
00540 $sqlResult = $this->handlerInstance[$this->lastHandlerKey]->exec_UPDATEquery($table,$where,$fields_values,$no_quote_fields);
00541 break;
00542 }
00543
00544 if ($this->printErrors && $this->sql_error()) {
00545 debug(array($this->lastQuery, $this->sql_error()));
00546 }
00547
00548 if ($this->debug) {
00549 $this->debugHandler(
00550 'exec_UPDATEquery',
00551 t3lib_div::milliseconds()-$pt,
00552 array(
00553 'handlerType' => $hType,
00554 'args' => array($table,$where, $fields_values),
00555 'ORIG_from_table' => $ORIG_tableName
00556 )
00557 );
00558 }
00559
00560
00561 return $sqlResult;
00562 }
00563
00564
00565
00566
00567
00568
00569
00570
00571 function exec_DELETEquery($table,$where) {
00572
00573 if ($this->debug) $pt = t3lib_div::milliseconds();
00574
00575
00576 $ORIG_tableName = $table;
00577 if ($tableArray = $this->map_needMapping($table)) {
00578
00579
00580 $whereParts = $this->SQLparser->parseWhereClause($where);
00581 $this->map_sqlParts($whereParts,$tableArray[0]['table']);
00582 $where = $this->SQLparser->compileWhereClause($whereParts, false);
00583
00584
00585 if ($this->mapping[$table]['mapTableName']) {
00586 $table = $this->mapping[$table]['mapTableName'];
00587 }
00588 }
00589
00590
00591 $this->lastHandlerKey = $this->handler_getFromTableList($ORIG_tableName);
00592 switch((string)$this->handlerCfg[$this->lastHandlerKey]['type']) {
00593 case 'native':
00594 $this->lastQuery = $this->DELETEquery($table,$where);
00595 $sqlResult = mysql_query($this->lastQuery, $this->handlerInstance[$this->lastHandlerKey]['link']);
00596 break;
00597 case 'adodb':
00598 $this->lastQuery = $this->DELETEquery($table,$where);
00599 $sqlResult = $this->handlerInstance[$this->lastHandlerKey]->_query($this->lastQuery,false);
00600 break;
00601 case 'userdefined':
00602 $sqlResult = $this->handlerInstance[$this->lastHandlerKey]->exec_DELETEquery($table,$where);
00603 break;
00604 }
00605
00606 if ($this->printErrors && $this->sql_error()) {
00607 debug(array($this->lastQuery, $this->sql_error()));
00608 }
00609
00610 if ($this->debug) {
00611 $this->debugHandler(
00612 'exec_DELETEquery',
00613 t3lib_div::milliseconds()-$pt,
00614 array(
00615 'handlerType' => $hType,
00616 'args' => array($table,$where),
00617 'ORIG_from_table' => $ORIG_tableName
00618 )
00619 );
00620 }
00621
00622
00623 return $sqlResult;
00624 }
00625
00626
00627
00628
00629
00630
00631
00632
00633
00634
00635
00636
00637 function exec_SELECTquery($select_fields,$from_table,$where_clause,$groupBy='',$orderBy='',$limit='') {
00638
00639 if ($this->debug) $pt = t3lib_div::milliseconds();
00640
00641
00642 $ORIG_tableName = $from_table;
00643 if ($tableArray = $this->map_needMapping($ORIG_tableName)) {
00644 $this->map_remapSELECTQueryParts($select_fields,$from_table,$where_clause,$groupBy,$orderBy);
00645 }
00646
00647
00648 $this->lastHandlerKey = $this->handler_getFromTableList($ORIG_tableName);
00649 $hType = (string)$this->handlerCfg[$this->lastHandlerKey]['type'];
00650 switch($hType) {
00651 case 'native':
00652 $this->lastQuery = $this->SELECTquery($select_fields,$from_table,$where_clause,$groupBy,$orderBy,$limit);
00653 $sqlResult = mysql_query($this->lastQuery, $this->handlerInstance[$this->lastHandlerKey]['link']);
00654 $this->resourceIdToTableNameMap[(string)$sqlResult] = $ORIG_tableName;
00655 break;
00656 case 'adodb':
00657 if ($limit!='') {
00658 $splitLimit = t3lib_div::intExplode(',',$limit);
00659 if ($splitLimit[1]) {
00660 $numrows = $splitLimit[1];
00661 $offset = $splitLimit[0];
00662 } else {
00663 $numrows = $splitLimit[0];
00664 $offset = 0;
00665 }
00666
00667 $sqlResult = $this->handlerInstance[$this->lastHandlerKey]->SelectLimit($this->SELECTquery($select_fields,$from_table,$where_clause,$groupBy,$orderBy), $numrows, $offset);
00668 $this->lastQuery = $sqlResult->sql;
00669 } else {
00670 $this->lastQuery = $this->SELECTquery($select_fields,$from_table,$where_clause,$groupBy,$orderBy);
00671 $sqlResult = $this->handlerInstance[$this->lastHandlerKey]->_Execute($this->lastQuery);
00672 }
00673
00674 $sqlResult->TYPO3_DBAL_handlerType = 'adodb';
00675 $sqlResult->TYPO3_DBAL_tableList = $ORIG_tableName;
00676 break;
00677 case 'userdefined':
00678 $sqlResult = $this->handlerInstance[$this->lastHandlerKey]->exec_SELECTquery($select_fields,$from_table,$where_clause,$groupBy,$orderBy,$limit);
00679 if (is_object($sqlResult)) {
00680 $sqlResult->TYPO3_DBAL_handlerType = 'userdefined';
00681 $sqlResult->TYPO3_DBAL_tableList = $ORIG_tableName;
00682 }
00683 break;
00684 }
00685
00686 if ($this->printErrors && $this->sql_error()) {
00687 debug(array($this->lastQuery, $this->sql_error()));
00688 }
00689
00690 if ($this->debug) {
00691 $this->debugHandler(
00692 'exec_SELECTquery',
00693 t3lib_div::milliseconds()-$pt,
00694 array(
00695 'handlerType' => $hType,
00696 'args' => array($from_table,$select_fields,$where_clause,$groupBy,$orderBy,$limit),
00697 'ORIG_from_table' => $ORIG_tableName
00698 )
00699 );
00700 }
00701
00702
00703 return $sqlResult;
00704 }
00705
00706
00707
00708
00709
00710
00711
00712
00713
00714
00715
00716
00717
00718
00719
00720
00721
00722
00723 function INSERTquery($table,$fields_values,$no_quote_fields='') {
00724
00725 if (is_array($fields_values) && count($fields_values)) {
00726
00727 if (is_string($no_quote_fields)) {
00728 $no_quote_fields = explode(',',$no_quote_fields);
00729 } elseif (!is_array($no_quote_fields)) {
00730 $no_quote_fields = array();
00731 }
00732
00733 $blobfields = array();
00734 $nArr = array();
00735 foreach($fields_values as $k => $v) {
00736 if(!$this->runningNative() && $this->sql_field_metatype($table,$k) == 'B') {
00737
00738 $blobfields[$this->quoteFieldNames($k)] = $v;
00739 } elseif(!$this->runningNative() && $this->sql_field_metatype($table,$k) == 'XL') {
00740
00741 $clobfields[$this->quoteFieldNames($k)] = $v;
00742 } else {
00743
00744
00745 $mt = $this->sql_field_metatype($table,$k);
00746 $v = (($mt{0}=='I')||($mt{0}=='F')) ? (int)$v : $v;
00747
00748 $nArr[$this->quoteFieldNames($k)] = (!in_array($k,$no_quote_fields)) ? $this->fullQuoteStr($v, $table) : $v;
00749 }
00750 }
00751
00752 if(count($blobfields) || count($clobfields)) {
00753 if(count($nArr)) {
00754 $query[0] = 'INSERT INTO '.$this->quoteFromTables($table).'
00755 (
00756 '.implode(',
00757 ',array_keys($nArr)).'
00758 ) VALUES (
00759 '.implode(',
00760 ',$nArr).'
00761 )';
00762 }
00763 if(count($blobfields)) $query[1] = $blobfields;
00764 if(count($clobfields)) $query[2] = $clobfields;
00765 if ($this->debugOutput || $this->store_lastBuiltQuery) $this->debug_lastBuiltQuery = $query[0];
00766 } else {
00767 $query = 'INSERT INTO '.$this->quoteFromTables($table).'
00768 (
00769 '.implode(',
00770 ',array_keys($nArr)).'
00771 ) VALUES (
00772 '.implode(',
00773 ',$nArr).'
00774 )';
00775
00776 if ($this->debugOutput || $this->store_lastBuiltQuery) $this->debug_lastBuiltQuery = $query;
00777 }
00778
00779 return $query;
00780 }
00781 }
00782
00783
00784
00785
00786
00787
00788
00789
00790
00791
00792
00793 function UPDATEquery($table,$where,$fields_values,$no_quote_fields='') {
00794
00795 if (is_string($where)) {
00796 if (is_array($fields_values) && count($fields_values)) {
00797
00798 if (is_string($no_quote_fields)) {
00799 $no_quote_fields = explode(',',$no_quote_fields);
00800 } elseif (!is_array($no_quote_fields)) {
00801 $no_quote_fields = array();
00802 }
00803
00804 $blobfields = array();
00805 $nArr = array();
00806 foreach($fields_values as $k => $v) {
00807 if(!$this->runningNative() && $this->sql_field_metatype($table,$k) == 'B') {
00808
00809 $blobfields[$this->quoteFieldNames($k)] = $v;
00810 } elseif(!$this->runningNative() && $this->sql_field_metatype($table,$k) == 'XL') {
00811
00812 $clobfields[$this->quoteFieldNames($k)] = $v;
00813 } else {
00814
00815
00816 $mt = $this->sql_field_metatype($table,$k);
00817 $v = (($mt{0}=='I')||($mt{0}=='F')) ? (int)$v : $v;
00818 $nArr[] = $this->quoteFieldNames($k).'='.((!in_array($k,$no_quote_fields)) ? $this->fullQuoteStr($v, $table) : $v);
00819 }
00820 }
00821
00822 if(count($blobfields) || count($clobfields)) {
00823 if(count($nArr)) {
00824 $query[0] = 'UPDATE '.$this->quoteFromTables($table).'
00825 SET
00826 '.implode(',
00827 ',$nArr).
00828 (strlen($where)>0 ? '
00829 WHERE
00830 '.$this->quoteWhereClause($where) : '');
00831 }
00832 if(count($blobfields)) $query[1] = $blobfields;
00833 if(count($clobfields)) $query[2] = $clobfields;
00834 if ($this->debugOutput || $this->store_lastBuiltQuery) $this->debug_lastBuiltQuery = $query[0];
00835 } else {
00836 $query = 'UPDATE '.$this->quoteFromTables($table).'
00837 SET
00838 '.implode(',
00839 ',$nArr).
00840 (strlen($where)>0 ? '
00841 WHERE
00842 '.$this->quoteWhereClause($where) : '');
00843
00844 if ($this->debugOutput || $this->store_lastBuiltQuery) $this->debug_lastBuiltQuery = $query;
00845 }
00846
00847 return $query;
00848 }
00849 } else {
00850 die('<strong>TYPO3 Fatal Error:</strong> "Where" clause argument for UPDATE query was not a string in $this->UPDATEquery() !');
00851 }
00852 }
00853
00854
00855
00856
00857
00858
00859
00860
00861
00862 function DELETEquery($table,$where) {
00863 if (is_string($where)) {
00864 $table = $this->quoteFromTables($table);
00865 $where = $this->quoteWhereClause($where);
00866
00867 $query = parent::DELETEquery($table, $where);
00868
00869 if ($this->debugOutput || $this->store_lastBuiltQuery) $this->debug_lastBuiltQuery = $query;
00870 return $query;
00871 } else {
00872 die('<strong>TYPO3 Fatal Error:</strong> "Where" clause argument for DELETE query was not a string in $this->DELETEquery() !');
00873 }
00874 }
00875
00876
00877
00878
00879
00880
00881
00882
00883
00884
00885
00886
00887
00888 function SELECTquery($select_fields,$from_table,$where_clause,$groupBy='',$orderBy='',$limit='') {
00889
00890 $select_fields = $this->quoteFieldNames($select_fields);
00891 $from_table = $this->quoteFromTables($from_table);
00892 $where_clause = $this->quoteWhereClause($where_clause);
00893 $groupBy = $this->quoteGroupBy($groupBy);
00894 $orderBy = $this->quoteOrderBy($orderBy);
00895
00896
00897 $query = parent::SELECTquery($select_fields,$from_table,$where_clause,$groupBy,$orderBy,$limit);
00898
00899 if ($this->debugOutput || $this->store_lastBuiltQuery) $this->debug_lastBuiltQuery = $query;
00900
00901 return $query;
00902 }
00903
00904
00905
00906
00907
00908
00909
00910
00911
00912
00913
00914
00915
00916
00917
00918
00919 function quoteSelectFields($select_fields) {
00920 $this->quoteFieldNames($select_fields);
00921 }
00922
00923
00924
00925
00926
00927
00928
00929 function quoteFieldNames($select_fields) {
00930 if($select_fields == '') return '';
00931 if($this->runningNative()) return $select_fields;
00932
00933 $select_fields = $this->SQLparser->parseFieldList($select_fields);
00934 foreach($select_fields as $k => $v) {
00935 if($select_fields[$k]['field'] != '' && $select_fields[$k]['field'] != '*') {
00936 $select_fields[$k]['field'] = $this->handlerInstance[$this->lastHandlerKey]->nameQuote.$select_fields[$k]['field'].$this->handlerInstance[$this->lastHandlerKey]->nameQuote;
00937 }
00938 if($select_fields[$k]['table'] != '') {
00939 $select_fields[$k]['table'] = $this->handlerInstance[$this->lastHandlerKey]->nameQuote.$select_fields[$k]['table'].$this->handlerInstance[$this->lastHandlerKey]->nameQuote;
00940 }
00941 if($select_fields[$k]['as'] != '') {
00942 $select_fields[$k]['as'] = $this->handlerInstance[$this->lastHandlerKey]->nameQuote.$select_fields[$k]['as'].$this->handlerInstance[$this->lastHandlerKey]->nameQuote;
00943 }
00944 if(isset($select_fields[$k]['func_content.']) && $select_fields[$k]['func_content.'][0]['func_content'] != '*'){
00945 $select_fields[$k]['func_content.'][0]['func_content'] = $this->quoteFieldNames($select_fields[$k]['func_content.'][0]['func_content']);
00946 $select_fields[$k]['func_content'] = $this->quoteFieldNames($select_fields[$k]['func_content']);
00947 }
00948 }
00949
00950 return $this->SQLparser->compileFieldList($select_fields);
00951 }
00952
00953
00954
00955
00956
00957
00958
00959 function quoteFromTables($from_table) {
00960 if($from_table == '') return '';
00961 if($this->runningNative()) return $from_table;
00962
00963 $from_table = $this->SQLparser->parseFromTables($from_table);
00964 foreach($from_table as $k => $v) {
00965 $from_table[$k]['table'] = $this->handlerInstance[$this->lastHandlerKey]->nameQuote.$from_table[$k]['table'].$this->handlerInstance[$this->lastHandlerKey]->nameQuote;
00966 if($from_table[$k]['as'] != '') {
00967 $from_table[$k]['as'] = $this->handlerInstance[$this->lastHandlerKey]->nameQuote.$from_table[$k]['as'].$this->handlerInstance[$this->lastHandlerKey]->nameQuote;
00968 }
00969 if (is_array($v['JOIN'])) {
00970 $from_table[$k]['JOIN']['withTable'] = $this->handlerInstance[$this->lastHandlerKey]->nameQuote.$from_table[$k]['JOIN']['withTable'].$this->handlerInstance[$this->lastHandlerKey]->nameQuote;
00971 $from_table[$k]['JOIN']['ON'][0]['table'] = ($from_table[$k]['JOIN']['ON'][0]['table']) ? $this->handlerInstance[$this->lastHandlerKey]->nameQuote.$from_table[$k]['JOIN']['ON'][0]['table'].$this->handlerInstance[$this->lastHandlerKey]->nameQuote : '';
00972 $from_table[$k]['JOIN']['ON'][0]['field'] = $this->handlerInstance[$this->lastHandlerKey]->nameQuote.$from_table[$k]['JOIN']['ON'][0]['field'].$this->handlerInstance[$this->lastHandlerKey]->nameQuote;
00973 $from_table[$k]['JOIN']['ON'][1]['table'] = ($from_table[$k]['JOIN']['ON'][1]['table']) ? $this->handlerInstance[$this->lastHandlerKey]->nameQuote.$from_table[$k]['JOIN']['ON'][1]['table'].$this->handlerInstance[$this->lastHandlerKey]->nameQuote : '';
00974 $from_table[$k]['JOIN']['ON'][1]['field'] = $this->handlerInstance[$this->lastHandlerKey]->nameQuote.$from_table[$k]['JOIN']['ON'][1]['field'].$this->handlerInstance[$this->lastHandlerKey]->nameQuote;
00975 }
00976 }
00977 return $this->SQLparser->compileFromTables($from_table);
00978 }
00979
00980
00981
00982
00983
00984
00985
00986 function quoteWhereClause($where_clause) {
00987 if($where_clause == '' || $this->runningNative()) return $where_clause;
00988
00989 $where_clause = $this->SQLparser->parseWhereClause($where_clause);
00990 if(is_array($where_clause)) {
00991 $where_clause = $this->_quoteWhereClause($where_clause);
00992 $where_clause = $this->SQLparser->compileWhereClause($where_clause);
00993 } else {
00994 die('Could not parse where clause in '.__FILE__.' : '.__LINE__);
00995 }
00996
00997 return $where_clause;
00998 }
00999
01000
01001
01002
01003
01004
01005
01006
01007 function _quoteWhereClause($where_clause) {
01008 foreach($where_clause as $k => $v) {
01009
01010 if (is_array($where_clause[$k]['sub'])) {
01011 $where_clause[$k]['sub'] = $this->_quoteWhereClause($where_clause[$k]['sub']);
01012 } else {
01013 if($where_clause[$k]['table'] != '') {
01014 $where_clause[$k]['table'] = $this->handlerInstance[$this->lastHandlerKey]->nameQuote.$where_clause[$k]['table'].$this->handlerInstance[$this->lastHandlerKey]->nameQuote;
01015 }
01016 if(!is_numeric($where_clause[$k]['field'])) {
01017 $where_clause[$k]['field'] = $this->handlerInstance[$this->lastHandlerKey]->nameQuote.$where_clause[$k]['field'].$this->handlerInstance[$this->lastHandlerKey]->nameQuote;
01018 }
01019 }
01020 if ($where_clause[$k]['comparator']) {
01021
01022 if ((!isset($where_clause[$k]['value'][1]) || $where_clause[$k]['value'][1] == '') && is_string($where_clause[$k]['value'][0]) && strstr($where_clause[$k]['value'][0], '.') && !t3lib_div::inList('NOTIN,IN',strtoupper(str_replace(array(" ","\n","\r","\t"),'',$where_clause[$k]['comparator'])))) {
01023 $where_clause[$k]['value'][0] = $this->quoteFieldNames($where_clause[$k]['value'][0]);
01024 }
01025 }
01026 }
01027
01028 return $where_clause;
01029 }
01030
01031
01032
01033
01034
01035
01036
01037 function quoteGroupBy($groupBy) {
01038 if($groupBy == '') return '';
01039 if($this->runningNative()) return $groupBy;
01040
01041 $groupBy = $this->SQLparser->parseFieldList($groupBy);
01042 foreach($groupBy as $k => $v) {
01043 $groupBy[$k]['field'] = $this->handlerInstance[$this->lastHandlerKey]->nameQuote.$groupBy[$k]['field'].$this->handlerInstance[$this->lastHandlerKey]->nameQuote;
01044 if($groupBy[$k]['table'] != '') {
01045 $groupBy[$k]['table'] = $this->handlerInstance[$this->lastHandlerKey]->nameQuote.$groupBy[$k]['table'].$this->handlerInstance[$this->lastHandlerKey]->nameQuote;
01046 }
01047 }
01048 return $this->SQLparser->compileFieldList($groupBy);
01049 }
01050
01051
01052
01053
01054
01055
01056
01057 function quoteOrderBy($orderBy) {
01058 if($orderBy == '') return '';
01059 if($this->runningNative()) return $orderBy;
01060
01061 $orderBy = $this->SQLparser->parseFieldList($orderBy);
01062 foreach($orderBy as $k => $v) {
01063 $orderBy[$k]['field'] = $this->handlerInstance[$this->lastHandlerKey]->nameQuote.$orderBy[$k]['field'].$this->handlerInstance[$this->lastHandlerKey]->nameQuote;
01064 if($orderBy[$k]['table'] != '') {
01065 $orderBy[$k]['table'] = $this->handlerInstance[$this->lastHandlerKey]->nameQuote.$orderBy[$k]['table'].$this->handlerInstance[$this->lastHandlerKey]->nameQuote;
01066 }
01067 }
01068 return $this->SQLparser->compileFieldList($orderBy);
01069 }
01070
01071
01072
01073
01074
01075
01076
01077
01078
01079
01080
01081
01082
01083
01084
01085
01086
01087 function fullQuoteStr($str,$table) {
01088 return '\''.$this->quoteStr($str, $table).'\'';
01089 }
01090
01091
01092
01093
01094
01095
01096
01097
01098
01099
01100 function quoteStr($str, $table) {
01101 $this->lastHandlerKey = $this->handler_getFromTableList($table);
01102 switch((string)$this->handlerCfg[$this->lastHandlerKey]['type']) {
01103 case 'native':
01104 $str = mysql_real_escape_string($str, $this->handlerInstance[$this->lastHandlerKey]['link']);
01105 break;
01106 case 'adodb':
01107 $str = substr($this->handlerInstance[$this->lastHandlerKey]->qstr($str),1,-1);
01108 break;
01109 case 'userdefined':
01110 $str = $this->handlerInstance[$this->lastHandlerKey]->quoteStr($str);
01111 break;
01112 default:
01113 die('No handler found!!!');
01114 break;
01115 }
01116
01117 return $str;
01118 }
01119
01120
01121
01122
01123
01124
01125
01126
01127
01128 function MetaType($type,$table,$max_length=-1) {
01129 $this->lastHandlerKey = $this->handler_getFromTableList($table);
01130 switch((string)$this->handlerCfg[$this->lastHandlerKey]['type']) {
01131 case 'native':
01132 $str = $type;
01133 break;
01134 case 'adodb':
01135 $rs = $this->handlerInstance[$this->lastHandlerKey]->SelectLimit('SELECT * FROM '.$this->quoteFromTables($table),1);
01136 $str = $rs->MetaType($type, $max_length);
01137 break;
01138 case 'userdefined':
01139 $str = $this->handlerInstance[$this->lastHandlerKey]->MetaType($str,$table,$max_length);
01140 break;
01141 default:
01142 die('No handler found!!!');
01143 break;
01144 }
01145
01146 return $str;
01147 }
01148
01149
01150
01151
01152
01153
01154
01155
01156 function MySQLMetaType($t) {
01157
01158 switch (strtoupper($t)) {
01159 case 'STRING':
01160 case 'CHAR':
01161 case 'VARCHAR':
01162 case 'TINYBLOB':
01163 case 'TINYTEXT':
01164 case 'ENUM':
01165 case 'SET': return 'C';
01166
01167 case 'TEXT':
01168 case 'LONGTEXT':
01169 case 'MEDIUMTEXT': return 'XL';
01170
01171 case 'IMAGE':
01172 case 'LONGBLOB':
01173 case 'BLOB':
01174 case 'MEDIUMBLOB': return 'B';
01175
01176 case 'YEAR':
01177 case 'DATE': return 'D';
01178
01179 case 'TIME':
01180 case 'DATETIME':
01181 case 'TIMESTAMP': return 'T';
01182
01183 case 'FLOAT':
01184 case 'DOUBLE': return 'F';
01185
01186 case 'INT':
01187 case 'INTEGER':
01188 case 'TINYINT':
01189 case 'SMALLINT':
01190 case 'MEDIUMINT':
01191 case 'BIGINT': return 'I8';
01192
01193 default: return 'N';
01194 }
01195 }
01196
01197
01198
01199
01200
01201
01202
01203 function MySQLActualType($meta) {
01204 switch(strtoupper($meta)) {
01205 case 'C': return 'VARCHAR';
01206 case 'XL':
01207 case 'X': return 'LONGTEXT';
01208
01209 case 'C2': return 'VARCHAR';
01210 case 'X2': return 'LONGTEXT';
01211
01212 case 'B': return 'LONGBLOB';
01213
01214 case 'D': return 'DATE';
01215 case 'T': return 'DATETIME';
01216 case 'L': return 'TINYINT';
01217
01218 case 'I':
01219 case 'I1':
01220 case 'I2':
01221 case 'I4':
01222 case 'I8': return 'BIGINT';
01223
01224 case 'F': return 'DOUBLE';
01225 case 'N': return 'NUMERIC';
01226
01227 default: return $meta;
01228 }
01229 }
01230
01231
01232
01233
01234
01235
01236
01237
01238
01239
01240
01241
01242
01243
01244
01245
01246 function sql_error() {
01247
01248 switch($this->handlerCfg[$this->lastHandlerKey]['type']) {
01249 case 'native':
01250 $output = mysql_error($this->handlerInstance[$this->lastHandlerKey]['link']);
01251 break;
01252 case 'adodb':
01253 $output = $this->handlerInstance[$this->lastHandlerKey]->ErrorMsg();
01254 break;
01255 case 'userdefined':
01256 $output = $this->handlerInstance[$this->lastHandlerKey]->sql_error();
01257 break;
01258 }
01259 return $output;
01260 }
01261
01262
01263
01264
01265
01266
01267 function sql_errno() {
01268
01269 switch($this->handlerCfg[$this->lastHandlerKey]['type']) {
01270 case 'native':
01271 $output = mysql_errno($this->handlerInstance[$this->lastHandlerKey]['link']);
01272 break;
01273 case 'adodb':
01274 $output = $this->handlerInstance[$this->lastHandlerKey]->ErrorNo();
01275 break;
01276 case 'userdefined':
01277 $output = $this->handlerInstance[$this->lastHandlerKey]->sql_errno();
01278 break;
01279 }
01280 return $output;
01281 }
01282
01283
01284
01285
01286
01287
01288
01289 function sql_num_rows(&$res) {
01290 if($res === false) return 0;
01291
01292 $handlerType = is_object($res) ? $res->TYPO3_DBAL_handlerType : 'native';
01293 switch($handlerType) {
01294 case 'native':
01295 $output = mysql_num_rows($res);
01296 break;
01297 case 'adodb':
01298 $output = method_exists($res, 'RecordCount') ? $res->RecordCount() : 0;
01299 break;
01300 case 'userdefined':
01301 $output = $res->sql_num_rows();
01302 break;
01303 }
01304 return $output;
01305 }
01306
01307
01308
01309
01310
01311
01312
01313 function sql_fetch_assoc(&$res) {
01314 $output = array();
01315
01316 $handlerType = is_object($res) ? $res->TYPO3_DBAL_handlerType : (is_resource($res) ? 'native' : false);
01317 switch($handlerType) {
01318 case 'native':
01319 $output = mysql_fetch_assoc($res);
01320 $tableList = $this->resourceIdToTableNameMap[(string)$res];
01321 break;
01322 case 'adodb':
01323
01324
01325
01326 if(method_exists($res, 'FetchRow')) {
01327 $output = $res->FetchRow();
01328 $tableList = $res->TYPO3_DBAL_tableList;
01329
01330
01331
01332 if (is_array($output)) {
01333 foreach($output as $key => $value) {
01334 if (is_integer($key)) unset($output[$key]);
01335 elseif($value===' ' && $this->runningADOdbDriver('mssql')) $output[$key]='';
01336 }
01337 }
01338 }
01339 break;
01340 case 'userdefined':
01341 $output = $res->sql_fetch_assoc();
01342 $tableList = $res->TYPO3_DBAL_tableList;
01343 break;
01344 }
01345
01346
01347 if (is_array($output)) {
01348 if ($tables = $this->map_needMapping($tableList,TRUE)) {
01349 $output = $this->map_assocArray($output,$tables,1);
01350 }
01351 }
01352
01353
01354 return $output;
01355 }
01356
01357
01358
01359
01360
01361
01362
01363
01364 function sql_fetch_row(&$res) {
01365 $handlerType = is_object($res) ? $res->TYPO3_DBAL_handlerType : 'native';
01366 switch($handlerType) {
01367 case 'native':
01368 $output = mysql_fetch_row($res);
01369 break;
01370 case 'adodb':
01371
01372
01373
01374 if(method_exists($res, 'FetchRow')) {
01375 $output = $res->FetchRow();
01376
01377
01378
01379 if (is_array($output)) {
01380 foreach($output as $key => $value) {
01381 if (!is_integer($key)) unset($output[$key]);
01382 elseif($value===' ' && $this->runningADOdbDriver('mssql')) $output[$key]='';
01383 }
01384 }
01385 }
01386 break;
01387 case 'userdefined':
01388 $output = $res->sql_fetch_row();
01389 break;
01390 }
01391 return $output;
01392 }
01393
01394
01395
01396
01397
01398
01399
01400 function sql_free_result(&$res) {
01401 if($res===false) return false;
01402
01403 $handlerType = is_object($res) ? $res->TYPO3_DBAL_handlerType : 'native';
01404 switch($handlerType) {
01405 case 'native':
01406 $output = mysql_free_result($res);
01407 break;
01408 case 'adodb':
01409 if(method_exists($res, 'Close')) {
01410 $res->Close();
01411 unset($res);
01412 $output = true;
01413 } else {
01414 $output = false;
01415 }
01416 break;
01417 case 'userdefined':
01418 unset($res);
01419 break;
01420 }
01421 return $output;
01422 }
01423
01424
01425
01426
01427
01428
01429 function sql_insert_id() {
01430
01431 switch($this->handlerCfg[$this->lastHandlerKey]['type']) {
01432 case 'native':
01433 $output = mysql_insert_id($this->handlerInstance[$this->lastHandlerKey]['link']);
01434 break;
01435 case 'adodb':
01436 $output = $this->handlerInstance[$this->lastHandlerKey]->last_insert_id;
01437 break;
01438 case 'userdefined':
01439 $output = $this->handlerInstance[$this->lastHandlerKey]->sql_insert_id();
01440 break;
01441 }
01442 return $output;
01443 }
01444
01445
01446
01447
01448
01449
01450 function sql_affected_rows() {
01451
01452 switch($this->handlerCfg[$this->lastHandlerKey]['type']) {
01453 case 'native':
01454 $output = mysql_affected_rows();
01455 break;
01456 case 'adodb':
01457 $output = $this->handlerInstance[$this->lastHandlerKey]->Affected_Rows();
01458 break;
01459 case 'userdefined':
01460 $output = $this->handlerInstance[$this->lastHandlerKey]->sql_affected_rows();
01461 break;
01462 }
01463 return $output;
01464 }
01465
01466
01467
01468
01469
01470
01471
01472
01473 function sql_data_seek(&$res,$seek) {
01474
01475 $handlerType = is_object($res) ? $res->TYPO3_DBAL_handlerType : 'native';
01476 switch($handlerType) {
01477 case 'native':
01478 $output = mysql_data_seek($res,$seek);
01479 break;
01480 case 'adodb':
01481 $output = $res->Move($seek);
01482 break;
01483 case 'userdefined':
01484 $output = $res->sql_data_seek($seek);
01485 break;
01486 }
01487 return $output;
01488 }
01489
01490
01491
01492
01493
01494
01495
01496
01497
01498
01499 function sql_field_metatype($table,$field) {
01500
01501 foreach ($this->mapping as $tableName => $tableMapInfo) {
01502 if (isset($tableMapInfo['mapTableName']) && $tableMapInfo['mapTableName'] === $table) {
01503
01504 $table = $tableName;
01505 }
01506
01507 if (isset($tableMapInfo['mapFieldNames'])) {
01508 foreach ($tableMapInfo['mapFieldNames'] as $fieldName => $fieldMapInfo) {
01509 if ($fieldMapInfo === $field) {
01510
01511 $field = $fieldName;
01512 }
01513 }
01514 }
01515 }
01516
01517 return $this->cache_fieldType[$table][$field]['metaType'];
01518 }
01519
01520
01521
01522
01523
01524
01525
01526
01527
01528
01529 function sql_field_type(&$res,$pointer) {
01530 if($res === null) {
01531 debug(array('no res in sql_field_type!'));
01532 return 'text';
01533 }
01534 else if(is_string($res)){
01535 if($res == 'tx_dbal_debuglog') return 'text';
01536 $handlerType = 'adodb';
01537 }
01538 else {
01539 $handlerType = is_object($res) ? $res->TYPO3_DBAL_handlerType : 'native';
01540 }
01541
01542 switch($handlerType) {
01543 case 'native':
01544 $output = mysql_field_type($res,$pointer);
01545 break;
01546 case 'adodb':
01547 if(is_string($pointer)){
01548 $output = $this->cache_fieldType[$res][$pointer]['type'];
01549 }
01550
01551 break;
01552 case 'userdefined':
01553 $output = $res->sql_field_type($pointer);
01554 break;
01555 }
01556
01557 return $output;
01558 }
01559
01560
01561
01562
01563
01564
01565
01566
01567
01568
01569
01570
01571
01572
01573
01574
01575
01576
01577
01578
01579
01580
01581
01582
01583 function sql($db,$query) {
01584 return $this->sql_query($query);
01585 }
01586
01587
01588
01589
01590
01591
01592
01593
01594
01595
01596
01597 function sql_query($query) {
01598
01599 switch($this->handlerCfg['_DEFAULT']['type']) {
01600 case 'native':
01601 $sqlResult = mysql_query($query, $this->handlerInstance['_DEFAULT']['link']);
01602 break;
01603 case 'adodb':
01604 $sqlResult = $this->handlerInstance['_DEFAULT']->Execute($query);
01605 $sqlResult->TYPO3_DBAL_handlerType = 'adodb';
01606 break;
01607 case 'userdefined':
01608 $sqlResult = $this->handlerInstance['_DEFAULT']->sql_query($query);
01609 $sqlResult->TYPO3_DBAL_handlerType = 'userdefined';
01610 break;
01611 }
01612
01613 if ($this->printErrors && $this->sql_error()) {
01614 debug(array($this->lastQuery, $this->sql_error()));
01615 }
01616
01617 return $sqlResult;
01618 }
01619
01620
01621
01622
01623
01624
01625
01626
01627
01628
01629
01630
01631
01632 function sql_pconnect($TYPO3_db_host, $TYPO3_db_username, $TYPO3_db_password) {
01633
01634 $this->handlerCfg['_DEFAULT']['config']['username'] = $TYPO3_db_username;
01635 $this->handlerCfg['_DEFAULT']['config']['password'] = $TYPO3_db_password;
01636 $this->handlerCfg['_DEFAULT']['config']['host'] = $TYPO3_db_host;
01637 $this->handlerCfg['_DEFAULT']['config']['database'] = TYPO3_db;
01638
01639
01640 $sqlResult = $this->handler_init('_DEFAULT');
01641 return $sqlResult;
01642 }
01643
01644
01645
01646
01647
01648
01649
01650
01651 function sql_select_db($TYPO3_db) {
01652 return TRUE;
01653 }
01654
01655
01656
01657
01658
01659
01660
01661
01662
01663
01664
01665
01666
01667
01668
01669
01670
01671
01672
01673
01674
01675
01676
01677
01678
01679
01680
01681
01682
01683 function admin_get_dbs() {
01684 $dbArr = array();
01685 switch($this->handlerCfg['_DEFAULT']['type']) {
01686 case 'native':
01687 $db_list = mysql_list_dbs($this->link);
01688 while ($row = mysql_fetch_object($db_list)) {
01689 if ($this->sql_select_db($row->Database)) {
01690 $dbArr[] = $row->Database;
01691 }
01692 }
01693 break;
01694 case 'adodb':
01695
01696
01697 if(method_exists($this->handlerInstance['_DEFAULT'],'MetaDatabases')) {
01698 $sqlDBs = $this->handlerInstance['_DEFAULT']->MetaDatabases();
01699 if(is_array($sqlDBs)) {
01700 foreach($sqlDBs as $k => $theDB) {
01701 $dbArr[] = $theDB;
01702 }
01703 }
01704 }
01705 break;
01706 case 'userdefined':
01707 $dbArr = $this->handlerInstance['_DEFAULT']->admin_get_tables();
01708 break;
01709 }
01710
01711 return $dbArr;
01712 }
01713
01714
01715
01716
01717
01718
01719
01720
01721
01722
01723
01724 function admin_get_tables() {
01725 $whichTables = array();
01726
01727
01728 switch($this->handlerCfg['_DEFAULT']['type']) {
01729 case 'native':
01730 $tables_result = mysql_list_tables(TYPO3_db, $this->handlerInstance['_DEFAULT']['link']);
01731 if (!$this->sql_error()) {
01732 while ($theTable = $this->sql_fetch_assoc($tables_result)) {
01733 $whichTables[current($theTable)] = current($theTable);
01734 }
01735 }
01736 break;
01737 case 'adodb':
01738 $sqlTables = $this->handlerInstance['_DEFAULT']->MetaTables('TABLES');
01739 while (list($k, $theTable) = each($sqlTables)) {
01740 if(preg_match('/BIN\$/', $theTable)) continue;
01741 $whichTables[$theTable] = $theTable;
01742 }
01743 break;
01744 case 'userdefined':
01745 $whichTables = $this->handlerInstance['_DEFAULT']->admin_get_tables();
01746 break;
01747 }
01748
01749
01750 if (is_array($this->mapping) && count($this->mapping)) {
01751
01752
01753 $tMap = array();
01754 foreach($this->mapping as $tN => $tMapInfo) {
01755 if (isset($tMapInfo['mapTableName'])) $tMap[$tMapInfo['mapTableName']]=$tN;
01756 }
01757
01758
01759 $newList=array();
01760 foreach($whichTables as $tN) {
01761 if (isset($tMap[$tN])) $tN = $tMap[$tN];
01762 $newList[$tN] = $tN;
01763 }
01764
01765 $whichTables = $newList;
01766 }
01767
01768
01769 if (is_array($this->table2handlerKeys)) {
01770 foreach($this->table2handlerKeys as $key => $handlerKey) {
01771 $whichTables[$key] = $key;
01772 }
01773 }
01774
01775 return $whichTables;
01776 }
01777
01778
01779
01780
01781
01782
01783
01784
01785
01786 function admin_get_fields($tableName) {
01787 $output = array();
01788
01789
01790 $ORIG_tableName = $tableName;
01791 if ($tableArray = $this->map_needMapping($tableName)) {
01792
01793
01794 if ($this->mapping[$tableName]['mapTableName']) {
01795 $tableName = $this->mapping[$tableName]['mapTableName'];
01796 }
01797 }
01798
01799
01800 $this->lastHandlerKey = $this->handler_getFromTableList($ORIG_tableName);
01801 switch((string)$this->handlerCfg[$this->lastHandlerKey]['type']) {
01802 case 'native':
01803 $columns_res = mysql_query('SHOW columns FROM '.$tableName, $this->handlerInstance[$this->lastHandlerKey]['link']);
01804 while($fieldRow = mysql_fetch_assoc($columns_res)) {
01805 $output[$fieldRow['Field']] = $fieldRow;
01806 }
01807 break;
01808 case 'adodb':
01809 $fieldRows = $this->handlerInstance[$this->lastHandlerKey]->MetaColumns($tableName, false);
01810 if(is_array($fieldRows)) {
01811 foreach($fieldRows as $k => $fieldRow) {
01812 settype($fieldRow, 'array');
01813 $fieldRow['Field'] = $fieldRow['name'];
01814 $ntype = $this->MySQLActualType($this->MetaType($fieldRow['type'],$tableName));
01815 $ntype .= (($fieldRow['max_length'] != -1) ? (($ntype == 'INT') ? '(11)' :'('.$fieldRow['max_length'].')') : '');
01816 $fieldRow['Type'] = strtolower($ntype);
01817 $fieldRow['Null'] = '';
01818 $fieldRow['Key'] = '';
01819 $fieldRow['Default'] = $fieldRow['default_value'];
01820 $fieldRow['Extra'] = '';
01821 $output[$fieldRow['name']] = $fieldRow;
01822 }
01823 }
01824 break;
01825 case 'userdefined':
01826 $output = $this->handlerInstance[$this->lastHandlerKey]->admin_get_fields($tableName);
01827 break;
01828 }
01829
01830
01831 if (is_array($tableArray) && is_array($this->mapping[$ORIG_tableName]['mapFieldNames'])) {
01832 $revFields = array_flip($this->mapping[$ORIG_tableName]['mapFieldNames']);
01833
01834 $newOutput = array();
01835 foreach($output as $fN => $fInfo) {
01836 if (isset($revFields[$fN])) {
01837 $fN = $revFields[$fN];
01838 $fInfo['Field'] = $fN;
01839 }
01840 $newOutput[$fN] = $fInfo;
01841 }
01842 $output = $newOutput;
01843 }
01844
01845 return $output;
01846 }
01847
01848
01849
01850
01851
01852
01853
01854
01855 function admin_get_keys($tableName) {
01856 $output = array();
01857
01858
01859 $ORIG_tableName = $tableName;
01860 if ($tableArray = $this->map_needMapping($tableName)) {
01861
01862
01863 if ($this->mapping[$tableName]['mapTableName']) {
01864 $tableName = $this->mapping[$tableName]['mapTableName'];
01865 }
01866 }
01867
01868
01869 $this->lastHandlerKey = $this->handler_getFromTableList($ORIG_tableName);
01870 switch((string)$this->handlerCfg[$this->lastHandlerKey]['type']) {
01871 case 'native':
01872 $keyRes = mysql_query('SHOW keys FROM '.$tableName, $this->handlerInstance[$this->lastHandlerKey]['link']);
01873 while($keyRow = mysql_fetch_assoc($keyRes)) {
01874 $output[] = $keyRow;
01875 }
01876 break;
01877 case 'adodb':
01878 $keyRows = $this->handlerInstance[$this->lastHandlerKey]->MetaIndexes($tableName);
01879 if($keyRows !== false) {
01880 while (list($k, $theKey) = each($keyRows)) {
01881 $theKey['Table'] = $tableName;
01882 $theKey['Non_unique'] = (int) !$theKey['unique'];
01883 $theKey['Key_name'] = str_replace($tableName.'_','',$k);
01884
01885
01886 $theKey['Collation'] = '';
01887 $theKey['Cardinality'] = '';
01888 $theKey['Sub_part'] = '';
01889 $theKey['Packed'] = '';
01890 $theKey['Null'] = '';
01891 $theKey['Index_type'] = '';
01892 $theKey['Comment'] = '';
01893
01894
01895 $keycols = $theKey['columns'];
01896 while (list($c, $theCol) = each($keycols)) {
01897 $theKey['Seq_in_index'] = $c+1;
01898 $theKey['Column_name'] = $theCol;
01899 $output[] = $theKey;
01900 }
01901 }
01902 }
01903 $priKeyRow = $this->handlerInstance[$this->lastHandlerKey]->MetaPrimaryKeys($tableName);
01904 $theKey = array();
01905 $theKey['Table'] = $tableName;
01906 $theKey['Non_unique'] = 0;
01907 $theKey['Key_name'] = 'PRIMARY';
01908
01909
01910 $theKey['Collation'] = '';
01911 $theKey['Cardinality'] = '';
01912 $theKey['Sub_part'] = '';
01913 $theKey['Packed'] = '';
01914 $theKey['Null'] = '';
01915 $theKey['Index_type'] = '';
01916 $theKey['Comment'] = '';
01917
01918
01919 if($priKeyRow !== false) {
01920 while (list($c, $theCol) = each($priKeyRow)) {
01921 $theKey['Seq_in_index'] = $c+1;
01922 $theKey['Column_name'] = $theCol;
01923 $output[] = $theKey;
01924 }
01925 }
01926 break;
01927 case 'userdefined':
01928 $output = $this->handlerInstance[$this->lastHandlerKey]->admin_get_keys($tableName);
01929 break;
01930 }
01931
01932
01933 if (is_array($tableArray) && is_array($this->mapping[$ORIG_tableName]['mapFieldNames'])) {
01934 $revFields = array_flip($this->mapping[$ORIG_tableName]['mapFieldNames']);
01935
01936 $newOutput = array();
01937 foreach($output as $kN => $kInfo) {
01938
01939 $kInfo['Table'] = $ORIG_tableName;
01940
01941
01942 if (isset($revFields[$kInfo['Column_name']])) {
01943 $kInfo['Column_name'] = $revFields[$kInfo['Column_name']];
01944 }
01945
01946
01947 $newOutput[$kN] = $kInfo;
01948 }
01949 $output = $newOutput;
01950 }
01951
01952 return $output;
01953 }
01954
01955
01956
01957
01958
01959
01960 function admin_get_charsets() {
01961 return array();
01962 }
01963
01964
01965
01966
01967
01968
01969
01970 function admin_query($query) {
01971 $parsedQuery = $this->SQLparser->parseSQL($query);
01972 $ORIG_table = $parsedQuery['TABLE'];
01973
01974 if (is_array($parsedQuery)) {
01975
01976
01977 switch($parsedQuery['type']) {
01978 case 'CREATETABLE':
01979 case 'ALTERTABLE':
01980 case 'DROPTABLE':
01981 if(file_exists(PATH_typo3conf.'temp_fieldInfo.php')) unlink(PATH_typo3conf.'temp_fieldInfo.php');
01982 $this->map_genericQueryParsed($parsedQuery);
01983 break;
01984 case 'INSERT':
01985 $this->map_genericQueryParsed($parsedQuery);
01986 break;
01987 case 'CREATEDATABASE':
01988 die('Creating a database with DBAL is not supported. Did you really read the manual?');
01989 break;
01990 default:
01991 die('ERROR: Invalid Query type ('.$parsedQuery['type'].') for ->admin_query() function!: "'.htmlspecialchars($query).'"');
01992 break;
01993 }
01994
01995
01996 $this->lastParsedAndMappedQueryArray = $parsedQuery;
01997
01998
01999 $this->lastHandlerKey = $this->handler_getFromTableList($ORIG_table);
02000 switch((string)$this->handlerCfg[$this->lastHandlerKey]['type']) {
02001 case 'native':
02002
02003 $compiledQuery = $this->SQLparser->compileSQL($this->lastParsedAndMappedQueryArray);
02004
02005 if($this->lastParsedAndMappedQueryArray['type']=='INSERT') {
02006 return mysql_query($compiledQuery, $this->link);
02007 }
02008 return mysql_query($compiledQuery[0], $this->link);
02009 break;
02010 case 'adodb':
02011
02012 $compiledQuery = $this->SQLparser->compileSQL($this->lastParsedAndMappedQueryArray);
02013 if($this->lastParsedAndMappedQueryArray['type']=='INSERT') {
02014 return $this->exec_INSERTquery($this->lastParsedAndMappedQueryArray['TABLE'],$compiledQuery);
02015 }
02016 return $this->handlerInstance[$this->lastHandlerKey]->DataDictionary->ExecuteSQLArray($compiledQuery);
02017 break;
02018 case 'userdefined':
02019
02020 $compiledQuery = $this->SQLparser->compileSQL($this->lastParsedAndMappedQueryArray);
02021
02022 return $this->handlerInstance[$this->lastHandlerKey]->admin_query($compiledQuery);
02023 break;
02024 }
02025 } else die('ERROR: Query could not be parsed: "'.htmlspecialchars($parsedQuery).'". Query: "'.htmlspecialchars($query).'"');
02026 }
02027
02028
02029
02030
02031
02032
02033
02034
02035
02036
02037
02038
02039
02040
02041
02042
02043
02044
02045
02046
02047
02048
02049
02050 function handler_getFromTableList($tableList) {
02051
02052 $key = $tableList;
02053
02054 if (!isset($this->cache_handlerKeyFromTableList[$key])) {
02055
02056
02057 $_tableList = $tableList;
02058 $tableArray = $this->SQLparser->parseFromTables($_tableList);
02059
02060
02061 if (is_array($tableArray) && count($tableArray)) {
02062 $outputHandlerKey = '';
02063
02064 foreach($tableArray as $vArray) {
02065
02066 $handlerKey = $this->table2handlerKeys[$vArray['table']] ? $this->table2handlerKeys[$vArray['table']] : '_DEFAULT';
02067
02068
02069 if ($outputHandlerKey && $handlerKey != $outputHandlerKey) {
02070 die('DBAL fatal error: Tables in this list "'.$tableList.'" didn\'t use the same DB handler!');
02071 }
02072
02073 $outputHandlerKey = $handlerKey;
02074 }
02075
02076
02077 if (!isset($this->handlerInstance[$outputHandlerKey])) {
02078 $this->handler_init($outputHandlerKey);
02079 }
02080
02081
02082 $this->cache_handlerKeyFromTableList[$key] = $outputHandlerKey;
02083 } else {
02084 die('DBAL fatal error: No handler found in handler_getFromTableList() for: "'.$tableList.'" ('.$tableArray.')');
02085 }
02086 }
02087
02088 return $this->cache_handlerKeyFromTableList[$key];
02089 }
02090
02091
02092
02093
02094
02095
02096
02097
02098 function handler_init($handlerKey) {
02099
02100
02101 $cfgArray = $this->handlerCfg[$handlerKey];
02102 $handlerType = (string)$cfgArray['type'];
02103 $output = FALSE;
02104
02105 if (is_array($cfgArray)) {
02106 switch($handlerType) {
02107 case 'native':
02108 if ($GLOBALS['TYPO3_CONF_VARS']['SYS']['no_pconnect']) {
02109 $link = mysql_connect($cfgArray['config']['host'].(isset($cfgArray['config']['port']) ? ':'.$cfgArray['config']['port'] : ''), $cfgArray['config']['username'], $cfgArray['config']['password'], true);
02110 } else {
02111 $link = mysql_pconnect($cfgArray['config']['host'].(isset($cfgArray['config']['port']) ? ':'.$cfgArray['config']['port'] : ''), $cfgArray['config']['username'], $cfgArray['config']['password']);
02112 }
02113
02114
02115 $this->handlerInstance[$handlerKey] = array('handlerType' => 'native', 'link' => $link);
02116
02117
02118 if ($link) {
02119
02120 if ($handlerKey == '_DEFAULT') {
02121 $this->link = $link;
02122 }
02123
02124
02125 if (mysql_select_db($cfgArray['config']['database'], $link)) {
02126 $output = TRUE;
02127 }
02128 $setDBinit = t3lib_div::trimExplode(chr(10), $GLOBALS['TYPO3_CONF_VARS']['SYS']['setDBinit'], 1);
02129 foreach ($setDBinit as $v) {
02130 if (mysql_query($v, $this->link) === FALSE) {
02131 t3lib_div::sysLog('Could not initialize DB connection with query "'.$v.'".','Core',3);
02132 }
02133 }
02134 } else {
02135 t3lib_div::sysLog('Could not connect to MySQL server '.$cfgArray['config']['host'].' with user '.$cfgArray['config']['username'].'.','Core',4);
02136 }
02137 break;
02138 case 'adodb':
02139 $output = true;
02140 require_once(t3lib_extMgm::extPath('adodb').'adodb/adodb.inc.php');
02141 if(!defined('ADODB_FORCE_NULLS')) define('ADODB_FORCE_NULLS', 1);
02142 $GLOBALS['ADODB_FORCE_TYPE'] = ADODB_FORCE_VALUE;
02143 $GLOBALS['ADODB_FETCH_MODE'] = ADODB_FETCH_BOTH;
02144
02145 $this->handlerInstance[$handlerKey] = &ADONewConnection($cfgArray['config']['driver']);
02146
02147
02148 if (isset($cfgArray['config']['driverOptions'])) {
02149 foreach ($cfgArray['config']['driverOptions'] as $optionName => $optionValue) {
02150 $optionSetterName = 'set' . ucfirst($optionName);
02151 if (method_exists($this->handlerInstance[$handlerKey], $optionSetterName)) {
02152 $this->handlerInstance[$handlerKey]->$optionSetterName($optionValue);
02153 } else {
02154 $this->handlerInstance[$handlerKey]->$optionName = $optionValue;
02155 }
02156 }
02157 }
02158
02159 if ($GLOBALS['TYPO3_CONF_VARS']['SYS']['no_pconnect']) {
02160 $this->handlerInstance[$handlerKey]->Connect($cfgArray['config']['host'].(isset($cfgArray['config']['port']) ? ':'.$cfgArray['config']['port'] : ''),$cfgArray['config']['username'],$cfgArray['config']['password'],$cfgArray['config']['database']);
02161 } else {
02162 $this->handlerInstance[$handlerKey]->PConnect($cfgArray['config']['host'].(isset($cfgArray['config']['port']) ? ':'.$cfgArray['config']['port'] : ''),$cfgArray['config']['username'],$cfgArray['config']['password'],$cfgArray['config']['database']);
02163 }
02164 if(!$this->handlerInstance[$handlerKey]->isConnected()) {
02165 $dsn = $cfgArray['config']['driver'].'://'.$cfgArray['config']['username'].
02166 (strlen($cfgArray['config']['password']) ? ':XXXX@' : '').
02167 $cfgArray['config']['host'].(isset($cfgArray['config']['port']) ? ':'.$cfgArray['config']['port'] : '').'/'.$cfgArray['config']['database'].
02168 ($GLOBALS['TYPO3_CONF_VARS']['SYS']['no_pconnect'] ? '' : '?persistent=1');
02169 t3lib_div::sysLog('Could not connect to DB server using ADOdb on '.$cfgArray['config']['host'].' with user '.$cfgArray['config']['username'].'.','Core',4);
02170 error_log('DBAL error: Connection to '.$dsn.' failed. Maybe PHP doesn\'t support the database?');
02171 $output = false;
02172 } else {
02173 $this->handlerInstance[$handlerKey]->DataDictionary = NewDataDictionary($this->handlerInstance[$handlerKey]);
02174 $this->handlerInstance[$handlerKey]->last_insert_id = 0;
02175 if(isset($cfgArray['config']['sequenceStart'])) {
02176 $this->handlerInstance[$handlerKey]->sequenceStart = $cfgArray['config']['sequenceStart'];
02177 } else {
02178 $this->handlerInstance[$handlerKey]->sequenceStart = 1;
02179 }
02180 }
02181 break;
02182 case 'userdefined':
02183
02184 $fileName = t3lib_div::getFileAbsFileName($cfgArray['config']['classFile']);
02185 if (@is_file($fileName)) {
02186 require_once($fileName);
02187 } else die('DBAL error: "'.$fileName.'" was not a file to include.');
02188
02189
02190 $this->handlerInstance[$handlerKey] = t3lib_div::makeInstance($cfgArray['config']['class']);
02191 $this->handlerInstance[$handlerKey]->init($cfgArray,$this);
02192
02193 if (is_object($this->handlerInstance[$handlerKey])) {
02194 $output = TRUE;
02195 }
02196 break;
02197 default:
02198 die('ERROR: Invalid handler type: "'.$cfgArray['type'].'"');
02199 break;
02200 }
02201
02202 return $output;
02203 } else die('ERROR: No handler for key "'.$handlerKey.'"');
02204 }
02205
02206
02207
02208
02209
02210
02211
02212 function runningNative() {
02213 return ((string)$this->handlerCfg[$this->lastHandlerKey]['type']==='native');
02214 }
02215
02216
02217
02218
02219
02220
02221
02222
02223 function runningADOdbDriver($driver) {
02224 return strstr($this->handlerCfg[$this->lastHandlerKey]['config']['driver'], $driver);
02225 }
02226
02227
02228
02229
02230
02231
02232
02233
02234
02235
02236
02237
02238
02239
02240
02241
02242
02243
02244
02245
02246
02247
02248
02249 function map_needMapping($tableList,$fieldMappingOnly=FALSE) {
02250
02251 $key = $tableList.'|'.$fieldMappingOnly;
02252 if (!isset($this->cache_mappingFromTableList[$key])) {
02253 $this->cache_mappingFromTableList[$key] = FALSE;
02254
02255 $tables = $this->SQLparser->parseFromTables($tableList);
02256 if (is_array($tables)) {
02257 foreach($tables as $tableCfg) {
02258 if ($fieldMappingOnly) {
02259 if (is_array($this->mapping[$tableCfg['table']]['mapFieldNames'])) {
02260 $this->cache_mappingFromTableList[$key] = $tables;
02261 }
02262 } else {
02263 if (is_array($this->mapping[$tableCfg['table']])) {
02264 $this->cache_mappingFromTableList[$key] = $tables;
02265 }
02266 }
02267 }
02268 }
02269 }
02270
02271 return $this->cache_mappingFromTableList[$key];
02272 }
02273
02274
02275
02276
02277
02278
02279
02280
02281
02282
02283
02284
02285 function map_assocArray($input,$tables,$rev=FALSE) {
02286
02287
02288 foreach($tables as $tableCfg) {
02289 if (is_array($this->mapping[$tableCfg['table']]['mapFieldNames'])) {
02290
02291
02292 if ($rev) {
02293 $theMap = array_flip($this->mapping[$tableCfg['table']]['mapFieldNames']);
02294 } else {
02295 $theMap = $this->mapping[$tableCfg['table']]['mapFieldNames'];
02296 }
02297
02298
02299 $output = array();
02300 foreach($input as $fN => $value) {
02301
02302
02303 if ($theMap[$fN]) {
02304 $newKey = $theMap[$fN];
02305 } else {
02306 $newKey = $fN;
02307 }
02308
02309
02310 $output[$newKey] = $value;
02311 }
02312
02313
02314 $input = $output;
02315 }
02316 }
02317
02318
02319 return $input;
02320 }
02321
02322
02323
02324
02325
02326
02327
02328
02329
02330
02331
02332
02333
02334 function map_remapSELECTQueryParts(&$select_fields,&$from_table,&$where_clause,&$groupBy,&$orderBy) {
02335
02336
02337 $tables = $this->SQLparser->parseFromTables($from_table);
02338 $defaultTable = $tables[0]['table'];
02339 foreach($tables as $k => $v) {
02340 if ($this->mapping[$v['table']]['mapTableName']) {
02341 $tables[$k]['table'] = $this->mapping[$v['table']]['mapTableName'];
02342 }
02343 }
02344 $from_table = $this->SQLparser->compileFromTables($tables);
02345
02346
02347 $whereParts = $this->SQLparser->parseWhereClause($where_clause);
02348 $this->map_sqlParts($whereParts,$defaultTable);
02349 $where_clause = $this->SQLparser->compileWhereClause($whereParts, false);
02350
02351
02352 $expFields = $this->SQLparser->parseFieldList($select_fields);
02353 $this->map_sqlParts($expFields,$defaultTable);
02354 $select_fields = $this->SQLparser->compileFieldList($expFields);
02355
02356
02357 $expFields = $this->SQLparser->parseFieldList($groupBy);
02358 $this->map_sqlParts($expFields,$defaultTable);
02359 $groupBy = $this->SQLparser->compileFieldList($expFields);
02360
02361
02362 $expFields = $this->SQLparser->parseFieldList($orderBy);
02363 $this->map_sqlParts($expFields,$defaultTable);
02364 $orderBy = $this->SQLparser->compileFieldList($expFields);
02365 }
02366
02367
02368
02369
02370
02371
02372
02373
02374
02375
02376 function map_sqlParts(&$sqlPartArray, $defaultTable) {
02377
02378
02379 if (is_array($sqlPartArray)) {
02380 foreach($sqlPartArray as $k => $v) {
02381
02382
02383 if (is_array($sqlPartArray[$k]['sub'])) {
02384 $this->map_sqlParts($sqlPartArray[$k]['sub'], $defaultTable);
02385 } else {
02386
02387 $t = $sqlPartArray[$k]['table'] ? $sqlPartArray[$k]['table'] : $defaultTable;
02388
02389
02390 if (is_array($this->mapping[$t]['mapFieldNames']) && $this->mapping[$t]['mapFieldNames'][$sqlPartArray[$k]['field']]) {
02391 $sqlPartArray[$k]['field'] = $this->mapping[$t]['mapFieldNames'][$sqlPartArray[$k]['field']];
02392 }
02393
02394
02395
02396 if (!is_numeric($sqlPartArray[$k]['value'][0]) && !isset($sqlPartArray[$k]['value'][1])) {
02397 $fieldArray = explode('.', $sqlPartArray[$k]['value'][0]);
02398 if (count($fieldArray) == 1 && is_array($this->mapping[$t]['mapFieldNames']) && isset($this->mapping[$t]['mapFieldNames'][$fieldArray[0]])) {
02399 $sqlPartArray[$k]['value'][0] = $this->mapping[$t]['mapFieldNames'][$fieldArray[0]];
02400 } elseif (count($fieldArray) == 2) {
02401
02402 $table = $fieldArray[0];
02403 if (isset($this->mapping[$fieldArray[0]]['mapTableName'])) {
02404 $table = $this->mapping[$fieldArray[0]]['mapTableName'];
02405 }
02406
02407 $field = $fieldArray[1];
02408 if (is_array($this->mapping[$fieldArray[0]]['mapFieldNames']) && isset($this->mapping[$fieldArray[0]]['mapFieldNames'][$fieldArray[1]])) {
02409 $field = $this->mapping[$fieldArray[0]]['mapFieldNames'][$fieldArray[1]];
02410 }
02411 $sqlPartArray[$k]['value'][0] = $table . '.' . $field;
02412 }
02413 }
02414
02415
02416 if ($sqlPartArray[$k]['table'] && $this->mapping[$sqlPartArray[$k]['table']]['mapTableName']) {
02417 $sqlPartArray[$k]['table'] = $this->mapping[$sqlPartArray[$k]['table']]['mapTableName'];
02418 }
02419 }
02420 }
02421 }
02422 }
02423
02424
02425
02426
02427
02428
02429
02430
02431
02432 function map_genericQueryParsed(&$parsedQuery) {
02433
02434
02435 $table = $parsedQuery['TABLE'];
02436 if ($table) {
02437
02438 if ($tableArray = $this->map_needMapping($table)) {
02439
02440
02441 if ($this->mapping[$table]['mapTableName']) {
02442 $parsedQuery['TABLE'] = $this->mapping[$table]['mapTableName'];
02443 }
02444
02445
02446 switch($parsedQuery['type']) {
02447 case 'ALTERTABLE':
02448
02449
02450 $newFieldName = $this->mapping[$table]['mapFieldNames'][$parsedQuery['FIELD']];
02451 if ($newFieldName) {
02452 if ($parsedQuery['FIELD'] == $parsedQuery['newField']) {
02453 $parsedQuery['FIELD'] = $parsedQuery['newField'] = $newFieldName;
02454 } else $parsedQuery['FIELD'] = $newFieldName;
02455 }
02456
02457
02458 if (is_array($parsedQuery['fields'])) {
02459 $this->map_fieldNamesInArray($table,$parsedQuery['fields']);
02460 }
02461 break;
02462 case 'CREATETABLE':
02463
02464 if (is_array($parsedQuery['FIELDS'])) {
02465 $newFieldsArray = array();
02466 foreach($parsedQuery['FIELDS'] as $fN => $fInfo) {
02467 if ($this->mapping[$table]['mapFieldNames'][$fN]) {
02468 $fN = $this->mapping[$table]['mapFieldNames'][$fN];
02469 }
02470 $newFieldsArray[$fN] = $fInfo;
02471 }
02472 $parsedQuery['FIELDS'] = $newFieldsArray;
02473 }
02474
02475
02476 if (is_array($parsedQuery['KEYS'])) {
02477 foreach($parsedQuery['KEYS'] as $kN => $kInfo) {
02478 $this->map_fieldNamesInArray($table,$parsedQuery['KEYS'][$kN]);
02479 }
02480 }
02481 break;
02482
02483
02484
02485 }
02486 }
02487 } else die('ERROR, mapping: No table found in parsed Query array...');
02488 }
02489
02490
02491
02492
02493
02494
02495
02496
02497 function map_fieldNamesInArray($table,&$fieldArray) {
02498 if (is_array($this->mapping[$table]['mapFieldNames'])) {
02499 foreach($fieldArray as $k => $v) {
02500 if ($this->mapping[$table]['mapFieldNames'][$v]) {
02501 $fieldArray[$k] = $this->mapping[$table]['mapFieldNames'][$v];
02502 }
02503 }
02504 }
02505 }
02506
02507
02508
02509
02510
02511
02512
02513
02514
02515
02516
02517
02518
02519
02520
02521
02522
02523
02524
02525
02526
02527
02528
02529
02530
02531
02532
02533
02534
02535
02536
02537
02538 function debugHandler($function,$execTime,$inData) {
02539
02540 $script = substr(PATH_thisScript,strlen(PATH_site));
02541
02542 if (substr($script,-strlen('dbal/mod1/index.php'))!='dbal/mod1/index.php' && !strstr($inData['args'][0], 'tx_dbal_debuglog')) {
02543 $data = array();
02544 $errorFlag = 0;
02545 $joinTable = '';
02546
02547 if ($this->sql_error()) {
02548 $data['sqlError'] = $this->sql_error();
02549 $errorFlag|=1;
02550 }
02551
02552
02553 if(empty($this->lastQuery))
02554 $query = implode(' ',$inData['args']);
02555 else
02556 $query = $this->lastQuery;
02557
02558 if($this->conf['debugOptions']['backtrace']) {
02559 $backtrace = debug_backtrace();
02560 unset($backtrace[0]);
02561 $data['backtrace'] = array_slice($backtrace, 0, $this->conf['debugOptions']['backtrace']);
02562 }
02563
02564 switch($function) {
02565 case 'exec_INSERTquery':
02566 case 'exec_UPDATEquery':
02567 case 'exec_DELETEquery':
02568 $this->debug_log($query,$execTime,$data,$joinTable,$errorFlag, $script);
02569 break;
02570
02571 case 'exec_SELECTquery':
02572
02573 if ($this->conf['debugOptions']['EXPLAIN'] && t3lib_div::inList('adodb,native',$inData['handlerType'])) {
02574 $data['EXPLAIN'] = $this->debug_explain($this->lastQuery);
02575 }
02576
02577
02578 if ($this->conf['debugOptions']['parseQuery']) {
02579 $parseResults = array();
02580 $parseResults['SELECT'] = $this->SQLparser->debug_parseSQLpart('SELECT',$inData['args'][1]);
02581 $parseResults['FROM'] = $this->SQLparser->debug_parseSQLpart('FROM',$inData['args'][0]);
02582 $parseResults['WHERE'] = $this->SQLparser->debug_parseSQLpart('WHERE',$inData['args'][2]);
02583 $parseResults['GROUPBY'] = $this->SQLparser->debug_parseSQLpart('SELECT',$inData['args'][3]);
02584 $parseResults['ORDERBY'] = $this->SQLparser->debug_parseSQLpart('SELECT',$inData['args'][4]);
02585
02586 foreach($parseResults as $k => $v) {
02587 if (!strlen($parseResults[$k])) unset($parseResults[$k]);
02588 }
02589 if (count($parseResults)) {
02590 $data['parseError'] = $parseResults;
02591 $errorFlag|=2;
02592 }
02593 }
02594
02595
02596 if ($this->conf['debugOptions']['joinTables']) {
02597 if (count(explode(',', $inData['ORIG_from_table']))>1) {
02598 $joinTable = $inData['args'][0];
02599 }
02600 }
02601
02602
02603 $this->debug_log($query,$execTime,$data,$joinTable,$errorFlag, $script);
02604 if(!empty($inData['args'][2]))
02605 $this->debug_WHERE($inData['args'][0], $inData['args'][2], $script);
02606 break;
02607 }
02608 }
02609 }
02610
02611
02612
02613
02614
02615
02616
02617
02618
02619 function debug_WHERE($table,$where, $script='') {
02620 $insertArray = array (
02621 'tstamp' => $GLOBALS['EXEC_TIME'],
02622 'beuser_id' => intval($GLOBALS['BE_USER']->user['uid']),
02623 'script' => $script,
02624 'tablename' => $table,
02625 'whereclause' => $where
02626 );
02627
02628 $this->exec_INSERTquery('tx_dbal_debuglog_where', $insertArray);
02629 }
02630
02631
02632
02633
02634
02635
02636
02637
02638
02639
02640
02641
02642 function debug_log($query,$ms,$data,$join,$errorFlag, $script='') {
02643 if(is_array($query)) {
02644 $queryToLog = $query[0].' -- ';
02645 if(count($query[1])) {
02646 $queryToLog .= count($query[1]).' BLOB FIELDS: '.implode(', ',array_keys($query[1]));
02647 }
02648 if(count($query[2])) {
02649 $queryToLog .= count($query[2]).' CLOB FIELDS: '.implode(', ',array_keys($query[2]));
02650 }
02651 } else {
02652 $queryToLog = $query;
02653 }
02654 $insertArray = array (
02655 'tstamp' => $GLOBALS['EXEC_TIME'],
02656 'beuser_id' => intval($GLOBALS['BE_USER']->user['uid']),
02657 'script' => $script,
02658 'exec_time' => $ms,
02659 'table_join' => $join,
02660 'serdata' => serialize($data),
02661 'query' => $queryToLog,
02662 'errorFlag' => $errorFlag
02663 );
02664
02665 $this->exec_INSERTquery('tx_dbal_debuglog', $insertArray);
02666 }
02667
02668
02669
02670
02671
02672
02673
02674
02675 function debug_explain($query) {
02676 $output = array();
02677 $hType = (string)$this->handlerCfg[$this->lastHandlerKey]['type'];
02678 switch($hType) {
02679 case 'native':
02680 $res = $this->sql_query('EXPLAIN '.$query);
02681 while($row = $this->sql_fetch_assoc($res)) {
02682 $output[] = $row;
02683 }
02684 break;
02685 case 'adodb':
02686 switch($this->handlerCfg['_DEFAULT']['config']['driver']) {
02687 case 'oci8':
02688 $res = $this->sql_query('EXPLAIN PLAN '.$query);
02689 $output[] = 'EXPLAIN PLAN data logged to default PLAN_TABLE';
02690 break;
02691 default:
02692 $res = $this->sql_query('EXPLAIN '.$query);
02693 while($row = $this->sql_fetch_assoc($res)) {
02694 $output[] = $row;
02695 }
02696 break;
02697 }
02698 break;
02699 }
02700
02701 return $output;
02702 }
02703 }
02704
02705
02706 if (defined('TYPO3_MODE') && $TYPO3_CONF_VARS[TYPO3_MODE]['XCLASS']['ext/dbal/class.ux_t3lib_db.php']) {
02707 include_once($TYPO3_CONF_VARS[TYPO3_MODE]['XCLASS']['ext/dbal/class.ux_t3lib_db.php']);
02708 }
02709
02710 ?>