]> git.mxchange.org Git - friendica.git/commitdiff
Modifed "update" and "insert" function / many changed queries
authorMichael <heluecht@pirati.ca>
Wed, 9 Aug 2017 21:12:41 +0000 (21:12 +0000)
committerMichael <heluecht@pirati.ca>
Wed, 9 Aug 2017 21:12:41 +0000 (21:12 +0000)
include/contact_selectors.php
include/conversation.php
include/dba.php
include/identity.php
include/items.php
include/plugin.php
mod/display.php
mod/network.php
src/App.php
src/Core/Config.php
src/Core/PConfig.php

index 27bdb47570918e662cd9025c31c87ec1b8f6d473..05059e3358abcfc92170569b8905979ddf4bb606 100644 (file)
@@ -101,12 +101,12 @@ function network_to_name($s, $profile = "") {
        $networkname = str_replace($search, $replace, $s);
 
        if ((in_array($s, array(NETWORK_DFRN, NETWORK_DIASPORA, NETWORK_OSTATUS))) && ($profile != "")) {
-               $r = q("SELECT `gserver`.`platform` FROM `gcontact`
+               $r = dba::fetch_first("SELECT `gserver`.`platform` FROM `gcontact`
                                INNER JOIN `gserver` ON `gserver`.`nurl` = `gcontact`.`server_url`
-                               WHERE `gcontact`.`nurl` = '%s' AND `platform` != ''",
-                               dbesc(normalise_link($profile)));
+                               WHERE `gcontact`.`nurl` = ? AND `platform` != ''", normalise_link($profile));
+
                if (dbm::is_result($r)) {
-                       $networkname = $r[0]["platform"];
+                       $networkname = $r['platform'];
                }
        }
 
index bd6e714654cca2fdf6512afcfbb04665ee7d4f31..a8ac83c1a2ca705365e0a816b86edef95091dc28 100644 (file)
@@ -921,10 +921,11 @@ function best_link_url($item, &$sparkle, $ssl_state = false) {
        $clean_url = normalise_link($item['author-link']);
 
        if (local_user()) {
-               $r = q("SELECT `id` FROM `contact` WHERE `network` = '%s' AND `uid` = %d AND `nurl` = '%s' AND NOT `pending` LIMIT 1",
-                       dbesc(NETWORK_DFRN), intval(local_user()), dbesc(normalise_link($clean_url)));
+               $r = dba::select('contact', array('id'),
+                       array('network' => NETWORK_DFRN, 'uid' => local_user(), 'nurl' => normalise_link($clean_url), 'pending' => false),
+                       array('limit' => 1));
                if (dbm::is_result($r)) {
-                       $best_url = 'redir/' . $r[0]['id'];
+                       $best_url = 'redir/' . $r['id'];
                        $sparkle = true;
                }
        }
@@ -940,7 +941,6 @@ function best_link_url($item, &$sparkle, $ssl_state = false) {
 }
 
 
-if (! function_exists('item_photo_menu')) {
 function item_photo_menu($item) {
        $ssl_state = false;
 
@@ -970,12 +970,11 @@ function item_photo_menu($item) {
        $cid = 0;
        $network = '';
        $rel = 0;
-       $r = q("SELECT `id`, `network`, `rel` FROM `contact` WHERE `uid` = %d AND `nurl` = '%s' LIMIT 1",
-               intval(local_user()), dbesc(normalise_link($item['author-link'])));
+       $r = dba::select('contact', array('id', 'network', 'rel'), array('uid' => local_user(), 'nurl' => normalise_link($item['author-link'])), array('limit' => 1));
        if (dbm::is_result($r)) {
-               $cid = $r[0]['id'];
-               $network = $r[0]['network'];
-               $rel = $r[0]['rel'];
+               $cid = $r['id'];
+               $network = $r['network'];
+               $rel = $r['rel'];
        }
 
        if ($sparkle) {
@@ -1036,7 +1035,7 @@ function item_photo_menu($item) {
                }
        }
        return $o;
-}}
+}
 
 if (! function_exists('builtin_activity_puller')) {
 /**
index c2ae78c0f79fef97365606a8eec21fea9690e27e..954d218c1aeaa6e210227e6a08b352d766008587 100644 (file)
@@ -891,12 +891,20 @@ class dba {
         *
         * @param string $table Table name
         * @param array $param parameter array
+        * @param bool $on_duplicate_update Do an update on a duplicate entry
         *
         * @return boolean was the insert successfull?
         */
-       static public function insert($table, $param) {
+       static public function insert($table, $param, $on_duplicate_update = false) {
                $sql = "INSERT INTO `".self::$dbo->escape($table)."` (`".implode("`, `", array_keys($param))."`) VALUES (".
-                       substr(str_repeat("?, ", count($param)), 0, -2).");";
+                       substr(str_repeat("?, ", count($param)), 0, -2).")";
+
+               if ($on_duplicate_update) {
+                       $sql .= " ON DUPLICATE KEY UPDATE `".implode("` = ?, `", array_keys($param))."` = ?";
+
+                       $values = array_values($param);
+                       $param = array_merge_recursive($values, $values);
+               }
 
                return self::e($sql, $param);
        }
@@ -1160,34 +1168,27 @@ class dba {
         * @param string $table Table name
         * @param array $fields contains the fields that are updated
         * @param array $condition condition array with the key values
-        * @param array|boolean $old_fields array with the old field values that are about to be replaced
+        * @param array|boolean $old_fields array with the old field values that are about to be replaced (true = update on duplicate)
         *
         * @return boolean was the update successfull?
         */
        static public function update($table, $fields, $condition, $old_fields = array()) {
 
-               /** @todo We may use MySQL specific functions here:
-                * INSERT INTO `config` (`cat`, `k`, `v`) VALUES ('%s', '%s', '%s') ON DUPLICATE KEY UPDATE `v` = '%s'"
-                * But I think that it doesn't make sense here.
-               */
-
                $table = self::$dbo->escape($table);
 
                if (is_bool($old_fields)) {
                        $sql = "SELECT * FROM `".$table."` WHERE `".
                        implode("` = ? AND `", array_keys($condition))."` = ? LIMIT 1";
 
-                       $params = array();
-                       foreach ($condition AS $value) {
-                               $params[] = $value;
-                       }
+                       $params = array_values($condition);
 
                        $do_insert = $old_fields;
 
                        $old_fields = self::fetch_first($sql, $params);
                        if (is_bool($old_fields)) {
                                if ($do_insert) {
-                                       return self::insert($table, $fields);
+                                       $values = array_merge($condition, $fields);
+                                       return self::insert($table, $values, $do_insert);
                                }
                                $old_fields = array();
                        }
@@ -1213,13 +1214,9 @@ class dba {
                        implode("` = ?, `", array_keys($fields))."` = ? WHERE `".
                        implode("` = ? AND `", array_keys($condition))."` = ?";
 
-               $params = array();
-               foreach ($fields AS $value) {
-                       $params[] = $value;
-               }
-               foreach ($condition AS $value) {
-                       $params[] = $value;
-               }
+               $params1 = array_values($fields);
+               $params2 = array_values($condition);
+               $params = array_merge_recursive($params1, $params2);
 
                return self::e($sql, $params);
        }
index b24a5314a5d8003a80e5b422ba9d7ad3c42ce19f..03a5416452634c6ec548502d34f88560c78e696a 100644 (file)
@@ -136,49 +136,47 @@ function profile_load(App $a, $nickname, $profile = 0, $profiledata = array()) {
  */
 function get_profiledata_by_nick($nickname, $uid = 0, $profile = 0) {
        if (remote_user() && count($_SESSION['remote'])) {
-                       foreach ($_SESSION['remote'] as $visitor) {
-                               if ($visitor['uid'] == $uid) {
-                                       $r = q("SELECT `profile-id` FROM `contact` WHERE `id` = %d LIMIT 1",
-                                               intval($visitor['cid'])
-                                       );
-                                       if (dbm::is_result($r))
-                                               $profile = $r[0]['profile-id'];
-                                       break;
+               foreach ($_SESSION['remote'] as $visitor) {
+                       if ($visitor['uid'] == $uid) {
+                               $r = dba::select('contact', array('profile-id'), array('id' => $visitor['cid']), array('limit' => 1));
+                               if (dbm::is_result($r)) {
+                                       $profile = $r['profile-id'];
                                }
+                               break;
                        }
                }
+       }
 
        $r = null;
 
        if ($profile) {
                $profile_int = intval($profile);
-               $r = q("SELECT `contact`.`id` AS `contact_id`, `contact`.`photo` AS `contact_photo`,
+               $r = dba::fetch_first("SELECT `contact`.`id` AS `contact_id`, `contact`.`photo` AS `contact_photo`,
                                `contact`.`thumb` AS `contact_thumb`, `contact`.`micro` AS `contact_micro`,
                                `profile`.`uid` AS `profile_uid`, `profile`.*,
                                `contact`.`avatar-date` AS picdate, `contact`.`addr`, `user`.*
                        FROM `profile`
                        INNER JOIN `contact` on `contact`.`uid` = `profile`.`uid` AND `contact`.`self`
                        INNER JOIN `user` ON `profile`.`uid` = `user`.`uid`
-                       WHERE `user`.`nickname` = '%s' AND `profile`.`id` = %d LIMIT 1",
-                               dbesc($nickname),
-                               intval($profile_int)
+                       WHERE `user`.`nickname` = ? AND `profile`.`id` = ? LIMIT 1",
+                               $nickname,
+                               $profile_int
                );
        }
        if (!dbm::is_result($r)) {
-               $r = q("SELECT `contact`.`id` AS `contact_id`, `contact`.`photo` as `contact_photo`,
+               $r = dba::fetch_first("SELECT `contact`.`id` AS `contact_id`, `contact`.`photo` as `contact_photo`,
                                `contact`.`thumb` AS `contact_thumb`, `contact`.`micro` AS `contact_micro`,
                                `profile`.`uid` AS `profile_uid`, `profile`.*,
                                `contact`.`avatar-date` AS picdate, `contact`.`addr`, `user`.*
                        FROM `profile`
                        INNER JOIN `contact` ON `contact`.`uid` = `profile`.`uid` AND `contact`.`self`
                        INNER JOIN `user` ON `profile`.`uid` = `user`.`uid`
-                       WHERE `user`.`nickname` = '%s' AND `profile`.`is-default` LIMIT 1",
-                               dbesc($nickname)
+                       WHERE `user`.`nickname` = ? AND `profile`.`is-default` LIMIT 1",
+                               $nickname
                );
        }
 
-       return $r[0];
-
+       return $r;
 }
 
 
index 94401509b49853eae5f6adf038f15ee21fa3b0da..521298647fc94dd3042118766cf823d4ac3e4520 100644 (file)
@@ -446,15 +446,6 @@ function store_conversation($arr) {
                        $conversation['source'] = $arr['source'];
                }
 
-               if (!Lock::set('store_conversation')) {
-                       // When using semaphores, this case never can't happen
-                       unset($arr['conversation-uri']);
-                       unset($arr['conversation-href']);
-                       unset($arr['protocol']);
-                       unset($arr['source']);
-                       return $arr;
-               }
-
                $old_conv = dba::fetch_first("SELECT `item-uri`, `reply-to-uri`, `conversation-uri`, `conversation-href`, `protocol`, `source`
                                FROM `conversation` WHERE `item-uri` = ?", $conversation['item-uri']);
                if (dbm::is_result($old_conv)) {
@@ -472,11 +463,10 @@ function store_conversation($arr) {
                                logger('Conversation: update for '.$conversation['item-uri'].' from '.$conv['protocol'].' to '.$conversation['protocol'].' failed', LOGGER_DEBUG);
                        }
                } else {
-                       if (!dba::insert('conversation', $conversation)) {
+                       if (!dba::insert('conversation', $conversation, true)) {
                                logger('Conversation: insert for '.$conversation['item-uri'].' (protocol '.$conversation['protocol'].') failed', LOGGER_DEBUG);
                        }
                }
-               Lock::remove('store_conversation');
        }
 
        unset($arr['conversation-uri']);
@@ -966,23 +956,10 @@ function item_store($arr, $force_parent = false, $notify = false, $dontcache = f
                }
        }
 
-       // Store the unescaped version
-       $unescaped = $arr;
-
-       dbm::esc_array($arr, true);
-
        logger('item_store: ' . print_r($arr,true), LOGGER_DATA);
 
        dba::transaction();
-
-       $r = dbq("INSERT INTO `item` (`"
-                       . implode("`, `", array_keys($arr))
-                       . "`) VALUES ("
-                       . implode(", ", array_values($arr))
-                       . ")");
-
-       // And restore it
-       $arr = $unescaped;
+       $r = dba::insert('item', $arr);
 
        // When the item was successfully stored we fetch the ID of the item.
        if (dbm::is_result($r)) {
index f5f0bb2b294f21ca6126bd46a5b5bdb755f6c060..60ef6138b9c4712145fb3c69ecf9d2e7c574936f 100644 (file)
@@ -183,20 +183,18 @@ function unregister_hook($hook,$file,$function) {
 }}
 
 
-if (! function_exists('load_hooks')) {
 function load_hooks() {
        $a = get_app();
        $a->hooks = array();
-       $r = q("SELECT * FROM `hook` WHERE 1 ORDER BY `priority` DESC, `file`");
+       $r = dba::select('hook', array('hook', 'file', 'function'), array(), array('order' => array('priority' => 'desc', 'file')));
 
-       if (dbm::is_result($r)) {
-               foreach ($r as $rr) {
-                       if (! array_key_exists($rr['hook'],$a->hooks))
-                               $a->hooks[$rr['hook']] = array();
-                       $a->hooks[$rr['hook']][] = array($rr['file'],$rr['function']);
+       while ($rr = dba::fetch($r)) {
+               if (! array_key_exists($rr['hook'],$a->hooks)) {
+                       $a->hooks[$rr['hook']] = array();
                }
+               $a->hooks[$rr['hook']][] = array($rr['file'],$rr['function']);
        }
-}}
+}
 
 /**
  * @brief Calls a hook.
index 0d347882e2b16c95120a5fcbd4f16e5c146d96ab..08b51bab48829cb0c4316c9c0497fc676300bd47 100644 (file)
@@ -423,9 +423,7 @@ function display_content(App $a, $update = 0) {
                                        intval($r[0]['parent']));
 
                        if ($unseen) {
-                               q("UPDATE `item` SET `unseen` = 0 WHERE `parent` = %d AND `unseen`",
-                                       intval($r[0]['parent'])
-                               );
+                               dba::update('item', array('unseen' => false), array('parent' => $r[0]['parent'], 'unseen' => true));
                        }
                }
 
index aad56bfd67e9dd2f3470fcc1572cdc74c216c82f..ebbd56dfbfb667f244eec35cbe21249addb00f09 100644 (file)
@@ -794,18 +794,12 @@ function network_content(App $a, $update = 0) {
 
 
        if (!$group && !$cid && !$star) {
-
-               $unseen = q("SELECT `id` FROM `item` WHERE `unseen` AND `uid` = %d LIMIT 1",
-                               intval(local_user()));
+               $unseen = dba::select('item', array('id'), array('unseen' => true, 'uid' => local_user()), array('limit' => 1));
 
                if (dbm::is_result($unseen)) {
-                       $r = q("UPDATE `item` SET `unseen` = 0
-                               WHERE `unseen` = 1 AND `uid` = %d",
-                               intval(local_user())
-                       );
+                       $r = dba::update('item', array('unseen' => false), array('uid' => local_user(), 'unseen' => true));
                }
        } elseif ($update_unseen) {
-
                $unseen = q("SELECT `id` FROM `item` ".$update_unseen. " LIMIT 1");
 
                if (dbm::is_result($unseen)) {
index ec03829933d8478ea81b6c53ecfe6b1d7ccfadf7..4cdaa94ccd6f4b2ae4fb44ce7a15e0510e3c486a 100644 (file)
@@ -6,6 +6,7 @@ use Friendica\Core\Config;
 use Friendica\Core\PConfig;
 
 use Cache;
+use dba;
 use dbm;
 
 use Detection\MobileDetect;
@@ -712,20 +713,20 @@ class App {
 
                $this->remove_inactive_processes();
 
-               q('START TRANSACTION');
+               dba::transaction();
 
                $r = q('SELECT `pid` FROM `process` WHERE `pid` = %d', intval(getmypid()));
                if (!dbm::is_result($r)) {
                        q("INSERT INTO `process` (`pid`,`command`,`created`) VALUES (%d, '%s', '%s')", intval(getmypid()), dbesc($command), dbesc(datetime_convert()));
                }
-               q('COMMIT');
+               dba::commit();
        }
 
        /**
         * @brief Remove inactive processes
         */
        function remove_inactive_processes() {
-               q('START TRANSACTION');
+               dba::transaction();
 
                $r = q('SELECT `pid` FROM `process`');
                if (dbm::is_result($r)) {
@@ -735,7 +736,7 @@ class App {
                                }
                        }
                }
-               q('COMMIT');
+               dba::commit();
        }
 
        /**
index 71189bd0eb811a7eaf0c7792d833602444da352c..0cdccb953a47c101238b7d2e8206488d488c0598 100644 (file)
@@ -164,19 +164,11 @@ class Config {
                $dbvalue = (is_array($value) ? serialize($value) : $dbvalue);
 
                if (is_null($stored) || !self::$in_db[$family][$key]) {
-                       $ret = q("INSERT INTO `config` (`cat`, `k`, `v`) VALUES ('%s', '%s', '%s') ON DUPLICATE KEY UPDATE `v` = '%s'",
-                               dbesc($family),
-                               dbesc($key),
-                               dbesc($dbvalue),
-                               dbesc($dbvalue)
-                       );
+                        dba::insert('config', array('cat' => $family, 'k' => $key, 'v' => $dbvalue), true);
                } else {
-                       $ret = q("UPDATE `config` SET `v` = '%s' WHERE `cat` = '%s' AND `k` = '%s'",
-                               dbesc($dbvalue),
-                               dbesc($family),
-                               dbesc($key)
-                       );
+                        dba::update('config', array('v' => $dbvalue), array('cat' => $family, 'k' => $key), true);
                }
+
                if ($ret) {
                        self::$in_db[$family][$key] = true;
                        return $value;
index ab3d548c0735be9532d4f3c73805d511b7038c60..e5a852e40393fb05ff701485b7773819f3160d01 100644 (file)
@@ -142,20 +142,9 @@ class PConfig {
                $dbvalue = (is_array($value) ? serialize($value) : $dbvalue);
 
                if (is_null($stored) || !self::$in_db[$uid][$family][$key]) {
-                       $ret = q("INSERT INTO `pconfig` (`uid`, `cat`, `k`, `v`) VALUES (%d, '%s', '%s', '%s') ON DUPLICATE KEY UPDATE `v` = '%s'",
-                               intval($uid),
-                               dbesc($family),
-                               dbesc($key),
-                               dbesc($dbvalue),
-                               dbesc($dbvalue)
-                       );
+                       dba::insert('pconfig', array('uid' => $uid, 'cat' => $family, 'k' => $key, 'v' => $dbvalue), true);
                } else {
-                       $ret = q("UPDATE `pconfig` SET `v` = '%s' WHERE `uid` = %d AND `cat` = '%s' AND `k` = '%s'",
-                               dbesc($dbvalue),
-                               intval($uid),
-                               dbesc($family),
-                               dbesc($key)
-                       );
+                       dba::update('pconfig', array('v' => $dbvalue), array('uid' => $uid, 'cat' => $family, 'k' => $key), true);
                }
 
                if ($ret) {