uw              Sat Mar  3 11:01:03 2001 EDT

  Modified files:              
    /php4/pear/Cache/Container  db.php 
  Log:
  Changes for the new cache structure
   - extended inline docs
   - extended calls to the still not existant call CacheError
   - several changed for the new cache structure
  
  WARNING: the code is untested, I don't have PEAR installed - Windows user.
  
  
  
Index: php4/pear/Cache/Container/db.php
diff -u php4/pear/Cache/Container/db.php:1.4 php4/pear/Cache/Container/db.php:1.5
--- php4/pear/Cache/Container/db.php:1.4        Fri Mar  2 06:16:38 2001
+++ php4/pear/Cache/Container/db.php    Sat Mar  3 11:01:03 2001
@@ -17,25 +17,42 @@
 // |          Chuck Hagenbuch <[EMAIL PROTECTED]>                           |
 // +----------------------------------------------------------------------+
 //
-// $Id: db.php,v 1.4 2001/03/02 14:16:38 uw Exp $
+// $Id: db.php,v 1.5 2001/03/03 19:01:03 uw Exp $
 
 require_once 'DB.php';
 require_once 'Cache/Container.php';
 
 /**
- * PEAR/DB Cache Container.
- * 
- *   CREATE TABLE cache (
- *     id       varchar(32) NOT NULL DEFAULT '',
- *     content  mediumtext NOT NULL DEFAULT '',
- *     expires  int(9) NOT NULL DEFAULT 0,
- *     changed  timestamp(14),
- *     INDEX(expires),
- *     PRIMARY KEY(id)
- *   )
- * 
+* PEAR/DB Cache Container.
+*
+* WARNING: Other systems might or might not support certain datatypes of 
+* the tables shown. As far as I know there's no large binary 
+* type in SQL-92 or SQL-99. Postgres seems to lack any 
+* BLOB or TEXT type, for MS-SQL you could use IMAGE, don't know 
+* about other databases. Please add sugestions for other databases to 
+* the inline docs.
+*
+* The field 'changed' has no meaning for the Cache itself. It's just there 
+* because it's a good idea to have an automatically updated timestamp
+* field for debugging in all of your tables.
+*
+* For _MySQL_ you need this DB table:
+*
+* CREATE TABLE cache (
+*   id          CHAR(32) NOT NULL DEFAULT '',
+*   cachegroup  VARCHAR(127) NOT NULL DEFAULT '',
+*   cachedata   BLOB NOT NULL DEFAULT '',
+*   userdata    VARCHAR(255) NOT NULL DEFAUL '',
+*   expires     INT(9) NOT NULL DEFAULT 0,
+*  
+*   changed     TIMESTAMP(14) NOT NULL,
+*  
+*   INDEX (expires),
+*   PRIMARY KEY (id, cachegroup)
+* )
+
  * @author   Sebastian Bergmann <[EMAIL PROTECTED]>
- * @version  $Id: db.php,v 1.4 2001/03/02 14:16:38 uw Exp $
+ * @version  $Id: db.php,v 1.5 2001/03/03 19:01:03 uw Exp $
  * @package  Cache
  */
 class Cache_Container_db extends Cache_Container {
@@ -79,28 +96,30 @@
         
         $this->db = DB::connect($this->dsn, true);
         if (DB::isError($this->db)) {
-            return new CacheError('DB::connect failed: ' . 
DB::errorMessage($this->db));
+            return new CacheError('DB::connect failed: ' . 
+DB::errorMessage($this->db), __FILE__, __LINE__);
         } else {
             $this->db->setFetchMode(DB_FETCHMODE_ASSOC);
         }
     }
     
     
-    function fetch($id)
+    function fetch($id, $group)
     {
-        $query = sprintf('SELECT content, expires FROM %s WHERE id = %s',
+        $query = sprintf("SELECT cachedata, userdata, expires FROM %s WHERE id = '%s' 
+AND cachegroup = '%s'",
                          $this->cache_table,
-                         "'" . $this->db->quoteString($id) . "'");
+                         $this->db->quoteString($id),
+                         $this->db->quoteString($group)
+                         );
         
         $res = $this->db->query($query);
 
         if (DB::isError($res))
-            return new CacheError('DB::query failed: ' . DB::errorMessage($res));
+            return new CacheError('DB::query failed: ' . DB::errorMessage($res), 
+__FILE__, __LINE__);
 
         $row = $res->fetchRow();
         
         if (is_array($row))
-            return array($row['expires'], $this->decode($row['content']));
+            return array($row['expires'], $this->decode($row['cachedata']), 
+$row['userdata']);
     }
     
     /**
@@ -110,61 +129,80 @@
     * MySQL specific. As MySQL is very popular the method should
     * work fine for 95% of you.
     */
