Restructured "dba::exists" function

This commit is contained in:
Michael 2017-08-12 08:55:50 +00:00
parent b14b44e8a8
commit 1d8c91c478
9 changed files with 33 additions and 35 deletions

View file

@ -1090,9 +1090,9 @@ function proc_run($cmd) {
array_shift($argv);
$parameters = json_encode($argv);
$found = dba::select('workerqueue', array('id'), array('parameter' => $parameters, 'done' => false), array('limit' => 1));
$found = dba::exists('workerqueue', array('parameter' => $parameters, 'done' => false));
if (!dbm::is_result($found)) {
if (!$found) {
dba::insert('workerqueue', array('parameter' => $parameters, 'created' => $created, 'priority' => $priority));
}

View file

@ -767,13 +767,19 @@ class dba {
/**
* @brief Check if data exists
*
* @param string $sql SQL statement
* @return boolean Are there rows for that query?
* @param string $table Table name
* @param array $condition array of fields for condition
*
* @return boolean Are there rows for that condition?
*/
static public function exists($sql) {
$params = self::getParam(func_get_args());
static public function exists($table, $condition) {
if (empty($table)) {
return false;
}
$stmt = self::p($sql, $params);
$fields = array_keys($condition);
$stmt = self::select($table, array($fields[0]), $condition, array('limit' => 1, 'only_query' => true));
if (is_bool($stmt)) {
$retval = $stmt;
@ -1270,11 +1276,13 @@ class dba {
$param_string = substr($param_string, 0, -2);
}
if (isset($params['limit'])) {
if (is_int($params['limit'])) {
if (isset($params['limit']) && is_int($params['limit'])) {
$param_string .= " LIMIT ".$params['limit'];
$single_row =($params['limit'] == 1);
$single_row = ($params['limit'] == 1);
}
if (isset($params['only_query']) && $params['only_query']) {
$single_row = !$params['only_query'];
}
$sql = "SELECT ".$select_fields." FROM `".$table."`".$condition_string.$param_string;

View file

@ -245,10 +245,7 @@ function profile_sidebar($profile, $block = 0) {
$profile_url = normalise_link(App::get_baseurl()."/profile/".$profile["nickname"]);
}
$r = dba::select('contact', array('id'),
array('pending' => false, 'uid' => local_user(), 'nurl' => $profile_url), array('limit' => 1));
if (dbm::is_result($r))
$connect = false;
$connect = !dba::exists('contact', array('pending' => false, 'uid' => local_user(), 'nurl' => $profile_url));
}
if ($connect && ($profile['network'] != NETWORK_DFRN) && !isset($profile['remoteconnect']))

View file

@ -121,8 +121,7 @@ function reload_plugins() {
* @return boolean
*/
function plugin_enabled($plugin) {
$r = dba::select('addon', array('id'), array('installed' => true, 'name' => $plugin), array('limit' => 1));
return ((dbm::is_result($r)) && (count($r) > 0));
return dba::exists('addon', array('installed' => true, 'name' => $plugin));
}

View file

@ -14,8 +14,8 @@ function remove_contact_run($argv, $argc) {
$id = intval($argv[1]);
// Only delete if the contact doesn't exist (anymore)
$r = dba::select('contact', array('id'), array('id' => $id), array('limit' => 1));
if (dbm::is_result($r)) {
$r = dba::exists('contact', array('id' => $id));
if ($r) {
return;
}

View file

@ -488,8 +488,6 @@ if (! function_exists('item_new_uri')) {
function item_new_uri($hostname, $uid, $guid = "") {
do {
$dups = false;
if ($guid == "") {
$hash = get_guid(32);
} else {
@ -499,10 +497,7 @@ function item_new_uri($hostname, $uid, $guid = "") {
$uri = "urn:X-dfrn:" . $hostname . ':' . $uid . ':' . $hash;
$r = dba::select('item', array('id'), array('uri' => $uri), array('limit' => 1));
if (dbm::is_result($r)) {
$dups = true;
}
$dups = dba::exists('item', array('uri' => $uri));
} while ($dups == true);
return $uri;

View file

@ -292,8 +292,8 @@ function display_content(App $a, $update = 0) {
}
// We are displaying an "alternate" link if that post was public. See issue 2864
$items = dba::select('item', array('id'), array('id' => $item_id, 'private' => false, 'wall' => true));
if (dbm::is_result($items)) {
$is_public = dba::exists('item', array('id' => $item_id, 'private' => false, 'wall' => true));
if ($is_public) {
$alternate = App::get_baseurl().'/display/'.$nick.'/'.$item_id.'.atom';
} else {
$alternate = '';
@ -369,14 +369,14 @@ function display_content(App $a, $update = 0) {
$sql_extra = item_permissions_sql($a->profile['uid'],$remote_contact,$groups);
if ($update) {
$r = dba::exists("SELECT `id` FROM `item` WHERE `item`.`uid` = ?
$r = dba::p("SELECT `id` FROM `item` WHERE `item`.`uid` = ?
AND `item`.`parent` = (SELECT `parent` FROM `item` WHERE `id` = ?)
$sql_extra AND `unseen`",
$a->profile['uid'],
$item_id
);
if (!$r) {
if (dba::num_rows($r) == 0) {
return '';
}
}

View file

@ -793,9 +793,9 @@ function network_content(App $a, $update = 0) {
if (!$group && !$cid && !$star) {
$unseen = dba::select('item', array('id'), array('unseen' => true, 'uid' => local_user()), array('limit' => 1));
$unseen = dba::exists('item', array('unseen' => true, 'uid' => local_user()));
if (dbm::is_result($unseen)) {
if ($unseen) {
$r = dba::update('item', array('unseen' => false), array('uid' => local_user(), 'unseen' => true));
}
} elseif ($update_unseen) {

View file

@ -334,9 +334,8 @@ function profile_content(App $a, $update = 0) {
if ($is_owner) {
$unseen = dba::select('item', array('id'), array('wall' => true, 'unseen' => true, 'uid' => local_user()),
array('limit' => 1));
if (dbm::is_result($unseen)) {
$unseen = dba::exists('item', array('wall' => true, 'unseen' => true, 'uid' => local_user()));
if ($unseen) {
$r = dba::update('item', array('unseen' => false),
array('wall' => true, 'unseen' => true, 'uid' => local_user()));
}