]> git.mxchange.org Git - friendica.git/commitdiff
DBA: The condition can now be a complex string
authorMichael <heluecht@pirati.ca>
Sat, 12 Aug 2017 13:54:29 +0000 (13:54 +0000)
committerMichael <heluecht@pirati.ca>
Sat, 12 Aug 2017 13:54:29 +0000 (13:54 +0000)
include/dba.php
include/items.php
include/uimport.php
mod/item.php

index 7926fa10d86da258ff381bd754dbced9f37b1fdd..36b37498a774663d24576af40a12d1af1c72abf8 100644 (file)
@@ -412,21 +412,6 @@ class dba {
                return $connected;
        }
 
-       function insert_id() {
-               switch ($this->driver) {
-                       case 'pdo':
-                               $id = $this->db->lastInsertId();
-                               break;
-                       case 'mysqli':
-                               $id = $this->db->insert_id;
-                               break;
-                       case 'mysql':
-                               $id = mysql_insert_id($this->db);
-                               break;
-               }
-               return $id;
-       }
-
        function __destruct() {
                if ($this->db) {
                        switch ($this->driver) {
@@ -777,9 +762,15 @@ class dba {
                        return false;
                }
 
-               $fields = array_keys($condition);
+               $fields = array();
+
+               $array_element = each($condition);
+               $array_key = $array_element['key'];
+               if (!is_int($array_key)) {
+                       $fields = array($array_key);
+               }
 
-               $stmt = self::select($table, array($fields[0]), $condition, array('limit' => 1, 'only_query' => true));
+               $stmt = self::select($table, $fields, $condition, array('limit' => 1, 'only_query' => true));
 
                if (is_bool($stmt)) {
                        $retval = $stmt;
@@ -914,6 +905,26 @@ class dba {
                return self::e($sql, $param);
        }
 
+       /**
+        * @brief Fetch the id of the last insert command
+        *
+        * @return integer Last inserted id
+        */
+       function lastInsertId() {
+               switch (self::$dbo->driver) {
+                       case 'pdo':
+                               $id = self::$dbo->db->lastInsertId();
+                               break;
+                       case 'mysqli':
+                               $id = self::$dbo->db->insert_id;
+                               break;
+                       case 'mysql':
+                               $id = mysql_insert_id(self::$dbo);
+                               break;
+               }
+               return $id;
+       }
+
        /**
         * @brief Locks a table for exclusive write access
         *
@@ -1181,15 +1192,23 @@ class dba {
 
                $table = self::$dbo->escape($table);
 
-               if (is_bool($old_fields)) {
-                       $sql = "SELECT * FROM `".$table."` WHERE `".
-                       implode("` = ? AND `", array_keys($condition))."` = ? LIMIT 1";
-
-                       $params = array_values($condition);
+               if (count($condition) > 0) {
+                       $array_element = each($condition);
+                       $array_key = $array_element['key'];
+                       if (is_int($array_key)) {
+                               $condition_string = " WHERE ".array_shift($condition);
+                       } else {
+                               $condition_string = " WHERE `".implode("` = ? AND `", array_keys($condition))."` = ?";
+                       }
+               } else {
+                       $condition_string = "";
+               }
 
+               if (is_bool($old_fields)) {
                        $do_insert = $old_fields;
 
-                       $old_fields = self::fetch_first($sql, $params);
+                       $old_fields = self::select($table, array(), $condition, array('limit' => 1));
+
                        if (is_bool($old_fields)) {
                                if ($do_insert) {
                                        $values = array_merge($condition, $fields);
@@ -1216,8 +1235,7 @@ class dba {
                }
 
                $sql = "UPDATE `".$table."` SET `".
-                       implode("` = ?, `", array_keys($fields))."` = ? WHERE `".
-                       implode("` = ? AND `", array_keys($condition))."` = ?";
+                       implode("` = ?, `", array_keys($fields))."` = ?".$condition_string;
 
                $params1 = array_values($fields);
                $params2 = array_values($condition);
@@ -1256,7 +1274,13 @@ class dba {
                }
 
                if (count($condition) > 0) {
-                       $condition_string = " WHERE `".implode("` = ? AND `", array_keys($condition))."` = ?";
+                       $array_element = each($condition);
+                       $array_key = $array_element['key'];
+                       if (is_int($array_key)) {
+                               $condition_string = " WHERE ".array_shift($condition);
+                       } else {
+                               $condition_string = " WHERE `".implode("` = ? AND `", array_keys($condition))."` = ?";
+                       }
                } else {
                        $condition_string = "";
                }
index e253f8de6635d64b60e431d0d08f52894b8e4808..8ba41e9e240758265d9e99693269731e24c6e1ed 100644 (file)
@@ -963,13 +963,7 @@ function item_store($arr, $force_parent = false, $notify = false, $dontcache = f
 
        // When the item was successfully stored we fetch the ID of the item.
        if (dbm::is_result($r)) {
-               $r = q("SELECT LAST_INSERT_ID() AS `item-id`");
-               if (dbm::is_result($r)) {
-                       $current_post = $r[0]['item-id'];
-               } else {
-                       // This shouldn't happen
-                       $current_post = 0;
-               }
+               $current_post = dba::lastInsertId();
        } else {
                // This can happen - for example - if there are locking timeouts.
                dba::rollback();
index b27f9dc12607d52ed20402915de4947955c8548f..3bdde22351d4dbb15f4ec304cf116de51bc2a224 100644 (file)
@@ -6,13 +6,11 @@ require_once("include/Photo.php");
 define("IMPORT_DEBUG", False);
 
 function last_insert_id() {
-       global $db;
-
        if (IMPORT_DEBUG) {
                return 1;
        }
 
-       return $db->insert_id();
+       return dba::lastInsertId();
 }
 
 function last_error() {
index 7e6d092711e31e705eaf0f59ec931ba7c928156c..cbe338c4f89f7a0f4590dcc2b1678b5099d55190 100644 (file)
@@ -887,12 +887,7 @@ function item_post(App $a) {
        );
 
        if (dbm::is_result($r)) {
-               $r = q("SELECT LAST_INSERT_ID() AS `item-id`");
-               if (dbm::is_result($r)) {
-                       $post_id = $r[0]['item-id'];
-               } else {
-                       $post_id = 0;
-               }
+               $post_id = dba::lastInsertId();
        } else {
                logger('mod_item: unable to create post.');
                $post_id = 0;