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

Source for file Common.php

Documentation is available at Common.php

  1. <?php
  2. // +----------------------------------------------------------------------+
  3. // | PHP versions 4 and 5                                                 |
  4. // +----------------------------------------------------------------------+
  5. // | Copyright (c) 1998-2006 Manuel Lemos, Tomas V.V.Cox,                 |
  6. // | Stig. S. Bakken, Lukas Smith                                         |
  7. // | All rights reserved.                                                 |
  8. // +----------------------------------------------------------------------+
  9. // | MDB2 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@pooteeweet.org>                           |
  43. // +----------------------------------------------------------------------+
  44. //
  45. // $Id: Common.php,v 1.28 2006/08/15 11:24:51 lsmith Exp $
  46. //
  47.  
  48. /**
  49.  * @package MDB2
  50.  * @category Database
  51.  */
  52.  
  53. /**
  54.  * These are constants for the tableInfo-function
  55.  * they are bitwised or'ed. so if there are more constants to be defined
  56.  * in the future, adjust MDB2_TABLEINFO_FULL accordingly
  57.  */
  58.  
  59. define('MDB2_TABLEINFO_ORDER',      1);
  60. define('MDB2_TABLEINFO_ORDERTABLE'2);
  61. define('MDB2_TABLEINFO_FULL',       3);
  62.  
  63. /**
  64.  * Base class for the schema reverse engineering module that is extended by each MDB2 driver
  65.  *
  66.  * @package MDB2
  67.  * @category Database
  68.  * @author  Lukas Smith <smith@pooteeweet.org>
  69.  */
  70. {
  71.     // }}}
  72.     // {{{ getTableFieldDefinition()
  73.  
  74.     
  75.     /**
  76.      * Get the stucture of a field into an array
  77.      *
  78.      * @param string    $table         name of table that should be used in method
  79.      * @param string    $fields     name of field that should be used in method
  80.      * @return mixed data array on success, a MDB2 error on failure
  81.      * @access public
  82.      */
  83.     function getTableFieldDefinition($table$field)
  84.     {
  85.         $db =$this->getDBInstance();
  86.         if (PEAR::isError($db)) {
  87.             return $db;
  88.         }
  89.  
  90.         return $db->raiseError(MDB2_ERROR_UNSUPPORTEDnullnull,
  91.             'method not implemented'__FUNCTION__);
  92.     }
  93.  
  94.     // }}}
  95.     // {{{ getTableIndexDefinition()
  96.  
  97.     
  98.     /**
  99.      * Get the stucture of an index into an array
  100.      *
  101.      * @param string    $table      name of table that should be used in method
  102.      * @param string    $index      name of index that should be used in method
  103.      * @return mixed data array on success, a MDB2 error on failure
  104.      * @access public
  105.      */
  106.     function getTableIndexDefinition($table$index)
  107.     {
  108.         $db =$this->getDBInstance();
  109.         if (PEAR::isError($db)) {
  110.             return $db;
  111.         }
  112.  
  113.         return $db->raiseError(MDB2_ERROR_UNSUPPORTEDnullnull,
  114.             'method not implemented'__FUNCTION__);
  115.     }
  116.  
  117.     // }}}
  118.     // {{{ getTableConstraintDefinition()
  119.  
  120.     
  121.     /**
  122.      * Get the stucture of an constraints into an array
  123.      *
  124.      * @param string    $table      name of table that should be used in method
  125.      * @param string    $index      name of index that should be used in method
  126.      * @return mixed data array on success, a MDB2 error on failure
  127.      * @access public
  128.      */
  129.     function getTableConstraintDefinition($table$index)
  130.     {
  131.         $db =$this->getDBInstance();
  132.         if (PEAR::isError($db)) {
  133.             return $db;
  134.         }
  135.  
  136.         return $db->raiseError(MDB2_ERROR_UNSUPPORTEDnullnull,
  137.             'method not implemented'__FUNCTION__);
  138.     }
  139.  
  140.     // }}}
  141.     // {{{ getSequenceDefinition()
  142.  
  143.     
  144.     /**
  145.      * Get the stucture of a sequence into an array
  146.      *
  147.      * @param string    $sequence   name of sequence that should be used in method
  148.      * @return mixed data array on success, a MDB2 error on failure
  149.      * @access public
  150.      */
  151.     function getSequenceDefinition($sequence)
  152.     {
  153.         $db =$this->getDBInstance();
  154.         if (PEAR::isError($db)) {
  155.             return $db;
  156.         }
  157.  
  158.         $start $db->currId($sequence);
  159.         if (PEAR::isError($start)) {
  160.             return $start;
  161.         }
  162.         if ($db->supports('current_id')) {
  163.             $start++;
  164.         else {
  165.             $db->warnings['database does not support getting current
  166.                 sequence value, the sequence value was incremented';
  167.         }
  168.         $definition = array();
  169.         if ($start != 1{
  170.             $definition = array('start' => $start);
  171.         }
  172.         return $definition;
  173.     }
  174.  
  175.     // }}}
  176.     // {{{ getTriggerDefinition()
  177.  
  178.     
  179.     /**
  180.      * Get the stucture of an trigger into an array
  181.      *
  182.      * @param string    $trigger    name of trigger that should be used in method
  183.      * @return mixed data array on success, a MDB2 error on failure
  184.      * @access public
  185.      */
  186.     function getTriggerDefinition($trigger)
  187.     {
  188.         $db =$this->getDBInstance();
  189.         if (PEAR::isError($db)) {
  190.             return $db;
  191.         }
  192.  
  193.         return $db->raiseError(MDB2_ERROR_UNSUPPORTEDnullnull,
  194.             'method not implemented'__FUNCTION__);
  195.     }
  196.  
  197.     // }}}
  198.     // {{{ tableInfo()
  199.  
  200.     
  201.     /**
  202.      * Returns information about a table or a result set
  203.      *
  204.      * The format of the resulting array depends on which <var>$mode</var>
  205.      * you select.  The sample output below is based on this query:
  206.      * <pre>
  207.      *    SELECT tblFoo.fldID, tblFoo.fldPhone, tblBar.fldId
  208.      *    FROM tblFoo
  209.      *    JOIN tblBar ON tblFoo.fldId = tblBar.fldId
  210.      * </pre>
  211.      *
  212.      * <ul>
  213.      * <li>
  214.      *
  215.      * <kbd>null</kbd> (default)
  216.      *   <pre>
  217.      *   [0] => Array (
  218.      *       [table] => tblFoo
  219.      *       [name] => fldId
  220.      *       [type] => int
  221.      *       [len] => 11
  222.      *       [flags] => primary_key not_null
  223.      *   )
  224.      *   [1] => Array (
  225.      *       [table] => tblFoo
  226.      *       [name] => fldPhone
  227.      *       [type] => string
  228.      *       [len] => 20
  229.      *       [flags] =>
  230.      *   )
  231.      *   [2] => Array (
  232.      *       [table] => tblBar
  233.      *       [name] => fldId
  234.      *       [type] => int
  235.      *       [len] => 11
  236.      *       [flags] => primary_key not_null
  237.      *   )
  238.      *   </pre>
  239.      *
  240.      * </li><li>
  241.      *
  242.      * <kbd>MDB2_TABLEINFO_ORDER</kbd>
  243.      *
  244.      *   <p>In addition to the information found in the default output,
  245.      *   a notation of the number of columns is provided by the
  246.      *   <samp>num_fields</samp> element while the <samp>order</samp>
  247.      *   element provides an array with the column names as the keys and
  248.      *   their location index number (corresponding to the keys in the
  249.      *   the default output) as the values.</p>
  250.      *
  251.      *   <p>If a result set has identical field names, the last one is
  252.      *   used.</p>
  253.      *
  254.      *   <pre>
  255.      *   [num_fields] => 3
  256.      *   [order] => Array (
  257.      *       [fldId] => 2
  258.      *       [fldTrans] => 1
  259.      *   )
  260.      *   </pre>
  261.      *
  262.      * </li><li>
  263.      *
  264.      * <kbd>MDB2_TABLEINFO_ORDERTABLE</kbd>
  265.      *
  266.      *   <p>Similar to <kbd>MDB2_TABLEINFO_ORDER</kbd> but adds more
  267.      *   dimensions to the array in which the table names are keys and
  268.      *   the field names are sub-keys.  This is helpful for queries that
  269.      *   join tables which have identical field names.</p>
  270.      *
  271.      *   <pre>
  272.      *   [num_fields] => 3
  273.      *   [ordertable] => Array (
  274.      *       [tblFoo] => Array (
  275.      *           [fldId] => 0
  276.      *           [fldPhone] => 1
  277.      *       )
  278.      *       [tblBar] => Array (
  279.      *           [fldId] => 2
  280.      *       )
  281.      *   )
  282.      *   </pre>
  283.      *
  284.      * </li>
  285.      * </ul>
  286.      *
  287.      * The <samp>flags</samp> element contains a space separated list
  288.      * of extra information about the field.  This data is inconsistent
  289.      * between DBMS's due to the way each DBMS works.
  290.      *   + <samp>primary_key</samp>
  291.      *   + <samp>unique_key</samp>
  292.      *   + <samp>multiple_key</samp>
  293.      *   + <samp>not_null</samp>
  294.      *
  295.      * Most DBMS's only provide the <samp>table</samp> and <samp>flags</samp>
  296.      * elements if <var>$result</var> is a table name.  The following DBMS's
  297.      * provide full information from queries:
  298.      *   + fbsql
  299.      *   + mysql
  300.      *
  301.      * If the 'portability' option has <samp>MDB2_PORTABILITY_FIX_CASE</samp>
  302.      * turned on, the names of tables and fields will be lower or upper cased.
  303.      *
  304.      * @param object|string $result  MDB2_result object from a query or a
  305.      *                                 string containing the name of a table.
  306.      *                                 While this also accepts a query result
  307.      *                                 resource identifier, this behavior is
  308.      *                                 deprecated.
  309.      * @param int  $mode   either unused or one of the tableInfo modes:
  310.      *                      <kbd>MDB2_TABLEINFO_ORDERTABLE</kbd>,
  311.      *                      <kbd>MDB2_TABLEINFO_ORDER</kbd> or
  312.      *                      <kbd>MDB2_TABLEINFO_FULL</kbd> (which does both).
  313.      *                      These are bitwise, so the first two can be
  314.      *                      combined using <kbd>|</kbd>.
  315.      *
  316.      * @return array  an associative array with the information requested.
  317.      *                  A MDB2_Error object on failure.
  318.      *
  319.      * @see MDB2_Driver_Common::setOption()
  320.      */
  321.     function tableInfo($result$mode = null)
  322.     {
  323.         $db =$this->getDBInstance();
  324.         if (PEAR::isError($db)) {
  325.             return $db;
  326.         }
  327.  
  328.         if (!is_string($result)) {
  329.             return $db->raiseError(MDB2_ERROR_UNSUPPORTEDnullnull,
  330.                 'method not implemented'__FUNCTION__);
  331.         }
  332.  
  333.         $db->loadModule('Manager'nulltrue);
  334.         $fields $db->manager->listTableFields($result);
  335.         if (PEAR::isError($fields)) {
  336.             return $fields;
  337.         }
  338.  
  339.         $flags = array();
  340.  
  341.         $idxname_format $db->getOption('idxname_format');
  342.         $db->setOption('idxname_format''%s');
  343.  
  344.         $indexes $db->manager->listTableIndexes($result);
  345.         if (PEAR::isError($indexes)) {
  346.             $db->setOption('idxname_format'$idxname_format);
  347.             return $indexes;
  348.         }
  349.  
  350.         foreach ($indexes as $index{
  351.             $definition $this->getTableIndexDefinition($result$index);
  352.             if (PEAR::isError($definition)) {
  353.                 $db->setOption('idxname_format'$idxname_format);
  354.                 return $definition;
  355.             }
  356.             if (count($definition['fields']> 1{
  357.                 foreach ($definition['fields'as $field => $sort{
  358.                     $flags[$field'multiple_key';
  359.                 }
  360.             }
  361.         }
  362.  
  363.         $constraints $db->manager->listTableConstraints($result);
  364.         if (PEAR::isError($constraints)) {
  365.             return $constraints;
  366.         }
  367.  
  368.         foreach ($constraints as $constraint{
  369.             $definition $this->getTableConstraintDefinition($result$constraint);
  370.             if (PEAR::isError($definition)) {
  371.                 $db->setOption('idxname_format'$idxname_format);
  372.                 return $definition;
  373.             }
  374.             $flag !empty($definition['primary'])
  375.                 ? 'primary_key' (!empty($definition['unique'])
  376.                     ? 'unique_key' : false);
  377.             if ($flag{
  378.                 foreach ($definition['fields'as $field => $sort{
  379.                     if (empty($flags[$field]|| $flags[$field!= 'primary_key'{
  380.                         $flags[$field$flag;
  381.                     }
  382.                 }
  383.             }
  384.         }
  385.  
  386.         if ($mode{
  387.             $res['num_fields'count($fields);
  388.         }
  389.  
  390.         foreach ($fields as $i => $field{
  391.             $definition $this->getTableFieldDefinition($result$field);
  392.             if (PEAR::isError($definition)) {
  393.                 $db->setOption('idxname_format'$idxname_format);
  394.                 return $definition;
  395.             }
  396.             $res[$i$definition[0];
  397.             $res[$i]['name'$field;
  398.             $res[$i]['table'$result;
  399.             $res[$i]['type'preg_replace('/^([a-z]+).*$/i''\\1'trim($definition[0]['nativetype']));
  400.             // 'primary_key', 'unique_key', 'multiple_key'
  401.             $res[$i]['flags'= empty($flags[$field]'' $flags[$field];
  402.             // not_null', 'unsigned', 'auto_increment', 'default_[rawencodedvalue]'
  403.             if (!empty($res[$i]['notnull'])) {
  404.                 $res[$i]['flags'].= ' not_null';
  405.             }
  406.             if (!empty($res[$i]['unsigned'])) {
  407.                 $res[$i]['flags'].= ' unsigned';
  408.             }
  409.             if (!empty($res[$i]['auto_increment'])) {
  410.                 $res[$i]['flags'].= ' autoincrement';
  411.             }
  412.             if (!empty($res[$i]['default'])) {
  413.                 $res[$i]['flags'].= ' default_'.rawurlencode($res[$i]['default']);
  414.             }
  415.  
  416.             if ($mode MDB2_TABLEINFO_ORDER{
  417.                 $res['order'][$res[$i]['name']] $i;
  418.             }
  419.             if ($mode MDB2_TABLEINFO_ORDERTABLE{
  420.                 $res['ordertable'][$res[$i]['table']][$res[$i]['name']] $i;
  421.             }
  422.         }
  423.  
  424.         $db->setOption('idxname_format'$idxname_format);
  425.         return $res;
  426.     }
  427. }
  428. ?>

Documentation generated on Fri, 03 Nov 2006 08:30:14 -0500 by phpDocumentor 1.3.0. PEAR Logo Copyright © PHP Group 2004.