| [ Index ] |
|
Code source de LifeType 1.2.4 |
1 <?php 2 /** 3 * number of SQL queries executed so far. It is up to the driver classes 4 * to keep this number updated!! 5 */ 6 $__pdb_num_queries = 0; 7 8 /** 9 * \ingroup PDb 10 * 11 * This class provides the base methods that all database drivers are expected to implement. Some methods 12 * provide some common functionality that should be called even when the method is overwritten, so please make sure 13 * to call the method of the parent class for those that need it. 14 */ 15 class PDbDriverBase 16 { 17 18 var $_dbname; 19 var $_host; 20 var $_user; 21 var $_password; 22 var $_type = 'generic'; 23 var $nameQuote = '`'; 24 var $_debug = false; 25 var $_connected = false; 26 var $_opts; 27 28 /** 29 * Generates a new instance of the driver. Please use PDb::getDriver() instead 30 * 31 * @see PDb::getDriver() 32 */ 33 function PDbDriverBase() 34 { 35 $this->_opts = Array(); 36 } 37 38 /** 39 * Allows drivers to use custom options 40 * 41 * @param key 42 * @param value 43 */ 44 function setDriverOpt( $key, $value ) 45 { 46 $this->_opts[$key] = $value; 47 } 48 49 /** 50 * Set the driver options via an array. This method will completely replace any 51 * options that have been set up until this moment via PDbDriverBase::setDriverOpt. 52 * 53 * @param opts An associative array containing driver-specific options 54 */ 55 function setDriverOpts( $opts ) 56 { 57 $this->_opts = $opts; 58 } 59 60 /** 61 * Executes a query and returns a PDbResultSet as a result, or false if the query wasn't 62 * successfully executed. 63 * 64 * This method must be implemented by database drivers. 65 * 66 * @param query A string with the query to execute 67 * @param page Page of records, when using pagination. Leave empty if pagination is not needed. 68 * @param itemsPerPage Amount of record per page, when using pagination. 69 * @return A PDbResultSet object if successful or false otherwise 70 */ 71 function Execute( $query, $page = -1, $itemsPerPage = 15 ) 72 { 73 // to be implemented by child classes 74 } 75 76 /** 77 * Starts a new connection to a databse 78 * 79 * Database drivers should call this method after providing their own implementation. 80 * 81 * @param host The host to which we're initiating the connection 82 * @param username The username used to connecto the database 83 * @param password Password assigned to the user above 84 * @param dbname The name of the database to which we're connecting 85 * @return Returns true if successful or false otherwise 86 */ 87 function Connect( $host, $username, $password, $dbname = null ) 88 { 89 $this->_host = $host; 90 $this->_username = $username; 91 $this->_password = $password; 92 $this->_dbname = $dbname; 93 $this->_connected = true; 94 95 // extra functionality to be implemented by child classes... 96 } 97 /** 98 * Starts a new persistent connection to the database 99 * 100 * Database drivers should call this method after providing their own implementation. 101 * 102 * @param host The host to which we're initiating the connection 103 * @param username The username used to connecto the database 104 * @param password Password assigned to the user above 105 * @param dbname The name of the database to which we're connecting 106 * @return Returns true if successful or false otherwise 107 */ 108 function PConnect( $host, $username, $password, $dbname ) 109 { 110 $this->_host = $host; 111 $this->_username = $username; 112 $this->_password = $password; 113 $this->_dbname = $dbname; 114 $this->_connected = true; 115 116 // extra functionality to be implemented by child classes... 117 } 118 /** 119 * Closes the current connection to the database 120 * 121 * This method must be implemented by database drivers. 122 * 123 * @return nothing 124 */ 125 function Close() 126 { 127 // to be implemented by child classes 128 } 129 130 /** 131 * Returns the last error message that was generated by the driver 132 * 133 * This method must be implemented by database drivers. 134 * 135 * @return A string representing an error message, if any. 136 */ 137 function ErrorMsg() 138 { 139 // to be implemented by child classes 140 } 141 142 /** 143 * The row id as generated by the last INSERT operation. 144 * 145 * This method must be implemented by database drivers. 146 * 147 * @return A row id 148 */ 149 function Insert_ID() 150 { 151 // to be implemented by child classes 152 } 153 154 /** 155 * Returns the number of rows affected by the last UPDATE, INSERT or DELETE operation. 156 * Use PDbRecordSet::Row_Count() to retrieve the number of rows in a SELECT operation. 157 * 158 * This method must be implemented by database drivers. 159 * 160 * @return the number of affected rows 161 * @see PDbRecordSet::Row_Count() 162 */ 163 function Affected_Rows() 164 { 165 // to be implemented by child classes 166 } 167 168 /** 169 * Returns true if the driver is currently connected to a database. Connections usually happen 170 * after Connect or PConnect are called 171 * 172 * @return true if the driver is currently conneced or false otherwise 173 * @see PDbDriverBase::PConnect() 174 * @see PDbDriverBase::Connect() 175 */ 176 function IsConnected() 177 { 178 return( $this->_connected ); 179 } 180 181 /** 182 * @private 183 */ 184 function _debugQuery( $query ) 185 { 186 if( $this->_debug ) { 187 print("<hr/>$query<hr/>"); 188 } 189 190 return( true ); 191 } 192 193 /** 194 * Activates the debug stuff. 195 * 196 * @param debug Whether debug should be enabled or not 197 */ 198 function setDebug( $debug ) 199 { 200 $this->_debug = $debug; 201 } 202 203 /** 204 * for compatibility with ADOdb. Use Db::qstr() instead 205 * 206 * @private 207 */ 208 function qstr( $string ) 209 { 210 return( Db::qstr( $string )); 211 } 212 213 /** 214 * Load the correct data dictionary driver. Child classes should overwrite this method 215 * and provide the right driver name so that this method can be called without parameters. 216 * 217 * @param driverName the driver name. 218 * @param A reference to the driver-specific PDbBaseDataDict object 219 */ 220 function &getDriverDataDictionary( $driverName ) 221 { 222 $dataDictPath = PLOG_CLASS_PATH."class/database/pdb/datadict/pdb".strtolower($driverName)."datadict.class.php"; 223 224 // check if the driver exists at all 225 if( !is_readable( $dataDictPath )) { 226 throw( new Exception( "Cannot load data dictionary for driver $driverName!" )); 227 die(); 228 } 229 230 //print( "Loading datadict driver $dataDictPath...<br/>" ); 231 232 lt_include( $dataDictPath ); 233 234 $className = "PDb{$driverName}DataDict"; 235 $class =& new $className(); 236 237 $class->dataProvider = $this; 238 $class->connection = &$this; 239 $class->upperName = strtoupper($driverName); 240 241 return( $class ); 242 } 243 244 /** 245 * @param ttype can either be 'VIEW' or 'TABLE' or false. 246 * If false, both views and tables are returned. 247 * "VIEW" returns only views 248 * "TABLE" returns only tables 249 * @param showSchema returns the schema/user with the table name, eg. USER.TABLE 250 * @param mask is the input mask - only supported by oci8 and postgresql 251 * 252 * @return array of tables for current database. 253 */ 254 function &MetaTables($ttype=false,$showSchema=false,$mask=false) 255 { 256 $metaTablesSQL = "SHOW TABLES"; 257 258 if ($showSchema && is_string($showSchema)) { 259 $metaTablesSQL .= " FROM $showSchema"; 260 } 261 262 if ($mask) { 263 $mask = $this->qstr($mask); 264 $metaTablesSQL .= " LIKE $mask"; 265 } 266 267 $rs = $this->Execute($metaTablesSQL); 268 269 if ($rs === false) return false; 270 $arr =& $rs->GetArray(); 271 $arr2 = array(); 272 273 if ($hast = ($ttype && isset($arr[0][1]))) { 274 $showt = strncmp($ttype,'T',1); 275 } 276 277 for ($i=0; $i < sizeof($arr); $i++) { 278 if ($hast) { 279 if ($showt == 0) { 280 if (strncmp($arr[$i][1],'T',1) == 0) $arr2[] = trim($arr[$i][0]); 281 } else { 282 if (strncmp($arr[$i][1],'V',1) == 0) $arr2[] = trim($arr[$i][0]); 283 } 284 } else 285 $arr2[] = trim($arr[$i][0]); 286 } 287 $rs->Close(); 288 return $arr2; 289 } 290 291 /** 292 * @returns an array with the primary key columns in it. 293 * 294 * @param table 295 * @param owner 296 */ 297 function MetaPrimaryKeys($table, $owner=false) 298 { 299 // owner not used in base class - see oci8 300 $p = array(); 301 $objs =& $this->MetaColumns($table); 302 if ($objs) { 303 foreach($objs as $v) { 304 if (!empty($v->primary_key)) 305 $p[] = $v->name; 306 } 307 } 308 if (sizeof($p)) return $p; 309 return false; 310 } 311 312 /** 313 * List columns in a database as an array of ADOFieldObjects. 314 * See top of file for definition of object. 315 * 316 * @param table table name to query 317 * @param upper uppercase table name (required by some databases) 318 * @schema is optional database schema to use - not supported by all databases. 319 * 320 * @return array of ADOFieldObjects for current table. 321 */ 322 function &MetaColumns($table) 323 { 324 lt_include( PLOG_CLASS_PATH."class/database/pdb/datadict/pdbfielddescobject.class.php" ); 325 326 $rs = $this->Execute(sprintf("SHOW COLUMNS FROM %s",$table)); 327 328 $retarr = array(); 329 330 if( !$rs ) 331 return $retarr; 332 333 while( $row = $rs->FetchRow()) { 334 $fld = new PDbFieldDescObject(); 335 336 // 337 // :TODO: 338 // Is this mysql-specific stuff?? If so, it should be moved to the Mysql driver!! 339 // 340 $fld->name = $row["Field"]; 341 $type = $row["Type"]; 342 343 // split type into type(length): 344 $fld->scale = null; 345 if (preg_match("/^(.+)\((\d+),(\d+)/", $type, $query_array)) { 346 $fld->type = $query_array[1]; 347 $fld->max_length = is_numeric($query_array[2]) ? $query_array[2] : -1; 348 $fld->scale = is_numeric($query_array[3]) ? $query_array[3] : -1; 349 } elseif (preg_match("/^(.+)\((\d+)/", $type, $query_array)) { 350 $fld->type = $query_array[1]; 351 $fld->max_length = is_numeric($query_array[2]) ? $query_array[2] : -1; 352 } else { 353 $fld->type = $type; 354 $fld->max_length = -1; 355 } 356 $fld->not_null = ($row["Null"] != 'YES'); 357 $fld->primary_key = ($row["Key"] == 'PRI'); 358 $fld->auto_increment = (strpos($row["Extra"], 'auto_increment') !== false); 359 $fld->binary = (strpos($type,'blob') !== false); 360 $fld->unsigned = (strpos($type,'unsigned') !== false); 361 362 if (!$fld->binary) { 363 $d = $row["Default"]; 364 if ($d != '' && $d != 'NULL') { 365 $fld->has_default = true; 366 $fld->default_value = $d; 367 } else { 368 $fld->has_default = false; 369 } 370 } 371 372 $retarr[strtoupper($fld->name)] = $fld; 373 } 374 375 $rs->Close(); 376 return $retarr; 377 } 378 379 /** 380 * List columns names in a table as an array. 381 * @param table table name to query 382 * 383 * @return array of column names for current table. 384 */ 385 function &MetaColumnNames($table, $numIndexes=false) 386 { 387 $objarr =& $this->MetaColumns($table); 388 if (!is_array($objarr)) return false; 389 390 $arr = array(); 391 if ($numIndexes) { 392 $i = 0; 393 foreach($objarr as $v) $arr[$i++] = $v->name; 394 } else 395 foreach($objarr as $v) $arr[strtoupper($v->name)] = $v->name; 396 397 return $arr; 398 } 399 400 /** 401 * Returns the number of queries executed so far 402 */ 403 function getNumQueries() 404 { 405 global $__pdb_num_queries; 406 return( $__pdb_num_queries ); 407 } 408 409 /** 410 * @return Returns the name of current character set, or 'default' if none has been explicitely selected 411 */ 412 function getDbCharacterSet() 413 { 414 return( 'default' ); 415 } 416 417 /** 418 * Returns true if the current database supports FULLTEXT searches. This method needs to be 419 * implemented by child classes. 420 * 421 * @return true if FULLTEXT is supported 422 */ 423 function isFullTextSupported() 424 { 425 return( false ); 426 } 427 } 428 ?>
titre
Description
Corps
titre
Description
Corps
titre
Description
Corps
titre
Corps
| Généré le : Mon Nov 26 21:04:15 2007 | par Balluche grâce à PHPXref 0.7 |
|