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

Source for file LOB.php

Documentation is available at LOB.php

  1. <?php
  2. // +----------------------------------------------------------------------+
  3. // | PHP Version 4                                                        |
  4. // +----------------------------------------------------------------------+
  5. // | Copyright (c) 1998-2004 Manuel Lemos, Tomas V.V.Cox,                 |
  6. // | Stig. S. Bakken, Lukas Smith                                         |
  7. // | All rights reserved.                                                 |
  8. // +----------------------------------------------------------------------+
  9. // | MDB is a merge of PEAR DB and Metabases that provides a unified DB   |
  10. // | API as well as database abstraction for PHP applications.            |
  11. // | This LICENSE is in the BSD license style.                            |
  12. // |                                                                      |
  13. // | Redistribution and use in source and binary forms, with or without   |
  14. // | modification, are permitted provided that the following conditions   |
  15. // | are met:                                                             |
  16. // |                                                                      |
  17. // | Redistributions of source code must retain the above copyright       |
  18. // | notice, this list of conditions and the following disclaimer.        |
  19. // |                                                                      |
  20. // | Redistributions in binary form must reproduce the above copyright    |
  21. // | notice, this list of conditions and the following disclaimer in the  |
  22. // | documentation and/or other materials provided with the distribution. |
  23. // |                                                                      |
  24. // | Neither the name of Manuel Lemos, Tomas V.V.Cox, Stig. S. Bakken,    |
  25. // | Lukas Smith nor the names of his contributors may be used to endorse |
  26. // | or promote products derived from this software without specific prior|
  27. // | written permission.                                                  |
  28. // |                                                                      |
  29. // | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS  |
  30. // | "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT    |
  31. // | LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS    |
  32. // | FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE      |
  33. // | REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,          |
  34. // | INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, |
  35. // | BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS|
  36. // |  OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED  |
  37. // | AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT          |
  38. // | LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY|
  39. // | WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE          |
  40. // | POSSIBILITY OF SUCH DAMAGE.                                          |
  41. // +----------------------------------------------------------------------+
  42. // | Author: Lukas Smith <smith@backendmedia.com>                         |
  43. // +----------------------------------------------------------------------+
  44. //
  45. // $Id: LOB.php,v 1.30.4.1 2004/01/08 13:43:03 lsmith Exp $
  46. //
  47.  
  48. if(!defined('MDB_LOB_INCLUDED'))
  49. {
  50.     define('MDB_LOB_INCLUDED'1);
  51.  
  52. /**
  53.  * MDB Large Object (BLOB/CLOB) classes
  54.  *
  55.  * @package MDB
  56.  * @category Database
  57.  * @access private
  58.  * @author  Lukas Smith <smith@backendmedia.com>
  59.  */
  60. class MDB_LOB
  61. {
  62.     var $database;
  63.     var $lob;
  64.     var $data '';
  65.     var $position = 0;
  66.  
  67.     function create(&$arguments)
  68.     {
  69.         if(isset($arguments['Data'])) {
  70.             $this->data $arguments['Data'];
  71.         }
  72.         return(MDB_OK);
  73.     }
  74.  
  75.     function destroy()
  76.     {
  77.         $this->data '';
  78.     }
  79.  
  80.     function endOfLob()
  81.     {
  82.         return($this->position >= strlen($this->data));
  83.     }
  84.  
  85.     function readLob(&$data$length)
  86.     {
  87.         $length min($lengthstrlen($this->data$this->position);
  88.         $data substr($this->data$this->position$length);
  89.         $this->position += $length;
  90.         return($length);
  91.     }
  92. };
  93.  
  94. class MDB_LOB_Result extends MDB_LOB
  95. {
  96.     var $result_lob = 0;
  97.  
  98.     function create(&$arguments)
  99.     {
  100.         if(!isset($arguments['ResultLOB'])) {
  101.             return(PEAR::raiseError(NULLMDB_ERROR_NEED_MORE_DATANULLNULL,
  102.                 'it was not specified a result Lob identifier',
  103.                 'MDB_Error'TRUE));
  104.         }
  105.         $this->result_lob $arguments['ResultLOB'];
  106.         return(MDB_OK);
  107.     }
  108.  
  109.     function destroy()
  110.     {
  111.         $this->database->_destroyResultLob($this->result_lob);
  112.     }
  113.  
  114.     function endOfLob()
  115.     {
  116.         return($this->database->endOfResultLob($this->result_lob));
  117.     }
  118.  
  119.     function readLob(&$data$length)
  120.     {
  121.         $read_length $this->database->_readResultLob($this->result_lob$data$length);
  122.         if (MDB::isError($read_length)) {
  123.             return($read_length);
  124.         }
  125.         if($read_length < 0{
  126.             return(PEAR::raiseError(NULLMDB_ERROR_INVALIDNULLNULL,
  127.                 'data was read beyond end of data source',
  128.                 'MDB_Error'TRUE));
  129.         }
  130.         return($read_length);
  131.     }
  132. };
  133.  
  134. class MDB_LOB_Input_File extends MDB_LOB
  135. {
  136.     var $file = 0;
  137.     var $opened_file = 0;
  138.  
  139.     function create(&$arguments)
  140.     {
  141.         if(isset($arguments['File'])) {
  142.             if(intval($arguments['File']== 0{
  143.                 return(PEAR::raiseError(NULLMDB_ERROR_INVALIDNULLNULL,
  144.                     'it was specified an invalid input file identifier',
  145.                     'MDB_Error'TRUE));
  146.             }
  147.             $this->file $arguments['File'];
  148.         }
  149.         else
  150.         {
  151.             if(isset($arguments['FileName'])) {
  152.                 if((!$this->file fopen($arguments['FileName']'rb'))) {
  153.                 return(PEAR::raiseError(NULLMDB_ERROR_NOT_FOUNDNULLNULL,
  154.                     'could not open specified input file ("'.$arguments['FileName'].'")',
  155.                     'MDB_Error'TRUE));
  156.                 }
  157.                 $this->opened_file = 1;
  158.             else {
  159.                 return(PEAR::raiseError(NULLMDB_ERROR_NEED_MORE_DATANULLNULL,
  160.                     'it was not specified the input file',
  161.                     'MDB_Error'TRUE));
  162.             }
  163.         }
  164.         return(MDB_OK);
  165.     }
  166.  
  167.     function destroy()
  168.     {
  169.         if($this->opened_file{
  170.             fclose($this->file);
  171.             $this->file = 0;
  172.             $this->opened_file = 0;
  173.         }
  174.     }
  175.  
  176.     function endOfLob({
  177.         return(feof($this->file));
  178.     }
  179.  
  180.     function readLob(&$data$length)
  181.     {
  182.         if(gettype($data @fread($this->file$length))!= 'string'{
  183.             return(PEAR::raiseError(NULLMDB_ERRORNULLNULL,
  184.                 'could not read from the input file',
  185.                 'MDB_Error'TRUE));
  186.         }
  187.         return(strlen($data));
  188.     }
  189. };
  190.  
  191. class MDB_LOB_Output_File extends MDB_LOB
  192. {
  193.     var $file = 0;
  194.     var $opened_file = 0;
  195.     var $input_lob = 0;
  196.     var $opened_lob = 0;
  197.     var $buffer_length = 8000;
  198.  
  199.     function create(&$arguments)
  200.     {
  201.         if(isset($arguments['BufferLength'])) {
  202.             if($arguments['BufferLength'<= 0{
  203.                 return(PEAR::raiseError(NULLMDB_ERROR_INVALIDNULLNULL,
  204.                     'it was specified an invalid buffer length',
  205.                     'MDB_Error'TRUE));
  206.             }
  207.             $this->buffer_length $arguments['BufferLength'];
  208.         }
  209.         if(isset($arguments['File'])) {
  210.             if(intval($arguments['File']== 0{
  211.                 return(PEAR::raiseError(NULLMDB_ERROR_INVALIDNULLNULL,
  212.                     'it was specified an invalid output file identifier',
  213.                     'MDB_Error'TRUE));
  214.             }
  215.             $this->file $arguments['File'];
  216.         else {
  217.             if(isset($arguments['FileName'])) {
  218.                 if((!$this->file fopen($arguments['FileName'],'wb'))) {
  219.                     return(PEAR::raiseError(NULLMDB_ERROR_NOT_FOUNDNULLNULL,
  220.                         'could not open specified output file ("'.$arguments['FileName'].'")',
  221.                         'MDB_Error'TRUE));
  222.                 }
  223.                 $this->opened_file = 1;
  224.             else {
  225.                 return(PEAR::raiseError(NULLMDB_ERROR_NEED_MORE_DATANULLNULL,
  226.                     'it was not specified the output file',
  227.                     'MDB_Error'TRUE));
  228.             }
  229.         }
  230.         if(isset($arguments['LOB'])) {
  231.             if(!is_object($arguments['LOB'])) {
  232.                 $this->destroy();
  233.                 return(PEAR::raiseError(NULLMDB_ERROR_INVALIDNULLNULL,
  234.                     'it was specified an invalid input large object identifier',
  235.                     'MDB_Error'TRUE));
  236.             }
  237.             $this->input_lob $arguments['LOB'];
  238.         else {
  239.             if($this->database
  240.                 && isset($arguments['Result'])
  241.                 && isset($arguments['Row'])
  242.                 && isset($arguments['Field'])
  243.                 && isset($arguments['Binary']))
  244.             {
  245.                 if($arguments['Binary']{
  246.                     $this->input_lob $this->database->fetchBlob($arguments['Result'],
  247.                         $arguments['Row']$arguments['Field']);
  248.                 else {
  249.                     $this->input_lob $this->database->fetchClob($arguments['Result'],
  250.                         $arguments['Row']$arguments['Field']);
  251.                 }
  252.                 if($this->input_lob == 0{
  253.                     $this->destroy();
  254.                     return(PEAR::raiseError(NULLMDB_ERRORNULLNULL,
  255.                         'could not fetch the input result large object',
  256.                         'MDB_Error'TRUE));
  257.                 }
  258.                 $this->opened_lob = 1;
  259.             else {
  260.                 $this->destroy();
  261.                 return(PEAR::raiseError(NULLMDB_ERROR_NEED_MORE_DATANULLNULL,
  262.                     'it was not specified the input large object identifier',
  263.                     'MDB_Error'TRUE));
  264.             }
  265.         }
  266.         return(MDB_OK);
  267.     }
  268.  
  269.     function destroy()
  270.     {
  271.         if($this->opened_file{
  272.             fclose($this->file);
  273.             $this->opened_file = 0;
  274.             $this->file = 0;
  275.         }
  276.         if($this->opened_lob{
  277.             $this->database->destroyLob($this->input_lob);
  278.             $this->input_lob = 0;
  279.             $this->opened_lob = 0;
  280.         }
  281.     }
  282.  
  283.     function endOfLob()
  284.     {
  285.         return($this->database->endOfLob($this->input_lob));
  286.     }
  287.  
  288.     function readLob(&$data$length{
  289.         $buffer_length ($length == 0 ? $this->buffer_length $length);
  290.         $written_full = 0;
  291.         do {
  292.             for($written = 0;
  293.                 !$this->database->endOfLob($this->input_lob)
  294.                 && $written $buffer_length;
  295.                 $written += $read)
  296.             {
  297.                 if(MDB::isError($result $this->database->
  298.                     readLob($this->input_lob$buffer$buffer_length)))
  299.                 {
  300.                     return($result);
  301.                 }
  302.                 $read strlen($buffer);
  303.                 if(@fwrite($this->file$buffer$read)!= $read{
  304.                     return(PEAR::raiseError(NULLMDB_ERRORNULLNULL,
  305.                         'could not write to the output file',
  306.                         'MDB_Error'TRUE));
  307.                 }
  308.             }
  309.             $written_full += $written;
  310.         while($length == 0 && !$this->database->endOfLob($this->input_lob));
  311.         return($written_full);
  312.     }
  313. }
  314.  
  315. };
  316. ?>

Documentation generated on Mon, 11 Mar 2019 10:15:25 -0400 by phpDocumentor 1.4.4. PEAR Logo Copyright © PHP Group 2004.