MySQLTableInfo.php

Go to the documentation of this file.
00001 <?php 00002 /* 00003 * $Id: MySQLTableInfo.php,v 1.5 2004/05/02 21:05:29 micha 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 00032 class MySQLTableInfo extends TableInfo 00033 { 00038 function initColumns() 00039 { 00040 include_once 'creole/metadata/ColumnInfo.php'; 00041 include_once 'creole/drivers/mysql/MySQLTypes.php'; 00042 00043 if (!@mysql_select_db($this->dbname, $this->dblink)) { 00044 return new SQLException(CREOLE_ERROR_NODBSELECTED, 'No database selected'); 00045 } 00046 00047 // To get all of the attributes we need, we use 00048 // the MySQL "SHOW COLUMNS FROM $tablename" SQL. We cannot 00049 // use the API functions (e.g. mysql_list_fields() because they 00050 // do not return complete information -- e.g. precision / scale, default 00051 // values). 00052 00053 $res = mysql_query("SHOW COLUMNS FROM " . $this->name, $this->dblink); 00054 /* 00055 $defaults = array(); 00056 $nativeTypes = array(); 00057 $precisions = array(); 00058 */ 00059 while($row = mysql_fetch_assoc($res)) 00060 { 00061 $name = $row['Field']; 00062 $default = $row['Default']; 00063 $is_nullable = ($row['Null'] == 'YES'); 00064 00065 $size = null; 00066 $precision = null; 00067 00068 if (preg_match('/^(\w+)[\(]?([\d,]*)[\)]?( |$)/', $row['Type'], $matches)) { 00069 // colname[1] size/precision[2] 00070 $nativeType = $matches[1]; 00071 if ($matches[2]) { 00072 if ( ($cpos = strpos($matches[2], ',')) !== false) { 00073 $size = (int) substr($matches[2], 0, $cpos); 00074 $precision = (int) substr($matches[2], $cpos + 1); 00075 } else { 00076 $size = (int) $matches[2]; 00077 } 00078 } 00079 } elseif (preg_match('/^(\w+)\(/', $row['Type'], $matches)) { 00080 $nativeType = $matches[1]; 00081 } else { 00082 $nativeType = $row['Type']; 00083 } 00084 00085 $this->columns[$name] = new ColumnInfo($this, $name, MySQLTypes::getType($nativeType), $nativeType, $size, $precision, $is_nullable, $default); 00086 } 00087 00088 $this->colsLoaded = true; 00089 return true; 00090 } 00091 00097 function initPrimaryKey() 00098 { 00099 include_once 'creole/metadata/PrimaryKeyInfo.php'; 00100 00101 // columns have to be loaded first 00102 if (!$this->colsLoaded) { 00103 if (($e = $this->initColumns()) !== true) { 00104 return $e; 00105 } 00106 } 00107 00108 if (!@mysql_select_db($this->dbname, $this->dblink)) { 00109 return new SQLException(CREOLE_ERROR_NODBSELECTED, 'No database selected'); 00110 } 00111 00112 // Primary Keys 00113 $res = mysql_query("SHOW KEYS FROM " . $this->name, $this->dblink); 00114 00115 // Loop through the returned results, grouping the same key_name together 00116 // adding each column for that key. 00117 00118 while($row = mysql_fetch_assoc($res)) { 00119 $name = $row["Column_name"]; 00120 if (!isset($this->primaryKey)) { 00121 $this->primaryKey =& new PrimaryKeyInfo($name); 00122 } 00123 $this->primaryKey->addColumn($this->columns[ $name ]); 00124 } 00125 00126 00127 $this->pkLoaded = true; 00128 return true; 00129 } 00130 00136 function initIndexes() 00137 { 00138 include_once 'creole/metadata/IndexInfo.php'; 00139 00140 // columns have to be loaded first 00141 if (!$this->colsLoaded) $this->initColumns(); 00142 00143 if (!@mysql_select_db($this->dbname, $this->dblink)) { 00144 return new SQLException(CREOLE_ERROR_NODBSELECTED, 'No database selected'); 00145 } 00146 00147 // Indexes 00148 $res = mysql_query("SHOW INDEX FROM " . $this->name, $this->dblink); 00149 00150 // Loop through the returned results, grouping the same key_name together 00151 // adding each column for that key. 00152 00153 while($row = mysql_fetch_assoc($res)) { 00154 $name = $row["Column_name"]; 00155 if (!isset($this->indexes[$name])) { 00156 $this->indexes[$name] = new IndexInfo($name); 00157 } 00158 $this->indexes[$name]->addColumn($this->columns[ $name ]); 00159 } 00160 00161 $this->indexesLoaded = true; 00162 return true; 00163 } 00164 00166 function initForeignKeys() 00167 { 00168 // columns have to be loaded first 00169 if (!$this->colsLoaded) { 00170 if (($e = $this->initColumns()) !== true) { 00171 return $e; 00172 } 00173 } 00174 00175 // Foreign keys are not supported in mysql. 00176 00177 $this->fksLoaded = true; 00178 return true; 00179 } 00180 00181 }

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


Copyright © 2004 Hans Lellelid  
Creole[php4] CVS