[ Index ]
 

Code source de Symfony 1.0.0

Accédez au Source d'autres logiciels libresSoutenez Angelica Josefina !

title

Body

[fermer]

/lib/vendor/creole/drivers/mysqli/metadata/ -> MySQLiTableInfo.php (source)

   1  <?php
   2  /*
   3   * $Id: MySQLiTableInfo.php,v 1.3 2006/01/17 19:44:39 hlellelid Exp $
   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 please see
  19   * <http://creole.phpdb.org>.
  20   */
  21  
  22  require_once 'creole/metadata/TableInfo.php';
  23  
  24  /**
  25   * MySQLi implementation of TableInfo.
  26   *
  27   * @author    Sebastian Bergmann <sb@sebastian-bergmann.de>
  28   * @version   $Revision: 1.3 $
  29   * @package   creole.drivers.mysqli.metadata
  30   */
  31  class MySQLiTableInfo extends TableInfo {
  32      /** Loads the columns for this table. */
  33      protected function initColumns()
  34      {
  35          require_once 'creole/metadata/ColumnInfo.php';
  36          require_once 'creole/drivers/mysql/MySQLTypes.php';
  37  
  38          if (!@mysqli_select_db($this->conn->getResource(), $this->dbname)) {
  39              throw new SQLException('No database selected');
  40          }
  41  
  42          // To get all of the attributes we need, we use
  43          // the MySQL "SHOW COLUMNS FROM $tablename" SQL.
  44          $res = mysqli_query($this->conn->getResource(), "SHOW COLUMNS FROM " . $this->name);
  45  
  46          $defaults = array();
  47          $nativeTypes = array();
  48          $precisions = array();
  49  
  50          while($row = mysqli_fetch_assoc($res)) {
  51              $name = $row['Field'];
  52              $default = $row['Default'];
  53              $is_nullable = ($row['Null'] == 'YES');
  54  
  55              $size = null;
  56              $precision = null;
  57              $scale = null;
  58  
  59              if (preg_match('/^(\w+)[\(]?([\d,]*)[\)]?( |$)/', $row['Type'], $matches)) {
  60                  //            colname[1]   size/precision[2]
  61                  $nativeType = $matches[1];
  62                  if ($matches[2]) {
  63                      if ( ($cpos = strpos($matches[2], ',')) !== false) {
  64                          $size = (int) substr($matches[2], 0, $cpos);
  65                          $precision = $size;
  66                          $scale = (int) substr($matches[2], $cpos + 1);
  67                      } else {
  68                          $size = (int) $matches[2];
  69                      }
  70                  }
  71              } elseif (preg_match('/^(\w+)\(/', $row['Type'], $matches)) {
  72                  $nativeType = $matches[1];
  73              } else {
  74                  $nativeType = $row['Type'];
  75              }
  76  
  77              $this->columns[$name] = new ColumnInfo($this, $name, MySQLTypes::getType($nativeType), $nativeType, $size, $precision, $scale, $is_nullable, $default);
  78          }
  79  
  80          $this->colsLoaded = true;
  81      }
  82  
  83      /** Loads the primary key information for this table. */
  84      protected function initPrimaryKey()
  85      {
  86          require_once 'creole/metadata/PrimaryKeyInfo.php';
  87  
  88          // columns have to be loaded first
  89          if (!$this->colsLoaded) {
  90              $this->initColumns();
  91          }
  92  
  93          if (!@mysqli_select_db($this->conn->getResource(), $this->dbname)) {
  94              throw new SQLException('No database selected');
  95          }
  96  
  97          // Primary Keys
  98          $res = mysqli_query($this->conn->getResource(), "SHOW KEYS FROM " . $this->name);
  99  
 100          // Loop through the returned results, grouping the same key_name together
 101          // adding each column for that key.
 102          while($row = mysqli_fetch_assoc($res)) {
 103              $name = $row["Column_name"];
 104              if (!isset($this->primaryKey)) {
 105                  $this->primaryKey = new PrimaryKeyInfo($name);
 106              }
 107  
 108              $this->primaryKey->addColumn($this->columns[ $name ]);
 109          }
 110  
 111          $this->pkLoaded = true;
 112      }
 113  
 114      /** Loads the indexes for this table. */
 115      protected function initIndexes() {
 116          require_once 'creole/metadata/IndexInfo.php';
 117  
 118          // columns have to be loaded first
 119          if (!$this->colsLoaded) {
 120              $this->initColumns();
 121          }
 122  
 123          if (!@mysqli_select_db($this->conn->getResource(), $this->dbname)) {
 124              throw new SQLException('No database selected');
 125          }
 126  
 127          // Indexes
 128          $res = mysqli_query($this->conn->getResource(), "SHOW INDEX FROM " . $this->name);
 129  
 130          // Loop through the returned results, grouping the same key_name together
 131          // adding each column for that key.
 132          while($row = mysqli_fetch_assoc($res)) {
 133              $name = $row["Column_name"];
 134  
 135              if (!isset($this->indexes[$name])) {
 136                  $this->indexes[$name] = new IndexInfo($name);
 137              }
 138  
 139              $this->indexes[$name]->addColumn($this->columns[ $name ]);
 140          }
 141  
 142          $this->indexesLoaded = true;
 143      }
 144  
 145      /** Load foreign keys (unsupported in MySQL). */
 146      protected function initForeignKeys() {
 147          // columns have to be loaded first
 148          if (!$this->colsLoaded) {
 149              $this->initColumns();
 150          }
 151  
 152          // Foreign keys are not supported in mysql.
 153          $this->fksLoaded = true;
 154      }
 155  }


Généré le : Fri Mar 16 22:42:14 2007 par Balluche grâce à PHPXref 0.7