Source
* @param string $doCommit True - do commit, rollback otherwise. Rollback is also always performed if a sql failed within this transaction.
<?php
/*
** Zabbix
** Copyright (C) 2001-2023 Zabbix SIA
**
** This program is free software; you can redistribute it and/or modify
** it under the terms of the GNU General Public License as published by
** the Free Software Foundation; either version 2 of the License, or
** (at your option) any later version.
**
** 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 General Public License for more details.
**
** You should have received a copy of the GNU General Public License
** along with this program; if not, write to the Free Software
** Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
**/
/**
* 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: