00001 <?php
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022 require_once 'creole/metadata/TableInfo.php';
00023
00031 class MySQLiTableInfo extends TableInfo {
00033 protected function initColumns()
00034 {
00035 require_once 'creole/metadata/ColumnInfo.php';
00036 require_once 'creole/drivers/mysql/MySQLTypes.php';
00037
00038
00039
00040 $res = mysqli_query($this->conn->getResource(), "SHOW COLUMNS FROM " . $this->name);
00041
00042 $defaults = array();
00043 $nativeTypes = array();
00044 $precisions = array();
00045
00046 while($row = mysqli_fetch_assoc($res)) {
00047 $name = $row['Field'];
00048 $default = $row['Default'];
00049 $is_nullable = ($row['Null'] == 'YES');
00050
00051 $size = null;
00052 $precision = null;
00053 $scale = null;
00054
00055 if (preg_match('/^(\w+)[\(]?([\d,]*)[\)]?( |$)/', $row['Type'], $matches)) {
00056
00057 $nativeType = $matches[1];
00058 if ($matches[2]) {
00059 if ( ($cpos = strpos($matches[2], ',')) !== false) {
00060 $size = (int) substr($matches[2], 0, $cpos);
00061 $precision = $size;
00062 $scale = (int) substr($matches[2], $cpos + 1);
00063 } else {
00064 $size = (int) $matches[2];
00065 }
00066 }
00067 } elseif (preg_match('/^(\w+)\(/', $row['Type'], $matches)) {
00068 $nativeType = $matches[1];
00069 } else {
00070 $nativeType = $row['Type'];
00071 }
00072
00073 $this->columns[$name] = new ColumnInfo($this, $name, MySQLTypes::getType($nativeType), $nativeType, $size, $precision, $scale, $is_nullable, $default);
00074 }
00075
00076 $this->colsLoaded = true;
00077 }
00078
00080 protected function initPrimaryKey()
00081 {
00082 require_once 'creole/metadata/PrimaryKeyInfo.php';
00083
00084
00085 if (!$this->colsLoaded) {
00086 $this->initColumns();
00087 }
00088
00089
00090 $res = mysqli_query($this->conn->getResource(), "SHOW KEYS FROM " . $this->name);
00091
00092
00093
00094 while($row = mysqli_fetch_assoc($res)) {
00095 $name = $row["Column_name"];
00096 if (!isset($this->primaryKey)) {
00097 $this->primaryKey = new PrimaryKeyInfo($name);
00098 }
00099
00100 $this->primaryKey->addColumn($this->columns[ $name ]);
00101 }
00102
00103 $this->pkLoaded = true;
00104 }
00105
00107 protected function initIndexes() {
00108 require_once 'creole/metadata/IndexInfo.php';
00109
00110
00111 if (!$this->colsLoaded) {
00112 $this->initColumns();
00113 }
00114
00115
00116 $res = mysqli_query($this->conn->getResource(), "SHOW INDEX FROM " . $this->name);
00117
00118
00119
00120 while($row = mysqli_fetch_assoc($res)) {
00121 $name = $row["Column_name"];
00122
00123 if (!isset($this->indexes[$name])) {
00124 $this->indexes[$name] = new IndexInfo($name);
00125 }
00126
00127 $this->indexes[$name]->addColumn($this->columns[ $name ]);
00128 }
00129
00130 $this->indexesLoaded = true;
00131 }
00132
00134 protected function initForeignKeys() {
00135
00136 if (!$this->colsLoaded) {
00137 $this->initColumns();
00138 }
00139
00140
00141 $this->fksLoaded = true;
00142 }
00143 }