diff --git a/boot.php b/boot.php index 9c0ef1d08..d5dc72ffb 100644 --- a/boot.php +++ b/boot.php @@ -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)); } diff --git a/include/dba.php b/include/dba.php index b44aa8fb7..7926fa10d 100644 --- a/include/dba.php +++ b/include/dba.php @@ -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'])) { - $param_string .= " LIMIT ".$params['limit']; - $single_row =($params['limit'] == 1); - } + if (isset($params['limit']) && is_int($params['limit'])) { + $param_string .= " LIMIT ".$params['limit']; + $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; diff --git a/include/identity.php b/include/identity.php index a50cb1c4a..44194c2fa 100644 --- a/include/identity.php +++ b/include/identity.php @@ -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'])) diff --git a/include/plugin.php b/include/plugin.php index 1ce13139f..1ea3ed73d 100644 --- a/include/plugin.php +++ b/include/plugin.php @@ -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)); } diff --git a/include/remove_contact.php b/include/remove_contact.php index 92f712255..9d4b1e4c3 100644 --- a/include/remove_contact.php +++ b/include/remove_contact.php @@ -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; } diff --git a/include/text.php b/include/text.php index 0a36a7b73..1d1515c17 100644 --- a/include/text.php +++ b/include/text.php @@ -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; diff --git a/mod/display.php b/mod/display.php index c3938117a..2bae9cf48 100644 --- a/mod/display.php +++ b/mod/display.php @@ -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 ''; } } diff --git a/mod/network.php b/mod/network.php index f498d17a4..395d1347b 100644 --- a/mod/network.php +++ b/mod/network.php @@ -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) { diff --git a/mod/profile.php b/mod/profile.php index 2af572b1c..c7bd13a3e 100644 --- a/mod/profile.php +++ b/mod/profile.php @@ -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())); }