Source for file Frontbase.php

Documentation is available at Frontbase.php

  1. <?php
  2. /*
  3.  *  $Id$
  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_Frontbase
  24.  *
  25.  * @package     Doctrine
  26.  * @author      Konsta Vesterinen <kvesteri@cc.hut.fi>
  27.  * @author      Lukas Smith <smith@pooteeweet.org> (PEAR MDB2 library)
  28.  * @license     http://www.opensource.org/licenses/lgpl-license.php LGPL
  29.  * @category    Object Relational Mapping
  30.  * @link        www.phpdoctrine.com
  31.  * @since       1.0
  32.  * @version     $Revision$
  33.  */
  34. {
  35.     /**
  36.      * create a new database
  37.      *
  38.      * @param string $name name of the database that should be created
  39.      * @return string 
  40.      */
  41.     public function createDatabaseSql($name)
  42.     {
  43.         $name  $this->conn->quoteIdentifier($nametrue);
  44.         return 'CREATE DATABASE ' $name;
  45.     }
  46.     /**
  47.      * drop an existing database
  48.      *
  49.      * @param string $name name of the database that should be dropped
  50.      * @return string 
  51.      */
  52.     public function dropDatabaseSql($name)
  53.     {
  54.         $name  $this->conn->quoteIdentifier($nametrue);
  55.         return 'DELETE DATABASE ' $name;    
  56.     }
  57.     /**
  58.      * drop an existing table
  59.      *
  60.      * @param object $this->conns        database object that is extended by this class
  61.      * @param string $name       name of the table that should be dropped
  62.      * @return string 
  63.      */
  64.     public function dropTableSql($name)
  65.     {
  66.         $name $this->conn->quoteIdentifier($nametrue);
  67.         return 'DROP TABLE ' $name ' CASCADE';
  68.     }
  69.     /**
  70.      * alter an existing table
  71.      *
  72.      * @param string $name         name of the table that is intended to be changed.
  73.      * @param array $changes     associative array that contains the details of each type
  74.      *                              of change that is intended to be performed. The types of
  75.      *                              changes that are currently supported are defined as follows:
  76.      *
  77.      *                              name
  78.      *
  79.      *                                 New name for the table.
  80.      *
  81.      *                             add
  82.      *
  83.      *                                 Associative array with the names of fields to be added as
  84.      *                                  indexes of the array. The value of each entry of the array
  85.      *                                  should be set to another associative array with the properties
  86.      *                                  of the fields to be added. The properties of the fields should
  87.      *                                  be the same as defined by the MDB2 parser.
  88.      *
  89.      *
  90.      *                             remove
  91.      *
  92.      *                                 Associative array with the names of fields to be removed as indexes
  93.      *                                  of the array. Currently the values assigned to each entry are ignored.
  94.      *                                  An empty array should be used for future compatibility.
  95.      *
  96.      *                             rename
  97.      *
  98.      *                                 Associative array with the names of fields to be renamed as indexes
  99.      *                                  of the array. The value of each entry of the array should be set to
  100.      *                                  another associative array with the entry named name with the new
  101.      *                                  field name and the entry named Declaration that is expected to contain
  102.      *                                  the portion of the field declaration already in DBMS specific SQL code
  103.      *                                  as it is used in the CREATE TABLE statement.
  104.      *
  105.      *                             change
  106.      *
  107.      *                                 Associative array with the names of the fields to be changed as indexes
  108.      *                                  of the array. Keep in mind that if it is intended to change either the
  109.      *                                  name of a field and any other properties, the change array entries
  110.      *                                  should have the new names of the fields as array indexes.
  111.      *
  112.      *                                 The value of each entry of the array should be set to another associative
  113.      *                                  array with the properties of the fields to that are meant to be changed as
  114.      *                                  array entries. These entries should be assigned to the new values of the
  115.      *                                  respective properties. The properties of the fields should be the same
  116.      *                                  as defined by the MDB2 parser.
  117.      *
  118.      *                             Example
  119.      *                                 array(
  120.      *                                     'name' => 'userlist',
  121.      *                                     'add' => array(
  122.      *                                         'quota' => array(
  123.      *                                             'type' => 'integer',
  124.      *                                             'unsigned' => 1
  125.      *                                         )
  126.      *                                     ),
  127.      *                                     'remove' => array(
  128.      *                                         'file_limit' => array(),
  129.      *                                         'time_limit' => array()
  130.      *                                     ),
  131.      *                                     'change' => array(
  132.      *                                         'name' => array(
  133.      *                                             'length' => '20',
  134.      *                                             'definition' => array(
  135.      *                                                 'type' => 'text',
  136.      *                                                 'length' => 20,
  137.      *                                             ),
  138.      *                                         )
  139.      *                                     ),
  140.      *                                     'rename' => array(
  141.      *                                         'sex' => array(
  142.      *                                             'name' => 'gender',
  143.      *                                             'definition' => array(
  144.      *                                                 'type' => 'text',
  145.      *                                                 'length' => 1,
  146.      *                                                 'default' => 'M',
  147.      *                                             ),
  148.      *                                         )
  149.      *                                     )
  150.      *                                 )
  151.      *
  152.      * @param boolean $check     indicates whether the function should just check if the DBMS driver
  153.      *                              can perform the requested table alterations if the value is true or
  154.      *                              actually perform them otherwise.
  155.      * @access public
  156.      *
  157.      * @return boolean 
  158.      */
  159.     public function alterTable($namearray $changes$check)
  160.     {
  161.         foreach ($changes as $changeName => $change){
  162.             switch ($changeName{
  163.             case 'add':
  164.             case 'remove':
  165.             case 'change':
  166.             case 'rename':
  167.             case 'name':
  168.                 break;
  169.             default:
  170.                 throw new Doctrine_Export_Exception('change type "'.$changeName.'" not yet supported');
  171.             }
  172.         }
  173.  
  174.         if ($check{
  175.             return true;
  176.         }
  177.  
  178.         $query '';
  179.         if (!empty($changes['name'])) {
  180.             $changeName $this->conn->quoteIdentifier($changes['name']true);
  181.             $query .= 'RENAME TO ' $changeName;
  182.         }
  183.  
  184.         if (!empty($changes['add']&& is_array($changes['add'])) {
  185.             foreach ($changes['add'as $fieldName => $field{
  186.                 if ($query{
  187.                     $query.= ', ';
  188.                 }
  189.                 $query.= 'ADD ' $this->conn->getDeclaration($field['type']$fieldName$field);
  190.             }
  191.         }
  192.  
  193.         if (!empty($changes['remove']&& is_array($changes['remove'])) {
  194.             foreach ($changes['remove'as $fieldName => $field{
  195.                 if ($query{
  196.                     $query.= ', ';
  197.                 }
  198.                 $fieldName $this->conn->quoteIdentifier($fieldNametrue);
  199.                 $query.= 'DROP ' $fieldName;
  200.             }
  201.         }
  202.  
  203.         $rename array();
  204.         if (!empty($changes['rename']&& is_array($changes['rename'])) {
  205.             foreach ($changes['rename'as $fieldName => $field{
  206.                 $rename[$field['name']] $fieldName;
  207.             }
  208.         }
  209.  
  210.         if (!empty($changes['change']&& is_array($changes['change'])) {
  211.             foreach ($changes['change'as $fieldName => $field{
  212.                 if ($query{
  213.                     $query.= ', ';
  214.                 }
  215.                 if (isset($rename[$fieldName])) {
  216.                     $oldFieldName $rename[$fieldName];
  217.                     unset($rename[$fieldName]);
  218.                 else {
  219.                     $oldFieldName $fieldName;
  220.                 }
  221.                 $oldFieldName $this->conn->quoteIdentifier($oldFieldNametrue);
  222.                 $query.= 'CHANGE ' $oldFieldName ' ' $this->conn->getDeclaration($field['definition']['type']$oldFieldName$field['definition']);
  223.             }
  224.         }
  225.  
  226.         if (!empty($rename&& is_array($rename)) {
  227.             foreach ($rename as $renamedFieldName => $renamed_field{
  228.                 if ($query{
  229.                     $query.= ', ';
  230.                 }
  231.                 $oldFieldName $rename[$renamedFieldName];
  232.                 $field $changes['rename'][$oldFieldName];
  233.                 $query.= 'CHANGE ' $this->conn->getDeclaration($field['definition']['type']$oldFieldName$field['definition']);
  234.             }
  235.         }
  236.  
  237.         if (!$query{
  238.             return true;
  239.         }
  240.  
  241.         $name $this->conn->quoteIdentifier($nametrue);
  242.         return $this->conn->exec('ALTER TABLE ' $name ' ' $query);
  243.     }
  244.     /**
  245.      * create sequence
  246.      *
  247.      * @param string    $seqName     name of the sequence to be created
  248.      * @param string    $start         start value of the sequence; default is 1
  249.      * @param array     $options  An associative array of table options:
  250.      *                           array(
  251.      *                               'comment' => 'Foo',
  252.      *                               'charset' => 'utf8',
  253.      *                               'collate' => 'utf8_unicode_ci',
  254.      *                           );
  255.      * @return void 
  256.      */
  257.     public function createSequence($sequenceName$start 1array $options array())
  258.     {
  259.         $sequenceName $this->conn->quoteIdentifier($this->conn->getSequenceName($sequenceName)true);
  260.         $seqcolName   $this->conn->quoteIdentifier($this->conn->getAttribute(Doctrine::ATTR_SEQCOL_NAME)true);
  261.  
  262.         $query 'CREATE TABLE ' $sequenceName ' (' $seqcolName ' INTEGER DEFAULT UNIQUE, PRIMARY KEY(' $seqcolName '))';
  263.         $res $this->conn->exec($query);
  264.         $res $this->conn->exec('SET UNIQUE = 1 FOR ' $sequenceName);
  265.  
  266.         if ($start == 1{
  267.             return true;
  268.         }
  269.         
  270.         try {
  271.             $this->conn->exec('INSERT INTO ' $sequenceName ' (' $seqcolName ') VALUES (' ($start-1')');
  272.         catch(Doctrine_Connection_Exception $e{
  273.             // Handle error
  274.             try {
  275.                 $this->conn->exec('DROP TABLE ' $sequenceName);
  276.             catch(Doctrine_Connection_Exception $e{
  277.                 throw new Doctrine_Export_Exception('could not drop inconsistent sequence table');
  278.             }
  279.  
  280.             throw new Doctrine_Export_Exception('could not create sequence table');
  281.         }
  282.     }
  283.     /**
  284.      * drop existing sequence
  285.      *
  286.      * @param string $seqName       name of the sequence to be dropped
  287.      * @return string 
  288.      */
  289.     public function dropSequenceSql($seqName)
  290.     {
  291.         $sequenceName $this->conn->quoteIdentifier($this->conn->getSequenceName($seqName)true);
  292.  
  293.         return 'DROP TABLE ' $sequenceName ' CASCADE';
  294.     }
  295.     /**
  296.      * drop existing index
  297.      *
  298.      * @param string    $table        name of table that should be used in method
  299.      * @param string    $name         name of the index to be dropped
  300.      * @return boolean 
  301.      */
  302.     public function dropIndexSql($table$name)
  303.     {
  304.         $table $this->conn->quoteIdentifier($tabletrue);
  305.         $name $this->conn->quoteIdentifier($this->conn->getIndexName($name)true);
  306.  
  307.         return 'ALTER TABLE ' $table ' DROP INDEX ' $name;
  308.     }
  309. }