Modifed "update" and "insert" function / many changed queries

This commit is contained in:
Michael 2017-08-09 21:12:41 +00:00
parent a5e7f7bf70
commit fce72cbbc8
11 changed files with 66 additions and 123 deletions

View file

@ -101,12 +101,12 @@ function network_to_name($s, $profile = "") {
$networkname = str_replace($search, $replace, $s); $networkname = str_replace($search, $replace, $s);
if ((in_array($s, array(NETWORK_DFRN, NETWORK_DIASPORA, NETWORK_OSTATUS))) && ($profile != "")) { 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` INNER JOIN `gserver` ON `gserver`.`nurl` = `gcontact`.`server_url`
WHERE `gcontact`.`nurl` = '%s' AND `platform` != ''", WHERE `gcontact`.`nurl` = ? AND `platform` != ''", normalise_link($profile));
dbesc(normalise_link($profile)));
if (dbm::is_result($r)) { if (dbm::is_result($r)) {
$networkname = $r[0]["platform"]; $networkname = $r['platform'];
} }
} }

View file

@ -921,10 +921,11 @@ function best_link_url($item, &$sparkle, $ssl_state = false) {
$clean_url = normalise_link($item['author-link']); $clean_url = normalise_link($item['author-link']);
if (local_user()) { if (local_user()) {
$r = q("SELECT `id` FROM `contact` WHERE `network` = '%s' AND `uid` = %d AND `nurl` = '%s' AND NOT `pending` LIMIT 1", $r = dba::select('contact', array('id'),
dbesc(NETWORK_DFRN), intval(local_user()), dbesc(normalise_link($clean_url))); array('network' => NETWORK_DFRN, 'uid' => local_user(), 'nurl' => normalise_link($clean_url), 'pending' => false),
array('limit' => 1));
if (dbm::is_result($r)) { if (dbm::is_result($r)) {
$best_url = 'redir/' . $r[0]['id']; $best_url = 'redir/' . $r['id'];
$sparkle = true; $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) { function item_photo_menu($item) {
$ssl_state = false; $ssl_state = false;
@ -970,12 +970,11 @@ function item_photo_menu($item) {
$cid = 0; $cid = 0;
$network = ''; $network = '';
$rel = 0; $rel = 0;
$r = q("SELECT `id`, `network`, `rel` FROM `contact` WHERE `uid` = %d AND `nurl` = '%s' LIMIT 1", $r = dba::select('contact', array('id', 'network', 'rel'), array('uid' => local_user(), 'nurl' => normalise_link($item['author-link'])), array('limit' => 1));
intval(local_user()), dbesc(normalise_link($item['author-link'])));
if (dbm::is_result($r)) { if (dbm::is_result($r)) {
$cid = $r[0]['id']; $cid = $r['id'];
$network = $r[0]['network']; $network = $r['network'];
$rel = $r[0]['rel']; $rel = $r['rel'];
} }
if ($sparkle) { if ($sparkle) {
@ -1036,7 +1035,7 @@ function item_photo_menu($item) {
} }
} }
return $o; return $o;
}} }
if (! function_exists('builtin_activity_puller')) { if (! function_exists('builtin_activity_puller')) {
/** /**

View file

@ -891,12 +891,20 @@ class dba {
* *
* @param string $table Table name * @param string $table Table name
* @param array $param parameter array * @param array $param parameter array
* @param bool $on_duplicate_update Do an update on a duplicate entry
* *
* @return boolean was the insert successfull? * @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 (". $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); return self::e($sql, $param);
} }
@ -1160,34 +1168,27 @@ class dba {
* @param string $table Table name * @param string $table Table name
* @param array $fields contains the fields that are updated * @param array $fields contains the fields that are updated
* @param array $condition condition array with the key values * @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? * @return boolean was the update successfull?
*/ */
static public function update($table, $fields, $condition, $old_fields = array()) { 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); $table = self::$dbo->escape($table);
if (is_bool($old_fields)) { if (is_bool($old_fields)) {
$sql = "SELECT * FROM `".$table."` WHERE `". $sql = "SELECT * FROM `".$table."` WHERE `".
implode("` = ? AND `", array_keys($condition))."` = ? LIMIT 1"; implode("` = ? AND `", array_keys($condition))."` = ? LIMIT 1";
$params = array(); $params = array_values($condition);
foreach ($condition AS $value) {
$params[] = $value;
}
$do_insert = $old_fields; $do_insert = $old_fields;
$old_fields = self::fetch_first($sql, $params); $old_fields = self::fetch_first($sql, $params);
if (is_bool($old_fields)) { if (is_bool($old_fields)) {
if ($do_insert) { if ($do_insert) {
return self::insert($table, $fields); $values = array_merge($condition, $fields);
return self::insert($table, $values, $do_insert);
} }
$old_fields = array(); $old_fields = array();
} }
@ -1213,13 +1214,9 @@ class dba {
implode("` = ?, `", array_keys($fields))."` = ? WHERE `". implode("` = ?, `", array_keys($fields))."` = ? WHERE `".
implode("` = ? AND `", array_keys($condition))."` = ?"; implode("` = ? AND `", array_keys($condition))."` = ?";
$params = array(); $params1 = array_values($fields);
foreach ($fields AS $value) { $params2 = array_values($condition);
$params[] = $value; $params = array_merge_recursive($params1, $params2);
}
foreach ($condition AS $value) {
$params[] = $value;
}
return self::e($sql, $params); return self::e($sql, $params);
} }

View 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) { function get_profiledata_by_nick($nickname, $uid = 0, $profile = 0) {
if (remote_user() && count($_SESSION['remote'])) { if (remote_user() && count($_SESSION['remote'])) {
foreach ($_SESSION['remote'] as $visitor) { foreach ($_SESSION['remote'] as $visitor) {
if ($visitor['uid'] == $uid) { if ($visitor['uid'] == $uid) {
$r = q("SELECT `profile-id` FROM `contact` WHERE `id` = %d LIMIT 1", $r = dba::select('contact', array('profile-id'), array('id' => $visitor['cid']), array('limit' => 1));
intval($visitor['cid']) if (dbm::is_result($r)) {
); $profile = $r['profile-id'];
if (dbm::is_result($r))
$profile = $r[0]['profile-id'];
break;
} }
break;
} }
} }
}
$r = null; $r = null;
if ($profile) { if ($profile) {
$profile_int = intval($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`, `contact`.`thumb` AS `contact_thumb`, `contact`.`micro` AS `contact_micro`,
`profile`.`uid` AS `profile_uid`, `profile`.*, `profile`.`uid` AS `profile_uid`, `profile`.*,
`contact`.`avatar-date` AS picdate, `contact`.`addr`, `user`.* `contact`.`avatar-date` AS picdate, `contact`.`addr`, `user`.*
FROM `profile` FROM `profile`
INNER JOIN `contact` on `contact`.`uid` = `profile`.`uid` AND `contact`.`self` INNER JOIN `contact` on `contact`.`uid` = `profile`.`uid` AND `contact`.`self`
INNER JOIN `user` ON `profile`.`uid` = `user`.`uid` INNER JOIN `user` ON `profile`.`uid` = `user`.`uid`
WHERE `user`.`nickname` = '%s' AND `profile`.`id` = %d LIMIT 1", WHERE `user`.`nickname` = ? AND `profile`.`id` = ? LIMIT 1",
dbesc($nickname), $nickname,
intval($profile_int) $profile_int
); );
} }
if (!dbm::is_result($r)) { 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`, `contact`.`thumb` AS `contact_thumb`, `contact`.`micro` AS `contact_micro`,
`profile`.`uid` AS `profile_uid`, `profile`.*, `profile`.`uid` AS `profile_uid`, `profile`.*,
`contact`.`avatar-date` AS picdate, `contact`.`addr`, `user`.* `contact`.`avatar-date` AS picdate, `contact`.`addr`, `user`.*
FROM `profile` FROM `profile`
INNER JOIN `contact` ON `contact`.`uid` = `profile`.`uid` AND `contact`.`self` INNER JOIN `contact` ON `contact`.`uid` = `profile`.`uid` AND `contact`.`self`
INNER JOIN `user` ON `profile`.`uid` = `user`.`uid` INNER JOIN `user` ON `profile`.`uid` = `user`.`uid`
WHERE `user`.`nickname` = '%s' AND `profile`.`is-default` LIMIT 1", WHERE `user`.`nickname` = ? AND `profile`.`is-default` LIMIT 1",
dbesc($nickname) $nickname
); );
} }
return $r[0]; return $r;
} }

View file

@ -446,15 +446,6 @@ function store_conversation($arr) {
$conversation['source'] = $arr['source']; $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` $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']); FROM `conversation` WHERE `item-uri` = ?", $conversation['item-uri']);
if (dbm::is_result($old_conv)) { 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); logger('Conversation: update for '.$conversation['item-uri'].' from '.$conv['protocol'].' to '.$conversation['protocol'].' failed', LOGGER_DEBUG);
} }
} else { } 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); logger('Conversation: insert for '.$conversation['item-uri'].' (protocol '.$conversation['protocol'].') failed', LOGGER_DEBUG);
} }
} }
Lock::remove('store_conversation');
} }
unset($arr['conversation-uri']); 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); logger('item_store: ' . print_r($arr,true), LOGGER_DATA);
dba::transaction(); dba::transaction();
$r = dba::insert('item', $arr);
$r = dbq("INSERT INTO `item` (`"
. implode("`, `", array_keys($arr))
. "`) VALUES ("
. implode(", ", array_values($arr))
. ")");
// And restore it
$arr = $unescaped;
// When the item was successfully stored we fetch the ID of the item. // When the item was successfully stored we fetch the ID of the item.
if (dbm::is_result($r)) { if (dbm::is_result($r)) {

View file

@ -183,20 +183,18 @@ function unregister_hook($hook,$file,$function) {
}} }}
if (! function_exists('load_hooks')) {
function load_hooks() { function load_hooks() {
$a = get_app(); $a = get_app();
$a->hooks = array(); $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)) { while ($rr = dba::fetch($r)) {
foreach ($r as $rr) { if (! array_key_exists($rr['hook'],$a->hooks)) {
if (! array_key_exists($rr['hook'],$a->hooks)) $a->hooks[$rr['hook']] = array();
$a->hooks[$rr['hook']] = array();
$a->hooks[$rr['hook']][] = array($rr['file'],$rr['function']);
} }
$a->hooks[$rr['hook']][] = array($rr['file'],$rr['function']);
} }
}} }
/** /**
* @brief Calls a hook. * @brief Calls a hook.

View file

@ -423,9 +423,7 @@ function display_content(App $a, $update = 0) {
intval($r[0]['parent'])); intval($r[0]['parent']));
if ($unseen) { if ($unseen) {
q("UPDATE `item` SET `unseen` = 0 WHERE `parent` = %d AND `unseen`", dba::update('item', array('unseen' => false), array('parent' => $r[0]['parent'], 'unseen' => true));
intval($r[0]['parent'])
);
} }
} }

View file

@ -794,18 +794,12 @@ function network_content(App $a, $update = 0) {
if (!$group && !$cid && !$star) { if (!$group && !$cid && !$star) {
$unseen = dba::select('item', array('id'), array('unseen' => true, 'uid' => local_user()), array('limit' => 1));
$unseen = q("SELECT `id` FROM `item` WHERE `unseen` AND `uid` = %d LIMIT 1",
intval(local_user()));
if (dbm::is_result($unseen)) { if (dbm::is_result($unseen)) {
$r = q("UPDATE `item` SET `unseen` = 0 $r = dba::update('item', array('unseen' => false), array('uid' => local_user(), 'unseen' => true));
WHERE `unseen` = 1 AND `uid` = %d",
intval(local_user())
);
} }
} elseif ($update_unseen) { } elseif ($update_unseen) {
$unseen = q("SELECT `id` FROM `item` ".$update_unseen. " LIMIT 1"); $unseen = q("SELECT `id` FROM `item` ".$update_unseen. " LIMIT 1");
if (dbm::is_result($unseen)) { if (dbm::is_result($unseen)) {

View file

@ -6,6 +6,7 @@ use Friendica\Core\Config;
use Friendica\Core\PConfig; use Friendica\Core\PConfig;
use Cache; use Cache;
use dba;
use dbm; use dbm;
use Detection\MobileDetect; use Detection\MobileDetect;
@ -712,20 +713,20 @@ class App {
$this->remove_inactive_processes(); $this->remove_inactive_processes();
q('START TRANSACTION'); dba::transaction();
$r = q('SELECT `pid` FROM `process` WHERE `pid` = %d', intval(getmypid())); $r = q('SELECT `pid` FROM `process` WHERE `pid` = %d', intval(getmypid()));
if (!dbm::is_result($r)) { if (!dbm::is_result($r)) {
q("INSERT INTO `process` (`pid`,`command`,`created`) VALUES (%d, '%s', '%s')", intval(getmypid()), dbesc($command), dbesc(datetime_convert())); 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 * @brief Remove inactive processes
*/ */
function remove_inactive_processes() { function remove_inactive_processes() {
q('START TRANSACTION'); dba::transaction();
$r = q('SELECT `pid` FROM `process`'); $r = q('SELECT `pid` FROM `process`');
if (dbm::is_result($r)) { if (dbm::is_result($r)) {
@ -735,7 +736,7 @@ class App {
} }
} }
} }
q('COMMIT'); dba::commit();
} }
/** /**

View file

@ -164,19 +164,11 @@ class Config {
$dbvalue = (is_array($value) ? serialize($value) : $dbvalue); $dbvalue = (is_array($value) ? serialize($value) : $dbvalue);
if (is_null($stored) || !self::$in_db[$family][$key]) { 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'", dba::insert('config', array('cat' => $family, 'k' => $key, 'v' => $dbvalue), true);
dbesc($family),
dbesc($key),
dbesc($dbvalue),
dbesc($dbvalue)
);
} else { } else {
$ret = q("UPDATE `config` SET `v` = '%s' WHERE `cat` = '%s' AND `k` = '%s'", dba::update('config', array('v' => $dbvalue), array('cat' => $family, 'k' => $key), true);
dbesc($dbvalue),
dbesc($family),
dbesc($key)
);
} }
if ($ret) { if ($ret) {
self::$in_db[$family][$key] = true; self::$in_db[$family][$key] = true;
return $value; return $value;

View file

@ -142,20 +142,9 @@ class PConfig {
$dbvalue = (is_array($value) ? serialize($value) : $dbvalue); $dbvalue = (is_array($value) ? serialize($value) : $dbvalue);
if (is_null($stored) || !self::$in_db[$uid][$family][$key]) { 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'", dba::insert('pconfig', array('uid' => $uid, 'cat' => $family, 'k' => $key, 'v' => $dbvalue), true);
intval($uid),
dbesc($family),
dbesc($key),
dbesc($dbvalue),
dbesc($dbvalue)
);
} else { } else {
$ret = q("UPDATE `pconfig` SET `v` = '%s' WHERE `uid` = %d AND `cat` = '%s' AND `k` = '%s'", dba::update('pconfig', array('v' => $dbvalue), array('uid' => $uid, 'cat' => $family, 'k' => $key), true);
dbesc($dbvalue),
intval($uid),
dbesc($family),
dbesc($key)
);
} }
if ($ret) { if ($ret) {