00001 <?php
00010 class db_pdo_mysql extends db_pdo_abstract {
00011
00017 protected $tables;
00018
00025 public function getTables($unPrefix = true) {
00026 $cache = $this->getCache();
00027 if (!$cache->get($this->tables, array('id'=>'tables'))) {
00028 if (is_null($this->tables)) {
00029 $this->tables = array();
00030 $stmt = $this->query('SHOW TABLES');
00031 $this->tables['raw'] = $stmt->fetchAll(db::FETCH_COLUMN);
00032 $this->tables['unprefix'] = array();
00033 foreach($this->tables['raw'] as $r) {
00034 $this->tables['unprefix'][$r] = $this->cfg->prefix ? str_replace($this->cfg->prefix, '', $r) : $r;
00035 }
00036 }
00037 $cache->save();
00038 }
00039 return $this->tables[$unPrefix ? 'raw' : 'unprefix'];
00040 }
00041
00048 public function prefixTable($table) {
00049 if ($this->cfg->prefix) {
00050 $tables = $this->getTables(false);
00051 $index = array_search($table, $tables);
00052 if ($index)
00053 $table = $index;
00054 }
00055 return $table;
00056 }
00057
00064 public function fields($table) {
00065 $cache = $this->getCache();
00066 $ret = array();
00067 if (!$cache->get($ret, array('id'=>'fields-'.$table))) {
00068 $stmt = $this->query('SHOW FULL FIELDS FROM '.$this->quoteIdentifier($this->prefixTable($table)));
00069
00070 $fField = 'Field';
00071 $fType = 'Type';
00072 $fNull = 'Null';
00073 $fKey = 'Key';
00074 $fDefault = 'Default';
00075 $fExtra = 'Extra';
00076 $fComment = 'Comment';
00077
00078 $i = 0;
00079 $p = 0;
00080 while($row = $stmt->fetch(db::FETCH_ASSOC)) {
00081 $length = null;
00082 $precision = null;
00083 $unsigned = preg_match('/unsigned/', $row[$fType]);
00084 $text = false;
00085 $auto = false;
00086 $htmlOut = true;
00087 $tmp = explode($this->cfg->sepCom, $row[$fComment]);
00088 $comment = array();
00089 $unique = false;
00090 foreach($tmp as $t) {
00091 $tt = explode($this->cfg->sepComVal, $t);
00092 if (count($tt) == 2)
00093 $comment[$tt[0]] = $tt[1];
00094 else
00095 $comment[] = $t;
00096 }
00097 if (preg_match('/^(set|enum)\((.+)\)/', $row[$fType], $matches)) {
00098 $row[$fType] = $matches[1];
00099 $tmp = explode(',',$matches[2]);
00100 array_walk($tmp, create_function('&$t','$t = substr(substr($t, 0, strlen($t)-1), 1);'));
00101 $precision = array();
00102 foreach($tmp as $v)
00103 $precision[$v] = $v;
00104 } else if (preg_match('/^((?:var)?char)\((\d+)\)/', $row[$fType], $matches)) {
00105 if (substr($row[$fField], -5) == '_file') {
00106 $row[$fType] = 'file';
00107 $htmlOut = false;
00108 }else {
00109 $row[$fType] = $matches[1];
00110 $length = $matches[2];
00111 $text = true;
00112 }
00113 } else if (preg_match('/^(decimal|float|double|decimal|dec|numeric|fixed)\((\d+),(\d+)\)/', $row[$fType], $matches)) {
00114 $row[$fType] = $matches[1];
00115 $length = $matches[2];
00116 $precision = $matches[3];
00117 } else if (preg_match('/^((?:big|medium|small|tiny)?int)\((\d+)\)/', $row[$fType], $matches)) {
00118 $row[$fType] = $matches[1];
00119 $length = $matches[2];
00120 } else if (preg_match('/^((?:tiny|medium|long)?(text|blob))/', $row[$fType], $matches)) {
00121 $text = true;
00122 $htmlOut = !in_array('richtext', $comment);
00123 }
00124 if (strtoupper($row[$fKey]) == 'PRI') {
00125 $primary = true;
00126 $primaryPos = $p++;
00127 $identity = ($row[$fExtra] == 'auto_increment');
00128 } else if (strtoupper($row[$fKey]) == 'UNI') {
00129 $primary = false;
00130 $primaryPos = null;
00131 $identity = false;
00132 $unique = true;
00133 } else {
00134 $primary = false;
00135 $primaryPos = null;
00136 $identity = false;
00137 }
00138 if (strtoupper($row[$fDefault]) == 'CURRENT_TIMESTAMP') {
00139 $cf = create_function('', 'return time();');
00140 $row[$fDefault] = $cf();
00141 $auto = true;
00142 }
00143 $ret[$row[$fField]] = array(
00144 'name' => $row[$fField],
00145 'pos' => $i++,
00146 'type' => $row[$fType],
00147 'default' => $row[$fDefault],
00148 'required' => ($row[$fNull] == 'NO'),
00149 'length' => $length,
00150 'precision' => $precision,
00151 'unsigned' => $unsigned,
00152 'primary' => $primary,
00153 'primaryPos' => $primaryPos,
00154 'identity' => $identity,
00155 'unique' => $unique,
00156 'text' => $text,
00157 'auto' => $auto,
00158 'htmlOut' => $htmlOut,
00159 'comment' => $comment,
00160 'rawComment' => $row[$fComment],
00161 );
00162 }
00163 $cache->save();
00164 }
00165 return $ret;
00166 }
00167
00168 }