From: Michael Date: Wed, 9 Aug 2017 21:12:41 +0000 (+0000) Subject: Modifed "update" and "insert" function / many changed queries X-Git-Url: https://git.mxchange.org/?a=commitdiff_plain;h=fce72cbbc8151a824269b7492e9ec7b6d551aa36;p=friendica.git Modifed "update" and "insert" function / many changed queries --- diff --git a/include/contact_selectors.php b/include/contact_selectors.php index 27bdb47570..05059e3358 100644 --- a/include/contact_selectors.php +++ b/include/contact_selectors.php @@ -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']; } } diff --git a/include/conversation.php b/include/conversation.php index bd6e714654..a8ac83c1a2 100644 --- a/include/conversation.php +++ b/include/conversation.php @@ -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')) { /** diff --git a/include/dba.php b/include/dba.php index c2ae78c0f7..954d218c1a 100644 --- a/include/dba.php +++ b/include/dba.php @@ -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); } diff --git a/include/identity.php b/include/identity.php index b24a5314a5..03a5416452 100644 --- a/include/identity.php +++ b/include/identity.php @@ -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; } diff --git a/include/items.php b/include/items.php index 94401509b4..521298647f 100644 --- a/include/items.php +++ b/include/items.php @@ -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)) { diff --git a/include/plugin.php b/include/plugin.php index f5f0bb2b29..60ef6138b9 100644 --- a/include/plugin.php +++ b/include/plugin.php @@ -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. diff --git a/mod/display.php b/mod/display.php index 0d347882e2..08b51bab48 100644 --- a/mod/display.php +++ b/mod/display.php @@ -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)); } } diff --git a/mod/network.php b/mod/network.php index aad56bfd67..ebbd56dfbf 100644 --- a/mod/network.php +++ b/mod/network.php @@ -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)) { diff --git a/src/App.php b/src/App.php index ec03829933..4cdaa94ccd 100644 --- a/src/App.php +++ b/src/App.php @@ -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(); } /** diff --git a/src/Core/Config.php b/src/Core/Config.php index 71189bd0eb..0cdccb953a 100644 --- a/src/Core/Config.php +++ b/src/Core/Config.php @@ -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; diff --git a/src/Core/PConfig.php b/src/Core/PConfig.php index ab3d548c07..e5a852e403 100644 --- a/src/Core/PConfig.php +++ b/src/Core/PConfig.php @@ -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) {