00001 <?php 00023 class DbMySQL extends DbBase implements DbBaseInterface { 00024 00030 public $id_connection; 00031 00037 public $last_result_query; 00038 00044 private $last_query; 00045 00051 public $last_error; 00052 00057 const DB_ASSOC = MYSQL_ASSOC; 00058 00063 const DB_BOTH = MYSQL_BOTH; 00064 00069 const DB_NUM = MYSQL_NUM; 00070 00075 const TYPE_INTEGER = 'INTEGER'; 00076 00081 const TYPE_DATE = 'DATE'; 00082 00087 const TYPE_VARCHAR = 'VARCHAR'; 00088 00093 const TYPE_DECIMAL = 'DECIMAL'; 00094 00099 const TYPE_DATETIME = 'DATETIME'; 00100 00105 const TYPE_CHAR = 'CHAR'; 00106 00113 public function connect($config){ 00114 00115 if(!extension_loaded('mysql')){ 00116 throw new KumbiaException('Debe cargar la extensión de PHP llamada php_mysql'); 00117 return false; 00118 } 00119 if(!isset($config['port']) || !$config['port']) { 00120 $config['port'] = 3306; 00121 } 00122 00123 if($this->id_connection = mysql_connect("{$config['host']}:{$config['port']}", $config['username'], $config['password'], true)){ 00124 if($config['name']!=='') { 00125 if(!mysql_select_db($config['name'], $this->id_connection)){ 00126 throw new KumbiaException($this->error(), $this->no_error(), false); 00127 } 00128 } 00129 return true; 00130 } else { 00131 throw new KumbiaException($this->error(), $this->no_error(), false); 00132 } 00133 } 00134 00141 public function query($sql_query){ 00142 $this->debug($sql_query); 00143 if($this->logger){ 00144 Logger::debug($sql_query); 00145 } 00146 if(!$this->id_connection){ 00147 $this->connect(); 00148 if(!$this->id_connection){ 00149 return false; 00150 } 00151 } 00152 00153 $this->last_query = $sql_query; 00154 if($result_query = mysql_query($sql_query, $this->id_connection)){ 00155 $this->last_result_query = $result_query; 00156 return $result_query; 00157 } else { 00158 $this->last_result_query = false; 00159 throw new KumbiaException($this->error(" al ejecutar <i>\"$sql_query\"</i>"), $this->no_error()); 00160 return false; 00161 } 00162 } 00163 00167 public function close(){ 00168 if($this->id_connection) { 00169 return mysql_close(); 00170 } 00171 return false; 00172 } 00173 00181 public function fetch_array($result_query='', $opt=''){ 00182 if($opt==='') { 00183 $opt = db::DB_BOTH; 00184 } 00185 if(!$this->id_connection){ 00186 return false; 00187 } 00188 if(!$result_query){ 00189 $result_query = $this->last_result_query; 00190 if(!$result_query){ 00191 return false; 00192 } 00193 } 00194 return mysql_fetch_array($result_query, $opt); 00195 } 00196 00202 public function __construct($config){ 00203 $this->connect($config); 00204 } 00205 00209 public function num_rows($result_query=''){ 00210 if(!$this->id_connection){ 00211 return false; 00212 } 00213 if(!$result_query){ 00214 $result_query = $this->last_result_query; 00215 if(!$result_query){ 00216 return false; 00217 } 00218 } 00219 if(($number_rows = mysql_num_rows($result_query))!==false){ 00220 return $number_rows; 00221 } else { 00222 throw new KumbiaException($this->error(), $this->no_error()); 00223 return false; 00224 } 00225 return false; 00226 } 00227 00235 public function field_name($number, $result_query=''){ 00236 if(!$this->id_connection){ 00237 return false; 00238 } 00239 if(!$result_query){ 00240 $result_query = $this->last_result_query; 00241 if(!$result_query){ 00242 return false; 00243 } 00244 } 00245 if(($fieldName = mysql_field_name($result_query, $number))!==false){ 00246 return $fieldName; 00247 } else { 00248 throw new KumbiaException($this->error(), $this->no_error()); 00249 return false; 00250 } 00251 return false; 00252 } 00253 00254 00262 public function data_seek($number, $result_query=''){ 00263 if(!$result_query){ 00264 $result_query = $this->last_result_query; 00265 if(!$result_query){ 00266 return false; 00267 } 00268 } 00269 if(($success = mysql_data_seek($result_query, $number))!==false){ 00270 return $success; 00271 } else { 00272 throw new KumbiaException($this->error(), $this->no_error()); 00273 return false; 00274 } 00275 return false; 00276 } 00277 00284 public function affected_rows($result_query=''){ 00285 if(($numberRows = mysql_affected_rows())!==false){ 00286 return $numberRows; 00287 } else { 00288 $this->lastError = $this->error(); 00289 throw new KumbiaException($this->error(), $this->no_error()); 00290 return false; 00291 } 00292 return false; 00293 } 00294 00300 public function error($err=''){ 00301 if(!$this->id_connection){ 00302 $this->last_error = mysql_error() ? mysql_error() : "[Error Desconocido en MySQL: $err]"; 00303 if($this->logger){ 00304 Logger::error($this->last_error); 00305 } 00306 return $this->last_error; 00307 } 00308 $this->last_error = mysql_error() ? mysql_error() : "[Error Desconocido en MySQL: $err]"; 00309 $this->last_error.= $err; 00310 if($this->logger){ 00311 Logger::error($this->last_error); 00312 } 00313 return $this->last_error; 00314 } 00315 00321 public function no_error(){ 00322 if(!$this->id_connection){ 00323 return false; 00324 } 00325 return mysql_errno(); 00326 } 00327 00333 public function last_insert_id($table='', $primary_key=''){ 00334 if(!$this->id_connection){ 00335 return false; 00336 } 00337 return mysql_insert_id($this->id_connection); 00338 } 00339 00346 public function table_exists($table, $schema=''){ 00347 $table = addslashes("$table"); 00348 if($schema==''){ 00349 $num = $this->fetch_one("SELECT COUNT(*) FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_NAME = '$table'"); 00350 } else { 00351 $schema = addslashes("$schema"); 00352 $num = $this->fetch_one("SELECT COUNT(*) FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_NAME = '$table' AND TABLE_SCHEMA = '$schema'"); 00353 } 00354 return $num[0]; 00355 } 00356 00363 public function limit($sql){ 00364 $params = Util::getParams(func_get_args()); 00365 $sql_new = $sql; 00366 00367 if(isset($params['limit']) && is_numeric($params['limit'])){ 00368 $sql_new.=" LIMIT $params[limit]"; 00369 } 00370 00371 if(isset($params['offset']) && is_numeric($params['offset'])){ 00372 $sql_new.=" OFFSET $params[offset]"; 00373 } 00374 00375 return $sql_new; 00376 } 00377 00384 public function drop_table($table, $if_exists=true){ 00385 if($if_exists){ 00386 return $this->query("DROP TABLE IF EXISTS $table"); 00387 } else { 00388 return $this->query("DROP TABLE $table"); 00389 } 00390 } 00391 00405 public function create_table($table, $definition, $index=array()){ 00406 $create_sql = "CREATE TABLE $table ("; 00407 if(!is_array($definition)){ 00408 new KumbiaException("Definición invalida para crear la tabla '$table'"); 00409 return false; 00410 } 00411 $create_lines = array(); 00412 $index = array(); 00413 $unique_index = array(); 00414 $primary = array(); 00415 $not_null = ""; 00416 $size = ""; 00417 foreach($definition as $field => $field_def){ 00418 if(isset($field_def['not_null'])){ 00419 $not_null = $field_def['not_null'] ? 'NOT NULL' : ''; 00420 } else { 00421 $not_null = ""; 00422 } 00423 if(isset($field_def['size'])){ 00424 $size = $field_def['size'] ? '('.$field_def['size'].')' : ''; 00425 } else { 00426 $size = ""; 00427 } 00428 if(isset($field_def['index'])){ 00429 if($field_def['index']){ 00430 $index[] = "INDEX(`$field`)"; 00431 } 00432 } 00433 if(isset($field_def['unique_index'])){ 00434 if($field_def['unique_index']){ 00435 $index[] = "UNIQUE(`$field`)"; 00436 } 00437 } 00438 if(isset($field_def['primary'])){ 00439 if($field_def['primary']){ 00440 $primary[] = "`$field`"; 00441 } 00442 } 00443 if(isset($field_def['auto'])){ 00444 if($field_def['auto']){ 00445 $field_def['extra'] = isset($field_def['extra']) ? $field_def['extra']." AUTO_INCREMENT" : "AUTO_INCREMENT"; 00446 } 00447 } 00448 if(isset($field_def['extra'])){ 00449 $extra = $field_def['extra']; 00450 } else { 00451 $extra = ""; 00452 } 00453 $create_lines[] = "`$field` ".$field_def['type'].$size.' '.$not_null.' '.$extra; 00454 } 00455 $create_sql.= join(',', $create_lines); 00456 $last_lines = array(); 00457 if(count($primary)){ 00458 $last_lines[] = 'PRIMARY KEY('.join(",", $primary).')'; 00459 } 00460 if(count($index)){ 00461 $last_lines[] = join(',', $index); 00462 } 00463 if(count($unique_index)){ 00464 $last_lines[] = join(',', $unique_index); 00465 } 00466 if(count($last_lines)){ 00467 $create_sql.= ','.join(',', $last_lines).')'; 00468 } 00469 return $this->query($create_sql); 00470 00471 } 00472 00478 public function list_tables(){ 00479 return $this->fetch_all("SHOW TABLES"); 00480 } 00481 00488 public function describe_table($table, $schema=''){ 00489 if($schema==''){ 00490 return $this->fetch_all("DESCRIBE `$table`"); 00491 } else { 00492 return $this->fetch_all("DESCRIBE `$schema`.`$table`"); 00493 } 00494 } 00495 }