From 12b1b6ecf7c8cc0e6ff418443787d7c247aa413a Mon Sep 17 00:00:00 2001
From: Remi Collet <fedora@famillecollet.com>
Date: Wed, 9 Jun 2010 01:24:25 +0800
Subject: add CommonTable->delete() method

---
 class/CommonTable.php | 42 ++++++++++++++++++++++++++++++++++++++++--
 1 file changed, 40 insertions(+), 2 deletions(-)

(limited to 'class/CommonTable.php')

diff --git a/class/CommonTable.php b/class/CommonTable.php
index 1ed47f8..261c7aa 100644
--- a/class/CommonTable.php
+++ b/class/CommonTable.php
@@ -73,6 +73,8 @@ abstract class CommonTable
      * Execute an SQL statement (INSERT, DELETE, ...)
      *
      * @param string $sql The SQL clause
+     *
+     * @param integer number of affected rows
      */
     protected function exec($sql)
     {
@@ -81,14 +83,17 @@ abstract class CommonTable
             $err = $this->db->errorInfo();
             throw new Exception($err[2]);
         }
+        return $res;
     }
 
     /**
      * Add a new row in the table
      *
      * @param hashtable $fields hashtable of fieldname => value
+     *
+     * @return integer primary key of inserted row
      */
-    protected function add(array $fields)
+    public function add(array $fields)
     {
         $col = array();
         $val = array();
@@ -99,14 +104,47 @@ abstract class CommonTable
             } else if (is_numeric($value)) {
                 $val[] = $value;
             } else {
-                $val[] = "'$value'";
+                $val[] = "'".addslashes($value)."'";
             }
         }
         $sql = "INSERT INTO `".$this->table."` (".implode(',', $col).")
                 VALUE (".implode(',', $val).")";
         $this->exec($sql);
+
+        $id = $this->db->lastInsertId();
+
+        return $id;
     }
 
+    /**
+     * Delete a row in the table
+     *
+     * @param hashtable $crit of key => value
+     *
+     * @return integer : number of row deleted
+     */
+    public function delete(array $crit)
+    {
+        $sql = "DELETE FROM `".$this->table."` ";
+
+        $link="WHERE";
+        foreach ($crit as $key => $value) {
+            $sql .= " $link `$key`";
+
+            if (is_null($value)) {
+                $sql .= 'IS NULL';
+            } else if (is_numeric($value)) {
+                $sql .= '='.$value;
+            } else {
+                $sql .= "='".addslashes($value)."'";
+            }
+
+            $link = "AND";
+        }
+        $nb = $this->exec($sql);
+
+        return $nb;
+    }
     /**
      * Create the table
      */
-- 
cgit 


From 06a6a4f777272421970bb827c898f6d36f1ab669 Mon Sep 17 00:00:00 2001
From: "Johan \"Papa\" Cwiklinski" <trasher@odysseus.(none)>
Date: Sun, 1 Aug 2010 10:55:01 +0200
Subject: Applying PEAR coding standards, refs #48

---
 class/CommonTable.php | 13 +++++++++----
 1 file changed, 9 insertions(+), 4 deletions(-)

(limited to 'class/CommonTable.php')

diff --git a/class/CommonTable.php b/class/CommonTable.php
index 8fd0471..51c0cd6 100644
--- a/class/CommonTable.php
+++ b/class/CommonTable.php
@@ -73,8 +73,8 @@ abstract class CommonTable
      * Execute an SQL statement (INSERT, DELETE, ...)
      *
      * @param string $sql The SQL clause
-     *
-     * @param integer number of affected rows
+    *
+     * @return integer number of affected rows
      */
     protected function exec($sql)
     {
@@ -148,6 +148,8 @@ abstract class CommonTable
 
     /**
      * Create the table
+    *
+    * @return void
      */
     abstract protected function createTable();
 
@@ -158,7 +160,10 @@ abstract class CommonTable
      *  foreach ($DB->request() as $ID => $data) { ... }
      *  foreach ($DB->request("ID=1") as $ID => $data) { ... }
      *  foreach ($DB->request("", "name") as $ID => $data) { ... }
-     *  foreach ($DB->request(array("name"=>"SBEI003W","entities_id"=>1),array("serial","otherserial")) { ... }
+     *  foreach ($DB->request(array(
+     *              "name"=>"SBEI003W",
+     *              "entities_id"=>1),
+     *              array("serial","otherserial")) { ... }
      *
      * @param string|array $crit string or array of field/values,
      *                           ex array("id"=>1), if empty => all rows
@@ -194,7 +199,7 @@ abstract class CommonTable
      *
      * @param string $fieldkey   name of the field to use as index
      * @param string $fieldvalue name of the field to use as value
-     * @param array $crit for request
+     * @param array  $crit       for request
      *
      * @return hashtable
      */
-- 
cgit