]> git.mxchange.org Git - friendica.git/blobdiff - include/dba.php
Merge remote-tracking branch 'upstream/develop' into 1610-performance
[friendica.git] / include / dba.php
index c66723033c1965c6251cd837fdba3c0f5f43276b..c7b598f2d68e09ed2f3a9ead81d25d23f00d7265 100644 (file)
@@ -1,4 +1,5 @@
 <?php
+require_once("dbm.php");
 
 # if PDO is avaible for mysql, use the new database abstraction
 # TODO: PDO is disabled for release 3.3. We need to investigate why
@@ -14,8 +15,7 @@ if(class_exists('\PDO') && in_array('mysql', PDO::getAvailableDrivers())) {
 require_once('include/datetime.php');
 
 /**
- *
- * MySQL database class
+ * @class MySQL database class
  *
  * For debugging, insert 'dbg(1);' anywhere in the program flow.
  * dbg(0); will turn it off. Logging is performed at LOGGER_DATA level.
@@ -66,6 +66,8 @@ class dba {
                        if(! mysqli_connect_errno()) {
                                $this->connected = true;
                        }
+                       if (isset($a->config["system"]["db_charset"]))
+                               $this->db->set_charset($a->config["system"]["db_charset"]);
                }
                else {
                        $this->mysqli = false;
@@ -73,6 +75,8 @@ class dba {
                        if($this->db && mysql_select_db($db,$this->db)) {
                                $this->connected = true;
                        }
+                       if (isset($a->config["system"]["db_charset"]))
+                               mysql_set_charset($a->config["system"]["db_charset"], $this->db);
                }
                if(! $this->connected) {
                        $this->db = null;
@@ -87,6 +91,40 @@ class dba {
                return $this->db;
        }
 
+       /**
+        * @brief Returns the MySQL server version string
+        * 
+        * 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
+        */
+       public function server_info() {
+               if ($this->mysqli) {
+                       $return = $this->db->server_info;
+               } else {
+                       $return = mysql_get_server_info($this->db);
+               }
+               return $return;
+       }
+
+       /**
+        * @brief Returns the number of rows
+        *
+        * @return string
+        */
+       public function num_rows() {
+               if (!$this->result)
+                       return 0;
+
+               if ($this->mysqli) {
+                       $return = $this->result->num_rows;
+               } else {
+                       $return = mysql_num_rows($this->result);
+               }
+               return $return;
+       }
+
        public function q($sql, $onlyquery = false) {
                global $a;
 
@@ -95,8 +133,18 @@ class dba {
 
                $this->error = '';
 
+               // Check the connection (This can reconnect the connection - if configured)
+               if ($this->mysqli)
+                       $connected = $this->db->ping();
+               else
+                       $connected = mysql_ping($this->db);
+
+               $connstr = ($connected ? "Connected": "Disonnected");
+
                $stamp1 = microtime(true);
 
+               $sql = "/*".$a->callstack()." */ ".$sql;
+
                if($this->mysqli)
                        $result = @$this->db->query($sql);
                else
@@ -107,6 +155,9 @@ class dba {
 
                $a->save_timestamp($stamp1, "database");
 
+               if (strtolower(substr($sql, 0, 6)) != "select")
+                       $a->save_timestamp($stamp1, "database_write");
+
                if(x($a->config,'system') && x($a->config['system'],'db_log')) {
                        if (($duration > $a->config["system"]["db_loglimit"])) {
                                $duration = round($duration, 3);
@@ -119,14 +170,17 @@ class dba {
                }
 
                if($this->mysqli) {
-                       if($this->db->errno)
+                       if($this->db->errno) {
                                $this->error = $this->db->error;
+                               $this->errorno = $this->db->errno;
+                       }
+               } elseif(mysql_errno($this->db)) {
+                       $this->error = mysql_error($this->db);
+                       $this->errorno = mysql_errno($this->db);
                }
-               elseif(mysql_errno($this->db))
-                               $this->error = mysql_error($this->db);
 
                if(strlen($this->error)) {
-                       logger('dba: ' . $this->error);
+                       logger('DB Error ('.$connstr.') '.$this->errorno.': '.$this->error);
                }
 
                if($this->debug) {
@@ -231,6 +285,15 @@ class dba {
                }
        }
 
+       function connected() {
+               if ($this->mysqli)
+                       $connected = $this->db->ping();
+               else
+                       $connected = mysql_ping($this->db);
+
+               return $connected;
+       }
+
        function __destruct() {
                if ($this->db)
                        if($this->mysqli)
@@ -299,6 +362,42 @@ function q($sql) {
 
 }}
 
+/**
+ * @brief Performs a query with "dirty reads"
+ *
+ * By doing dirty reads (reading uncommitted data) no locks are performed
+ * This function can be used to fetch data that doesn't need to be reliable.
+ *
+ * @param $args Query parameters (1 to N parameters of different types)
+ * @return array Query array
+ */
+function qu($sql) {
+
+       global $db;
+       $args = func_get_args();
+       unset($args[0]);
+
+       if($db && $db->connected) {
+               $stmt = @vsprintf($sql,$args); // Disabled warnings
+               if($stmt === false)
+                       logger('dba: vsprintf error: ' . print_r(debug_backtrace(),true), LOGGER_DEBUG);
+               $db->q("SET TRANSACTION ISOLATION LEVEL READ UNCOMMITTED;");
+               $retval = $db->q($stmt);
+               $db->q("COMMIT;");
+               return $retval;
+       }
+
+       /**
+        *
+        * This will happen occasionally trying to store the
+        * session data after abnormal program termination
+        *
+        */
+       logger('dba: no database: ' . print_r($args,true));
+       return false;
+
+}
+
 /**
  *
  * Raw db query, no arguments
@@ -341,4 +440,3 @@ function dbesc_array(&$arr) {
 function dba_timer() {
        return microtime(true);
 }
-