Source for file mysql.dbschema.php

Documentation is available at mysql.dbschema.php

  1. <?php
  2. /**
  3. @package    jelix
  4. @subpackage db
  5. @author     Laurent Jouanneau
  6. @copyright  2005-2010 Laurent Jouanneau
  7. @link        http://www.jelix.org
  8. @licence     http://www.gnu.org/licenses/lgpl.html GNU Lesser General Public Licence, see LICENCE file
  9. */
  10.  
  11. /**
  12.  * 
  13.  * @package    jelix
  14.  * @subpackage db_driver
  15.  */
  16. class mysqlDbTable extends jDbTable {
  17.  
  18.     public $attributes = array();
  19.  
  20.     protected function _loadColumns({
  21.  
  22.         $this->columns = array ();
  23.         $this->primaryKey = false;
  24.         $conn $this->schema->getConn();
  25.         $tools $conn->tools();
  26.         $rs $conn->query ('SHOW FIELDS FROM '.$conn->encloseName($this->name));
  27.  
  28.         while ($line $rs->fetch ()) {
  29.  
  30.             $length 0;
  31.             if (preg_match('/^(\w+)\s*(\((\d+)\))?.*$/',$line->Type,$m)) {
  32.                 $type strtolower($m[1]);
  33.                 if ($type == 'varchar' && isset($m[3])) {
  34.                     $length intval($m[3]);
  35.                 }
  36.             else {
  37.                 $type $line->Type;
  38.             }
  39.             $notNull ($line->Null == 'NO');
  40.             $autoIncrement  ($line->Extra == 'auto_increment');
  41.             $hasDefault ($line->Default != '' || !($line->Default == null && $notNull));
  42.             // to fix a bug in php 5.2.5 or mysql 5.0.51
  43.             if($notNull && $line->Default === null && !$autoIncrement)
  44.                 $default ='';
  45.             else
  46.                 $default $line->Default;
  47.  
  48.             $col new jDbColumn($line->Field$type$length$hasDefault$default$notNull);
  49.             $col->autoIncrement $autoIncrement;
  50.  
  51.             $typeinfo $tools->getTypeInfo($type);
  52.             //$col->unifiedType = $typeinfo[1];
  53.             $col->maxValue $typeinfo[3];
  54.             $col->minValue $typeinfo[2];
  55.             $col->maxLength $typeinfo[5];
  56.             $col->minLength $typeinfo[4];
  57.             if ($col->length !=0)
  58.                 $col->maxLength $col->length;
  59.  
  60.             if ($line->Key == 'PRI'{
  61.                 if (!$this->primaryKey)
  62.                     $this->primaryKey = new jDbPrimaryKey($line->Field);
  63.                 else
  64.                     $this->primaryKey->columns[$line->Field;
  65.             }
  66.             
  67.             $this->columns[$line->Field$col;
  68.         }
  69.     }
  70.  
  71.     protected function _alterColumn(jDbColumn $oldjDbColumn $new{
  72.         $conn $this->schema->getConn();
  73.  
  74.         $pk $this->getPrimaryKey();
  75.         $isPk ($pk && in_array($new->name$pk->columns));
  76.  
  77.         $sql 'ALTER TABLE '.$conn->encloseName($this->name)
  78.                 .' CHANGE COLUMN '.$conn->encloseName($old->name)
  79.                 .' '.$this->schema->_prepareSqlColumn($new);
  80.         if ($isPk && $col->autoIncrement)
  81.             $sql .= ' AUTO_INCREMENT';
  82.         $conn->exec($sql);
  83.     }
  84.  
  85.     protected function _addColumn(jDbColumn $new{
  86.         $conn $this->schema->getConn();
  87.         $pk $this->getPrimaryKey();
  88.         $isPk ($pk && in_array($new->name$pk->columns));
  89.         $sql 'ALTER TABLE '.$conn->encloseName($this->name)
  90.                 .' ADD COLUMN '.$this->schema->_prepareSqlColumn($new);
  91.         if ($isPk && $col->autoIncrement)
  92.             $sql .= ' AUTO_INCREMENT';
  93.  
  94.         $conn->exec($sql);
  95.     }
  96.  
  97.  
  98.     protected function _loadIndexesAndKeys({
  99.  
  100.         $conn $this->schema->getConn();
  101.         $rs $conn->query('SHOW INDEX FROM '.$conn->encloseName($this->name));
  102.  
  103.         $this->uniqueKeys = $this->indexes = array();
  104.         $this->primaryKey = false;
  105.  
  106.         while ($idx $rs->fetch ()) {
  107.             if ($idx->Key_name == 'PRIMARY'{
  108.                 if (!$this->primaryKey)
  109.                     $this->primaryKey = new jDbPrimaryKey($idx->Column_name);
  110.                 else
  111.                     $this->primaryKey->columns[$idx->Seq_in_index-1$idx->Column_name;
  112.             }
  113.             else if ($idx->Non_unique == 0{
  114.                 if(!isset($this->uniqueKeys[$idx->Key_name])) {
  115.                     $this->uniqueKeys[$idx->Key_namenew jDbUniqueKey($idx->Key_name);
  116.                 }
  117.                 $this->uniqueKeys[$idx->Key_name]->columns[$idx->Seq_in_index-1$idx->Column_name;
  118.             }
  119.             else {
  120.                 if(!isset($this->indexes[$idx->Key_name])) {
  121.                     $this->indexes[$idx->Key_namenew jDbIndex($idx->Key_name$idx->Index_type);
  122.                 }
  123.                 $this->indexes[$idx->Key_name]->columns[$idx->Seq_in_index-1$idx->Column_name;
  124.             }
  125.         }
  126.     }
  127.     
  128.     protected function _createIndex(jDbIndex $index{
  129.  
  130.         $conn $this->schema->getConn();
  131.         $sql 'ALTER TABLE '.$conn->encloseName($this->name).' ADD ';
  132.  
  133.         if ($index instanceof jDbPrimaryKey{
  134.             $sql .= 'PRIMARY KEY';
  135.         }
  136.         else if ($index instanceof jDbUniqueKey{
  137.             $sql .= 'CONSTRAINT UNIQUE KEY '.$conn->encloseName($index->name);
  138.         }
  139.         else {
  140.             $sql .= 'INDEX '.$conn->encloseName($index->name);
  141.             if ($index->type != '')
  142.                 $sql.= ' USING '.$index->type;
  143.         }
  144.  
  145.         $f '';
  146.         foreach ($index->columns as $col{
  147.             $f .= ','.$conn->encloseName($col);
  148.         }
  149.  
  150.         $conn->exec($sql.'('.substr($f,1).')');
  151.     }
  152.  
  153.     protected function _dropIndex(jDbIndex $index{
  154.  
  155.         $conn $this->schema->getConn();
  156.         $sql 'ALTER TABLE '.$conn->encloseName($this->name).' DROP ';
  157.  
  158.         if ($index instanceof jDbPrimaryKey{
  159.             $sql .= 'PRIMARY KEY';
  160.         }
  161.         else {
  162.             $sql .= 'INDEX '.$conn->encloseName($index->name);
  163.         }
  164.  
  165.         $conn->exec($sql);
  166.     }
  167.  
  168.     protected function _loadReferences({
  169.         $conn $this->schema->getConn();
  170.         $sql 'SHOW CREATE TABLE '.$conn->encloseName($this->name);
  171.         $rs $conn->query($sql);
  172.         $rec $rs->fetch();
  173.  
  174.         /*
  175.         CONSTRAINT [symbol] FOREIGN KEY [index_name] (col_name [(length)] [ASC | DESC],...)
  176.         REFERENCES tbl_name (col_name [(length)] [ASC | DESC],...)
  177.               [MATCH FULL | MATCH PARTIAL | MATCH SIMPLE]
  178.               [ON DELETE  RESTRICT | CASCADE | SET NULL | NO ACTION]
  179.               [ON UPDATE  RESTRICT | CASCADE | SET NULL | NO ACTION]
  180.         */
  181.  
  182.         preg_match_all('/^\s*(?:CONSTRAINT(?:\s+`(.+?)`)?\s+)?FOREIGN\s+KEY(?:\s+`(.+?)`)?\s+\((.+?)\)\s+REFERENCES\s+`(.+?)`\s+\((.+?)\)(?:\s+MATCH\s+(FULL|PARTIAL|SIMPLE))?(?:\s+ON DELETE\s+(RESTRICT|CASCADE|SET NULL|NO ACTION))?(?:\s+ON UPDATE\s+(RESTRICT|CASCADE|SET NULL|NO ACTION))?,?$/msi'$s$m);
  183.         foreach ($m[1as $i => $symbol{
  184.             //$match = $m[6][$i];
  185.             $ref new jDbReference();
  186.             $ref->name ($m[2][$i!= ''?$m[2][$i]:$symbol;
  187.             $ref->fTable $m[4][$i];
  188.             $ref->onDelete $m[7][$i];
  189.             $ref->onUpdate $m[8][$i];
  190.             if (preg_match_all('/`([^`]+)`/'$m[3][$i]$mc))
  191.                 $ref->columns $mc[1];
  192.             if (preg_match_all('/`([^`]+)`/'$m[5][$i]$mc))
  193.                 $ref->fColumns $mc[1];
  194.             if ($ref->name && count($ref->columns&& count($ref->fColumns))
  195.                 $this->references[$ref->name$ref;
  196.         }
  197.     }
  198.  
  199.     protected function _createReference(jDbReference $ref{
  200.         $conn $this->schema->getConn();
  201.         $sql 'ALTER TABLE '.$conn->encloseName($this->name).' ADD CONSTRAINT ';
  202.         $sql.= $conn->encloseName($ref->name)' FOREIGN KEY (';
  203.  
  204.         $cols array();
  205.         $fcols array();
  206.         foreach ($ref->columns as $c{
  207.             $cols[$conn->encloseName($c);
  208.         }
  209.         foreach ($ref->fColumns as $c{
  210.             $fcols[$conn->encloseName($c);
  211.         }
  212.  
  213.         $sql .= implode(','$cols).') REFERENCES '.$conn->encloseName($ref->fTable).'(';
  214.         $sql .= implode(','$fcols).')';
  215.  
  216.         if ($ref->onUpdate{
  217.             $sql .= 'ON UPDATE '.$ref->onUpdate.' ';
  218.         }
  219.         if ($ref->onDelete{
  220.             $sql .= 'ON DELETE '.$ref->onDelete.' ';
  221.         }
  222.         $conn->exec($sql);
  223.     }
  224.  
  225.     protected function _dropReference(jDbReference $ref{
  226.         $conn $this->schema->getConn();
  227.         $sql 'ALTER TABLE '.$conn->encloseName($this->name).' DROP FOREIGN KEY '.$conn->encloseName($ref->name);
  228.         $conn->exec($sql);
  229.     }
  230.  
  231. }
  232.  
  233. /**
  234.  * 
  235.  * @package    jelix
  236.  * @subpackage db_driver
  237.  */
  238. class mysqlDbSchema extends jDbSchema {
  239.  
  240.     /**
  241.      * @param string $name 
  242.      * @param array[jDbColumn] $columns 
  243.      */
  244.     function _createTable($name$columns$primaryKey$attributes=array()) {
  245.  
  246.         $cols array();
  247.  
  248.         if (is_string($primaryKey))
  249.             $primaryKey array($primaryKey);
  250.  
  251.         foreach ($columns as $col{
  252.             $colstr $this->_prepareSqlColumn($col);
  253.  
  254.             if (in_array($col->name$primaryKey&& $col->autoIncrement{
  255.                 $colstr .= '  AUTO_INCREMENT';
  256.             }
  257.  
  258.             $cols[$colstr;
  259.         }
  260.  
  261.         $sql 'CREATE TABLE '.$this->conn->encloseName($name).' ('.implode(", ",$cols);
  262.         if (count($primaryKey))
  263.             $sql .= ', PRIMARY KEY ('.implode(','$primaryKey).')';
  264.         $sql .= ')';
  265.  
  266.         if (isset($attributes['engine'])) {
  267.             $sql.= ' ENGINE='.$attributes['engine'];
  268.         }
  269.         if (isset($attributes['charset'])) {
  270.             $sql.= ' CHARACTER SET '.$attributes['charset'];
  271.         }
  272.         if (isset($attributes['collate'])) {
  273.             $sql.= ' COLLATE '.$attributes['collate'];
  274.         }
  275.  
  276.         $this->conn->exec($sql);
  277.  
  278.         $table new mysqlDbTable($name$this);
  279.         $table->attributes $attributes;
  280.         return $table;
  281.     }
  282.  
  283.     protected function _getTables({
  284.         $results array ();
  285.         $conn $this->conn;
  286.         if (isset($this->conn->profile['database'])) {
  287.             $db $this->conn->profile['database'];
  288.         }
  289.         else if (isset($this->conn->profile['dsn'])
  290.                  && preg_match('/dbname=([a-z0-9_ ]*)/'$this->conn->profile['dsn']$m)){
  291.             $db $m[1];
  292.         }
  293.         else {
  294.             throw new jException("jelix~error.no.database.name"$this->conn->profile['name']);
  295.         }
  296.         $rs $this->conn->query ('SHOW TABLES FROM '.$this->conn->encloseName($db));
  297.         $col_name 'Tables_in_'.$db;
  298.  
  299.         while ($line $rs->fetch ()){
  300.             $results[$line->$col_namenew mysqlDbTable($line->$col_name$this);
  301.         }
  302.         return $results;
  303.     }
  304. }

Documentation generated on Wed, 24 Sep 2014 22:03:49 +0200 by phpDocumentor 1.4.3