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

Source for file oci8.php

Documentation is available at oci8.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. // | Author: Lukas Smith <smith@pooteeweet.org>                           |
  43. // +----------------------------------------------------------------------+
  44.  
  45. // $Id: oci8.php 327310 2012-08-27 15:16:18Z danielc $
  46.  
  47. require_once 'MDB2/Driver/Manager/Common.php';
  48.  
  49. /**
  50.  * MDB2 oci8 driver for the management modules
  51.  *
  52.  * @package MDB2
  53.  * @category Database
  54.  * @author Lukas Smith <smith@pooteeweet.org>
  55.  */
  56. class MDB2_Driver_Manager_oci8 extends MDB2_Driver_Manager_Common
  57. {
  58.     // {{{ createDatabase()
  59.  
  60.     /**
  61.      * create a new database
  62.      *
  63.      * @param string $name    name of the database that should be created
  64.      * @param array  $options array with charset, collation info
  65.      *
  66.      * @return mixed MDB2_OK on success, a MDB2 error on failure
  67.      * @access public
  68.      */
  69.     function createDatabase($name$options = array())
  70.     {
  71.         $db $this->getDBInstance();
  72.         if (MDB2::isError($db)) {
  73.             return $db;
  74.         }
  75.  
  76.         $username $db->options['database_name_prefix'].$name;
  77.         $password $db->dsn['password'$db->dsn['password'$name;
  78.         $tablespace $db->options['default_tablespace']
  79.             ? ' DEFAULT TABLESPACE '.$db->options['default_tablespace''';
  80.  
  81.         $query 'CREATE USER '.$username.' IDENTIFIED BY '.$password.$tablespace;
  82.         $result $db->standaloneQuery($querynulltrue);
  83.         if (MDB2::isError($result)) {
  84.             return $result;
  85.         }
  86.         $query 'GRANT CREATE SESSION, CREATE TABLE, UNLIMITED TABLESPACE, CREATE SEQUENCE, CREATE TRIGGER TO '.$username;
  87.         $result $db->standaloneQuery($querynulltrue);
  88.         if (MDB2::isError($result)) {
  89.             $query 'DROP USER '.$username.' CASCADE';
  90.             $result2 $db->standaloneQuery($querynulltrue);
  91.             if (MDB2::isError($result2)) {
  92.                 return $db->raiseError($result2nullnull,
  93.                     'could not setup the database user'__FUNCTION__);
  94.             }
  95.             return $result;
  96.         }
  97.         return MDB2_OK;
  98.     }
  99.  
  100.     // }}}
  101.     // {{{ alterDatabase()
  102.  
  103.     /**
  104.      * alter an existing database
  105.      *
  106.      * IMPORTANT: the safe way to change the db charset is to do a full import/export!
  107.      * If - and only if - the new character set is a strict superset of the current
  108.      * character set, it is possible to use the ALTER DATABASE CHARACTER SET to
  109.      * expedite the change in the database character set.
  110.      *
  111.      * @param string $name    name of the database that is intended to be changed
  112.      * @param array  $options array with name, charset info
  113.      *
  114.      * @return mixed MDB2_OK on success, a MDB2 error on failure
  115.      * @access public
  116.      */
  117.     function alterDatabase($name$options = array())
  118.     {
  119.         //disabled
  120.         //return parent::alterDatabase($name, $options);
  121.  
  122.         $db $this->getDBInstance();
  123.         if (MDB2::isError($db)) {
  124.             return $db;
  125.         }
  126.  
  127.         if (!empty($options['name'])) {
  128.             $query 'ALTER DATABASE ' $db->quoteIdentifier($nametrue)
  129.                     .' RENAME GLOBAL_NAME TO ' $db->quoteIdentifier($options['name']true);
  130.             $result $db->standaloneQuery($query);
  131.             if (MDB2::isError($result)) {
  132.                 return $result;
  133.             }
  134.         }
  135.  
  136.         if (!empty($options['charset'])) {
  137.             $queries = array();
  138.             $queries['SHUTDOWN IMMEDIATE'//or NORMAL
  139.             $queries['STARTUP MOUNT';
  140.             $queries['ALTER SYSTEM ENABLE RESTRICTED SESSION';
  141.             $queries['ALTER SYSTEM SET JOB_QUEUE_PROCESSES=0';
  142.             $queries['ALTER DATABASE OPEN';
  143.             $queries['ALTER DATABASE CHARACTER SET ' $options['charset'];
  144.             $queries['ALTER DATABASE NATIONAL CHARACTER SET ' $options['charset'];
  145.             $queries['SHUTDOWN IMMEDIATE'//or NORMAL
  146.             $queries['STARTUP';
  147.  
  148.             foreach ($queries as $query{
  149.                 $result $db->standaloneQuery($query);
  150.                 if (MDB2::isError($result)) {
  151.                     return $result;
  152.                 }
  153.             }
  154.         }
  155.  
  156.         return MDB2_OK;
  157.     }
  158.  
  159.     // }}}
  160.     // {{{ dropDatabase()
  161.  
  162.     /**
  163.      * drop an existing database
  164.      *
  165.      * @param object $db database object that is extended by this class
  166.      * @param string $name name of the database that should be dropped
  167.      * @return mixed MDB2_OK on success, a MDB2 error on failure
  168.      * @access public
  169.      */
  170.     function dropDatabase($name)
  171.     {
  172.         $db $this->getDBInstance();
  173.         if (MDB2::isError($db)) {
  174.             return $db;
  175.         }
  176.  
  177.         $username $db->options['database_name_prefix'].$name;
  178.         return $db->standaloneQuery('DROP USER '.$username.' CASCADE'nulltrue);
  179.     }
  180.  
  181.  
  182.     // }}}
  183.     // {{{ _makeAutoincrement()
  184.  
  185.     /**
  186.      * add an autoincrement sequence + trigger
  187.      *
  188.      * @param string $name  name of the PK field
  189.      * @param string $table name of the table
  190.      * @param string $start start value for the sequence
  191.      * @return mixed        MDB2_OK on success, a MDB2 error on failure
  192.      * @access private
  193.      */
  194.     function _makeAutoincrement($name$table$start = 1)
  195.     {
  196.         $db $this->getDBInstance();
  197.         if (MDB2::isError($db)) {
  198.             return $db;
  199.         }
  200.  
  201.         $table_uppercase strtoupper($table);
  202.         $index_name  $table_uppercase '_AI_PK';
  203.         $definition = array(
  204.             'primary' => true,
  205.             'fields' => array($name => true),
  206.         );
  207.         $idxname_format $db->getOption('idxname_format');
  208.         $db->setOption('idxname_format''%s');
  209.         $result $this->createConstraint($table$index_name$definition);
  210.         $db->setOption('idxname_format'$idxname_format);
  211.         if (MDB2::isError($result)) {
  212.             return $db->raiseError($resultnullnull,
  213.                 'primary key for autoincrement PK could not be created'__FUNCTION__);
  214.         }
  215.  
  216.         if (null === $start{
  217.             $db->beginTransaction();
  218.             $query 'SELECT MAX(' $db->quoteIdentifier($nametrue') FROM ' $db->quoteIdentifier($tabletrue);
  219.             $start $this->db->queryOne($query'integer');
  220.             if (MDB2::isError($start)) {
  221.                 return $start;
  222.             }
  223.             ++$start;
  224.             $result $this->createSequence($table$start);
  225.             $db->commit();
  226.         else {
  227.             $result $this->createSequence($table$start);
  228.         }
  229.         if (MDB2::isError($result)) {
  230.             return $db->raiseError($resultnullnull,
  231.                 'sequence for autoincrement PK could not be created'__FUNCTION__);
  232.         }
  233.         $seq_name        $db->getSequenceName($table);
  234.         $trigger_name    $db->quoteIdentifier($table_uppercase '_AI_PK'true);
  235.         $seq_name_quoted $db->quoteIdentifier($seq_nametrue);
  236.         $table $db->quoteIdentifier($tabletrue);
  237.         $name  $db->quoteIdentifier($nametrue);
  238.         $trigger_sql '
  239. CREATE TRIGGER '.$trigger_name.'
  240.    BEFORE INSERT
  241.    ON '.$table.'
  242.    FOR EACH ROW
  243. DECLARE
  244.    last_Sequence NUMBER;
  245.    last_InsertID NUMBER;
  246. BEGIN
  247.    SELECT '.$seq_name_quoted.'.NEXTVAL INTO :NEW.'.$name.' FROM DUAL;
  248.    IF (:NEW.'.$name.' IS NULL OR :NEW.'.$name.' = 0) THEN
  249.       SELECT '.$seq_name_quoted.'.NEXTVAL INTO :NEW.'.$name.' FROM DUAL;
  250.    ELSE
  251.       SELECT NVL(Last_Number, 0) INTO last_Sequence
  252.         FROM User_Sequences
  253.        WHERE UPPER(Sequence_Name) = UPPER(\''.$seq_name.'\');
  254.       SELECT :NEW.'.$name.' INTO last_InsertID FROM DUAL;
  255.       WHILE (last_InsertID > last_Sequence) LOOP
  256.          SELECT '.$seq_name_quoted.'.NEXTVAL INTO last_Sequence FROM DUAL;
  257.       END LOOP;
  258.    END IF;
  259. END;
  260. ';
  261.         $result $db->exec($trigger_sql);
  262.         if (MDB2::isError($result)) {
  263.             return $result;
  264.         }
  265.         return MDB2_OK;
  266.     }
  267.  
  268.     // }}}
  269.     // {{{ _dropAutoincrement()
  270.  
  271.     /**
  272.      * drop an existing autoincrement sequence + trigger
  273.      *
  274.      * @param string $table name of the table
  275.      * @return mixed        MDB2_OK on success, a MDB2 error on failure
  276.      * @access private
  277.      */
  278.     function _dropAutoincrement($table)
  279.     {
  280.         $db $this->getDBInstance();
  281.         if (MDB2::isError($db)) {
  282.             return $db;
  283.         }
  284.  
  285.         $table strtoupper($table);
  286.         $trigger_name $table '_AI_PK';
  287.         $trigger_name_quoted $db->quote($trigger_name'text');
  288.         $query 'SELECT trigger_name FROM user_triggers';
  289.         $query.= ' WHERE trigger_name='.$trigger_name_quoted.' OR trigger_name='.strtoupper($trigger_name_quoted);
  290.         $trigger $db->queryOne($query);
  291.         if (MDB2::isError($trigger)) {
  292.             return $trigger;
  293.         }
  294.  
  295.         if ($trigger{
  296.             $trigger_name  $db->quoteIdentifier($table '_AI_PK'true);
  297.             $trigger_sql 'DROP TRIGGER ' $trigger_name;
  298.             $result $db->exec($trigger_sql);
  299.             if (MDB2::isError($result)) {
  300.                 return $db->raiseError($resultnullnull,
  301.                     'trigger for autoincrement PK could not be dropped'__FUNCTION__);
  302.             }
  303.  
  304.             $result $this->dropSequence($table);
  305.             if (MDB2::isError($result)) {
  306.                 return $db->raiseError($resultnullnull,
  307.                     'sequence for autoincrement PK could not be dropped'__FUNCTION__);
  308.             }
  309.  
  310.             $index_name $table '_AI_PK';
  311.             $idxname_format $db->getOption('idxname_format');
  312.             $db->setOption('idxname_format''%s');
  313.             $result1 $this->dropConstraint($table$index_name);
  314.             $db->setOption('idxname_format'$idxname_format);
  315.             $result2 $this->dropConstraint($table$index_name);
  316.             if (MDB2::isError($result1&& MDB2::isError($result2)) {
  317.                 return $db->raiseError($result1nullnull,
  318.                     'primary key for autoincrement PK could not be dropped'__FUNCTION__);
  319.             }
  320.         }
  321.  
  322.         return MDB2_OK;
  323.     }
  324.  
  325.     // }}}
  326.     // {{{ _getTemporaryTableQuery()
  327.  
  328.     /**
  329.      * A method to return the required SQL string that fits between CREATE ... TABLE
  330.      * to create the table as a temporary table.
  331.      *
  332.      * @return string The string required to be placed between "CREATE" and "TABLE"
  333.      *                 to generate a temporary table, if possible.
  334.      */
  335.     function _getTemporaryTableQuery()
  336.     {
  337.         return 'GLOBAL TEMPORARY';
  338.     }
  339.  
  340.     // }}}
  341.     // {{{ _getAdvancedFKOptions()
  342.  
  343.     /**
  344.      * Return the FOREIGN KEY query section dealing with non-standard options
  345.      * as MATCH, INITIALLY DEFERRED, ON UPDATE, ...
  346.      *
  347.      * @param array $definition 
  348.      * @return string 
  349.      * @access protected
  350.      */
  351.     function _getAdvancedFKOptions($definition)
  352.     {
  353.         $query '';
  354.         if (!empty($definition['ondelete']&& (strtoupper($definition['ondelete']!= 'NO ACTION')) {
  355.             $query .= ' ON DELETE '.$definition['ondelete'];
  356.         }
  357.         if (!empty($definition['deferrable'])) {
  358.             $query .= ' DEFERRABLE';
  359.         else {
  360.             $query .= ' NOT DEFERRABLE';
  361.         }
  362.         if (!empty($definition['initiallydeferred'])) {
  363.             $query .= ' INITIALLY DEFERRED';
  364.         else {
  365.             $query .= ' INITIALLY IMMEDIATE';
  366.         }
  367.         return $query;
  368.     }
  369.  
  370.     // }}}
  371.     // {{{ createTable()
  372.  
  373.     /**
  374.      * create a new table
  375.      *
  376.      * @param string $name     Name of the database that should be created
  377.      * @param array $fields Associative array that contains the definition of each field of the new table
  378.      *                         The indexes of the array entries are the names of the fields of the table an
  379.      *                         the array entry values are associative arrays like those that are meant to be
  380.      *                          passed with the field definitions to get[Type]Declaration() functions.
  381.      *
  382.      *                         Example
  383.      *                         array(
  384.      *
  385.      *                             'id' => array(
  386.      *                                 'type' => 'integer',
  387.      *                                 'unsigned' => 1
  388.      *                                 'notnull' => 1
  389.      *                                 'default' => 0
  390.      *                             ),
  391.      *                             'name' => array(
  392.      *                                 'type' => 'text',
  393.      *                                 'length' => 12
  394.      *                             ),
  395.      *                             'password' => array(
  396.      *                                 'type' => 'text',
  397.      *                                 'length' => 12
  398.      *                             )
  399.      *                         );
  400.      * @param array $options  An associative array of table options:
  401.      *                           array(
  402.      *                               'comment' => 'Foo',
  403.      *                               'temporary' => true|false,
  404.      *                           );
  405.      * @return mixed MDB2_OK on success, a MDB2 error on failure
  406.      * @access public
  407.      */
  408.     function createTable($name$fields$options = array())
  409.     {
  410.         $db $this->getDBInstance();
  411.         if (MDB2::isError($db)) {
  412.             return $db;
  413.         }
  414.         $db->beginNestedTransaction();
  415.         $result = parent::createTable($name$fields$options);
  416.         if (!MDB2::isError($result)) {
  417.             foreach ($fields as $field_name => $field{
  418.                 if (!empty($field['autoincrement'])) {
  419.                     $result $this->_makeAutoincrement($field_name$name);
  420.                 }
  421.             }
  422.         }
  423.         $db->completeNestedTransaction();
  424.         return $result;
  425.     }
  426.  
  427.     // }}}
  428.     // {{{ dropTable()
  429.  
  430.     /**
  431.      * drop an existing table
  432.      *
  433.      * @param string $name name of the table that should be dropped
  434.      * @return mixed MDB2_OK on success, a MDB2 error on failure
  435.      * @access public
  436.      */
  437.     function dropTable($name)
  438.     {
  439.         $db $this->getDBInstance();
  440.         if (MDB2::isError($db)) {
  441.             return $db;
  442.         }
  443.         $db->beginNestedTransaction();
  444.         $result $this->_dropAutoincrement($name);
  445.         if (!MDB2::isError($result)) {
  446.             $result = parent::dropTable($name);
  447.             if (MDB2::isError($result)) {
  448.                 return $result;
  449.             }
  450.         }
  451.         $db->completeNestedTransaction();
  452.         return $result;
  453.     }
  454.  
  455.     // }}}
  456.     // {{{ truncateTable()
  457.  
  458.     /**
  459.      * Truncate an existing table (if the TRUNCATE TABLE syntax is not supported,
  460.      * it falls back to a DELETE FROM TABLE query)
  461.      *
  462.      * @param string $name name of the table that should be truncated
  463.      * @return mixed MDB2_OK on success, a MDB2 error on failure
  464.      * @access public
  465.      */
  466.     function truncateTable($name)
  467.     {
  468.         $db $this->getDBInstance();
  469.         if (MDB2::isError($db)) {
  470.             return $db;
  471.         }
  472.  
  473.         $name $db->quoteIdentifier($nametrue);
  474.         $result $db->exec("TRUNCATE TABLE $name");
  475.         if (MDB2::isError($result)) {
  476.             return $result;
  477.         }
  478.         return MDB2_OK;
  479.     }
  480.  
  481.     // }}}
  482.     // {{{ vacuum()
  483.  
  484.     /**
  485.      * Optimize (vacuum) all the tables in the db (or only the specified table)
  486.      * and optionally run ANALYZE.
  487.      *
  488.      * @param string $table table name (all the tables if empty)
  489.      * @param array  $options an array with driver-specific options:
  490.      *                - timeout [int] (in seconds) [mssql-only]
  491.      *                - analyze [boolean] [pgsql and mysql]
  492.      *                - full [boolean] [pgsql-only]
  493.      *                - freeze [boolean] [pgsql-only]
  494.      *
  495.      * @return mixed MDB2_OK success, a MDB2 error on failure
  496.      * @access public
  497.      */
  498.     function vacuum($table = null$options = array())
  499.     {
  500.         // not needed in Oracle
  501.         return MDB2_OK;
  502.     }
  503.  
  504.     // }}}
  505.     // {{{ alterTable()
  506.  
  507.     /**
  508.      * alter an existing table
  509.      *
  510.      * @param string $name         name of the table that is intended to be changed.
  511.      * @param array $changes     associative array that contains the details of each type
  512.      *                              of change that is intended to be performed. The types of
  513.      *                              changes that are currently supported are defined as follows:
  514.      *
  515.      *                              name
  516.      *
  517.      *                                 New name for the table.
  518.      *
  519.      *                             add
  520.      *
  521.      *                                 Associative array with the names of fields to be added as
  522.      *                                  indexes of the array. The value of each entry of the array
  523.      *                                  should be set to another associative array with the properties
  524.      *                                  of the fields to be added. The properties of the fields should
  525.      *                                  be the same as defined by the MDB2 parser.
  526.      *
  527.      *
  528.      *                             remove
  529.      *
  530.      *                                 Associative array with the names of fields to be removed as indexes
  531.      *                                  of the array. Currently the values assigned to each entry are ignored.
  532.      *                                  An empty array should be used for future compatibility.
  533.      *
  534.      *                             rename
  535.      *
  536.      *                                 Associative array with the names of fields to be renamed as indexes
  537.      *                                  of the array. The value of each entry of the array should be set to
  538.      *                                  another associative array with the entry named name with the new
  539.      *                                  field name and the entry named Declaration that is expected to contain
  540.      *                                  the portion of the field declaration already in DBMS specific SQL code
  541.      *                                  as it is used in the CREATE TABLE statement.
  542.      *
  543.      *                             change
  544.      *
  545.      *                                 Associative array with the names of the fields to be changed as indexes
  546.      *                                  of the array. Keep in mind that if it is intended to change either the
  547.      *                                  name of a field and any other properties, the change array entries
  548.      *                                  should have the new names of the fields as array indexes.
  549.      *
  550.      *                                 The value of each entry of the array should be set to another associative
  551.      *                                  array with the properties of the fields to that are meant to be changed as
  552.      *                                  array entries. These entries should be assigned to the new values of the
  553.      *                                  respective properties. The properties of the fields should be the same
  554.      *                                  as defined by the MDB2 parser.
  555.      *
  556.      *                             Example
  557.      *                                 array(
  558.      *                                     'name' => 'userlist',
  559.      *                                     'add' => array(
  560.      *                                         'quota' => array(
  561.      *                                             'type' => 'integer',
  562.      *                                             'unsigned' => 1
  563.      *                                         )
  564.      *                                     ),
  565.      *                                     'remove' => array(
  566.      *                                         'file_limit' => array(),
  567.      *                                         'time_limit' => array()
  568.      *                                     ),
  569.      *                                     'change' => array(
  570.      *                                         'name' => array(
  571.      *                                             'length' => '20',
  572.      *                                             'definition' => array(
  573.      *                                                 'type' => 'text',
  574.      *                                                 'length' => 20,
  575.      *                                             ),
  576.      *                                         )
  577.      *                                     ),
  578.      *                                     'rename' => array(
  579.      *                                         'sex' => array(
  580.      *                                             'name' => 'gender',
  581.      *                                             'definition' => array(
  582.      *                                                 'type' => 'text',
  583.      *                                                 'length' => 1,
  584.      *                                                 'default' => 'M',
  585.      *                                             ),
  586.      *                                         )
  587.      *                                     )
  588.      *                                 )
  589.      *
  590.      * @param boolean $check     indicates whether the function should just check if the DBMS driver
  591.      *                              can perform the requested table alterations if the value is true or
  592.      *                              actually perform them otherwise.
  593.      * @access public
  594.      *
  595.      * @return mixed MDB2_OK on success, a MDB2 error on failure
  596.      */
  597.     function alterTable($name$changes$check)
  598.     {
  599.         $db $this->getDBInstance();
  600.         if (MDB2::isError($db)) {
  601.             return $db;
  602.         }
  603.  
  604.         foreach ($changes as $change_name => $change{
  605.             switch ($change_name{
  606.             case 'add':
  607.             case 'remove':
  608.             case 'change':
  609.             case 'name':
  610.             case 'rename':
  611.                 break;
  612.             default:
  613.                 return $db->raiseError(MDB2_ERROR_CANNOT_ALTERnullnull,
  614.                     'change type "'.$change_name.'" not yet supported'__FUNCTION__);
  615.             }
  616.         }
  617.  
  618.         if ($check{
  619.             return MDB2_OK;
  620.         }
  621.  
  622.         $name $db->quoteIdentifier($nametrue);
  623.  
  624.         if (!empty($changes['add']&& is_array($changes['add'])) {
  625.             $fields = array();
  626.             foreach ($changes['add'as $field_name => $field{
  627.                 $fields[$db->getDeclaration($field['type']$field_name$field);
  628.             }
  629.             $result $db->exec("ALTER TABLE $name ADD (". implode(', '$fields).')');
  630.             if (MDB2::isError($result)) {
  631.                 return $result;
  632.             }
  633.         }
  634.  
  635.         if (!empty($changes['change']&& is_array($changes['change'])) {
  636.             $fields = array();
  637.             foreach ($changes['change'as $field_name => $field{
  638.                 //fix error "column to be modified to NOT NULL is already NOT NULL" 
  639.                 if (!array_key_exists('notnull'$field)) {
  640.                     unset($field['definition']['notnull']);
  641.                 }
  642.                 $fields[$db->getDeclaration($field['definition']['type']$field_name$field['definition']);
  643.             }
  644.             $result $db->exec("ALTER TABLE $name MODIFY (". implode(', '$fields).')');
  645.             if (MDB2::isError($result)) {
  646.                 return $result;
  647.             }
  648.         }
  649.  
  650.         if (!empty($changes['rename']&& is_array($changes['rename'])) {
  651.             foreach ($changes['rename'as $field_name => $field{
  652.                 $field_name $db->quoteIdentifier($field_nametrue);
  653.                 $query = "ALTER TABLE $name RENAME COLUMN $field_name TO ".$db->quoteIdentifier($field['name']);
  654.                 $result $db->exec($query);
  655.                 if (MDB2::isError($result)) {
  656.                     return $result;
  657.                 }
  658.             }
  659.         }
  660.  
  661.         if (!empty($changes['remove']&& is_array($changes['remove'])) {
  662.             $fields = array();
  663.             foreach ($changes['remove'as $field_name => $field{
  664.                 $fields[$db->quoteIdentifier($field_nametrue);
  665.             }
  666.             $result $db->exec("ALTER TABLE $name DROP COLUMN ". implode(', '$fields));
  667.             if (MDB2::isError($result)) {
  668.                 return $result;
  669.             }
  670.         }
  671.  
  672.         if (!empty($changes['name'])) {
  673.             $change_name $db->quoteIdentifier($changes['name']true);
  674.             $result $db->exec("ALTER TABLE $name RENAME TO ".$change_name);
  675.             if (MDB2::isError($result)) {
  676.                 return $result;
  677.             }
  678.         }
  679.  
  680.         return MDB2_OK;
  681.     }
  682.  
  683.     // }}}
  684.     // {{{ _fetchCol()
  685.  
  686.     /**
  687.      * Utility method to fetch and format a column from a resultset
  688.      *
  689.      * @param resource $result 
  690.      * @param boolean $fixname (used when listing indices or constraints)
  691.      * @return mixed array of names on success, a MDB2 error on failure
  692.      * @access private
  693.      */
  694.     function _fetchCol($result$fixname = false)
  695.     {
  696.         if (MDB2::isError($result)) {
  697.             return $result;
  698.         }
  699.         $col $result->fetchCol();
  700.         if (MDB2::isError($col)) {
  701.             return $col;
  702.         }
  703.         $result->free();
  704.         
  705.         $db $this->getDBInstance();
  706.         if (MDB2::isError($db)) {
  707.             return $db;
  708.         }
  709.         
  710.         if ($fixname{
  711.             foreach ($col as $k => $v{
  712.                 $col[$k$this->_fixIndexName($v);
  713.             }
  714.         }
  715.         
  716.         if ($db->options['portability'MDB2_PORTABILITY_FIX_CASE
  717.             && $db->options['field_case'== CASE_LOWER
  718.         {
  719.             $col array_map(($db->options['field_case'== CASE_LOWER ? 'strtolower' 'strtoupper')$col);
  720.         }
  721.         return $col;
  722.     }
  723.  
  724.     // }}}
  725.     // {{{ listDatabases()
  726.  
  727.     /**
  728.      * list all databases
  729.      *
  730.      * @return mixed array of database names on success, a MDB2 error on failure
  731.      * @access public
  732.      */
  733.     function listDatabases()
  734.     {
  735.         $db $this->getDBInstance();
  736.         if (MDB2::isError($db)) {
  737.             return $db;
  738.         }
  739.  
  740.         if (!$db->options['emulate_database']{
  741.             return $db->raiseError(MDB2_ERROR_UNSUPPORTEDnullnull,
  742.                 'database listing is only supported if the "emulate_database" option is enabled'__FUNCTION__);
  743.         }
  744.  
  745.         if ($db->options['database_name_prefix']{
  746.             $query 'SELECT SUBSTR(username, ';
  747.             $query.= (strlen($db->options['database_name_prefix'])+1);
  748.             $query.= ") FROM sys.dba_users WHERE username LIKE '";
  749.             $query.= $db->options['database_name_prefix']."%'";
  750.         else {
  751.             $query 'SELECT username FROM sys.dba_users';
  752.         }
  753.         $result $db->standaloneQuery($queryarray('text')false);
  754.         return $this->_fetchCol($result);
  755.     }
  756.  
  757.     // }}}
  758.     // {{{ listUsers()
  759.  
  760.     /**
  761.      * list all users
  762.      *
  763.      * @return mixed array of user names on success, a MDB2 error on failure
  764.      * @access public
  765.      */
  766.     function listUsers()
  767.     {
  768.         $db $this->getDBInstance();
  769.         if (MDB2::isError($db)) {
  770.             return $db;
  771.         }
  772.  
  773.         if ($db->options['emulate_database'&& $db->options['database_name_prefix']{
  774.             $query 'SELECT SUBSTR(username, ';
  775.             $query.= (strlen($db->options['database_name_prefix'])+1);
  776.             $query.= ") FROM sys.dba_users WHERE username NOT LIKE '";
  777.             $query.= $db->options['database_name_prefix']."%'";
  778.         else {
  779.             $query 'SELECT username FROM sys.dba_users';
  780.         }
  781.         return $db->queryCol($query);
  782.     }
  783.  
  784.     // }}}
  785.     // {{{ listViews()
  786.  
  787.     /**
  788.      * list all views in the current database
  789.      *
  790.      * @param string owner, the current is default
  791.      * @return mixed array of view names on success, a MDB2 error on failure
  792.      * @access public
  793.      */
  794.     function listViews($owner = null)
  795.     {
  796.         $db $this->getDBInstance();
  797.         if (MDB2::isError($db)) {
  798.             return $db;
  799.         }
  800.         
  801.         if (empty($owner)) {
  802.             $owner $db->dsn['username'];
  803.         }
  804.  
  805.         $query 'SELECT view_name
  806.                     FROM sys.all_views
  807.                    WHERE owner=? OR owner=?';
  808.         $stmt $db->prepare($query);
  809.         if (MDB2::isError($stmt)) {
  810.             return $stmt;
  811.         }
  812.         $result $stmt->execute(array($ownerstrtoupper($owner)));
  813.         return $this->_fetchCol($result);
  814.     }
  815.  
  816.     // }}}
  817.     // {{{ listFunctions()
  818.  
  819.     /**
  820.      * list all functions in the current database
  821.      *
  822.      * @param string owner, the current is default
  823.      * @return mixed array of function names on success, a MDB2 error on failure
  824.      * @access public
  825.      */
  826.     function listFunctions($owner = null)
  827.     {
  828.         $db $this->getDBInstance();
  829.         if (MDB2::isError($db)) {
  830.             return $db;
  831.         }
  832.  
  833.         if (empty($owner)) {
  834.             $owner $db->dsn['username'];
  835.         }
  836.  
  837.         $query "SELECT name
  838.                     FROM sys.all_source
  839.                    WHERE line = 1
  840.                      AND type = 'FUNCTION'
  841.                      AND (owner=? OR owner=?)";
  842.         $stmt $db->prepare($query);
  843.         if (MDB2::isError($stmt)) {
  844.             return $stmt;
  845.         }
  846.         $result $stmt->execute(array($ownerstrtoupper($owner)));
  847.         return $this->_fetchCol($result);
  848.     }
  849.  
  850.     // }}}
  851.     // {{{ listTableTriggers()
  852.  
  853.     /**
  854.      * list all triggers in the database that reference a given table
  855.      *
  856.      * @param string table for which all referenced triggers should be found
  857.      * @return mixed array of trigger names on success, a MDB2 error on failure
  858.      * @access public
  859.      */
  860.     function listTableTriggers($table = null)
  861.     {
  862.         $db $this->getDBInstance();
  863.         if (MDB2::isError($db)) {
  864.             return $db;
  865.         }
  866.  
  867.         if (empty($owner)) {
  868.             $owner $db->dsn['username'];
  869.         }
  870.  
  871.         $query "SELECT trigger_name
  872.                     FROM sys.all_triggers
  873.                    WHERE (table_name=? OR table_name=?)
  874.                      AND (owner=? OR owner=?)";
  875.         $stmt $db->prepare($query);
  876.         if (MDB2::isError($stmt)) {
  877.             return $stmt;
  878.         }
  879.         $args = array(
  880.             $table,
  881.             strtoupper($table),
  882.             $owner,
  883.             strtoupper($owner),
  884.         );
  885.         $result $stmt->execute($args);
  886.         return $this->_fetchCol($result);
  887.     }
  888.  
  889.     // }}}
  890.     // {{{ listTables()
  891.  
  892.     /**
  893.      * list all tables in the database
  894.      *
  895.      * @param string owner, the current is default
  896.      * @return mixed array of table names on success, a MDB2 error on failure
  897.      * @access public
  898.      */
  899.     function listTables($owner = null)
  900.     {
  901.         $db $this->getDBInstance();
  902.         if (MDB2::isError($db)) {
  903.             return $db;
  904.         }
  905.         
  906.         if (empty($owner)) {
  907.             $owner $db->dsn['username'];
  908.         }
  909.  
  910.         $query 'SELECT table_name
  911.                     FROM sys.all_tables
  912.                    WHERE owner=? OR owner=?';
  913.         $stmt $db->prepare($query);
  914.         if (MDB2::isError($stmt)) {
  915.             return $stmt;
  916.         }
  917.         $result $stmt->execute(array($ownerstrtoupper($owner)));
  918.         return $this->_fetchCol($result);
  919.     }
  920.  
  921.     // }}}
  922.     // {{{ listTableFields()
  923.  
  924.     /**
  925.      * list all fields in a table in the current database
  926.      *
  927.      * @param string $table name of table that should be used in method
  928.      * @return mixed array of field names on success, a MDB2 error on failure
  929.      * @access public
  930.      */
  931.     function listTableFields($table)
  932.     {
  933.         $db $this->getDBInstance();
  934.         if (MDB2::isError($db)) {
  935.             return $db;
  936.         }
  937.         
  938.         list($owner$table$this->splitTableSchema($table);
  939.         if (empty($owner)) {
  940.             $owner $db->dsn['username'];
  941.         }
  942.  
  943.         $query 'SELECT column_name
  944.                     FROM all_tab_columns
  945.                    WHERE (table_name=? OR table_name=?)
  946.                      AND (owner=? OR owner=?)
  947.                 ORDER BY column_id';
  948.         $stmt $db->prepare($query);
  949.         if (MDB2::isError($stmt)) {
  950.             return $stmt;
  951.         }
  952.         $args = array(
  953.             $table,
  954.             strtoupper($table),
  955.             $owner,
  956.             strtoupper($owner),
  957.         );
  958.         $result $stmt->execute($args);
  959.         return $this->_fetchCol($result);
  960.     }
  961.  
  962.     // }}}
  963.     // {{{ listTableIndexes()
  964.  
  965.     /**
  966.      * list all indexes in a table
  967.      *
  968.      * @param string $table name of table that should be used in method
  969.      * @return mixed array of index names on success, a MDB2 error on failure
  970.      * @access public
  971.      */
  972.     function listTableIndexes($table)
  973.     {
  974.         $db $this->getDBInstance();
  975.         if (MDB2::isError($db)) {
  976.             return $db;
  977.         }
  978.         
  979.         list($owner$table$this->splitTableSchema($table);
  980.         if (empty($owner)) {
  981.             $owner $db->dsn['username'];
  982.         }
  983.         
  984.         $query 'SELECT i.index_name name
  985.                     FROM all_indexes i
  986.                LEFT JOIN all_constraints c
  987.                       ON c.index_name = i.index_name
  988.                      AND c.owner = i.owner
  989.                      AND c.table_name = i.table_name
  990.                    WHERE (i.table_name=? OR i.table_name=?)
  991.                      AND (i.owner=? OR i.owner=?)
  992.                      AND c.index_name IS NULL
  993.                      AND i.generated=' .$db->quote('N''text');
  994.         $stmt $db->prepare($query);
  995.         if (MDB2::isError($stmt)) {
  996.             return $stmt;
  997.         }
  998.         $args = array(
  999.             $table,
  1000.             strtoupper($table),
  1001.             $owner,
  1002.             strtoupper($owner),
  1003.         );
  1004.         $result $stmt->execute($args);
  1005.         return $this->_fetchCol($resulttrue);
  1006.     }
  1007.  
  1008.     // }}}
  1009.     // {{{ createConstraint()
  1010.  
  1011.     /**
  1012.      * create a constraint on a table
  1013.      *
  1014.      * @param string    $table        name of the table on which the constraint is to be created
  1015.      * @param string    $name         name of the constraint to be created
  1016.      * @param array     $definition   associative array that defines properties of the constraint to be created.
  1017.      *                                 Currently, only one property named FIELDS is supported. This property
  1018.      *                                 is also an associative with the names of the constraint fields as array
  1019.      *                                 constraints. Each entry of this array is set to another type of associative
  1020.      *                                 array that specifies properties of the constraint that are specific to
  1021.      *                                 each field.
  1022.      *
  1023.      *                                 Example
  1024.      *                                    array(
  1025.      *                                        'fields' => array(
  1026.      *                                            'user_name' => array(),
  1027.      *                                            'last_login' => array()
  1028.      *                                        )
  1029.      *                                    )
  1030.      * @return mixed MDB2_OK on success, a MDB2 error on failure
  1031.      * @access public
  1032.      */
  1033.     function createConstraint($table$name$definition)
  1034.     {
  1035.         $result = parent::createConstraint($table$name$definition);
  1036.         if (MDB2::isError($result)) {
  1037.             return $result;
  1038.         }
  1039.         if (!empty($definition['foreign'])) {
  1040.             return $this->_createFKTriggers($tablearray($name => $definition));
  1041.         }
  1042.         return MDB2_OK;
  1043.     }
  1044.  
  1045.     // }}}
  1046.     // {{{ dropConstraint()
  1047.  
  1048.     /**
  1049.      * drop existing constraint
  1050.      *
  1051.      * @param string    $table        name of table that should be used in method
  1052.      * @param string    $name         name of the constraint to be dropped
  1053.      * @param string    $primary      hint if the constraint is primary
  1054.      * @return mixed MDB2_OK on success, a MDB2 error on failure
  1055.      * @access public
  1056.      */
  1057.     function dropConstraint($table$name$primary = false)
  1058.     {
  1059.         $db $this->getDBInstance();
  1060.         if (MDB2::isError($db)) {
  1061.             return $db;
  1062.         }
  1063.  
  1064.         //is it a FK constraint? If so, also delete the associated triggers
  1065.         $db->loadModule('Reverse'nulltrue);
  1066.         $definition $db->reverse->getTableConstraintDefinition($table$name);
  1067.         if (!MDB2::isError($definition&& !empty($definition['foreign'])) {
  1068.             //first drop the FK enforcing triggers
  1069.             $result $this->_dropFKTriggers($table$name$definition['references']['table']);
  1070.             if (MDB2::isError($result)) {
  1071.                 return $result;
  1072.             }
  1073.         }
  1074.  
  1075.         return parent::dropConstraint($table$name$primary);
  1076.     }
  1077.  
  1078.     // }}}
  1079.     // {{{ _createFKTriggers()
  1080.  
  1081.     /**
  1082.      * Create triggers to enforce the FOREIGN KEY constraint on the table
  1083.      *
  1084.      * @param string $table        table name
  1085.      * @param array  $foreign_keys FOREIGN KEY definitions
  1086.      *
  1087.      * @return mixed MDB2_OK on success, a MDB2 error on failure
  1088.      * @access private
  1089.      */
  1090.     function _createFKTriggers($table$foreign_keys)
  1091.     {
  1092.         $db $this->getDBInstance();
  1093.         if (MDB2::isError($db)) {
  1094.             return $db;
  1095.         }
  1096.         // create triggers to enforce FOREIGN KEY constraints
  1097.         if ($db->supports('triggers'&& !empty($foreign_keys)) {
  1098.             $table $db->quoteIdentifier($tabletrue);
  1099.             foreach ($foreign_keys as $fkname => $fkdef{
  1100.                 if (empty($fkdef)) {
  1101.                     continue;
  1102.                 }
  1103.                 $fkdef['onupdate'= empty($fkdef['onupdate']$db->options['default_fk_action_onupdate'strtoupper($fkdef['onupdate']);
  1104.                 if ('RESTRICT' == $fkdef['onupdate'|| 'NO ACTION' == $fkdef['onupdate']{
  1105.                     // already handled by default
  1106.                     continue;
  1107.                 }
  1108.  
  1109.                 $trigger_name substr(strtolower($fkname.'_pk_upd_trg')0$db->options['max_identifiers_length']);
  1110.                 $table_fields array_keys($fkdef['fields']);
  1111.                 $referenced_fields array_keys($fkdef['references']['fields']);
  1112.  
  1113.                 //create the ON UPDATE trigger on the primary table
  1114.                 $restrict_action ' IF (SELECT ';
  1115.                 $aliased_fields = array();
  1116.                 foreach ($table_fields as $field{
  1117.                     $aliased_fields[$table .'.'.$field .' AS '.$field;
  1118.                 }
  1119.                 $restrict_action .= implode(','$aliased_fields)
  1120.                        .' FROM '.$table
  1121.                        .' WHERE ';
  1122.                 $conditions  = array();
  1123.                 $new_values  = array();
  1124.                 $null_values = array();
  1125.                 for ($i=0; $i<count($table_fields)$i++{
  1126.                     $conditions[]  $table_fields[$i.' = :OLD.'.$referenced_fields[$i];
  1127.                     $new_values[]  $table_fields[$i.' = :NEW.'.$referenced_fields[$i];
  1128.                     $null_values[$table_fields[$i.' = NULL';
  1129.                 }
  1130.  
  1131.                 $cascade_action 'UPDATE '.$table.' SET '.implode(', '$new_values.' WHERE '.implode(' AND '$conditions)';';
  1132.                 $setnull_action 'UPDATE '.$table.' SET '.implode(', '$null_values).' WHERE '.implode(' AND '$conditions)';';
  1133.  
  1134.                 if ('SET DEFAULT' == $fkdef['onupdate'|| 'SET DEFAULT' == $fkdef['ondelete']{
  1135.                     $db->loadModule('Reverse'nulltrue);
  1136.                     $default_values = array();
  1137.                     foreach ($table_fields as $table_field{
  1138.                         $field_definition $db->reverse->getTableFieldDefinition($table$field);
  1139.                         if (MDB2::isError($field_definition)) {
  1140.                             return $field_definition;
  1141.                         }
  1142.                         $default_values[$table_field .' = '$field_definition[0]['default'];
  1143.                     }
  1144.                     $setdefault_action 'UPDATE '.$table.' SET '.implode(', '$default_values).' WHERE '.implode(' AND '$conditions)';';
  1145.                 }
  1146.  
  1147.                 $query 'CREATE TRIGGER %s'
  1148.                         .' %s ON '.$fkdef['references']['table']
  1149.                         .' FOR EACH ROW '
  1150.                         .' BEGIN ';
  1151.  
  1152.                 if ('CASCADE' == $fkdef['onupdate']{
  1153.                     $sql_update sprintf($query$trigger_name'BEFORE UPDATE',  'update'$cascade_action;
  1154.                 elseif ('SET NULL' == $fkdef['onupdate']{
  1155.                     $sql_update sprintf($query$trigger_name'BEFORE UPDATE''update'$setnull_action;
  1156.                 elseif ('SET DEFAULT' == $fkdef['onupdate']{
  1157.                     $sql_update sprintf($query$trigger_name'BEFORE UPDATE''update'$setdefault_action;
  1158.                 }
  1159.                 $sql_update .= ' END;';
  1160.                 $result $db->exec($sql_update);
  1161.                 if (MDB2::isError($result)) {
  1162.                     if ($result->getCode(=== MDB2_ERROR_ALREADY_EXISTS{
  1163.                         return MDB2_OK;
  1164.                     }
  1165.                     return $result;
  1166.                 }
  1167.             }
  1168.         }
  1169.         return MDB2_OK;
  1170.     }
  1171.  
  1172.     // }}}
  1173.     // {{{ _dropFKTriggers()
  1174.  
  1175.     /**
  1176.      * Drop the triggers created to enforce the FOREIGN KEY constraint on the table
  1177.      *
  1178.      * @param string $table            table name
  1179.      * @param string $fkname           FOREIGN KEY constraint name
  1180.      * @param string $referenced_table referenced table name
  1181.      *
  1182.      * @return mixed MDB2_OK on success, a MDB2 error on failure
  1183.      * @access private
  1184.      */
  1185.     function _dropFKTriggers($table$fkname$referenced_table)
  1186.     {
  1187.         $db $this->getDBInstance();
  1188.         if (MDB2::isError($db)) {
  1189.             return $db;
  1190.         }
  1191.  
  1192.         $triggers  $this->listTableTriggers($table);
  1193.         $triggers2 $this->listTableTriggers($referenced_table);
  1194.         if (!MDB2::isError($triggers2&& !MDB2::isError($triggers)) {
  1195.             $triggers array_merge($triggers$triggers2);
  1196.             $trigger_name substr(strtolower($fkname.'_pk_upd_trg')0$db->options['max_identifiers_length']);
  1197.             $pattern '/^'.$trigger_name.'$/i';
  1198.             foreach ($triggers as $trigger{
  1199.                 if (preg_match($pattern$trigger)) {
  1200.                     $result $db->exec('DROP TRIGGER '.$trigger);
  1201.                     if (MDB2::isError($result)) {
  1202.                         return $result;
  1203.                     }
  1204.                 }
  1205.             }
  1206.         }
  1207.         return MDB2_OK;
  1208.     }
  1209.  
  1210.     // }}}
  1211.     // {{{ listTableConstraints()
  1212.  
  1213.     /**
  1214.      * list all constraints in a table
  1215.      *
  1216.      * @param string $table name of table that should be used in method
  1217.      * @return mixed array of constraint names on success, a MDB2 error on failure
  1218.      * @access public
  1219.      */
  1220.     function listTableConstraints($table)
  1221.     {
  1222.         $db $this->getDBInstance();
  1223.         if (MDB2::isError($db)) {
  1224.             return $db;
  1225.         }
  1226.  
  1227.         list($owner$table$this->splitTableSchema($table);
  1228.         if (empty($owner)) {
  1229.             $owner $db->dsn['username'];
  1230.         }
  1231.  
  1232.         $query 'SELECT constraint_name
  1233.                     FROM all_constraints
  1234.                    WHERE (table_name=? OR table_name=?)
  1235.                      AND (owner=? OR owner=?)';
  1236.         $stmt $db->prepare($query);
  1237.         if (MDB2::isError($stmt)) {
  1238.             return $stmt;
  1239.         }
  1240.         $args = array(
  1241.             $table,
  1242.             strtoupper($table),
  1243.             $owner,
  1244.             strtoupper($owner),
  1245.         );
  1246.         $result $stmt->execute($args);
  1247.         return $this->_fetchCol($resulttrue);
  1248.     }
  1249.  
  1250.     // }}}
  1251.     // {{{ createSequence()
  1252.  
  1253.     /**
  1254.      * create sequence
  1255.      *
  1256.      * @param object $db database object that is extended by this class
  1257.      * @param string $seq_name name of the sequence to be created
  1258.      * @param string $start start value of the sequence; default is 1
  1259.      * @return mixed MDB2_OK on success, a MDB2 error on failure
  1260.      * @access public
  1261.      */
  1262.     function createSequence($seq_name$start = 1)
  1263.     {
  1264.         $db $this->getDBInstance();
  1265.         if (MDB2::isError($db)) {
  1266.             return $db;
  1267.         }
  1268.  
  1269.         $sequence_name $db->quoteIdentifier($db->getSequenceName($seq_name)true);
  1270.         $query = "CREATE SEQUENCE $sequence_name START WITH $start INCREMENT BY 1 NOCACHE";
  1271.         $query.= ($start < 1 ? " MINVALUE $start" : '');
  1272.         $result $db->exec($query);
  1273.         if (MDB2::isError($result)) {
  1274.             return $result;
  1275.         }
  1276.         return MDB2_OK;
  1277.     }
  1278.  
  1279.     // }}}
  1280.     // {{{ dropSequence()
  1281.  
  1282.     /**
  1283.      * drop existing sequence
  1284.      *
  1285.      * @param object $db database object that is extended by this class
  1286.      * @param string $seq_name name of the sequence to be dropped
  1287.      * @return mixed MDB2_OK on success, a MDB2 error on failure
  1288.      * @access public
  1289.      */
  1290.     function dropSequence($seq_name)
  1291.     {
  1292.         $db $this->getDBInstance();
  1293.         if (MDB2::isError($db)) {
  1294.             return $db;
  1295.         }
  1296.  
  1297.         $sequence_name $db->quoteIdentifier($db->getSequenceName($seq_name)true);
  1298.         $result $db->exec("DROP SEQUENCE $sequence_name");
  1299.         if (MDB2::isError($result)) {
  1300.             return $result;
  1301.         }
  1302.         return MDB2_OK;
  1303.     }
  1304.  
  1305.     // }}}
  1306.     // {{{ listSequences()
  1307.  
  1308.     /**
  1309.      * list all sequences in the current database
  1310.      *
  1311.      * @param string owner, the current is default
  1312.      * @return mixed array of sequence names on success, a MDB2 error on failure
  1313.      * @access public
  1314.      */
  1315.     function listSequences($owner = null)
  1316.     {
  1317.         $db $this->getDBInstance();
  1318.         if (MDB2::isError($db)) {
  1319.             return $db;
  1320.         }
  1321.  
  1322.         if (empty($owner)) {
  1323.             $owner $db->dsn['username'];
  1324.         }
  1325.  
  1326.         $query 'SELECT sequence_name
  1327.                     FROM sys.all_sequences
  1328.                    WHERE (sequence_owner=? OR sequence_owner=?)';
  1329.         $stmt $db->prepare($query);
  1330.         if (MDB2::isError($stmt)) {
  1331.             return $stmt;
  1332.         }
  1333.         $result $stmt->execute(array($ownerstrtoupper($owner)));
  1334.         if (MDB2::isError($result)) {
  1335.             return $result;
  1336.         }
  1337.         $col $result->fetchCol();
  1338.         if (MDB2::isError($col)) {
  1339.             return $col;
  1340.         }
  1341.         $result->free();
  1342.         
  1343.         foreach ($col as $k => $v{
  1344.             $col[$k$this->_fixSequenceName($v);
  1345.         }
  1346.  
  1347.         if ($db->options['portability'MDB2_PORTABILITY_FIX_CASE
  1348.             && $db->options['field_case'== CASE_LOWER
  1349.         {
  1350.             $col array_map(($db->options['field_case'== CASE_LOWER ? 'strtolower' 'strtoupper')$col);
  1351.         }
  1352.         return $col;
  1353.     }
  1354. }
  1355. ?>

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