1
1
Fork 0

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); array_shift($argv);
$parameters = json_encode($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)); dba::insert('workerqueue', array('parameter' => $parameters, 'created' => $created, 'priority' => $priority));
} }

View file

@ -767,13 +767,19 @@ class dba {
/** /**
* @brief Check if data exists * @brief Check if data exists
* *
* @param string $sql SQL statement * @param string $table Table name
* @return boolean Are there rows for that query? * @param array $condition array of fields for condition
*
* @return boolean Are there rows for that condition?
*/ */
static public function exists($sql) { static public function exists($table, $condition) {
$params = self::getParam(func_get_args()); 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)) { if (is_bool($stmt)) {
$retval = $stmt; $retval = $stmt;
@ -1270,11 +1276,13 @@ class dba {
$param_string = substr($param_string, 0, -2); $param_string = substr($param_string, 0, -2);
} }
if (isset($params['limit'])) { if (isset($params['limit']) && is_int($params['limit'])) {
if (is_int($params['limit'])) { $param_string .= " LIMIT ".$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; $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"]); $profile_url = normalise_link(App::get_baseurl()."/profile/".$profile["nickname"]);
} }
$r = dba::select('contact', array('id'), $connect = !dba::exists('contact', array('pending' => false, 'uid' => local_user(), 'nurl' => $profile_url));
array('pending' => false, 'uid' => local_user(), 'nurl' => $profile_url), array('limit' => 1));
if (dbm::is_result($r))
$connect = false;
} }
if ($connect && ($profile['network'] != NETWORK_DFRN) && !isset($profile['remoteconnect'])) if ($connect && ($profile['network'] != NETWORK_DFRN) && !isset($profile['remoteconnect']))

View file

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

View file

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

View file

@ -488,8 +488,6 @@ if (! function_exists('item_new_uri')) {
function item_new_uri($hostname, $uid, $guid = "") { function item_new_uri($hostname, $uid, $guid = "") {
do { do {
$dups = false;
if ($guid == "") { if ($guid == "") {
$hash = get_guid(32); $hash = get_guid(32);
} else { } else {
@ -499,10 +497,7 @@ function item_new_uri($hostname, $uid, $guid = "") {
$uri = "urn:X-dfrn:" . $hostname . ':' . $uid . ':' . $hash; $uri = "urn:X-dfrn:" . $hostname . ':' . $uid . ':' . $hash;
$r = dba::select('item', array('id'), array('uri' => $uri), array('limit' => 1)); $dups = dba::exists('item', array('uri' => $uri));
if (dbm::is_result($r)) {
$dups = true;
}
} while ($dups == true); } while ($dups == true);
return $uri; 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 // 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)); $is_public = dba::exists('item', array('id' => $item_id, 'private' => false, 'wall' => true));
if (dbm::is_result($items)) { if ($is_public) {
$alternate = App::get_baseurl().'/display/'.$nick.'/'.$item_id.'.atom'; $alternate = App::get_baseurl().'/display/'.$nick.'/'.$item_id.'.atom';
} else { } else {
$alternate = ''; $alternate = '';
@ -369,14 +369,14 @@ function display_content(App $a, $update = 0) {
$sql_extra = item_permissions_sql($a->profile['uid'],$remote_contact,$groups); $sql_extra = item_permissions_sql($a->profile['uid'],$remote_contact,$groups);
if ($update) { 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` = ?) AND `item`.`parent` = (SELECT `parent` FROM `item` WHERE `id` = ?)
$sql_extra AND `unseen`", $sql_extra AND `unseen`",
$a->profile['uid'], $a->profile['uid'],
$item_id $item_id
); );
if (!$r) { if (dba::num_rows($r) == 0) {
return ''; return '';
} }
} }

View file

@ -793,9 +793,9 @@ 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 = 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)); $r = dba::update('item', array('unseen' => false), array('uid' => local_user(), 'unseen' => true));
} }
} elseif ($update_unseen) { } elseif ($update_unseen) {

View file

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