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
2 // @see DbPdo Padre de Drivers Pdo
3 require_once CORE_PATH . 'libs/db/adapters/pdo.php';
4 
26 class DbPdoMySQL extends DbPDO {
27 
31  protected $db_rbdm = "mysql";
32 
38  protected $db_port = 3306;
39 
44  const TYPE_INTEGER = "INTEGER";
45 
50  const TYPE_DATE = "DATE";
51 
56  const TYPE_VARCHAR = "VARCHAR";
57 
62  const TYPE_DECIMAL = "DECIMAL";
63 
68  const TYPE_DATETIME = "DATETIME";
69 
74  const TYPE_CHAR = "CHAR";
75 
80  public function initialize(){
81 
82  }
83 
90  public function table_exists($table, $schema=''){
91  $table = addslashes("$table");
92  if($schema==''){
93  $num = $this->fetch_one("SELECT COUNT(*) FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_NAME = '$table'");
94  } else {
95  $schema = addslashes("$schema");
96  $num = $this->fetch_one("SELECT COUNT(*) FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_NAME = '$table' AND TABLE_SCHEMA = '$schema'");
97  }
98  return $num[0];
99  }
100 
107  public function limit($sql){
108  $params = Util::getParams(func_get_args());
109  $sql_new = $sql;
110 
111  if(isset($params['limit']) && is_numeric($params['limit'])){
112  $sql_new.=" LIMIT $params[limit]";
113  }
114 
115  if(isset($params['offset']) && is_numeric($params['offset'])){
116  $sql_new.=" OFFSET $params[offset]";
117  }
118 
119  return $sql_new;
120  }
121 
128  public function drop_table($table, $if_exists=true){
129  if($if_exists){
130  return $this->query("DROP TABLE IF EXISTS $table");
131  } else {
132  return $this->query("DROP TABLE $table");
133  }
134  }
135 
149  public function create_table($table, $definition, $index=array()){
150  $create_sql = "CREATE TABLE $table (";
151  if(!is_array($definition)){
152  new KumbiaException("Definici&oacute;n invalida para crear la tabla '$table'");
153  return false;
154  }
155  $create_lines = array();
156  $index = array();
157  $unique_index = array();
158  $primary = array();
159  $not_null = "";
160  $size = "";
161  foreach($definition as $field => $field_def){
162  if(isset($field_def['not_null'])){
163  $not_null = $field_def['not_null'] ? 'NOT NULL' : '';
164  } else {
165  $not_null = "";
166  }
167  if(isset($field_def['size'])){
168  $size = $field_def['size'] ? '('.$field_def['size'].')' : '';
169  } else {
170  $size = "";
171  }
172  if(isset($field_def['index'])){
173  if($field_def['index']){
174  $index[] = "INDEX(`$field`)";
175  }
176  }
177  if(isset($field_def['unique_index'])){
178  if($field_def['unique_index']){
179  $index[] = "UNIQUE(`$field`)";
180  }
181  }
182  if(isset($field_def['primary'])){
183  if($field_def['primary']){
184  $primary[] = "`$field`";
185  }
186  }
187  if(isset($field_def['auto'])){
188  if($field_def['auto']){
189  $field_def['extra'] = isset($field_def['extra']) ? $field_def['extra']." AUTO_INCREMENT" : "AUTO_INCREMENT";
190  }
191  }
192  if(isset($field_def['extra'])){
193  $extra = $field_def['extra'];
194  } else {
195  $extra = "";
196  }
197  $create_lines[] = "`$field` ".$field_def['type'].$size.' '.$not_null.' '.$extra;
198  }
199  $create_sql.= join(',', $create_lines);
200  $last_lines = array();
201  if(count($primary)){
202  $last_lines[] = 'PRIMARY KEY('.join(",", $primary).')';
203  }
204  if(count($index)){
205  $last_lines[] = join(',', $index);
206  }
207  if(count($unique_index)){
208  $last_lines[] = join(',', $unique_index);
209  }
210  if(count($last_lines)){
211  $create_sql.= ','.join(',', $last_lines).')';
212  }
213  return $this->query($create_sql);
214 
215  }
216 
222  public function list_tables(){
223  return $this->fetch_all("SHOW TABLES");
224  }
225 
232  public function describe_table($table, $schema=''){
233  if($schema==''){
234  $describe = $this->fetch_all("DESCRIBE `$table`");
235  } else {
236  $describe = $this->fetch_all("DESCRIBE `$schema`.`$table`");
237  }
238  $final_describe = array();
239  foreach($describe as $key => $value){
240  $final_describe[] = array(
241  "Field" => $value["field"],
242  "Type" => $value["type"],
243  "Null" => $value["null"],
244  "Key" => $value["key"]
245  );
246  }
247  return $final_describe;
248  }
249 
250 }