VFS
[ class tree: VFS ] [ index: VFS ] [ all elements ]

Source for file sql.php

Documentation is available at sql.php

  1. <?php
  2.  
  3. /** @constant integer VFS_FILE  File value for vfs_type column. */
  4. define('VFS_FILE'1);
  5.  
  6. /** @constant integer VFS_FOLDER  Folder value for vfs_type column. */
  7. define('VFS_FOLDER'2);
  8.  
  9. /**
  10.  * VFS implementation for PHP's PEAR database abstraction layer.
  11.  *
  12.  * <pre>
  13.  * Required values for $params:
  14.  *      'phptype'       The database type (ie. 'pgsql', 'mysql, etc.).
  15.  *      'hostspec'      The hostname of the database server.
  16.  *      'protocol'      The communication protocol ('tcp', 'unix', etc.).
  17.  *      'username'      The username with which to connect to the database.
  18.  *      'password'      The password associated with 'username'.
  19.  *      'database'      The name of the database.
  20.  *
  21.  * Optional values:
  22.  *      'table'         The name of the vfs table in 'database'. Defaults to
  23.  *                      'horde_vfs'.
  24.  *
  25.  * Required by some database implementations:
  26.  *      'options'       Additional options to pass to the database.
  27.  *      'tty'           The TTY on which to connect to the database.
  28.  *      'port'          The port on which to connect to the database.
  29.  * </pre>
  30.  *
  31.  * The table structure for the VFS can be found in
  32.  * data/vfs.sql.
  33.  *
  34.  * Database specific notes:
  35.  *
  36.  * MSSQL:
  37.  * <pre>
  38.  * - The vfs_data field must be of type IMAGE.
  39.  * - You need the following php.ini settings:
  40.  *    ; Valid range 0 - 2147483647. Default = 4096.
  41.  *    mssql.textlimit = 0 ; zero to pass through
  42.  *
  43.  *    ; Valid range 0 - 2147483647. Default = 4096.
  44.  *    mssql.textsize = 0 ; zero to pass through
  45.  * </pre>
  46.  *
  47.  * $Horde: framework/VFS/VFS/sql.php,v 1.92 2005/04/07 13:24:23 jan Exp $
  48.  *
  49.  * Copyright 2002-2005 Chuck Hagenbuch <chuck@horde.org>
  50.  *
  51.  * See the enclosed file COPYING for license information (LGPL). If you
  52.  * did not receive this file, see http://www.fsf.org/copyleft/lgpl.html.
  53.  *
  54.  * @author  Chuck Hagenbuch <chuck@horde.org>
  55.  * @since   Horde 2.2
  56.  * @package VFS
  57.  */
  58. class VFS_sql extends VFS {
  59.  
  60.     /**
  61.      * Handle for the current database connection.
  62.      *
  63.      * @var object DB $_db 
  64.      */
  65.     var $_db = false;
  66.  
  67.     /**
  68.      * Retrieve a file from the VFS.
  69.      *
  70.      * @access public
  71.      *
  72.      * @param string $path  The pathname to the file.
  73.      * @param string $name  The filename to retrieve.
  74.      *
  75.      * @return string  The file data.
  76.      */
  77.     function read($path$name)
  78.     {
  79.         $conn $this->_connect();
  80.         if (is_a($conn'PEAR_Error')) {
  81.             return $conn;
  82.         }
  83.  
  84.         return $this->_readBlob($this->_params['table']'vfs_data',
  85.                                 array('vfs_path' => $path,
  86.                                       'vfs_name' => $name));
  87.     }
  88.  
  89.     /**
  90.      * Stores a file in the VFS.
  91.      *
  92.      * @access public
  93.      *
  94.      * @param string $path         The path to store the file in.
  95.      * @param string $name         The filename to use.
  96.      * @param string $tmpFile      The temporary file containing the data to
  97.      *                              be stored.
  98.      * @param boolean $autocreate  Automatically create directories?
  99.      *
  100.      * @return mixed  True on success or a PEAR_Error object on failure.
  101.      */
  102.     function write($path$name$tmpFile$autocreate = false)
  103.     {
  104.         $size filesize($tmpFile);
  105.         if ($size === 0{
  106.             $data '';
  107.         else {
  108.             $dataFP @fopen($tmpFile'rb');
  109.             if (!$dataFP{
  110.                 return PEAR::raiseError(sprintf(_("The temporary file \"%s\" does not exist.")$tmpFile));
  111.             }
  112.             $data @fread($dataFP$size);
  113.             @fclose($dataFP);
  114.         }
  115.  
  116.         return $this->writeData($path$name$data$autocreate);
  117.     }
  118.  
  119.     /**
  120.      * Store a file in the VFS from raw data.
  121.      *
  122.      * @access public
  123.      *
  124.      * @param string $path                  The path to store the file in.
  125.      * @param string $name                  The filename to use.
  126.      * @param string $data                  The file data.
  127.      * @param optional boolean $autocreate  Automatically create directories?
  128.      *
  129.      * @return mixed  True on success or a PEAR_Error object on failure.
  130.      */
  131.     function writeData($path$name$data$autocreate = false)
  132.     {
  133.         $conn $this->_connect();
  134.         if (is_a($conn'PEAR_Error')) {
  135.             return $conn;
  136.         }
  137.  
  138.         /* Check to see if the data already exists. */
  139.         $sql sprintf('SELECT vfs_id FROM %s WHERE vfs_path %s AND vfs_name = %s',
  140.                        $this->_params['table'],
  141.                        (empty($path&& $this->_db->dbsyntax == 'oci8'' IS NULL' ' = ' $this->_db->quote($path),
  142.                        $this->_db->quote($name));
  143.         $this->log($sqlPEAR_LOG_DEBUG);
  144.         $id $this->_db->getOne($sql);
  145.  
  146.         if (is_a($id'PEAR_Error')) {
  147.             $this->log($idPEAR_LOG_ERR);
  148.             return $id;
  149.         }
  150.  
  151.         if (!is_null($id)) {
  152.             return $this->_updateBlob($this->_params['table']'vfs_data',
  153.                                       $dataarray('vfs_id' => $id),
  154.                                       array('vfs_modified' => time()));
  155.         else {
  156.             /* Check to see if the folder already exists. */
  157.             $dirs explode('/'$path);
  158.             $path_name array_pop($dirs);
  159.             $parent implode('/'$dirs);
  160.             if (!$this->isFolder($parent$path_name)) {
  161.                 if (!$autocreate{
  162.                     return PEAR::raiseError(sprintf(_("Folder %s does not exist")$path)'horde.error');
  163.                 else {
  164.                     $result $this->autocreatePath($path);
  165.                     if (is_a($result'PEAR_Error')) {
  166.                         return $result;
  167.                     }
  168.                 }
  169.             }
  170.  
  171.             $id $this->_db->nextId($this->_params['table']);
  172.             if (is_a($id'PEAR_Error')) {
  173.                 $this->log($idPEAR_LOG_ERR);
  174.                 return $id;
  175.             }
  176.             return $this->_insertBlob($this->_params['table']'vfs_data',
  177.                                       $dataarray('vfs_id' => $id,
  178.                                                    'vfs_type' => VFS_FILE,
  179.                                                    'vfs_path' => $path,
  180.                                                    'vfs_name' => $name,
  181.                                                    'vfs_modified' => time(),
  182.                                                    'vfs_owner' => $this->_params['user']));
  183.         }
  184.     }
  185.  
  186.     /**
  187.      * Delete a file from the VFS.
  188.      *
  189.      * @access public
  190.      *
  191.      * @param string $path  The path to store the file in.
  192.      * @param string $name  The filename to use.
  193.      *
  194.      * @return mixed  True on success or a PEAR_Error object on failure.
  195.      */
  196.     function deleteFile($path$name)
  197.     {
  198.         $conn $this->_connect();
  199.         if (is_a($conn'PEAR_Error')) {
  200.             return $conn;
  201.         }
  202.  
  203.         $sql sprintf('DELETE FROM %s WHERE vfs_type = %s AND vfs_path %s AND vfs_name = %s',
  204.                        $this->_params['table'],
  205.                        $this->_db->quote(VFS_FILE),
  206.                        (empty($path&& $this->_db->dbsyntax == 'oci8'' IS NULL' ' = ' $this->_db->quote($path),
  207.                        $this->_db->quote($name));
  208.         $this->log($sqlPEAR_LOG_DEBUG);
  209.         $result $this->_db->query($sql);
  210.  
  211.         if ($this->_db->affectedRows(== 0{
  212.             return PEAR::raiseError(_("Unable to delete VFS file."));
  213.         }
  214.  
  215.         return $result;
  216.     }
  217.  
  218.     /**
  219.      * Rename a file or folder in the VFS.
  220.      *
  221.      * @access public
  222.      *
  223.      * @param string $oldpath  The old path to the file.
  224.      * @param string $oldname  The old filename.
  225.      * @param string $newpath  The new path of the file.
  226.      * @param string $newname  The new filename.
  227.      *
  228.      * @return mixed  True on success or a PEAR_Error object on failure.
  229.      */
  230.     function rename($oldpath$oldname$newpath$newname)
  231.     {
  232.         $conn $this->_connect();
  233.         if (is_a($conn'PEAR_Error')) {
  234.             return $conn;
  235.         }
  236.  
  237.         $values = array();
  238.  
  239.         $sql  'UPDATE ' $this->_params['table'];
  240.         $sql .= ' SET vfs_path = ?, vfs_name = ?, vfs_modified = ? WHERE vfs_path = ? AND vfs_name = ?';
  241.         $this->log($sqlPEAR_LOG_DEBUG);
  242.  
  243.         array_push($values$newpath$newnametime()$oldpath$oldname);
  244.  
  245.         $sth $this->_db->prepare($sql);
  246.         $result $this->_db->execute($sth$values);
  247.  
  248.         if ($this->_db->affectedRows(== 0{
  249.             return PEAR::raiseError(_("Unable to rename VFS file."));
  250.         }
  251.  
  252.         $rename $this->_recursiveRename($oldpath$oldname$newpath$newname);
  253.         if (is_a($rename'PEAR_Error')) {
  254.             $this->log($renamePEAR_LOG_ERR);
  255.             return PEAR::raiseError(sprintf(_("Unable to rename VFS directory: %s.")$rename->getMessage()));
  256.         }
  257.  
  258.         return $result;
  259.     }
  260.  
  261.     /**
  262.      * Creates a folder on the VFS.
  263.      *
  264.      * @access public
  265.      *
  266.      * @param string $path  Holds the path of directory to create folder.
  267.      * @param string $name  Holds the name of the new folder.
  268.      *
  269.      * @return mixed  True on success or a PEAR_Error object on failure.
  270.      */
  271.     function createFolder($path$name)
  272.     {
  273.         $conn $this->_connect();
  274.         if (is_a($conn'PEAR_Error')) {
  275.             return $conn;
  276.         }
  277.  
  278.         $id $this->_db->nextId($this->_params['table']);
  279.         if (is_a($id'PEAR_Error')) {
  280.             $this->log($idPEAR_LOG_ERR);
  281.             return $id;
  282.         }
  283.  
  284.         $values = array();
  285.  
  286.         $sql  'INSERT INTO ' $this->_params['table'];
  287.         $sql .= ' (vfs_id, vfs_type, vfs_path, vfs_name, vfs_modified, vfs_owner) VALUES (?, ?, ?, ?, ?, ?)';
  288.         $this->log($sqlPEAR_LOG_DEBUG);
  289.  
  290.         array_push($values$idVFS_FOLDER$path$nametime()$this->_params['user']);
  291.  
  292.         $sth $this->_db->prepare($sql);
  293.         return $this->_db->execute($sth$values);
  294.     }
  295.  
  296.     /**
  297.      * Delete a folder from the VFS.
  298.      *
  299.      * @access public
  300.      *
  301.      * @param string $path                 The path of the folder.
  302.      * @param string $name                 The folder name to use.
  303.      * @param optional boolean $recursive  Force a recursive delete?
  304.      *
  305.      * @return mixed  True on success or a PEAR_Error object on failure.
  306.      */
  307.     function deleteFolder($path$name$recursive = false)
  308.     {
  309.         $conn $this->_connect();
  310.         if (is_a($conn'PEAR_Error')) {
  311.             return $conn;
  312.         }
  313.  
  314.         $folderPath $this->_getNativePath($path$name);
  315.  
  316.         /* Check if not recursive and fail if directory not empty */
  317.         if (!$recursive{
  318.             $folderList $this->listFolder($folderPathnulltrue);
  319.             if (is_a($folderList'PEAR_Error')) {
  320.                 $this->log($folderListPEAR_LOG_ERR);
  321.                 return $folderList;
  322.             elseif (!empty($folderList)) {
  323.                 return PEAR::raiseError(sprintf(_("Unable to delete %s, the directory is not empty"),
  324.                                                 $path '/' $name));
  325.             }
  326.         }
  327.  
  328.         /* First delete everything below the folder, so if error we
  329.          * get no orphans */
  330.         $sql sprintf('DELETE FROM %s WHERE vfs_path %s',
  331.                        $this->_params['table'],
  332.                        (empty($folderPath&& $this->_db->dbsyntax == 'oci8'' IS NULL' ' LIKE ' $this->_db->quote($this->_getNativePath($folderPath'%')));
  333.         $this->log($sqlPEAR_LOG_DEBUG);
  334.         $deleteContents $this->_db->query($sql);
  335.         if (is_a($deleteContents'PEAR_Error')) {
  336.             $this->log($deleteContentsPEAR_LOG_ERR);
  337.             return PEAR::raiseError(sprintf(_("Unable to delete VFS recursively: %s.")$deleteContents->getMessage()));
  338.         }
  339.  
  340.         /* Now delete everything inside the folder. */
  341.         $sql sprintf('DELETE FROM %s WHERE vfs_path %s',
  342.                        $this->_params['table'],
  343.                        (empty($path&& $this->_db->dbsyntax == 'oci8'' IS NULL' ' = ' $this->_db->quote($folderPath));
  344.         $this->log($sqlPEAR_LOG_DEBUG);
  345.         $delete $this->_db->query($sql);
  346.         if (is_a($delete'PEAR_Error')) {
  347.             $this->log($deletePEAR_LOG_ERR);
  348.             return PEAR::raiseError(sprintf(_("Unable to delete VFS directory: %s.")$delete->getMessage()));
  349.         }
  350.  
  351.         /* All ok now delete the actual folder */
  352.         $sql sprintf('DELETE FROM %s WHERE vfs_path %s AND vfs_name = %s',
  353.                        $this->_params['table'],
  354.                        (empty($path&& $this->_db->dbsyntax == 'oci8'' IS NULL' ' = ' $this->_db->quote($path),
  355.                        $this->_db->quote($name));
  356.         $this->log($sqlPEAR_LOG_DEBUG);
  357.         $delete $this->_db->query($sql);
  358.         if (is_a($delete'PEAR_Error')) {
  359.             $this->log($deletePEAR_LOG_ERR);
  360.             return PEAR::raiseError(sprintf(_("Unable to delete VFS directory: %s.")$delete->getMessage()));
  361.         }
  362.  
  363.         return $delete;
  364.     }
  365.  
  366.     /**
  367.      * Return a list of the contents of a folder.
  368.      *
  369.      * @access public
  370.      *
  371.      * @param string $path                The directory path.
  372.      * @param optional mixed $filter      String/hash of items to filter based
  373.      *                                     on filename.
  374.      * @param optional boolean $dotfiles  Show dotfiles?
  375.      * @param optional boolean $dironly   Show directories only?
  376.      *
  377.      * @return mixed  File list on success or false on failure.
  378.      */
  379.     function _listFolder($path$filter = null$dotfiles = true,
  380.                         $dironly = false)
  381.     {
  382.         $conn $this->_connect();
  383.         if (is_a($conn'PEAR_Error')) {
  384.             return $conn;
  385.         }
  386.  
  387.         $files = array();
  388.         $fileList = array();
  389.  
  390.         // Fix for Oracle not differentiating between '' and NULL.
  391.         if (empty($path&& $this->_db->dbsyntax == 'oci8'{
  392.             $where 'vfs_path IS NULL';
  393.         else {
  394.             $where 'vfs_path = ' $this->_db->quote($path);
  395.         }
  396.  
  397.         $sql sprintf('SELECT vfs_name, vfs_type, vfs_data, vfs_modified, vfs_owner FROM %s WHERE %s',
  398.                             $this->_params['table'],
  399.                             $where);
  400.         $this->log($sqlPEAR_LOG_DEBUG);
  401.         $fileList $this->_db->getAll($sql);
  402.         if (is_a($fileList'PEAR_Error')) {
  403.             return $fileList;
  404.         }
  405.  
  406.         foreach ($fileList as $line{
  407.             // Filter out dotfiles if they aren't wanted.
  408.             if (!$dotfiles && substr($line[0]01== '.'{
  409.                 continue;
  410.             }
  411.  
  412.             $file['name'$line[0];
  413.  
  414.             if ($line[1== VFS_FILE{
  415.                 $name explode('.'$line[0]);
  416.  
  417.                 if (count($name== 1{
  418.                     $file['type''**none';
  419.                 else {
  420.                     $file['type'VFS::strtolower($name[count($name- 1]);
  421.                 }
  422.  
  423.                 $file['size'VFS::strlen($line[2]);
  424.             elseif ($line[1== VFS_FOLDER{
  425.                 $file['type''**dir';
  426.                 $file['size'= -1;
  427.             }
  428.  
  429.             $file['date'$line[3];
  430.             $file['owner'$line[4];
  431.             $file['perms''-';
  432.             $file['group''-';
  433.  
  434.             // filtering
  435.             if ($this->_filterMatch($filter$file['name'])) {
  436.                 unset($file);
  437.                 continue;
  438.             }
  439.             if ($dironly && $file['type'!== '**dir'{
  440.                 unset($file);
  441.                 continue;
  442.             }
  443.  
  444.             $files[$file['name']] $file;
  445.             unset($file);
  446.        }
  447.  
  448.         return $files;
  449.     }
  450.  
  451.     /**
  452.      * Returns a sorted list of folders in specified directory.
  453.      *
  454.      * @access public
  455.      *
  456.      * @param optional string $path         The path of the directory to get
  457.      *                                       the directory list for.
  458.      * @param optional mixed $filter        String/hash of items to filter
  459.      *                                       based on folderlist.
  460.      * @param optional boolean $dotfolders  Include dotfolders?
  461.      *
  462.      * @return mixed  Folder list on success or PEAR_Error object on failure.
  463.      */
  464.     function listFolders($path ''$filter = array()$dotfolders = true)
  465.     {
  466.         $conn $this->_connect();
  467.         if (is_a($conn'PEAR_Error')) {
  468.             return $conn;
  469.         }
  470.  
  471.         $values = array();
  472.  
  473.         $sql  'SELECT vfs_name, vfs_path FROM ' $this->_params['table'];
  474.         $sql .= ' WHERE vfs_path = ? AND vfs_type = ?';
  475.         $this->log($sqlPEAR_LOG_DEBUG);
  476.  
  477.         array_push($values$pathVFS_FOLDER);
  478.  
  479.         $folderList $this->_db->getAll($sql$values);
  480.         if (is_a($folderList'PEAR_Error')) {
  481.             return $folderList;
  482.         }
  483.  
  484.         $folders = array();
  485.         foreach ($folderList as $line{
  486.             $folder['val'$this->_getNativePath($line[1]$line[0]);
  487.             $folder['abbrev''';
  488.             $folder['label''';
  489.  
  490.             $count substr_count($folder['val']'/');
  491.  
  492.             $x = 0;
  493.             while ($x $count{
  494.                 $folder['abbrev'.= '    ';
  495.                 $folder['label'.= '    ';
  496.                 $x++;
  497.             }
  498.  
  499.             $folder['abbrev'.= $line[0];
  500.             $folder['label'.= $line[0];
  501.  
  502.             $strlen VFS::strlen($folder['label']);
  503.             if ($strlen > 26{
  504.                 $folder['abbrev'substr($folder['label']0($count * 4));
  505.                 $length (29 - ($count * 4)) / 2;
  506.                 $folder['abbrev'.= substr($folder['label']($count * 4)$length);
  507.                 $folder['abbrev'.= '...';
  508.                 $folder['abbrev'.= substr($folder['label']-1 * $length$length);
  509.             }
  510.  
  511.             $found = false;
  512.             foreach ($filter as $fltr{
  513.                 if ($folder['val'== $fltr{
  514.                     $found = true;
  515.                 }
  516.             }
  517.  
  518.             if (!$found{
  519.                 $folders[$folder['val']] $folder;
  520.             }
  521.         }
  522.  
  523.         ksort($folders);
  524.         return $folders;
  525.     }
  526.  
  527.     /**
  528.      * Renames all child paths.
  529.      *
  530.      * @access private
  531.      *
  532.      * @param string $path  The path of the folder to rename.
  533.      * @param string $name  The foldername to use.
  534.      *
  535.      * @return mixed  True on success or a PEAR_Error object on failure.
  536.      */
  537.     function _recursiveRename($oldpath$oldname$newpath$newname)
  538.     {
  539.         $values = array();
  540.  
  541.         $sql  'SELECT vfs_name FROM ' $this->_params['table'];
  542.         $sql .= ' WHERE vfs_type = ? AND vfs_path = ?';
  543.         $this->log($sqlPEAR_LOG_DEBUG);
  544.  
  545.         array_push($valuesVFS_FOLDER$this->_getNativePath($oldpath$oldname));
  546.  
  547.         $folderList $this->_db->getCol($sql0$values);
  548.  
  549.         foreach ($folderList as $folder{
  550.             $this->_recursiveRename($this->_getNativePath($oldpath$oldname)$folder$this->_getNativePath($newpath$newname)$folder);
  551.         }
  552.  
  553.         $values = array();
  554.  
  555.         $sql 'UPDATE ' $this->_params['table'' SET vfs_path = ? WHERE vfs_path = ?';
  556.         $this->log($sqlPEAR_LOG_DEBUG);
  557.  
  558.         array_push($values$this->_getNativePath($newpath$newname)$this->_getNativePath($oldpath$oldname));
  559.  
  560.         $sth $this->_db->prepare($sql);
  561.         $result $this->_db->execute($sth$values);
  562.  
  563.         return $result;
  564.     }
  565.  
  566.     /**
  567.      * Return a full filename on the native filesystem, from a VFS
  568.      * path and name.
  569.      *
  570.      * @access private
  571.      *
  572.      * @param string $path  The VFS file path.
  573.      * @param string $name  The VFS filename.
  574.      *
  575.      * @return string  The full native filename.
  576.      */
  577.     function _getNativePath($path$name)
  578.     {
  579.         if (empty($path)) {
  580.             return $name;
  581.         }
  582.  
  583.         if (!empty($path)) {
  584.             if (isset($this->_params['home']&&
  585.                 preg_match('|^~/?(.*)$|'$path$matches)) {
  586.                 $path $this->_params['home''/' $matches[1];
  587.             }
  588.         }
  589.  
  590.         return $path '/' $name;
  591.     }
  592.  
  593.     /**
  594.      * Attempts to open a persistent connection to the SQL server.
  595.      *
  596.      * @access private
  597.      *
  598.      * @return mixed  True on success or a PEAR_Error object on failure.
  599.      */
  600.     function _connect()
  601.     {
  602.         if ($this->_db === false{
  603.             if (!is_array($this->_params)) {
  604.                 return PEAR::raiseError(_("No configuration information specified for SQL VFS."));
  605.             }
  606.  
  607.             $required = array('phptype''hostspec''username''password''database');
  608.             foreach ($required as $val{
  609.                 if (!isset($this->_params[$val])) {
  610.                     return PEAR::raiseError(sprintf(_("Required '%s' not specified in VFS configuration.")$val));
  611.                 }
  612.             }
  613.  
  614.             if (!isset($this->_params['table'])) {
  615.                 $this->_params['table''horde_vfs';
  616.             }
  617.  
  618.             /* Connect to the SQL server using the supplied parameters. */
  619.             require_once 'DB.php';
  620.             $this->_db &DB::connect($this->_params,
  621.                                       array('persistent' => !empty($this->_params['persistent'])));
  622.             if (is_a($this->_db'PEAR_Error')) {
  623.                 $this->log($this->_dbPEAR_LOG_ERR);
  624.                 $error $this->_db;
  625.                 $this->_db = false;
  626.                 return $error;
  627.             }
  628.  
  629.             $this->_db->setOption('portability'DB_PORTABILITY_LOWERCASE | DB_PORTABILITY_ERRORS);
  630.         }
  631.  
  632.         return true;
  633.     }
  634.  
  635.     /**
  636.      * Disconnect from the SQL server and clean up the connection.
  637.      *
  638.      * @access private
  639.      */
  640.     function _disconnect()
  641.     {
  642.         if ($this->_db{
  643.             $this->_db->disconnect();
  644.             $this->_db = false;
  645.         }
  646.     }
  647.  
  648.     /**
  649.      * TODO
  650.      *
  651.      * @access private
  652.      *
  653.      * @param string $table     TODO
  654.      * @param string $field     TODO
  655.      * @param string $criteria  TODO
  656.      *
  657.      * @return mixed  TODO
  658.      */
  659.     function _readBlob($table$field$criteria)
  660.     {
  661.         if (!count($criteria)) {
  662.             return PEAR::raiseError('You must specify the fetch criteria');
  663.         }
  664.  
  665.         $where '';
  666.  
  667.         switch ($this->_db->dbsyntax{
  668.         case 'oci8':
  669.             foreach ($criteria as $key => $value{
  670.                 if (!empty($where)) {
  671.                     $where .= ' AND ';
  672.                 }
  673.                 if (empty($value)) {
  674.                     $where .= $key ' IS NULL';
  675.                 else {
  676.                     $where .= $key ' = ' $this->_db->quote($value);
  677.                 }
  678.             }
  679.  
  680.             $statement = OCIParse($this->_db->connection,
  681.                                   sprintf('SELECT %s FROM %s WHERE %s',
  682.                                           $field$table$where));
  683.             OCIExecute($statement);
  684.             if (OCIFetchInto($statement$lob)) {
  685.                 $result $lob[0]->load();
  686.                 if (is_null($result)) {
  687.                     $result = PEAR::raiseError('Unable to load SQL data.');
  688.                 }
  689.             else {
  690.                 $result = PEAR::raiseError('Unable to load SQL data.');
  691.             }
  692.             OCIFreeStatement($statement);
  693.             break;
  694.  
  695.         default:
  696.             foreach ($criteria as $key => $value{
  697.                 if (!empty($where)) {
  698.                     $where .= ' AND ';
  699.                 }
  700.                 $where .= $key ' = ' $this->_db->quote($value);
  701.             }
  702.  
  703.             $sql sprintf('SELECT %s FROM %s WHERE %s',
  704.                            $field$table$where);
  705.             $this->log($sqlPEAR_LOG_DEBUG);
  706.             $result $this->_db->getOne($sql);
  707.  
  708.             if (is_null($result)) {
  709.                 $result = PEAR::raiseError('Unable to load SQL data.');
  710.             else {
  711.                 switch ($this->_db->dbsyntax{
  712.                 case 'pgsql':
  713.                     $result pack('H' strlen($result)$result);
  714.                     break;
  715.                 }
  716.             }
  717.         }
  718.  
  719.         return $result;
  720.     }
  721.  
  722.     /**
  723.      * TODO
  724.      *
  725.      * @access private
  726.      *
  727.      * @param string $table       TODO
  728.      * @param string $field       TODO
  729.      * @param string $data        TODO
  730.      * @param string $attributes  TODO
  731.      *
  732.      * @return mixed  TODO
  733.      */
  734.     function _insertBlob($table$field$data$attributes)
  735.     {
  736.         $fields = array();
  737.         $values = array();
  738.  
  739.         switch ($this->_db->dbsyntax{
  740.         case 'oci8':
  741.             foreach ($attributes as $key => $value{
  742.                 $fields[$key;
  743.                 $values[$this->_db->quoteSmart($value);
  744.             }
  745.  
  746.             $statement = OCIParse($this->_db->connection,
  747.                                   sprintf('INSERT INTO %s (%s, %s)' .
  748.                                           ' VALUES (%s, EMPTY_BLOB()) RETURNING %s INTO :blob',
  749.                                           $table,
  750.                                           implode(', '$fields),
  751.                                           $field,
  752.                                           implode(', '$values),
  753.                                           $field));
  754.  
  755.             $lob = OCINewDescriptor($this->_db->connection);
  756.             OCIBindByName($statement':blob'$lob-1SQLT_BLOB);
  757.             OCIExecute($statementOCI_DEFAULT);
  758.             $lob->save($data);
  759.             $result = OCICommit($this->_db->connection);
  760.             $lob->free();
  761.             OCIFreeStatement($statement);
  762.             return $result ? true : PEAR::raiseError('Unknown Error');
  763.  
  764.         default:
  765.             foreach ($attributes as $key => $value{
  766.                 $fields[$key;
  767.                 $values[$value;
  768.             }
  769.  
  770.             $query sprintf('INSERT INTO %s (%s, %s) VALUES (%s)',
  771.                              $table,
  772.                              implode(', '$fields),
  773.                              $field,
  774.                              '?' str_repeat(', ?'count($values)));
  775.             break;
  776.         }
  777.  
  778.         switch ($this->_db->dbsyntax{
  779.         case 'mssql':
  780.         case 'pgsql':
  781.             $values[bin2hex($data);
  782.             break;
  783.  
  784.         default:
  785.             $values[$data;
  786.         }
  787.  
  788.         /* Execute the query. */
  789.         $this->log($queryPEAR_LOG_DEBUG);
  790.         $stmt $this->_db->prepare($query);
  791.         return $this->_db->execute($stmt$values);
  792.     }
  793.  
  794.     /**
  795.      * TODO
  796.      *
  797.      * @access private
  798.      *
  799.      * @param string $table      TODO
  800.      * @param string $field      TODO
  801.      * @param string $data       TODO
  802.      * @param string $where      TODO
  803.      * @param array $alsoupdate  TODO
  804.      *
  805.      * @return mixed  TODO
  806.      */
  807.     function _updateBlob($table$field$data$where$alsoupdate)
  808.     {
  809.         $fields = array();
  810.         $values = array();
  811.  
  812.         switch ($this->_db->dbsyntax{
  813.         case 'oci8':
  814.             $wherestring '';
  815.             foreach ($where as $key => $value{
  816.                 if (!empty($wherestring)) {
  817.                     $wherestring .= ' AND ';
  818.                 }
  819.                 $wherestring .= $key ' = ' $this->_db->quote($value);
  820.             }
  821.  
  822.             $statement = OCIParse($this->_db->connection,
  823.                                   sprintf('SELECT %s FROM %s WHERE %s FOR UPDATE',
  824.                                           $field,
  825.                                           $table,
  826.                                           $wherestring));
  827.  
  828.             OCIExecute($statementOCI_DEFAULT);
  829.             OCIFetchInto($statement$lob);
  830.             $lob[0]->save($data);
  831.             $result = OCICommit($this->_db->connection);
  832.             $lob[0]->free();
  833.             OCIFreeStatement($statement);
  834.             return $result ? true : PEAR::raiseError('Unknown Error');
  835.  
  836.         default:
  837.             $updatestring '';
  838.             $values = array();
  839.             foreach ($alsoupdate as $key => $value{
  840.                 $updatestring .= $key ' = ?, ';
  841.                 $values[$value;
  842.             }
  843.             $updatestring .= $field ' = ?';
  844.             switch ($this->_db->dbsyntax{
  845.             case 'mssql':
  846.             case 'pgsql':
  847.                 $values[bin2hex($data);
  848.                 break;
  849.  
  850.             default:
  851.                 $values[$data;
  852.             }
  853.  
  854.             $wherestring '';
  855.             foreach ($where as $key => $value{
  856.                 if (!empty($wherestring)) {
  857.                     $wherestring .= ' AND ';
  858.                 }
  859.                 $wherestring .= $key ' = ?';
  860.                 $values[$value;
  861.             }
  862.  
  863.             $query sprintf('UPDATE %s SET %s WHERE %s',
  864.                              $table,
  865.                              $updatestring,
  866.                              $wherestring);
  867.             break;
  868.         }
  869.  
  870.         /* Execute the query. */
  871.         $this->log($queryPEAR_LOG_DEBUG);
  872.         $stmt $this->_db->prepare($query);
  873.         return $this->_db->execute($stmt$values);
  874.     }
  875.  
  876. }

Documentation generated on Mon, 11 Mar 2019 14:31:26 -0400 by phpDocumentor 1.4.4. PEAR Logo Copyright © PHP Group 2004.