-    function save($id, $data, $expires = 0)
+    function save($id, $data, $expires, $group, $userdata)
     {
-        $query = sprintf('REPLACE INTO %s (content, expires, id) VALUES (%s, %d, %s)',
+        
+        $this->flushPreload($id, $group);
+        
+        $query = sprintf("REPLACE INTO %s (userdata, cachedata, expires, id, 
+cachegroup) VALUES ('%s', '%s', %d, '%s', '%s')",
                          $this->cache_table,
-                         "'" . $this->db->quoteString($this->encode(($data))) . "'",
+                         $this->db->quoteString($userdata),
+                         $this->db->quoteString($this->encode(($data))),
                          ($expires) ? $expires + time() : 0,
-                         "'" . $this->db->quoteString($id) . "'");
+                         $this->db->quoteString($id),
+                         $this->db->quoteString($group)
+                        );
         
         $res = $this->db->query($query);
         
         if (DB::isError($res)) {
-            return new CacheError('DB::query failed: ' . DB::errorMessage($res));
+            return new CacheError('DB::query failed: ' . DB::errorMessage($res), 
+__FILE__, __LINE__);
         }
     }
 
 
-    function delete($id)
+    function delete($id, $group)
     {
-        $query = sprintf('DELETE FROM %s WHERE id = %s',
+        $this->flushPreload($id, $group);
+        
+        $query = sprintf("DELETE FROM %s WHERE id = '%s' and cachegroup = '%s'",
                          $this->cache_table,
-                         "'" . $this->db->quoteString($id) . "'");
+                         $this->db->quoteString($id),
+                         $this->db->quoteString($group)
+                        );
         
         $res = $this->db->query($query);
         
         if (DB::isError($res))
-            return new CacheError('DB::query failed: ' . DB::errorMessage($res));
+            return new CacheError('DB::query failed: ' . DB::errorMessage($res), 
+__FILE__, __LINE__);
     }
 
 
-    function flush()
+    function flush($group = "")
     {
-        $query = sprintf('DELETE FROM %s', $this->cache_table);
+    
+        $this->flushPreload();
+        
+         if ($group) {
+            $query = sprintf("DELETE FROM %s WHERE cachegroup = '%s'", 
+$this->cache_table, $this->db->quoteString($group));    
+        } else {
+            $query = sprintf("DELETE FROM %s", $this->cache_table);    
+        }
         
         $res = $this->db->query($query);
 
         if (DB::isError($res))
-            return new CacheError('DB::query failed: ' . DB::errorMessage($res));
+            return new CacheError('DB::query failed: ' . DB::errorMessage($res), 
+__FILE__, __LINE__);
     }
 
 
-    function idExists($id)
+    function idExists($id, $group)
     {
-        $query = sprintf('SELECT id FROM %s WHERE ID = %s', 
+        $query = sprintf("SELECT id FROM %s WHERE ID = '%s' AND cachegroup = '%s'", 
                          $this->cache_table,
-                         "'" . $this->db->quoteString($id) . "'");
+                         $this->db->quoteString($id),
+                         $this->db->quoteString($group)
+                        );
         
         $res = $this->db->query($query);
         
         if (DB::isError($res))
-            return new CacheError('DB::query failed: ' . DB::errorMessage($res));
+            return new CacheError('DB::query failed: ' . DB::errorMessage($res), 
+__FILE__, __LINE__);
 
         $row = $res->fetchRow();
         
         if (is_array($row)) {
-            return $row['id'];
+            return true;
         } else {
             return false;
         }
@@ -179,7 +217,7 @@
         $res = $this->db->query($query);
         
         if (DB::isError($res)) {
-            return new CacheError('DB::query failed: ' . DB::errorMessage($res));
+            return new CacheError('DB::query failed: ' . DB::errorMessage($res), 
+__FILE__, __LINE__);
         }
     }
 



-- 
PHP CVS Mailing List (http://www.php.net/)
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
To contact the list administrators, e-mail: [EMAIL PROTECTED]

Reply via email to