[ Index ] |
|
Code source de CMS made simple 1.0.5 |
1 <?php 2 3 /** 4 * ADOdb Lite is a PHP class to encapsulate multiple database APIs and is compatible with 5 * a subset of the ADODB Command Syntax. 6 * Currently supports Frontbase, MaxDB, miniSQL, MSSQL, MSSQL Pro, MySQLi, MySQLt, MySQL, PostgresSQL, 7 * PostgresSQL64, PostgresSQL7, SqLite and Sybase. 8 * 9 */ 10 11 class maxdb_driver_ADOConnection extends ADOConnection 12 { 13 function maxdb_driver_ADOConnection() 14 { 15 $this->dbtype = 'maxdb'; 16 } 17 18 /** 19 * Connection to database server and selected database 20 * 21 * @access private 22 */ 23 24 function _connect($host = "", $username = "", $password = "", $database = "", $persistent, $forcenew) 25 { 26 if (!function_exists('maxdb_connect')) return false; 27 28 $this->host = $host; 29 $this->username = $username; 30 $this->password = $password; 31 $this->database = $database; 32 $this->persistent = $persistent; 33 $this->forcenewconnection = $forcenew; 34 35 $this->connectionId = @maxdb_connect( $this->host, $this->username, $this->password ); 36 37 if ($this->connectionId === false) 38 { 39 if ($fn = $this->raiseErrorFn) 40 $fn($this->dbtype, 'CONNECT', $this->ErrorNo(), $this->ErrorMsg(), $this->host, $this->database, $this); 41 return false; 42 } 43 44 if (!empty($this->database)) return $this->SelectDB( $this->database ); 45 return true; 46 } 47 48 /** 49 * Choose a database to connect. 50 * 51 * @param dbname is the name of the database to select 52 * @return true or false 53 * @access public 54 */ 55 56 function SelectDB($dbname) 57 { 58 $this->database = $dbname; 59 60 if ($this->connectionId === false) 61 { 62 $this->connectionId = false; 63 return false; 64 } 65 else 66 { 67 $result = @maxdb_select( $this->connectionId, $this->database ); 68 69 if($result === false) 70 { 71 if($this->createdatabase == true) 72 { 73 $result = @maxdb_query( "CREATE DATABASE " . $this->database, $this->connectionId ); 74 if ($result === false) { // error handling if query fails 75 return false; 76 } 77 $result = @maxdb_select( $this->database, $this->connectionId ); 78 if($result === false) 79 { 80 return false; 81 } 82 } 83 else 84 { 85 return false; 86 } 87 } 88 return true; 89 } 90 } 91 92 /** 93 * Return database error message 94 * Usage: $errormessage =& $db->ErrorMsg(); 95 * 96 * @access public 97 */ 98 99 function ErrorMsg() 100 { 101 return @maxdb_error($this->connectionId); 102 } 103 104 /** 105 * Return database error number 106 * Usage: $errorbo =& $db->ErrorNo(); 107 * 108 * @access public 109 */ 110 111 function ErrorNo() 112 { 113 return @maxdb_errno($this->connectionId); 114 } 115 116 /** 117 * Returns # of affected rows from insert/delete/update query 118 * 119 * @access public 120 * @return integer Affected rows 121 */ 122 123 function Affected_Rows() 124 { 125 return @maxdb_affected_rows($this->connectionId); 126 } 127 128 /** 129 * Returns the last record id of an inserted item 130 * Usage: $db->Insert_ID(); 131 * 132 * @access public 133 */ 134 135 function Insert_ID() 136 { 137 return @maxdb_insert_id($this->connectionId); 138 } 139 140 /** 141 * Correctly quotes a string so that all strings are escape coded. 142 * An example is $db->qstr("Haven't a clue."); 143 * 144 * @param string the string to quote 145 * @param [magic_quotes] if $s is GET/POST var, set to get_magic_quotes_gpc(). 146 * 147 * @return single-quoted string IE: 'Haven\'t a clue.' 148 */ 149 150 function qstr($string, $magic_quotes=false) 151 { 152 if (!$magic_quotes) { 153 $string = str_replace("'", "\\'", str_replace('\\', '\\\\', str_replace("\0", "\\\0", $string))); 154 return "'" . $string . "'"; 155 } 156 return "'" . str_replace('\\"', '"', $string) . "'"; 157 } 158 159 function QMagic($string) 160 { 161 return $this->qstr($string, get_magic_quotes_gpc()); 162 } 163 164 /** 165 * Returns concatenated string 166 * Usage: $db->Concat($str1,$str2); 167 * 168 * @return concatenated string 169 */ 170 function Concat() 171 { 172 $arr = func_get_args(); 173 return implode("+", $arr); 174 } 175 176 function IfNull( $field, $ifNull ) 177 { 178 return " CASE WHEN $field is null THEN $ifNull ELSE $field END "; 179 } 180 181 /** 182 * Closes database connection 183 * Usage: $db->close(); 184 * 185 * @access public 186 */ 187 188 function Close() 189 { 190 @maxdb_close( $this->connectionId ); 191 $this->connectionId = false; 192 } 193 194 /** 195 * Returns All Records in an array 196 * 197 * Usage: $db->GetAll($sql); 198 * @access public 199 */ 200 201 function &GetAll($sql, $inputarr = false) 202 { 203 $data =& $this->GetArray($sql, $inputarr); 204 return $data; 205 } 206 207 /** 208 * Returns All Records in an array 209 * 210 * Usage: $db->GetArray($sql); 211 * @access public 212 */ 213 214 function &GetArray($sql, $inputarr = false) 215 { 216 $data = false; 217 $result =& $this->Execute($sql, $inputarr); 218 if ($result) 219 { 220 $data =& $result->GetArray(); 221 $result->Close(); 222 } 223 return $data; 224 } 225 226 /** 227 * Executes SQL query and instantiates resultset methods 228 * 229 * @access private 230 * @return mixed Resultset methods 231 */ 232 233 function &do_query( $sql, $offset, $nrows, $inputarr=false ) 234 { 235 global $ADODB_FETCH_MODE; 236 237 $false = false; 238 239 // $limit = ''; 240 // if ($offset != -1 || $nrows != -1) 241 // { 242 // $offset = ($offset>=0) ? $offset . "," : ''; 243 // $limit = ' LIMIT ' . $offset . ' ' . $nrows; 244 // } 245 246 if ($inputarr && is_array($inputarr)) { 247 $sqlarr = explode('?', $sql); 248 if (!is_array(reset($inputarr))) $inputarr = array($inputarr); 249 foreach($inputarr as $arr) { 250 $sql = ''; $i = 0; 251 foreach($arr as $v) { 252 $sql .= $sqlarr[$i]; 253 switch(gettype($v)){ 254 case 'string': 255 $sql .= $this->qstr($v); 256 break; 257 case 'double': 258 $sql .= str_replace(',', '.', $v); 259 break; 260 case 'boolean': 261 $sql .= $v ? 1 : 0; 262 break; 263 default: 264 if ($v === null) 265 $sql .= 'NULL'; 266 else $sql .= $v; 267 } 268 $i += 1; 269 } 270 $sql .= $sqlarr[$i]; 271 if ($i+1 != sizeof($sqlarr)) 272 return $false; 273 $this->sql = $sql; 274 $time_start = array_sum(explode(' ', microtime())); 275 $this->query_count++; 276 $resultId = @maxdb_query( $this->sql, $this->connectionId ); 277 $this->query_time_total += (array_sum(explode(' ', microtime())) - $time_start); 278 if($this->debug) 279 { 280 $this->outp($sql); 281 } 282 if ($resultId === false) return $false; 283 } 284 } 285 else 286 { 287 $this->sql = $sql; 288 $time_start = array_sum(explode(' ', microtime())); 289 $this->query_count++; 290 $resultId = @maxdb_query( $this->sql, $this->connectionId ); 291 $this->query_time_total += (array_sum(explode(' ', microtime())) - $time_start); 292 if($this->debug) 293 { 294 $this->outp($sql); 295 } 296 } 297 298 if ($resultId === false) { // error handling if query fails 299 if ($fn = $this->raiseErrorFn) 300 $fn($this->dbtype, 'EXECUTE', $this->ErrorNo(), $this->ErrorMsg(), $this->sql, $inputarr, $this); 301 return $false; 302 } 303 304 if ($resultId === true) { // return simplified recordset for inserts/updates/deletes with lower overhead 305 $rs =& new ADORecordSet_empty(); 306 return $rs; 307 } 308 309 $resultset_name = "maxdb_" . $this->last_module_name . "_ResultSet"; 310 $recordset = new $resultset_name( $resultId, $this->connectionId ); 311 312 $recordset->_currentRow = 0; 313 314 switch ($ADODB_FETCH_MODE) 315 { 316 case ADODB_FETCH_NUM: $recordset->fetchMode = MAXDB_NUM; break; 317 case ADODB_FETCH_ASSOC:$recordset->fetchMode = MAXDB_ASSOC; break; 318 default: 319 case ADODB_FETCH_DEFAULT: 320 case ADODB_FETCH_BOTH:$recordset->fetchMode = MAXDB_BOTH; break; 321 } 322 323 $recordset->_numOfRows = @maxdb_num_rows( $resultId ); 324 if( $recordset->_numOfRows == 0) 325 { 326 $recordset->EOF = true; 327 } 328 $recordset->_numOfFields = @maxdb_field_count( $resultId ); 329 $recordset->_fetch(); 330 331 return $recordset; 332 } 333 } 334 335 class maxdb_driver_ResultSet 336 { 337 var $connectionId; 338 var $fields; 339 var $resultId; 340 var $_currentRow = 0; 341 var $_numOfRows = -1; 342 var $_numOfFields = -1; 343 var $fetchMode; 344 var $EOF; 345 346 /** 347 * maxdbResultSet Constructor 348 * 349 * @access private 350 * @param string $record 351 * @param string $resultId 352 */ 353 354 function maxdb_driver_ResultSet( $resultId, $connectionId ) 355 { 356 $this->fields = array(); 357 $this->connectionId = $connectionId; 358 $this->record = array(); 359 $this->resultId = $resultId; 360 $this->EOF = false; 361 } 362 363 /** 364 * Frees resultset 365 * 366 * @access public 367 */ 368 369 function Close() 370 { 371 @maxdb_free_result( $this->resultId ); 372 $this->fields = array(); 373 $this->resultId = false; 374 } 375 376 /** 377 * Returns field name from select query 378 * 379 * @access public 380 * @param string $field 381 * @return string Field name 382 */ 383 384 function fields( $field ) 385 { 386 return $this->fields[$field]; 387 } 388 389 /** 390 * Returns numrows from select query 391 * 392 * @access public 393 * @return integer Numrows 394 */ 395 396 function RecordCount() 397 { 398 return $this->_numOfRows; 399 } 400 401 /** 402 * Returns num of fields from select query 403 * 404 * @access public 405 * @return integer numfields 406 */ 407 408 function FieldCount() 409 { 410 return $this->_numOfFields; 411 } 412 413 /** 414 * Returns next record 415 * 416 * @access public 417 */ 418 419 function MoveNext() 420 { 421 if (@$this->fields = maxdb_fetch_array($this->resultId,$this->fetchMode)) { 422 $this->_currentRow += 1; 423 return true; 424 } 425 if (!$this->EOF) { 426 $this->_currentRow += 1; 427 $this->EOF = true; 428 } 429 return false; 430 } 431 432 /** 433 * Move to the first row in the recordset. Many databases do NOT support this. 434 * 435 * @return true or false 436 */ 437 438 function MoveFirst() 439 { 440 if ($this->_currentRow == 0) return true; 441 return $this->Move(0); 442 } 443 444 /** 445 * Returns the Last Record 446 * 447 * @access public 448 */ 449 450 function MoveLast() 451 { 452 if ($this->EOF) return false; 453 return $this->Move($this->_numOfRows - 1); 454 } 455 456 /** 457 * Random access to a specific row in the recordset. Some databases do not support 458 * access to previous rows in the databases (no scrolling backwards). 459 * 460 * @param rowNumber is the row to move to (0-based) 461 * 462 * @return true if there still rows available, or false if there are no more rows (EOF). 463 */ 464 465 function Move($rowNumber = 0) 466 { 467 if ($rowNumber == $this->_currentRow) return true; 468 $this->EOF = false; 469 if ($this->_numOfRows > 0){ 470 if ($rowNumber >= $this->_numOfRows - 1){ 471 $rowNumber = $this->_numOfRows - 1; 472 $this->EOF = true; 473 } 474 } 475 476 if ($this->_seek($rowNumber)) { 477 $this->_currentRow = $rowNumber; 478 if ($this->_fetch()) { 479 return true; 480 } 481 $this->fields = false; 482 } 483 $this->EOF = true; 484 return false; 485 } 486 487 /** 488 * Perform Seek to specific row 489 * 490 * @access private 491 */ 492 493 function _seek($row) 494 { 495 return false; 496 } 497 498 /** 499 * Fills field array with first database element when query initially executed 500 * 501 * @access private 502 */ 503 504 function _fetch() 505 { 506 $this->fields = @maxdb_fetch_array($this->resultId,$this->fetchMode); 507 return is_array($this->fields); 508 } 509 510 /** 511 * Check to see if last record reached 512 * 513 * @access public 514 */ 515 516 function EOF() 517 { 518 if( $this->_currentRow < $this->_numOfRows) 519 { 520 return false; 521 } 522 else 523 { 524 $this->EOF = true; 525 return true; 526 } 527 } 528 529 /** 530 * Returns All Records in an array 531 * 532 * @access public 533 * @param [nRows] is the number of rows to return. -1 means every row. 534 */ 535 536 function GetArray($nRows = -1) 537 { 538 $results = array(); 539 $cnt = 0; 540 while (!$this->EOF && $nRows != $cnt) { 541 $results[] = $this->fields; 542 $this->MoveNext(); 543 $cnt++; 544 } 545 if($this->EOF && $nRows != 0 && $cnt == 0) 546 { 547 $results[] = $this->fields; 548 } 549 return $results; 550 } 551 552 function &GetRows($nRows = -1) 553 { 554 $arr =& $this->GetArray($nRows); 555 return $arr; 556 } 557 558 function &GetAll($nRows = -1) 559 { 560 $arr =& $this->GetArray($nRows); 561 return $arr; 562 } 563 564 /** 565 * Fetch field information for a table. 566 * 567 * @return object containing the name, type and max_length 568 */ 569 function FetchField($fieldOffset = -1) 570 { 571 // $fieldOffset not supported by MaxDB 572 $fieldObject = @maxdb_fetch_field($this->resultId); 573 return $fieldObject; 574 } 575 } 576 ?>
titre
Description
Corps
titre
Description
Corps
titre
Description
Corps
titre
Corps
Généré le : Tue Apr 3 18:50:37 2007 | par Balluche grâce à PHPXref 0.7 |