Source for file Firebird.php

Documentation is available at Firebird.php

  1. <?php
  2. /*
  3.  *  $Id: Firebird.php 1753 2007-06-19 11:10:13Z zYne $
  4.  *
  5.  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  6.  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  7.  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  8.  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  9.  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  10.  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  11.  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  12.  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  13.  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  14.  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  15.  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  16.  *
  17.  * This software consists of voluntary contributions made by many individuals
  18.  * and is licensed under the LGPL. For more information, see
  19.  * <http://www.phpdoctrine.com>.
  20.  */
  21. Doctrine::autoload('Doctrine_Export');
  22. /**
  23.  * Doctrine_Export_Sqlite
  24.  *
  25.  * @package     Doctrine
  26.  * @author      Konsta Vesterinen <kvesteri@cc.hut.fi>
  27.  * @author      Lukas Smith <smith@pooteeweet.org> (PEAR MDB2 library)
  28.  * @author      Lorenzo Alberton <l.alberton@quipo.it> (PEAR MDB2 Interbase driver)
  29.  * @license     http://www.opensource.org/licenses/lgpl-license.php LGPL
  30.  * @category    Object Relational Mapping
  31.  * @link        www.phpdoctrine.com
  32.  * @since       1.0
  33.  * @version     $Revision: 1753 $
  34.  */
  35. {
  36.     /**
  37.      * create a new database
  38.      *
  39.      * @param string $name  name of the database that should be created
  40.      * @return void 
  41.      */
  42.     public function createDatabase($name)
  43.     {
  44.         throw new Doctrine_Export_Exception(
  45.                 'PHP Interbase API does not support direct queries. You have to ' .
  46.                 'create the db manually by using isql command or a similar program');
  47.     }
  48.     /**
  49.      * drop an existing database
  50.      *
  51.      * @param string $name  name of the database that should be dropped
  52.      * @return void 
  53.      */
  54.     public  function dropDatabase($name)
  55.     {
  56.         throw new Doctrine_Export_Exception(
  57.                 'PHP Interbase API does not support direct queries. You have ' .
  58.                 'to drop the db manually by using isql command or a similar program');
  59.     }
  60.     /**
  61.      * add an autoincrement sequence + trigger
  62.      *
  63.      * @param string $name  name of the PK field
  64.      * @param string $table name of the table
  65.      * @param string $start start value for the sequence
  66.      * @return void 
  67.      */
  68.     public function _makeAutoincrement($name$table$start null)
  69.     {
  70.         if (is_null($start)) {
  71.             $this->conn->beginTransaction();
  72.             $query 'SELECT MAX(' $this->conn->quoteIdentifier($nametrue') FROM ' $this->conn->quoteIdentifier($tabletrue);
  73.             $start $this->conn->fetchOne($query'integer');
  74.  
  75.             ++$start;
  76.             $result $this->createSequence($table$start);
  77.             $this->conn->commit();
  78.         else {
  79.             $result $this->createSequence($table$start);
  80.         }
  81.  
  82.         $sequence_name $this->conn->formatter->getSequenceName($table);
  83.         $trigger_name  $this->conn->quoteIdentifier($table '_AUTOINCREMENT_PK'true);
  84.  
  85.         $table $this->conn->quoteIdentifier($tabletrue);
  86.         $name  $this->conn->quoteIdentifier($name,  true);
  87.  
  88.         $triggerSql 'CREATE TRIGGER ' $trigger_name ' FOR ' $table '
  89.                         ACTIVE BEFORE INSERT POSITION 0
  90.                         AS
  91.                         BEGIN
  92.                         IF (NEW.' $name ' IS NULL OR NEW.' $name ' = 0) THEN
  93.                             NEW.' $name ' = GEN_ID('.$sequence_name.', 1);
  94.                         END';
  95.         $result $this->conn->exec($triggerSql);
  96.  
  97.         // TODO ? $this->_silentCommit();
  98.  
  99.         return $result;
  100.     }
  101.     /**
  102.      * drop an existing autoincrement sequence + trigger
  103.      *
  104.      * @param string $table name of the table
  105.      * @return void 
  106.      */
  107.     public function _dropAutoincrement($table)
  108.     {
  109.  
  110.         $result $this->dropSequence($table);
  111.  
  112.         //remove autoincrement trigger associated with the table
  113.         $table $this->conn->quote(strtoupper($table));
  114.         $triggerName $this->conn->quote(strtoupper($table'_AUTOINCREMENT_PK');
  115.  
  116.         return $this->conn->exec("DELETE FROM RDB\$TRIGGERS WHERE UPPER(RDB\$RELATION_NAME)=" $table " AND UPPER(RDB\$TRIGGER_NAME)=" $triggerName);
  117.     }
  118.     /**
  119.      * create a new table
  120.      *
  121.      * @param string $name     Name of the database that should be created
  122.      * @param array $fields Associative array that contains the definition of each field of the new table
  123.      *                         The indexes of the array entries are the names of the fields of the table an
  124.      *                         the array entry values are associative arrays like those that are meant to be
  125.      *                          passed with the field definitions to get[Type]Declaration() functions.
  126.      *
  127.      *                         Example
  128.      *                         array(
  129.      *
  130.      *                             'id' => array(
  131.      *                                 'type' => 'integer',
  132.      *                                 'unsigned' => 1,
  133.      *                                 'notnull' => 1,
  134.      *                                 'default' => 0,
  135.      *                             ),
  136.      *                             'name' => array(
  137.      *                                 'type' => 'text',
  138.      *                                 'length' => 12,
  139.      *                             ),
  140.      *                             'description' => array(
  141.      *                                 'type' => 'text',
  142.      *                                 'length' => 12,
  143.      *                             )
  144.      *                         );
  145.      * @param array $options  An associative array of table options:
  146.      *
  147.      * @return void 
  148.      */
  149.     public function createTable($namearray $fieldsarray $options array()) {
  150.         parent::createTable($name$fields$options);
  151.  
  152.         // TODO ? $this->_silentCommit();
  153.         foreach ($fields as $field_name => $field{
  154.             if empty($field['autoincrement'])) {
  155.                 //create PK constraint
  156.                 $pk_definition array(
  157.                     'fields' => array($field_name => array()),
  158.                     'primary' => true,
  159.                 );
  160.                 //$pk_name = $name.'_PK';
  161.                 $pk_name null;
  162.                 $result $this->createConstraint($name$pk_name$pk_definition);
  163.  
  164.                 //create autoincrement sequence + trigger
  165.                 return $this->_makeAutoincrement($field_name$name1);
  166.             }
  167.         }
  168.     }
  169.     /**
  170.      * Check if planned changes are supported
  171.      *
  172.      * @param string $name name of the database that should be dropped
  173.      * @return void 
  174.      */
  175.     public function checkSupportedChanges(&$changes)
  176.     {
  177.         foreach ($changes as $change_name => $change{
  178.             switch ($change_name{
  179.                 case 'notnull':
  180.                     throw new Doctrine_DataDict_Exception('it is not supported changes to field not null constraint');
  181.                 case 'default':
  182.                     throw new Doctrine_DataDict_Exception('it is not supported changes to field default value');
  183.                 case 'length':
  184.                     /*
  185.                     return throw new Doctrine_DataDict_Firebird_Exception('it is not supported changes to field default length');
  186.                     */
  187.                 case 'unsigned':
  188.                 case 'type':
  189.                 case 'declaration':
  190.                 case 'definition':
  191.                     break;
  192.                 default:
  193.                     throw new Doctrine_DataDict_Exception('it is not supported change of type' $change_name);
  194.             }
  195.         }
  196.         return true;
  197.     }
  198.     /**
  199.      * drop an existing table
  200.      *
  201.      * @param string $name name of the table that should be dropped
  202.      * @return mixed MDB2_OK on success, a MDB2 error on failure
  203.      * @access public
  204.      */
  205.     public function dropTable($name)
  206.     {
  207.         $result $this->_dropAutoincrement($name);
  208.         $result parent::dropTable($name);
  209.  
  210.         //$this->_silentCommit();
  211.  
  212.         return $result;
  213.     }
  214.     /**
  215.      * alter an existing table
  216.      *
  217.      * @param string $name         name of the table that is intended to be changed.
  218.      * @param array $changes     associative array that contains the details of each type
  219.      *                              of change that is intended to be performed. The types of
  220.      *                              changes that are currently supported are defined as follows:
  221.      *
  222.      *                              name
  223.      *
  224.      *                                 New name for the table.
  225.      *
  226.      *                             add
  227.      *
  228.      *                                 Associative array with the names of fields to be added as
  229.      *                                  indexes of the array. The value of each entry of the array
  230.      *                                  should be set to another associative array with the properties
  231.      *                                  of the fields to be added. The properties of the fields should
  232.      *                                  be the same as defined by the Metabase parser.
  233.      *
  234.      *
  235.      *                             remove
  236.      *
  237.      *                                 Associative array with the names of fields to be removed as indexes
  238.      *                                  of the array. Currently the values assigned to each entry are ignored.
  239.      *                                  An empty array should be used for future compatibility.
  240.      *
  241.      *                             rename
  242.      *
  243.      *                                 Associative array with the names of fields to be renamed as indexes
  244.      *                                  of the array. The value of each entry of the array should be set to
  245.      *                                  another associative array with the entry named name with the new
  246.      *                                  field name and the entry named Declaration that is expected to contain
  247.      *                                  the portion of the field declaration already in DBMS specific SQL code
  248.      *                                  as it is used in the CREATE TABLE statement.
  249.      *
  250.      *                             change
  251.      *
  252.      *                                 Associative array with the names of the fields to be changed as indexes
  253.      *                                  of the array. Keep in mind that if it is intended to change either the
  254.      *                                  name of a field and any other properties, the change array entries
  255.      *                                  should have the new names of the fields as array indexes.
  256.      *
  257.      *                                 The value of each entry of the array should be set to another associative
  258.      *                                  array with the properties of the fields to that are meant to be changed as
  259.      *                                  array entries. These entries should be assigned to the new values of the
  260.      *                                  respective properties. The properties of the fields should be the same
  261.      *                                  as defined by the Metabase parser.
  262.      *
  263.      *                             Example
  264.      *                                 array(
  265.      *                                     'name' => 'userlist',
  266.      *                                     'add' => array(
  267.      *                                         'quota' => array(
  268.      *                                             'type' => 'integer',
  269.      *                                             'unsigned' => 1
  270.      *                                         )
  271.      *                                     ),
  272.      *                                     'remove' => array(
  273.      *                                         'file_limit' => array(),
  274.      *                                         'time_limit' => array()
  275.      *                                     ),
  276.      *                                     'change' => array(
  277.      *                                         'name' => array(
  278.      *                                             'length' => '20',
  279.      *                                             'definition' => array(
  280.      *                                                 'type' => 'text',
  281.      *                                                 'length' => 20,
  282.      *                                             ),
  283.      *                                         )
  284.      *                                     ),
  285.      *                                     'rename' => array(
  286.      *                                         'sex' => array(
  287.      *                                             'name' => 'gender',
  288.      *                                             'definition' => array(
  289.      *                                                 'type' => 'text',
  290.      *                                                 'length' => 1,
  291.      *                                                 'default' => 'M',
  292.      *                                             ),
  293.      *                                         )
  294.      *                                     )
  295.      *                                 )
  296.      *
  297.      * @param boolean $check     indicates whether the function should just check if the DBMS driver
  298.      *                              can perform the requested table alterations if the value is true or
  299.      *                              actually perform them otherwise.
  300.      * @return void 
  301.      */
  302.     public function alterTable($namearray $changes$check)
  303.     {
  304.         foreach ($changes as $changeName => $change{
  305.             switch ($changeName{
  306.                 case 'add':
  307.                 case 'remove':
  308.                 case 'rename':
  309.                     break;
  310.                 case 'change':
  311.                     foreach ($changes['change'as $field{
  312.                         $this->checkSupportedChanges($field);
  313.                     }
  314.                     break;
  315.                 default:
  316.                     throw new Doctrine_DataDict_Exception('change type ' $changeName ' not yet supported');
  317.             }
  318.         }
  319.         if ($check{
  320.             return true;
  321.         }
  322.         $query '';
  323.         if (!empty($changes['add']&& is_array($changes['add'])) {
  324.             foreach ($changes['add'as $fieldName => $field{
  325.                 if ($query{
  326.                     $query.= ', ';
  327.                 }
  328.                 $query.= 'ADD ' $this->getDeclaration($field['type']$fieldName$field$name);
  329.             }
  330.         }
  331.  
  332.         if (!empty($changes['remove']&& is_array($changes['remove'])) {
  333.             foreach ($changes['remove'as $field_name => $field{
  334.                 if ($query{
  335.                     $query.= ', ';
  336.                 }
  337.                 $field_name $this->conn->quoteIdentifier($field_nametrue);
  338.                 $query.= 'DROP ' $field_name;
  339.             }
  340.         }
  341.  
  342.         if (!empty($changes['rename']&& is_array($changes['rename'])) {
  343.             foreach ($changes['rename'as $field_name => $field{
  344.                 if ($query{
  345.                     $query.= ', ';
  346.                 }
  347.                 $field_name $this->conn->quoteIdentifier($field_nametrue);
  348.                 $query.= 'ALTER ' $field_name ' TO ' $this->conn->quoteIdentifier($field['name']true);
  349.             }
  350.         }
  351.  
  352.         if (!empty($changes['change']&& is_array($changes['change'])) {
  353.             // missing support to change DEFAULT and NULLability
  354.             foreach ($changes['change'as $fieldName => $field{
  355.                 $this->checkSupportedChanges($field);
  356.                 if ($query{
  357.                     $query.= ', ';
  358.                 }
  359.                 $this->conn->loadModule('Datatype'nulltrue);
  360.                 $field_name $this->conn->quoteIdentifier($fieldNametrue);
  361.                 $query.= 'ALTER ' $field_name.' TYPE ' $this->getTypeDeclaration($field['definition']);
  362.             }
  363.         }
  364.  
  365.         if (!strlen($query)) {
  366.             return false;
  367.         }
  368.  
  369.         $name $this->conn->quoteIdentifier($nametrue);
  370.         $result $this->conn->exec('ALTER TABLE ' $name ' ' $query);
  371.         $this->_silentCommit();
  372.         return $result;
  373.     }
  374.     /**
  375.      * Get the stucture of a field into an array
  376.      *
  377.      * @param string    $table         name of the table on which the index is to be created
  378.      * @param string    $name         name of the index to be created
  379.      * @param array     $definition        associative array that defines properties of the index to be created.
  380.      *                                  Currently, only one property named FIELDS is supported. This property
  381.      *                                  is also an associative with the names of the index fields as array
  382.      *                                  indexes. Each entry of this array is set to another type of associative
  383.      *                                  array that specifies properties of the index that are specific to
  384.      *                                  each field.
  385.      *
  386.      *                                 Currently, only the sorting property is supported. It should be used
  387.      *                                  to define the sorting direction of the index. It may be set to either
  388.      *                                  ascending or descending.
  389.      *
  390.      *                                 Not all DBMS support index sorting direction configuration. The DBMS
  391.      *                                  drivers of those that do not support it ignore this property. Use the
  392.      *                                  function support() to determine whether the DBMS driver can manage indexes.
  393.  
  394.      *                                  Example
  395.      *                                     array(
  396.      *                                         'fields' => array(
  397.      *                                             'user_name' => array(
  398.      *                                                 'sorting' => 'ascending'
  399.      *                                             ),
  400.      *                                             'last_login' => array()
  401.      *                                         )
  402.      *                                     )
  403.      * @return void 
  404.      */
  405.     public function createIndexSql($table$namearray $definition)
  406.     {
  407.         $query 'CREATE';
  408.  
  409.         $query_sort '';
  410.         foreach ($definition['fields'as $field{
  411.             if (!strcmp($query_sort''&& isset($field['sorting'])) {
  412.                 switch ($field['sorting']{
  413.                     case 'ascending':
  414.                         $query_sort ' ASC';
  415.                         break;
  416.                     case 'descending':
  417.                         $query_sort ' DESC';
  418.                         break;
  419.                 }
  420.             }
  421.         }
  422.         $table $this->conn->quoteIdentifier($tabletrue);
  423.         $name  $this->conn->quoteIdentifier($this->conn->formatter->getIndexName($name)true);
  424.         $query .= $query_sort' INDEX ' $name ' ON ' $table;
  425.         $fields array();
  426.         foreach (array_keys($definition['fields']as $field{
  427.             $fields[$this->conn->quoteIdentifier($fieldtrue);
  428.         }
  429.         $query .= ' ('.implode(', '$fields')';
  430.  
  431.         return $query;
  432.     }
  433.     /**
  434.      * create a constraint on a table
  435.      *
  436.      * @param string    $table      name of the table on which the constraint is to be created
  437.      * @param string    $name       name of the constraint to be created
  438.      * @param array     $definition associative array that defines properties of the constraint to be created.
  439.      *                               Currently, only one property named FIELDS is supported. This property
  440.      *                               is also an associative with the names of the constraint fields as array
  441.      *                               constraints. Each entry of this array is set to another type of associative
  442.      *                               array that specifies properties of the constraint that are specific to
  443.      *                               each field.
  444.      *
  445.      *                               Example
  446.      *                                   array(
  447.      *                                       'fields' => array(
  448.      *                                           'user_name' => array(),
  449.      *                                           'last_login' => array(),
  450.      *                                       )
  451.      *                                   )
  452.      * @return void 
  453.      */
  454.     public function createConstraint($table$name$definition)
  455.     {
  456.         $table $this->conn->quoteIdentifier($tabletrue);
  457.  
  458.         if (!empty($name)) {
  459.             $name $this->conn->quoteIdentifier($this->conn->formatter->getIndexName($name)true);
  460.         }
  461.         $query "ALTER TABLE $table ADD";
  462.         if (!empty($definition['primary'])) {
  463.             if (!empty($name)) {
  464.                 $query.= ' CONSTRAINT '.$name;
  465.             }
  466.             $query.= ' PRIMARY KEY';
  467.         else {
  468.             $query.= ' CONSTRAINT '$name;
  469.             if (!empty($definition['unique'])) {
  470.                $query.= ' UNIQUE';
  471.             }
  472.         }
  473.         $fields array();
  474.         foreach (array_keys($definition['fields']as $field{
  475.             $fields[$this->conn->quoteIdentifier($fieldtrue);
  476.         }
  477.         $query .= ' ('implode(', '$fields')';
  478.         $result $this->conn->exec($query);
  479.         // TODO ? $this->_silentCommit();
  480.         return $result;
  481.     }
  482.     /**
  483.      * A method to return the required SQL string that fits between CREATE ... TABLE
  484.      * to create the table as a temporary table.
  485.      *
  486.      * @return string The string required to be placed between "CREATE" and "TABLE"
  487.      *                 to generate a temporary table, if possible.
  488.      */
  489.     public function getTemporaryTableQuery()
  490.     {
  491.         return 'GLOBAL TEMPORARY';
  492.     }
  493.     /**
  494.      * create sequence
  495.      *
  496.      * @param string $seqName name of the sequence to be created
  497.      * @param string $start start value of the sequence; default is 1
  498.      * @param array     $options  An associative array of table options:
  499.      *                           array(
  500.      *                               'comment' => 'Foo',
  501.      *                               'charset' => 'utf8',
  502.      *                               'collate' => 'utf8_unicode_ci',
  503.      *                           );
  504.      * @return boolean 
  505.      */
  506.     public function createSequence($seqName$start 1array $options array())
  507.     {
  508.         $sequenceName $this->conn->formatter->getSequenceName($seqName);
  509.  
  510.         $this->conn->exec('CREATE GENERATOR ' $sequenceName);
  511.  
  512.         try {
  513.             $this->conn->exec('SET GENERATOR ' $sequenceName ' TO ' ($start-1));
  514.             
  515.             return true;
  516.         catch (Doctrine_Connection_Exception $e{
  517.             try {
  518.                 $this->dropSequence($seqName);
  519.             catch(Doctrine_Connection_Exception $e{
  520.                 throw new Doctrine_Export_Exception('Could not drop inconsistent sequence table');
  521.             }
  522.         }
  523.         throw new Doctrine_Export_Exception('could not create sequence table');
  524.     }
  525.     /**
  526.      * drop existing sequence
  527.      *
  528.      * @param string $seqName name of the sequence to be dropped
  529.      * @return void 
  530.      */
  531.     public function dropSequenceSql($seqName)
  532.     {
  533.         $sequenceName $this->conn->formatter->getSequenceName($seqName);
  534.         $sequenceName $this->conn->quote($sequenceName);
  535.         $query "DELETE FROM RDB\$GENERATORS WHERE UPPER(RDB\$GENERATOR_NAME)=" $sequenceName;
  536.         
  537.         return $query;
  538.     }
  539. }