1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973
|
<?php
/*
** Copyright (C) 2001-2025 Zabbix SIA
**
** This program is free software: you can redistribute it and/or modify it under the terms of
** the GNU Affero General Public License as published by the Free Software Foundation, version 3.
**
** This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
** without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
** See the GNU Affero General Public License for more details.
**
** You should have received a copy of the GNU Affero General Public License along with this program.
** If not, see <https://www.gnu.org/licenses/>.
**/
/**
* Creates global database connection.
*
* @param string $error returns a message in case of an error
* @param bool $debug turns On or Off trace calls when making connections. Suggested debug mode Off during Zabbix setup
*
* @return bool
*/
function DBconnect(&$error) {
global $DB;
if (isset($DB['DB'])) {
$error = _('Cannot create another database connection.');
return false;
}
$DB['DB'] = null; // global db handler
$DB['TRANSACTIONS'] = 0; // level of a nested transaction
$DB['TRANSACTION_NO_FAILED_SQLS'] = true; // true - if no statements failed in transaction, false - there are failed statements
$DB['SELECT_COUNT'] = 0; // stats
$DB['EXECUTE_COUNT'] = 0;
if (!isset($DB['TYPE'])) {
$error = 'Unknown database type.';
return false;
}
$db_types = [
ZBX_DB_MYSQL => MysqlDbBackend::class,
ZBX_DB_POSTGRESQL => PostgresqlDbBackend::class,
ZBX_DB_ORACLE => OracleDbBackend::class
];
if (!array_key_exists($DB['TYPE'], $db_types)) {
$error = 'Unsupported database';
return false;
}
$db = new $db_types[$DB['TYPE']];
if ($DB['ENCRYPTION']) {
$db->setConnectionSecurity($DB['KEY_FILE'], $DB['CERT_FILE'], $DB['CA_FILE'], $DB['VERIFY_HOST'],
$DB['CIPHER_LIST']
);
}
$DB['DB'] = $db->connect($DB['SERVER'], $DB['PORT'], $DB['USER'], $DB['PASSWORD'], $DB['DATABASE'], $DB['SCHEMA']);
if ($DB['DB']) {
$db->init();
}
if ($db->getError() || ($DB['ENCRYPTION'] && !$db->isConnectionSecure()) || !$db->checkDbVersion()
|| !$db->checkConfig()) {
$error = $db->getError();
return false;
}
return true;
}
function DBclose() {
global $DB;
$result = false;
if (isset($DB['DB']) && !empty($DB['DB'])) {
switch ($DB['TYPE']) {
case ZBX_DB_MYSQL:
$result = mysqli_close($DB['DB']);
break;
case ZBX_DB_POSTGRESQL:
$result = pg_close($DB['DB']);
break;
case ZBX_DB_ORACLE:
$result = oci_close($DB['DB']);
break;
}
}
unset($DB['DB']);
return $result;
}
function DBstart() {
global $DB;
$result = false;
if ($DB['TRANSACTIONS'] != 0) {
info('POSSIBLE ERROR: Used incorrect logic in database processing, started subtransaction!');
return $result;
}
$DB['TRANSACTIONS']++;
$DB['TRANSACTION_NO_FAILED_SQLS'] = true;
if (!isset($DB['DB']) || empty($DB['DB'])) {
return $result;
}
switch ($DB['TYPE']) {
case ZBX_DB_MYSQL:
$result = DBexecute('BEGIN');
break;
case ZBX_DB_POSTGRESQL:
$result = DBexecute('BEGIN');
break;
case ZBX_DB_ORACLE:
$result = true;
break;
}
return $result;
}
/**
* Closes transaction.
*
* @param string $doCommit True - do commit, rollback otherwise. Rollback is also always performed if a sql failed within this transaction.
*
* @return bool True - successful commit, False - otherwise
*/
function DBend($doCommit = true) {
global $DB;
$result = false;
if (!isset($DB['DB']) || empty($DB['DB'])) {
return $result;
}
if ($DB['TRANSACTIONS'] == 0) {
info('POSSIBLE ERROR: Used incorrect logic in database processing, transaction not started!');
return $result;
}
$DBresult = $doCommit && $DB['TRANSACTION_NO_FAILED_SQLS'];
if ($DBresult) {
$DBresult = DBcommit();
}
else {
DBrollback();
}
$DB['TRANSACTIONS'] = 0;
return (!is_null($doCommit) && $DBresult) ? $doCommit : $DBresult;
}
function DBcommit() {
global $DB;
$result = false;
switch ($DB['TYPE']) {
case ZBX_DB_MYSQL:
$result = DBexecute('COMMIT');
break;
case ZBX_DB_POSTGRESQL:
$result = DBexecute('COMMIT');
break;
case ZBX_DB_ORACLE:
$result = oci_commit($DB['DB']);
break;
}
return $result;
}
function DBrollback() {
global $DB;
$result = false;
switch ($DB['TYPE']) {
case ZBX_DB_MYSQL:
$result = DBexecute('ROLLBACK');
break;
case ZBX_DB_POSTGRESQL:
$result = DBexecute('ROLLBACK');
break;
case ZBX_DB_ORACLE:
$result = oci_rollback($DB['DB']);
break;
}
return $result;
}
/**
* Select data from DB. Use function DBexecute for non-selects.
*
* Example:
* DBselect('select * from users')
* DBselect('select * from users',50,200)
*
* @param string $query
* @param int $limit max number of record to return
* @param int $offset return starting from $offset record
*
* @return resource|false
*/
function DBselect($query, $limit = null, $offset = 0) {
global $DB;
if (!array_key_exists('DB', $DB) || $DB['DB'] === null) {
return false;
}
$query = DBaddLimit($query, $limit, $offset);
if ($query === false) {
return false;
}
$time_start = microtime(true);
$DB['SELECT_COUNT']++;
$result = false;
switch ($DB['TYPE']) {
case ZBX_DB_MYSQL:
try {
$result = mysqli_query($DB['DB'], $query);
}
catch (mysqli_sql_exception $e) {
trigger_error('Error in query ['.$query.'] ['.$e->getMessage().']', E_USER_WARNING);
}
break;
case ZBX_DB_POSTGRESQL:
if (!$result = pg_query($DB['DB'], $query)) {
trigger_error('Error in query ['.$query.'] ['.pg_last_error($DB['DB']).']', E_USER_WARNING);
}
break;
case ZBX_DB_ORACLE:
$result = oci_parse($DB['DB'], $query);
if ($result === false) {
$e = oci_error();
trigger_error('SQL error ['.$e['message'].'] in ['.$e['sqltext'].']', E_USER_WARNING);
break;
}
if (!@oci_execute($result, ($DB['TRANSACTIONS'] ? OCI_DEFAULT : OCI_COMMIT_ON_SUCCESS))) {
$e = oci_error($result);
trigger_error('SQL error ['.$e['message'].'] in ['.$e['sqltext'].']', E_USER_WARNING);
}
break;
}
if (!$result) {
$DB['TRANSACTION_NO_FAILED_SQLS'] = false;
}
if (CApiService::$userData !== null && array_key_exists('debug_mode', CApiService::$userData)
&& CApiService::$userData['debug_mode'] == GROUP_DEBUG_MODE_ENABLED) {
CProfiler::getInstance()->profileSql(microtime(true) - $time_start, $query);
}
return $result;
}
/**
* Add the LIMIT clause to the given query.
*
* NOTE:
* LIMIT and OFFSET records
*
* Example: select 6-15 row.
*
* MySQL:
* SELECT a FROM tbl LIMIT 5,10
* SELECT a FROM tbl LIMIT 10 OFFSET 5
*
* PostgreSQL:
* SELECT a FROM tbl LIMIT 10 OFFSET 5
*
* Oracle:
* SELECT a FROM tbl WHERE rownum < 15 // ONLY < 15
* SELECT * FROM (SELECT * FROM tbl) WHERE rownum BETWEEN 6 AND 15
*
* @param $query
* @param int $limit max number of record to return
* @param int $offset return starting from $offset record
*
* @return bool|string
*/
function DBaddLimit($query, $limit = 0, $offset = 0) {
global $DB;
if ((isset($limit) && ($limit < 0 || !zbx_ctype_digit($limit))) || $offset < 0 || !zbx_ctype_digit($offset)) {
$moreDetails = isset($limit) ? ' Limit ['.$limit.'] Offset ['.$offset.']' : ' Offset ['.$offset.']';
error('Incorrect parameters for limit and/or offset. Query ['.$query.']'.$moreDetails, true);
return false;
}
// Process limit and offset
if (isset($limit)) {
switch ($DB['TYPE']) {
case ZBX_DB_MYSQL:
case ZBX_DB_POSTGRESQL:
$query .= ' LIMIT '.intval($limit);
$query .= $offset != 0 ? ' OFFSET '.intval($offset) : '';
break;
case ZBX_DB_ORACLE:
$till = $offset + $limit;
$query = 'SELECT * FROM ('.$query.') WHERE rownum BETWEEN '.intval($offset).' AND '.intval($till);
break;
}
}
return $query;
}
/**
* @param $query
*
* @return bool
*/
function DBexecute($query): bool {
global $DB;
if (!array_key_exists('DB', $DB) || $DB['DB'] === null) {
return false;
}
$time_start = microtime(true);
$DB['EXECUTE_COUNT']++;
$result = false;
switch ($DB['TYPE']) {
case ZBX_DB_MYSQL:
try {
$result = mysqli_query($DB['DB'], $query);
}
catch (mysqli_sql_exception $e) {
trigger_error('Error in query ['.$query.'] ['.$e->getMessage().']', E_USER_WARNING);
}
break;
case ZBX_DB_POSTGRESQL:
if (!$result = (bool) pg_query($DB['DB'], $query)) {
error('Error in query ['.$query.'] ['.pg_last_error($DB['DB']).']', true);
}
break;
case ZBX_DB_ORACLE:
$handle = oci_parse($DB['DB'], $query);
$result = $handle && @oci_execute($handle, $DB['TRANSACTIONS'] ? OCI_DEFAULT : OCI_COMMIT_ON_SUCCESS);
if (!$result) {
$error = $handle ? oci_error($handle) : oci_error();
error('SQL error ['.$error['message'].'] in ['.$error['sqltext'].']', true);
}
break;
}
if (!$result) {
$DB['TRANSACTION_NO_FAILED_SQLS'] = false;
}
if (CApiService::$userData !== null && array_key_exists('debug_mode', CApiService::$userData)
&& CApiService::$userData['debug_mode'] == GROUP_DEBUG_MODE_ENABLED) {
CProfiler::getInstance()->profileSql(microtime(true) - $time_start, $query);
}
return (bool) $result;
}
/**
* Return the next data set from a DB resource or false if there are no more results.
*
* @param mixed $cursor A DB-specific resource returned by DBselect or DBexecute.
* @param bool $convertNulls Convert all null values to string zeros.
*
* @return array|bool
*/
function DBfetch($cursor, $convertNulls = true) {
global $DB;
if (!array_key_exists('DB', $DB) || $DB['DB'] === null || $cursor === false) {
return false;
}
$result = false;
switch ($DB['TYPE']) {
case ZBX_DB_MYSQL:
$result = mysqli_fetch_assoc($cursor);
if (!$result) {
mysqli_free_result($cursor);
}
break;
case ZBX_DB_POSTGRESQL:
if ($result = pg_fetch_assoc($cursor)) {
$i = 0;
foreach ($result as &$value) {
if (pg_field_type($cursor, $i++) === 'bytea') {
$value = pg_unescape_bytea($value);
}
}
unset($value);
}
else {
pg_free_result($cursor);
}
break;
case ZBX_DB_ORACLE:
if ($row = oci_fetch_assoc($cursor)) {
$result = [];
foreach ($row as $key => $value) {
$field_type = strtolower(oci_field_type($cursor, $key));
// Since Oracle reports nulls for empty strings, convert those back to empty strings.
$value = (str_in_array($field_type, ['varchar', 'varchar2', 'blob', 'clob']) && is_null($value))
? ''
: $value;
if (is_object($value) && (strpos($field_type, 'lob') !== false)) {
$value = $value->load();
}
$result[strtolower($key)] = $value;
}
}
break;
}
if ($result) {
if ($convertNulls) {
foreach ($result as $key => $val) {
if (is_null($val)) {
$result[$key] = '0';
}
}
}
return $result;
}
return false;
}
function zbx_sql_mod($x, $y) {
return 'MOD('.$x.','.$y.')';
}
function get_dbid($table, $field) {
// PGSQL on transaction failure on all queries returns false..
global $DB;
if ($DB['TYPE'] == ZBX_DB_POSTGRESQL && $DB['TRANSACTIONS'] && !$DB['TRANSACTION_NO_FAILED_SQLS']) {
return 0;
}
$found = false;
$min = 0;
$max = ZBX_DB_MAX_ID;
do {
$dbSelect = DBselect('SELECT i.nextid FROM ids i WHERE i.table_name='.zbx_dbstr($table).' AND i.field_name='.zbx_dbstr($field));
if (!$dbSelect) {
return false;
}
$row = DBfetch($dbSelect);
if (!$row) {
$row = DBfetch(DBselect('SELECT MAX('.$field.') AS id FROM '.$table.' WHERE '.$field.' BETWEEN '.$min.' AND '.$max));
if (!$row || ($row['id'] == 0)) {
DBexecute("INSERT INTO ids (table_name,field_name,nextid) VALUES ('$table','$field',$min)");
}
else {
DBexecute("INSERT INTO ids (table_name,field_name,nextid) VALUES ('$table','$field',".$row['id'].')');
}
continue;
}
else {
$ret1 = $row['nextid'];
if (bccomp($ret1, $min) < 0 || !bccomp($ret1, $max) < 0) {
DBexecute('DELETE FROM ids WHERE table_name='.zbx_dbstr($table).' AND field_name='.zbx_dbstr($field));
continue;
}
$sql = 'UPDATE ids SET nextid=nextid+1 WHERE table_name='.zbx_dbstr($table).' AND field_name='.zbx_dbstr($field);
DBexecute($sql);
$row = DBfetch(DBselect('SELECT i.nextid FROM ids i WHERE i.table_name='.zbx_dbstr($table).' AND i.field_name='.zbx_dbstr($field)));
if (!$row || is_null($row['nextid'])) {
// should never be here
continue;
}
else {
$ret2 = $row['nextid'];
if (bccomp(bcadd($ret1, 1, 0), $ret2, 0) == 0) {
$found = true;
}
}
}
}
while (false == $found);
return $ret2;
}
function zbx_db_search($table, $options, &$sql_parts) {
global $DB;
list($table, $tableShort) = explode(' ', $table);
$tableSchema = DB::getSchema($table);
if (!$tableSchema) {
info(_s('Error in search request for table "%1$s".', $table));
}
$start = $options['startSearch'] ? '' : '%';
$exclude = $options['excludeSearch'] ? ' NOT' : '';
$glue = $options['searchByAny'] ? ' OR ' : ' AND ';
$search = [];
foreach ($options['search'] as $field => $patterns) {
if ($patterns === null || !array_key_exists($field, $tableSchema['fields'])
|| ($tableSchema['fields'][$field]['type'] & DB::SUPPORTED_SEARCH_TYPES) == 0) {
continue;
}
$patterns = array_filter((array)$patterns, function($pattern) {
return ($pattern !== '');
});
if (!$patterns) {
continue;
}
$fieldSearch = [];
foreach ($patterns as $pattern) {
// escaping parameter that is about to be used in LIKE statement
$pattern = mb_strtoupper(strtr($pattern, ['!' => '!!', '%' => '!%', '_' => '!_']));
$pattern = !$options['searchWildcardsEnabled']
? $start.$pattern.'%'
: str_replace('*', '%', $pattern);
if ($DB['TYPE'] == ZBX_DB_ORACLE && $tableSchema['fields'][$field]['type'] & DB::FIELD_TYPE_NCLOB
&& strlen($pattern) > ORACLE_MAX_STRING_SIZE) {
$chunks = zbx_dbstr(DB::chunkMultibyteStr($pattern, ORACLE_MAX_STRING_SIZE));
$pattern = 'TO_NCLOB('.implode(') || TO_NCLOB(', $chunks).')';
}
else {
$pattern = zbx_dbstr($pattern);
}
$fieldSearch[] = DB::uppercaseField($field, $table, $tableShort).$exclude.' LIKE '.$pattern." ESCAPE '!'";
}
$search[$field] = '('.implode($glue, $fieldSearch).')';
}
if ($search) {
if (isset($sql_parts['where']['search'])) {
$search[] = $sql_parts['where']['search'];
}
$sql_parts['where']['search'] = '('.implode($glue, $search).')';
return true;
}
return false;
}
/**
* Checks whether all $db_fields keys exists as $args keys.
*
* If $db_fields element value is given and corresponding $args is not then it is assigned to $args element.
*
* @param $dbFields
* @param $args
*
* @return bool
*/
function check_db_fields($dbFields, &$args) {
if (!is_array($args)) {
return false;
}
foreach ($dbFields as $field => $def) {
if (!isset($args[$field])) {
if (is_null($def)) {
return false;
}
else {
$args[$field] = $def;
}
}
}
return true;
}
/**
* Create condition SQL for field matching against numeric values.
*
* @param string $field_name
* @param array $values
* @param bool $not_in Create inverse condition.
* @param bool $zero_to_null Cast zero to null.
*
* @return string
*/
function dbConditionInt($field_name, array $values, $not_in = false, $zero_to_null = false) {
global $DB;
$MIN_NUM_BETWEEN = 4; // Minimum number of consecutive values for using "BETWEEN <id1> AND <idN>".
$MAX_NUM_IN = 950; // Maximum number of values for using "IN (<id1>,<id2>,...,<idN>)".
if (is_bool(reset($values))) {
return $not_in ? '1=1' : '1=0';
}
$values = array_flip($values);
$has_zero = false;
if ($zero_to_null && array_key_exists(0, $values)) {
$has_zero = true;
unset($values[0]);
}
$values = array_keys($values);
natsort($values);
$values = array_values($values);
$intervals = [];
$singles = [];
if ($DB['TYPE'] == ZBX_DB_ORACLE) {
// For better performance, use "BETWEEN" constructs for sequential integer values, for Oracle database.
for ($i = 0, $size = count($values); $i < $size; $i++) {
if ($i + $MIN_NUM_BETWEEN < $size && bcsub($values[$i + $MIN_NUM_BETWEEN], $values[$i]) == $MIN_NUM_BETWEEN) {
$interval_first = $values[$i];
// Search for the last sequential integer value.
for ($i += $MIN_NUM_BETWEEN; $i < $size && bcsub($values[$i], $values[$i - 1]) == 1; $i++);
$i--;
$interval_last = $values[$i];
// Save the first and last values of the sequential interval.
$intervals[] = [dbQuoteInt($interval_first), dbQuoteInt($interval_last)];
}
else {
$singles[] = dbQuoteInt($values[$i]);
}
}
}
else {
// For better performance, use only "IN" constructs all other databases, except Oracle.
$singles = array_map(function($value) {
return dbQuoteInt($value);
}, $values);
}
$condition = '';
// Process intervals.
foreach ($intervals as $interval) {
if ($condition !== '') {
$condition .= $not_in ? ' AND ' : ' OR ';
}
$condition .= ($not_in ? 'NOT ' : '').$field_name.' BETWEEN '.$interval[0].' AND '.$interval[1];
}
// Process individual values.
$single_chunks = array_chunk($singles, $MAX_NUM_IN);
foreach ($single_chunks as $chunk) {
if ($condition !== '') {
$condition .= $not_in ? ' AND ' : ' OR ';
}
if (count($chunk) == 1) {
$condition .= $field_name.($not_in ? '!=' : '=').$chunk[0];
}
else {
$condition .= $field_name.($not_in ? ' NOT' : '').' IN ('.implode(',', $chunk).')';
}
}
if ($has_zero) {
if ($condition !== '') {
$condition .= $not_in ? ' AND ' : ' OR ';
}
$condition .= $field_name.($not_in ? ' IS NOT NULL' : ' IS NULL');
}
if (!$not_in) {
if ((int) $has_zero + count($intervals) + count($single_chunks) > 1) {
$condition = '('.$condition.')';
}
}
return $condition;
}
/**
* Takes an initial part of SQL query and appends a generated WHERE condition.
*
* @param string $fieldName field name to be used in SQL WHERE condition
* @param array $values array of numerical values sorted in ascending order to be included in WHERE
* @param bool $notIn builds inverted condition
*
* @return string
*/
function dbConditionId($fieldName, array $values, $notIn = false) {
return dbConditionInt($fieldName, $values, $notIn, true);
}
/**
* Takes an initial part of SQL query and appends a generated WHERE condition.
*
* @param string $fieldName field name to be used in SQL WHERE condition
* @param array $values array of string values sorted in ascending order to be included in WHERE
* @param bool $notIn builds inverted condition
*
* @return string
*/
function dbConditionString($fieldName, array $values, $notIn = false) {
switch (count($values)) {
case 0:
return '1=0';
case 1:
return $notIn
? $fieldName.'!='.zbx_dbstr(reset($values))
: $fieldName.'='.zbx_dbstr(reset($values));
}
$in = $notIn ? ' NOT IN ' : ' IN ';
$concat = $notIn ? ' AND ' : ' OR ';
$items = array_chunk($values, 950);
$condition = '';
foreach ($items as $values) {
$condition .= !empty($condition) ? ')'.$concat.$fieldName.$in.'(' : '';
$condition .= implode(',', zbx_dbstr($values));
}
return '('.$fieldName.$in.'('.$condition.'))';
}
/**
* Quote a value if not an integer or out of BC Math bounds.
*
* @param mixed $value Either the original or quoted value.
*/
function dbQuoteInt($value) {
if (!ctype_digit((string) $value) || bccomp($value, ZBX_MAX_UINT64) > 0) {
$value = zbx_dbstr($value);
}
return $value;
}
/**
* Return SQL for COALESCE like select. For fields with type NCHAR, NVARCHAR or NTEXT in Oracle NVL should be used
* instead of COALESCE because it will not check that all arguments have same type.
*
* @param string $field_name Field name to be used in returned query part.
* @param int|string $default_value Default value to be returned.
* @param string $alias Alias to be used in 'AS' query part.
* @return string
*/
function dbConditionCoalesce($field_name, $default_value, $alias = '') {
global $DB;
if (is_string($default_value)) {
$default_value = ($default_value == '') ? '\'\'' : zbx_dbstr($default_value);
}
$query = (($DB['TYPE'] == ZBX_DB_ORACLE) ? 'NVL(' : 'COALESCE(').$field_name.','.$default_value.')';
if ($alias) {
$query .= ' AS '.$alias;
}
return $query;
}
/**
* Transform DB cursor to array.
*
* @return array
*/
function DBfetchArray($cursor) {
$result = [];
while ($row = DBfetch($cursor)) {
$result[] = $row;
}
return $result;
}
/**
* Transform DB cursor to array.
*
* @return array
*/
function DBfetchArrayAssoc($cursor, $field) {
$result = [];
while ($row = DBfetch($cursor)) {
$result[$row[$field]] = $row;
}
return $result;
}
/**
* Fetch only values from one column to array.
*
* @param resource $cursor
* @param string $column
*
* @return array
*/
function DBfetchColumn($cursor, $column) {
$result = [];
while ($dbResult = DBfetch($cursor)) {
$result[] = $dbResult[$column];
}
return $result;
}
/**
* Returns true if both IDs are equal.
*
* @param $id1
* @param $id2
*
* @return bool
*/
function idcmp($id1, $id2) {
return (string) $id1 === (string) $id2;
}
/**
* Escapes the value to be used in the PostgreSQL connection string for the pg_connect() function.
*
* @param $string
*
* @return string
*/
function pg_connect_escape($string) {
return addcslashes($string, "'\\");
}
/**
* Escape string for safe usage in SQL queries.
* Works for mysql, oracle, postgresql.
*
* @param array|string $var
*
* @return array|bool|string
*/
function zbx_dbstr($var) {
global $DB;
if (!isset($DB['TYPE'])) {
return false;
}
switch ($DB['TYPE']) {
case ZBX_DB_MYSQL:
if (is_array($var)) {
foreach ($var as $vnum => $value) {
$var[$vnum] = "'".mysqli_real_escape_string($DB['DB'], $value)."'";
}
return $var;
}
return "'".mysqli_real_escape_string($DB['DB'], $var)."'";
case ZBX_DB_ORACLE:
if (is_array($var)) {
foreach ($var as $vnum => $value) {
$var[$vnum] = "'".preg_replace('/\'/', '\'\'', $value)."'";
}
return $var;
}
return "'".preg_replace('/\'/','\'\'',$var)."'";
case ZBX_DB_POSTGRESQL:
if (is_array($var)) {
foreach ($var as $vnum => $value) {
$var[$vnum] = "'".pg_escape_string($DB['DB'], $value)."'";
}
return $var;
}
return "'".pg_escape_string($DB['DB'], $var)."'";
default:
return false;
}
}
/**
* Creates db dependent string with sql expression that casts passed value to bigint.
* Works for mysql, oracle, postgresql.
*
* @param int $field
*
* @return bool|string
*/
function zbx_dbcast_2bigint($field) {
global $DB;
if (!isset($DB['TYPE'])) {
return false;
}
switch ($DB['TYPE']) {
case ZBX_DB_POSTGRESQL:
return 'CAST('.$field.' AS BIGINT)';
case ZBX_DB_MYSQL:
return 'CAST('.$field.' AS UNSIGNED)';
case ZBX_DB_ORACLE:
return 'CAST('.$field.' AS NUMBER(20))';
default:
return false;
}
}
|