[ 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 postgres7_driver_ADOConnection extends ADOConnection 12 { 13 var $connect_string; 14 var $sysDate = "CURRENT_DATE"; 15 var $sysTimeStamp = "CURRENT_TIMESTAMP"; 16 var $pg_escape_enable = false; 17 18 function postgres7_driver_ADOConnection() 19 { 20 $this->dbtype = 'postgres7'; 21 } 22 23 /** 24 * Connection to database server and selected database 25 * 26 * @access private 27 */ 28 29 function _connect($host = "", $username = "", $password = "", $database = "", $persistent, $forcenew) 30 { 31 if (!function_exists('pg_connect')) return false; 32 33 $this->host = $host; 34 $this->username = $this->query_addslashes($username); 35 $this->password = $this->query_addslashes($password); 36 if (strlen($database) == 0) $database = 'template1'; 37 $this->database = $this->query_addslashes($database); 38 39 if ($this->username || $this->password || $this->database) { 40 $this->connect_string = $this->host; 41 if ($this->connect_string) { 42 $host = split(":", $this->connect_string); 43 if ($host[0]) $this->connect_string = "host=" . $this->query_addslashes($host[0]); 44 else $this->connect_string = 'host=localhost'; 45 if (isset($host[1])) $this->connect_string .= " port=$host[1]"; 46 else if (!empty($this->port)) $this->connect_string .= " port=" . $this->port; 47 } 48 if ($this->username) $this->connect_string .= " user=" . $this->username; 49 if ($this->password) $this->connect_string .= " password=" . $this->password; 50 if ($this->database) $this->connect_string .= " dbname=" . $this->database; 51 } 52 else 53 { 54 $this->connect_string = $this->host; 55 } 56 57 $this->persistent = $persistent; 58 $this->forcenewconnection = $forcenew; 59 60 $this->_makeconnection(); 61 62 if ($this->connectionId === false) 63 { 64 if ($fn = $this->raiseErrorFn) 65 $fn($this->dbtype, 'CONNECT', $this->ErrorNo(), $this->ErrorMsg(), $this->host, $this->database, $this); 66 return $this->SelectDB( $this->database ); 67 } 68 69 return true; 70 } 71 72 function _makeconnection() 73 { 74 if($this->persistent == 1) 75 { 76 $this->connectionId = @pg_pconnect( $this->connect_string ); 77 } 78 else 79 { 80 if($this->forcenewconnection) 81 { 82 $this->connectionId = @pg_connect( $this->connect_string ); 83 } 84 else 85 { 86 $this->connectionId = @pg_connect( $this->connect_string, PGSQL_CONNECT_FORCE_NEW ); 87 } 88 } 89 } 90 91 function query_addslashes($query) 92 { 93 $len = strlen($query); 94 if ($len == 0) 95 return "''"; 96 if (strncmp($query,"'",1) === 0 && substr($query,$len-1) == "'") 97 return $s; 98 return "'".addslashes($query)."'"; 99 } 100 101 /** 102 * Choose a database to connect. 103 * 104 * @param dbname is the name of the database to select 105 * @return true or false 106 * @access public 107 */ 108 109 function SelectDB($dbname) 110 { 111 $this->database = $dbname; 112 113 if ($this->connectionId === false) 114 { 115 if($this->createdatabase == true) 116 { 117 $this->connectionId = @pg_pconnect( "host=$this->host user=$this->username password=$this->password" ); 118 $result = @pg_query($this->connectionId, "CREATE DATABASE " . $this->database ); 119 if ($result === false) { // error handling if query fails 120 $this->connectionId = false; 121 return false; 122 } 123 124 $this->_makeconnection(); 125 if($this->connectionId === false) 126 { 127 $this->connectionId = false; 128 return false; 129 } 130 else 131 { 132 return true; 133 } 134 } 135 $this->connectionId = false; 136 return false; 137 } 138 else 139 { 140 return true; 141 } 142 } 143 144 /** 145 * Return database error message 146 * Usage: $errormessage =& $db->ErrorMsg(); 147 * 148 * @access public 149 */ 150 151 function ErrorMsg() 152 { 153 return @pg_last_error($this->connectionId); 154 } 155 156 /** 157 * Return database error number 158 * Usage: $errorbo =& $db->ErrorNo(); 159 * 160 * @access public 161 */ 162 163 function ErrorNo() 164 { 165 $error = @pg_last_error( $this->connectionId ); 166 return strlen($error) ? $error : 0; 167 } 168 169 /** 170 * Returns # of affected rows from insert/delete/update query 171 * 172 * @access public 173 * @return integer Affected rows 174 */ 175 176 function Affected_Rows() 177 { 178 return @pg_affected_rows( $this->record_set ); 179 } 180 181 /** 182 * Returns the last record id of an inserted item 183 * Usage: $db->Insert_ID(); 184 * 185 * @access public 186 */ 187 188 function Insert_ID() 189 { 190 return @pg_getlastoid($this->record_set); 191 } 192 193 /** 194 * Correctly quotes a string so that all strings are escape coded. 195 * An example is $db->qstr("Haven't a clue."); 196 * 197 * @param string the string to quote 198 * @param [magic_quotes] if $s is GET/POST var, set to get_magic_quotes_gpc(). 199 * 200 * @return single-quoted string IE: 'Haven\'t a clue.' 201 */ 202 203 function qstr($string, $magic_quotes=false) 204 { 205 if (!$magic_quotes) { 206 if (strnatcmp(PHP_VERSION, '4.2.0') >= 0 && $this->pg_escape_enable) { 207 return "'" . pg_escape_string($string) . "'"; 208 } 209 $string = str_replace("'", "\\'", str_replace('\\', '\\\\', str_replace("\0", "\\\0", $string))); 210 return "'" . $string . "'"; 211 } 212 return "'" . str_replace('\\"', '"', $string) . "'"; 213 } 214 215 function QMagic($string) 216 { 217 return $this->qstr($string, get_magic_quotes_gpc()); 218 } 219 220 /** 221 * Returns concatenated string 222 * Usage: $db->Concat($str1,$str2); 223 * 224 * @return concatenated string 225 */ 226 function Concat() 227 { 228 $arr = func_get_args(); 229 return implode("||", $arr); 230 } 231 232 function IfNull( $field, $ifNull ) 233 { 234 return " coalesce($field, $ifNull) "; 235 } 236 237 /** 238 * Closes database connection 239 * Usage: $db->close(); 240 * 241 * @access public 242 */ 243 244 function Close() 245 { 246 @pg_close( $this->connectionId ); 247 $this->connectionId = false; 248 } 249 250 /** 251 * Returns All Records in an array 252 * 253 * Usage: $db->GetAll($sql); 254 * @access public 255 */ 256 257 function &GetAll($sql, $inputarr = false) 258 { 259 $data =& $this->GetArray($sql, $inputarr); 260 return $data; 261 } 262 263 /** 264 * Returns All Records in an array 265 * 266 * Usage: $db->GetArray($sql); 267 * @access public 268 */ 269 270 function &GetArray($sql, $inputarr = false) 271 { 272 $data = false; 273 $result =& $this->Execute($sql, $inputarr); 274 if ($result) 275 { 276 $data =& $result->GetArray(); 277 $result->Close(); 278 } 279 return $data; 280 } 281 282 /** 283 * Executes SQL query and instantiates resultset methods 284 * 285 * @access private 286 * @return mixed Resultset methods 287 */ 288 289 function &do_query( $sql, $offset, $nrows, $inputarr=false ) 290 { 291 global $ADODB_FETCH_MODE; 292 293 $false = false; 294 295 $offsetStr = ''; 296 $limitStr = ''; 297 if ($offset != -1 || $nrows != -1) 298 { 299 $offsetStr = ($offset >= 0) ? " OFFSET " . $offset : ''; 300 $limitStr = ($nrows >= 0) ? " LIMIT " . $nrows : ''; 301 } 302 303 if ($inputarr && is_array($inputarr)) { 304 $sqlarr = explode('?', $sql); 305 if (!is_array(reset($inputarr))) $inputarr = array($inputarr); 306 foreach($inputarr as $arr) { 307 $sql = ''; $i = 0; 308 foreach($arr as $v) { 309 $sql .= $sqlarr[$i]; 310 switch(gettype($v)){ 311 case 'string': 312 $sql .= $this->qstr($v); 313 break; 314 case 'double': 315 $sql .= str_replace(',', '.', $v); 316 break; 317 case 'boolean': 318 $sql .= $v ? "'t'" : "'f'"; 319 break; 320 default: 321 if ($v === null) 322 $sql .= 'NULL'; 323 else $sql .= $v; 324 } 325 $i += 1; 326 } 327 $sql .= $sqlarr[$i]; 328 if ($i+1 != sizeof($sqlarr)) 329 return $false; 330 $this->sql = $sql . "$limitStr$offsetStr"; 331 $time_start = array_sum(explode(' ', microtime())); 332 $this->query_count++; 333 $resultId = @pg_query( $this->sql); 334 $this->query_time_total += (array_sum(explode(' ', microtime())) - $time_start); 335 if($this->debug) 336 { 337 $this->outp($sql . "$limitStr$offsetStr"); 338 } 339 if ($resultId === false) { // error handling if query fails 340 if ($fn = $this->raiseErrorFn) 341 $fn($this->dbtype, 'EXECUTE', $this->ErrorNo(), $this->ErrorMsg(), $this->sql, $inputarr, $this); 342 return $false; 343 } 344 } 345 } 346 else 347 { 348 $this->sql = $sql . "$limitStr$offsetStr"; 349 $time_start = array_sum(explode(' ', microtime())); 350 $this->query_count++; 351 $resultId = @pg_query( $this->sql); 352 $this->query_time_total += (array_sum(explode(' ', microtime())) - $time_start); 353 if($this->debug) 354 { 355 $this->outp($sql . "$limitStr$offsetStr"); 356 } 357 } 358 359 if ($resultId === false) { // error handling if query fails 360 if ($fn = $this->raiseErrorFn) 361 $fn($this->dbtype, 'EXECUTE', $this->ErrorNo(), $this->ErrorMsg(), $this->sql, $inputarr, $this); 362 return $false; 363 } 364 365 if ($resultId === true) { // return simplified recordset for inserts/updates/deletes with lower overhead 366 $rs =& new ADORecordSet_empty(); 367 $this->record_set = $recordset; 368 return $rs; 369 } 370 371 $resultset_name = $this->last_module_name . "_ResultSet"; 372 $recordset = new $resultset_name( $resultId, $this->connectionId ); 373 $this->record_set = $recordset; 374 375 $recordset->_currentRow = 0; 376 377 switch ($ADODB_FETCH_MODE) 378 { 379 case ADODB_FETCH_NUM: $recordset->fetchMode = PGSQL_NUM; break; 380 case ADODB_FETCH_ASSOC:$recordset->fetchMode = PGSQL_ASSOC; break; 381 default: 382 case ADODB_FETCH_DEFAULT: 383 case ADODB_FETCH_BOTH:$recordset->fetchMode = PGSQL_BOTH; break; 384 } 385 386 $recordset->_numOfRows = @pg_numrows( $resultId ); 387 if( $recordset->_numOfRows == 0) 388 { 389 $recordset->EOF = true; 390 } 391 $recordset->_numOfFields = @pg_numfields( $resultId ); 392 $recordset->_fetch(); 393 394 return $recordset; 395 } 396 } 397 398 class postgres7_driver_ResultSet 399 { 400 var $connectionId; 401 var $fields; 402 var $resultId; 403 var $_currentRow = 0; 404 var $_numOfRows = -1; 405 var $_numOfFields = -1; 406 var $fetchMode; 407 var $EOF; 408 409 /** 410 * pgsqlResultSet Constructor 411 * 412 * @access private 413 * @param string $record 414 * @param string $resultId 415 */ 416 417 function postgres7_driver_ResultSet( $resultId, $connectionId ) 418 { 419 $this->fields = array(); 420 $this->connectionId = $connectionId; 421 $this->record = array(); 422 $this->resultId = $resultId; 423 $this->EOF = false; 424 } 425 426 /** 427 * Frees resultset 428 * 429 * @access public 430 */ 431 432 function Close() 433 { 434 pg_free_result( $this->resultId ); 435 $this->fields = array(); 436 $this->resultId = false; 437 } 438 439 /** 440 * Returns field name from select query 441 * 442 * @access public 443 * @param string $field 444 * @return string Field name 445 */ 446 447 function fields( $field ) 448 { 449 return $this->fields[$field]; 450 } 451 452 /** 453 * Returns numrows from select query 454 * 455 * @access public 456 * @return integer Numrows 457 */ 458 459 function RecordCount() 460 { 461 return $this->_numOfRows; 462 } 463 464 /** 465 * PEAR DB Compatable Command 466 */ 467 function NumRows() 468 { 469 return $this->_numOfRows; 470 } 471 472 /** 473 * Returns num of fields from select query 474 * 475 * @access public 476 * @return integer numfields 477 */ 478 479 function FieldCount() 480 { 481 return $this->_numOfFields; 482 } 483 484 /** 485 * PEAR DB Compatable Command 486 */ 487 function NumCols() 488 { 489 return $this->_numOfFields; 490 } 491 492 /** 493 * Returns next record 494 * 495 * @access public 496 */ 497 498 function MoveNext() 499 { 500 if (@$this->fields = pg_fetch_array($this->resultId, NULL, $this->fetchMode)) { 501 $this->_currentRow += 1; 502 return true; 503 } 504 if (!$this->EOF) { 505 $this->_currentRow += 1; 506 $this->EOF = true; 507 } 508 return false; 509 } 510 511 /** 512 * Move to the first row in the recordset. Many databases do NOT support this. 513 * 514 * @return true or false 515 */ 516 517 function MoveFirst() 518 { 519 if ($this->_currentRow == 0) return true; 520 return $this->Move(0); 521 } 522 523 /** 524 * Returns the Last Record 525 * 526 * @access public 527 */ 528 529 function MoveLast() 530 { 531 if ($this->EOF) return false; 532 return $this->Move($this->_numOfRows - 1); 533 } 534 535 /** 536 * Random access to a specific row in the recordset. Some databases do not support 537 * access to previous rows in the databases (no scrolling backwards). 538 * 539 * @param rowNumber is the row to move to (0-based) 540 * 541 * @return true if there still rows available, or false if there are no more rows (EOF). 542 */ 543 544 function Move($rowNumber = 0) 545 { 546 if ($rowNumber == $this->_currentRow) return true; 547 $this->EOF = false; 548 if ($this->_numOfRows > 0){ 549 if ($rowNumber >= $this->_numOfRows - 1){ 550 $rowNumber = $this->_numOfRows - 1; 551 } 552 } 553 554 if ($this->_seek($rowNumber)) { 555 $this->_currentRow = $rowNumber; 556 if ($this->_fetch()) { 557 return true; 558 } 559 $this->fields = false; 560 } 561 $this->EOF = true; 562 return false; 563 } 564 565 /** 566 * Perform Seek to specific row 567 * 568 * @access private 569 */ 570 571 function _seek($row) 572 { 573 if ($this->_numOfRows == 0) return false; 574 return @pg_result_seek($this->resultId,$row); 575 } 576 577 /** 578 * Fills field array with first database element when query initially executed 579 * 580 * @access private 581 */ 582 583 function _fetch() 584 { 585 $this->fields = @pg_fetch_array($this->resultId, NULL, $this->fetchMode); 586 return is_array($this->fields); 587 } 588 589 /** 590 * Check to see if last record reached 591 * 592 * @access public 593 */ 594 595 function EOF() 596 { 597 if( $this->_currentRow < $this->_numOfRows) 598 { 599 return false; 600 } 601 else 602 { 603 $this->EOF = true; 604 return true; 605 } 606 } 607 608 /** 609 * Returns All Records in an array 610 * 611 * @access public 612 * @param [nRows] is the number of rows to return. -1 means every row. 613 */ 614 615 function &GetArray($nRows = -1) 616 { 617 $results = array(); 618 $cnt = 0; 619 while (!$this->EOF && $nRows != $cnt) { 620 $results[] = $this->fields; 621 $this->MoveNext(); 622 $cnt++; 623 } 624 return $results; 625 } 626 627 function &GetRows($nRows = -1) 628 { 629 $arr =& $this->GetArray($nRows); 630 return $arr; 631 } 632 633 function &GetAll($nRows = -1) 634 { 635 $arr =& $this->GetArray($nRows); 636 return $arr; 637 } 638 639 /** 640 * Fetch field information for a table. 641 * 642 * @return object containing the name, type and max_length 643 */ 644 function FetchField($fieldOffset = -1) 645 { 646 $fieldObject= new ADOFieldObject(); 647 $fieldObject->name = @pg_fieldname($this->resultId, $fieldOffset); 648 $fieldObject->type = @pg_fieldtype($this->resultId, $fieldOffset); 649 $fieldObject->max_length = @pg_fieldsize($this->resultId, $fieldOffset); 650 return $fieldObject; 651 } 652 } 653 ?>
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 |