2 require_once("dbm.php");
3 require_once('include/datetime.php');
6 * @class MySQL database class
8 * For debugging, insert 'dbg(1);' anywhere in the program flow.
9 * dbg(0); will turn it off. Logging is performed at LOGGER_DATA level.
10 * When logging, all binary info is converted to text and html entities are escaped so that
11 * the debugging stream is safe to view within both terminals and web pages.
13 * This class is for the low level database stuff that does driver specific things.
22 public $connected = false;
23 public $error = false;
24 private $_server_info = '';
26 private static $relation = array();
28 function __construct($serveraddr, $user, $pass, $db, $install = false) {
31 $stamp1 = microtime(true);
33 $serveraddr = trim($serveraddr);
35 $serverdata = explode(':', $serveraddr);
36 $server = $serverdata[0];
38 if (count($serverdata) > 1) {
39 $port = trim($serverdata[1]);
42 $server = trim($server);
47 if (!(strlen($server) && strlen($user))) {
48 $this->connected = false;
54 if (strlen($server) && ($server !== 'localhost') && ($server !== '127.0.0.1')) {
55 if (! dns_get_record($server, DNS_A + DNS_CNAME + DNS_PTR)) {
56 $this->error = sprintf(t('Cannot locate DNS info for database server \'%s\''), $server);
57 $this->connected = false;
64 if (class_exists('\PDO') && in_array('mysql', PDO::getAvailableDrivers())) {
65 $this->driver = 'pdo';
66 $connect = "mysql:host=".$server.";dbname=".$db;
69 $connect .= ";port=".$port;
72 if (isset($a->config["system"]["db_charset"])) {
73 $connect .= ";charset=".$a->config["system"]["db_charset"];
75 $this->db = @new PDO($connect, $user, $pass);
76 if (!$this->db->errorCode()) {
77 $this->connected = true;
79 } elseif (class_exists('mysqli')) {
80 $this->driver = 'mysqli';
81 $this->db = @new mysqli($server, $user, $pass, $db, $port);
82 if (!mysqli_connect_errno()) {
83 $this->connected = true;
85 if (isset($a->config["system"]["db_charset"])) {
86 $this->db->set_charset($a->config["system"]["db_charset"]);
89 } elseif (function_exists('mysql_connect')) {
90 $this->driver = 'mysql';
91 $this->db = mysql_connect($serveraddr, $user, $pass);
92 if ($this->db && mysql_select_db($db, $this->db)) {
93 $this->connected = true;
95 if (isset($a->config["system"]["db_charset"])) {
96 mysql_set_charset($a->config["system"]["db_charset"], $this->db);
100 // No suitable SQL driver was found.
102 system_unavailable();
106 if (!$this->connected) {
109 system_unavailable();
112 $a->save_timestamp($stamp1, "network");
118 * @brief Returns the MySQL server version string
120 * This function discriminate between the deprecated mysql API and the current
121 * object-oriented mysqli API. Example of returned string: 5.5.46-0+deb8u1
125 public function server_info() {
126 if ($this->_server_info == '') {
127 switch ($this->driver) {
129 $this->_server_info = $this->db->getAttribute(PDO::ATTR_SERVER_VERSION);
132 $this->_server_info = $this->db->server_info;
135 $this->_server_info = mysql_get_server_info($this->db);
139 return $this->_server_info;
143 * @brief Returns the selected database name
147 public function database_name() {
148 $r = $this->q("SELECT DATABASE() AS `db`");
154 * @brief Analyze a database query and log this if some conditions are met.
156 * @param string $query The database query that will be analyzed
158 public function log_index($query) {
161 if ($a->config["system"]["db_log_index"] == "") {
165 // Don't explain an explain statement
166 if (strtolower(substr($query, 0, 7)) == "explain") {
170 // Only do the explain on "select", "update" and "delete"
171 if (!in_array(strtolower(substr($query, 0, 6)), array("select", "update", "delete"))) {
175 $r = $this->q("EXPLAIN ".$query);
176 if (!dbm::is_result($r)) {
180 $watchlist = explode(',', $a->config["system"]["db_log_index_watch"]);
181 $blacklist = explode(',', $a->config["system"]["db_log_index_blacklist"]);
183 foreach ($r AS $row) {
184 if ((intval($a->config["system"]["db_loglimit_index"]) > 0)) {
185 $log = (in_array($row['key'], $watchlist) AND
186 ($row['rows'] >= intval($a->config["system"]["db_loglimit_index"])));
191 if ((intval($a->config["system"]["db_loglimit_index_high"]) > 0) AND ($row['rows'] >= intval($a->config["system"]["db_loglimit_index_high"]))) {
195 if (in_array($row['key'], $blacklist) OR ($row['key'] == "")) {
200 $backtrace = debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS);
201 @file_put_contents($a->config["system"]["db_log_index"], datetime_convert()."\t".
202 $row['key']."\t".$row['rows']."\t".$row['Extra']."\t".
203 basename($backtrace[1]["file"])."\t".
204 $backtrace[1]["line"]."\t".$backtrace[2]["function"]."\t".
205 substr($query, 0, 2000)."\n", FILE_APPEND);
210 public function q($sql, $onlyquery = false) {
213 if (!$this->db || !$this->connected) {
219 $connstr = ($this->connected() ? "Connected" : "Disonnected");
221 $stamp1 = microtime(true);
225 if (x($a->config,'system') && x($a->config['system'], 'db_callstack')) {
226 $sql = "/*".$a->callstack()." */ ".$sql;
231 switch ($this->driver) {
233 $result = @$this->db->query($sql);
234 // Is used to separate between queries that returning data - or not
235 if (!is_bool($result)) {
236 $columns = $result->columnCount();
240 $result = @$this->db->query($sql);
243 $result = @mysql_query($sql,$this->db);
246 $stamp2 = microtime(true);
247 $duration = (float)($stamp2 - $stamp1);
249 $a->save_timestamp($stamp1, "database");
251 if (strtolower(substr($orig_sql, 0, 6)) != "select") {
252 $a->save_timestamp($stamp1, "database_write");
254 if (x($a->config,'system') && x($a->config['system'],'db_log')) {
255 if (($duration > $a->config["system"]["db_loglimit"])) {
256 $duration = round($duration, 3);
257 $backtrace = debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS);
258 @file_put_contents($a->config["system"]["db_log"], datetime_convert()."\t".$duration."\t".
259 basename($backtrace[1]["file"])."\t".
260 $backtrace[1]["line"]."\t".$backtrace[2]["function"]."\t".
261 substr($sql, 0, 2000)."\n", FILE_APPEND);
265 switch ($this->driver) {
267 $errorInfo = $this->db->errorInfo();
269 $this->error = $errorInfo[2];
270 $this->errorno = $errorInfo[1];
274 if ($this->db->errno) {
275 $this->error = $this->db->error;
276 $this->errorno = $this->db->errno;
280 if (mysql_errno($this->db)) {
281 $this->error = mysql_error($this->db);
282 $this->errorno = mysql_errno($this->db);
286 if (strlen($this->error)) {
287 logger('DB Error ('.$connstr.') '.$this->errorno.': '.$this->error);
294 if ($result === false) {
296 } elseif ($result === true) {
299 switch ($this->driver) {
301 $mesg = $result->rowCount().' results'.EOL;
304 $mesg = $result->num_rows.' results'.EOL;
307 $mesg = mysql_num_rows($result).' results'.EOL;
312 $str = 'SQL = ' . printable($sql) . EOL . 'SQL returned ' . $mesg
313 . (($this->error) ? ' error: ' . $this->error : '')
316 logger('dba: ' . $str );
320 * If dbfail.out exists, we will write any failed calls directly to it,
321 * regardless of any logging that may or may nor be in effect.
322 * These usually indicate SQL syntax errors that need to be resolved.
325 if ($result === false) {
326 logger('dba: ' . printable($sql) . ' returned false.' . "\n" . $this->error);
327 if (file_exists('dbfail.out')) {
328 file_put_contents('dbfail.out', datetime_convert() . "\n" . printable($sql) . ' returned false' . "\n" . $this->error . "\n", FILE_APPEND);
332 if (is_bool($result)) {
336 $this->result = $result;
341 switch ($this->driver) {
343 while ($x = $result->fetch(PDO::FETCH_ASSOC)) {
346 $result->closeCursor();
349 while ($x = $result->fetch_array(MYSQLI_ASSOC)) {
352 $result->free_result();
355 while ($x = mysql_fetch_array($result, MYSQL_ASSOC)) {
358 mysql_free_result($result);
362 // PDO doesn't return "true" on successful operations - like mysqli does
363 // Emulate this behaviour by checking if the query returned data and had columns
364 // This should be reliable enough
365 if (($this->driver == 'pdo') AND (count($r) == 0) AND ($columns == 0)) {
369 //$a->save_timestamp($stamp1, "database");
372 logger('dba: ' . printable(print_r($r, true)));
377 public function dbg($dbg) {
381 public function escape($str) {
382 if ($this->db && $this->connected) {
383 switch ($this->driver) {
385 return substr(@$this->db->quote($str, PDO::PARAM_STR), 1, -1);
387 return @$this->db->real_escape_string($str);
389 return @mysql_real_escape_string($str,$this->db);
394 function connected() {
395 switch ($this->driver) {
397 // Not sure if this really is working like expected
398 $connected = ($this->db->getAttribute(PDO::ATTR_CONNECTION_STATUS) != "");
401 $connected = $this->db->ping();
404 $connected = mysql_ping($this->db);
410 function insert_id() {
411 switch ($this->driver) {
413 $id = $this->db->lastInsertId();
416 $id = $this->db->insert_id;
419 $id = mysql_insert_id($this->db);
425 function __destruct() {
427 switch ($this->driver) {
435 mysql_close($this->db);
442 * @brief Replaces ANY_VALUE() function by MIN() function,
443 * if the database server does not support ANY_VALUE().
445 * Considerations for Standard SQL, or MySQL with ONLY_FULL_GROUP_BY (default since 5.7.5).
446 * ANY_VALUE() is available from MySQL 5.7.5 https://dev.mysql.com/doc/refman/5.7/en/miscellaneous-functions.html
447 * A standard fall-back is to use MIN().
449 * @param string $sql An SQL string without the values
450 * @return string The input SQL string modified if necessary.
452 public function any_value_fallback($sql) {
453 $server_info = $this->server_info();
454 if (version_compare($server_info, '5.7.5', '<') ||
455 (stripos($server_info, 'MariaDB') !== false)) {
456 $sql = str_ireplace('ANY_VALUE(', 'MIN(', $sql);
462 * @brief Replaces the ? placeholders with the parameters in the $args array
464 * @param string $sql SQL query
465 * @param array $args The parameters that are to replace the ? placeholders
466 * @return string The replaced SQL query
468 static private function replace_parameters($sql, $args) {
470 foreach ($args AS $param => $value) {
471 if (is_int($args[$param]) OR is_float($args[$param])) {
472 $replace = intval($args[$param]);
474 $replace = "'".self::$dbo->escape($args[$param])."'";
477 $pos = strpos($sql, '?', $offset);
478 if ($pos !== false) {
479 $sql = substr_replace($sql, $replace, $pos, 1);
481 $offset = $pos + strlen($replace);
487 * @brief Executes a prepared statement that returns data
488 * @usage Example: $r = p("SELECT * FROM `item` WHERE `guid` = ?", $guid);
489 * @param string $sql SQL statement
490 * @return object statement object
492 static public function p($sql) {
495 $stamp1 = microtime(true);
497 $args = func_get_args();
500 // When the second function parameter is an array then use this as the parameter array
501 if ((count($args) > 0) AND (is_array($args[1]))) {
507 // Renumber the array keys to be sure that they fit
510 foreach ($params AS $param) {
511 $args[++$i] = $param;
514 if (!self::$dbo OR !self::$dbo->connected) {
518 if (substr_count($sql, '?') != count($args)) {
519 // Question: Should we continue or stop the query here?
520 logger('Parameter mismatch. Query "'.$sql.'" - Parameters '.print_r($args, true), LOGGER_DEBUG);
523 $sql = self::$dbo->any_value_fallback($sql);
525 if (x($a->config,'system') && x($a->config['system'], 'db_callstack')) {
526 $sql = "/*".$a->callstack()." */ ".$sql;
529 self::$dbo->error = '';
530 self::$dbo->errorno = 0;
532 switch (self::$dbo->driver) {
534 if (!$stmt = self::$dbo->db->prepare($sql)) {
535 $errorInfo = self::$dbo->db->errorInfo();
536 self::$dbo->error = $errorInfo[2];
537 self::$dbo->errorno = $errorInfo[1];
542 foreach ($args AS $param => $value) {
543 $stmt->bindParam($param, $args[$param]);
546 if (!$stmt->execute()) {
547 $errorInfo = $stmt->errorInfo();
548 self::$dbo->error = $errorInfo[2];
549 self::$dbo->errorno = $errorInfo[1];
556 $stmt = self::$dbo->db->stmt_init();
558 if (!$stmt->prepare($sql)) {
559 self::$dbo->error = $stmt->error;
560 self::$dbo->errorno = $stmt->errno;
567 foreach ($args AS $param => $value) {
568 if (is_int($args[$param])) {
570 } elseif (is_float($args[$param])) {
572 } elseif (is_string($args[$param])) {
577 $values[] = &$args[$param];
580 if (count($values) > 0) {
581 array_unshift($values, $params);
582 call_user_func_array(array($stmt, 'bind_param'), $values);
585 if (!$stmt->execute()) {
586 self::$dbo->error = self::$dbo->db->error;
587 self::$dbo->errorno = self::$dbo->db->errno;
590 $stmt->store_result();
595 // For the old "mysql" functions we cannot use prepared statements
596 $retval = mysql_query(self::replace_parameters($sql, $args), self::$dbo->db);
597 if (mysql_errno(self::$dbo->db)) {
598 self::$dbo->error = mysql_error(self::$dbo->db);
599 self::$dbo->errorno = mysql_errno(self::$dbo->db);
604 if (self::$dbo->errorno != 0) {
605 logger('DB Error '.self::$dbo->errorno.': '.self::$dbo->error."\n".self::replace_parameters($sql, $args));
608 $a->save_timestamp($stamp1, 'database');
610 if (x($a->config,'system') && x($a->config['system'], 'db_log')) {
612 $stamp2 = microtime(true);
613 $duration = (float)($stamp2 - $stamp1);
615 if (($duration > $a->config["system"]["db_loglimit"])) {
616 $duration = round($duration, 3);
617 $backtrace = debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS);
619 @file_put_contents($a->config["system"]["db_log"], datetime_convert()."\t".$duration."\t".
620 basename($backtrace[1]["file"])."\t".
621 $backtrace[1]["line"]."\t".$backtrace[2]["function"]."\t".
622 substr(self::replace_parameters($sql, $args), 0, 2000)."\n", FILE_APPEND);
629 * @brief Executes a prepared statement like UPDATE or INSERT that doesn't return data
631 * @param string $sql SQL statement
632 * @return boolean Was the query successfull? False is returned only if an error occurred
634 static public function e($sql) {
637 $stamp = microtime(true);
639 $args = func_get_args();
641 $stmt = call_user_func_array('self::p', $args);
643 if (is_bool($stmt)) {
645 } elseif (is_object($stmt)) {
653 $a->save_timestamp($stamp, "database_write");
659 * @brief Check if data exists
661 * @param string $sql SQL statement
662 * @return boolean Are there rows for that query?
664 static public function exists($sql) {
665 $args = func_get_args();
667 $stmt = call_user_func_array('self::p', $args);
669 if (is_bool($stmt)) {
672 $retval = (self::num_rows($stmt) > 0);
681 * @brief Fetches the first row
683 * @param string $sql SQL statement
684 * @return array first row of query
686 static public function fetch_first($sql) {
687 $args = func_get_args();
689 $stmt = call_user_func_array('self::p', $args);
691 if (is_bool($stmt)) {
694 $retval = self::fetch($stmt);
703 * @brief Returns the number of rows of a statement
705 * @param object Statement object
706 * @return int Number of rows
708 static public function num_rows($stmt) {
709 switch (self::$dbo->driver) {
711 return $stmt->rowCount();
713 return $stmt->num_rows;
715 return mysql_num_rows($stmt);
721 * @brief Fetch a single row
723 * @param object $stmt statement object
724 * @return array current row
726 static public function fetch($stmt) {
727 if (!is_object($stmt)) {
731 switch (self::$dbo->driver) {
733 return $stmt->fetch(PDO::FETCH_ASSOC);
735 // This code works, but is slow
737 // Bind the result to a result array
741 for ($x = 0; $x < $stmt->field_count; $x++) {
742 $cols[] = &$cols_num[$x];
745 call_user_func_array(array($stmt, 'bind_result'), $cols);
747 if (!$stmt->fetch()) {
752 // We need to get the field names for the array keys
753 // It seems that there is no better way to do this.
754 $result = $stmt->result_metadata();
755 $fields = $result->fetch_fields();
758 foreach ($cols_num AS $param => $col) {
759 $columns[$fields[$param]->name] = $col;
763 return mysql_fetch_array(self::$dbo->result, MYSQL_ASSOC);
768 * @brief Insert a row into a table
770 * @param string $table Table name
771 * @param array $param parameter array
773 * @return boolean was the insert successfull?
775 static public function insert($table, $param) {
776 $sql = "INSERT INTO `".self::$dbo->escape($table)."` (`".implode("`, `", array_keys($param))."`) VALUES (".
777 substr(str_repeat("?, ", count($param)), 0, -2).");";
779 return self::e($sql, $param);
783 * @brief Build the array with the table relations
785 * The array is build from the database definitions in dbstructure.php
787 * This process must only be started once, since the value is cached.
789 static private function build_relation_data() {
790 $definition = db_definition();
792 foreach ($definition AS $table => $structure) {
793 foreach ($structure['fields'] AS $field => $field_struct) {
794 if (isset($field_struct['relation'])) {
795 foreach ($field_struct['relation'] AS $rel_table => $rel_field) {
796 self::$relation[$rel_table][$rel_field][$table][] = $field;
804 * @brief Insert a row into a table
806 * @param string $table Table name
807 * @param array $param parameter array
808 * @param boolean $in_commit Internal use: Only do a commit after the last delete
809 * @param array $callstack Internal use: prevent endless loops
811 * @return boolean|array was the delete successfull? When $in_commit is set: deletion data
813 static public function delete($table, $param, $in_commit = false, &$callstack = array()) {
817 // Create a key for the loop prevention
818 $key = $table.':'.implode(':', array_keys($param)).':'.implode(':', $param);
820 // We quit when this key already exists in the callstack.
821 if (isset($callstack[$key])) {
825 $callstack[$key] = true;
827 $table = self::$dbo->escape($table);
829 $commands[$key] = array('table' => $table, 'param' => $param);
831 // To speed up the whole process we cache the table relations
832 if (count(self::$relation) == 0) {
833 self::build_relation_data();
836 // Is there a relation entry for the table?
837 if (isset(self::$relation[$table])) {
838 // We only allow a simple "one field" relation.
839 $field = array_keys(self::$relation[$table])[0];
840 $rel_def = array_values(self::$relation[$table])[0];
842 // Create a key for preventing double queries
843 $qkey = $field.'-'.$table.':'.implode(':', array_keys($param)).':'.implode(':', $param);
845 // When the search field is the relation field, we don't need to fetch the rows
846 // This is useful when the leading record is already deleted in the frontend but the rest is done in the backend
847 if ((count($param) == 1) AND ($field == array_keys($param)[0])) {
848 foreach ($rel_def AS $rel_table => $rel_fields) {
849 foreach ($rel_fields AS $rel_field) {
850 $retval = self::delete($rel_table, array($rel_field => array_values($param)[0]), true, $callstack);
851 $commands = array_merge($commands, $retval);
854 // We quit when this key already exists in the callstack.
855 } elseif (!isset($callstack[$qkey])) {
857 $callstack[$qkey] = true;
859 // Fetch all rows that are to be deleted
860 $sql = "SELECT ".self::$dbo->escape($field)." FROM `".$table."` WHERE `".
861 implode("` = ? AND `", array_keys($param))."` = ?";
863 $data = self::p($sql, $param);
864 while ($row = self::fetch($data)) {
865 // Now we accumulate the delete commands
866 $retval = self::delete($table, array($field => $row[$field]), true, $callstack);
867 $commands = array_merge($commands, $retval);
870 // Since we had split the delete command we don't need the original command anymore
871 unset($commands[$key]);
876 // Now we finalize the process
878 self::p("START TRANSACTION");
880 $compacted = array();
882 foreach ($commands AS $command) {
883 if (count($command['param']) > 1) {
884 $sql = "DELETE FROM `".$command['table']."` WHERE `".
885 implode("` = ? AND `", array_keys($command['param']))."` = ?";
887 logger(dba::replace_parameters($sql, $command['param']), LOGGER_DATA);
889 if (!self::e($sql, $command['param'])) {
894 $key_table = $command['table'];
895 $key_param = array_keys($command['param'])[0];
896 $value = array_values($command['param'])[0];
898 // Split the SQL queries in chunks of 100 values
899 // We do the $i stuff here to make the code better readable
900 $i = $counter[$key_table][$key_param];
901 if (count($compacted[$key_table][$key_param][$i]) > 100) {
905 $compacted[$key_table][$key_param][$i][$value] = $value;
906 $counter[$key_table][$key_param] = $i;
909 foreach ($compacted AS $table => $values) {
910 foreach ($values AS $field => $field_value_list) {
911 foreach ($field_value_list AS $field_values) {
912 $sql = "DELETE FROM `".$table."` WHERE `".$field."` IN (".
913 substr(str_repeat("?, ", count($field_values)), 0, -2).");";
915 logger(dba::replace_parameters($sql, $field_values), LOGGER_DATA);
917 if (!self::e($sql, $field_values)) {
932 * @brief Updates rows
934 * Updates rows in the database. When $old_fields is set to an array,
935 * the system will only do an update if the fields in that array changed.
938 * Only the values in $old_fields are compared.
939 * This is an intentional behaviour.
942 * We include the timestamp field in $fields but not in $old_fields.
943 * Then the row will only get the new timestamp when the other fields had changed.
945 * When $old_fields is set to a boolean value the system will do this compare itself.
946 * When $old_fields is set to "true" the system will do an insert if the row doesn't exists.
949 * Only set $old_fields to a boolean value when you are sure that you will update a single row.
950 * When you set $old_fields to "true" then $fields must contain all relevant fields!
952 * @param string $table Table name
953 * @param array $fields contains the fields that are updated
954 * @param array $condition condition array with the key values
955 * @param array|boolean $old_fields array with the old field values that are about to be replaced
957 * @return boolean was the update successfull?
959 static public function update($table, $fields, $condition, $old_fields = array()) {
961 /** @todo We may use MySQL specific functions here:
962 * INSERT INTO `config` (`cat`, `k`, `v`) VALUES ('%s', '%s', '%s') ON DUPLICATE KEY UPDATE `v` = '%s'"
963 * But I think that it doesn't make sense here.
966 $table = self::$dbo->escape($table);
968 if (is_bool($old_fields)) {
969 $sql = "SELECT * FROM `".$table."` WHERE `".
970 implode("` = ? AND `", array_keys($condition))."` = ? LIMIT 1";
973 foreach ($condition AS $value) {
977 $do_insert = $old_fields;
979 $old_fields = self::fetch_first($sql, $params);
980 if (is_bool($old_fields)) {
982 return self::insert($table, $fields);
984 $old_fields = array();
988 $do_update = (count($old_fields) == 0);
990 foreach ($old_fields AS $fieldname => $content) {
991 if (isset($fields[$fieldname])) {
992 if ($fields[$fieldname] == $content) {
993 unset($fields[$fieldname]);
1000 if (!$do_update OR (count($fields) == 0)) {
1004 $sql = "UPDATE `".$table."` SET `".
1005 implode("` = ?, `", array_keys($fields))."` = ? WHERE `".
1006 implode("` = ? AND `", array_keys($condition))."` = ?";
1009 foreach ($fields AS $value) {
1012 foreach ($condition AS $value) {
1016 return self::e($sql, $params);
1020 * @brief Select rows from a table
1022 * @param string $table Table name
1023 * @param array $fields array of selected fields
1024 * @param array $condition array of fields for condition
1025 * @param array $params array of several parameters
1027 * @return boolean|object If "limit" is equal "1" only a single row is returned, else a query object is returned
1031 * $fields = array("id", "uri", "uid", "network");
1032 * $condition = array("uid" => 1, "network" => 'dspr');
1033 * $params = array("order" => array("id", "received" => true), "limit" => 1);
1035 * $data = dba::select($table, $fields, $condition, $params);
1037 static public function select($table, $fields = array(), $condition = array(), $params = array()) {
1042 if (count($fields) > 0) {
1043 $select_fields = "`".implode("`, `", array_values($fields))."`";
1045 $select_fields = "*";
1048 if (count($condition) > 0) {
1049 $condition_string = " WHERE `".implode("` = ? AND `", array_keys($condition))."` = ?";
1051 $condition_string = "";
1055 $single_row = false;
1057 if (isset($params['order'])) {
1058 $param_string .= " ORDER BY ";
1059 foreach ($params['order'] AS $fields => $order) {
1060 if (!is_int($fields)) {
1061 $param_string .= "`".$fields."` ".($order ? "DESC" : "ASC").", ";
1063 $param_string .= "`".$order."`, ";
1066 $param_string = substr($param_string, 0, -2);
1069 if (isset($params['limit'])) {
1070 if (is_int($params['limit'])) {
1071 $param_string .= " LIMIT ".$params['limit'];
1072 $single_row =($params['limit'] == 1);
1076 $sql = "SELECT ".$select_fields." FROM `".$table."`".$condition_string.$param_string;
1078 $result = self::p($sql, $condition);
1080 if (is_bool($result) OR !$single_row) {
1083 $row = self::fetch($result);
1084 self::close($result);
1090 * @brief Closes the current statement
1092 * @param object $stmt statement object
1093 * @return boolean was the close successfull?
1095 static public function close($stmt) {
1096 if (!is_object($stmt)) {
1100 switch (self::$dbo->driver) {
1102 return $stmt->closeCursor();
1104 return $stmt->free_result();
1105 return $stmt->close();
1107 return mysql_free_result($stmt);
1112 function printable($s) {
1113 $s = preg_replace("~([\x01-\x08\x0E-\x0F\x10-\x1F\x7F-\xFF])~",".", $s);
1114 $s = str_replace("\x00",'.',$s);
1115 if (x($_SERVER,'SERVER_NAME')) {
1116 $s = escape_tags($s);
1121 // Procedural functions
1122 function dbg($state) {
1130 function dbesc($str) {
1133 if ($db && $db->connected) {
1134 return($db->escape($str));
1136 return(str_replace("'","\\'",$str));
1140 // Function: q($sql,$args);
1141 // Description: execute SQL query with printf style args.
1142 // Example: $r = q("SELECT * FROM `%s` WHERE `uid` = %d",
1146 $args = func_get_args();
1149 if ($db && $db->connected) {
1150 $sql = $db->any_value_fallback($sql);
1151 $stmt = @vsprintf($sql,$args); // Disabled warnings
1152 //logger("dba: q: $stmt", LOGGER_ALL);
1153 if ($stmt === false)
1154 logger('dba: vsprintf error: ' . print_r(debug_backtrace(),true), LOGGER_DEBUG);
1156 $db->log_index($stmt);
1158 return $db->q($stmt);
1163 * This will happen occasionally trying to store the
1164 * session data after abnormal program termination
1167 logger('dba: no database: ' . print_r($args,true));
1172 * @brief Performs a query with "dirty reads"
1174 * By doing dirty reads (reading uncommitted data) no locks are performed
1175 * This function can be used to fetch data that doesn't need to be reliable.
1177 * @param $args Query parameters (1 to N parameters of different types)
1178 * @return array Query array
1183 $args = func_get_args();
1186 if ($db && $db->connected) {
1187 $sql = $db->any_value_fallback($sql);
1188 $stmt = @vsprintf($sql,$args); // Disabled warnings
1189 if ($stmt === false)
1190 logger('dba: vsprintf error: ' . print_r(debug_backtrace(),true), LOGGER_DEBUG);
1192 $db->log_index($stmt);
1194 $db->q("SET SESSION TRANSACTION ISOLATION LEVEL READ UNCOMMITTED;");
1195 $retval = $db->q($stmt);
1196 $db->q("SET SESSION TRANSACTION ISOLATION LEVEL REPEATABLE READ;");
1202 * This will happen occasionally trying to store the
1203 * session data after abnormal program termination
1206 logger('dba: no database: ' . print_r($args,true));
1212 * Raw db query, no arguments
1215 function dbq($sql) {
1218 if ($db && $db->connected) {
1219 $ret = $db->q($sql);
1226 // Caller is responsible for ensuring that any integer arguments to
1227 // dbesc_array are actually integers and not malformed strings containing
1228 // SQL injection vectors. All integer array elements should be specifically
1229 // cast to int to avoid trouble.
1230 function dbesc_array_cb(&$item, $key) {
1231 if (is_string($item))
1232 $item = dbesc($item);
1235 function dbesc_array(&$arr) {
1236 if (is_array($arr) && count($arr)) {
1237 array_walk($arr,'dbesc_array_cb');
1241 function dba_timer() {
1242 return microtime(true);