]> git.mxchange.org Git - friendica.git/blobdiff - include/dba.php
Bugfix for pictures that weren't stored / reworked database calls
[friendica.git] / include / dba.php
index 53ee3e9655bebdb50080b045a812c9ecd6246198..fa33f245aeb0210c3a23030f16c4f3d72e3ceaf1 100644 (file)
@@ -214,6 +214,16 @@ class dba {
                }
        }
 
+       /**
+        * @brief execute SQL query - deprecated
+        *
+        * Please use the dba:: functions instead:
+        * dba::select, dba::exists, dba::insert
+        * dba::delete, dba::update, dba::p, dba::e
+        *
+        * @param string $sql SQL query
+        * @return array Query array
+        */
        public function q($sql) {
                $ret = self::p($sql);
 
@@ -366,6 +376,10 @@ class dba {
        /**
         * @brief Executes a prepared statement that returns data
         * @usage Example: $r = p("SELECT * FROM `item` WHERE `guid` = ?", $guid);
+        *
+        * Please only use it with complicated queries.
+        * For all regular queries please use dba::select or dba::exists
+        *
         * @param string $sql SQL statement
         * @return object statement object
         */
@@ -391,7 +405,7 @@ class dba {
                        return false;
                }
 
-               if (substr_count($sql, '?') != count($args)) {
+               if ((substr_count($sql, '?') != count($args)) && (count($args) > 0)) {
                        // Question: Should we continue or stop the query here?
                        logger('Parameter mismatch. Query "'.$sql.'" - Parameters '.print_r($args, true), LOGGER_DEBUG);
                }
@@ -423,6 +437,19 @@ class dba {
 
                switch (self::$dbo->driver) {
                        case 'pdo':
+                               // If there are no arguments we use "query"
+                               if (count($args) == 0) {
+                                       if (!$retval = self::$dbo->db->query($sql)) {
+                                               $errorInfo = self::$dbo->db->errorInfo();
+                                               self::$dbo->error = $errorInfo[2];
+                                               self::$dbo->errorno = $errorInfo[1];
+                                               $retval = false;
+                                               break;
+                                       }
+                                       self::$dbo->affected_rows = $retval->rowCount();
+                                       break;
+                               }
+
                                if (!$stmt = self::$dbo->db->prepare($sql)) {
                                        $errorInfo = self::$dbo->db->errorInfo();
                                        self::$dbo->error = $errorInfo[2];
@@ -451,8 +478,8 @@ class dba {
                                $command = strtolower($parts[0]);
                                $can_be_prepared = in_array($command, array('select', 'update', 'insert', 'delete'));
 
-                               // The fallback routine currently only works with statements that doesn't return values
-                               if (!$can_be_prepared && $called_from_e) {
+                               // The fallback routine is called as well when there are no arguments
+                               if (!$can_be_prepared || (count($args) == 0)) {
                                        $retval = self::$dbo->db->query(self::replace_parameters($sql, $args));
                                        if (self::$dbo->db->errno) {
                                                self::$dbo->error = self::$dbo->db->error;
@@ -561,6 +588,8 @@ class dba {
        /**
         * @brief Executes a prepared statement like UPDATE or INSERT that doesn't return data
         *
+        * Please use dba::delete, dba::insert, dba::update, ... instead
+        *
         * @param string $sql SQL statement
         * @return boolean Was the query successfull? False is returned only if an error occurred
         */
@@ -643,6 +672,8 @@ class dba {
        /**
         * @brief Fetches the first row
         *
+        * Please use dba::select or dba::exists whenever this is possible.
+        *
         * @param string $sql SQL statement
         * @return array first row of query
         */
@@ -727,6 +758,10 @@ class dba {
                        case 'pdo':
                                return $stmt->fetch(PDO::FETCH_ASSOC);
                        case 'mysqli':
+                               if (get_class($stmt) == 'mysqli_result') {
+                                       return $stmt->fetch_assoc();
+                               }
+
                                // This code works, but is slow
 
                                // Bind the result to a result array
@@ -890,13 +925,13 @@ class dba {
                $definition = db_definition();
 
                foreach ($definition AS $table => $structure) {
-                       foreach ($structure['fields'] AS $field => $field_struct) {
-                               if (isset($field_struct['relation'])) {
-                                       foreach ($field_struct['relation'] AS $rel_table => $rel_field) {
-                                               self::$relation[$rel_table][$rel_field][$table][] = $field;
-                                       }
-                               }
-                       }
+                       foreach ($structure['fields'] AS $field => $field_struct) {
+                               if (isset($field_struct['relation'])) {
+                                       foreach ($field_struct['relation'] AS $rel_table => $rel_field) {
+                                               self::$relation[$rel_table][$rel_field][$table][] = $field;
+                                       }
+                               }
+                       }
                }
        }
 
@@ -1142,7 +1177,11 @@ class dba {
         * Example:
         * $table = "item";
         * $fields = array("id", "uri", "uid", "network");
+        *
         * $condition = array("uid" => 1, "network" => 'dspr');
+        * or:
+        * $condition = array("`uid` = ? AND `network` IN (?, ?)", 1, 'dfrn', 'dspr');
+        *
         * $params = array("order" => array("id", "received" => true), "limit" => 1);
         *
         * $data = dba::select($table, $fields, $condition, $params);
@@ -1280,10 +1319,16 @@ function dbesc($str) {
        }
 }
 
-// Function: q($sql,$args);
-// Description: execute SQL query with printf style args.
-// Example: $r = q("SELECT * FROM `%s` WHERE `uid` = %d",
-//                   'user', 1);
+/**
+ * @brief execute SQL query with printf style args - deprecated
+ *
+ * Please use the dba:: functions instead:
+ * dba::select, dba::exists, dba::insert
+ * dba::delete, dba::update, dba::p, dba::e
+ *
+ * @param $args Query parameters (1 to N parameters of different types)
+ * @return array Query array
+ */
 function q($sql) {
        global $db;
 
@@ -1319,10 +1364,9 @@ function q($sql) {
 /**
  * @brief Performs a query with "dirty reads" - deprecated
  *
- * 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.
- *
- * Hadn't worked like expected and does now the same like the other function.
+ * Please use the dba:: functions instead:
+ * dba::select, dba::exists, dba::insert
+ * dba::delete, dba::update, dba::p, dba::e
  *
  * @param $args Query parameters (1 to N parameters of different types)
  * @return array Query array
@@ -1356,37 +1400,6 @@ function qu($sql) {
        return false;
 }
 
-/**
- *
- * Raw db query, no arguments
- *
- */
-function dbq($sql) {
-       global $db;
-
-       if ($db && $db->connected) {
-               $ret = $db->q($sql);
-       } else {
-               $ret = false;
-       }
-       return $ret;
-}
-
-// Caller is responsible for ensuring that any integer arguments to
-// dbesc_array are actually integers and not malformed strings containing
-// SQL injection vectors. All integer array elements should be specifically
-// cast to int to avoid trouble.
-function dbesc_array_cb(&$item, $key) {
-       if (is_string($item))
-               $item = dbesc($item);
-}
-
-function dbesc_array(&$arr) {
-       if (is_array($arr) && count($arr)) {
-               array_walk($arr,'dbesc_array_cb');
-       }
-}
-
 function dba_timer() {
        return microtime(true);
 }