00001 <?php 00023 class DbPdoMySQL extends DbPDO { 00024 00028 protected $db_rbdm = "mysql"; 00029 00035 protected $db_port = 3306; 00036 00041 const TYPE_INTEGER = "INTEGER"; 00042 00047 const TYPE_DATE = "DATE"; 00048 00053 const TYPE_VARCHAR = "VARCHAR"; 00054 00059 const TYPE_DECIMAL = "DECIMAL"; 00060 00065 const TYPE_DATETIME = "DATETIME"; 00066 00071 const TYPE_CHAR = "CHAR"; 00072 00077 public function initialize(){ 00078 00079 } 00080 00087 public function table_exists($table, $schema=''){ 00088 $table = addslashes("$table"); 00089 if($schema==''){ 00090 $num = $this->fetch_one("SELECT COUNT(*) FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_NAME = '$table'"); 00091 } else { 00092 $schema = addslashes("$schema"); 00093 $num = $this->fetch_one("SELECT COUNT(*) FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_NAME = '$table' AND TABLE_SCHEMA = '$schema'"); 00094 } 00095 return $num[0]; 00096 } 00097 00104 public function limit($sql){ 00105 $params = Util::getParams(func_get_args()); 00106 $sql_new = $sql; 00107 00108 if(isset($params['limit']) && is_numeric($params['limit'])){ 00109 $sql_new.=" LIMIT $params[limit]"; 00110 } 00111 00112 if(isset($params['offset']) && is_numeric($params['offset'])){ 00113 $sql_new.=" OFFSET $params[offset]"; 00114 } 00115 00116 return $sql_new; 00117 } 00118 00125 public function drop_table($table, $if_exists=true){ 00126 if($if_exists){ 00127 return $this->query("DROP TABLE IF EXISTS $table"); 00128 } else { 00129 return $this->query("DROP TABLE $table"); 00130 } 00131 } 00132 00146 public function create_table($table, $definition, $index=array()){ 00147 $create_sql = "CREATE TABLE $table ("; 00148 if(!is_array($definition)){ 00149 new KumbiaException("Definición invalida para crear la tabla '$table'"); 00150 return false; 00151 } 00152 $create_lines = array(); 00153 $index = array(); 00154 $unique_index = array(); 00155 $primary = array(); 00156 $not_null = ""; 00157 $size = ""; 00158 foreach($definition as $field => $field_def){ 00159 if(isset($field_def['not_null'])){ 00160 $not_null = $field_def['not_null'] ? 'NOT NULL' : ''; 00161 } else { 00162 $not_null = ""; 00163 } 00164 if(isset($field_def['size'])){ 00165 $size = $field_def['size'] ? '('.$field_def['size'].')' : ''; 00166 } else { 00167 $size = ""; 00168 } 00169 if(isset($field_def['index'])){ 00170 if($field_def['index']){ 00171 $index[] = "INDEX(`$field`)"; 00172 } 00173 } 00174 if(isset($field_def['unique_index'])){ 00175 if($field_def['unique_index']){ 00176 $index[] = "UNIQUE(`$field`)"; 00177 } 00178 } 00179 if(isset($field_def['primary'])){ 00180 if($field_def['primary']){ 00181 $primary[] = "`$field`"; 00182 } 00183 } 00184 if(isset($field_def['auto'])){ 00185 if($field_def['auto']){ 00186 $field_def['extra'] = isset($field_def['extra']) ? $field_def['extra']." AUTO_INCREMENT" : "AUTO_INCREMENT"; 00187 } 00188 } 00189 if(isset($field_def['extra'])){ 00190 $extra = $field_def['extra']; 00191 } else { 00192 $extra = ""; 00193 } 00194 $create_lines[] = "`$field` ".$field_def['type'].$size.' '.$not_null.' '.$extra; 00195 } 00196 $create_sql.= join(',', $create_lines); 00197 $last_lines = array(); 00198 if(count($primary)){ 00199 $last_lines[] = 'PRIMARY KEY('.join(",", $primary).')'; 00200 } 00201 if(count($index)){ 00202 $last_lines[] = join(',', $index); 00203 } 00204 if(count($unique_index)){ 00205 $last_lines[] = join(',', $unique_index); 00206 } 00207 if(count($last_lines)){ 00208 $create_sql.= ','.join(',', $last_lines).')'; 00209 } 00210 return $this->query($create_sql); 00211 00212 } 00213 00219 public function list_tables(){ 00220 return $this->fetch_all("SHOW TABLES"); 00221 } 00222 00229 public function describe_table($table, $schema=''){ 00230 if($schema==''){ 00231 $describe = $this->fetch_all("DESCRIBE `$table`"); 00232 } else { 00233 $describe = $this->fetch_all("DESCRIBE `$schema`.`$table`"); 00234 } 00235 $final_describe = array(); 00236 foreach($describe as $key => $value){ 00237 $final_describe[] = array( 00238 "Field" => $value["field"], 00239 "Type" => $value["type"], 00240 "Null" => $value["null"], 00241 "Key" => $value["key"] 00242 ); 00243 } 00244 return $final_describe; 00245 } 00246 00247 }