KumbiaPHP beta2-dev
Framework PHP en español
|
00001 <?php 00002 // @see DbPdo Padre de Drivers Pdo 00003 require_once CORE_PATH . 'libs/db/adapters/pdo.php'; 00004 00026 class DbPdoMySQL extends DbPDO { 00027 00031 protected $db_rbdm = "mysql"; 00032 00038 protected $db_port = 3306; 00039 00044 const TYPE_INTEGER = "INTEGER"; 00045 00050 const TYPE_DATE = "DATE"; 00051 00056 const TYPE_VARCHAR = "VARCHAR"; 00057 00062 const TYPE_DECIMAL = "DECIMAL"; 00063 00068 const TYPE_DATETIME = "DATETIME"; 00069 00074 const TYPE_CHAR = "CHAR"; 00075 00080 public function initialize(){ 00081 00082 } 00083 00090 public function table_exists($table, $schema=''){ 00091 $table = addslashes("$table"); 00092 if($schema==''){ 00093 $num = $this->fetch_one("SELECT COUNT(*) FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_NAME = '$table'"); 00094 } else { 00095 $schema = addslashes("$schema"); 00096 $num = $this->fetch_one("SELECT COUNT(*) FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_NAME = '$table' AND TABLE_SCHEMA = '$schema'"); 00097 } 00098 return $num[0]; 00099 } 00100 00107 public function limit($sql){ 00108 $params = Util::getParams(func_get_args()); 00109 $sql_new = $sql; 00110 00111 if(isset($params['limit']) && is_numeric($params['limit'])){ 00112 $sql_new.=" LIMIT $params[limit]"; 00113 } 00114 00115 if(isset($params['offset']) && is_numeric($params['offset'])){ 00116 $sql_new.=" OFFSET $params[offset]"; 00117 } 00118 00119 return $sql_new; 00120 } 00121 00128 public function drop_table($table, $if_exists=true){ 00129 if($if_exists){ 00130 return $this->query("DROP TABLE IF EXISTS $table"); 00131 } else { 00132 return $this->query("DROP TABLE $table"); 00133 } 00134 } 00135 00149 public function create_table($table, $definition, $index=array()){ 00150 $create_sql = "CREATE TABLE $table ("; 00151 if(!is_array($definition)){ 00152 new KumbiaException("Definición invalida para crear la tabla '$table'"); 00153 return false; 00154 } 00155 $create_lines = array(); 00156 $index = array(); 00157 $unique_index = array(); 00158 $primary = array(); 00159 $not_null = ""; 00160 $size = ""; 00161 foreach($definition as $field => $field_def){ 00162 if(isset($field_def['not_null'])){ 00163 $not_null = $field_def['not_null'] ? 'NOT NULL' : ''; 00164 } else { 00165 $not_null = ""; 00166 } 00167 if(isset($field_def['size'])){ 00168 $size = $field_def['size'] ? '('.$field_def['size'].')' : ''; 00169 } else { 00170 $size = ""; 00171 } 00172 if(isset($field_def['index'])){ 00173 if($field_def['index']){ 00174 $index[] = "INDEX(`$field`)"; 00175 } 00176 } 00177 if(isset($field_def['unique_index'])){ 00178 if($field_def['unique_index']){ 00179 $index[] = "UNIQUE(`$field`)"; 00180 } 00181 } 00182 if(isset($field_def['primary'])){ 00183 if($field_def['primary']){ 00184 $primary[] = "`$field`"; 00185 } 00186 } 00187 if(isset($field_def['auto'])){ 00188 if($field_def['auto']){ 00189 $field_def['extra'] = isset($field_def['extra']) ? $field_def['extra']." AUTO_INCREMENT" : "AUTO_INCREMENT"; 00190 } 00191 } 00192 if(isset($field_def['extra'])){ 00193 $extra = $field_def['extra']; 00194 } else { 00195 $extra = ""; 00196 } 00197 $create_lines[] = "`$field` ".$field_def['type'].$size.' '.$not_null.' '.$extra; 00198 } 00199 $create_sql.= join(',', $create_lines); 00200 $last_lines = array(); 00201 if(count($primary)){ 00202 $last_lines[] = 'PRIMARY KEY('.join(",", $primary).')'; 00203 } 00204 if(count($index)){ 00205 $last_lines[] = join(',', $index); 00206 } 00207 if(count($unique_index)){ 00208 $last_lines[] = join(',', $unique_index); 00209 } 00210 if(count($last_lines)){ 00211 $create_sql.= ','.join(',', $last_lines).')'; 00212 } 00213 return $this->query($create_sql); 00214 00215 } 00216 00222 public function list_tables(){ 00223 return $this->fetch_all("SHOW TABLES"); 00224 } 00225 00232 public function describe_table($table, $schema=''){ 00233 if($schema==''){ 00234 $describe = $this->fetch_all("DESCRIBE `$table`"); 00235 } else { 00236 $describe = $this->fetch_all("DESCRIBE `$schema`.`$table`"); 00237 } 00238 $final_describe = array(); 00239 foreach($describe as $key => $value){ 00240 $final_describe[] = array( 00241 "Field" => $value["field"], 00242 "Type" => $value["type"], 00243 "Null" => $value["null"], 00244 "Key" => $value["key"] 00245 ); 00246 } 00247 return $final_describe; 00248 } 00249 00250 }