Main Page | Directories | Namespace List | Class Hierarchy | Alphabetical List | Class List | File List | Class Members | File Members | Related Pages | Examples

t3lib_sqlengine Class Reference

Inherits t3lib_sqlparser.

List of all members.

Public Member Functions

 init ($config, &$pObj)
 Dummy function for initializing SQL handler.
 resetStatusVars ()
 Reset SQL engine status variables (insert id, affected rows, error status).
 processAccordingToConfig (&$value, $fInfo)
 Processing of update/insert values based on field type.
 exec_INSERTquery ($table, $fields_values)
 Execute an INSERT query.
 exec_UPDATEquery ($table, $where, $fields_values)
 Execute UPDATE query on table.
 exec_DELETEquery ($table, $where)
 Execute DELETE query.
 exec_SELECTquery ($select_fields, $from_table, $where_clause, $groupBy, $orderBy, $limit)
 Execute SELECT query.
 sql_query ($query)
 Performs an SQL query on the "database".
 sql_error ()
 Returns most recent error.
 sql_insert_id ()
 Returns most recently create unique ID (of INSERT queries).
 sql_affected_rows ()
 Returns affected rows (of UPDATE and DELETE queries).
 quoteStr ($str)
 Quoting strings for insertion in SQL queries.
 admin_get_tables ()
 (DUMMY) Returns the list of tables from the database
 admin_get_fields ($tableName)
 (DUMMY) Returns information about each field in the $table
 admin_get_keys ($tableName)
 (DUMMY) Returns information about each index key in the $table
 admin_query ($query)
 (DUMMY) mysql() wrapper function, used by the Install Tool and EM for all queries regarding management of the database!
 readDataSource ($table)
 Dummy function for setting table data.
 saveDataSource ($table)
 Dummy function for setting table data.
 selectFromData ($table, $where)
 [Describe function...]
 select_evalSingle ($table, $config, &$itemKeys)
 [Describe function...]
 getResultSet ($keys, $table, $fieldList)
 [Describe function...]
 debug_printResultSet ($array)
 Returns the result set (in array) as HTML table.

Public Attributes

 $data = array()
 $errorStatus = ''
 $lastInsertedId = 0
 $lastAffectedRows = 0


Member Function Documentation

t3lib_sqlengine::admin_get_fields tableName  ) 
 

(DUMMY) Returns information about each field in the $table

Parameters:
string Table name
Returns:
array Field information in an associative array with fieldname => field row

Definition at line 495 of file class.t3lib_sqlengine.php.

Referenced by exec_INSERTquery(), and exec_UPDATEquery().

00495                                           {
00496       $output = array();
00497       return $output;
00498    }

t3lib_sqlengine::admin_get_keys tableName  ) 
 

(DUMMY) Returns information about each index key in the $table

Parameters:
string Table name
Returns:
array Key information in a numeric array

Definition at line 506 of file class.t3lib_sqlengine.php.

00506                                        {
00507       $output = array();
00508       return $output;
00509    }

t3lib_sqlengine::admin_get_tables  ) 
 

(DUMMY) Returns the list of tables from the database

Returns:
array Tables in an array (tablename is in both key and value)

Definition at line 484 of file class.t3lib_sqlengine.php.

00484                                  {
00485       $whichTables = array();
00486       return $whichTables;
00487    }

t3lib_sqlengine::admin_query query  ) 
 

(DUMMY) mysql() wrapper function, used by the Install Tool and EM for all queries regarding management of the database!

Parameters:
string Query to execute
Returns:
pointer Result pointer

Definition at line 517 of file class.t3lib_sqlengine.php.

References sql_query().

00517                                  {
00518       return $this->sql_query($query);
00519    }

t3lib_sqlengine::debug_printResultSet array  ) 
 

Returns the result set (in array) as HTML table.

For debugging.

Parameters:
array Result set array (array of rows)
Returns:
string HTML table

Definition at line 785 of file class.t3lib_sqlengine.php.

References $tRows, and table().

