2010-07-02 01:48:07 +02:00
|
|
|
<?php
|
2018-01-25 03:08:45 +01:00
|
|
|
|
2018-04-28 12:36:40 +02:00
|
|
|
use Friendica\App;
|
2018-01-25 03:08:45 +01:00
|
|
|
use Friendica\Core\L10n;
|
2017-12-14 22:13:02 +01:00
|
|
|
use Friendica\Core\System;
|
2017-11-08 04:57:46 +01:00
|
|
|
use Friendica\Database\DBM;
|
2017-12-14 22:13:02 +01:00
|
|
|
use Friendica\Database\DBStructure;
|
2018-01-27 03:38:34 +01:00
|
|
|
use Friendica\Util\DateTimeFormat;
|
2017-08-26 12:01:50 +02:00
|
|
|
|
2010-12-08 04:40:12 +01:00
|
|
|
/**
|
2015-12-25 23:17:34 +01:00
|
|
|
* @class MySQL database class
|
2010-12-08 04:40:12 +01:00
|
|
|
*
|
2017-04-25 18:05:26 +02:00
|
|
|
* This class is for the low level database stuff that does driver specific things.
|
2010-12-08 04:40:12 +01:00
|
|
|
*/
|
2014-09-07 17:28:38 +02:00
|
|
|
|
2012-04-12 15:50:11 +02:00
|
|
|
class dba {
|
2018-03-18 10:15:14 +01:00
|
|
|
public static $connected = false;
|
2017-10-11 14:56:36 +02:00
|
|
|
|
|
|
|
private static $_server_info = '';
|
|
|
|
private static $db;
|
|
|
|
private static $driver;
|
|
|
|
private static $error = false;
|
|
|
|
private static $errorno = 0;
|
|
|
|
private static $affected_rows = 0;
|
2017-05-11 22:19:43 +02:00
|
|
|
private static $in_transaction = false;
|
2018-01-15 14:05:12 +01:00
|
|
|
private static $relation = [];
|
2012-04-12 15:50:11 +02:00
|
|
|
|
2018-04-28 12:36:40 +02:00
|
|
|
public static function connect($serveraddr, $user, $pass, $db) {
|
2017-10-11 14:56:36 +02:00
|
|
|
if (!is_null(self::$db)) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2016-12-13 10:16:36 +01:00
|
|
|
$a = get_app();
|
2013-01-26 18:35:39 +01:00
|
|
|
|
|
|
|
$stamp1 = microtime(true);
|
2012-04-12 15:50:11 +02:00
|
|
|
|
2017-05-03 21:22:44 +02:00
|
|
|
$serveraddr = trim($serveraddr);
|
|
|
|
|
|
|
|
$serverdata = explode(':', $serveraddr);
|
|
|
|
$server = $serverdata[0];
|
|
|
|
|
|
|
|
if (count($serverdata) > 1) {
|
|
|
|
$port = trim($serverdata[1]);
|
|
|
|
}
|
|
|
|
|
2012-04-12 15:50:11 +02:00
|
|
|
$server = trim($server);
|
|
|
|
$user = trim($user);
|
|
|
|
$pass = trim($pass);
|
|
|
|
$db = trim($db);
|
|
|
|
|
2016-10-22 12:14:41 +02:00
|
|
|
if (!(strlen($server) && strlen($user))) {
|
2017-10-11 14:56:36 +02:00
|
|
|
return false;
|
2012-04-12 15:50:11 +02:00
|
|
|
}
|
2012-05-20 00:11:32 +02:00
|
|
|
|
2017-03-05 22:56:50 +01:00
|
|
|
if (class_exists('\PDO') && in_array('mysql', PDO::getAvailableDrivers())) {
|
2017-10-11 14:56:36 +02:00
|
|
|
self::$driver = 'pdo';
|
2017-03-05 22:56:50 +01:00
|
|
|
$connect = "mysql:host=".$server.";dbname=".$db;
|
2017-05-03 21:22:44 +02:00
|
|
|
|
|
|
|
if (isset($port)) {
|
|
|
|
$connect .= ";port=".$port;
|
|
|
|
}
|
|
|
|
|
2017-03-05 22:56:50 +01:00
|
|
|
if (isset($a->config["system"]["db_charset"])) {
|
|
|
|
$connect .= ";charset=".$a->config["system"]["db_charset"];
|
|
|
|
}
|
2017-08-04 11:01:29 +02:00
|
|
|
try {
|
2017-10-11 14:56:36 +02:00
|
|
|
self::$db = @new PDO($connect, $user, $pass);
|
|
|
|
self::$connected = true;
|
2017-08-04 11:01:29 +02:00
|
|
|
} catch (PDOException $e) {
|
2017-03-05 22:56:50 +01:00
|
|
|
}
|
2017-08-09 08:02:48 +02:00
|
|
|
}
|
|
|
|
|
2017-10-11 14:56:36 +02:00
|
|
|
if (!self::$connected && class_exists('mysqli')) {
|
|
|
|
self::$driver = 'mysqli';
|
|
|
|
self::$db = @new mysqli($server, $user, $pass, $db, $port);
|
2017-03-05 22:56:50 +01:00
|
|
|
if (!mysqli_connect_errno()) {
|
2017-10-11 14:56:36 +02:00
|
|
|
self::$connected = true;
|
2017-03-06 11:10:22 +01:00
|
|
|
|
|
|
|
if (isset($a->config["system"]["db_charset"])) {
|
2017-10-11 14:56:36 +02:00
|
|
|
self::$db->set_charset($a->config["system"]["db_charset"]);
|
2017-03-06 11:10:22 +01:00
|
|
|
}
|
2016-10-22 12:14:41 +02:00
|
|
|
}
|
2017-08-09 08:02:48 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// No suitable SQL driver was found.
|
2017-10-11 14:56:36 +02:00
|
|
|
if (!self::$connected) {
|
2018-04-27 21:07:04 +02:00
|
|
|
self::$driver = null;
|
2017-10-11 14:56:36 +02:00
|
|
|
self::$db = null;
|
2011-03-02 01:24:22 +01:00
|
|
|
}
|
2013-01-26 18:35:39 +01:00
|
|
|
$a->save_timestamp($stamp1, "network");
|
2017-04-24 01:15:38 +02:00
|
|
|
|
2018-03-18 10:15:14 +01:00
|
|
|
return self::$connected;
|
2017-10-11 00:18:57 +02:00
|
|
|
}
|
|
|
|
|
2018-04-09 21:23:41 +02:00
|
|
|
/**
|
|
|
|
* Return the database object.
|
|
|
|
* @return PDO|mysqli
|
|
|
|
*/
|
|
|
|
public static function get_db()
|
|
|
|
{
|
|
|
|
return self::$db;
|
|
|
|
}
|
|
|
|
|
2016-10-07 14:33:13 +02:00
|
|
|
/**
|
|
|
|
* @brief Returns the MySQL server version string
|
2017-06-08 04:00:59 +02:00
|
|
|
*
|
2016-10-05 05:43:44 +02:00
|
|
|
* This function discriminate between the deprecated mysql API and the current
|
|
|
|
* object-oriented mysqli API. Example of returned string: 5.5.46-0+deb8u1
|
|
|
|
*
|
|
|
|
* @return string
|
|
|
|
*/
|
2017-10-11 14:56:36 +02:00
|
|
|
public static function server_info() {
|
|
|
|
if (self::$_server_info == '') {
|
|
|
|
switch (self::$driver) {
|
2017-04-15 12:40:32 +02:00
|
|
|
case 'pdo':
|
2017-10-11 14:56:36 +02:00
|
|
|
self::$_server_info = self::$db->getAttribute(PDO::ATTR_SERVER_VERSION);
|
2017-04-15 12:40:32 +02:00
|
|
|
break;
|
|
|
|
case 'mysqli':
|
2017-10-11 14:56:36 +02:00
|
|
|
self::$_server_info = self::$db->server_info;
|
2017-04-15 12:40:32 +02:00
|
|
|
break;
|
|
|
|
}
|
2016-10-05 05:43:44 +02:00
|
|
|
}
|
2017-10-11 14:56:36 +02:00
|
|
|
return self::$_server_info;
|
2016-10-05 05:43:44 +02:00
|
|
|
}
|
|
|
|
|
2016-11-22 17:47:08 +01:00
|
|
|
/**
|
|
|
|
* @brief Returns the selected database name
|
|
|
|
*
|
|
|
|
* @return string
|
|
|
|
*/
|
2017-10-11 14:56:36 +02:00
|
|
|
public static function database_name() {
|
|
|
|
$ret = self::p("SELECT DATABASE() AS `db`");
|
|
|
|
$data = self::inArray($ret);
|
|
|
|
return $data[0]['db'];
|
2016-11-22 17:47:08 +01:00
|
|
|
}
|
|
|
|
|
2017-01-21 21:15:49 +01:00
|
|
|
/**
|
|
|
|
* @brief Analyze a database query and log this if some conditions are met.
|
|
|
|
*
|
|
|
|
* @param string $query The database query that will be analyzed
|
|
|
|
*/
|
2018-03-04 00:02:45 +01:00
|
|
|
private static function logIndex($query) {
|
2017-01-13 08:46:47 +01:00
|
|
|
$a = get_app();
|
|
|
|
|
2017-08-03 07:50:44 +02:00
|
|
|
if (empty($a->config["system"]["db_log_index"])) {
|
2017-01-13 08:46:47 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-01-14 22:36:34 +01:00
|
|
|
// Don't explain an explain statement
|
2017-01-13 08:46:47 +01:00
|
|
|
if (strtolower(substr($query, 0, 7)) == "explain") {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-01-14 22:36:34 +01:00
|
|
|
// Only do the explain on "select", "update" and "delete"
|
2018-01-15 14:05:12 +01:00
|
|
|
if (!in_array(strtolower(substr($query, 0, 6)), ["select", "update", "delete"])) {
|
2017-01-14 22:36:34 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-10-11 14:56:36 +02:00
|
|
|
$r = self::p("EXPLAIN ".$query);
|
2017-11-08 04:57:46 +01:00
|
|
|
if (!DBM::is_result($r)) {
|
2017-01-13 08:46:47 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
$watchlist = explode(',', $a->config["system"]["db_log_index_watch"]);
|
2017-01-14 22:36:34 +01:00
|
|
|
$blacklist = explode(',', $a->config["system"]["db_log_index_blacklist"]);
|
2017-01-13 08:46:47 +01:00
|
|
|
|
2017-10-11 14:56:36 +02:00
|
|
|
while ($row = dba::fetch($r)) {
|
2017-01-14 22:36:34 +01:00
|
|
|
if ((intval($a->config["system"]["db_loglimit_index"]) > 0)) {
|
2017-06-08 04:00:59 +02:00
|
|
|
$log = (in_array($row['key'], $watchlist) &&
|
2017-01-14 22:36:34 +01:00
|
|
|
($row['rows'] >= intval($a->config["system"]["db_loglimit_index"])));
|
2017-03-05 22:56:50 +01:00
|
|
|
} else {
|
2017-01-14 22:36:34 +01:00
|
|
|
$log = false;
|
2017-03-05 22:56:50 +01:00
|
|
|
}
|
2017-01-14 22:36:34 +01:00
|
|
|
|
2017-06-08 04:00:59 +02:00
|
|
|
if ((intval($a->config["system"]["db_loglimit_index_high"]) > 0) && ($row['rows'] >= intval($a->config["system"]["db_loglimit_index_high"]))) {
|
2017-01-14 22:36:34 +01:00
|
|
|
$log = true;
|
|
|
|
}
|
|
|
|
|
2017-06-08 04:00:59 +02:00
|
|
|
if (in_array($row['key'], $blacklist) || ($row['key'] == "")) {
|
2017-01-14 22:36:34 +01:00
|
|
|
$log = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($log) {
|
2017-01-13 08:46:47 +01:00
|
|
|
$backtrace = debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS);
|
2018-01-27 03:38:34 +01:00
|
|
|
@file_put_contents($a->config["system"]["db_log_index"], DateTimeFormat::utcNow()."\t".
|
2017-01-15 13:36:06 +01:00
|
|
|
$row['key']."\t".$row['rows']."\t".$row['Extra']."\t".
|
2017-01-13 08:46:47 +01:00
|
|
|
basename($backtrace[1]["file"])."\t".
|
|
|
|
$backtrace[1]["line"]."\t".$backtrace[2]["function"]."\t".
|
|
|
|
substr($query, 0, 2000)."\n", FILE_APPEND);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-10-11 14:56:36 +02:00
|
|
|
public static function escape($str) {
|
|
|
|
switch (self::$driver) {
|
|
|
|
case 'pdo':
|
|
|
|
return substr(@self::$db->quote($str, PDO::PARAM_STR), 1, -1);
|
|
|
|
case 'mysqli':
|
|
|
|
return @self::$db->real_escape_string($str);
|
2011-11-28 02:41:23 +01:00
|
|
|
}
|
2012-04-09 00:45:10 +02:00
|
|
|
}
|
2010-07-02 01:48:07 +02:00
|
|
|
|
2017-10-11 14:56:36 +02:00
|
|
|
public static function connected() {
|
2017-10-29 07:31:01 +01:00
|
|
|
$connected = false;
|
|
|
|
|
2017-10-11 14:56:36 +02:00
|
|
|
switch (self::$driver) {
|
2017-03-05 22:56:50 +01:00
|
|
|
case 'pdo':
|
2017-10-29 07:31:01 +01:00
|
|
|
$r = dba::p("SELECT 1");
|
2017-11-08 04:57:46 +01:00
|
|
|
if (DBM::is_result($r)) {
|
2017-10-29 07:31:01 +01:00
|
|
|
$row = dba::inArray($r);
|
|
|
|
$connected = ($row[0]['1'] == '1');
|
|
|
|
}
|
2017-03-05 22:56:50 +01:00
|
|
|
break;
|
|
|
|
case 'mysqli':
|
2017-10-11 14:56:36 +02:00
|
|
|
$connected = self::$db->ping();
|
2017-03-05 22:56:50 +01:00
|
|
|
break;
|
2016-10-22 12:14:41 +02:00
|
|
|
}
|
2016-09-22 04:57:40 +02:00
|
|
|
return $connected;
|
|
|
|
}
|
|
|
|
|
2017-04-24 01:15:38 +02:00
|
|
|
/**
|
|
|
|
* @brief Replaces ANY_VALUE() function by MIN() function,
|
|
|
|
* if the database server does not support ANY_VALUE().
|
|
|
|
*
|
|
|
|
* Considerations for Standard SQL, or MySQL with ONLY_FULL_GROUP_BY (default since 5.7.5).
|
|
|
|
* ANY_VALUE() is available from MySQL 5.7.5 https://dev.mysql.com/doc/refman/5.7/en/miscellaneous-functions.html
|
|
|
|
* A standard fall-back is to use MIN().
|
|
|
|
*
|
|
|
|
* @param string $sql An SQL string without the values
|
|
|
|
* @return string The input SQL string modified if necessary.
|
|
|
|
*/
|
2017-10-11 14:56:36 +02:00
|
|
|
public static function any_value_fallback($sql) {
|
|
|
|
$server_info = self::server_info();
|
2017-04-24 01:15:38 +02:00
|
|
|
if (version_compare($server_info, '5.7.5', '<') ||
|
|
|
|
(stripos($server_info, 'MariaDB') !== false)) {
|
|
|
|
$sql = str_ireplace('ANY_VALUE(', 'MIN(', $sql);
|
|
|
|
}
|
|
|
|
return $sql;
|
|
|
|
}
|
|
|
|
|
2017-05-15 23:06:17 +02:00
|
|
|
/**
|
2017-05-16 08:00:01 +02:00
|
|
|
* @brief beautifies the query - useful for "SHOW PROCESSLIST"
|
2017-05-15 23:06:17 +02:00
|
|
|
*
|
|
|
|
* This is safe when we bind the parameters later.
|
|
|
|
* The parameter values aren't part of the SQL.
|
|
|
|
*
|
|
|
|
* @param string $sql An SQL string without the values
|
|
|
|
* @return string The input SQL string modified if necessary.
|
|
|
|
*/
|
2017-10-11 14:56:36 +02:00
|
|
|
public static function clean_query($sql) {
|
2018-01-15 14:05:12 +01:00
|
|
|
$search = ["\t", "\n", "\r", " "];
|
|
|
|
$replace = [' ', ' ', ' ', ' '];
|
2017-05-15 23:06:17 +02:00
|
|
|
do {
|
|
|
|
$oldsql = $sql;
|
|
|
|
$sql = str_replace($search, $replace, $sql);
|
|
|
|
} while ($oldsql != $sql);
|
|
|
|
|
|
|
|
return $sql;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-04-25 07:20:34 +02:00
|
|
|
/**
|
|
|
|
* @brief Replaces the ? placeholders with the parameters in the $args array
|
|
|
|
*
|
|
|
|
* @param string $sql SQL query
|
|
|
|
* @param array $args The parameters that are to replace the ? placeholders
|
|
|
|
* @return string The replaced SQL query
|
|
|
|
*/
|
2018-03-04 00:02:45 +01:00
|
|
|
private static function replaceParameters($sql, $args) {
|
2017-04-25 07:20:34 +02:00
|
|
|
$offset = 0;
|
|
|
|
foreach ($args AS $param => $value) {
|
2017-06-08 04:00:59 +02:00
|
|
|
if (is_int($args[$param]) || is_float($args[$param])) {
|
2017-04-25 07:20:34 +02:00
|
|
|
$replace = intval($args[$param]);
|
|
|
|
} else {
|
2017-10-11 14:56:36 +02:00
|
|
|
$replace = "'".self::escape($args[$param])."'";
|
2017-04-25 07:20:34 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
$pos = strpos($sql, '?', $offset);
|
|
|
|
if ($pos !== false) {
|
|
|
|
$sql = substr_replace($sql, $replace, $pos, 1);
|
|
|
|
}
|
|
|
|
$offset = $pos + strlen($replace);
|
|
|
|
}
|
|
|
|
return $sql;
|
|
|
|
}
|
|
|
|
|
2017-08-10 14:38:32 +02:00
|
|
|
/**
|
|
|
|
* @brief Convert parameter array to an universal form
|
|
|
|
* @param array $args Parameter array
|
|
|
|
* @return array universalized parameter array
|
|
|
|
*/
|
|
|
|
private static function getParam($args) {
|
|
|
|
unset($args[0]);
|
|
|
|
|
|
|
|
// When the second function parameter is an array then use this as the parameter array
|
|
|
|
if ((count($args) > 0) && (is_array($args[1]))) {
|
|
|
|
return $args[1];
|
|
|
|
} else {
|
|
|
|
return $args;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-04-15 17:31:32 +02:00
|
|
|
/**
|
2017-04-25 07:11:04 +02:00
|
|
|
* @brief Executes a prepared statement that returns data
|
|
|
|
* @usage Example: $r = p("SELECT * FROM `item` WHERE `guid` = ?", $guid);
|
2017-09-15 08:07:34 +02:00
|
|
|
*
|
|
|
|
* Please only use it with complicated queries.
|
|
|
|
* For all regular queries please use dba::select or dba::exists
|
|
|
|
*
|
2017-04-24 01:15:38 +02:00
|
|
|
* @param string $sql SQL statement
|
2018-01-10 04:20:58 +01:00
|
|
|
* @return bool|object statement object
|
2017-04-24 01:15:38 +02:00
|
|
|
*/
|
2017-08-25 17:56:08 +02:00
|
|
|
public static function p($sql) {
|
2017-04-24 01:15:38 +02:00
|
|
|
$a = get_app();
|
|
|
|
|
|
|
|
$stamp1 = microtime(true);
|
|
|
|
|
2017-08-10 14:38:32 +02:00
|
|
|
$params = self::getParam(func_get_args());
|
2017-05-06 13:32:18 +02:00
|
|
|
|
|
|
|
// Renumber the array keys to be sure that they fit
|
|
|
|
$i = 0;
|
2018-01-15 14:05:12 +01:00
|
|
|
$args = [];
|
2017-05-06 13:32:18 +02:00
|
|
|
foreach ($params AS $param) {
|
2017-08-23 21:40:42 +02:00
|
|
|
// Avoid problems with some MySQL servers and boolean values. See issue #3645
|
|
|
|
if (is_bool($param)) {
|
|
|
|
$param = (int)$param;
|
|
|
|
}
|
2017-05-06 13:32:18 +02:00
|
|
|
$args[++$i] = $param;
|
2017-04-28 06:47:28 +02:00
|
|
|
}
|
|
|
|
|
2017-10-11 14:56:36 +02:00
|
|
|
if (!self::$connected) {
|
2017-04-24 01:15:38 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2017-09-15 05:10:04 +02:00
|
|
|
if ((substr_count($sql, '?') != count($args)) && (count($args) > 0)) {
|
2017-05-06 13:32:18 +02:00
|
|
|
// Question: Should we continue or stop the query here?
|
|
|
|
logger('Parameter mismatch. Query "'.$sql.'" - Parameters '.print_r($args, true), LOGGER_DEBUG);
|
|
|
|
}
|
|
|
|
|
2017-10-11 14:56:36 +02:00
|
|
|
$sql = self::clean_query($sql);
|
|
|
|
$sql = self::any_value_fallback($sql);
|
2017-04-24 01:15:38 +02:00
|
|
|
|
2017-08-08 08:07:04 +02:00
|
|
|
$orig_sql = $sql;
|
|
|
|
|
2017-04-24 01:15:38 +02:00
|
|
|
if (x($a->config,'system') && x($a->config['system'], 'db_callstack')) {
|
2017-08-26 12:01:50 +02:00
|
|
|
$sql = "/*".System::callstack()." */ ".$sql;
|
2017-04-24 01:15:38 +02:00
|
|
|
}
|
|
|
|
|
2017-10-11 14:56:36 +02:00
|
|
|
self::$error = '';
|
|
|
|
self::$errorno = 0;
|
|
|
|
self::$affected_rows = 0;
|
2017-04-26 23:16:25 +02:00
|
|
|
|
2017-08-08 08:07:04 +02:00
|
|
|
// We have to make some things different if this function is called from "e"
|
2017-08-10 14:38:32 +02:00
|
|
|
$trace = debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS, 2);
|
2017-08-08 08:07:04 +02:00
|
|
|
|
2017-08-10 14:38:32 +02:00
|
|
|
if (isset($trace[1])) {
|
|
|
|
$called_from = $trace[1];
|
2017-08-08 08:07:04 +02:00
|
|
|
} else {
|
|
|
|
// We use just something that is defined to avoid warnings
|
|
|
|
$called_from = $trace[0];
|
|
|
|
}
|
|
|
|
// We are having an own error logging in the function "e"
|
|
|
|
$called_from_e = ($called_from['function'] == 'e');
|
|
|
|
|
2017-10-11 14:56:36 +02:00
|
|
|
switch (self::$driver) {
|
2017-04-24 01:15:38 +02:00
|
|
|
case 'pdo':
|
2017-09-15 05:00:38 +02:00
|
|
|
// If there are no arguments we use "query"
|
|
|
|
if (count($args) == 0) {
|
2017-10-11 14:56:36 +02:00
|
|
|
if (!$retval = self::$db->query($sql)) {
|
|
|
|
$errorInfo = self::$db->errorInfo();
|
|
|
|
self::$error = $errorInfo[2];
|
|
|
|
self::$errorno = $errorInfo[1];
|
2017-09-15 05:00:38 +02:00
|
|
|
$retval = false;
|
|
|
|
break;
|
|
|
|
}
|
2017-10-11 14:56:36 +02:00
|
|
|
self::$affected_rows = $retval->rowCount();
|
2017-09-15 05:00:38 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2017-10-11 14:56:36 +02:00
|
|
|
if (!$stmt = self::$db->prepare($sql)) {
|
|
|
|
$errorInfo = self::$db->errorInfo();
|
|
|
|
self::$error = $errorInfo[2];
|
|
|
|
self::$errorno = $errorInfo[1];
|
2017-04-24 21:23:49 +02:00
|
|
|
$retval = false;
|
|
|
|
break;
|
|
|
|
}
|
2017-04-24 01:15:38 +02:00
|
|
|
|
|
|
|
foreach ($args AS $param => $value) {
|
|
|
|
$stmt->bindParam($param, $args[$param]);
|
|
|
|
}
|
|
|
|
|
2017-04-24 21:23:49 +02:00
|
|
|
if (!$stmt->execute()) {
|
2017-05-01 23:38:37 +02:00
|
|
|
$errorInfo = $stmt->errorInfo();
|
2017-10-11 14:56:36 +02:00
|
|
|
self::$error = $errorInfo[2];
|
|
|
|
self::$errorno = $errorInfo[1];
|
2017-04-24 21:23:49 +02:00
|
|
|
$retval = false;
|
|
|
|
} else {
|
|
|
|
$retval = $stmt;
|
2017-10-11 14:56:36 +02:00
|
|
|
self::$affected_rows = $retval->rowCount();
|
2017-04-24 01:15:38 +02:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
case 'mysqli':
|
2017-08-08 08:07:04 +02:00
|
|
|
// There are SQL statements that cannot be executed with a prepared statement
|
|
|
|
$parts = explode(' ', $orig_sql);
|
|
|
|
$command = strtolower($parts[0]);
|
2018-01-15 14:05:12 +01:00
|
|
|
$can_be_prepared = in_array($command, ['select', 'update', 'insert', 'delete']);
|
2017-08-08 08:07:04 +02:00
|
|
|
|
2017-09-15 05:00:38 +02:00
|
|
|
// The fallback routine is called as well when there are no arguments
|
|
|
|
if (!$can_be_prepared || (count($args) == 0)) {
|
2018-03-04 00:02:45 +01:00
|
|
|
$retval = self::$db->query(self::replaceParameters($sql, $args));
|
2017-10-11 14:56:36 +02:00
|
|
|
if (self::$db->errno) {
|
|
|
|
self::$error = self::$db->error;
|
|
|
|
self::$errorno = self::$db->errno;
|
2017-08-08 08:07:04 +02:00
|
|
|
$retval = false;
|
|
|
|
} else {
|
|
|
|
if (isset($retval->num_rows)) {
|
2017-10-11 14:56:36 +02:00
|
|
|
self::$affected_rows = $retval->num_rows;
|
2017-08-08 08:07:04 +02:00
|
|
|
} else {
|
2017-10-11 14:56:36 +02:00
|
|
|
self::$affected_rows = self::$db->affected_rows;
|
2017-08-08 08:07:04 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2017-10-11 14:56:36 +02:00
|
|
|
$stmt = self::$db->stmt_init();
|
2017-04-24 01:15:38 +02:00
|
|
|
|
|
|
|
if (!$stmt->prepare($sql)) {
|
2017-10-11 14:56:36 +02:00
|
|
|
self::$error = $stmt->error;
|
|
|
|
self::$errorno = $stmt->errno;
|
2017-04-24 01:15:38 +02:00
|
|
|
$retval = false;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
$params = '';
|
2018-01-15 14:05:12 +01:00
|
|
|
$values = [];
|
2017-04-24 01:15:38 +02:00
|
|
|
foreach ($args AS $param => $value) {
|
|
|
|
if (is_int($args[$param])) {
|
|
|
|
$params .= 'i';
|
|
|
|
} elseif (is_float($args[$param])) {
|
|
|
|
$params .= 'd';
|
|
|
|
} elseif (is_string($args[$param])) {
|
|
|
|
$params .= 's';
|
|
|
|
} else {
|
|
|
|
$params .= 'b';
|
|
|
|
}
|
|
|
|
$values[] = &$args[$param];
|
|
|
|
}
|
|
|
|
|
2017-05-03 21:22:44 +02:00
|
|
|
if (count($values) > 0) {
|
|
|
|
array_unshift($values, $params);
|
2018-01-15 14:05:12 +01:00
|
|
|
call_user_func_array([$stmt, 'bind_param'], $values);
|
2017-05-03 21:22:44 +02:00
|
|
|
}
|
2017-04-24 01:15:38 +02:00
|
|
|
|
|
|
|
if (!$stmt->execute()) {
|
2017-10-11 14:56:36 +02:00
|
|
|
self::$error = self::$db->error;
|
|
|
|
self::$errorno = self::$db->errno;
|
2017-04-24 01:15:38 +02:00
|
|
|
$retval = false;
|
|
|
|
} else {
|
2017-04-24 22:32:35 +02:00
|
|
|
$stmt->store_result();
|
2017-04-24 01:15:38 +02:00
|
|
|
$retval = $stmt;
|
2017-10-11 14:56:36 +02:00
|
|
|
self::$affected_rows = $retval->affected_rows;
|
2017-04-24 01:15:38 +02:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2017-08-08 08:07:04 +02:00
|
|
|
// We are having an own error logging in the function "e"
|
2017-10-11 14:56:36 +02:00
|
|
|
if ((self::$errorno != 0) && !$called_from_e) {
|
2017-08-08 08:07:04 +02:00
|
|
|
// We have to preserve the error code, somewhere in the logging it get lost
|
2017-10-11 14:56:36 +02:00
|
|
|
$error = self::$error;
|
|
|
|
$errorno = self::$errorno;
|
2017-06-13 07:52:59 +02:00
|
|
|
|
2017-10-11 14:56:36 +02:00
|
|
|
logger('DB Error '.self::$errorno.': '.self::$error."\n".
|
2018-03-04 00:02:45 +01:00
|
|
|
System::callstack(8)."\n".self::replaceParameters($sql, $params));
|
2017-06-13 07:52:59 +02:00
|
|
|
|
2017-10-11 14:56:36 +02:00
|
|
|
self::$error = $error;
|
|
|
|
self::$errorno = $errorno;
|
2017-04-26 23:16:25 +02:00
|
|
|
}
|
|
|
|
|
2017-04-24 01:15:38 +02:00
|
|
|
$a->save_timestamp($stamp1, 'database');
|
|
|
|
|
2017-04-24 23:20:24 +02:00
|
|
|
if (x($a->config,'system') && x($a->config['system'], 'db_log')) {
|
2017-04-24 21:23:49 +02:00
|
|
|
|
|
|
|
$stamp2 = microtime(true);
|
|
|
|
$duration = (float)($stamp2 - $stamp1);
|
2017-04-24 01:15:38 +02:00
|
|
|
|
2017-04-24 23:20:24 +02:00
|
|
|
if (($duration > $a->config["system"]["db_loglimit"])) {
|
|
|
|
$duration = round($duration, 3);
|
|
|
|
$backtrace = debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS);
|
2017-04-25 07:20:34 +02:00
|
|
|
|
2018-01-27 03:38:34 +01:00
|
|
|
@file_put_contents($a->config["system"]["db_log"], DateTimeFormat::utcNow()."\t".$duration."\t".
|
2017-04-24 23:20:24 +02:00
|
|
|
basename($backtrace[1]["file"])."\t".
|
|
|
|
$backtrace[1]["line"]."\t".$backtrace[2]["function"]."\t".
|
2018-03-04 00:02:45 +01:00
|
|
|
substr(self::replaceParameters($sql, $args), 0, 2000)."\n", FILE_APPEND);
|
2017-04-24 23:20:24 +02:00
|
|
|
}
|
|
|
|
}
|
2017-04-24 01:15:38 +02:00
|
|
|
return $retval;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2017-04-25 07:11:04 +02:00
|
|
|
* @brief Executes a prepared statement like UPDATE or INSERT that doesn't return data
|
2017-04-15 17:31:32 +02:00
|
|
|
*
|
2017-09-15 08:07:34 +02:00
|
|
|
* Please use dba::delete, dba::insert, dba::update, ... instead
|
|
|
|
*
|
2017-04-24 01:15:38 +02:00
|
|
|
* @param string $sql SQL statement
|
2017-04-25 07:11:04 +02:00
|
|
|
* @return boolean Was the query successfull? False is returned only if an error occurred
|
2017-04-15 17:31:32 +02:00
|
|
|
*/
|
2017-08-25 17:56:08 +02:00
|
|
|
public static function e($sql) {
|
2017-04-24 21:23:49 +02:00
|
|
|
$a = get_app();
|
|
|
|
|
|
|
|
$stamp = microtime(true);
|
|
|
|
|
2017-08-10 14:38:32 +02:00
|
|
|
$params = self::getParam(func_get_args());
|
2017-04-24 08:13:42 +02:00
|
|
|
|
2017-06-13 11:03:19 +02:00
|
|
|
// In a case of a deadlock we are repeating the query 20 times
|
|
|
|
$timeout = 20;
|
2017-04-24 21:23:49 +02:00
|
|
|
|
2017-06-13 07:52:59 +02:00
|
|
|
do {
|
2017-08-10 14:38:32 +02:00
|
|
|
$stmt = self::p($sql, $params);
|
2017-06-13 07:52:59 +02:00
|
|
|
|
|
|
|
if (is_bool($stmt)) {
|
|
|
|
$retval = $stmt;
|
|
|
|
} elseif (is_object($stmt)) {
|
|
|
|
$retval = true;
|
|
|
|
} else {
|
|
|
|
$retval = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
self::close($stmt);
|
|
|
|
|
2017-10-11 14:56:36 +02:00
|
|
|
} while ((self::$errorno == 1213) && (--$timeout > 0));
|
2017-06-13 07:52:59 +02:00
|
|
|
|
2017-10-11 14:56:36 +02:00
|
|
|
if (self::$errorno != 0) {
|
2017-06-13 07:52:59 +02:00
|
|
|
// We have to preserve the error code, somewhere in the logging it get lost
|
2017-10-11 14:56:36 +02:00
|
|
|
$error = self::$error;
|
|
|
|
$errorno = self::$errorno;
|
2017-04-24 21:23:49 +02:00
|
|
|
|
2017-10-11 14:56:36 +02:00
|
|
|
logger('DB Error '.self::$errorno.': '.self::$error."\n".
|
2018-03-04 00:02:45 +01:00
|
|
|
System::callstack(8)."\n".self::replaceParameters($sql, $params));
|
2017-06-13 07:52:59 +02:00
|
|
|
|
2017-10-11 14:56:36 +02:00
|
|
|
self::$error = $error;
|
|
|
|
self::$errorno = $errorno;
|
2017-06-13 07:52:59 +02:00
|
|
|
}
|
2017-04-24 21:23:49 +02:00
|
|
|
|
|
|
|
$a->save_timestamp($stamp, "database_write");
|
|
|
|
|
|
|
|
return $retval;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Check if data exists
|
|
|
|
*
|
2017-08-12 10:55:50 +02:00
|
|
|
* @param string $table Table name
|
|
|
|
* @param array $condition array of fields for condition
|
|
|
|
*
|
|
|
|
* @return boolean Are there rows for that condition?
|
2017-04-24 21:23:49 +02:00
|
|
|
*/
|
2017-08-25 17:56:08 +02:00
|
|
|
public static function exists($table, $condition) {
|
2017-08-12 10:55:50 +02:00
|
|
|
if (empty($table)) {
|
|
|
|
return false;
|
|
|
|
}
|
2017-04-24 21:23:49 +02:00
|
|
|
|
2018-01-15 14:05:12 +01:00
|
|
|
$fields = [];
|
2017-08-12 15:54:29 +02:00
|
|
|
|
2018-03-04 00:02:45 +01:00
|
|
|
reset($condition);
|
|
|
|
$first_key = key($condition);
|
|
|
|
if (!is_int($first_key)) {
|
|
|
|
$fields = [$first_key];
|
2017-08-12 15:54:29 +02:00
|
|
|
}
|
2017-08-12 10:55:50 +02:00
|
|
|
|
2018-01-10 04:20:33 +01:00
|
|
|
$stmt = self::select($table, $fields, $condition, ['limit' => 1]);
|
2017-04-24 21:23:49 +02:00
|
|
|
|
|
|
|
if (is_bool($stmt)) {
|
|
|
|
$retval = $stmt;
|
|
|
|
} else {
|
2017-04-27 22:38:46 +02:00
|
|
|
$retval = (self::num_rows($stmt) > 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
self::close($stmt);
|
|
|
|
|
|
|
|
return $retval;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2018-01-10 04:20:58 +01:00
|
|
|
* Fetches the first row
|
2018-01-10 16:51:49 +01:00
|
|
|
*
|
|
|
|
* Please use dba::selectFirst or dba::exists whenever this is possible.
|
2017-09-15 08:07:34 +02:00
|
|
|
*
|
2018-01-10 04:20:58 +01:00
|
|
|
* @brief Fetches the first row
|
2017-04-27 22:38:46 +02:00
|
|
|
* @param string $sql SQL statement
|
|
|
|
* @return array first row of query
|
|
|
|
*/
|
2017-08-25 17:56:08 +02:00
|
|
|
public static function fetch_first($sql) {
|
2017-08-10 14:38:32 +02:00
|
|
|
$params = self::getParam(func_get_args());
|
2017-04-27 22:38:46 +02:00
|
|
|
|
2017-08-10 14:38:32 +02:00
|
|
|
$stmt = self::p($sql, $params);
|
2017-04-27 22:38:46 +02:00
|
|
|
|
|
|
|
if (is_bool($stmt)) {
|
|
|
|
$retval = $stmt;
|
|
|
|
} else {
|
|
|
|
$retval = self::fetch($stmt);
|
2017-04-24 21:23:49 +02:00
|
|
|
}
|
|
|
|
|
2017-04-24 08:13:42 +02:00
|
|
|
self::close($stmt);
|
2017-04-24 21:23:49 +02:00
|
|
|
|
|
|
|
return $retval;
|
2017-04-24 01:15:38 +02:00
|
|
|
}
|
|
|
|
|
2017-06-13 23:56:50 +02:00
|
|
|
/**
|
|
|
|
* @brief Returns the number of affected rows of the last statement
|
|
|
|
*
|
|
|
|
* @return int Number of rows
|
|
|
|
*/
|
2017-08-25 17:56:08 +02:00
|
|
|
public static function affected_rows() {
|
2017-10-11 14:56:36 +02:00
|
|
|
return self::$affected_rows;
|
2017-06-13 23:56:50 +02:00
|
|
|
}
|
|
|
|
|
2017-09-14 07:19:05 +02:00
|
|
|
/**
|
|
|
|
* @brief Returns the number of columns of a statement
|
|
|
|
*
|
|
|
|
* @param object Statement object
|
|
|
|
* @return int Number of columns
|
|
|
|
*/
|
|
|
|
public static function columnCount($stmt) {
|
|
|
|
if (!is_object($stmt)) {
|
|
|
|
return 0;
|
|
|
|
}
|
2017-10-11 14:56:36 +02:00
|
|
|
switch (self::$driver) {
|
2017-09-14 07:19:05 +02:00
|
|
|
case 'pdo':
|
|
|
|
return $stmt->columnCount();
|
|
|
|
case 'mysqli':
|
|
|
|
return $stmt->field_count;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
2017-04-24 22:32:35 +02:00
|
|
|
/**
|
2017-04-24 23:23:00 +02:00
|
|
|
* @brief Returns the number of rows of a statement
|
2017-04-24 22:32:35 +02:00
|
|
|
*
|
2018-01-10 04:20:58 +01:00
|
|
|
* @param PDOStatement|mysqli_result|mysqli_stmt Statement object
|
2017-04-24 22:32:35 +02:00
|
|
|
* @return int Number of rows
|
|
|
|
*/
|
2017-08-25 17:56:08 +02:00
|
|
|
public static function num_rows($stmt) {
|
2017-05-15 17:17:38 +02:00
|
|
|
if (!is_object($stmt)) {
|
|
|
|
return 0;
|
|
|
|
}
|
2017-10-11 14:56:36 +02:00
|
|
|
switch (self::$driver) {
|
2017-04-24 22:32:35 +02:00
|
|
|
case 'pdo':
|
|
|
|
return $stmt->rowCount();
|
|
|
|
case 'mysqli':
|
|
|
|
return $stmt->num_rows;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2017-04-24 01:15:38 +02:00
|
|
|
/**
|
|
|
|
* @brief Fetch a single row
|
|
|
|
*
|
2018-01-16 01:27:48 +01:00
|
|
|
* @param mixed $stmt statement object
|
2017-04-24 01:15:38 +02:00
|
|