nyroFwk  0.2
mysql.class.php
Go to the documentation of this file.
1 <?php
11 
17  protected $tables;
18 
25  public function getTables($unPrefix = true) {
26  $cache = $this->getCache();
27  if (!$cache->get($this->tables, array('id'=>'tables'))) {
28  if (is_null($this->tables)) {
29  $this->tables = array();
30  $stmt = $this->query('SHOW TABLES');
31  $this->tables['raw'] = $stmt->fetchAll(db::FETCH_COLUMN);
32  $this->tables['unprefix'] = array();
33  foreach($this->tables['raw'] as $r) {
34  $this->tables['unprefix'][$r] = $this->cfg->prefix ? str_replace($this->cfg->prefix, '', $r) : $r;
35  }
36  }
37  $cache->save();
38  }
39  return $this->tables[$unPrefix ? 'raw' : 'unprefix'];
40  }
41 
48  public function prefixTable($table) {
49  if ($this->cfg->prefix) {
50  $tables = $this->getTables(false);
51  $index = array_search($table, $tables);
52  if ($index)
53  $table = $index;
54  }
55  return $table;
56  }
57 
64  public function fields($table) {
65  $cache = $this->getCache();
66  $ret = array();
67  if (!$cache->get($ret, array('id'=>'fields-'.$table))) {
68  $stmt = $this->query('SHOW FULL FIELDS FROM '.$this->quoteIdentifier($this->prefixTable($table)));
69 
70  $fField = 'Field';
71  $fType = 'Type';
72  $fNull = 'Null';
73  $fKey = 'Key';
74  $fDefault = 'Default';
75  $fExtra = 'Extra';
76  $fComment = 'Comment';
77 
78  $i = 0;
79  $p = 0;
80  while($row = $stmt->fetch(db::FETCH_ASSOC)) {
81  $length = null;
82  $precision = null;
83  $unsigned = preg_match('/unsigned/', $row[$fType]);
84  $text = false;
85  $auto = false;
86  $htmlOut = true;
87  $tmp = explode($this->cfg->sepCom, $row[$fComment]);
88  $comment = array();
89  $unique = false;
90  foreach($tmp as $t) {
91  $tt = explode($this->cfg->sepComVal, $t);
92  if (count($tt) == 2)
93  $comment[$tt[0]] = $tt[1];
94  else
95  $comment[] = $t;
96  }
97  if (preg_match('/^(set|enum)\((.+)\)/', $row[$fType], $matches)) {
98  $row[$fType] = $matches[1];
99  $tmp = explode(',',$matches[2]);
100  array_walk($tmp, create_function('&$t','$t = substr(substr($t, 0, strlen($t)-1), 1);'));
101  $precision = array();
102  foreach($tmp as $v)
103  $precision[$v] = $v;
104  } else if (preg_match('/^((?:var)?char)\((\d+)\)/', $row[$fType], $matches)) {
105  if (substr($row[$fField], -5) == '_file') {
106  $row[$fType] = 'file';
107  $htmlOut = false;
108  }else {
109  $row[$fType] = $matches[1];
110  $length = $matches[2];
111  $text = true;
112  }
113  } else if (preg_match('/^(decimal|float|double|decimal|dec|numeric|fixed)\((\d+),(\d+)\)/', $row[$fType], $matches)) {
114  $row[$fType] = $matches[1];
115  $length = $matches[2];
116  $precision = $matches[3];
117  } else if (preg_match('/^((?:big|medium|small|tiny)?int)\((\d+)\)/', $row[$fType], $matches)) {
118  $row[$fType] = $matches[1];
119  $length = $matches[2];
120  } else if (preg_match('/^((?:tiny|medium|long)?(text|blob))/', $row[$fType], $matches)) {
121  $text = true;
122  $htmlOut = !in_array('richtext', $comment);
123  }
124  if (strtoupper($row[$fKey]) == 'PRI') {
125  $primary = true;
126  $primaryPos = $p++;
127  $identity = ($row[$fExtra] == 'auto_increment');
128  } else if (strtoupper($row[$fKey]) == 'UNI') {
129  $primary = false;
130  $primaryPos = null;
131  $identity = false;
132  $unique = true;
133  } else {
134  $primary = false;
135  $primaryPos = null;
136  $identity = false;
137  }
138  if (strtoupper($row[$fDefault]) == 'CURRENT_TIMESTAMP') {
139  $cf = create_function('', 'return time();');
140  $row[$fDefault] = $cf();
141  $auto = true;
142  }
143  $ret[$row[$fField]] = array(
144  'name' => $row[$fField],
145  'pos' => $i++,
146  'type' => $row[$fType],
147  'default' => $row[$fDefault],
148  'required' => ($row[$fNull] == 'NO'),
149  'length' => $length,
150  'precision' => $precision,
151  'unsigned' => $unsigned,
152  'primary' => $primary,
153  'primaryPos' => $primaryPos,
154  'identity' => $identity,
155  'unique' => $unique,
156  'text' => $text,
157  'auto' => $auto,
158  'htmlOut' => $htmlOut,
159  'comment' => $comment,
160  'rawComment' => $row[$fComment],
161  );
162  }
163  $cache->save();
164  }
165  return $ret;
166  }
167 
168 }
count(array $prm)
fields($table)
Definition: mysql.class.php:64
prefixTable($table)
Definition: mysql.class.php:48
getTables($unPrefix=true)
Definition: mysql.class.php:25
query($sql, array $bind=array())
quoteIdentifier($ident)
const FETCH_ASSOC
Definition: db.class.php:12
const FETCH_COLUMN
Definition: db.class.php:16
Generated on Sun Oct 15 2017 22:25:20 for nyroFwk by doxygen 1.8.13