summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--class/CommonTable.php39
-rw-r--r--testdb.php31
2 files changed, 69 insertions, 1 deletions
diff --git a/class/CommonTable.php b/class/CommonTable.php
index a7d029d..7e1dd8e 100644
--- a/class/CommonTable.php
+++ b/class/CommonTable.php
@@ -30,7 +30,7 @@
* @license http://www.gnu.org/licenses/agpl-3.0-standalone.html AGPL License 3.0 or (at your option) any later version
* @link http://github.com/remicollet/rpmphp/
* @since The begining of times.
-*/
+ */
abstract class CommonTable
{
@@ -117,6 +117,38 @@ abstract class CommonTable
}
/**
+ * Find a row from the table (the first matching criteria)
+ *
+ * @param hastable $crit array of field name => value
+ *
+ * @return hashtable (the first row)
+ */
+ function find(array $crit)
+ {
+ if (count($crit)) {
+ foreach ($this->request($crit) as $row) {
+ return $row;
+ }
+ }
+ return false;
+ }
+
+ /**
+ * Read a row from the table
+ *
+ * @param integer $id of the row
+ *
+ * @return hashtable
+ */
+ function get($id)
+ {
+ if (intval($id)>0) {
+ return $this->find(array('id'=>$id));
+ }
+ return false;
+ }
+
+ /**
* Delete a row in the table
*
* @param hashtable $crit of key => value
@@ -160,6 +192,11 @@ abstract class CommonTable
$link = 'SET';
foreach ($fields as $key => $value) {
+ if ($key=='id') {
+ // Don't update id
+ continue;
+ }
+
$sql .= "$link `$key`=";
if (is_null($value)) {
$sql .= 'NULL';
diff --git a/testdb.php b/testdb.php
index 3454245..85c78e3 100644
--- a/testdb.php
+++ b/testdb.php
@@ -35,4 +35,35 @@ if ($rpm->update(999, array('stamp'=>2)))
}
$all = $rpm->getAllRepo();
print_r(array_pop($all));
+
+echo "find(devel):";
+if ($row = $rpm->find(array('main'=>'devel'))) {
+ print_r($row);
+} else {
+ echo "not found\n";
+}
+echo "find(rawhide):";
+if ($row = $rpm->find(array('main'=>'rawhide'))) {
+ print_r($row);
+} else {
+ echo "not found\n";
+}
+echo "find():";
+if ($row = $rpm->find(array())) {
+ print_r($row);
+} else {
+ echo "not found\n";
+}
+echo "get(999):";
+if ($row = $rpm->get(999)) {
+ print_r($row);
+} else {
+ echo "not found\n";
+}
+echo "get(888):";
+if ($row = $rpm->get(888)) {
+ print_r($row);
+} else {
+ echo "not found\n";
+}
?>