[ Index ]
 

Code source de LifeType 1.2.4

Accédez au Source d'autres logiciels libres

Classes | Fonctions | Variables | Constantes | Tables

title

Body

[fermer]

/class/database/pdb/datadict/ -> pdbmysqldatadict.class.php (source)

   1  <?php
   2      
   3      /**

   4        V4.62 2 Apr 2005  (c) 2000-2005 John Lim (jlim@natsoft.com.my). All rights reserved.

   5        Released under both BSD license and Lesser GPL library license. 

   6        Whenever there is any discrepancy between the two licenses, 

   7        the BSD license will take precedence.

   8      */
   9      
  10      lt_include( PLOG_CLASS_PATH."class/database/pdb/datadict/pdbbasedatadict.class.php" );
  11      
  12      /**

  13       * Data dictionary for MySQL, used by PDb to be able to convert a table meta-definition

  14        * into SQL executable code for altering the db schema, adding new fields, etc.

  15       *

  16       * \ingroup PDb

  17       */    
  18      class PDbMysqlDataDict extends PDbBaseDataDict 
  19      {
  20          var $databaseType = 'mysql';
  21          var $alterCol = ' MODIFY COLUMN';
  22          var $alterTableAddIndex = true;
  23          var $dropTable = 'DROP TABLE IF EXISTS %s'; // requires mysql 3.22 or later

  24          
  25          var $dropIndex = 'DROP INDEX %s ON %s';
  26          var $renameColumn = 'ALTER TABLE %s CHANGE COLUMN %s %s %s';    // needs column-definition!

  27  
  28          /**

  29           * @see PdbBaseDataDict::MetaType

  30           */
  31          function MetaType($t,$len=-1,$fieldobj=false)
  32          {
  33              if (is_object($t)) {
  34                  $fieldobj = $t;
  35                  $t = $fieldobj->type;
  36                  $len = $fieldobj->max_length;
  37              }
  38              $is_serial = is_object($fieldobj) && $fieldobj->primary_key && $fieldobj->auto_increment;
  39              
  40              $len = -1; // mysql max_length is not accurate

  41              switch (strtoupper($t)) {
  42              case 'STRING': 
  43              case 'CHAR':
  44              case 'VARCHAR': 
  45              case 'TINYBLOB': 
  46              case 'TINYTEXT': 
  47              case 'ENUM': 
  48              case 'SET':
  49                  if ($len <= $this->blobSize) return 'C';
  50                  
  51              case 'TEXT':
  52              case 'LONGTEXT': 
  53              case 'MEDIUMTEXT':
  54                  return 'X';
  55                  
  56              // php_mysql extension always returns 'blob' even if 'text'

  57              // so we have to check whether binary...

  58              case 'IMAGE':
  59              case 'LONGBLOB': 
  60              case 'BLOB':
  61              case 'MEDIUMBLOB':
  62                  return !empty($fieldobj->binary) ? 'B' : 'X';
  63                  
  64              case 'YEAR':
  65              case 'DATE': return 'D';
  66              
  67              case 'TIME':
  68              case 'DATETIME':
  69              case 'TIMESTAMP': return 'T';
  70              
  71              case 'FLOAT':
  72              case 'DOUBLE':
  73                  return 'F';
  74                  
  75              case 'INT': 
  76              case 'INTEGER': return $is_serial ? 'R' : 'I';
  77              case 'TINYINT': return $is_serial ? 'R' : 'I1';
  78              case 'SMALLINT': return $is_serial ? 'R' : 'I2';
  79              case 'MEDIUMINT': return $is_serial ? 'R' : 'I4';
  80              case 'BIGINT':  return $is_serial ? 'R' : 'I8';
  81              default: return 'N';
  82              }
  83          }
  84      
  85          /**

  86           * @see PdbBaseDataDict::ActualType

  87           */
  88          function ActualType($meta)
  89          {
  90              switch(strtoupper($meta)) {
  91              case 'C': return 'VARCHAR';
  92              case 'XL':return 'LONGTEXT';
  93              case 'X': return 'TEXT';
  94              
  95              case 'C2': return 'VARCHAR';
  96              case 'X2': return 'LONGTEXT';
  97              
  98              case 'B': return 'LONGBLOB';
  99                  
 100              case 'D': return 'DATE';
 101              case 'T': return 'TIMESTAMP';
 102              case 'L': return 'TINYINT';
 103              
 104              case 'R':
 105              case 'I4':
 106              case 'I': return 'INTEGER';
 107              case 'I1': return 'TINYINT';
 108              case 'I2': return 'SMALLINT';
 109              case 'I8': return 'BIGINT';
 110              
 111              case 'F': return 'FLOAT';
 112              case 'N': return 'NUMERIC';
 113              default:
 114                  return $meta;
 115              }
 116          }
 117          
 118          /**

 119           * @private

 120           */
 121          function _CreateSuffix($fname,$ftype,$fnotnull,$fdefault,$fautoinc,$fconstraint,$funsigned)
 122          {    
 123              $suffix = '';
 124              if ($funsigned) $suffix .= ' UNSIGNED';
 125              if ($fnotnull) $suffix .= ' NOT NULL';
 126              if (strlen($fdefault)) $suffix .= " DEFAULT $fdefault";
 127              if ($fautoinc) $suffix .= ' AUTO_INCREMENT';
 128              if ($fconstraint) $suffix .= ' '.$fconstraint;
 129              return $suffix;
 130          }        
 131          
 132          /**

 133           * @private

 134           */
 135          function _IndexSQL($idxname, $tabname, $flds, $idxoptions)
 136          {
 137              $sql = array();
 138              
 139              if ( isset($idxoptions['REPLACE']) || isset($idxoptions['DROP']) ) {
 140                  if ($this->alterTableAddIndex) $sql[] = "ALTER TABLE $tabname DROP INDEX $idxname";
 141                  else $sql[] = sprintf($this->dropIndex, $idxname, $tabname);
 142      
 143                  if ( isset($idxoptions['DROP']) )
 144                      return $sql;
 145              }
 146              
 147              if ( empty ($flds) ) {
 148                  return $sql;
 149              }
 150              
 151              if (isset($idxoptions['FULLTEXT'])) {
 152                  $unique = ' FULLTEXT';
 153              } elseif (isset($idxoptions['UNIQUE'])) {
 154                  $unique = ' UNIQUE';
 155              } else {
 156                  $unique = '';
 157              }
 158              
 159              if ( is_array($flds) ) $flds = implode(', ',$flds);
 160              
 161              if ($this->alterTableAddIndex) $s = "ALTER TABLE $tabname ADD $unique INDEX $idxname ";
 162              else $s = 'CREATE' . $unique . ' INDEX ' . $idxname . ' ON ' . $tabname;
 163              
 164              $s .= ' (' . $flds . ')';
 165              
 166              if ( isset($idxoptions[$this->upperName]) )
 167                  $s .= $idxoptions[$this->upperName];
 168              
 169              $sql[] = $s;
 170              
 171              return $sql;
 172          }
 173      }
 174  ?>


Généré le : Mon Nov 26 21:04:15 2007 par Balluche grâce à PHPXref 0.7
  Clicky Web Analytics