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

Source for file dbase.php

Documentation is available at dbase.php

  1. <?php
  2.  
  3. /* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */
  4.  
  5. /**
  6.  * The PEAR DB driver for PHP's dbase extension
  7.  * for interacting with dBase databases
  8.  *
  9.  * PHP versions 4 and 5
  10.  *
  11.  * LICENSE: This source file is subject to version 3.0 of the PHP license
  12.  * that is available through the world-wide-web at the following URI:
  13.  * http://www.php.net/license/3_0.txt.  If you did not receive a copy of
  14.  * the PHP License and are unable to obtain it through the web, please
  15.  * send a note to license@php.net so we can mail you a copy immediately.
  16.  *
  17.  * @category   Database
  18.  * @package    DB
  19.  * @author     Tomas V.V. Cox <cox@idecnet.com>
  20.  * @author     Daniel Convissor <danielc@php.net>
  21.  * @copyright  1997-2005 The PHP Group
  22.  * @license    http://www.php.net/license/3_0.txt  PHP License 3.0
  23.  * @version    CVS: $Id: dbase.php,v 1.42 2007/01/11 07:43:09 aharvey Exp $
  24.  * @link       http://pear.php.net/package/DB
  25.  */
  26.  
  27. /**
  28.  * Obtain the DB_common class so it can be extended from
  29.  */
  30. require_once 'DB/common.php';
  31.  
  32. /**
  33.  * The methods PEAR DB uses to interact with PHP's dbase extension
  34.  * for interacting with dBase databases
  35.  *
  36.  * These methods overload the ones declared in DB_common.
  37.  *
  38.  * @category   Database
  39.  * @package    DB
  40.  * @author     Tomas V.V. Cox <cox@idecnet.com>
  41.  * @author     Daniel Convissor <danielc@php.net>
  42.  * @copyright  1997-2005 The PHP Group
  43.  * @license    http://www.php.net/license/3_0.txt  PHP License 3.0
  44.  * @version    Release: 1.7.10
  45.  * @link       http://pear.php.net/package/DB
  46.  */
  47. class DB_dbase extends DB_common
  48. {
  49.     // {{{ properties
  50.  
  51.     
  52.     /**
  53.      * The DB driver type (mysql, oci8, odbc, etc.)
  54.      * @var string 
  55.      */
  56.     var $phptype = 'dbase';
  57.  
  58.     /**
  59.      * The database syntax variant to be used (db2, access, etc.), if any
  60.      * @var string 
  61.      */
  62.     var $dbsyntax = 'dbase';
  63.  
  64.     /**
  65.      * The capabilities of this DB implementation
  66.      *
  67.      * The 'new_link' element contains the PHP version that first provided
  68.      * new_link support for this DBMS.  Contains false if it's unsupported.
  69.      *
  70.      * Meaning of the 'limit' element:
  71.      *   + 'emulate' = emulate with fetch row by number
  72.      *   + 'alter'   = alter the query
  73.      *   + false     = skip rows
  74.      *
  75.      * @var array 
  76.      */
  77.     var $features = array(
  78.         'limit'         => false,
  79.         'new_link'      => false,
  80.         'numrows'       => true,
  81.         'pconnect'      => false,
  82.         'prepare'       => false,
  83.         'ssl'           => false,
  84.         'transactions'  => false,
  85.     );
  86.  
  87.     /**
  88.      * A mapping of native error codes to DB error codes
  89.      * @var array 
  90.      */
  91.     var $errorcode_map = array(
  92.     );
  93.  
  94.     /**
  95.      * The raw database connection created by PHP
  96.      * @var resource 
  97.      */
  98.     var $connection;
  99.  
  100.     /**
  101.      * The DSN information for connecting to a database
  102.      * @var array 
  103.      */
  104.     var $dsn = array();
  105.  
  106.  
  107.     /**
  108.      * A means of emulating result resources
  109.      * @var array 
  110.      */
  111.     var $res_row = array();
  112.  
  113.     /**
  114.      * The quantity of results so far
  115.      *
  116.      * For emulating result resources.
  117.      *
  118.      * @var integer 
  119.      */
  120.     var $result = 0;
  121.  
  122.     /**
  123.      * Maps dbase data type id's to human readable strings
  124.      *
  125.      * The human readable values are based on the output of PHP's
  126.      * dbase_get_header_info() function.
  127.      *
  128.      * @var array 
  129.      * @since Property available since Release 1.7.0
  130.      */
  131.     var $types = array(
  132.         'C' => 'character',
  133.         'D' => 'date',
  134.         'L' => 'boolean',
  135.         'M' => 'memo',
  136.         'N' => 'number',
  137.     );
  138.  
  139.  
  140.     // }}}
  141.     // {{{ constructor
  142.  
  143.     
  144.     /**
  145.      * This constructor calls <kbd>$this->DB_common()</kbd>
  146.      *
  147.      * @return void 
  148.      */
  149.     function DB_dbase()
  150.     {
  151.         $this->DB_common();
  152.     }
  153.  
  154.     // }}}
  155.     // {{{ connect()
  156.  
  157.     
  158.     /**
  159.      * Connect to the database and create it if it doesn't exist
  160.      *
  161.      * Don't call this method directly.  Use DB::connect() instead.
  162.      *
  163.      * PEAR DB's dbase driver supports the following extra DSN options:
  164.      *   + mode    An integer specifying the read/write mode to use
  165.      *              (0 = read only, 1 = write only, 2 = read/write).
  166.      *              Available since PEAR DB 1.7.0.
  167.      *   + fields  An array of arrays that PHP's dbase_create() function needs
  168.      *              to create a new database.  This information is used if the
  169.      *              dBase file specified in the "database" segment of the DSN
  170.      *              does not exist.  For more info, see the PHP manual's
  171.      *              {@link http://php.net/dbase_create dbase_create()} page.
  172.      *              Available since PEAR DB 1.7.0.
  173.      *
  174.      * Example of how to connect and establish a new dBase file if necessary:
  175.      * <code>
  176.      * require_once 'DB.php';
  177.      *
  178.      * $dsn = array(
  179.      *     'phptype'  => 'dbase',
  180.      *     'database' => '/path/and/name/of/dbase/file',
  181.      *     'mode'     => 2,
  182.      *     'fields'   => array(
  183.      *         array('a', 'N', 5, 0),
  184.      *         array('b', 'C', 40),
  185.      *         array('c', 'C', 255),
  186.      *         array('d', 'C', 20),
  187.      *     ),
  188.      * );
  189.      * $options = array(
  190.      *     'debug'       => 2,
  191.      *     'portability' => DB_PORTABILITY_ALL,
  192.      * );
  193.      *
  194.      * $db =& DB::connect($dsn, $options);
  195.      * if (PEAR::isError($db)) {
  196.      *     die($db->getMessage());
  197.      * }
  198.      * </code>
  199.      *
  200.      * @param array $dsn         the data source name
  201.      * @param bool  $persistent  should the connection be persistent?
  202.      *
  203.      * @return int  DB_OK on success. A DB_Error object on failure.
  204.      */
  205.     function connect($dsn$persistent = false)
  206.     {
  207.         if (!PEAR::loadExtension('dbase')) {
  208.             return $this->raiseError(DB_ERROR_EXTENSION_NOT_FOUND);
  209.         }
  210.  
  211.         $this->dsn = $dsn;
  212.         if ($dsn['dbsyntax']{
  213.             $this->dbsyntax = $dsn['dbsyntax'];
  214.         }
  215.  
  216.         /*
  217.          * Turn track_errors on for entire script since $php_errormsg
  218.          * is the only way to find errors from the dbase extension.
  219.          */
  220.         @ini_set('track_errors'1);
  221.         $php_errormsg '';
  222.  
  223.         if (!file_exists($dsn['database'])) {
  224.             $this->dsn['mode'= 2;
  225.             if (empty($dsn['fields']|| !is_array($dsn['fields'])) {
  226.                 return $this->raiseError(DB_ERROR_CONNECT_FAILED,
  227.                                          nullnullnull,
  228.                                          'the dbase file does not exist and '
  229.                                          . 'it could not be created because '
  230.                                          . 'the "fields" element of the DSN '
  231.                                          . 'is not properly set');
  232.             }
  233.             $this->connection = @dbase_create($dsn['database'],
  234.                                               $dsn['fields']);
  235.             if (!$this->connection{
  236.                 return $this->raiseError(DB_ERROR_CONNECT_FAILED,
  237.                                          nullnullnull,
  238.                                          'the dbase file does not exist and '
  239.                                          . 'the attempt to create it failed: '
  240.                                          . $php_errormsg);
  241.             }
  242.         else {
  243.             if (!isset($this->dsn['mode'])) {
  244.                 $this->dsn['mode'= 0;
  245.             }
  246.             $this->connection = @dbase_open($dsn['database'],
  247.                                             $this->dsn['mode']);
  248.             if (!$this->connection{
  249.                 return $this->raiseError(DB_ERROR_CONNECT_FAILED,
  250.                                          nullnullnull,
  251.                                          $php_errormsg);
  252.             }
  253.         }
  254.         return DB_OK;
  255.     }
  256.  
  257.     // }}}
  258.     // {{{ disconnect()
  259.  
  260.     
  261.     /**
  262.      * Disconnects from the database server
  263.      *
  264.      * @return bool  TRUE on success, FALSE on failure
  265.      */
  266.     function disconnect()
  267.     {
  268.         $ret @dbase_close($this->connection);
  269.         $this->connection = null;
  270.         return $ret;
  271.     }
  272.  
  273.     // }}}
  274.     // {{{ &query()
  275.  
  276.     
  277.     function &query($query = null)
  278.     {
  279.         // emulate result resources
  280.         $this->res_row[(int)$this->result= 0;
  281.         $tmp =new DB_result($this$this->result++);
  282.         return $tmp;
  283.     }
  284.  
  285.     // }}}
  286.     // {{{ fetchInto()
  287.  
  288.     
  289.     /**
  290.      * Places a row from the result set into the given array
  291.      *
  292.      * Formating of the array and the data therein are configurable.
  293.      * See DB_result::fetchInto() for more information.
  294.      *
  295.      * This method is not meant to be called directly.  Use
  296.      * DB_result::fetchInto() instead.  It can't be declared "protected"
  297.      * because DB_result is a separate object.
  298.      *
  299.      * @param resource $result    the query result resource
  300.      * @param array    $arr       the referenced array to put the data in
  301.      * @param int      $fetchmode how the resulting array should be indexed
  302.      * @param int      $rownum    the row number to fetch (0 = first row)
  303.      *
  304.      * @return mixed  DB_OK on success, NULL when the end of a result set is
  305.      *                  reached or on failure
  306.      *
  307.      * @see DB_result::fetchInto()
  308.      */
  309.     function fetchInto($result&$arr$fetchmode$rownum = null)
  310.     {
  311.         if ($rownum === null{
  312.             $rownum $this->res_row[(int)$result]++;
  313.         }
  314.         if ($fetchmode DB_FETCHMODE_ASSOC{
  315.             $arr @dbase_get_record_with_names($this->connection$rownum);
  316.             if ($this->options['portability'DB_PORTABILITY_LOWERCASE && $arr{
  317.                 $arr array_change_key_case($arrCASE_LOWER);
  318.             }
  319.         else {
  320.             $arr @dbase_get_record($this->connection$rownum);
  321.         }
  322.         if (!$arr{
  323.             return null;
  324.         }
  325.         if ($this->options['portability'DB_PORTABILITY_RTRIM{
  326.             $this->_rtrimArrayValues($arr);
  327.         }
  328.         if ($this->options['portability'DB_PORTABILITY_NULL_TO_EMPTY{
  329.             $this->_convertNullArrayValuesToEmpty($arr);
  330.         }
  331.         return DB_OK;
  332.     }
  333.  
  334.     // }}}
  335.     // {{{ freeResult()
  336.  
  337.     
  338.     /**
  339.      * Deletes the result set and frees the memory occupied by the result set.
  340.      *
  341.      * This method is a no-op for dbase, as there aren't result resources in
  342.      * the same sense as most other database backends.
  343.      *
  344.      * @param resource $result  PHP's query result resource
  345.      *
  346.      * @return bool  TRUE on success, FALSE if $result is invalid
  347.      *
  348.      * @see DB_result::free()
  349.      */
  350.     function freeResult($result)
  351.     {
  352.         return true;
  353.     }
  354.  
  355.     // }}}
  356.     // {{{ numCols()
  357.  
  358.     
  359.     /**
  360.      * Gets the number of columns in a result set
  361.      *
  362.      * This method is not meant to be called directly.  Use
  363.      * DB_result::numCols() instead.  It can't be declared "protected"
  364.      * because DB_result is a separate object.
  365.      *
  366.      * @param resource $result  PHP's query result resource
  367.      *
  368.      * @return int  the number of columns.  A DB_Error object on failure.
  369.      *
  370.      * @see DB_result::numCols()
  371.      */
  372.     function numCols($foo)
  373.     {
  374.         return @dbase_numfields($this->connection);
  375.     }
  376.  
  377.     // }}}
  378.     // {{{ numRows()
  379.  
  380.     
  381.     /**
  382.      * Gets the number of rows in a result set
  383.      *
  384.      * This method is not meant to be called directly.  Use
  385.      * DB_result::numRows() instead.  It can't be declared "protected"
  386.      * because DB_result is a separate object.
  387.      *
  388.      * @param resource $result  PHP's query result resource
  389.      *
  390.      * @return int  the number of rows.  A DB_Error object on failure.
  391.      *
  392.      * @see DB_result::numRows()
  393.      */
  394.     function numRows($foo)
  395.     {
  396.         return @dbase_numrecords($this->connection);
  397.     }
  398.  
  399.     // }}}
  400.     // {{{ quoteBoolean()
  401.  
  402.     
  403.     /**
  404.      * Formats a boolean value for use within a query in a locale-independent
  405.      * manner.
  406.      *
  407.      * @param boolean the boolean value to be quoted.
  408.      * @return string the quoted string.
  409.      * @see DB_common::quoteSmart()
  410.      * @since Method available since release 1.7.8.
  411.      */
  412.     function quoteBoolean($boolean{
  413.         return $boolean 'T' 'F';
  414.     }
  415.      
  416.     // }}}
  417.     // {{{ tableInfo()
  418.  
  419.     
  420.     /**
  421.      * Returns information about the current database
  422.      *
  423.      * @param mixed $result  THIS IS UNUSED IN DBASE.  The current database
  424.      *                        is examined regardless of what is provided here.
  425.      * @param int   $mode    a valid tableInfo mode
  426.      *
  427.      * @return array  an associative array with the information requested.
  428.      *                  A DB_Error object on failure.
  429.      *
  430.      * @see DB_common::tableInfo()
  431.      * @since Method available since Release 1.7.0
  432.      */
  433.     function tableInfo($result = null$mode = null)
  434.     {
  435.         if (function_exists('dbase_get_header_info')) {
  436.             $id @dbase_get_header_info($this->connection);
  437.             if (!$id && $php_errormsg{
  438.                 return $this->raiseError(DB_ERROR,
  439.                                          nullnullnull,
  440.                                          $php_errormsg);
  441.             }
  442.         else {
  443.             /*
  444.              * This segment for PHP 4 is loosely based on code by
  445.              * Hadi Rusiah <deegos@yahoo.com> in the comments on
  446.              * the dBase reference page in the PHP manual.
  447.              */
  448.             $db @fopen($this->dsn['database']'r');
  449.             if (!$db{
  450.                 return $this->raiseError(DB_ERROR_CONNECT_FAILED,
  451.                                          nullnullnull,
  452.                                          $php_errormsg);
  453.             }
  454.  
  455.             $id = array();
  456.             $i  = 0;
  457.  
  458.             $line fread($db32);
  459.             while (!feof($db)) {
  460.                 $line fread($db32);
  461.                 if (substr($line01== chr(13)) {
  462.                     break;
  463.                 else {
  464.                     $pos strpos(substr($line010)chr(0));
  465.                     $pos ($pos == 0 ? 10 : $pos);
  466.                     $id[$i= array(
  467.                         'name'   => substr($line0$pos),
  468.                         'type'   => $this->types[substr($line111)],
  469.                         'length' => ord(substr($line161)),
  470.                         'precision' => ord(substr($line171)),
  471.                     );
  472.                 }
  473.                 $i++;
  474.             }
  475.  
  476.             fclose($db);
  477.         }
  478.  
  479.         if ($this->options['portability'DB_PORTABILITY_LOWERCASE{
  480.             $case_func 'strtolower';
  481.         else {
  482.             $case_func 'strval';
  483.         }
  484.  
  485.         $res   = array();
  486.         $count count($id);
  487.  
  488.         if ($mode{
  489.             $res['num_fields'$count;
  490.         }
  491.  
  492.         for ($i = 0; $i $count$i++{
  493.             $res[$i= array(
  494.                 'table' => $this->dsn['database'],
  495.                 'name'  => $case_func($id[$i]['name']),
  496.                 'type'  => $id[$i]['type'],
  497.                 'len'   => $id[$i]['length'],
  498.                 'flags' => ''
  499.             );
  500.             if ($mode DB_TABLEINFO_ORDER{
  501.                 $res['order'][$res[$i]['name']] $i;
  502.             }
  503.             if ($mode DB_TABLEINFO_ORDERTABLE{
  504.                 $res['ordertable'][$res[$i]['table']][$res[$i]['name']] $i;
  505.             }
  506.         }
  507.  
  508.         return $res;
  509.     }
  510.  
  511.     // }}}
  512. }
  513.  
  514. /*
  515.  * Local variables:
  516.  * tab-width: 4
  517.  * c-basic-offset: 4
  518.  * End:
  519.  */
  520.  
  521. ?>

Documentation generated on Tue, 20 Mar 2007 05:30:25 -0500 by phpDocumentor 1.3.0. PEAR Logo Copyright © PHP Group 2004.