|
TYPO3 API
SVNRelease
|
00001 <?php 00002 /*************************************************************** 00003 * Copyright notice 00004 * 00005 * (c) 2004-2009 Kasper Skårhøj (kasper@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 * Contains an example DBAL handler class 00029 * 00030 * $Id: class.tx_dbal_handler_rawmysql.php 40828 2010-12-05 14:55:53Z xperseguers $ 00031 * 00032 * @author Kasper Skårhøj <kasper@typo3.com> 00033 */ 00034 /** 00035 * [CLASS/FUNCTION INDEX of SCRIPT] 00036 * 00037 * 00038 * 00039 * 86: class tx_dbal_handler_rawmysql extends tx_dbal_sqlengine 00040 * 99: function init($config,&$pObj) 00041 * 123: function exec_INSERTquery($table,$fields_values) 00042 * 135: function exec_UPDATEquery($table,$where,$fields_values) 00043 * 146: function exec_DELETEquery($table,$where) 00044 * 161: function exec_SELECTquery($select_fields,$from_table,$where_clause,$groupBy,$orderBy,$limit) 00045 * 173: function sql_error() 00046 * 182: function sql_insert_id() 00047 * 191: function sql_affected_rows() 00048 * 201: function sql_query($query) 00049 * 213: function quoteStr($str) 00050 * 00051 * SECTION: SQL admin functions 00052 * 237: function admin_get_tables() 00053 * 254: function admin_get_fields($tableName) 00054 * 272: function admin_get_keys($tableName) 00055 * 290: function admin_query($query) 00056 * 00057 * 00058 * 308: class tx_dbal_handler_rawmysql_sqlObj extends tx_dbal_sqlengine_resultobj 00059 * 317: function sql_num_rows() 00060 * 326: function sql_fetch_assoc() 00061 * 335: function sql_fetch_row() 00062 * 345: function sql_data_seek($pointer) 00063 * 00064 * TOTAL FUNCTIONS: 18 00065 * (This index is automatically created/updated by the extension "extdeveval") 00066 * 00067 */ 00068 00069 00070 00071 00072 00073 00074 00075 00076 00077 00078 /** 00079 * Example DBAL userdefined handler class 00080 * It simply makes pass-through of MySQL 00081 * 00082 * @author Kasper Skårhøj <kasper@typo3.com> 00083 * @package TYPO3 00084 * @subpackage tx_dbal 00085 */ 00086 class tx_dbal_handler_rawmysql extends tx_dbal_sqlengine { 00087 00088 var $config = array(); 00089 var $link; 00090 var $pObj; // Set from DBAL class. 00091 00092 /** 00093 * Initialize. 00094 * For MySQL we will have to connect to the database and select the database. 00095 * 00096 * @param array Configuration array from handler 00097 * @param object Parent object. 00098 * @return boolean True if connection and database selection worked out well. 00099 */ 00100 function init($config,&$pObj) { 00101 $this->config = $config['config']; 00102 $this->pObj = $pObj; 00103 $this->link = mysql_pconnect( 00104 $this->config['host'], 00105 $this->config['username'], 00106 $this->config['password'] 00107 ); 00108 00109 // Select database as well: 00110 if (mysql_select_db($this->config['database'], $this->link)) { 00111 $output = TRUE; 00112 } 00113 00114 return $output; 00115 } 00116 00117 /** 00118 * Execute INSERT query 00119 * 00120 * @param string Table name 00121 * @param array Field=>Value array 00122 * @return boolean True on success 00123 */ 00124 function exec_INSERTquery($table,$fields_values) { 00125 return mysql_query($GLOBALS['TYPO3_DB']->INSERTquery($table,$fields_values), $this->link); 00126 } 00127 00128 /** 00129 * Execute UPDATE query 00130 * 00131 * @param string Table name 00132 * @param string WHERE clause 00133 * @param array Field=>Value array 00134 * @return boolean True on success 00135 */ 00136 function exec_UPDATEquery($table,$where,$fields_values) { 00137 return mysql_query($GLOBALS['TYPO3_DB']->UPDATEquery($table,$where,$fields_values), $this->link); 00138 } 00139 00140 /** 00141 * Execute DELETE query 00142 * 00143 * @param string Table name 00144 * @param string WHERE clause 00145 * @return boolean True on success 00146 */ 00147 function exec_DELETEquery($table,$where) { 00148 return mysql_query($GLOBALS['TYPO3_DB']->DELETEquery($table,$where), $this->link); 00149 } 00150 00151 /** 00152 * Execute SELECT query 00153 * 00154 * @param string List of fields to select from the table. This is what comes right after "SELECT ...". Required value. 00155 * @param string Table(s) from which to select. This is what comes right after "FROM ...". Required value. 00156 * @param string Optional additional WHERE clauses put in the end of the query. NOTICE: You must escape values with addslashes() first 00157 * @param string Optional GROUP BY field(s), if none, supply blank string. 00158 * @param string Optional ORDER BY field(s), if none, supply blank string. 00159 * @param string Optional LIMIT value ([begin,]max), if none, supply blank string. 00160 * @return object Result object 00161 */ 00162 function exec_SELECTquery($select_fields,$from_table,$where_clause,$groupBy,$orderBy,$limit) { 00163 $res = t3lib_div::makeInstance('tx_dbal_handler_rawmysql_sqlObj'); // Create result object 00164 $this->pObj->lastQuery = $GLOBALS['TYPO3_DB']->SELECTquery($select_fields,$from_table,$where_clause,$groupBy,$orderBy,$limit); 00165 $res->result = mysql(TYPO3_db, $this->pObj->lastQuery, $this->link); // Execute query 00166 return $res; 00167 } 00168 00169 /** 00170 * mysql_error() wrapper 00171 * 00172 * @return string mysql_error() 00173 */ 00174 function sql_error() { 00175 return mysql_error(); 00176 } 00177 00178 /** 00179 * mysql_insert_id() wrapper 00180 * 00181 * @return integer mysql_insert_id(); 00182 */ 00183 function sql_insert_id() { 00184 return mysql_insert_id(); 00185 } 00186 00187 /** 00188 * mysql_affected_rows() wrapper 00189 * 00190 * @return integer mysql_affected_rows() 00191 */ 00192 function sql_affected_rows() { 00193 return mysql_affected_rows(); 00194 } 00195 00196 /** 00197 * mysql_query() wrapper 00198 * 00199 * @param string Query string 00200 * @return object Result object 00201 */ 00202 function sql_query($query) { 00203 $res = t3lib_div::makeInstance('tx_dbal_handler_rawmysql_sqlObj'); 00204 $res->result = mysql_query($query, $this->link); 00205 return $res; 00206 } 00207 00208 /** 00209 * Escape quotes in strings 00210 * 00211 * @param string Input string 00212 * @return string Output string 00213 */ 00214 function quoteStr($str) { 00215 return addslashes($str); 00216 } 00217 00218 00219 00220 00221 00222 00223 00224 00225 00226 /************************************** 00227 * 00228 * SQL admin functions 00229 * (For use in the Install Tool and Extension Manager) 00230 * 00231 **************************************/ 00232 00233 /** 00234 * Returns the list of tables from the database, quering MySQL for it. 00235 * 00236 * @return array Tables in an array (tablename is in both key and value) 00237 * @todo Should return table details in value! see t3lib_db::admin_get_tables() 00238 */ 00239 function admin_get_tables() { 00240 $whichTables = array(); 00241 $tables_result = mysql_list_tables($this->config['database'], $this->link); 00242 if (!mysql_error()) { 00243 while ($theTable = mysql_fetch_assoc($tables_result)) { 00244 $whichTables[current($theTable)] = current($theTable); 00245 } 00246 } 00247 return $whichTables; 00248 } 00249 00250 /** 00251 * Returns information about each field in the $table, quering MySQL for it. 00252 * 00253 * @param string Table name 00254 * @return array Field information in an associative array with fieldname => field row 00255 */ 00256 function admin_get_fields($tableName) { 00257 $output = array(); 00258 00259 if ($columns_res = @mysql_query('SHOW columns FROM '.$tableName, $this->link)) { 00260 while($fieldRow = mysql_fetch_assoc($columns_res)) { 00261 $output[$fieldRow["Field"]] = $fieldRow; 00262 } 00263 } 00264 00265 return $output; 00266 } 00267 00268 /** 00269 * Returns information about each index key in the $table, quering MySQL for it. 00270 * 00271 * @param string Table name 00272 * @return array Key information in a numeric array 00273 */ 00274 function admin_get_keys($tableName) { 00275 $output = array(); 00276 00277 if ($keyRes = @mysql_query('SHOW keys FROM '.$tableName, $this->link)) { 00278 while($keyRow = mysql_fetch_assoc($keyRes)) { 00279 $output[] = $keyRow; 00280 } 00281 } 00282 00283 return $output; 00284 } 00285 00286 /** 00287 * mysql() wrapper function, used by the Install Tool and EM for all queries regarding management of the database! 00288 * 00289 * @param string Query to execute 00290 * @return pointer Result pointer 00291 */ 00292 function admin_query($query) { 00293 return $this->sql_query($query); 00294 } 00295 } 00296 00297 00298 00299 00300 00301 00302 00303 /** 00304 * Result object for this MySQL userdefined handler 00305 * 00306 * @author Kasper Skårhøj <kasper@typo3.com> 00307 * @package TYPO3 00308 * @subpackage tx_dbal 00309 */ 00310 class tx_dbal_handler_rawmysql_sqlObj extends tx_dbal_sqlengine_resultobj { 00311 00312 var $result = ''; // Not array here, but resource pointer. 00313 00314 /** 00315 * mysql_num_rows() Wrapper 00316 * 00317 * @return integer mysql_num_rows() 00318 */ 00319 function sql_num_rows() { 00320 return mysql_num_rows($this->result); 00321 } 00322 00323 /** 00324 * mysql_fetch_assoc() Wrapper 00325 * 00326 * @return array mysql_fetch_assoc() 00327 */ 00328 function sql_fetch_assoc() { 00329 return mysql_fetch_assoc($this->result); 00330 } 00331 00332 /** 00333 * mysql_fetch_row() wrapper 00334 * 00335 * @return array mysql_fetch_row() 00336 */ 00337 function sql_fetch_row() { 00338 return mysql_fetch_row($this->result); 00339 } 00340 00341 /** 00342 * mysql_data_seek() wrapper 00343 * 00344 * @param integer Pointer to go to. 00345 * @return boolean mysql_data_seek() 00346 */ 00347 function sql_data_seek($pointer) { 00348 return mysql_data_seek($this->result,$pointer); 00349 } 00350 } 00351 00352 00353 if (defined('TYPO3_MODE') && isset($GLOBALS['TYPO3_CONF_VARS'][TYPO3_MODE]['XCLASS']['ext/dbal/handlers/class.tx_dbal_handler_rawmysql.php'])) { 00354 include_once($GLOBALS['TYPO3_CONF_VARS'][TYPO3_MODE]['XCLASS']['ext/dbal/handlers/class.tx_dbal_handler_rawmysql.php']); 00355 } 00356 ?>
1.8.0