MySQLTableInfo.php

Go to the documentation of this file.
00001 <?php 00002 /* 00003 * $Id: MySQLTableInfo.php,v 1.11 2004/05/01 03:17:27 hlellelid Exp $ 00004 * 00005 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 00006 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 00007 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 00008 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 00009 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 00010 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 00011 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 00012 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 00013 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 00014 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 00015 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 00016 * 00017 * This software consists of voluntary contributions made by many individuals 00018 * and is licensed under the LGPL. For more information please see 00019 * <http://creole.phpdb.org>. 00020 */ 00021 00022 require_once 'creole/metadata/TableInfo.php'; 00023 00031 class MySQLTableInfo extends TableInfo { 00032 00034 protected function initColumns() 00035 { 00036 00037 include_once 'creole/metadata/ColumnInfo.php'; 00038 include_once 'creole/drivers/mysql/MySQLTypes.php'; 00039 00040 if (!@mysql_select_db($this->dbname, $this->dblink)) { 00041 throw new SQLException('No database selected'); 00042 } 00043 00044 // To get all of the attributes we need, we use 00045 // the MySQL "SHOW COLUMNS FROM $tablename" SQL. We cannot 00046 // use the API functions (e.g. mysql_list_fields() because they 00047 // do not return complete information -- e.g. precision / scale, default 00048 // values). 00049 00050 $res = mysql_query("SHOW COLUMNS FROM " . $this->name, $this->dblink); 00051 00052 $defaults = array(); 00053 $nativeTypes = array(); 00054 $precisions = array(); 00055 00056 while($row = mysql_fetch_assoc($res)) { 00057 $name = $row['Field']; 00058 $default = $row['Default']; 00059 $is_nullable = ($row['Null'] == 'YES'); 00060 00061 $size = null; 00062 $precision = null; 00063 00064 if (preg_match('/^(\w+)[\(]?([\d,]*)[\)]?( |$)/', $row['Type'], $matches)) { 00065 // colname[1] size/precision[2] 00066 $nativeType = $matches[1]; 00067 if ($matches[2]) { 00068 if ( ($cpos = strpos($matches[2], ',')) !== false) { 00069 $size = (int) substr($matches[2], 0, $cpos); 00070 $precision = (int) substr($matches[2], $cpos + 1); 00071 } else { 00072 $size = (int) $matches[2]; 00073 } 00074 } 00075 } elseif (preg_match('/^(\w+)\(/', $row['Type'], $matches)) { 00076 $nativeType = $matches[1]; 00077 } else { 00078 $nativeType = $row['Type']; 00079 } 00080 00081 $this->columns[$name] = new ColumnInfo($this, $name, MySQLTypes::getType($nativeType), $nativeType, $size, $precision, $is_nullable, $default); 00082 } 00083 00084 $this->colsLoaded = true; 00085 } 00086 00088 protected function initPrimaryKey() 00089 { 00090 include_once 'creole/metadata/PrimaryKeyInfo.php'; 00091 00092 // columns have to be loaded first 00093 if (!$this->colsLoaded) $this->initColumns(); 00094 00095 if (!@mysql_select_db($this->dbname, $this->dblink)) { 00096 throw new SQLException('No database selected'); 00097 } 00098 00099 // Primary Keys 00100 $res = mysql_query("SHOW KEYS FROM " . $this->name, $this->dblink); 00101 00102 // Loop through the returned results, grouping the same key_name together 00103 // adding each column for that key. 00104 00105 while($row = mysql_fetch_assoc($res)) { 00106 $name = $row["Column_name"]; 00107 if (!isset($this->primaryKey)) { 00108 $this->primaryKey = new PrimaryKeyInfo($name); 00109 } 00110 $this->primaryKey->addColumn($this->columns[ $name ]); 00111 } 00112 00113 00114 $this->pkLoaded = true; 00115 } 00116 00118 protected function initIndexes() { 00119 00120 include_once 'creole/metadata/IndexInfo.php'; 00121 00122 // columns have to be loaded first 00123 if (!$this->colsLoaded) $this->initColumns(); 00124 00125 if (!@mysql_select_db($this->dbname, $this->dblink)) { 00126 throw new SQLException('No database selected'); 00127 } 00128 00129 // Indexes 00130 $res = mysql_query("SHOW INDEX FROM " . $this->name, $this->dblink); 00131 00132 // Loop through the returned results, grouping the same key_name together 00133 // adding each column for that key. 00134 00135 while($row = mysql_fetch_assoc($res)) { 00136 $name = $row["Column_name"]; 00137 if (!isset($this->indexes[$name])) { 00138 $this->indexes[$name] = new IndexInfo($name); 00139 } 00140 $this->indexes[$name]->addColumn($this->columns[ $name ]); 00141 } 00142 00143 $this->indexesLoaded = true; 00144 } 00145 00147 protected function initForeignKeys() { 00148 00149 // columns have to be loaded first 00150 if (!$this->colsLoaded) $this->initColumns(); 00151 00152 // Foreign keys are not supported in mysql. 00153 00154 $this->fksLoaded = true; 00155 } 00156 00157 }

This file is part of the Creole[php5] library.


Copyright © 2004 Hans Lellelid  
Creole[php5] CVS