TYPO3 API  SVNRelease
class.t3lib_sqlengine.php
Go to the documentation of this file.
00001 <?php
00002 /***************************************************************
00003  *  Copyright notice
00004  *
00005  *  (c) 2004-2011 Kasper Skårhøj (kasperYYYY@typo3.com)
00006  *  All rights reserved
00007  *
00008  *  This script is part of the TYPO3 project. The TYPO3 project is
00009  *  free software; you can redistribute it and/or modify
00010  *  it under the terms of the GNU General Public License as published by
00011  *  the Free Software Foundation; either version 2 of the License, or
00012  *  (at your option) any later version.
00013  *
00014  *  The GNU General Public License can be found at
00015  *  http://www.gnu.org/copyleft/gpl.html.
00016  *  A copy is found in the textfile GPL.txt and important notices to the license
00017  *  from the author is found in LICENSE.txt distributed with these scripts.
00018  *
00019  *
00020  *  This script is distributed in the hope that it will be useful,
00021  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
00022  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00023  *  GNU General Public License for more details.
00024  *
00025  *  This copyright notice MUST APPEAR in all copies of the script!
00026  ***************************************************************/
00027 /**
00028  * PHP SQL engine
00029  * EXPERIMENTAL!
00030  *
00031  * $Id: class.t3lib_sqlengine.php 10121 2011-01-18 20:15:30Z ohader $
00032  *
00033  * @author  Kasper Skårhøj <kasperYYYY@typo3.com>
00034  * @deprecated since TYPO3 4.3, this class will be removed in TYPO3 4.5, it has been integrated to extension DBAL.
00035  */
00036 /**
00037  * [CLASS/FUNCTION INDEX of SCRIPT]
00038  *
00039  *
00040  *
00041  *  104: class t3lib_sqlengine extends t3lib_sqlparser
00042  *  126:     function init($config, &$pObj)
00043  *  134:     function resetStatusVars()
00044  *  150:     function processAccordingToConfig(&$value,$fInfo)
00045  *
00046  *            SECTION: SQL queries
00047  *  205:     function exec_INSERTquery($table,$fields_values)
00048  *  273:     function exec_UPDATEquery($table,$where,$fields_values)
00049  *  332:     function exec_DELETEquery($table,$where)
00050  *  383:     function exec_SELECTquery($select_fields,$from_table,$where_clause,$groupBy,$orderBy,$limit)
00051  *  426:     function sql_query($query)
00052  *  437:     function sql_error()
00053  *  446:     function sql_insert_id()
00054  *  455:     function sql_affected_rows()
00055  *  465:     function quoteStr($str)
00056  *
00057  *            SECTION: SQL admin functions
00058  *  490:     function admin_get_tables()
00059  *  501:     function admin_get_fields($tableName)
00060  *  512:     function admin_get_keys($tableName)
00061  *  523:     function admin_query($query)
00062  *
00063  *            SECTION: Data Source I/O
00064  *  548:     function readDataSource($table)
00065  *  560:     function saveDataSource($table)
00066  *
00067  *            SECTION: SQL engine functions (PHP simulation of SQL) - still experimental
00068  *  590:     function selectFromData($table,$where)
00069  *  628:     function select_evalSingle($table,$config,&$itemKeys)
00070  *  747:     function getResultSet($keys, $table, $fieldList)
00071  *
00072  *            SECTION: Debugging
00073  *  790:     function debug_printResultSet($array)
00074  *
00075  *
00076  *  829: class t3lib_sqlengine_resultobj
00077  *  843:     function sql_num_rows()
00078  *  852:     function sql_fetch_assoc()
00079  *  863:     function sql_fetch_row()
00080  *  881:     function sql_data_seek($pointer)
00081  *  894:     function sql_field_type()
00082  *
00083  * TOTAL FUNCTIONS: 27
00084  * (This index is automatically created/updated by the extension "extdeveval")
00085  *
00086  */
00087 
00088 
00089 /**
00090  * PHP SQL engine / server
00091  * Basically this is trying to emulation SQL record selection by PHP, thus allowing SQL queries into alternative data storages managed by PHP.
00092  * EXPERIMENTAL!
00093  *
00094  * @author  Kasper Skårhøj <kasperYYYY@typo3.com>
00095  * @package TYPO3
00096  * @subpackage t3lib
00097  */
00098 class t3lib_sqlengine extends t3lib_sqlparser {
00099 
00100         // array with data records: [table name][num.index] = records
00101     var $data = array(); // Data source storage
00102 
00103 
00104         // Internal, SQL Status vars:
00105     var $errorStatus = ''; // Set with error message of last operation
00106     var $lastInsertedId = 0; // Set with last inserted unique ID
00107     var $lastAffectedRows = 0; // Set with last number of affected rows.
00108 
00109 
00110     /**
00111      * Constrcutor
00112      */
00113     function __construct() {
00114         t3lib_div::deprecationLog("Class t3lib_sqlengine is deprecated since TYPO3 4.3, will be removed in TYPO3 4.6, it has been integrated to extension DBAL.");
00115     }
00116 
00117 
00118     /**
00119      * Dummy function for initializing SQL handler. Create you own in derived classes.
00120      *
00121      * @param   array       Configuration array from handler
00122      * @param   object      Parent object
00123      * @return  void
00124      */
00125     function init($config, $pObj) {
00126     }
00127 
00128     /**
00129      * Reset SQL engine status variables (insert id, affected rows, error status)
00130      *
00131      * @return  void
00132      */
00133     function resetStatusVars() {
00134         $this->errorStatus = '';
00135         $this->lastInsertedId = 0;
00136         $this->lastAffectedRows = 0;
00137     }
00138 
00139     /**
00140      * Processing of update/insert values based on field type.
00141      *
00142      * The input value is typecast and trimmed/shortened according to the field
00143      * type and the configuration options from the $fInfo parameter.
00144      *
00145      * @param   mixed       $value The input value to process
00146      * @param   array       $fInfo Field configuration data
00147      * @return  mixed       The processed input value
00148      */
00149     function processAccordingToConfig(&$value, $fInfo) {
00150         $options = $this->parseFieldDef($fInfo['Type']);
00151 
00152         switch (strtolower($options['fieldType'])) {
00153             case 'int':
00154             case 'smallint':
00155             case 'tinyint':
00156             case 'mediumint':
00157                 $value = intval($value);
00158                 if ($options['featureIndex']['UNSIGNED']) {
00159                     $value = t3lib_div::intInRange($value, 0);
00160                 }
00161             break;
00162             case 'double':
00163                 $value = (double) $value;
00164             break;
00165             case 'varchar':
00166             case 'char':
00167                 $value = substr($value, 0, trim($options['value']));
00168             break;
00169             case 'text':
00170             case 'blob':
00171                 $value = substr($value, 0, 65536);
00172             break;
00173             case 'tinytext':
00174             case 'tinyblob':
00175                 $value = substr($value, 0, 256);
00176             break;
00177             case 'mediumtext':
00178             case 'mediumblob':
00179                     // ??
00180             break;
00181         }
00182     }
00183 
00184 
00185     /********************************
00186      *
00187      * SQL queries
00188      * This is the SQL access functions used when this class is instantiated as a SQL handler with DBAL. Override these in derived classes.
00189      *
00190      ********************************/
00191 
00192     /**
00193      * Execute an INSERT query
00194      *
00195      * @param   string      Table name
00196      * @param   array       Field values as key=>value pairs.
00197      * @return  boolean     TRUE on success and FALSE on failure (error is set internally)
00198      */
00199     function exec_INSERTquery($table, $fields_values) {
00200 
00201             // Initialize
00202         $this->resetStatusVars();
00203 
00204             // Reading Data Source if not done already.
00205         $this->readDataSource($table);
00206 
00207             // If data source is set:
00208         if (is_array($this->data[$table])) {
00209 
00210             $fieldInformation = $this->admin_get_fields($table); // Should cache this...!
00211 
00212                 // Looking for unique keys:
00213             $saveArray = array();
00214             foreach ($fieldInformation as $fInfo) {
00215 
00216                     // Field name:
00217                 $fN = $fInfo['Field'];
00218 
00219                     // Set value:
00220                     // FIXME $options not defined
00221                 $saveArray[$fN] = isset($fields_values[$fN]) ? $fields_values[$fN] : $options['Default'];
00222 
00223                     // Process value:
00224                 $this->processAccordingToConfig($saveArray[$fN], $fInfo);
00225 
00226                     // If an auto increment field is found, find the largest current uid:
00227                 if ($fInfo['Extra'] == 'auto_increment') {
00228 
00229                         // Get all UIDs:
00230                     $uidArray = array();
00231                     foreach ($this->data[$table] as $r) {
00232                         $uidArray[] = $r[$fN];
00233                     }
00234 
00235                         // If current value is blank or already in array, we create a new:
00236                     if (!$saveArray[$fN] || in_array(intval($saveArray[$fN]), $uidArray)) {
00237                         if (count($uidArray)) {
00238                             $saveArray[$fN] = max($uidArray) + 1;
00239                         } else {
00240                             $saveArray[$fN] = 1;
00241                         }
00242                     }
00243 
00244                         // Update "last inserted id":
00245                     $this->lastInsertedId = $saveArray[$fN];
00246                 }
00247             }
00248 
00249                 // Insert row in table:
00250             $this->data[$table][] = $saveArray;
00251 
00252                 // Save data source
00253             $this->saveDataSource($table);
00254 
00255             return TRUE;
00256         } else {
00257             $this->errorStatus = 'No data loaded.';
00258         }
00259 
00260         return FALSE;
00261     }
00262 
00263     /**
00264      * Execute UPDATE query on table
00265      *
00266      * @param   string      Table name
00267      * @param   string      WHERE clause
00268      * @param   array       Field values as key=>value pairs.
00269      * @return  boolean     TRUE on success and FALSE on failure (error is set internally)
00270      */
00271     function exec_UPDATEquery($table, $where, $fields_values) {
00272 
00273             // Initialize:
00274         $this->resetStatusVars();
00275 
00276             // Reading Data Source if not done already.
00277         $this->readDataSource($table);
00278 
00279             // If anything is there:
00280         if (is_array($this->data[$table])) {
00281 
00282                 // Parse WHERE clause:
00283             $where = $this->parseWhereClause($where);
00284 
00285             if (is_array($where)) {
00286 
00287                     // Field information
00288                 $fieldInformation = $this->admin_get_fields($table); // Should cache this...!
00289 
00290                     // Traverse fields to update:
00291                 foreach ($fields_values as $fName => $fValue) {
00292                     $this->processAccordingToConfig($fields_values[$fName], $fieldInformation[$fName]);
00293                 }
00294 
00295                     // Do query, returns array with keys to the data array of the result:
00296                 $itemKeys = $this->selectFromData($table, $where);
00297 
00298                     // Set "last affected rows":
00299                 $this->lastAffectedRows = count($itemKeys);
00300 
00301                     // Update rows:
00302                 if ($this->lastAffectedRows) {
00303                         // Traverse result set here:
00304                     foreach ($itemKeys as $dataArrayKey) {
00305 
00306                             // Traverse fields to update:
00307                         foreach ($fields_values as $fName => $fValue) {
00308                             $this->data[$table][$dataArrayKey][$fName] = $fValue;
00309                         }
00310                     }
00311 
00312                         // Save data source
00313                     $this->saveDataSource($table);
00314                 }
00315 
00316                 return TRUE;
00317             } else {
00318                 $this->errorStatus = 'WHERE clause contained errors: ' . $where;
00319             }
00320         } else {
00321             $this->errorStatus = 'No data loaded.';
00322         }
00323 
00324         return FALSE;
00325     }
00326 
00327     /**
00328      * Execute DELETE query
00329      *
00330      * @param   string      Table to delete from
00331      * @param   string      WHERE clause
00332      * @return  boolean     TRUE on success and FALSE on failure (error is set internally)
00333      */
00334     function exec_DELETEquery($table, $where) {
00335 
00336             // Initialize:
00337         $this->resetStatusVars();
00338 
00339             // Reading Data Source if not done already.
00340         $this->readDataSource($table);
00341 
00342             // If anything is there:
00343         if (is_array($this->data[$table])) {
00344 
00345                 // Parse WHERE clause:
00346             $where = $this->parseWhereClause($where);
00347 
00348             if (is_array($where)) {
00349 
00350                     // Do query, returns array with keys to the data array of the result:
00351                 $itemKeys = $this->selectFromData($table, $where);
00352 
00353                     // Set "last affected rows":
00354                 $this->lastAffectedRows = count($itemKeys);
00355 
00356                     // Remove rows:
00357                 if ($this->lastAffectedRows) {
00358                         // Traverse result set:
00359                     foreach ($itemKeys as $dataArrayKey) {
00360                         unset($this->data[$table][$dataArrayKey]);
00361                     }
00362 
00363                         // Saving data source
00364                     $this->saveDataSource($table);
00365                 }
00366 
00367                 return TRUE;
00368             } else {
00369                 $this->errorStatus = 'WHERE clause contained errors: ' . $where;
00370             }
00371         } else {
00372             $this->errorStatus = 'No data loaded.';
00373         }
00374 
00375         return FALSE;
00376     }
00377 
00378     /**
00379      * Execute SELECT query
00380      *
00381      * @param   string      List of fields to select from the table. This is what comes right after "SELECT ...". Required value.
00382      * @param   string      Table(s) from which to select. This is what comes right after "FROM ...". Required value.
00383      * @param   string      Optional additional WHERE clauses put in the end of the query. NOTICE: You must escape values in this argument with $this->fullQuoteStr() yourself! DO NOT PUT IN GROUP BY, ORDER BY or LIMIT!
00384      * @param   string      Optional GROUP BY field(s), if none, supply blank string.
00385      * @param   string      Optional ORDER BY field(s), if none, supply blank string.
00386      * @param   string      Optional LIMIT value ([begin,]max), if none, supply blank string.
00387      * @return  object      Returns result object, but if errors, returns false
00388      */
00389     function exec_SELECTquery($select_fields, $from_table, $where_clause, $groupBy, $orderBy, $limit) {
00390 
00391             // Initialize:
00392         $this->resetStatusVars();
00393 
00394             // Create result object
00395         $sqlObj = t3lib_div::makeInstance('t3lib_sqlengine_resultobj');
00396         $sqlObj->result = array(); // Empty result as a beginning
00397 
00398             // Get table list:
00399         $tableArray = $this->parseFromTables($from_table);
00400         $table = $tableArray[0]['table'];
00401 
00402             // Reading Data Source if not done already.
00403         $this->readDataSource($table);
00404 
00405             // If anything is there:
00406         if (is_array($this->data[$table])) {
00407 
00408                 // Parse WHERE clause:
00409             $where = $this->parseWhereClause($where_clause);
00410             if (is_array($where)) {
00411 
00412                     // Do query, returns array with keys to the data array of the result:
00413                 $itemKeys = $this->selectFromData($table, $where);
00414 
00415                     // Finally, read the result rows into this variable:
00416                 $sqlObj->result = $this->getResultSet($itemKeys, $table, '*');
00417                     // Reset and return result:
00418                 reset($sqlObj->result);
00419                 return $sqlObj;
00420             } else {
00421                 $this->errorStatus = 'WHERE clause contained errors: ' . $where;
00422             }
00423         } else {
00424             $this->errorStatus = 'No data loaded: ' . $this->errorStatus;
00425         }
00426 
00427         return FALSE;
00428     }
00429 
00430     /**
00431      * Performs an SQL query on the "database"
00432      *
00433      * @param   string      Query to execute
00434      * @return  object      Result object or false if error
00435      */
00436     function sql_query($query) {
00437         $res = t3lib_div::makeInstance('t3lib_sqlengine_resultobj');
00438         $res->result = array();
00439         return $res;
00440     }
00441 
00442     /**
00443      * Returns most recent error
00444      *
00445      * @return  string      Error message, if any
00446      */
00447     function sql_error() {
00448         return $this->errorStatus;
00449     }
00450 
00451     /**
00452      * Returns most recently create unique ID (of INSERT queries)
00453      *
00454      * @return  integer     Last unique id created.
00455      */
00456     function sql_insert_id() {
00457         return $this->lastInsertedId;
00458     }
00459 
00460     /**
00461      * Returns affected rows (of UPDATE and DELETE queries)
00462      *
00463      * @return  integer     Last amount of affected rows.
00464      */
00465     function sql_affected_rows() {
00466         return $this->lastAffectedRows;
00467     }
00468 
00469     /**
00470      * Quoting strings for insertion in SQL queries
00471      *
00472      * @param   string      Input String
00473      * @return  string      String, with quotes escaped
00474      */
00475     function quoteStr($str) {
00476         return addslashes($str);
00477     }
00478 
00479 
00480     /**************************************
00481      *
00482      * SQL admin functions
00483      * (For use in the Install Tool and Extension Manager)
00484      *
00485      **************************************/
00486 
00487     /**
00488      * (DUMMY) Returns the list of tables from the database
00489      *
00490      * @return  array       Tables in an array (tablename is in both key and value)
00491      * @todo    Should return table details in value! see t3lib_db::admin_get_tables()
00492      */
00493     function admin_get_tables() {
00494         $whichTables = array();
00495         return $whichTables;
00496     }
00497 
00498     /**
00499      * (DUMMY) Returns information about each field in the $table
00500      *
00501      * @param   string      Table name
00502      * @return  array       Field information in an associative array with fieldname => field row
00503      */
00504     function admin_get_fields($tableName) {
00505         $output = array();
00506         return $output;
00507     }
00508 
00509     /**
00510      * (DUMMY) Returns information about each index key in the $table
00511      *
00512      * @param   string      Table name
00513      * @return  array       Key information in a numeric array
00514      */
00515     function admin_get_keys($tableName) {
00516         $output = array();
00517         return $output;
00518     }
00519 
00520     /**
00521      * (DUMMY) mysql() wrapper function, used by the Install Tool and EM for all queries regarding management of the database!
00522      *
00523      * @param   string      Query to execute
00524      * @return  pointer     Result pointer
00525      */
00526     function admin_query($query) {
00527         return $this->sql_query($query);
00528     }
00529 
00530 
00531     /********************************
00532      *
00533      * Data Source I/O
00534      *
00535      ********************************/
00536 
00537     /**
00538      * Dummy function for setting table data. Create your own.
00539      * NOTICE: Handler to "table-locking" needs to be made probably!
00540      *
00541      * @param   string      Table name
00542      * @return  void
00543      * @todo    Table locking tools?
00544      */
00545     function readDataSource($table) {
00546         $this->data[$table] = array();
00547     }
00548 
00549     /**
00550      * Dummy function for setting table data. Create your own.
00551      * NOTICE: Handler to "table-locking" needs to be made probably!
00552      *
00553      * @param   string      Table name
00554      * @return  void
00555      * @todo    Table locking tools?
00556      */
00557     function saveDataSource($table) {
00558         debug($this->data[$table]);
00559     }
00560 
00561 
00562     /********************************
00563      *
00564      * SQL engine functions (PHP simulation of SQL) - still experimental
00565      *
00566      ********************************/
00567 
00568     /**
00569      * PHP simulation of SQL "SELECT"
00570      * Yet EXPERIMENTAL!
00571      *
00572      * @param   string      Table name
00573      * @param   array       Where clause parsed into array
00574      * @return  array       Array of keys pointing to result rows in $this->data[$table]
00575      */
00576     function selectFromData($table, $where) {
00577 
00578         $output = array();
00579         if (is_array($this->data[$table])) {
00580 
00581                 // All keys:
00582             $OR_index = 0;
00583             $itemKeys = array();
00584 
00585             foreach ($where as $config) {
00586 
00587                 if (strtoupper($config['operator']) == 'OR') {
00588                     $OR_index++;
00589                 }
00590 
00591                 if (!isset($itemKeys[$OR_index])) {
00592                     $itemKeys[$OR_index] = array_keys($this->data[$table]);
00593                 }
00594 
00595                 $this->select_evalSingle($table, $config, $itemKeys[$OR_index]);
00596             }
00597 
00598             foreach ($itemKeys as $uidKeys) {
00599                 $output = array_merge($output, $uidKeys);
00600             }
00601             $output = array_unique($output);
00602         }
00603 
00604         return $output;
00605     }
00606 
00607     /**
00608      * Evalutaion of a WHERE-clause-array.
00609      * Yet EXPERIMENTAL
00610      *
00611      * @param   string      Tablename
00612      * @param   array       WHERE-configuration array
00613      * @param   array       Data array to work on.
00614      * @return  void        Data array passed by reference
00615      * @see selectFromData()
00616      */
00617     function select_evalSingle($table, $config, &$itemKeys) {
00618         $neg = preg_match('/^AND[[:space:]]+NOT$/', trim($config['operator']));
00619 
00620         if (is_array($config['sub'])) {
00621             $subSelKeys = $this->selectFromData($table, $config['sub']);
00622             if ($neg) {
00623                 foreach ($itemKeys as $kk => $vv) {
00624                     if (in_array($vv, $subSelKeys)) {
00625                         unset($itemKeys[$kk]);
00626                     }
00627                 }
00628             } else {
00629                 $itemKeys = array_intersect($itemKeys, $subSelKeys);
00630             }
00631         } else {
00632             $comp = strtoupper(str_replace(array(' ', TAB, CR, LF), '', $config['comparator']));
00633             $mod = strtoupper($config['modifier']);
00634             switch ($comp) {
00635                 case 'NOTLIKE':
00636                 case 'LIKE':
00637                     $like_value = strtolower($config['value'][0]);
00638                     if (substr($like_value, 0, 1) == '%') {
00639                         $wildCard_begin = TRUE;
00640                         $like_value = substr($like_value, 1);
00641                     }
00642                     if (substr($like_value, -1) == '%') {
00643                         $wildCard_end = TRUE;
00644                         $like_value = substr($like_value, 0, -1);
00645                     }
00646                 break;
00647                 case 'NOTIN':
00648                 case 'IN':
00649                     $in_valueArray = array();
00650                     foreach ($config['value'] as $vParts) {
00651                         $in_valueArray[] = (string) $vParts[0];
00652                     }
00653                 break;
00654             }
00655 
00656             foreach ($itemKeys as $kk => $v) {
00657                 $field_value = $this->data[$table][$v][$config['field']];
00658 
00659                     // Calculate it:
00660                 if ($config['calc'] == '&') {
00661                     $field_value &= intval($config['calc_value']);
00662                 }
00663 
00664                     // Compare it:
00665                 switch ($comp) {
00666                     case '<=':
00667                         $bool = $field_value <= $config['value'][0];
00668                     break;
00669                     case '>=':
00670                         $bool = $field_value >= $config['value'][0];
00671                     break;
00672                     case '<':
00673                         $bool = $field_value < $config['value'][0];
00674                     break;
00675                     case '>':
00676                         $bool = $field_value > $config['value'][0];
00677                     break;
00678                     case '=':
00679                         $bool = !strcmp($field_value, $config['value'][0]);
00680                     break;
00681                     case '!=':
00682                         $bool = strcmp($field_value, $config['value'][0]);
00683                     break;
00684                     case 'NOTIN':
00685                     case 'IN':
00686                         $bool = in_array((string) $field_value, $in_valueArray);
00687                         if ($comp == 'NOTIN') {
00688                             $bool = !$bool;
00689                         }
00690                     break;
00691                     case 'NOTLIKE':
00692                     case 'LIKE':
00693                         if (!strlen($like_value)) {
00694                             $bool = TRUE;
00695                         } elseif ($wildCard_begin && !$wildCard_end) {
00696                             $bool = !strcmp(substr(strtolower($field_value), -strlen($like_value)), $like_value);
00697                         } elseif (!$wildCard_begin && $wildCard_end) {
00698                             $bool = !strcmp(substr(strtolower($field_value), 0, strlen($like_value)), $like_value);
00699                         } elseif ($wildCard_begin && $wildCard_end) {
00700                             $bool = strstr($field_value, $like_value);
00701                         } else {
00702                             $bool = !strcmp(strtolower($field_value), $like_value);
00703                         }
00704                         if ($comp == 'NOTLIKE') {
00705                             $bool = !$bool;
00706                         }
00707                     break;
00708                     default:
00709                         $bool = $field_value ? TRUE : FALSE;
00710                     break;
00711                 }
00712 
00713                     // General negation:
00714                 if ($neg) {
00715                     $bool = !$bool;
00716                 }
00717 
00718                     // Modify?
00719                 switch ($mod) {
00720                     case 'NOT':
00721                     case '!':
00722                         $bool = !$bool;
00723                     break;
00724                 }
00725 
00726                     // Action:
00727                 if (!$bool) {
00728                     unset($itemKeys[$kk]);
00729                 }
00730             }
00731         }
00732     }
00733 
00734     /**
00735      * Returning result set based on result keys, table and field list
00736      *
00737      * @param   array       Result keys
00738      * @param   string      Tablename
00739      * @param   string      Fieldlist (commaseparated)
00740      * @return  array       Result array with "rows"
00741      */
00742     function getResultSet($keys, $table, $fieldList) {
00743         $fields = t3lib_div::trimExplode(',', $fieldList);
00744 
00745         $output = array();
00746         foreach ($keys as $kValue) {
00747             if ($fieldList == '*') {
00748                 $output[$kValue] = $this->data[$table][$kValue];
00749             } else {
00750                 foreach ($fields as $fieldName) {
00751                     $output[$kValue][$fieldName] = $this->data[$table][$kValue][$fieldName];
00752                 }
00753             }
00754         }
00755 
00756         return $output;
00757     }
00758 
00759 
00760     /*************************
00761      *
00762      * Debugging
00763      *
00764      *************************/
00765 
00766     /**
00767      * Returns the result set (in array) as HTML table. For debugging.
00768      *
00769      * @param   array       Result set array (array of rows)
00770      * @return  string      HTML table
00771      */
00772     function debug_printResultSet($array) {
00773 
00774         if (count($array)) {
00775             $tRows = array();
00776             $fields = array_keys(current($array));
00777             $tCell[] = '
00778                             <td>IDX</td>';
00779             foreach ($fields as $fieldName) {
00780                 $tCell[] = '
00781                             <td>' . htmlspecialchars($fieldName) . '</td>';
00782             }
00783             $tRows[] = '<tr>' . implode('', $tCell) . '</tr>';
00784 
00785 
00786             foreach ($array as $index => $rec) {
00787 
00788                 $tCell = array();
00789                 $tCell[] = '
00790                         <td>' . htmlspecialchars($index) . '</td>';
00791                 foreach ($fields as $fieldName) {
00792                     $tCell[] = '
00793                             <td>' . htmlspecialchars($rec[$fieldName]) . '</td>';
00794                 }
00795                 $tRows[] = '<tr>' . implode('', $tCell) . '</tr>';
00796             }
00797 
00798             return '<table border="1">' . implode('', $tRows) . '</table>';
00799         } else {
00800             'Empty resultset';
00801         }
00802     }
00803 }
00804 
00805 
00806 /**
00807  * PHP SQL engine, result object
00808  *
00809  * @author  Kasper Skårhøj <kasperYYYY@typo3.com>
00810  * @package TYPO3
00811  * @subpackage t3lib
00812  */
00813 class t3lib_sqlengine_resultobj {
00814 
00815         // Result array, must contain the fields in the order they were selected in the SQL statement (for sql_fetch_row())
00816     var $result = array();
00817 
00818     var $TYPO3_DBAL_handlerType = '';
00819     var $TYPO3_DBAL_tableList = '';
00820 
00821     /**
00822      * Constrcutor
00823      */
00824     function __construct() {
00825         t3lib_div::deprecationLog("Class t3lib_sqlengine_resultobj is deprecated since TYPO3 4.3, will be removed in TYPO3 4.6, it has been integrated to extension DBAL.");
00826     }
00827 
00828     /**
00829      * Counting number of rows
00830      *
00831      * @return  integer
00832      */
00833     function sql_num_rows() {
00834         return count($this->result);
00835     }
00836 
00837     /**
00838      * Fetching next row in result array
00839      *
00840      * @return  array       Associative array
00841      */
00842     function sql_fetch_assoc() {
00843         $row = current($this->result);
00844         next($this->result);
00845         return $row;
00846     }
00847 
00848     /**
00849      * Fetching next row, numerical indices
00850      *
00851      * @return  array       Numerical array
00852      */
00853     function sql_fetch_row() {
00854         $resultRow = $this->sql_fetch_assoc();
00855 
00856         if (is_array($resultRow)) {
00857             $numArray = array();
00858             foreach ($resultRow as $value) {
00859                 $numArray[] = $value;
00860             }
00861             return $numArray;
00862         }
00863     }
00864 
00865     /**
00866      * Seeking position in result
00867      *
00868      * @param   integer     Position pointer.
00869      * @return  boolean     Returns true on success
00870      */
00871     function sql_data_seek($pointer) {
00872         reset($this->result);
00873         for ($a = 0; $a < $pointer; $a++) {
00874             next($this->result);
00875         }
00876         return TRUE;
00877     }
00878 
00879     /**
00880      * Returning SQL field type
00881      *
00882      * @return  string      Blank string, not supported (it seems)
00883      */
00884     function sql_field_type() {
00885         return '';
00886     }
00887 }
00888 
00889 
00890 if (defined('TYPO3_MODE') && isset($GLOBALS['TYPO3_CONF_VARS'][TYPO3_MODE]['XCLASS']['t3lib/class.t3lib_sqlengine.php'])) {
00891     include_once($GLOBALS['TYPO3_CONF_VARS'][TYPO3_MODE]['XCLASS']['t3lib/class.t3lib_sqlengine.php']);
00892 }
00893 
00894 ?>