Changeset 27337
- Timestamp:
- Feb 11, 2014, 11:12:57 PM (10 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/include/dblayer/functions_mysqli.inc.php
r27336 r27337 22 22 // +-----------------------------------------------------------------------+ 23 23 24 /** 25 * @package functions\mysql 26 */ 27 24 28 define('DB_ENGINE', 'MySQL'); 25 29 define('REQUIRED_MYSQL_VERSION', '5.0.0'); … … 28 32 define('DB_RANDOM_FUNCTION', 'RAND'); 29 33 30 /** 31 * 32 * simple functions 33 * 34 */ 35 34 35 /** 36 * Connect to database and store MySQLi resource in __$mysqli__ global variable. 37 * 38 * @param string $host 39 * - localhost 40 * - 1.2.3.4:3405 41 * - /path/to/socket 42 * @param string $user 43 * @param string $password 44 * @param string $database 45 * 46 * @throws Exception 47 */ 36 48 function pwg_db_connect($host, $user, $password, $database) 37 49 { 38 50 global $mysqli; 39 40 // $host can be:41 //42 // $host = localhost43 // $host = 1.2.3.4:340544 // $host = /path/to/socket45 51 46 52 $port = null; … … 70 76 } 71 77 78 /** 79 * Set charset for database connection. 80 */ 72 81 function pwg_db_check_charset() 73 82 { … … 80 89 } 81 90 91 /** 92 * Check MySQL version. Can call fatal_error(). 93 */ 82 94 function pwg_db_check_version() 83 95 { … … 95 107 } 96 108 109 /** 110 * Get Mysql Version. 111 * 112 * @return string 113 */ 97 114 function pwg_get_db_version() 98 115 { … … 102 119 } 103 120 121 /** 122 * Execute a query 123 * 124 * @param string $query 125 * @return mysqli_result|bool 126 */ 104 127 function pwg_query($query) 105 128 { … … 150 173 } 151 174 175 /** 176 * Get max value plus one of a particular column. 177 * 178 * @param string $column 179 * @param string $table 180 * @param int 181 */ 152 182 function pwg_db_nextval($column, $table) 153 183 { … … 232 262 } 233 263 234 /**235 *236 * complex functions237 *238 */239 264 240 265 define('MASS_UPDATES_SKIP_EMPTY', 1); 241 /** 242 * updates multiple lines in a table 243 * 244 * @param string table_name245 * @param array dbfields246 * @param array datas247 * @param int flags - if MASS_UPDATES_SKIP_EMPTY - empty values do not overwrite existing ones248 * @ return void266 267 /** 268 * Updates multiple lines in a table. 269 * 270 * @param string $tablename 271 * @param array $dbfields - contains 'primary' and 'update' arrays 272 * @param array $datas - indexed by column names 273 * @param int $flags - if MASS_UPDATES_SKIP_EMPTY, empty values do not overwrite existing ones 249 274 */ 250 275 function mass_updates($tablename, $dbfields, $datas, $flags=0) 251 276 { 252 277 if (count($datas) == 0) 278 { 253 279 return; 254 255 // depending on the MySQL version, we use the multi table update or N update queries 280 } 281 282 // we use the multi table update or N update queries 256 283 if (count($datas) < 10) 257 284 { 258 285 foreach ($datas as $data) 259 286 { 287 $is_first = true; 288 260 289 $query = ' 261 290 UPDATE '.$tablename.' 262 291 SET '; 263 $is_first = true; 292 264 293 foreach ($dbfields['update'] as $key) 265 294 { … … 272 301 else 273 302 { 274 if ( $flags & MASS_UPDATES_SKIP_EMPTY ) 303 if ($flags & MASS_UPDATES_SKIP_EMPTY) 304 { 275 305 continue; // next field 306 } 276 307 $query.= "$separator$key = NULL"; 277 308 } 278 309 $is_first = false; 279 310 } 311 280 312 if (!$is_first) 281 313 {// only if one field at least updated 314 $is_first = true; 315 282 316 $query.= ' 283 317 WHERE '; 284 $is_first = true;285 318 foreach ($dbfields['primary'] as $key) 286 319 { … … 289 322 $query.= ' AND '; 290 323 } 291 if ( isset($data[$key]))324 if (isset($data[$key])) 292 325 { 293 326 $query.= $key.' = \''.$data[$key].'\''; … … 299 332 $is_first = false; 300 333 } 334 301 335 pwg_query($query); 302 336 } 303 337 } // foreach update 304 } // if mysqli_ver orcount<X338 } // if count<X 305 339 else 306 340 { 307 341 // creation of the temporary table 308 $query = ' 309 SHOW FULL COLUMNS FROM '.$tablename; 310 $result = pwg_query($query); 342 $result = pwg_query('SHOW FULL COLUMNS FROM '.$tablename); 311 343 $columns = array(); 312 344 $all_fields = array_merge($dbfields['primary'], $dbfields['update']); 345 313 346 while ($row = pwg_db_fetch_assoc($result)) 314 347 { … … 351 384 pwg_query($query); 352 385 mass_inserts($temporary_tablename, $all_fields, $datas); 353 if ( $flags & MASS_UPDATES_SKIP_EMPTY ) 386 387 if ($flags & MASS_UPDATES_SKIP_EMPTY) 354 388 $func_set = create_function('$s', 'return "t1.$s = IFNULL(t2.$s, t1.$s)";'); 355 389 else 356 390 $func_set = create_function('$s', 'return "t1.$s = t2.$s";'); 357 391 358 // update of imagestable by joining with temporary table392 // update of table by joining with temporary table 359 393 $query = ' 360 394 UPDATE '.$tablename.' AS t1, '.$temporary_tablename.' AS t2 … … 373 407 ); 374 408 pwg_query($query); 375 $query = ' 376 DROP TABLE '.$temporary_tablename; 377 pwg_query($query); 378 } 379 } 380 381 /** 382 * updates one line in a table 383 * 384 * @param string table_name 385 * @param array set_fields 386 * @param array where_fields 387 * @param int flags - if MASS_UPDATES_SKIP_EMPTY - empty values do not overwrite existing ones 388 * @return void 389 */ 390 function single_update($tablename, $set_fields, $where_fields, $flags=0) 391 { 392 if (count($set_fields) == 0) 409 410 pwg_query('DROP TABLE '.$temporary_tablename); 411 } 412 } 413 414 /** 415 * Updates one line in a table. 416 * 417 * @param string $tablename 418 * @param array $datas 419 * @param array $where 420 * @param int $flags - if MASS_UPDATES_SKIP_EMPTY, empty values do not overwrite existing ones 421 */ 422 function single_update($tablename, $datas, $where, $flags=0) 423 { 424 if (count($datas) == 0) 393 425 { 394 426 return; 395 427 } 428 429 $is_first = true; 396 430 397 431 $query = ' 398 432 UPDATE '.$tablename.' 399 433 SET '; 400 $is_first = true; 401 foreach ($ set_fields as $key => $value)434 435 foreach ($datas as $key => $value) 402 436 { 403 437 $separator = $is_first ? '' : ",\n "; … … 409 443 else 410 444 { 411 if ( $flags & MASS_UPDATES_SKIP_EMPTY ) 445 if ($flags & MASS_UPDATES_SKIP_EMPTY) 446 { 412 447 continue; // next field 448 } 413 449 $query.= "$separator$key = NULL"; 414 450 } 415 451 $is_first = false; 416 452 } 453 417 454 if (!$is_first) 418 455 {// only if one field at least updated 456 $is_first = true; 457 419 458 $query.= ' 420 459 WHERE '; 421 $is_first = true; 422 foreach ($where _fieldsas $key => $value)460 461 foreach ($where as $key => $value) 423 462 { 424 463 if (!$is_first) … … 426 465 $query.= ' AND '; 427 466 } 428 if ( isset($value))467 if (isset($value)) 429 468 { 430 469 $query.= $key.' = \''.$value.'\''; … … 436 475 $is_first = false; 437 476 } 477 438 478 pwg_query($query); 439 479 } 440 480 } 441 481 442 443 /** 444 * inserts multiple lines in a table445 * 446 * @param string table_name447 * @param array dbfields448 * @param array inserts449 * @return void482 /** 483 * Inserts multiple lines in a table. 484 * 485 * @param string $table_name 486 * @param array $dbfields - fields from $datas which will be used 487 * @param array $datas 488 * @param array $options 489 * - boolean ignore - use "INSERT IGNORE" 450 490 */ 451 491 function mass_inserts($table_name, $dbfields, $datas, $options=array()) … … 507 547 $query .= ')'; 508 548 } 549 509 550 pwg_query($query); 510 551 } … … 512 553 513 554 /** 514 * inserts one line in a table 515 * 516 * @param string table_name 517 * @param array dbfields 518 * @param array insert 519 * @return void 555 * Inserts one line in a table. 556 * 557 * @param string $table_name 558 * @param array $data 520 559 */ 521 560 function single_insert($table_name, $data) … … 556 595 } 557 596 558 /** 559 * Do maintenance on all PWG tables 560 * 561 * @return none 597 598 /** 599 * Do maintenance on all Piwigo tables 562 600 */ 563 601 function do_maintenance_all_tables() … … 632 670 633 671 /** 634 * returns an array containing the possible values of an enum field 635 * 636 * @param string tablename 637 * @param string fieldname 672 * Returns an array containing the possible values of an enum field. 673 * 674 * @param string $table 675 * @param string $field 676 * @return string[] 638 677 */ 639 678 function get_enums($table, $field) 640 679 { 641 // retrieving the properties of the table. Each line represents a field : 642 // columns are 'Field', 'Type' 643 $result = pwg_query('desc '.$table); 680 $result = pwg_query('DESC '.$table); 644 681 while ($row = pwg_db_fetch_assoc($result)) 645 682 { 646 // we are only interested in the the field given in parameter for the647 // function648 683 if ($row['Field'] == $field) 649 684 { 650 // retrieving possible values of the enum field 651 // enum('blue','green','black') 685 // parse enum('blue','green','black') 652 686 $options = explode(',', substr($row['Type'], 5, -1)); 653 687 foreach ($options as $i => $option) … … 657 691 } 658 692 } 693 659 694 pwg_db_free_result($result); 660 695 return $options; … … 662 697 663 698 /** 664 * Smartly checks if a variable is equivalent to true or false665 * 666 * @param mixed input699 * Checks if a variable is equivalent to true or false. 700 * 701 * @param mixed $input 667 702 * @return bool 668 703 */ … … 678 713 679 714 /** 680 * returns boolean string 'true' or 'false' if the given var is boolean 715 * Returns string 'true' or 'false' if the given var is boolean. 716 * If the input is another type, it is not changed. 681 717 * 682 718 * @param mixed $var … … 694 730 } 695 731 } 696 697 /**698 *699 * interval and date functions700 *701 */702 732 703 733 function pwg_db_get_recent_period_expression($period, $date='CURRENT_DATE') … … 722 752 function pwg_db_get_flood_period_expression($seconds) 723 753 { 724 return 'SUBDATE( now(), INTERVAL '.$seconds.' SECOND)';754 return 'SUBDATE(NOW(), INTERVAL '.$seconds.' SECOND)'; 725 755 } 726 756 727 757 function pwg_db_get_hour($date) 728 758 { 729 return ' hour('.$date.')';759 return 'HOUR('.$date.')'; 730 760 } 731 761 … … 782 812 } 783 813 784 // my_error returns (or send to standard output) the message concerning the 785 // error occured for the last mysql query. 814 /** 815 * Returns (or send to standard output) the message concerning the 816 * error occured for the last mysql query. 817 */ 786 818 function my_error($header, $die) 787 819 {
Note: See TracChangeset
for help on using the changeset viewer.