1
0
mirror of https://github.com/Yubico/yubikey-val.git synced 2025-04-09 20:53:50 +02:00

Cosmetic changes.

- make it easier to understand query construction.
This commit is contained in:
Jean Paul Galea 2015-08-18 12:03:58 +02:00
parent 0c3a48ee63
commit b76ffc313f

View File

@ -126,60 +126,79 @@ class DbImpl extends Db
if($result) $result->closeCursor(); if($result) $result->closeCursor();
} }
/** /**
* main function used to get rows by multiple key=>value pairs from Db table. * Main function used to get rows by multiple key=>value pairs from Db table.
* *
* @param string $table Database table to update row in * @param string $table Database table to update row in
* @param array $where Array with column=>values to select rows by * @param array $where Array with column=>values to select rows by
* @param int $nr Number of rows to collect. NULL=>inifinity. Default=NULL. * @param int $nr Number of rows to collect. NULL=>inifinity. Default=NULL.
* @param int $rev rev=1 indicates order should be reversed. Default=NULL. * @param int $rev rev=1 indicates order should be reversed. Default=NULL.
* @param string distinct Select rows with distinct columns, Default=NULL * @param string distinct Select rows with distinct columns, Default=NULL
* @return mixed Array with values from Db row or 2d-array with multiple rows *
* * @return mixed Array with values from Db row or 2d-array with multiple rows
*/ */
public function findByMultiple($table, $where, $nr=null, $rev=null, $distinct=null) public function findByMultiple($table, $where, $nr=NULL, $rev=NULL, $distinct=NULL)
{ {
$value=""; /* quiet the PHP Notice */ $value = '';
$match=null; /* quiet the PHP Notice */ $match = NULL;
$query="SELECT"; $query = 'SELECT';
if ($distinct!=null) {
$query.= " DISTINCT " . $distinct; if ($distinct != NULL)
} else { $query.= " DISTINCT " . $distinct;
$query.= " *"; else
} $query.= " *";
$query.= " FROM " . $table;
if ($where!=null){ $query.= " FROM " . $table;
foreach ($where as $key=>$value) {
if ($key!=null) { if ($where != NULL)
if ($value!=null) $match.= " ". $key . " = '" . $value . "' and"; {
else $match.= " ". $key . " is NULL and"; foreach ($where as $key => $value)
{
if ($key != NULL)
{
if ($value != NULL)
$match .= " ". $key . " = '" . $value . "' and";
else
$match .= " ". $key . " is NULL and";
}
}
if ($match != NULL)
$query .= " WHERE" . $match;
$query = rtrim($query, "and");
$query = rtrim($query);
}
if ($rev == 1)
$query.= " ORDER BY id DESC";
if ($nr != NULL)
$query.= " LIMIT " . $nr;
$result = $this->query($query, true);
if (!$result)
return false;
if ($nr == 1)
{
$row = $this->fetchArray($result);
$this->closeCursor($result);
return $row;
}
else
{
$collection = array();
while($row = $this->fetchArray($result))
$collection[] = $row;
$this->closeCursor($result);
return $collection;
}
} }
}
if ($match!=null) $query .= " WHERE" . $match;
$query=rtrim($query, "and");
$query=rtrim($query);
}
if ($rev==1) $query.= " ORDER BY id DESC";
if ($nr!=null) $query.= " LIMIT " . $nr;
$result = $this->query($query, true);
if (!$result) return false;
if ($nr==1) {
$row = $this->fetchArray($result);
$this->closeCursor($result);
return $row;
}
else {
$collection=array();
while($row = $this->fetchArray($result)){
$collection[]=$row;
}
$this->closeCursor($result);
return $collection;
}
}
/** /**
* main function used to delete rows by multiple key=>value pairs from Db table. * main function used to delete rows by multiple key=>value pairs from Db table.