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-2008 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. // | Authors: Lukas Smith <smith@pooteeweet.org>                          |
  43. // |          Lorenzo Alberton <l.alberton@quipo.it>                      |
  44. // +----------------------------------------------------------------------+
  45. //
  46. // $Id: Common.php,v 1.72 2009/01/14 15:00:40 quipo Exp $
  47. //
  48.  
  49. /**
  50.  * @package  MDB2
  51.  * @category Database
  52.  * @author   Lukas Smith <smith@pooteeweet.org>
  53.  * @author   Lorenzo Alberton <l.alberton@quipo.it>
  54.  */
  55.  
  56. /**
  57.  * Base class for the management modules that is extended by each MDB2 driver
  58.  *
  59.  * To load this module in the MDB2 object:
  60.  * $mdb->loadModule('Manager');
  61.  *
  62.  * @package MDB2
  63.  * @category Database
  64.  * @author  Lukas Smith <smith@pooteeweet.org>
  65.  */
  66. {
  67.     // {{{ splitTableSchema()
  68.  
  69.     /**
  70.      * Split the "[owner|schema].table" notation into an array
  71.      *
  72.      * @param string $table [schema and] table name
  73.      *
  74.      * @return array array(schema, table)
  75.      * @access private
  76.      */
  77.     function splitTableSchema($table)
  78.     {
  79.         $ret = array();
  80.         if (strpos($table'.'!== false{
  81.             return explode('.'$table);
  82.         }
  83.         return array(null$table);
  84.     }
  85.  
  86.     // }}}
  87.     // {{{ getFieldDeclarationList()
  88.  
  89.     /**
  90.      * Get declaration of a number of field in bulk
  91.      *
  92.      * @param array $fields  a multidimensional associative array.
  93.      *       The first dimension determines the field name, while the second
  94.      *       dimension is keyed with the name of the properties
  95.      *       of the field being declared as array indexes. Currently, the types
  96.      *       of supported field properties are as follows:
  97.      *
  98.      *       default
  99.      *           Boolean value to be used as default for this field.
  100.      *
  101.      *       notnull
  102.      *           Boolean flag that indicates whether this field is constrained
  103.      *           to not be set to null.
  104.      *
  105.      * @return mixed string on success, a MDB2 error on failure
  106.      * @access public
  107.      */
  108.     function getFieldDeclarationList($fields)
  109.     {
  110.         $db =$this->getDBInstance();
  111.         if (PEAR::isError($db)) {
  112.             return $db;
  113.         }
  114.  
  115.         if (!is_array($fields|| empty($fields)) {
  116.             return $db->raiseError(MDB2_ERROR_NEED_MORE_DATAnullnull,
  117.                 'missing any fields'__FUNCTION__);
  118.         }
  119.         foreach ($fields as $field_name => $field{
  120.             $query $db->getDeclaration($field['type']$field_name$field);
  121.             if (PEAR::isError($query)) {
  122.                 return $query;
  123.             }
  124.             $query_fields[$query;
  125.         }
  126.         return implode(', '$query_fields);
  127.     }
  128.  
  129.     // }}}
  130.     // {{{ _fixSequenceName()
  131.  
  132.     /**
  133.      * Removes any formatting in an sequence name using the 'seqname_format' option
  134.      *
  135.      * @param string $sqn string that containts name of a potential sequence
  136.      * @param bool $check if only formatted sequences should be returned
  137.      * @return string name of the sequence with possible formatting removed
  138.      * @access protected
  139.      */
  140.     function _fixSequenceName($sqn$check = false)
  141.     {
  142.         $db =$this->getDBInstance();
  143.         if (PEAR::isError($db)) {
  144.             return $db;
  145.         }
  146.  
  147.         $seq_pattern '/^'.preg_replace('/%s/''([a-z0-9_]+)'$db->options['seqname_format']).'$/i';
  148.         $seq_name preg_replace($seq_pattern'\\1'$sqn);
  149.         if ($seq_name && !strcasecmp($sqn$db->getSequenceName($seq_name))) {
  150.             return $seq_name;
  151.         }
  152.         if ($check{
  153.             return false;
  154.         }
  155.         return $sqn;
  156.     }
  157.  
  158.     // }}}
  159.     // {{{ _fixIndexName()
  160.  
  161.     /**
  162.      * Removes any formatting in an index name using the 'idxname_format' option
  163.      *
  164.      * @param string $idx string that containts name of anl index
  165.      * @return string name of the index with eventual formatting removed
  166.      * @access protected
  167.      */
  168.     function _fixIndexName($idx)
  169.     {
  170.         $db =$this->getDBInstance();
  171.         if (PEAR::isError($db)) {
  172.             return $db;
  173.         }
  174.  
  175.         $idx_pattern '/^'.preg_replace('/%s/''([a-z0-9_]+)'$db->options['idxname_format']).'$/i';
  176.         $idx_name preg_replace($idx_pattern'\\1'$idx);
  177.         if ($idx_name && !strcasecmp($idx$db->getIndexName($idx_name))) {
  178.             return $idx_name;
  179.         }
  180.         return $idx;
  181.     }
  182.  
  183.     // }}}
  184.     // {{{ createDatabase()
  185.  
  186.     /**
  187.      * create a new database
  188.      *
  189.      * @param string $name    name of the database that should be created
  190.      * @param array  $options array with charset, collation info
  191.      *
  192.      * @return mixed MDB2_OK on success, a MDB2 error on failure
  193.      * @access public
  194.      */
  195.     function createDatabase($database$options = array())
  196.     {
  197.         $db =$this->getDBInstance();
  198.         if (PEAR::isError($db)) {
  199.             return $db;
  200.         }
  201.  
  202.         return $db->raiseError(MDB2_ERROR_UNSUPPORTEDnullnull,
  203.             'method not implemented'__FUNCTION__);
  204.     }
  205.  
  206.     // }}}
  207.     // {{{ alterDatabase()
  208.  
  209.     /**
  210.      * alter an existing database
  211.      *
  212.      * @param string $name    name of the database that should be created
  213.      * @param array  $options array with charset, collation info
  214.      *
  215.      * @return mixed MDB2_OK on success, a MDB2 error on failure
  216.      * @access public
  217.      */
  218.     function alterDatabase($database$options = array())
  219.     {
  220.         $db =$this->getDBInstance();
  221.         if (PEAR::isError($db)) {
  222.             return $db;
  223.         }
  224.  
  225.         return $db->raiseError(MDB2_ERROR_UNSUPPORTEDnullnull,
  226.             'method not implemented'__FUNCTION__);
  227.     }
  228.  
  229.     // }}}
  230.     // {{{ dropDatabase()
  231.  
  232.     /**
  233.      * drop an existing database
  234.      *
  235.      * @param string $name name of the database that should be dropped
  236.      * @return mixed MDB2_OK on success, a MDB2 error on failure
  237.      * @access public
  238.      */
  239.     function dropDatabase($database)
  240.     {
  241.         $db =$this->getDBInstance();
  242.         if (PEAR::isError($db)) {
  243.             return $db;
  244.         }
  245.  
  246.         return $db->raiseError(MDB2_ERROR_UNSUPPORTEDnullnull,
  247.             'method not implemented'__FUNCTION__);
  248.     }
  249.  
  250.     // }}}
  251.     // {{{ _getCreateTableQuery()
  252.  
  253.     /**
  254.      * Create a basic SQL query for a new table creation
  255.      *
  256.      * @param string $name    Name of the database that should be created
  257.      * @param array  $fields  Associative array that contains the definition of each field of the new table
  258.      * @param array  $options An associative array of table options
  259.      *
  260.      * @return mixed string (the SQL query) on success, a MDB2 error on failure
  261.      * @see createTable()
  262.      */
  263.     function _getCreateTableQuery($name$fields$options = array())
  264.     {
  265.         $db =$this->getDBInstance();
  266.         if (PEAR::isError($db)) {
  267.             return $db;
  268.         }
  269.  
  270.         if (!$name{
  271.             return $db->raiseError(MDB2_ERROR_CANNOT_CREATEnullnull,
  272.                 'no valid table name specified'__FUNCTION__);
  273.         }
  274.         if (empty($fields)) {
  275.             return $db->raiseError(MDB2_ERROR_CANNOT_CREATEnullnull,
  276.                 'no fields specified for table "'.$name.'"'__FUNCTION__);
  277.         }
  278.         $query_fields $this->getFieldDeclarationList($fields);
  279.         if (PEAR::isError($query_fields)) {
  280.             return $query_fields;
  281.         }
  282.         if (!empty($options['primary'])) {
  283.             $query_fields.= ', PRIMARY KEY ('.implode(', 'array_keys($options['primary'])).')';
  284.         }
  285.  
  286.         $name $db->quoteIdentifier($nametrue);
  287.         $result 'CREATE ';
  288.         if (!empty($options['temporary'])) {
  289.             $result .= $this->_getTemporaryTableQuery();
  290.         }
  291.         $result .= " TABLE $name ($query_fields)";
  292.         return $result;
  293.     }
  294.  
  295.     // }}}
  296.     // {{{ _getTemporaryTableQuery()
  297.  
  298.     /**
  299.      * A method to return the required SQL string that fits between CREATE ... TABLE
  300.      * to create the table as a temporary table.
  301.      *
  302.      * Should be overridden in driver classes to return the correct string for the
  303.      * specific database type.
  304.      *
  305.      * The default is to return the string "TEMPORARY" - this will result in a
  306.      * SQL error for any database that does not support temporary tables, or that
  307.      * requires a different SQL command from "CREATE TEMPORARY TABLE".
  308.      *
  309.      * @return string The string required to be placed between "CREATE" and "TABLE"
  310.      *                 to generate a temporary table, if possible.
  311.      */
  312.     function _getTemporaryTableQuery()
  313.     {
  314.         return 'TEMPORARY';
  315.     }
  316.  
  317.     // }}}
  318.     // {{{ createTable()
  319.  
  320.     /**
  321.      * create a new table
  322.      *
  323.      * @param string $name   Name of the database that should be created
  324.      * @param array $fields  Associative array that contains the definition of each field of the new table
  325.      *                        The indexes of the array entries are the names of the fields of the table an
  326.      *                        the array entry values are associative arrays like those that are meant to be
  327.      *                        passed with the field definitions to get[Type]Declaration() functions.
  328.      *                           array(
  329.      *                               'id' => array(
  330.      *                                   'type' => 'integer',
  331.      *                                   'unsigned' => 1
  332.      *                                   'notnull' => 1
  333.      *                                   'default' => 0
  334.      *                               ),
  335.      *                               'name' => array(
  336.      *                                   'type' => 'text',
  337.      *                                   'length' => 12
  338.      *                               ),
  339.      *                               'password' => array(
  340.      *                                   'type' => 'text',
  341.      *                                   'length' => 12
  342.      *                               )
  343.      *                           );
  344.      * @param array $options  An associative array of table options:
  345.      *                           array(
  346.      *                               'comment' => 'Foo',
  347.      *                               'temporary' => true|false,
  348.      *                           );
  349.      * @return mixed MDB2_OK on success, a MDB2 error on failure
  350.      * @access public
  351.      */
  352.     function createTable($name$fields$options = array())
  353.     {
  354.         $query $this->_getCreateTableQuery($name$fields$options);
  355.         if (PEAR::isError($query)) {
  356.             return $query;
  357.         }
  358.         $db =$this->getDBInstance();
  359.         if (PEAR::isError($db)) {
  360.             return $db;
  361.         }
  362.         $result $db->exec($query);
  363.         if (PEAR::isError($result)) {
  364.             return $result;
  365.         }
  366.         return MDB2_OK;
  367.     }
  368.  
  369.     // }}}
  370.     // {{{ dropTable()
  371.  
  372.     /**
  373.      * drop an existing table
  374.      *
  375.      * @param string $name name of the table that should be dropped
  376.      * @return mixed MDB2_OK on success, a MDB2 error on failure
  377.      * @access public
  378.      */
  379.     function dropTable($name)
  380.     {
  381.         $db =$this->getDBInstance();
  382.         if (PEAR::isError($db)) {
  383.             return $db;
  384.         }
  385.  
  386.         $name $db->quoteIdentifier($nametrue);
  387.         return $db->exec("DROP TABLE $name");
  388.     }
  389.  
  390.     // }}}
  391.     // {{{ truncateTable()
  392.  
  393.     /**
  394.      * Truncate an existing table (if the TRUNCATE TABLE syntax is not supported,
  395.      * it falls back to a DELETE FROM TABLE query)
  396.      *
  397.      * @param string $name name of the table that should be truncated
  398.      * @return mixed MDB2_OK on success, a MDB2 error on failure
  399.      * @access public
  400.      */
  401.     function truncateTable($name)
  402.     {
  403.         $db =$this->getDBInstance();
  404.         if (PEAR::isError($db)) {
  405.             return $db;
  406.         }
  407.  
  408.         $name $db->quoteIdentifier($nametrue);
  409.         return $db->exec("DELETE FROM $name");
  410.     }
  411.  
  412.     // }}}
  413.     // {{{ vacuum()
  414.  
  415.     /**
  416.      * Optimize (vacuum) all the tables in the db (or only the specified table)
  417.      * and optionally run ANALYZE.
  418.      *
  419.      * @param string $table table name (all the tables if empty)
  420.      * @param array  $options an array with driver-specific options:
  421.      *                - timeout [int] (in seconds) [mssql-only]
  422.      *                - analyze [boolean] [pgsql and mysql]
  423.      *                - full [boolean] [pgsql-only]
  424.      *                - freeze [boolean] [pgsql-only]
  425.      *
  426.      * @return mixed MDB2_OK success, a MDB2 error on failure
  427.      * @access public
  428.      */
  429.     function vacuum($table = null$options = array())
  430.     {
  431.         $db =$this->getDBInstance();
  432.         if (PEAR::isError($db)) {
  433.             return $db;
  434.         }
  435.  
  436.         return $db->raiseError(MDB2_ERROR_UNSUPPORTEDnullnull,
  437.             'method not implemented'__FUNCTION__);
  438.     }
  439.  
  440.     // }}}
  441.     // {{{ alterTable()
  442.  
  443.     /**
  444.      * alter an existing table
  445.      *
  446.      * @param string $name         name of the table that is intended to be changed.
  447.      * @param array $changes     associative array that contains the details of each type
  448.      *                              of change that is intended to be performed. The types of
  449.      *                              changes that are currently supported are defined as follows:
  450.      *
  451.      *                              name
  452.      *
  453.      *                                 New name for the table.
  454.      *
  455.      *                             add
  456.      *
  457.      *                                 Associative array with the names of fields to be added as
  458.      *                                  indexes of the array. The value of each entry of the array
  459.      *                                  should be set to another associative array with the properties
  460.      *                                  of the fields to be added. The properties of the fields should
  461.      *                                  be the same as defined by the MDB2 parser.
  462.      *
  463.      *
  464.      *                             remove
  465.      *
  466.      *                                 Associative array with the names of fields to be removed as indexes
  467.      *                                  of the array. Currently the values assigned to each entry are ignored.
  468.      *                                  An empty array should be used for future compatibility.
  469.      *
  470.      *                             rename
  471.      *
  472.      *                                 Associative array with the names of fields to be renamed as indexes
  473.      *                                  of the array. The value of each entry of the array should be set to
  474.      *                                  another associative array with the entry named name with the new
  475.      *                                  field name and the entry named Declaration that is expected to contain
  476.      *                                  the portion of the field declaration already in DBMS specific SQL code
  477.      *                                  as it is used in the CREATE TABLE statement.
  478.      *
  479.      *                             change
  480.      *
  481.      *                                 Associative array with the names of the fields to be changed as indexes
  482.      *                                  of the array. Keep in mind that if it is intended to change either the
  483.      *                                  name of a field and any other properties, the change array entries
  484.      *                                  should have the new names of the fields as array indexes.
  485.      *
  486.      *                                 The value of each entry of the array should be set to another associative
  487.      *                                  array with the properties of the fields to that are meant to be changed as
  488.      *                                  array entries. These entries should be assigned to the new values of the
  489.      *                                  respective properties. The properties of the fields should be the same
  490.      *                                  as defined by the MDB2 parser.
  491.      *
  492.      *                             Example
  493.      *                                 array(
  494.      *                                     'name' => 'userlist',
  495.      *                                     'add' => array(
  496.      *                                         'quota' => array(
  497.      *                                             'type' => 'integer',
  498.      *                                             'unsigned' => 1
  499.      *                                         )
  500.      *                                     ),
  501.      *                                     'remove' => array(
  502.      *                                         'file_limit' => array(),
  503.      *                                         'time_limit' => array()
  504.      *                                     ),
  505.      *                                     'change' => array(
  506.      *                                         'name' => array(
  507.      *                                             'length' => '20',
  508.      *                                             'definition' => array(
  509.      *                                                 'type' => 'text',
  510.      *                                                 'length' => 20,
  511.      *                                             ),
  512.      *                                         )
  513.      *                                     ),
  514.      *                                     'rename' => array(
  515.      *                                         'sex' => array(
  516.      *                                             'name' => 'gender',
  517.      *                                             'definition' => array(
  518.      *                                                 'type' => 'text',
  519.      *                                                 'length' => 1,
  520.      *                                                 'default' => 'M',
  521.      *                                             ),
  522.      *                                         )
  523.      *                                     )
  524.      *                                 )
  525.      *
  526.      * @param boolean $check     indicates whether the function should just check if the DBMS driver
  527.      *                              can perform the requested table alterations if the value is true or
  528.      *                              actually perform them otherwise.
  529.      * @access public
  530.      *
  531.       * @return mixed MDB2_OK on success, a MDB2 error on failure
  532.      */
  533.     function alterTable($name$changes$check)
  534.     {
  535.         $db =$this->getDBInstance();
  536.         if (PEAR::isError($db)) {
  537.             return $db;
  538.         }
  539.  
  540.         return $db->raiseError(MDB2_ERROR_UNSUPPORTEDnullnull,
  541.             'method not implemented'__FUNCTION__);
  542.     }
  543.  
  544.     // }}}
  545.     // {{{ listDatabases()
  546.  
  547.     /**
  548.      * list all databases
  549.      *
  550.      * @return mixed array of database names on success, a MDB2 error on failure
  551.      * @access public
  552.      */
  553.     function listDatabases()
  554.     {
  555.         $db =$this->getDBInstance();
  556.         if (PEAR::isError($db)) {
  557.             return $db;
  558.         }
  559.  
  560.         return $db->raiseError(MDB2_ERROR_UNSUPPORTEDnullnull,
  561.             'method not implementedd'__FUNCTION__);
  562.     }
  563.  
  564.     // }}}
  565.     // {{{ listUsers()
  566.  
  567.     /**
  568.      * list all users
  569.      *
  570.      * @return mixed array of user names on success, a MDB2 error on failure
  571.      * @access public
  572.      */
  573.     function listUsers()
  574.     {
  575.         $db =$this->getDBInstance();
  576.         if (PEAR::isError($db)) {
  577.             return $db;
  578.         }
  579.  
  580.         return $db->raiseError(MDB2_ERROR_UNSUPPORTEDnullnull,
  581.             'method not implemented'__FUNCTION__);
  582.     }
  583.  
  584.     // }}}
  585.     // {{{ listViews()
  586.  
  587.     /**
  588.      * list all views in the current database
  589.      *
  590.      * @param string database, the current is default
  591.      *                NB: not all the drivers can get the view names from
  592.      *                a database other than the current one
  593.      * @return mixed array of view names on success, a MDB2 error on failure
  594.      * @access public
  595.      */
  596.     function listViews($database = null)
  597.     {
  598.         $db =$this->getDBInstance();
  599.         if (PEAR::isError($db)) {
  600.             return $db;
  601.         }
  602.  
  603.         return $db->raiseError(MDB2_ERROR_UNSUPPORTEDnullnull,
  604.             'method not implemented'__FUNCTION__);
  605.     }
  606.  
  607.     // }}}
  608.     // {{{ listTableViews()
  609.  
  610.     /**
  611.      * list the views in the database that reference a given table
  612.      *
  613.      * @param string table for which all referenced views should be found
  614.      * @return mixed array of view names on success, a MDB2 error on failure
  615.      * @access public
  616.      */
  617.     function listTableViews($table)
  618.     {
  619.         $db =$this->getDBInstance();
  620.         if (PEAR::isError($db)) {
  621.             return $db;
  622.         }
  623.  
  624.         return $db->raiseError(MDB2_ERROR_UNSUPPORTEDnullnull,
  625.             'method not implemented'__FUNCTION__);
  626.     }
  627.  
  628.     // }}}
  629.     // {{{ listTableTriggers()
  630.  
  631.     /**
  632.      * list all triggers in the database that reference a given table
  633.      *
  634.      * @param string table for which all referenced triggers should be found
  635.      * @return mixed array of trigger names on success, a MDB2 error on failure
  636.      * @access public
  637.      */
  638.     function listTableTriggers($table = null)
  639.     {
  640.         $db =$this->getDBInstance();
  641.         if (PEAR::isError($db)) {
  642.             return $db;
  643.         }
  644.  
  645.         return $db->raiseError(MDB2_ERROR_UNSUPPORTEDnullnull,
  646.             'method not implemented'__FUNCTION__);
  647.     }
  648.  
  649.     // }}}
  650.     // {{{ listFunctions()
  651.  
  652.     /**
  653.      * list all functions in the current database
  654.      *
  655.      * @return mixed array of function names on success, a MDB2 error on failure
  656.      * @access public
  657.      */
  658.     function listFunctions()
  659.     {
  660.         $db =$this->getDBInstance();
  661.         if (PEAR::isError($db)) {
  662.             return $db;
  663.         }
  664.  
  665.         return $db->raiseError(MDB2_ERROR_UNSUPPORTEDnullnull,
  666.             'method not implemented'__FUNCTION__);
  667.     }
  668.  
  669.     // }}}
  670.     // {{{ listTables()
  671.  
  672.     /**
  673.      * list all tables in the current database
  674.      *
  675.      * @param string database, the current is default.
  676.      *                NB: not all the drivers can get the table names from
  677.      *                a database other than the current one
  678.      * @return mixed array of table names on success, a MDB2 error on failure
  679.      * @access public
  680.      */
  681.     function listTables($database = null)
  682.     {
  683.         $db =$this->getDBInstance();
  684.         if (PEAR::isError($db)) {
  685.             return $db;
  686.         }
  687.  
  688.         return $db->raiseError(MDB2_ERROR_UNSUPPORTEDnullnull,
  689.             'method not implemented'__FUNCTION__);
  690.     }
  691.  
  692.     // }}}
  693.     // {{{ listTableFields()
  694.  
  695.     /**
  696.      * list all fields in a table in the current database
  697.      *
  698.      * @param string $table name of table that should be used in method
  699.      * @return mixed array of field names on success, a MDB2 error on failure
  700.      * @access public
  701.      */
  702.     function listTableFields($table)
  703.     {
  704.         $db =$this->getDBInstance();
  705.         if (PEAR::isError($db)) {
  706.             return $db;
  707.         }
  708.  
  709.         return $db->raiseError(MDB2_ERROR_UNSUPPORTEDnullnull,
  710.             'method not implemented'__FUNCTION__);
  711.     }
  712.  
  713.     // }}}
  714.     // {{{ createIndex()
  715.  
  716.     /**
  717.      * Get the stucture of a field into an array
  718.      *
  719.      * @param string    $table         name of the table on which the index is to be created
  720.      * @param string    $name         name of the index to be created
  721.      * @param array     $definition        associative array that defines properties of the index to be created.
  722.      *                                  Currently, only one property named FIELDS is supported. This property
  723.      *                                  is also an associative with the names of the index fields as array
  724.      *                                  indexes. Each entry of this array is set to another type of associative
  725.      *                                  array that specifies properties of the index that are specific to
  726.      *                                  each field.
  727.      *
  728.      *                                 Currently, only the sorting property is supported. It should be used
  729.      *                                  to define the sorting direction of the index. It may be set to either
  730.      *                                  ascending or descending.
  731.      *
  732.      *                                 Not all DBMS support index sorting direction configuration. The DBMS
  733.      *                                  drivers of those that do not support it ignore this property. Use the
  734.      *                                  function supports() to determine whether the DBMS driver can manage indexes.
  735.      *
  736.      *                                  Example
  737.      *                                     array(
  738.      *                                         'fields' => array(
  739.      *                                             'user_name' => array(
  740.      *                                                 'sorting' => 'ascending'
  741.      *                                             ),
  742.      *                                             'last_login' => array()
  743.      *                                         )
  744.      *                                     )
  745.      * @return mixed MDB2_OK on success, a MDB2 error on failure
  746.      * @access public
  747.      */
  748.     function createIndex($table$name$definition)
  749.     {
  750.         $db =$this->getDBInstance();
  751.         if (PEAR::isError($db)) {
  752.             return $db;
  753.         }
  754.  
  755.         $table $db->quoteIdentifier($tabletrue);
  756.         $name $db->quoteIdentifier($db->getIndexName($name)true);
  757.         $query = "CREATE INDEX $name ON $table";
  758.         $fields = array();
  759.         foreach (array_keys($definition['fields']as $field{
  760.             $fields[$db->quoteIdentifier($fieldtrue);
  761.         }
  762.         $query .= ' ('implode(', '$fields')';
  763.         return $db->exec($query);
  764.     }
  765.  
  766.     // }}}
  767.     // {{{ dropIndex()
  768.  
  769.     /**
  770.      * drop existing index
  771.      *
  772.      * @param string    $table         name of table that should be used in method
  773.      * @param string    $name         name of the index to be dropped
  774.      * @return mixed MDB2_OK on success, a MDB2 error on failure
  775.      * @access public
  776.      */
  777.     function dropIndex($table$name)
  778.     {
  779.         $db =$this->getDBInstance();
  780.         if (PEAR::isError($db)) {
  781.             return $db;
  782.         }
  783.  
  784.         $name $db->quoteIdentifier($db->getIndexName($name)true);
  785.         return $db->exec("DROP INDEX $name");
  786.     }
  787.  
  788.     // }}}
  789.     // {{{ listTableIndexes()
  790.  
  791.     /**
  792.      * list all indexes in a table
  793.      *
  794.      * @param string $table name of table that should be used in method
  795.      * @return mixed array of index names on success, a MDB2 error on failure
  796.      * @access public
  797.      */
  798.     function listTableIndexes($table)
  799.     {
  800.         $db =$this->getDBInstance();
  801.         if (PEAR::isError($db)) {
  802.             return $db;
  803.         }
  804.  
  805.         return $db->raiseError(MDB2_ERROR_UNSUPPORTEDnullnull,
  806.             'method not implemented'__FUNCTION__);
  807.     }
  808.  
  809.     // }}}
  810.     // {{{ _getAdvancedFKOptions()
  811.  
  812.     /**
  813.      * Return the FOREIGN KEY query section dealing with non-standard options
  814.      * as MATCH, INITIALLY DEFERRED, ON UPDATE, ...
  815.      *
  816.      * @param array $definition 
  817.      * @return string 
  818.      * @access protected
  819.      */
  820.     function _getAdvancedFKOptions($definition)
  821.     {
  822.         return '';
  823.     }
  824.  
  825.     // }}}
  826.     // {{{ createConstraint()
  827.  
  828.     /**
  829.      * create a constraint on a table
  830.      *
  831.      * @param string    $table       name of the table on which the constraint is to be created
  832.      * @param string    $name        name of the constraint to be created
  833.      * @param array     $definition  associative array that defines properties of the constraint to be created.
  834.      *                                The full structure of the array looks like this:
  835.      *           <pre>
  836.      *           array (
  837.      *               [primary] => 0
  838.      *               [unique]  => 0
  839.      *               [foreign] => 1
  840.      *               [check]   => 0
  841.      *               [fields] => array (
  842.      *                   [field1name] => array() // one entry per each field covered
  843.      *                   [field2name] => array() // by the index
  844.      *                   [field3name] => array(
  845.      *                       [sorting]  => ascending
  846.      *                       [position] => 3
  847.      *                   )
  848.      *               )
  849.      *               [references] => array(
  850.      *                   [table] => name
  851.      *                   [fields] => array(
  852.      *                       [field1name] => array(  //one entry per each referenced field
  853.      *                            [position] => 1
  854.      *                       )
  855.      *                   )
  856.      *               )
  857.      *               [deferrable] => 0
  858.      *               [initiallydeferred] => 0
  859.      *               [onupdate] => CASCADE|RESTRICT|SET NULL|SET DEFAULT|NO ACTION
  860.      *               [ondelete] => CASCADE|RESTRICT|SET NULL|SET DEFAULT|NO ACTION
  861.      *               [match] => SIMPLE|PARTIAL|FULL
  862.      *           );
  863.      *           </pre>
  864.      * @return mixed MDB2_OK on success, a MDB2 error on failure
  865.      * @access public
  866.      */
  867.     function createConstraint($table$name$definition)
  868.     {
  869.         $db =$this->getDBInstance();
  870.         if (PEAR::isError($db)) {
  871.             return $db;
  872.         }
  873.         $table $db->quoteIdentifier($tabletrue);
  874.         $name $db->quoteIdentifier($db->getIndexName($name)true);
  875.         $query = "ALTER TABLE $table ADD CONSTRAINT $name";
  876.         if (!empty($definition['primary'])) {
  877.             $query.= ' PRIMARY KEY';
  878.         elseif (!empty($definition['unique'])) {
  879.             $query.= ' UNIQUE';
  880.         elseif (!empty($definition['foreign'])) {
  881.             $query.= ' FOREIGN KEY';
  882.         }
  883.         $fields = array();
  884.         foreach (array_keys($definition['fields']as $field{
  885.             $fields[$db->quoteIdentifier($fieldtrue);
  886.         }
  887.         $query .= ' ('implode(', '$fields')';
  888.         if (!empty($definition['foreign'])) {
  889.             $query.= ' REFERENCES ' $db->quoteIdentifier($definition['references']['table']true);
  890.             $referenced_fields = array();
  891.             foreach (array_keys($definition['references']['fields']as $field{
  892.                 $referenced_fields[$db->quoteIdentifier($fieldtrue);
  893.             }
  894.             $query .= ' ('implode(', '$referenced_fields')';
  895.             $query .= $this->_getAdvancedFKOptions($definition);
  896.         }
  897.         return $db->exec($query);
  898.     }
  899.  
  900.     // }}}
  901.     // {{{ dropConstraint()
  902.  
  903.     /**
  904.      * drop existing constraint
  905.      *
  906.      * @param string    $table        name of table that should be used in method
  907.      * @param string    $name         name of the constraint to be dropped
  908.      * @param string    $primary      hint if the constraint is primary
  909.      * @return mixed MDB2_OK on success, a MDB2 error on failure
  910.      * @access public
  911.      */
  912.     function dropConstraint($table$name$primary = false)
  913.     {
  914.         $db =$this->getDBInstance();
  915.         if (PEAR::isError($db)) {
  916.             return $db;
  917.         }
  918.  
  919.         $table $db->quoteIdentifier($tabletrue);
  920.         $name $db->quoteIdentifier($db->getIndexName($name)true);
  921.         return $db->exec("ALTER TABLE $table DROP CONSTRAINT $name");
  922.     }
  923.  
  924.     // }}}
  925.     // {{{ listTableConstraints()
  926.  
  927.     /**
  928.      * list all constraints in a table
  929.      *
  930.      * @param string $table name of table that should be used in method
  931.      * @return mixed array of constraint names on success, a MDB2 error on failure
  932.      * @access public
  933.      */
  934.     function listTableConstraints($table)
  935.     {
  936.         $db =$this->getDBInstance();
  937.         if (PEAR::isError($db)) {
  938.             return $db;
  939.         }
  940.  
  941.         return $db->raiseError(MDB2_ERROR_UNSUPPORTEDnullnull,
  942.             'method not implemented'__FUNCTION__);
  943.     }
  944.  
  945.     // }}}
  946.     // {{{ createSequence()
  947.  
  948.     /**
  949.      * create sequence
  950.      *
  951.      * @param string    $seq_name     name of the sequence to be created
  952.      * @param string    $start         start value of the sequence; default is 1
  953.      * @return mixed MDB2_OK on success, a MDB2 error on failure
  954.      * @access public
  955.      */
  956.     function createSequence($seq_name$start = 1)
  957.     {
  958.         $db =$this->getDBInstance();
  959.         if (PEAR::isError($db)) {
  960.             return $db;
  961.         }
  962.  
  963.         return $db->raiseError(MDB2_ERROR_UNSUPPORTEDnullnull,
  964.             'method not implemented'__FUNCTION__);
  965.     }
  966.  
  967.     // }}}
  968.     // {{{ dropSequence()
  969.  
  970.     /**
  971.      * drop existing sequence
  972.      *
  973.      * @param string    $seq_name     name of the sequence to be dropped
  974.      * @return mixed MDB2_OK on success, a MDB2 error on failure
  975.      * @access public
  976.      */
  977.     function dropSequence($name)
  978.     {
  979.         $db =$this->getDBInstance();
  980.         if (PEAR::isError($db)) {
  981.             return $db;
  982.         }
  983.  
  984.         return $db->raiseError(MDB2_ERROR_UNSUPPORTEDnullnull,
  985.             'method not implemented'__FUNCTION__);
  986.     }
  987.  
  988.     // }}}
  989.     // {{{ listSequences()
  990.  
  991.     /**
  992.      * list all sequences in the current database
  993.      *
  994.      * @param string database, the current is default
  995.      *                NB: not all the drivers can get the sequence names from
  996.      *                a database other than the current one
  997.      * @return mixed array of sequence names on success, a MDB2 error on failure
  998.      * @access public
  999.      */
  1000.     function listSequences($database = null)
  1001.     {
  1002.         $db =$this->getDBInstance();
  1003.         if (PEAR::isError($db)) {
  1004.             return $db;
  1005.         }
  1006.  
  1007.         return $db->raiseError(MDB2_ERROR_UNSUPPORTEDnullnull,
  1008.             'method not implemented'__FUNCTION__);
  1009.     }
  1010.  
  1011.     // }}}
  1012. }
  1013. ?>

Documentation generated on Wed, 14 Jan 2009 12:30:10 -0500 by phpDocumentor 1.4.0. PEAR Logo Copyright © PHP Group 2004.