00001 <?php
00023 class DbInformix extends DbBase implements DbBaseInterface {
00024
00030 public $id_connection;
00031
00037 public $last_result_query;
00038
00044 private $last_query;
00050 public $last_error;
00051
00057 private $return_rows = true;
00058
00062 private $limit = -1;
00063
00067 private $actual_limit = 0;
00068
00073 const DB_ASSOC = 1;
00074
00079 const DB_BOTH = 2;
00080
00085 const DB_NUM = 3;
00086
00091 const TYPE_INTEGER = 'INTEGER';
00092
00097 const TYPE_DATE = 'DATE';
00098
00103 const TYPE_VARCHAR = 'VARCHAR';
00104
00109 const TYPE_DECIMAL = 'DECIMAL';
00110
00115 const TYPE_DATETIME = 'DATETIME';
00116
00121 const TYPE_CHAR = 'CHAR';
00122
00129 public function connect($config){
00130 if(!extension_loaded('informix')){
00131 throw new KumbiaException('Debe cargar la extensión de PHP llamada php_ifx');
00132 return false;
00133 }
00134
00135 if($this->id_connection = ifx_connect("{$config['name']}@{$config['host']}", $config['username'], $config['password'])){
00136 return true;
00137 } else {
00138 throw new KumbiaException($this->error(), $this->no_error(), false);
00139 return false;
00140 }
00141
00142 }
00143
00150 public function query($sql_query){
00151 $this->debug($sql_query);
00152 if($this->logger){
00153 Logger::debug($sql_query);
00154 }
00155 if(!$this->id_connection){
00156 $this->connect();
00157 if(!$this->id_connection){
00158 return false;
00159 }
00160 }
00161 $this->last_query = $sql_query;
00165 if($this->return_rows){
00166 $result_query = ifx_query($sql_query, $this->id_connection, IFX_HOLD);
00167 } else {
00168 $result_query = ifx_query($sql_query, $this->id_connection);
00169 }
00170 $this->set_return_rows(true);
00171 if($result_query===false){
00172 $this->last_result_query = false;
00173 throw new KumbiaException($this->error(" al ejecutar <i>\"$sql_query\"</i>"), $this->no_error());
00174 return false;
00175 } else {
00176 $this->last_result_query = $result_query;
00177 return $result_query;
00178 }
00179 }
00180
00184 public function close(){
00185 if($this->id_connection) {
00186 return ifx_close($this->id_connection);
00187 }
00188 return false;
00189 }
00190
00198 public function fetch_array($result_query='', $opt=''){
00199 if($opt==='') {
00200 $opt = db::DB_BOTH;
00201 }
00202 if(!$this->id_connection){
00203 return false;
00204 }
00205 if(!$result_query){
00206 $result_query = $this->last_result_query;
00207 if(!$result_query){
00208 return false;
00209 }
00210 }
00211 $fetch = ifx_fetch_row($result_query, $opt);
00215 if($this->limit!=-1){
00216 if($this->actual_limit>=$this->limit){
00217 $this->limit = -1;
00218 $this->actual_limit = 0;
00219 return false;
00220 } else {
00221 $this->actual_limit++;
00222 if($this->actual_limit==$this->limit){
00223 $this->limit = -1;
00224 $this->actual_limit = 0;
00225 }
00226 }
00227 }
00231 if(!is_array($fetch)||($opt==db::DB_ASSOC)){
00232 return $fetch;
00233 }
00234 if($opt==db::DB_BOTH){
00235 $result = array();
00236 $i = 0;
00237 foreach($fetch as $key => $value){
00238 $result[$key] = $value;
00239 $result[$i++] = $value;
00240 }
00241 return $result;
00242 }
00243 if($opt==db::DB_NUM){
00244 return array_values($fetch);
00245 }
00246 }
00247
00251 public function __construct($config){
00252 $this->connect($config);
00253 }
00254
00258 public function num_rows($result_query=''){
00259 if(!$this->id_connection){
00260 return false;
00261 }
00262 if(!$result_query){
00263 $result_query = $this->last_result_query;
00264 if(!$result_query){
00265 return false;
00266 }
00267 }
00268 if(($number_rows = ifx_num_rows($result_query))!==false){
00272 if($this->limit==-1){
00273 return $number_rows;
00274 } else {
00275 return $this->limit < $number_rows ? $this->limit : $number_rows;
00276 }
00277 } else {
00278 throw new KumbiaException($this->error(), $this->no_error());
00279 return false;
00280 }
00281 return false;
00282 }
00283
00291 public function field_name($number, $result_query=''){
00292 if(!$this->id_connection){
00293 return false;
00294 }
00295 if(!$result_query){
00296 $result_query = $this->last_result_query;
00297 if(!$result_query){
00298 return false;
00299 }
00300 }
00301 $fields = ifx_fieldproperties($result_query);
00302 if(!is_array($fields)){
00303 return false;
00304 }
00305
00306 $fields = array_keys($fields);
00307 return $fields[$number];
00308 }
00309
00310
00319 public function data_seek($number, $result_query=''){
00320 if(!$result_query){
00321 $result_query = $this->last_result_query;
00322 if(!$result_query){
00323 return false;
00324 }
00325 }
00326 if(($success = ifx_fetch_row($result_query, $number))!==false){
00327 return $success;
00328 } else {
00329 throw new KumbiaException($this->error(), $this->no_error());
00330 return false;
00331 }
00332 return false;
00333 }
00334
00341 public function affected_rows($result_query=''){
00342 if(!$result_query){
00343 $result_query = $this->last_result_query;
00344 if(!$result_query){
00345 return false;
00346 }
00347 }
00348 if(($numberRows = ifx_affected_rows($result_query))!==false){
00349 return $numberRows;
00350 } else {
00351 $this->lastError = $this->error();
00352 throw new KumbiaException($this->error(), $this->no_error());
00353 return false;
00354 }
00355 return false;
00356 }
00357
00363 public function error($err=''){
00364 if(!$this->id_connection){
00365 $this->last_error = ifx_errormsg() ? ifx_errormsg() : "[Error Desconocido en Informix: $err]";
00366 if($this->logger){
00367 Logger::error($this->last_error);
00368 }
00369 return $this->last_error;
00370 }
00371 $this->last_error = ifx_errormsg($this->id_connection) ? ifx_errormsg($this->id_connection) : "[Error Desconocido en Informix: $err]";
00372 $this->last_error.= $err;
00373 if($this->logger){
00374 Logger::error($this->last_error);
00375 }
00376 return $this->last_error;
00377 }
00378
00384 public function no_error(){
00385 if(!$this->id_connection){
00386 return ifx_error();
00387 }
00388 return ifx_error();
00389 }
00390
00396 public function last_insert_id($table='', $primary_key=''){
00397 if(!$this->id_connection){
00398 return false;
00399 }
00400 $sqlca = ifx_getsqlca($this->last_result_query);
00401 return $sqlca["sqlerrd1"];
00402 }
00403
00410 public function table_exists($table, $schema=''){
00414 $table = addslashes("$table");
00415 $num = $this->fetch_one("SELECT COUNT(*) FROM systables WHERE tabname = '$table'");
00416 return (int) $num[0];
00417 }
00418
00425 public function limit($sql, $number){
00429 $number = (int) $number;
00430 $this->limit = $number;
00431 return "$sql -- LIMIT $number\n";
00432 }
00433
00440 public function drop_table($table, $if_exists=true){
00441 if($if_exists){
00442 if($this->table_exists($table)){
00443 $this->set_return_rows(false);
00444 return $this->query("DROP TABLE $table");
00445 } else {
00446 return true;
00447 }
00448 } else {
00449 $this->set_return_rows(false);
00450 return $this->query("DROP TABLE $table");
00451 }
00452 }
00453
00467 public function create_table($table, $definition, $index=array()){
00468 $create_sql = "CREATE TABLE $table (";
00469 if(!is_array($definition)){
00470 new KumbiaException("Definición invalida para crear la tabla '$table'");
00471 return false;
00472 }
00473 $create_lines = array();
00474 $index = array();
00475 $unique_index = array();
00476 $primary = array();
00477 $not_null = "";
00478 $size = "";
00479 foreach($definition as $field => $field_def){
00480 if(isset($field_def['not_null'])){
00481 $not_null = $field_def['not_null'] ? 'NOT NULL' : '';
00482 } else {
00483 $not_null = "";
00484 }
00485 if(isset($field_def['size'])){
00486 $size = $field_def['size'] ? '('.$field_def['size'].')' : '';
00487 } else {
00488 $size = "";
00489 }
00490 if(isset($field_def['index'])){
00491 if($field_def['index']){
00492 $index[] = "INDEX($field)";
00493 }
00494 }
00495 if(isset($field_def['unique_index'])){
00496 if($field_def['unique_index']){
00497 $index[] = "UNIQUE($field)";
00498 }
00499 }
00500 if(isset($field_def['primary'])){
00501 if($field_def['primary']){
00502 $primary[] = "$field";
00503 }
00504 }
00505 if(isset($field_def['auto'])){
00506 if($field_def['auto']){
00507 $field_def['type'] = "SERIAL";
00508 }
00509 }
00510 if(isset($field_def['extra'])){
00511 $extra = $field_def['extra'];
00512 } else {
00513 $extra = "";
00514 }
00515 $create_lines[] = "$field ".$field_def['type'].$size.' '.$not_null.' '.$extra;
00516 }
00517 $create_sql.= join(',', $create_lines);
00518 $last_lines = array();
00519 if(count($primary)){
00520 $last_lines[] = 'PRIMARY KEY('.join(",", $primary).')';
00521 }
00522 if(count($index)){
00523 $last_lines[] = join(',', $index);
00524 }
00525 if(count($unique_index)){
00526 $last_lines[] = join(',', $unique_index);
00527 }
00528 if(count($last_lines)){
00529 $create_sql.= ','.join(',', $last_lines).')';
00530 }
00531 $this->set_return_rows(false);
00532 return $this->query($create_sql);
00533
00534 }
00535
00541 public function list_tables(){
00542 return $this->fetch_all("SELECT tabname FROM systables WHERE tabtype = 'T' AND version <> 65537");
00543 }
00544
00551 public function describe_table($table, $schema=''){
00558 $describe = $this->fetch_all("SELECT c.colname AS Field, c.coltype AS Type,
00559 'YES' AS NULL FROM systables t, syscolumns c WHERE
00560 c.tabid = t.tabid AND t.tabname = '$table' ORDER BY c.colno");
00561 $final_describe = array();
00562 foreach($describe as $field){
00563
00564 if($field['field']=='id'){
00565 $field["key"] = 'PRI';
00566 $field["null"] = 'NO';
00567 } else {
00568 $field["key"] = '';
00569 }
00570 if(substr($field['field'], -3)=='_id'){
00571 $field["null"] = 'NO';
00572 }
00573 if($field['type']==262){
00574 $field['type'] = "serial";
00575 }
00576 if($field['type']==13){
00577 $field['type'] = "varchar";
00578 }
00579 if($field['type']==7){
00580 $field['type'] = "date";
00581 }
00582 $final_describe[] = array(
00583 "Field" => $field["field"],
00584 "Type" => $field["type"],
00585 "Null" => $field["null"],
00586 "Key" => $field["key"]
00587 );
00588 }
00589 return $final_describe;
00590
00591 }
00592
00601 public function insert($table, $values, $fields=null){
00602 $this->set_return_rows(false);
00603 return parent::insert($table, $values, $fields);
00604 }
00605
00615 public function update($table, $fields, $values, $where_condition=null){
00616 $this->set_return_rows(false);
00617 return parent::update($table, $fields, $values, $where_condition);
00618 }
00619
00626 public function delete($table, $where_condition){
00627 $this->set_return_rows(false);
00628 return parent::delete($table, $where_condition);
00629 }
00630
00634 public function set_return_rows($value=true){
00635 $this->return_rows = $value;
00636 }
00637
00642 public function begin(){
00643 $this->set_return_rows(false);
00644 return $this->query("BEGIN WORK");
00645 }
00646
00647
00652 public function rollback(){
00653 $this->set_return_rows(false);
00654 return $this->query("ROLLBACK");
00655 }
00656
00661 public function commit(){
00662 $this->set_return_rows(false);
00663 return $this->query("COMMIT");
00664 }
00665
00666 }