00785                                           {
00786 
00787       if (count($array))   {
00788          $tRows=array();
00789          $fields = array_keys(current($array));
00790                $tCell[]='
00791                      <td>IDX</td>';
00792             foreach($fields as $fieldName)   {
00793                $tCell[]='
00794                      <td>'.htmlspecialchars($fieldName).'</td>';
00795             }
00796             $tRows[]='<tr>'.implode('',$tCell).'</tr>';
00797 
00798 
00799          foreach($array as $index => $rec)   {
00800 
00801             $tCell=array();
00802             $tCell[]='
00803                   <td>'.htmlspecialchars($index).'</td>';
00804             foreach($fields as $fieldName)   {
00805                $tCell[]='
00806                      <td>'.htmlspecialchars($rec[$fieldName]).'</td>';
00807             }
00808             $tRows[]='<tr>'.implode('',$tCell).'</tr>';
00809          }
00810 
00811          return '<table border="1">'.implode('',$tRows).'</table>';
00812       } else 'Empty resultset';
00813    }

t3lib_sqlengine::exec_DELETEquery table,
where
 

Execute DELETE query.

Parameters:
string Table to delete from
string WHERE clause
Returns:
boolean TRUE on success and FALSE on failure (error is set internally)

Definition at line 326 of file class.t3lib_sqlengine.php.

References t3lib_sqlparser::parseWhereClause(), readDataSource(), resetStatusVars(), saveDataSource(), selectFromData(), and table().

00326                                              {
00327 
00328          // Initialize:
00329       $this->resetStatusVars();
00330 
00331          // Reading Data Source if not done already.
00332       $this->readDataSource($table);
00333 
00334          // If anything is there:
00335       if (is_array($this->data[$table]))  {
00336 
00337             // Parse WHERE clause:
00338          $where = $this->parseWhereClause($where);
00339 
00340          if (is_array($where))   {
00341 
00342                // Do query, returns array with keys to the data array of the result:
00343             $itemKeys = $this->selectFromData($table,$where);
00344 
00345                // Set "last affected rows":
00346             $this->lastAffectedRows = count($itemKeys);
00347 
00348                // Remove rows:
00349             if ($this->lastAffectedRows)  {
00350                   // Traverse result set:
00351                foreach($itemKeys as $dataArrayKey) {
00352                   unset($this->data[$table][$dataArrayKey]);
00353                }
00354 
00355                   // Saving data source
00356                $this->saveDataSource($table);
00357             }
00358 
00359             return TRUE;
00360          } else $this->errorStatus = 'WHERE clause contained errors: '.$where;
00361       } else $this->errorStatus = 'No data loaded.';
00362 
00363       return FALSE;
00364    }

t3lib_sqlengine::exec_INSERTquery table,
fields_values
 

Execute an INSERT query.

Parameters:
string Table name
array Field values as key=>value pairs.
Returns:
boolean TRUE on success and FALSE on failure (error is set internally)

Definition at line 200 of file class.t3lib_sqlengine.php.

References admin_get_fields(), processAccordingToConfig(), readDataSource(), resetStatusVars(), saveDataSource(), and table().

00200                                                       {
00201 
00202          // Initialize
00203       $this->resetStatusVars();
00204 
00205          // Reading Data Source if not done already.
00206       $this->readDataSource($table);
00207 
00208          // If data source is set:
00209       if (is_array($this->data[$table]))  {
00210 
00211          $fieldInformation = $this->admin_get_fields($table);     // Should cache this...!
00212 
00213             // Looking for unique keys:
00214          $saveArray = array();
00215          foreach($fieldInformation as $fInfo)   {
00216 
00217                // Field name:
00218             $fN = $fInfo['Field'];
00219 
00220                // Set value:
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 $saveArray[$fN] = 1;
00240                }
00241 
00242                   // Update "last inserted id":
00243                $this->lastInsertedId = $saveArray[$fN];
00244             }
00245          }
00246 #debug(array($fields_values,$saveArray));
00247             // Insert row in table:
00248          $this->data[$table][] = $saveArray;
00249 
00250             // Save data source
00251          $this->saveDataSource($table);
00252 
00253          return TRUE;
00254       } else $this->errorStatus = 'No data loaded.';
00255 
00256       return FALSE;
00257    }

