Advertisement
Guest User

Hans

a guest
Sep 6th, 2007
126
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 3.75 KB | None | 0 0
  1. <?php
  2.  
  3. class PDOStreamWrapper {
  4.     private $pk;
  5.     private static $dbh;
  6.     private $fp;
  7.     private $writeMode = false;
  8.    
  9.     public static function setPDO(PDO $pdo)
  10.     {
  11.         self::$dbh = $pdo;
  12.     }
  13.    
  14.     public function stream_open($path, $mode, $options, $opened_path)
  15.     {
  16.         $this->pk = parse_url($path, PHP_URL_HOST);
  17.  
  18.         $stmt = self::$dbh->prepare('SELECT file_contents FROM lob_test WHERE pk = ?');
  19.         $stmt->bindValue(1, $this->pk, PDO::PARAM_INT);
  20.         $stmt->execute();
  21.         $res = $stmt->fetchColumn();
  22.        
  23.         if (!$res) {
  24.             trigger_error("No matching file in database.", E_USER_ERROR);
  25.             // there's no such item in the database
  26.             return false;
  27.         }
  28.  
  29.         if ($mode{0} == 'w' || $mode == 'r+') {
  30.             $this->writeMode = true;
  31.             trigger_error("File is being opened in WRITE mode (Forcing fopen with 'r+').", E_USER_NOTICE);
  32.             // If we are writing a file, then we should open up a temporary
  33.             // stream which we will update the contents of the database row with
  34.             // at stream flush time
  35.             $this->fp = fopen("php://temp", 'r+');
  36.         } elseif ($mode == 'r') {
  37.             // if we're reading, then we expect there to be an existing row for the
  38.             // data and so we should return the results from the PDO resultset.
  39.             trigger_error("File is being opened in READ mode (Using PDO stream resource).", E_USER_NOTICE);
  40.             $this->fp = $res;
  41.         } else {
  42.             trigger_error("Invalid mode: " . $mode, E_USER_ERROR);
  43.             return false;
  44.         }
  45.  
  46.         return true;
  47.     }
  48.  
  49.     /**
  50.      * Flushes stream resource.
  51.      */
  52.     public function stream_flush()
  53.     {
  54.         if ($this->writeMode) {
  55.             fflush($this->fp); // this shouldn't have any effect for memory streams anyway
  56.             $stmt = self::$dbh->prepare('SELECT pk FROM lob_test WHERE pk = ?');
  57.             $stmt->bindValue(1, $this->pk, PDO::PARAM_INT);
  58.             $stmt->execute();
  59.             if ($stmt->fetchColumn()) {
  60.                 $sql = 'UPDATE lob_test SET file_contents = ? WHERE pk = ?';
  61.             } else {
  62.                 $sql = 'INSERT INTO lob_test (file_contents, pk) VALUES (?, ?)';
  63.             }
  64.             $updateStmt = self::$dbh->prepare($sql);
  65.             $updateStmt->bindValue(1, $this->fp, PDO::PARAM_LOB);
  66.             $updateStmt->bindValue(2, $this->pk, PDO::PARAM_INT);
  67.             $updateStmt->execute();
  68.         }
  69.     }
  70.  
  71.     public function stream_read($count)
  72.     {
  73.         return fread($this->fp, $count);
  74.     }
  75.  
  76.     public function stream_write($data)
  77.     {
  78.         return fwrite($this->fp, $data);
  79.     }
  80.  
  81.     public function stream_eof()
  82.     {
  83.         return feof($this->fp);
  84.     }
  85.  
  86.     public function stream_tell()
  87.     {
  88.         return ftell($this->fp);
  89.     }
  90.  
  91.     public function stream_seek($offset, $whence)
  92.     {
  93.         return fseek($this->fp, $offset, $whence);
  94.     }
  95.  
  96.     public function stream_close()
  97.     {
  98.         return fclose($this->fp);
  99.     }
  100. }
  101.  
  102.  
  103. // Establish connection
  104. $dbh = new PDO('pgsql:dbname=testdb user=test');
  105. $dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
  106.  
  107. // Setup stream wrapper
  108. PDOStreamWrapper::setPDO($dbh);
  109. stream_wrapper_register('pdotest', 'PDOStreamWrapper');
  110.  
  111. // Attempt to create table
  112. try {
  113.     $dbh->exec('CREATE TABLE lob_test( pk INTEGER NOT NULL, file_contents BYTEA, PRIMARY KEY (pk))');
  114. } catch (PDOException $x) {
  115.     trigger_error($x->getMessage(), E_USER_WARNING);
  116.     // Assume that table already exists; allow continue
  117. }
  118.  
  119. $dbh->exec('DELETE FROM lob_test');
  120.  
  121. // 1) Insert value!
  122. $tmpfp = fopen("php://temp", "r+");
  123. fwrite($tmpfp, "This is just a database test!");
  124. rewind($tmpfp);
  125.  
  126. /// print "Debug dump of \$fp: "; debug_zval_dump($fp);
  127.  
  128. $sql = "INSERT INTO lob_test (pk, file_contents) VALUES (?,?)";
  129. $stmt = $dbh->prepare($sql);
  130. $stmt->bindValue(1, 201, PDO::PARAM_INT);
  131. $stmt->bindValue(2, $tmpfp, PDO::PARAM_LOB);
  132. $stmt->execute();
  133.  
  134. fclose($tmpfp);
  135. unset($stmt);
  136.  
  137. $fp = fopen("pdotest://201", "r+");
  138. if (!$fp) {
  139.     throw new Exception("Unable to open stream for write.");
  140. }
  141.  
  142. fwrite($fp, "This is rewritten data!");
  143. fclose($fp);
  144.  
  145. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement