]> git.mxchange.org Git - friendica.git/blob - include/dba.php
Force a database reconnection in the daemon to prevent lost connections
[friendica.git] / include / dba.php
1 <?php
2
3 use Friendica\App;
4 use Friendica\Core\L10n;
5 use Friendica\Core\System;
6 use Friendica\Database\DBM;
7 use Friendica\Database\DBStructure;
8 use Friendica\Util\DateTimeFormat;
9
10 /**
11  * @class MySQL database class
12  *
13  * This class is for the low level database stuff that does driver specific things.
14  */
15
16 class dba {
17         public static $connected = false;
18
19         private static $_server_info = '';
20         private static $db;
21         private static $driver;
22         private static $error = false;
23         private static $errorno = 0;
24         private static $affected_rows = 0;
25         private static $in_transaction = false;
26         private static $relation = [];
27
28         public static function connect($serveraddr, $user, $pass, $db) {
29                 if (!is_null(self::$db) && self::connected()) {
30                         return true;
31                 }
32
33                 $a = get_app();
34
35                 $stamp1 = microtime(true);
36
37                 $serveraddr = trim($serveraddr);
38
39                 $serverdata = explode(':', $serveraddr);
40                 $server = $serverdata[0];
41
42                 if (count($serverdata) > 1) {
43                         $port = trim($serverdata[1]);
44                 }
45
46                 $server = trim($server);
47                 $user = trim($user);
48                 $pass = trim($pass);
49                 $db = trim($db);
50
51                 if (!(strlen($server) && strlen($user))) {
52                         return false;
53                 }
54
55                 if (class_exists('\PDO') && in_array('mysql', PDO::getAvailableDrivers())) {
56                         self::$driver = 'pdo';
57                         $connect = "mysql:host=".$server.";dbname=".$db;
58
59                         if (isset($port)) {
60                                 $connect .= ";port=".$port;
61                         }
62
63                         if (isset($a->config["system"]["db_charset"])) {
64                                 $connect .= ";charset=".$a->config["system"]["db_charset"];
65                         }
66                         try {
67                                 self::$db = @new PDO($connect, $user, $pass);
68                                 self::$connected = true;
69                         } catch (PDOException $e) {
70                         }
71                 }
72
73                 if (!self::$connected && class_exists('mysqli')) {
74                         self::$driver = 'mysqli';
75                         self::$db = @new mysqli($server, $user, $pass, $db, $port);
76                         if (!mysqli_connect_errno()) {
77                                 self::$connected = true;
78
79                                 if (isset($a->config["system"]["db_charset"])) {
80                                         self::$db->set_charset($a->config["system"]["db_charset"]);
81                                 }
82                         }
83                 }
84
85                 // No suitable SQL driver was found.
86                 if (!self::$connected) {
87                         self::$driver = null;
88                         self::$db = null;
89                 }
90                 $a->save_timestamp($stamp1, "network");
91
92                 return self::$connected;
93         }
94
95         /**
96          * Disconnects the current database connection
97          */
98         public static function disconnect()
99         {
100                 if (is_null(self::$db)) {
101                         return;
102                 }
103
104                 switch (self::$driver) {
105                         case 'pdo':
106                                 self::$db = null;
107                                 break;
108                         case 'mysqli':
109                                 self::$db->close();
110                                 self::$db = null;
111                                 break;
112                 }
113         }
114
115         /**
116          * Return the database object.
117          * @return PDO|mysqli
118          */
119         public static function get_db()
120         {
121                 return self::$db;
122         }
123
124         /**
125          * @brief Returns the MySQL server version string
126          *
127          * This function discriminate between the deprecated mysql API and the current
128          * object-oriented mysqli API. Example of returned string: 5.5.46-0+deb8u1
129          *
130          * @return string
131          */
132         public static function server_info() {
133                 if (self::$_server_info == '') {
134                         switch (self::$driver) {
135                                 case 'pdo':
136                                         self::$_server_info = self::$db->getAttribute(PDO::ATTR_SERVER_VERSION);
137                                         break;
138                                 case 'mysqli':
139                                         self::$_server_info = self::$db->server_info;
140                                         break;
141                         }
142                 }
143                 return self::$_server_info;
144         }
145
146         /**
147          * @brief Returns the selected database name
148          *
149          * @return string
150          */
151         public static function database_name() {
152                 $ret = self::p("SELECT DATABASE() AS `db`");
153                 $data = self::inArray($ret);
154                 return $data[0]['db'];
155         }
156
157         /**
158          * @brief Analyze a database query and log this if some conditions are met.
159          *
160          * @param string $query The database query that will be analyzed
161          */
162         private static function logIndex($query) {
163                 $a = get_app();
164
165                 if (empty($a->config["system"]["db_log_index"])) {
166                         return;
167                 }
168
169                 // Don't explain an explain statement
170                 if (strtolower(substr($query, 0, 7)) == "explain") {
171                         return;
172                 }
173
174                 // Only do the explain on "select", "update" and "delete"
175                 if (!in_array(strtolower(substr($query, 0, 6)), ["select", "update", "delete"])) {
176                         return;
177                 }
178
179                 $r = self::p("EXPLAIN ".$query);
180                 if (!DBM::is_result($r)) {
181                         return;
182                 }
183
184                 $watchlist = explode(',', $a->config["system"]["db_log_index_watch"]);
185                 $blacklist = explode(',', $a->config["system"]["db_log_index_blacklist"]);
186
187                 while ($row = dba::fetch($r)) {
188                         if ((intval($a->config["system"]["db_loglimit_index"]) > 0)) {
189                                 $log = (in_array($row['key'], $watchlist) &&
190                                         ($row['rows'] >= intval($a->config["system"]["db_loglimit_index"])));
191                         } else {
192                                 $log = false;
193                         }
194
195                         if ((intval($a->config["system"]["db_loglimit_index_high"]) > 0) && ($row['rows'] >= intval($a->config["system"]["db_loglimit_index_high"]))) {
196                                 $log = true;
197                         }
198
199                         if (in_array($row['key'], $blacklist) || ($row['key'] == "")) {
200                                 $log = false;
201                         }
202
203                         if ($log) {
204                                 $backtrace = debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS);
205                                 @file_put_contents($a->config["system"]["db_log_index"], DateTimeFormat::utcNow()."\t".
206                                                 $row['key']."\t".$row['rows']."\t".$row['Extra']."\t".
207                                                 basename($backtrace[1]["file"])."\t".
208                                                 $backtrace[1]["line"]."\t".$backtrace[2]["function"]."\t".
209                                                 substr($query, 0, 2000)."\n", FILE_APPEND);
210                         }
211                 }
212         }
213
214         public static function escape($str) {
215                 switch (self::$driver) {
216                         case 'pdo':
217                                 return substr(@self::$db->quote($str, PDO::PARAM_STR), 1, -1);
218                         case 'mysqli':
219                                 return @self::$db->real_escape_string($str);
220                 }
221         }
222
223         public static function connected() {
224                 $connected = false;
225
226                 switch (self::$driver) {
227                         case 'pdo':
228                                 $r = dba::p("SELECT 1");
229                                 if (DBM::is_result($r)) {
230                                         $row = dba::inArray($r);
231                                         $connected = ($row[0]['1'] == '1');
232                                 }
233                                 break;
234                         case 'mysqli':
235                                 $connected = self::$db->ping();
236                                 break;
237                 }
238                 return $connected;
239         }
240
241         /**
242          * @brief Replaces ANY_VALUE() function by MIN() function,
243          *  if the database server does not support ANY_VALUE().
244          *
245          * Considerations for Standard SQL, or MySQL with ONLY_FULL_GROUP_BY (default since 5.7.5).
246          * ANY_VALUE() is available from MySQL 5.7.5 https://dev.mysql.com/doc/refman/5.7/en/miscellaneous-functions.html
247          * A standard fall-back is to use MIN().
248          *
249          * @param string $sql An SQL string without the values
250          * @return string The input SQL string modified if necessary.
251          */
252         public static function any_value_fallback($sql) {
253                 $server_info = self::server_info();
254                 if (version_compare($server_info, '5.7.5', '<') ||
255                         (stripos($server_info, 'MariaDB') !== false)) {
256                         $sql = str_ireplace('ANY_VALUE(', 'MIN(', $sql);
257                 }
258                 return $sql;
259         }
260
261         /**
262          * @brief beautifies the query - useful for "SHOW PROCESSLIST"
263          *
264          * This is safe when we bind the parameters later.
265          * The parameter values aren't part of the SQL.
266          *
267          * @param string $sql An SQL string without the values
268          * @return string The input SQL string modified if necessary.
269          */
270         public static function clean_query($sql) {
271                 $search = ["\t", "\n", "\r", "  "];
272                 $replace = [' ', ' ', ' ', ' '];
273                 do {
274                         $oldsql = $sql;
275                         $sql = str_replace($search, $replace, $sql);
276                 } while ($oldsql != $sql);
277
278                 return $sql;
279         }
280
281
282         /**
283          * @brief Replaces the ? placeholders with the parameters in the $args array
284          *
285          * @param string $sql SQL query
286          * @param array $args The parameters that are to replace the ? placeholders
287          * @return string The replaced SQL query
288          */
289         private static function replaceParameters($sql, $args) {
290                 $offset = 0;
291                 foreach ($args AS $param => $value) {
292                         if (is_int($args[$param]) || is_float($args[$param])) {
293                                 $replace = intval($args[$param]);
294                         } else {
295                                 $replace = "'".self::escape($args[$param])."'";
296                         }
297
298                         $pos = strpos($sql, '?', $offset);
299                         if ($pos !== false) {
300                                 $sql = substr_replace($sql, $replace, $pos, 1);
301                         }
302                         $offset = $pos + strlen($replace);
303                 }
304                 return $sql;
305         }
306
307         /**
308          * @brief Convert parameter array to an universal form
309          * @param array $args Parameter array
310          * @return array universalized parameter array
311          */
312         private static function getParam($args) {
313                 unset($args[0]);
314
315                 // When the second function parameter is an array then use this as the parameter array
316                 if ((count($args) > 0) && (is_array($args[1]))) {
317                         return $args[1];
318                 } else {
319                         return $args;
320                 }
321         }
322
323         /**
324          * @brief Executes a prepared statement that returns data
325          * @usage Example: $r = p("SELECT * FROM `item` WHERE `guid` = ?", $guid);
326          *
327          * Please only use it with complicated queries.
328          * For all regular queries please use dba::select or dba::exists
329          *
330          * @param string $sql SQL statement
331          * @return bool|object statement object
332          */
333         public static function p($sql) {
334                 $a = get_app();
335
336                 $stamp1 = microtime(true);
337
338                 $params = self::getParam(func_get_args());
339
340                 // Renumber the array keys to be sure that they fit
341                 $i = 0;
342                 $args = [];
343                 foreach ($params AS $param) {
344                         // Avoid problems with some MySQL servers and boolean values. See issue #3645
345                         if (is_bool($param)) {
346                                 $param = (int)$param;
347                         }
348                         $args[++$i] = $param;
349                 }
350
351                 if (!self::$connected) {
352                         return false;
353                 }
354
355                 if ((substr_count($sql, '?') != count($args)) && (count($args) > 0)) {
356                         // Question: Should we continue or stop the query here?
357                         logger('Parameter mismatch. Query "'.$sql.'" - Parameters '.print_r($args, true), LOGGER_DEBUG);
358                 }
359
360                 $sql = self::clean_query($sql);
361                 $sql = self::any_value_fallback($sql);
362
363                 $orig_sql = $sql;
364
365                 if (x($a->config,'system') && x($a->config['system'], 'db_callstack')) {
366                         $sql = "/*".System::callstack()." */ ".$sql;
367                 }
368
369                 self::$error = '';
370                 self::$errorno = 0;
371                 self::$affected_rows = 0;
372
373                 // We have to make some things different if this function is called from "e"
374                 $trace = debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS, 2);
375
376                 if (isset($trace[1])) {
377                         $called_from = $trace[1];
378                 } else {
379                         // We use just something that is defined to avoid warnings
380                         $called_from = $trace[0];
381                 }
382                 // We are having an own error logging in the function "e"
383                 $called_from_e = ($called_from['function'] == 'e');
384
385                 switch (self::$driver) {
386                         case 'pdo':
387                                 // If there are no arguments we use "query"
388                                 if (count($args) == 0) {
389                                         if (!$retval = self::$db->query($sql)) {
390                                                 $errorInfo = self::$db->errorInfo();
391                                                 self::$error = $errorInfo[2];
392                                                 self::$errorno = $errorInfo[1];
393                                                 $retval = false;
394                                                 break;
395                                         }
396                                         self::$affected_rows = $retval->rowCount();
397                                         break;
398                                 }
399
400                                 if (!$stmt = self::$db->prepare($sql)) {
401                                         $errorInfo = self::$db->errorInfo();
402                                         self::$error = $errorInfo[2];
403                                         self::$errorno = $errorInfo[1];
404                                         $retval = false;
405                                         break;
406                                 }
407
408                                 foreach ($args AS $param => $value) {
409                                         $stmt->bindParam($param, $args[$param]);
410                                 }
411
412                                 if (!$stmt->execute()) {
413                                         $errorInfo = $stmt->errorInfo();
414                                         self::$error = $errorInfo[2];
415                                         self::$errorno = $errorInfo[1];
416                                         $retval = false;
417                                 } else {
418                                         $retval = $stmt;
419                                         self::$affected_rows = $retval->rowCount();
420                                 }
421                                 break;
422                         case 'mysqli':
423                                 // There are SQL statements that cannot be executed with a prepared statement
424                                 $parts = explode(' ', $orig_sql);
425                                 $command = strtolower($parts[0]);
426                                 $can_be_prepared = in_array($command, ['select', 'update', 'insert', 'delete']);
427
428                                 // The fallback routine is called as well when there are no arguments
429                                 if (!$can_be_prepared || (count($args) == 0)) {
430                                         $retval = self::$db->query(self::replaceParameters($sql, $args));
431                                         if (self::$db->errno) {
432                                                 self::$error = self::$db->error;
433                                                 self::$errorno = self::$db->errno;
434                                                 $retval = false;
435                                         } else {
436                                                 if (isset($retval->num_rows)) {
437                                                         self::$affected_rows = $retval->num_rows;
438                                                 } else {
439                                                         self::$affected_rows = self::$db->affected_rows;
440                                                 }
441                                         }
442                                         break;
443                                 }
444
445                                 $stmt = self::$db->stmt_init();
446
447                                 if (!$stmt->prepare($sql)) {
448                                         self::$error = $stmt->error;
449                                         self::$errorno = $stmt->errno;
450                                         $retval = false;
451                                         break;
452                                 }
453
454                                 $params = '';
455                                 $values = [];
456                                 foreach ($args AS $param => $value) {
457                                         if (is_int($args[$param])) {
458                                                 $params .= 'i';
459                                         } elseif (is_float($args[$param])) {
460                                                 $params .= 'd';
461                                         } elseif (is_string($args[$param])) {
462                                                 $params .= 's';
463                                         } else {
464                                                 $params .= 'b';
465                                         }
466                                         $values[] = &$args[$param];
467                                 }
468
469                                 if (count($values) > 0) {
470                                         array_unshift($values, $params);
471                                         call_user_func_array([$stmt, 'bind_param'], $values);
472                                 }
473
474                                 if (!$stmt->execute()) {
475                                         self::$error = self::$db->error;
476                                         self::$errorno = self::$db->errno;
477                                         $retval = false;
478                                 } else {
479                                         $stmt->store_result();
480                                         $retval = $stmt;
481                                         self::$affected_rows = $retval->affected_rows;
482                                 }
483                                 break;
484                 }
485
486                 // We are having an own error logging in the function "e"
487                 if ((self::$errorno != 0) && !$called_from_e) {
488                         // We have to preserve the error code, somewhere in the logging it get lost
489                         $error = self::$error;
490                         $errorno = self::$errorno;
491
492                         logger('DB Error '.self::$errorno.': '.self::$error."\n".
493                                 System::callstack(8)."\n".self::replaceParameters($sql, $params));
494
495                         self::$error = $error;
496                         self::$errorno = $errorno;
497                 }
498
499                 $a->save_timestamp($stamp1, 'database');
500
501                 if (x($a->config,'system') && x($a->config['system'], 'db_log')) {
502
503                         $stamp2 = microtime(true);
504                         $duration = (float)($stamp2 - $stamp1);
505
506                         if (($duration > $a->config["system"]["db_loglimit"])) {
507                                 $duration = round($duration, 3);
508                                 $backtrace = debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS);
509
510                                 @file_put_contents($a->config["system"]["db_log"], DateTimeFormat::utcNow()."\t".$duration."\t".
511                                                 basename($backtrace[1]["file"])."\t".
512                                                 $backtrace[1]["line"]."\t".$backtrace[2]["function"]."\t".
513                                                 substr(self::replaceParameters($sql, $args), 0, 2000)."\n", FILE_APPEND);
514                         }
515                 }
516                 return $retval;
517         }
518
519         /**
520          * @brief Executes a prepared statement like UPDATE or INSERT that doesn't return data
521          *
522          * Please use dba::delete, dba::insert, dba::update, ... instead
523          *
524          * @param string $sql SQL statement
525          * @return boolean Was the query successfull? False is returned only if an error occurred
526          */
527         public static function e($sql) {
528                 $a = get_app();
529
530                 $stamp = microtime(true);
531
532                 $params = self::getParam(func_get_args());
533
534                 // In a case of a deadlock we are repeating the query 20 times
535                 $timeout = 20;
536
537                 do {
538                         $stmt = self::p($sql, $params);
539
540                         if (is_bool($stmt)) {
541                                 $retval = $stmt;
542                         } elseif (is_object($stmt)) {
543                                 $retval = true;
544                         } else {
545                                 $retval = false;
546                         }
547
548                         self::close($stmt);
549
550                 } while ((self::$errorno == 1213) && (--$timeout > 0));
551
552                 if (self::$errorno != 0) {
553                         // We have to preserve the error code, somewhere in the logging it get lost
554                         $error = self::$error;
555                         $errorno = self::$errorno;
556
557                         logger('DB Error '.self::$errorno.': '.self::$error."\n".
558                                 System::callstack(8)."\n".self::replaceParameters($sql, $params));
559
560                         self::$error = $error;
561                         self::$errorno = $errorno;
562                 }
563
564                 $a->save_timestamp($stamp, "database_write");
565
566                 return $retval;
567         }
568
569         /**
570          * @brief Check if data exists
571          *
572          * @param string $table Table name
573          * @param array $condition array of fields for condition
574          *
575          * @return boolean Are there rows for that condition?
576          */
577         public static function exists($table, $condition) {
578                 if (empty($table)) {
579                         return false;
580                 }
581
582                 $fields = [];
583
584                 reset($condition);
585                 $first_key = key($condition);
586                 if (!is_int($first_key)) {
587                         $fields = [$first_key];
588                 }
589
590                 $stmt = self::select($table, $fields, $condition, ['limit' => 1]);
591
592                 if (is_bool($stmt)) {
593                         $retval = $stmt;
594                 } else {
595                         $retval = (self::num_rows($stmt) > 0);
596                 }
597
598                 self::close($stmt);
599
600                 return $retval;
601         }
602
603         /**
604          * Fetches the first row
605          *
606          * Please use dba::selectFirst or dba::exists whenever this is possible.
607          *
608          * @brief Fetches the first row
609          * @param string $sql SQL statement
610          * @return array first row of query
611          */
612         public static function fetch_first($sql) {
613                 $params = self::getParam(func_get_args());
614
615                 $stmt = self::p($sql, $params);
616
617                 if (is_bool($stmt)) {
618                         $retval = $stmt;
619                 } else {
620                         $retval = self::fetch($stmt);
621                 }
622
623                 self::close($stmt);
624
625                 return $retval;
626         }
627
628         /**
629          * @brief Returns the number of affected rows of the last statement
630          *
631          * @return int Number of rows
632          */
633         public static function affected_rows() {
634                 return self::$affected_rows;
635         }
636
637         /**
638          * @brief Returns the number of columns of a statement
639          *
640          * @param object Statement object
641          * @return int Number of columns
642          */
643         public static function columnCount($stmt) {
644                 if (!is_object($stmt)) {
645                         return 0;
646                 }
647                 switch (self::$driver) {
648                         case 'pdo':
649                                 return $stmt->columnCount();
650                         case 'mysqli':
651                                 return $stmt->field_count;
652                 }
653                 return 0;
654         }
655         /**
656          * @brief Returns the number of rows of a statement
657          *
658          * @param PDOStatement|mysqli_result|mysqli_stmt Statement object
659          * @return int Number of rows
660          */
661         public static function num_rows($stmt) {
662                 if (!is_object($stmt)) {
663                         return 0;
664                 }
665                 switch (self::$driver) {
666                         case 'pdo':
667                                 return $stmt->rowCount();
668                         case 'mysqli':
669                                 return $stmt->num_rows;
670                 }
671                 return 0;
672         }
673
674         /**
675          * @brief Fetch a single row
676          *
677          * @param mixed $stmt statement object
678          * @return array current row
679          */
680         public static function fetch($stmt) {
681                 $a = get_app();
682
683                 $stamp1 = microtime(true);
684
685                 $columns = [];
686
687                 if (!is_object($stmt)) {
688                         return false;
689                 }
690
691                 switch (self::$driver) {
692                         case 'pdo':
693                                 $columns = $stmt->fetch(PDO::FETCH_ASSOC);
694                                 break;
695                         case 'mysqli':
696                                 if (get_class($stmt) == 'mysqli_result') {
697                                         $columns = $stmt->fetch_assoc();
698                                         break;
699                                 }
700
701                                 // This code works, but is slow
702
703                                 // Bind the result to a result array
704                                 $cols = [];
705
706                                 $cols_num = [];
707                                 for ($x = 0; $x < $stmt->field_count; $x++) {
708                                         $cols[] = &$cols_num[$x];
709                                 }
710
711                                 call_user_func_array([$stmt, 'bind_result'], $cols);
712
713                                 if (!$stmt->fetch()) {
714                                         return false;
715                                 }
716
717                                 // The slow part:
718                                 // We need to get the field names for the array keys
719                                 // It seems that there is no better way to do this.
720                                 $result = $stmt->result_metadata();
721                                 $fields = $result->fetch_fields();
722
723                                 foreach ($cols_num AS $param => $col) {
724                                         $columns[$fields[$param]->name] = $col;
725                                 }
726                 }
727
728                 $a->save_timestamp($stamp1, 'database');
729
730                 return $columns;
731         }
732
733         /**
734          * @brief Insert a row into a table
735          *
736          * @param string $table Table name
737          * @param array $param parameter array
738          * @param bool $on_duplicate_update Do an update on a duplicate entry
739          *
740          * @return boolean was the insert successfull?
741          */
742         public static function insert($table, $param, $on_duplicate_update = false) {
743
744                 if (empty($table) || empty($param)) {
745                         logger('Table and fields have to be set');
746                         return false;
747                 }
748
749                 $sql = "INSERT INTO `".self::escape($table)."` (`".implode("`, `", array_keys($param))."`) VALUES (".
750                         substr(str_repeat("?, ", count($param)), 0, -2).")";
751
752                 if ($on_duplicate_update) {
753                         $sql .= " ON DUPLICATE KEY UPDATE `".implode("` = ?, `", array_keys($param))."` = ?";
754
755                         $values = array_values($param);
756                         $param = array_merge_recursive($values, $values);
757                 }
758
759                 return self::e($sql, $param);
760         }
761
762         /**
763          * @brief Fetch the id of the last insert command
764          *
765          * @return integer Last inserted id
766          */
767         public static function lastInsertId() {
768                 switch (self::$driver) {
769                         case 'pdo':
770                                 $id = self::$db->lastInsertId();
771                                 break;
772                         case 'mysqli':
773                                 $id = self::$db->insert_id;
774                                 break;
775                 }
776                 return $id;
777         }
778
779         /**
780          * @brief Locks a table for exclusive write access
781          *
782          * This function can be extended in the future to accept a table array as well.
783          *
784          * @param string $table Table name
785          *
786          * @return boolean was the lock successful?
787          */
788         public static function lock($table) {
789                 // See here: https://dev.mysql.com/doc/refman/5.7/en/lock-tables-and-transactions.html
790                 self::e("SET autocommit=0");
791                 $success = self::e("LOCK TABLES `".self::escape($table)."` WRITE");
792                 if (!$success) {
793                         self::e("SET autocommit=1");
794                 } else {
795                         self::$in_transaction = true;
796                 }
797                 return $success;
798         }
799
800         /**
801          * @brief Unlocks all locked tables
802          *
803          * @return boolean was the unlock successful?
804          */
805         public static function unlock() {
806                 // See here: https://dev.mysql.com/doc/refman/5.7/en/lock-tables-and-transactions.html
807                 self::e("COMMIT");
808                 $success = self::e("UNLOCK TABLES");
809                 self::e("SET autocommit=1");
810                 self::$in_transaction = false;
811                 return $success;
812         }
813
814         /**
815          * @brief Starts a transaction
816          *
817          * @return boolean Was the command executed successfully?
818          */
819         public static function transaction() {
820                 if (!self::e('COMMIT')) {
821                         return false;
822                 }
823                 if (!self::e('START TRANSACTION')) {
824                         return false;
825                 }
826                 self::$in_transaction = true;
827                 return true;
828         }
829
830         /**
831          * @brief Does a commit
832          *
833          * @return boolean Was the command executed successfully?
834          */
835         public static function commit() {
836                 if (!self::e('COMMIT')) {
837                         return false;
838                 }
839                 self::$in_transaction = false;
840                 return true;
841         }
842
843         /**
844          * @brief Does a rollback
845          *
846          * @return boolean Was the command executed successfully?
847          */
848         public static function rollback() {
849                 if (!self::e('ROLLBACK')) {
850                         return false;
851                 }
852                 self::$in_transaction = false;
853                 return true;
854         }
855
856         /**
857          * @brief Build the array with the table relations
858          *
859          * The array is build from the database definitions in DBStructure.php
860          *
861          * This process must only be started once, since the value is cached.
862          */
863         private static function buildRelationData() {
864                 $definition = DBStructure::definition();
865
866                 foreach ($definition AS $table => $structure) {
867                         foreach ($structure['fields'] AS $field => $field_struct) {
868                                 if (isset($field_struct['relation'])) {
869                                         foreach ($field_struct['relation'] AS $rel_table => $rel_field) {
870                                                 self::$relation[$rel_table][$rel_field][$table][] = $field;
871                                         }
872                                 }
873                         }
874                 }
875         }
876
877         /**
878          * @brief Delete a row from a table
879          *
880          * @param string  $table       Table name
881          * @param array   $conditions  Field condition(s)
882          * @param array   $options
883          *                - cascade: If true we delete records in other tables that depend on the one we're deleting through
884          *                           relations (default: true)
885          * @param boolean $in_process  Internal use: Only do a commit after the last delete
886          * @param array   $callstack   Internal use: prevent endless loops
887          *
888          * @return boolean|array was the delete successful? When $in_process is set: deletion data
889          */
890         public static function delete($table, array $conditions, array $options = [], $in_process = false, array &$callstack = [])
891         {
892                 if (empty($table) || empty($conditions)) {
893                         logger('Table and conditions have to be set');
894                         return false;
895                 }
896
897                 $commands = [];
898
899                 // Create a key for the loop prevention
900                 $key = $table . ':' . implode(':', array_keys($conditions)) . ':' . implode(':', $conditions);
901
902                 // We quit when this key already exists in the callstack.
903                 if (isset($callstack[$key])) {
904                         return $commands;
905                 }
906
907                 $callstack[$key] = true;
908
909                 $table = self::escape($table);
910
911                 $commands[$key] = ['table' => $table, 'conditions' => $conditions];
912
913                 $cascade = defaults($options, 'cascade', true);
914
915                 // To speed up the whole process we cache the table relations
916                 if ($cascade && count(self::$relation) == 0) {
917                         self::buildRelationData();
918                 }
919
920                 // Is there a relation entry for the table?
921                 if ($cascade && isset(self::$relation[$table])) {
922                         // We only allow a simple "one field" relation.
923                         $field = array_keys(self::$relation[$table])[0];
924                         $rel_def = array_values(self::$relation[$table])[0];
925
926                         // Create a key for preventing double queries
927                         $qkey = $field . '-' . $table . ':' . implode(':', array_keys($conditions)) . ':' . implode(':', $conditions);
928
929                         // When the search field is the relation field, we don't need to fetch the rows
930                         // This is useful when the leading record is already deleted in the frontend but the rest is done in the backend
931                         if ((count($conditions) == 1) && ($field == array_keys($conditions)[0])) {
932                                 foreach ($rel_def AS $rel_table => $rel_fields) {
933                                         foreach ($rel_fields AS $rel_field) {
934                                                 $retval = self::delete($rel_table, [$rel_field => array_values($conditions)[0]], $options, true, $callstack);
935                                                 $commands = array_merge($commands, $retval);
936                                         }
937                                 }
938                                 // We quit when this key already exists in the callstack.
939                         } elseif (!isset($callstack[$qkey])) {
940
941                                 $callstack[$qkey] = true;
942
943                                 // Fetch all rows that are to be deleted
944                                 $data = self::select($table, [$field], $conditions);
945
946                                 while ($row = self::fetch($data)) {
947                                         // Now we accumulate the delete commands
948                                         $retval = self::delete($table, [$field => $row[$field]], $options, true, $callstack);
949                                         $commands = array_merge($commands, $retval);
950                                 }
951
952                                 self::close($data);
953
954                                 // Since we had split the delete command we don't need the original command anymore
955                                 unset($commands[$key]);
956                         }
957                 }
958
959                 if (!$in_process) {
960                         // Now we finalize the process
961                         $do_transaction = !self::$in_transaction;
962
963                         if ($do_transaction) {
964                                 self::transaction();
965                         }
966
967                         $compacted = [];
968                         $counter = [];
969
970                         foreach ($commands AS $command) {
971                                 $conditions = $command['conditions'];
972                                 reset($conditions);
973                                 $first_key = key($conditions);
974
975                                 $condition_string = self::buildCondition($conditions);
976
977                                 if ((count($command['conditions']) > 1) || is_int($first_key)) {
978                                         $sql = "DELETE FROM `" . $command['table'] . "`" . $condition_string;
979                                         logger(self::replaceParameters($sql, $conditions), LOGGER_DATA);
980
981                                         if (!self::e($sql, $conditions)) {
982                                                 if ($do_transaction) {
983                                                         self::rollback();
984                                                 }
985                                                 return false;
986                                         }
987                                 } else {
988                                         $key_table = $command['table'];
989                                         $key_condition = array_keys($command['conditions'])[0];
990                                         $value = array_values($command['conditions'])[0];
991
992                                         // Split the SQL queries in chunks of 100 values
993                                         // We do the $i stuff here to make the code better readable
994                                         $i = $counter[$key_table][$key_condition];
995                                         if (isset($compacted[$key_table][$key_condition][$i]) && count($compacted[$key_table][$key_condition][$i]) > 100) {
996                                                 ++$i;
997                                         }
998
999                                         $compacted[$key_table][$key_condition][$i][$value] = $value;
1000                                         $counter[$key_table][$key_condition] = $i;
1001                                 }
1002                         }
1003                         foreach ($compacted AS $table => $values) {
1004                                 foreach ($values AS $field => $field_value_list) {
1005                                         foreach ($field_value_list AS $field_values) {
1006                                                 $sql = "DELETE FROM `" . $table . "` WHERE `" . $field . "` IN (" .
1007                                                         substr(str_repeat("?, ", count($field_values)), 0, -2) . ");";
1008
1009                                                 logger(self::replaceParameters($sql, $field_values), LOGGER_DATA);
1010
1011                                                 if (!self::e($sql, $field_values)) {
1012                                                         if ($do_transaction) {
1013                                                                 self::rollback();
1014                                                         }
1015                                                         return false;
1016                                                 }
1017                                         }
1018                                 }
1019                         }
1020                         if ($do_transaction) {
1021                                 self::commit();
1022                         }
1023                         return true;
1024                 }
1025
1026                 return $commands;
1027         }
1028
1029         /**
1030          * @brief Updates rows
1031          *
1032          * Updates rows in the database. When $old_fields is set to an array,
1033          * the system will only do an update if the fields in that array changed.
1034          *
1035          * Attention:
1036          * Only the values in $old_fields are compared.
1037          * This is an intentional behaviour.
1038          *
1039          * Example:
1040          * We include the timestamp field in $fields but not in $old_fields.
1041          * Then the row will only get the new timestamp when the other fields had changed.
1042          *
1043          * When $old_fields is set to a boolean value the system will do this compare itself.
1044          * When $old_fields is set to "true" the system will do an insert if the row doesn't exists.
1045          *
1046          * Attention:
1047          * Only set $old_fields to a boolean value when you are sure that you will update a single row.
1048          * When you set $old_fields to "true" then $fields must contain all relevant fields!
1049          *
1050          * @param string $table Table name
1051          * @param array $fields contains the fields that are updated
1052          * @param array $condition condition array with the key values
1053          * @param array|boolean $old_fields array with the old field values that are about to be replaced (true = update on duplicate)
1054          *
1055          * @return boolean was the update successfull?
1056          */
1057         public static function update($table, $fields, $condition, $old_fields = []) {
1058
1059                 if (empty($table) || empty($fields) || empty($condition)) {
1060                         logger('Table, fields and condition have to be set');
1061                         return false;
1062                 }
1063
1064                 $table = self::escape($table);
1065
1066                 $condition_string = self::buildCondition($condition);
1067
1068                 if (is_bool($old_fields)) {
1069                         $do_insert = $old_fields;
1070
1071                         $old_fields = self::selectFirst($table, [], $condition);
1072
1073                         if (is_bool($old_fields)) {
1074                                 if ($do_insert) {
1075                                         $values = array_merge($condition, $fields);
1076                                         return self::insert($table, $values, $do_insert);
1077                                 }
1078                                 $old_fields = [];
1079                         }
1080                 }
1081
1082                 $do_update = (count($old_fields) == 0);
1083
1084                 foreach ($old_fields AS $fieldname => $content) {
1085                         if (isset($fields[$fieldname])) {
1086                                 if ($fields[$fieldname] == $content) {
1087                                         unset($fields[$fieldname]);
1088                                 } else {
1089                                         $do_update = true;
1090                                 }
1091                         }
1092                 }
1093
1094                 if (!$do_update || (count($fields) == 0)) {
1095                         return true;
1096                 }
1097
1098                 $sql = "UPDATE `".$table."` SET `".
1099                         implode("` = ?, `", array_keys($fields))."` = ?".$condition_string;
1100
1101                 $params1 = array_values($fields);
1102                 $params2 = array_values($condition);
1103                 $params = array_merge_recursive($params1, $params2);
1104
1105                 return self::e($sql, $params);
1106         }
1107
1108         /**
1109          * Retrieve a single record from a table and returns it in an associative array
1110          *
1111          * @brief Retrieve a single record from a table
1112          * @param string $table
1113          * @param array  $fields
1114          * @param array  $condition
1115          * @param array  $params
1116          * @return bool|array
1117          * @see dba::select
1118          */
1119         public static function selectFirst($table, array $fields = [], array $condition = [], $params = [])
1120         {
1121                 $params['limit'] = 1;
1122                 $result = self::select($table, $fields, $condition, $params);
1123
1124                 if (is_bool($result)) {
1125                         return $result;
1126                 } else {
1127                         $row = self::fetch($result);
1128                         self::close($result);
1129                         return $row;
1130                 }
1131         }
1132
1133         /**
1134          * @brief Select rows from a table
1135          *
1136          * @param string $table     Table name
1137          * @param array  $fields    Array of selected fields, empty for all
1138          * @param array  $condition Array of fields for condition
1139          * @param array  $params    Array of several parameters
1140          *
1141          * @return boolean|object
1142          *
1143          * Example:
1144          * $table = "item";
1145          * $fields = array("id", "uri", "uid", "network");
1146          *
1147          * $condition = array("uid" => 1, "network" => 'dspr');
1148          * or:
1149          * $condition = array("`uid` = ? AND `network` IN (?, ?)", 1, 'dfrn', 'dspr');
1150          *
1151          * $params = array("order" => array("id", "received" => true), "limit" => 10);
1152          *
1153          * $data = dba::select($table, $fields, $condition, $params);
1154          */
1155         public static function select($table, array $fields = [], array $condition = [], array $params = [])
1156         {
1157                 if ($table == '') {
1158                         return false;
1159                 }
1160
1161                 $table = self::escape($table);
1162
1163                 if (count($fields) > 0) {
1164                         $select_fields = "`" . implode("`, `", array_values($fields)) . "`";
1165                 } else {
1166                         $select_fields = "*";
1167                 }
1168
1169                 $condition_string = self::buildCondition($condition);
1170
1171                 $param_string = self::buildParameter($params);
1172
1173                 $sql = "SELECT " . $select_fields . " FROM `" . $table . "`" . $condition_string . $param_string;
1174
1175                 $result = self::p($sql, $condition);
1176
1177                 return $result;
1178         }
1179
1180         /**
1181          * @brief Counts the rows from a table satisfying the provided condition
1182          *
1183          * @param string $table Table name
1184          * @param array $condition array of fields for condition
1185          *
1186          * @return int
1187          *
1188          * Example:
1189          * $table = "item";
1190          *
1191          * $condition = ["uid" => 1, "network" => 'dspr'];
1192          * or:
1193          * $condition = ["`uid` = ? AND `network` IN (?, ?)", 1, 'dfrn', 'dspr'];
1194          *
1195          * $count = dba::count($table, $condition);
1196          */
1197         public static function count($table, array $condition = [])
1198         {
1199                 if ($table == '') {
1200                         return false;
1201                 }
1202
1203                 $condition_string = self::buildCondition($condition);
1204
1205                 $sql = "SELECT COUNT(*) AS `count` FROM `".$table."`".$condition_string;
1206
1207                 $row = self::fetch_first($sql, $condition);
1208
1209                 return $row['count'];
1210         }
1211
1212         /**
1213          * @brief Returns the SQL condition string built from the provided condition array
1214          *
1215          * This function operates with two modes.
1216          * - Supplied with a filed/value associative array, it builds simple strict
1217          *   equality conditions linked by AND.
1218          * - Supplied with a flat list, the first element is the condition string and
1219          *   the following arguments are the values to be interpolated
1220          *
1221          * $condition = ["uid" => 1, "network" => 'dspr'];
1222          * or:
1223          * $condition = ["`uid` = ? AND `network` IN (?, ?)", 1, 'dfrn', 'dspr'];
1224          *
1225          * In either case, the provided array is left with the parameters only
1226          *
1227          * @param array $condition
1228          * @return string
1229          */
1230         public static function buildCondition(array &$condition = [])
1231         {
1232                 $condition_string = '';
1233                 if (count($condition) > 0) {
1234                         reset($condition);
1235                         $first_key = key($condition);
1236                         if (is_int($first_key)) {
1237                                 $condition_string = " WHERE (" . array_shift($condition) . ")";
1238                         } else {
1239                                 $new_values = [];
1240                                 $condition_string = "";
1241                                 foreach ($condition as $field => $value) {
1242                                         if ($condition_string != "") {
1243                                                 $condition_string .= " AND ";
1244                                         }
1245                                         if (is_array($value)) {
1246                                                 $new_values = array_merge($new_values, array_values($value));
1247                                                 $placeholders = substr(str_repeat("?, ", count($value)), 0, -2);
1248                                                 $condition_string .= "`" . $field . "` IN (" . $placeholders . ")";
1249                                         } else {
1250                                                 $new_values[$field] = $value;
1251                                                 $condition_string .= "`" . $field . "` = ?";
1252                                         }
1253                                 }
1254                                 $condition_string = " WHERE (" . $condition_string . ")";
1255                                 $condition = $new_values;
1256                         }
1257                 }
1258
1259                 return $condition_string;
1260         }
1261
1262         /**
1263          * @brief Returns the SQL parameter string built from the provided parameter array
1264          *
1265          * @param array $params
1266          * @return string
1267          */
1268         public static function buildParameter(array $params = [])
1269         {
1270                 $order_string = '';
1271                 if (isset($params['order'])) {
1272                         $order_string = " ORDER BY ";
1273                         foreach ($params['order'] AS $fields => $order) {
1274                                 if (!is_int($fields)) {
1275                                         $order_string .= "`" . $fields . "` " . ($order ? "DESC" : "ASC") . ", ";
1276                                 } else {
1277                                         $order_string .= "`" . $order . "`, ";
1278                                 }
1279                         }
1280                         $order_string = substr($order_string, 0, -2);
1281                 }
1282
1283                 $limit_string = '';
1284                 if (isset($params['limit']) && is_int($params['limit'])) {
1285                         $limit_string = " LIMIT " . $params['limit'];
1286                 }
1287
1288                 if (isset($params['limit']) && is_array($params['limit'])) {
1289                         $limit_string = " LIMIT " . intval($params['limit'][0]) . ", " . intval($params['limit'][1]);
1290                 }
1291
1292                 return $order_string.$limit_string;
1293         }
1294
1295         /**
1296          * @brief Fills an array with data from a query
1297          *
1298          * @param object $stmt statement object
1299          * @return array Data array
1300          */
1301         public static function inArray($stmt, $do_close = true) {
1302                 if (is_bool($stmt)) {
1303                         return $stmt;
1304                 }
1305
1306                 $data = [];
1307                 while ($row = self::fetch($stmt)) {
1308                         $data[] = $row;
1309                 }
1310                 if ($do_close) {
1311                         self::close($stmt);
1312                 }
1313                 return $data;
1314         }
1315
1316         /**
1317          * @brief Returns the error number of the last query
1318          *
1319          * @return string Error number (0 if no error)
1320          */
1321         public static function errorNo() {
1322                 return self::$errorno;
1323         }
1324
1325         /**
1326          * @brief Returns the error message of the last query
1327          *
1328          * @return string Error message ('' if no error)
1329          */
1330         public static function errorMessage() {
1331                 return self::$error;
1332         }
1333
1334         /**
1335          * @brief Closes the current statement
1336          *
1337          * @param object $stmt statement object
1338          * @return boolean was the close successful?
1339          */
1340         public static function close($stmt) {
1341                 $a = get_app();
1342
1343                 $stamp1 = microtime(true);
1344
1345                 if (!is_object($stmt)) {
1346                         return false;
1347                 }
1348
1349                 switch (self::$driver) {
1350                         case 'pdo':
1351                                 $ret = $stmt->closeCursor();
1352                                 break;
1353                         case 'mysqli':
1354                                 $stmt->free_result();
1355                                 $ret = $stmt->close();
1356                                 break;
1357                 }
1358
1359                 $a->save_timestamp($stamp1, 'database');
1360
1361                 return $ret;
1362         }
1363 }
1364
1365 function dbesc($str) {
1366         if (dba::$connected) {
1367                 return(dba::escape($str));
1368         } else {
1369                 return(str_replace("'","\\'",$str));
1370         }
1371 }
1372
1373 /**
1374  * @brief execute SQL query with printf style args - deprecated
1375  *
1376  * Please use the dba:: functions instead:
1377  * dba::select, dba::exists, dba::insert
1378  * dba::delete, dba::update, dba::p, dba::e
1379  *
1380  * @param $args Query parameters (1 to N parameters of different types)
1381  * @return array|bool Query array
1382  */
1383 function q($sql) {
1384         $args = func_get_args();
1385         unset($args[0]);
1386
1387         if (!dba::$connected) {
1388                 return false;
1389         }
1390
1391         $sql = dba::clean_query($sql);
1392         $sql = dba::any_value_fallback($sql);
1393
1394         $stmt = @vsprintf($sql, $args);
1395
1396         $ret = dba::p($stmt);
1397
1398         if (is_bool($ret)) {
1399                 return $ret;
1400         }
1401
1402         $columns = dba::columnCount($ret);
1403
1404         $data = dba::inArray($ret);
1405
1406         if ((count($data) == 0) && ($columns == 0)) {
1407                 return true;
1408         }
1409
1410         return $data;
1411 }
1412
1413 function dba_timer() {
1414         return microtime(true);
1415 }