t3lib_sqlengine::exec_SELECTquery select_fields,
from_table,
where_clause,
groupBy,
orderBy,
limit
 

Execute SELECT query.

Parameters:
string List of fields to select from the table. This is what comes right after "SELECT ...". Required value.
string Table(s) from which to select. This is what comes right after "FROM ...". Required value.
string Optional additional WHERE clauses put in the end of the query. NOTICE: You must escape values in this argument with $this->quoteStr() yourself! DO NOT PUT IN GROUP BY, ORDER BY or LIMIT!
string Optional GROUP BY field(s), if none, supply blank string.
string Optional ORDER BY field(s), if none, supply blank string.
string Optional LIMIT value ([begin,]max), if none, supply blank string.
Returns:
object Returns result object, but if errors, returns false

Definition at line 377 of file class.t3lib_sqlengine.php.

References getResultSet(), t3lib_sqlparser::parseFromTables(), t3lib_sqlparser::parseWhereClause(), readDataSource(), resetStatusVars(), selectFromData(), and table().

00377                                                                                                 {
00378 
00379          // Initialize:
00380       $this->resetStatusVars();
00381 
00382          // Create result object
00383       $sqlObj = t3lib_div::makeInstance('t3lib_sqlengine_resultobj');
00384       $sqlObj->result = array(); // Empty result as a beginning
00385 
00386          // Get table list:
00387       $tableArray = $this->parseFromTables($from_table);
00388       $table = $tableArray[0]['table'];
00389 
00390          // Reading Data Source if not done already.
00391       $this->readDataSource($table);
00392 
00393          // If anything is there:
00394       if (is_array($this->data[$table]))  {
00395 
00396             // Parse WHERE clause:
00397          $where = $this->parseWhereClause($where_clause);
00398          if (is_array($where))   {
00399 
00400                // Do query, returns array with keys to the data array of the result:
00401             $itemKeys = $this->selectFromData($table,$where);
00402 
00403                // Finally, read the result rows into this variable:
00404             $sqlObj->result = $this->getResultSet($itemKeys,$table,'*');
00405                // Reset and return result:
00406             reset($sqlObj->result);
00407             return $sqlObj;
00408          } else $this->errorStatus = 'WHERE clause contained errors: '.$where;
00409       }  else $this->errorStatus = 'No data loaded: '.$this->errorStatus;
00410 
00411       return FALSE;
00412    }

t3lib_sqlengine::exec_UPDATEquery table,
where,
fields_values
 

Execute UPDATE query on table.

Parameters:
string Table name
string WHERE clause
array Field values as key=>value pairs.
Returns:
boolean TRUE on success and FALSE on failure (error is set internally)

Definition at line 267 of file class.t3lib_sqlengine.php.

References admin_get_fields(), t3lib_sqlparser::parseWhereClause(), processAccordingToConfig(), readDataSource(), resetStatusVars(), saveDataSource(), selectFromData(), and table().

