KumbiaPHP  beta2
Framework PHP
 Todo Estructuras de Datos Namespaces Archivos Funciones Variables Páginas
mysql.php
Ir a la documentación de este archivo.
1 <?php
24 require_once CORE_PATH . 'libs/db/adapters/pdo.php';
25 
33 class DbPdoMySQL extends DbPDO
34 {
35 
39  protected $db_rbdm = "mysql";
45  protected $db_port = 3306;
46 
51  const TYPE_INTEGER = "INTEGER";
52 
57  const TYPE_DATE = "DATE";
58 
63  const TYPE_VARCHAR = "VARCHAR";
64 
69  const TYPE_DECIMAL = "DECIMAL";
70 
75  const TYPE_DATETIME = "DATETIME";
76 
81  const TYPE_CHAR = "CHAR";
82 
87  public function initialize()
88  {
89 
90  }
91 
98  public function table_exists($table, $schema='')
99  {
100  $table = addslashes("$table");
101  if ($schema == '') {
102  $num = $this->fetch_one("SELECT COUNT(*) FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_NAME = '$table'");
103  } else {
104  $schema = addslashes("$schema");
105  $num = $this->fetch_one("SELECT COUNT(*) FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_NAME = '$table' AND TABLE_SCHEMA = '$schema'");
106  }
107  return $num[0];
108  }
109 
116  public function limit($sql)
117  {
118  $params = Util::getParams(func_get_args());
119  $sql_new = $sql;
120 
121  if (isset($params['limit']) && is_numeric($params['limit'])) {
122  $sql_new.=" LIMIT $params[limit]";
123  }
124 
125  if (isset($params['offset']) && is_numeric($params['offset'])) {
126  $sql_new.=" OFFSET $params[offset]";
127  }
128 
129  return $sql_new;
130  }
131 
138  public function drop_table($table, $if_exists=true)
139  {
140  if ($if_exists) {
141  return $this->query("DROP TABLE IF EXISTS $table");
142  } else {
143  return $this->query("DROP TABLE $table");
144  }
145  }
146 
160  public function create_table($table, $definition, $index=array())
161  {
162  $create_sql = "CREATE TABLE $table (";
163  if (!is_array($definition)) {
164  new KumbiaException("Definici&oacute;n invalida para crear la tabla '$table'");
165  return false;
166  }
167  $create_lines = array();
168  $index = array();
169  $unique_index = array();
170  $primary = array();
171  //$not_null = "";
172  //$size = "";
173  foreach ($definition as $field => $field_def) {
174  if (isset($field_def['not_null'])) {
175  $not_null = $field_def['not_null'] ? 'NOT NULL' : '';
176  } else {
177  $not_null = "";
178  }
179  if (isset($field_def['size'])) {
180  $size = $field_def['size'] ? '(' . $field_def['size'] . ')' : '';
181  } else {
182  $size = "";
183  }
184  if (isset($field_def['index'])) {
185  if ($field_def['index']) {
186  $index[] = "INDEX(`$field`)";
187  }
188  }
189  if (isset($field_def['unique_index'])) {
190  if ($field_def['unique_index']) {
191  $index[] = "UNIQUE(`$field`)";
192  }
193  }
194  if (isset($field_def['primary'])) {
195  if ($field_def['primary']) {
196  $primary[] = "`$field`";
197  }
198  }
199  if (isset($field_def['auto'])) {
200  if ($field_def['auto']) {
201  $field_def['extra'] = isset($field_def['extra']) ? $field_def['extra'] . " AUTO_INCREMENT" : "AUTO_INCREMENT";
202  }
203  }
204  if (isset($field_def['extra'])) {
205  $extra = $field_def['extra'];
206  } else {
207  $extra = "";
208  }
209  $create_lines[] = "`$field` " . $field_def['type'] . $size . ' ' . $not_null . ' ' . $extra;
210  }
211  $create_sql.= join(',', $create_lines);
212  $last_lines = array();
213  if (count($primary)) {
214  $last_lines[] = 'PRIMARY KEY(' . join(",", $primary) . ')';
215  }
216  if (count($index)) {
217  $last_lines[] = join(',', $index);
218  }
219  if (count($unique_index)) {
220  $last_lines[] = join(',', $unique_index);
221  }
222  if (count($last_lines)) {
223  $create_sql.= ',' . join(',', $last_lines) . ')';
224  }
225  return $this->query($create_sql);
226  }
227 
233  public function list_tables()
234  {
235  return $this->fetch_all("SHOW TABLES");
236  }
237 
244  public function describe_table($table, $schema='')
245  {
246  if ($schema == '') {
247  $describe = $this->fetch_all("DESCRIBE `$table`");
248  } else {
249  $describe = $this->fetch_all("DESCRIBE `$schema`.`$table`");
250  }
251  $final_describe = array();
252  foreach ($describe as $key => $value) {
253  $final_describe[] = array(
254  "Field" => $value["field"],
255  "Type" => $value["type"],
256  "Null" => $value["null"],
257  "Key" => $value["key"]
258  );
259  }
260  return $final_describe;
261  }
262 
263 }