KumbiaPHP beta2-dev
Framework PHP en español
oracle.php
Ir a la documentación de este archivo.
00001 <?php
00023 class DbOracle extends DbBase implements DbBaseInterface  {
00024 
00030         public $id_connection;
00031 
00037         public $last_result_query;
00038 
00044         private $last_query;
00050         public $lastError;
00051 
00057         private $autocommit = false;
00058 
00064         private $num_rows = false;
00065 
00070         const DB_ASSOC = OCI_ASSOC;
00071 
00072 
00077         const DB_BOTH = OCI_BOTH;
00078 
00083         const DB_NUM = OCI_NUM;
00084 
00089         const TYPE_INTEGER = 'INTEGER';
00090 
00095         const TYPE_DATE = 'DATE';
00096 
00101         const TYPE_VARCHAR = 'VARCHAR2';
00102 
00107         const TYPE_DECIMAL = 'DECIMAL';
00108 
00113         const TYPE_DATETIME = 'DATETIME';
00114 
00119         const TYPE_CHAR = 'CHAR';
00120 
00127         function connect($config){
00128 
00129                 if(!extension_loaded('oci8')){
00130                         throw new KumbiaException('Debe cargar la extensión de PHP llamada php_oci8');
00131                 }
00132 
00133                 if($this->id_connection = @oci_pconnect($config['username'], $config['password'], "//{$config['host']}/{$config['name']}")){
00137                         $this->query("alter session set nls_date_format = 'YYYY-MM-DD'");
00138                         return true;
00139                 } else {
00140                         throw new KumbiaException($this->error($php_errormsg));
00141                 }
00142         }
00143 
00150         function query($sqlQuery){
00151                 $this->debug($sqlQuery);
00152         if($this->logger){
00153             Logger::debug($sqlQuery);
00154         }
00155                 if(!$this->id_connection){
00156                         $this->connect();
00157                         if(!$this->id_connection){
00158                                 return false;
00159                         }
00160                 }
00161                 $this->num_rows = false;
00162                 $this->lastQuery = $sqlQuery;
00163                 $resultQuery = @oci_parse($this->id_connection, $sqlQuery);
00164                 if($resultQuery){
00165                         $this->last_result_query = $resultQuery;
00166                 } else {
00167                         $this->last_result_query = false;
00168                         throw new KumbiaException($this->error($php_errormsg));
00169                 }
00170                 if($this->autocommit){
00171                         $commit = OCI_COMMIT_ON_SUCCESS;
00172                 } else {
00173                         $commit = OCI_DEFAULT;
00174                 }
00175 
00176                 if(!@oci_execute($resultQuery, $commit)){
00177                         $this->last_result_query = false;
00178                         throw new KumbiaException($this->error($php_errormsg));
00179                 }
00180                 return $resultQuery;
00181         }
00182 
00186         function close(){
00187                 if($this->id_connection) {
00188                         return oci_close($this->id_connection);
00189                 }
00190         }
00191 
00199         function fetch_array($resultQuery='', $opt=OCI_BOTH){
00200                 if(!$this->id_connection){
00201                         return false;
00202                 }
00203                 if(!$resultQuery){
00204                         $resultQuery = $this->last_result_query;
00205                         if(!$resultQuery){
00206                                 return false;
00207                         }
00208                 }
00209                 $result = oci_fetch_array($resultQuery, $opt);
00210                 if(is_array($result)){
00211                         $result_to_lower = array();
00212                         foreach($result as $key => $value){
00213                                 $result_to_lower[strtolower($key)] = $value;
00214                         }
00215                         return $result_to_lower;
00216                 } else {
00217                         return false;
00218                 }
00219                 return false;
00220         }
00221 
00227         function __construct($config){
00228                 $this->connect($config);
00229         }
00230 
00234         function num_rows($resultQuery=''){
00235                 if(!$this->id_connection){
00236                         return false;
00237                 }
00238                 if(!$resultQuery){
00239                         $resultQuery = $this->last_result_query;
00240                         if(!$resultQuery){
00241                                 throw new KumbiaException($this->error('Resource invalido para db::num_rows'));
00242                         }
00243                 }
00244                 
00245         // El Adaptador cachea la ultima llamada a num_rows por razones de performance
00246         
00247                 /*if($resultQuery==$this->last_result_query){
00248                         if($this->num_rows!==false){
00249                                 return $this->num_rows;
00250                         }
00251                 }*/
00252                 if($this->autocommit){
00253                         $commit = OCI_COMMIT_ON_SUCCESS;
00254                 } else {
00255                         $commit = OCI_DEFAULT;
00256                 }
00257                 if(!@oci_execute($resultQuery, $commit)){
00258                         $this->last_result_query = false;
00259                         throw new KumbiaException($this->error($php_errormsg." al ejecutar <em>'{$this->lastQuery}'</em>"));
00260                         return false;
00261                 }
00262                 $tmp = array();
00263                 $this->num_rows = oci_fetch_all($resultQuery, $tmp);
00264                 unset($tmp);
00265                 @oci_execute($resultQuery, $commit);
00266                 return $this->num_rows;
00267         }
00268 
00276         function field_name($number, $resultQuery=''){
00277                 if(!$this->id_connection){
00278                         return false;
00279                 }
00280                 if(!$resultQuery){
00281                         $resultQuery = $this->last_result_query;
00282                         if(!$resultQuery){
00283                                 throw new KumbiaException($this->error('Resource invalido para db::field_name'));
00284                         }
00285                 }
00286 
00287                 if(($fieldName = oci_field_name($resultQuery, $number+1))!==false){
00288                         return strtolower($fieldName);
00289                 } else {
00290                         throw new KumbiaException($this->error());
00291                 }
00292                 return false;
00293         }
00294 
00295 
00303         function data_seek($number, $resultQuery=''){
00304                 if(!$resultQuery){
00305                         $resultQuery = $this->last_result_query;
00306                         if(!$resultQuery){
00307                                 throw new KumbiaException($this->error('Resource invalido para db::data_seek'));
00308                         }
00309                 }
00310                 if($this->autocommit){
00311                         $commit = OCI_COMMIT_ON_SUCCESS;
00312                 } else {
00313                         $commit = OCI_DEFAULT;
00314                 }
00315                 if(!@oci_execute($resultQuery, $commit)){
00316                         throw new KumbiaException($this->error($php_errormsg." al ejecutar <em>'{$this->lastQuery}'</em>"));
00317                 }
00318                 if($number){
00319                         for($i=0;$i<=$number-1;$i++){
00320                                 if(!oci_fetch_row($resultQuery)){
00321                                         return false;
00322                                 }
00323                         }
00324                 } else {
00325                         return true;
00326                 }
00327                 return true;
00328         }
00329 
00336         function affected_rows($resultQuery=''){
00337                 if(!$this->id_connection){
00338                         return false;
00339                 }
00340                 if(!$resultQuery){
00341                         $resultQuery = $this->last_result_query;
00342                         if(!$resultQuery){
00343                                 return false;
00344                         }
00345                 }
00346                 if(($numberRows = oci_num_rows($resultQuery))!==false){
00347                         return $numberRows;
00348                 } else {
00349                         throw new KumbiaException($this->error('Resource invalido para db::affected_rows'));
00350                 }
00351                 return false;
00352         }
00353 
00359         function error($err=''){
00360                 if(!$this->id_connection){
00361                         $error = oci_error() ? oci_error() : "[Error Desconocido en Oracle]";
00362                         if(is_array($error)){
00363                                 $error['message'].=" > $err ";
00364                                 return $error['message'];
00365                         } else {
00366                                 $error.=" $php_errormsg ";
00367                                 return $error;
00368                         }
00369                 }
00370                 $error = oci_error($this->id_connection);
00371                 if($error){
00372                         $error['message'].=" > $err ";
00373                 } else {
00374                         $error['message'] = $err;
00375                 }
00376                 return $error['message'];
00377         }
00378 
00384         function no_error(){
00385                 if(!$this->id_connection){
00386                         $error = oci_error() ? oci_error() : "0";
00387                         if(is_array($error)){
00388                                 return $error['code'];
00389                         } else {
00390                                 return $error;
00391                         }
00392                 }
00393                 $error = oci_error($this->id_connection);
00394                 return $error['code'];
00395         }
00396 
00403         public function limit($sql, $number){
00404                 if(!is_numeric($number)||$number<0){
00405                         return $sql;
00406                 }
00407                 if(eregi("ORDER[\t\n\r ]+BY", $sql)){
00408                         if(stripos($sql, "WHERE")){
00409                                 return eregi_replace("ORDER[\t\n\r ]+BY", "AND ROWNUM <= $number ORDER BY", $sql);
00410                         } else {
00411                                 return eregi_replace("ORDER[\t\n\r ]+BY", "WHERE ROWNUM <= $number ORDER BY", $sql);
00412                         }
00413                 } else {
00414                         if(stripos($sql, "WHERE")){
00415                                 return "$sql AND ROWNUM <= $number";
00416                         } else {
00417                                 return "$sql WHERE ROWNUM <= $number";
00418                         }
00419                 }
00420         }
00421 
00428         public function drop_table($table, $if_exists=true){
00429                 if($if_exists){
00430                         if($this->table_exists($table)){
00431                                 return $this->query("DROP TABLE $table");
00432                         } else {
00433                                 return true;
00434                         }
00435                 } else {
00436                         return $this->query("DROP TABLE $table");
00437                 }
00438         }
00439 
00453         public function create_table($table, $definition, $index=array()){
00454                 $create_sql = "CREATE TABLE $table (";
00455                 if(!is_array($definition)){
00456                         throw new KumbiaException("Definición invalida para crear la tabla '$table'");
00457                 }
00458                 $create_lines = array();
00459                 $index = array();
00460                 $unique_index = array();
00461                 $primary = array();
00462                 $not_null = "";
00463                 $size = "";
00464                 foreach($definition as $field => $field_def){
00465                         if(isset($field_def['not_null'])){
00466                                 $not_null = $field_def['not_null'] ? 'NOT NULL' : '';
00467                         } else {
00468                                 $not_null = "";
00469                         }
00470                         if(isset($field_def['size'])){
00471                                 $size = $field_def['size'] ? '('.$field_def['size'].')' : '';
00472                         } else {
00473                                 $size = "";
00474                         }
00475                         if(isset($field_def['index'])){
00476                                 if($field_def['index']){
00477                                         $index[] = "INDEX($field)";
00478                                 }
00479                         }
00480                         if(isset($field_def['unique_index'])){
00481                                 if($field_def['unique_index']){
00482                                         $index[] = "UNIQUE($field)";
00483                                 }
00484                         }
00485                         if(isset($field_def['primary'])){
00486                                 if($field_def['primary']){
00487                                         $primary[] = "$field";
00488                                 }
00489                         }
00490                         if(isset($field_def['auto'])){
00491                                 if($field_def['auto']){
00492                                         $this->query("CREATE SEQUENCE {$table}_{$field}_seq START WITH 1");
00493                                 }
00494                         }
00495                         if(isset($field_def['extra'])){
00496                                 $extra = $field_def['extra'];
00497                         } else {
00498                                 $extra = "";
00499                         }
00500                         $create_lines[] = "$field ".$field_def['type'].$size.' '.$not_null.' '.$extra;
00501                 }
00502                 $create_sql.= join(',', $create_lines);
00503                 $last_lines = array();
00504                 if(count($primary)){
00505                         $last_lines[] = 'PRIMARY KEY('.join(",", $primary).')';
00506                 }
00507                 if(count($index)){
00508                         $last_lines[] = join(',', $index);
00509                 }
00510                 if(count($unique_index)){
00511                         $last_lines[] = join(',', $unique_index);
00512                 }
00513                 if(count($last_lines)){
00514                         $create_sql.= ','.join(',', $last_lines).')';
00515                 }
00516                 return $this->query($create_sql);
00517 
00518         }
00519 
00526         function list_tables(){
00527                 return $this->fetch_all("SELECT table_name FROM all_tables");
00528         }
00529 
00535         public function last_insert_id($table='', $primary_key=''){
00536                 if(!$this->id_connection){
00537                         return false;
00538                 }
00542                 if($table&&$primary_key){
00543                         $sequence = $table."_".$primary_key."_seq";
00544                         $value = $this->fetch_one("SELECT $sequence.CURRVAL FROM dual");
00545                         return $value[0];
00546                 }
00547                 return false;
00548         }
00549 
00556         function table_exists($table, $schema=''){
00557                 $num = $this->fetch_one("SELECT COUNT(*) FROM ALL_TABLES WHERE TABLE_NAME = '".strtoupper($table)."'");
00558                 return $num[0];
00559         }
00560 
00567         public function describe_table($table, $schema=''){
00571                 $describe = $this->fetch_all("SELECT LOWER(ALL_TAB_COLUMNS.COLUMN_NAME) AS FIELD, LOWER(ALL_TAB_COLUMNS.DATA_TYPE) AS TYPE, ALL_TAB_COLUMNS.DATA_LENGTH AS LENGTH, (SELECT COUNT(*) FROM ALL_CONS_COLUMNS WHERE TABLE_NAME = '".strtoupper($table)."' AND ALL_CONS_COLUMNS.COLUMN_NAME = ALL_TAB_COLUMNS.COLUMN_NAME AND ALL_CONS_COLUMNS.POSITION IS NOT NULL) AS KEY, ALL_TAB_COLUMNS.NULLABLE AS ISNULL FROM ALL_TAB_COLUMNS WHERE ALL_TAB_COLUMNS.TABLE_NAME = '".strtoupper($table)."'");
00572                 $final_describe = array();
00573                 foreach($describe as $key => $value){
00574                         $final_describe[] = array(
00575                                 "Field" => $value["field"],
00576                                 "Type" => $value["type"],
00577                                 "Null" => $value["isnull"] == "Y" ? "YES" : "NO",
00578                                 "Key" => $value["key"] == 1 ? "PRI" : ""
00579                         );
00580                 }
00581                 return $final_describe;
00582         }
00583 
00588         public function begin(){
00589                 //Siempre hay una transaccion
00590                 //return $this->query("BEGIN WORK");
00591                 return true;
00592         }
00593 
00594 }
00595 
00596 ?>
 Todo Estructuras de Datos Namespaces Archivos Funciones Variables Enumeraciones