00267                                                             {
00268 
00269          // Initialize:
00270       $this->resetStatusVars();
00271 
00272          // Reading Data Source if not done already.
00273       $this->readDataSource($table);
00274 
00275          // If anything is there:
00276       if (is_array($this->data[$table]))  {
00277 
00278             // Parse WHERE clause:
00279          $where = $this->parseWhereClause($where);
00280 
00281          if (is_array($where))   {
00282 
00283                // Field information
00284             $fieldInformation = $this->admin_get_fields($table);     // Should cache this...!
00285 
00286                // Traverse fields to update:
00287             foreach($fields_values as $fName => $fValue) {
00288                $this->processAccordingToConfig($fields_values[$fName],$fieldInformation[$fName]);
00289             }
00290 #debug($fields_values);
00291                // Do query, returns array with keys to the data array of the result:
00292             $itemKeys = $this->selectFromData($table,$where);
00293 
00294                // Set "last affected rows":
00295             $this->lastAffectedRows = count($itemKeys);
00296 
00297                // Update rows:
00298             if ($this->lastAffectedRows)  {
00299                   // Traverse result set here:
00300                foreach($itemKeys as $dataArrayKey) {
00301 
00302                      // Traverse fields to update:
00303                   foreach($fields_values as $fName => $fValue) {
00304                      $this->data[$table][$dataArrayKey][$fName] = $fValue;
00305                   }
00306                }
00307 
00308                // Save data source
00309                $this->saveDataSource($table);
00310             }
00311 
00312             return TRUE;
00313          } else $this->errorStatus = 'WHERE clause contained errors: '.$where;
00314       } else $this->errorStatus = 'No data loaded.';
00315 
00316       return FALSE;
00317    }

t3lib_sqlengine::getResultSet keys,
table,
fieldList
 

[Describe function...]

Parameters:
[type] $keys: ...
[type] $table: ...
[type] $fields: ...
Returns:
[type] ...

Definition at line 740 of file class.t3lib_sqlengine.php.

Referenced by exec_SELECTquery().

00740                                                       {
00741       $fields = t3lib_div::trimExplode(',',$fieldList);
00742 
00743       $output = array();
00744       foreach($keys as $kValue)  {
00745          if ($fieldList=='*') {
00746             $output[$kValue] = $this->data[$table][$kValue];
00747          } else {
00748             foreach($fields as $fieldName)   {
00749                $output[$kValue][$fieldName] = $this->data[$table][$kValue][$fieldName];
00750             }
00751          }
00752       }
00753 
00754       return $output;
00755    }

t3lib_sqlengine::init config,
&$  pObj
 

Dummy function for initializing SQL handler.

Create you own in derived classes.

Parameters:
array Configuration array from handler
object Parent object
Returns:
void

Definition at line 124 of file class.t3lib_sqlengine.php.

00124                                     {
00125    }

t3lib_sqlengine::processAccordingToConfig &$  value,
fInfo
 

Processing of update/insert values based on field type.

Parameters:
[type] $$value: ...
[type] $fInfo: ...
Returns:
[type] ...

Definition at line 145 of file class.t3lib_sqlengine.php.

References t3lib_sqlparser::parseFieldDef().

Referenced by exec_INSERTquery(), and exec_UPDATEquery().

00145                                                       {
00146       $options = $this->parseFieldDef($fInfo['Type']);
00147 
00148       switch(strtolower($options['fieldType'])) {
00149          case 'int':
00150          case 'smallint':
00151          case 'tinyint':
00152          case 'mediumint':
00153             $value = intval($value);
00154             if ($options['featureIndex']['UNSIGNED']) {
00155                $value = t3lib_div::intInRange($value,0);
00156             }
00157          break;
00158          case 'double':
00159             $value = (double)$value;
00160          break;
00161          case 'varchar':
00162          case 'char':
00163             $value = substr($value,0,trim($options['value']));
00164          break;
00165          case 'text':
00166          case 'blob':
00167             $value = substr($value,0,65536);
00168          break;
00169          case 'tinytext':
00170          case 'tinyblob':
00171             $value = substr($value,0,256);
00172          break;
00173          case 'mediumtext':
00174          case 'mediumblob':
00175             // ??
00176          break;
00177       }
00178    }

t3lib_sqlengine::quoteStr str  ) 
 

Quoting strings for insertion in SQL queries.

Parameters:
string String
Returns:
string String, with quotes escaped

Definition at line 459 of file class.t3lib_sqlengine.php.

00459                            {
00460       return addslashes($str);
00461    }

t3lib_sqlengine::readDataSource table  ) 
 

Dummy function for setting table data.

Create your own. NOTICE: Handler to "table-locking" needs to be made probably!

Parameters:
string Table name
Returns:
void
Todo:
Table locking tools?

Definition at line 542 of file class.t3lib_sqlengine.php.

