Changeset 5826 in subversion
- Timestamp:
- Jan 26, 2012 7:18:26 AM (16 months ago)
- Location:
- branches/devel-framework/roundcubemail/program/include
- Files:
-
- 1 deleted
- 1 edited
-
rcube_mdb2.php (modified) (38 diffs)
-
rcube_sqlite.inc (deleted)
Legend:
- Unmodified
- Added
- Removed
-
branches/devel-framework/roundcubemail/program/include/rcube_mdb2.php
r5822 r5826 57 57 * @param string $db_dsnr Optional DSN for read only operations 58 58 */ 59 function __construct($db_dsnw, $db_dsnr='', $pconn=false)60 { 61 if (empty($db_dsnr)) 59 public function __construct($db_dsnw, $db_dsnr='', $pconn=false) 60 { 61 if (empty($db_dsnr)) { 62 62 $db_dsnr = $db_dsnw; 63 } 63 64 64 65 $this->db_dsnw = $db_dsnw; … … 86 87 'debug' => $this->debug_mode, 87 88 'debug_handler' => array($this, 'debug_handler'), 88 'portability' => MDB2_PORTABILITY_ALL ^ MDB2_PORTABILITY_EMPTY_TO_NULL); 89 'portability' => MDB2_PORTABILITY_ALL ^ MDB2_PORTABILITY_EMPTY_TO_NULL, 90 ); 89 91 90 92 if ($this->db_provider == 'pgsql') { … … 107 109 else if ($this->db_provider == 'sqlite') { 108 110 $dsn_array = MDB2::parseDSN($dsn); 109 if (!filesize($dsn_array['database']) && !empty($this->sqlite_initials)) 110 $this->_sqlite_create_database($dbh, $this->sqlite_initials); 111 } 112 else if ($this->db_provider!='mssql' && $this->db_provider!='sqlsrv') 111 if (!filesize($dsn_array['database']) && !empty($this->sqlite_initials)) { 112 $this->sqlite_create_database($dbh, $this->sqlite_initials); 113 } 114 } 115 else if ($this->db_provider != 'mssql' && $this->db_provider != 'sqlsrv') { 113 116 $dbh->setCharset('utf8'); 117 } 114 118 115 119 return $dbh; … … 121 125 * 122 126 * @param string $mode Connection mode (r|w) 123 * @access public 124 */ 125 function db_connect($mode) 127 */ 128 public function db_connect($mode) 126 129 { 127 130 // previous connection failed, don't attempt to connect again … … 155 158 } 156 159 157 if ($this->db_connected) 160 if ($this->db_connected) { 158 161 $this->db_mode = $mode; 159 else 162 } 163 else { 160 164 $this->conn_failure = true; 165 } 161 166 } 162 167 … … 166 171 * 167 172 * @param boolean $dbg True if SQL queries should be logged 168 * @access public 169 */ 170 function set_debug($dbg = true) 173 */ 174 public function set_debug($dbg = true) 171 175 { 172 176 $this->debug_mode = $dbg; … … 182 186 * 183 187 * @param boolean True on error 184 * @access public 185 */ 186 function is_error() 188 */ 189 public function is_error() 187 190 { 188 191 return $this->db_error ? $this->db_error_msg : false; … … 194 197 * 195 198 * @param boolean True if in connected state 196 * @access public 197 */ 198 function is_connected() 199 */ 200 public function is_connected() 199 201 { 200 202 return PEAR::isError($this->db_handle) ? false : $this->db_connected; … … 206 208 * This returns true if dsnw != dsnr 207 209 */ 208 function is_replicated()210 public function is_replicated() 209 211 { 210 212 return !empty($this->db_dsnr) && $this->db_dsnw != $this->db_dsnr; … … 217 219 * @param string SQL query to execute 218 220 * @param mixed Values to be inserted in query 221 * 219 222 * @return number Query handle identifier 220 * @access public 221 */ 222 function query() 223 */ 224 public function query() 223 225 { 224 226 $params = func_get_args(); … … 226 228 227 229 // Support one argument of type array, instead of n arguments 228 if (count($params) == 1 && is_array($params[0])) 230 if (count($params) == 1 && is_array($params[0])) { 229 231 $params = $params[0]; 232 } 230 233 231 234 return $this->_query($query, 0, 0, $params); … … 240 243 * @param number Number of rows for LIMIT statement 241 244 * @param mixed Values to be inserted in query 245 * 242 246 * @return number Query handle identifier 243 * @access public 244 */ 245 function limitquery() 247 */ 248 public function limitquery() 246 249 { 247 250 $params = func_get_args(); … … 272 275 273 276 // check connection before proceeding 274 if (!$this->is_connected()) 277 if (!$this->is_connected()) { 275 278 return null; 276 277 if ($this->db_provider == 'sqlite') 278 $this->_sqlite_prepare(); 279 280 if ($numrows || $offset) 279 } 280 281 if ($this->db_provider == 'sqlite') { 282 $this->sqlite_prepare(); 283 } 284 285 if ($numrows || $offset) { 281 286 $result = $this->db_handle->setLimit($numrows,$offset); 282 283 if (empty($params)) 287 } 288 289 if (empty($params)) { 284 290 $result = $mode == 'r' ? $this->db_handle->query($query) : $this->db_handle->exec($query); 291 } 285 292 else { 286 293 $params = (array)$params; … … 313 320 * @param number $res_id Optional query handle identifier 314 321 * @return mixed Number of rows or false on failure 315 * @access public 316 */ 317 function num_rows($res_id=null) 318 { 319 if (!$this->db_connected) 322 */ 323 public function num_rows($res_id=null) 324 { 325 if (!$this->db_connected) { 320 326 return false; 321 322 if ($result = $this->_get_result($res_id)) 327 } 328 329 if ($result = $this->_get_result($res_id)) { 323 330 return $result->numRows(); 324 else 325 return false; 331 } 332 333 return false; 326 334 } 327 335 … … 332 340 * @param number $res_id Optional query handle identifier 333 341 * @return mixed Number of rows or false on failure 334 * @access public 335 */ 336 function affected_rows($res_id = null) 337 { 338 if (!$this->db_connected) 342 */ 343 public function affected_rows($res_id = null) 344 { 345 if (!$this->db_connected) { 339 346 return false; 347 } 340 348 341 349 return $this->_get_result($res_id); … … 348 356 * 349 357 * @param string $table Table name (to find the incremented sequence) 358 * 350 359 * @return mixed ID or false on failure 351 * @access public 352 */ 353 function insert_id($table = '') 354 { 355 if (!$this->db_connected || $this->db_mode == 'r') 360 */ 361 public function insert_id($table = '') 362 { 363 if (!$this->db_connected || $this->db_mode == 'r') { 356 364 return false; 365 } 357 366 358 367 if ($table) { 359 if ($this->db_provider == 'pgsql') 368 if ($this->db_provider == 'pgsql') { 360 369 // find sequence name 361 370 $table = $this->sequence_name($table); 362 else 371 } 372 else { 363 373 // resolve table name 364 374 $table = $this->table_name($table); 375 } 365 376 } 366 377 … … 376 387 * 377 388 * @param number $res_id Optional query handle identifier 389 * 378 390 * @return mixed Array with col values or false on failure 379 * @access public 380 */ 381 function fetch_assoc($res_id=null) 391 */ 392 public function fetch_assoc($res_id = null) 382 393 { 383 394 $result = $this->_get_result($res_id); … … 391 402 * 392 403 * @param number $res_id Optional query handle identifier 404 * 393 405 * @return mixed Array with col values or false on failure 394 * @access public 395 */ 396 function fetch_array($res_id=null) 406 */ 407 public function fetch_array($res_id = null) 397 408 { 398 409 $result = $this->_get_result($res_id); … … 406 417 * @param MDB2_Result_Common Query $result result handle 407 418 * @param number $mode Fetch mode identifier 408 * @return mixed Array with col values or false on failure409 * @ access private419 * 420 * @return mixed Array with col values or false on failure 410 421 */ 411 422 private function _fetch_row($result, $mode) 412 423 { 413 if ($result === false || PEAR::isError($result) || !$this->is_connected()) 424 if ($result === false || PEAR::isError($result) || !$this->is_connected()) { 414 425 return false; 426 } 415 427 416 428 return $result->fetchRow($mode); … … 422 434 * 423 435 * @return array List of all tables of the current database 424 * @access public425 436 * @since 0.4-beta 426 437 */ 427 function list_tables()438 public function list_tables() 428 439 { 429 440 // get tables if not cached 430 441 if (!$this->tables) { 431 442 $this->db_handle->loadModule('Manager'); 432 if (!PEAR::isError($result = $this->db_handle->listTables())) 443 if (!PEAR::isError($result = $this->db_handle->listTables())) { 433 444 $this->tables = $result; 434 else 445 } 446 else { 435 447 $this->tables = array(); 448 } 436 449 } 437 450 … … 444 457 * 445 458 * @param string Table name 459 * 446 460 * @return array List of table cols 447 461 */ 448 function list_cols($table)462 public function list_cols($table) 449 463 { 450 464 $this->db_handle->loadModule('Manager'); … … 462 476 * @param mixed $input Value to quote 463 477 * @param string $type Type of data 478 * 464 479 * @return string Quoted/converted string for use in query 465 * @access public 466 */ 467 function quote($input, $type = null) 480 */ 481 public function quote($input, $type = null) 468 482 { 469 483 // handle int directly for better performance 470 if ($type == 'integer') 484 if ($type == 'integer') { 471 485 return intval($input); 486 } 472 487 473 488 // create DB handle if not available 474 if (!$this->db_handle) 489 if (!$this->db_handle) { 475 490 $this->db_connect('r'); 491 } 476 492 477 493 return $this->db_connected ? $this->db_handle->quote($input, $type) : addslashes($input); … … 483 499 * 484 500 * @param string $str Value to quote 501 * 485 502 * @return string Quoted string for use in query 486 503 * @deprecated Replaced by rcube_MDB2::quote_identifier 487 504 * @see rcube_mdb2::quote_identifier 488 * @access public 489 */ 490 function quoteIdentifier($str) 505 */ 506 public function quoteIdentifier($str) 491 507 { 492 508 return $this->quote_identifier($str); … … 498 514 * 499 515 * @param string $str Value to quote 516 * 500 517 * @return string Quoted string for use in query 501 * @access public 502 */ 503 function quote_identifier($str) 504 { 505 if (!$this->db_handle) 518 */ 519 public function quote_identifier($str) 520 { 521 if (!$this->db_handle) { 506 522 $this->db_connect('r'); 523 } 507 524 508 525 return $this->db_connected ? $this->db_handle->quoteIdentifier($str) : $str; … … 514 531 * 515 532 * @param string $str The string to be escaped 533 * 516 534 * @return string The escaped string 517 * @access public518 535 * @since 0.1.1 519 536 */ 520 function escapeSimple($str)521 { 522 if (!$this->db_handle) 537 public function escapeSimple($str) 538 { 539 if (!$this->db_handle) { 523 540 $this->db_connect('r'); 541 } 524 542 525 543 return $this->db_handle->escape($str); … … 531 549 * 532 550 * @return string SQL function to use in query 533 * @access public 534 */ 535 function now() 551 */ 552 public function now() 536 553 { 537 554 switch ($this->db_provider) { … … 551 568 * @param array $arr Input array 552 569 * @param string $type Type of data 570 * 553 571 * @return string Comma-separated list of quoted values for use in query 554 * @access public 555 */ 556 function array2list($arr, $type = null) 557 { 558 if (!is_array($arr)) 572 */ 573 public function array2list($arr, $type = null) 574 { 575 if (!is_array($arr)) { 559 576 return $this->quote($arr, $type); 560 561 foreach ($arr as $idx => $item) 577 } 578 579 foreach ($arr as $idx => $item) { 562 580 $arr[$idx] = $this->quote($item, $type); 581 } 563 582 564 583 return implode(',', $arr); … … 573 592 * 574 593 * @param string $field Field name 594 * 575 595 * @return string SQL statement to use in query 576 596 * @deprecated 577 597 */ 578 function unixtimestamp($field)598 public function unixtimestamp($field) 579 599 { 580 600 switch($this->db_provider) { … … 596 616 * 597 617 * @param string $timestamp Field name 618 * 598 619 * @return string SQL statement to use in query 599 * @access public 600 */ 601 function fromunixtime($timestamp) 620 */ 621 public function fromunixtime($timestamp) 602 622 { 603 623 return date("'Y-m-d H:i:s'", $timestamp); … … 610 630 * @param string $column Field name 611 631 * @param string $value Search value 632 * 612 633 * @return string SQL statement to use in query 613 * @access public 614 */ 615 function ilike($column, $value) 634 */ 635 public function ilike($column, $value) 616 636 { 617 637 // TODO: use MDB2's matchPattern() function 618 switch ($this->db_provider) {638 switch ($this->db_provider) { 619 639 case 'pgsql': 620 640 return $this->quote_identifier($column).' ILIKE '.$this->quote($value); … … 624 644 } 625 645 646 626 647 /** 627 648 * Abstract SQL statement for value concatenation 628 649 * 629 650 * @return string SQL statement to be used in query 630 * @access public 631 */ 632 function concat(/* col1, col2, ... */) 651 */ 652 public function concat(/* col1, col2, ... */) 633 653 { 634 654 $func = ''; 635 655 $args = func_get_args(); 636 656 637 switch ($this->db_provider) {657 switch ($this->db_provider) { 638 658 case 'mysql': 639 659 case 'mysqli': … … 657 677 * 658 678 * @param mixed $input Data to fix 679 * 659 680 * @return mixed Properly UTF-8 encoded data 660 * @access public 661 */ 662 function encode($input) 681 */ 682 public static function encode($input) 663 683 { 664 684 if (is_object($input)) { 665 foreach (get_object_vars($input) as $idx => $value) 666 $input->$idx = $this->encode($value); 685 foreach (get_object_vars($input) as $idx => $value) { 686 $input->$idx = self::encode($value); 687 } 667 688 return $input; 668 689 } 669 690 else if (is_array($input)) { 670 foreach ($input as $idx => $value) 671 $input[$idx] = $this->encode($value); 672 return $input; 691 foreach ($input as $idx => $value) { 692 $input[$idx] = self::encode($value); 693 } 694 return $input; 673 695 } 674 696 … … 681 703 * 682 704 * @param mixed $input Input data 705 * 683 706 * @return mixed Decoded data 684 * @access public 685 */ 686 function decode($input) 707 */ 708 public static function decode($input) 687 709 { 688 710 if (is_object($input)) { 689 foreach (get_object_vars($input) as $idx => $value) 690 $input->$idx = $this->decode($value); 711 foreach (get_object_vars($input) as $idx => $value) { 712 $input->$idx = self::decode($value); 713 } 691 714 return $input; 692 715 } 693 716 else if (is_array($input)) { 694 foreach ($input as $idx => $value) 695 $input[$idx] = $this->decode($value); 696 return $input; 717 foreach ($input as $idx => $value) { 718 $input[$idx] = self::decode($value); 719 } 720 return $input; 697 721 } 698 722 … … 705 729 * 706 730 * @param object $res Query handle 731 * 707 732 * @return mixed Handle ID 708 * @access private709 733 */ 710 734 private function _add_result($res) … … 733 757 * 734 758 * @param number $res_id Handle ID 759 * 735 760 * @return mixed Resource handle or false on failure 736 * @access private737 761 */ 738 762 private function _get_result($res_id = null) 739 763 { 740 if ($res_id == null) 764 if ($res_id == null) { 741 765 $res_id = $this->last_res_id; 742 743 if (isset($this->a_query_results[$res_id])) 744 if (!PEAR::isError($this->a_query_results[$res_id])) 766 } 767 768 if (isset($this->a_query_results[$res_id])) { 769 if (!PEAR::isError($this->a_query_results[$res_id])) { 745 770 return $this->a_query_results[$res_id]; 771 } 772 } 746 773 747 774 return false; … … 754 781 * @param MDB2 $dbh SQLite database handle 755 782 * @param string $file_name File path to use for DB creation 756 * @access private 757 */ 758 private function _sqlite_create_database($dbh, $file_name) 759 { 760 if (empty($file_name) || !is_string($file_name)) 783 */ 784 private function sqlite_create_database($dbh, $file_name) 785 { 786 if (empty($file_name) || !is_string($file_name)) { 761 787 return; 788 } 762 789 763 790 $data = file_get_contents($file_name); 764 791 765 if (strlen($data)) 766 if (!sqlite_exec($dbh->connection, $data, $error) || MDB2::isError($dbh)) 792 if (strlen($data)) { 793 if (!sqlite_exec($dbh->connection, $data, $error) || MDB2::isError($dbh)) { 767 794 raise_error(array('code' => 500, 'type' => 'db', 768 795 'line' => __LINE__, 'file' => __FILE__, 769 'message' => $error), true, false); 796 'message' => $error), true, false); 797 } 798 } 770 799 } 771 800 … … 774 803 * Add some proprietary database functions to the current SQLite handle 775 804 * in order to make it MySQL compatible 776 * 777 * @access private 778 */ 779 private function _sqlite_prepare() 780 { 781 include_once(INSTALL_PATH . 'program/include/rcube_sqlite.inc'); 782 783 // we emulate via callback some missing MySQL function 805 */ 806 private function sqlite_prepare() 807 { 808 // we emulate via callback some missing MySQL functions 784 809 sqlite_create_function($this->db_handle->connection, 785 ' from_unixtime', 'rcube_sqlite_from_unixtime');810 'unix_timestamp', array('rcube_mdb2', 'sqlite_unix_timestamp')); 786 811 sqlite_create_function($this->db_handle->connection, 787 'unix_timestamp', 'rcube_sqlite_unix_timestamp'); 788 sqlite_create_function($this->db_handle->connection, 789 'now', 'rcube_sqlite_now'); 790 sqlite_create_function($this->db_handle->connection, 791 'md5', 'rcube_sqlite_md5'); 812 'now', array('rcube_mdb2', 'sqlite_now')); 792 813 } 793 814 … … 850 871 } 851 872 873 874 /** 875 * Callback for sqlite: unix_timestamp() 876 */ 877 public static function sqlite_unix_timestamp($timestamp = '') 878 { 879 $timestamp = trim($timestamp); 880 if (!$timestamp) { 881 $ret = time(); 882 } 883 else if (!preg_match('/^[0-9]+$/s', $timestamp)) { 884 $ret = strtotime($timestamp); 885 } 886 else { 887 $ret = $timestamp; 888 } 889 890 return $ret; 891 } 892 893 894 /** 895 * Callback for sqlite: now() 896 */ 897 public static function sqlite_now() 898 { 899 return date("Y-m-d H:i:s"); 900 } 901 852 902 }
Note: See TracChangeset
for help on using the changeset viewer.