Referenced by exec_DELETEquery(), exec_INSERTquery(), exec_SELECTquery(), and exec_UPDATEquery().

00542                                     {
00543       $this->data[$table] = array();
00544    }

t3lib_sqlengine::resetStatusVars  ) 
 

Reset SQL engine status variables (insert id, affected rows, error status).

Returns:
void

Definition at line 132 of file class.t3lib_sqlengine.php.

Referenced by exec_DELETEquery(), exec_INSERTquery(), exec_SELECTquery(), and exec_UPDATEquery().

00132                               {
00133       $this->errorStatus = '';
00134       $this->lastInsertedId = 0;
00135       $this->lastAffectedRows = 0;
00136    }

t3lib_sqlengine::saveDataSource table  ) 
 

Dummy function for setting table data.

Create your own. NOTICE: Handler to "table-locking" needs to be made probably!

Parameters:
string Table name
Returns:
void
Todo:
Table locking tools?

Definition at line 554 of file class.t3lib_sqlengine.php.

References debug(), and table().

Referenced by exec_DELETEquery(), exec_INSERTquery(), and exec_UPDATEquery().

00554                                     {
00555       debug($this->data[$table]);
00556    }

t3lib_sqlengine::select_evalSingle table,
config,
&$  itemKeys
 

[Describe function...]

Parameters:
[type] $table: ...
[type] $config: ...
[type] $itemKeys: ...
Returns:
[type] ...

Definition at line 619 of file class.t3lib_sqlengine.php.

References selectFromData(), and table().

Referenced by selectFromData().

00619                                                          {
00620       $neg = ereg('^AND[[:space:]]+NOT$',trim($config['operator']));
00621 
00622       if (is_array($config['sub'])) {
00623          $subSelKeys = $this->selectFromData($table,$config['sub']);
00624          if ($neg)   {
00625             foreach($itemKeys as $kk => $vv) {
00626                if (in_array($vv,$subSelKeys))   {
00627                   unset($itemKeys[$kk]);
00628                }
00629             }
00630          } else {
00631             $itemKeys = array_intersect($itemKeys, $subSelKeys);
00632          }
00633       } else {
00634          $comp = strtoupper(ereg_replace('[[:space:]]','',$config['comparator']));
00635          $mod = strtoupper($config['modifier']);
00636          switch($comp)  {
00637             case 'NOTLIKE':
00638             case 'LIKE':
00639                $like_value = strtolower($config['value'][0]);
00640                if (substr($like_value,0,1)=='%')   {
00641                   $wildCard_begin = TRUE;
00642                   $like_value = substr($like_value,1);
00643                }
00644                if (substr($like_value,-1)=='%') {
00645                   $wildCard_end = TRUE;
00646                   $like_value = substr($like_value,0,-1);
00647                }
00648             break;
00649             case 'NOTIN':
00650             case 'IN':
00651                $in_valueArray = array();
00652                foreach($config['value'] as $vParts)   {
00653                   $in_valueArray[] = (string)$vParts[0];
00654                }
00655             break;
00656          }
00657 
00658          foreach($itemKeys as $kk => $v)  {
00659             $field_value = $this->data[$table][$v][$config['field']];
00660 
00661                // Calculate it:
00662             if ($config['calc']=='&')  {
00663                $field_value&=intval($config['calc_value']);
00664             }
00665 
00666                // Compare it:
00667             switch($comp)  {
00668                case '<=':
00669                   $bool = $field_value <= $config['value'][0];
00670                break;
00671                case '>=':
00672                   $bool = $field_value >= $config['value'][0];
00673                break;
00674                case '<':
00675                   $bool = $field_value < $config['value'][0];
00676                break;
00677                case '>':
00678                   $bool = $field_value > $config['value'][0];
00679                break;
00680                case '=':
00681                   $bool = !strcmp($field_value,$config['value'][0]);
00682                break;
00683                case '!=':
00684                   $bool = strcmp($field_value,$config['value'][0]);
00685                break;
00686                case 'NOTIN':
00687                case 'IN':
00688                   $bool = in_array((string)$field_value, $in_valueArray);
00689                   if ($comp=='NOTIN')  $bool = !$bool;
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')   $bool = !$bool;
00705                break;
00706                default:
00707                   $bool = $field_value ? TRUE : FALSE;
00708                break;
00709             }
00710 
00711                // General negation:
00712             if ($neg)   $bool = !$bool;
00713 
00714                // Modify?
00715             switch($mod)   {
00716                case 'NOT':
00717                case '!':
00718                   $bool = !$bool;
00719                break;
00720             }
00721 
00722                // Action:
00723             if (!$bool) {
00724                unset($itemKeys[$kk]);
00725             }
00726          }
00727 
00728 #        echo $this->debug_printResultSet($this->getResultSet($itemKeys,$table,'uid,tstamp'));
00729       }
00730    }

t3lib_sqlengine::selectFromData table,
where
 

[Describe function...]

Parameters:
[type] $table: ...
[type] $where: ...
Returns:
[type] ...

Definition at line 583 of file class.t3lib_sqlengine.php.

References select_evalSingle(), and table().

Referenced by exec_DELETEquery(), exec_SELECTquery(), exec_UPDATEquery(), and select_evalSingle().

00583                                           {
00584 
00585       $output = array();
00586       if (is_array($this->data[$table]))  {
00587 
00588             // All keys:
00589          $OR_index = 0;
00590 
00591          foreach($where as $config) {
00592 
00593             if (strtoupper($config['operator'])=='OR')   {
00594                $OR_index++;
00595             }
00596 
00597             if (!isset($itemKeys[$OR_index]))   $itemKeys[$OR_index] = array_keys($this->data[$table]);
00598 
00599             $this->select_evalSingle($table,$config,$itemKeys[$OR_index]);
00600          }
00601 
00602          foreach($itemKeys as $uidKeys)   {
00603             $output = array_merge($output, $uidKeys);
00604          }
00605          $output = array_unique($output);
00606       }
00607 
00608       return $output;
00609    }

t3lib_sqlengine::sql_affected_rows  ) 
 

Returns affected rows (of UPDATE and DELETE queries).

Returns:
integer Last amount of affected rows.

Definition at line 449 of file class.t3lib_sqlengine.php.

00449                                 {
00450       return $this->lastAffectedRows;
00451    }

t3lib_sqlengine::sql_error  ) 
 

Returns most recent error.

Returns:
string Error message, if any

Definition at line 431 of file class.t3lib_sqlengine.php.

00431                         {
00432       return $this->errorStatus;
00433    }

t3lib_sqlengine::sql_insert_id  ) 
 

Returns most recently create unique ID (of INSERT queries).

Returns:
integer Last unique id created.

Definition at line 440 of file class.t3lib_sqlengine.php.

00440                             {
00441       return $this->lastInsertedId;
00442    }

t3lib_sqlengine::sql_query query  ) 
 

Performs an SQL query on the "database".

Parameters:
string Query to execute
Returns:
object Result object or false if error

Definition at line 420 of file class.t3lib_sqlengine.php.

Referenced by admin_query().

00420                               {
00421       $res = t3lib_div::makeInstance('t3lib_sqlengine_resultobj');
00422       $res->result = array();
00423       return $res;
00424    }


Member Data Documentation

t3lib_sqlengine::$data = array()
 

Definition at line 105 of file class.t3lib_sqlengine.php.

t3lib_sqlengine::$errorStatus = ''
 

Definition at line 109 of file class.t3lib_sqlengine.php.

t3lib_sqlengine::$lastAffectedRows = 0
 

Definition at line 111 of file class.t3lib_sqlengine.php.

t3lib_sqlengine::$lastInsertedId = 0
 

Definition at line 110 of file class.t3lib_sqlengine.php.


The documentation for this class was generated from the following file:
Generated on Sun Oct 3 01:07:12 2004 for TYPO3core 3.7.0 dev by  doxygen 1.3.8-20040913