KumbiaPHP  beta2
Framework PHP
 Todo Estructuras de Datos Namespaces Archivos Funciones Variables Páginas
oracle.php
Ir a la documentación de este archivo.
1 <?php
29 class DbOracle extends DbBase implements DbBaseInterface
30 {
31 
49  protected $last_query;
55  public $last_error;
61  private $autocommit = false;
67  private $num_rows = false;
68 
73  const DB_ASSOC = OCI_ASSOC;
74 
75 
80  const DB_BOTH = OCI_BOTH;
81 
86  const DB_NUM = OCI_NUM;
87 
92  const TYPE_INTEGER = 'INTEGER';
93 
98  const TYPE_DATE = 'DATE';
99 
104  const TYPE_VARCHAR = 'VARCHAR2';
105 
110  const TYPE_DECIMAL = 'DECIMAL';
111 
116  const TYPE_DATETIME = 'DATETIME';
117 
122  const TYPE_CHAR = 'CHAR';
123 
130  function connect($config)
131  {
132 
133  if (!extension_loaded('oci8')) {
134  throw new KumbiaException('Debe cargar la extensión de PHP llamada php_oci8');
135  }
136 
137  if ($this->id_connection = @oci_pconnect($config['username'], $config['password'], "//{$config['host']}/{$config['name']}")) {
141  $this->query("alter session set nls_date_format = 'YYYY-MM-DD'");
142  return true;
143  } else {
144  throw new KumbiaException($this->error($php_errormsg));
145  }
146  }
147 
154  function query($sqlQuery)
155  {
156  $this->debug($sqlQuery);
157  if ($this->logger) {
158  Logger::debug($sqlQuery);
159  }
160 
161  $this->num_rows = false;
162  $this->last_query = $sqlQuery;
163  $resultQuery = @oci_parse($this->id_connection, $sqlQuery);
164  if ($resultQuery) {
165  $this->last_result_query = $resultQuery;
166  } else {
167  throw new KumbiaException($this->error($php_errormsg));
168  }
169  if ($this->autocommit) {
170  $commit = OCI_COMMIT_ON_SUCCESS;
171  } else {
172  $commit = OCI_DEFAULT;
173  }
174 
175  if (!@oci_execute($resultQuery, $commit)) {
176  throw new KumbiaException($this->error($php_errormsg));
177  }
178  return $resultQuery;
179  }
180 
184  function close()
185  {
186  if ($this->id_connection) {
187  return oci_close($this->id_connection);
188  }
189  }
190 
198  function fetch_array($resultQuery=NULL, $opt=OCI_BOTH)
199  {
200 
201  if (!$resultQuery) {
202  $resultQuery = $this->last_result_query;
203  if (!$resultQuery) {
204  return false;
205  }
206  }
207  $result = oci_fetch_array($resultQuery, $opt);
208  if (is_array($result)) {
209  $result_to_lower = array();
210  foreach ($result as $key => $value) {
211  $result_to_lower[strtolower($key)] = $value;
212  }
213  return $result_to_lower;
214  }
215  return false;
216  }
217 
224  {
225  $this->connect($config);
226  }
227 
231  function num_rows($resultQuery=NULL)
232  {
233 
234  if (!$resultQuery) {
235  $resultQuery = $this->last_result_query;
236  if (!$resultQuery) {
237  throw new KumbiaException($this->error('Resource invalido para db::num_rows'));
238  }
239  }
240 
241  // El Adaptador cachea la ultima llamada a num_rows por razones de performance
242 
243  /* if($resultQuery==$this->last_result_query){
244  if($this->num_rows!==false){
245  return $this->num_rows;
246  }
247  } */
248  if ($this->autocommit) {
249  $commit = OCI_COMMIT_ON_SUCCESS;
250  } else {
251  $commit = OCI_DEFAULT;
252  }
253  if (!@oci_execute($resultQuery, $commit)) {
254  throw new KumbiaException($this->error($php_errormsg . " al ejecutar <em>'{$this->lastQuery}'</em>"));
255  }
256  $tmp = array();
257  $this->num_rows = oci_fetch_all($resultQuery, $tmp);
258  unset($tmp);
259  @oci_execute($resultQuery, $commit);
260  return $this->num_rows;
261  }
262 
270  function field_name($number, $resultQuery=NULL)
271  {
272 
273  if (!$resultQuery) {
274  $resultQuery = $this->last_result_query;
275  if (!$resultQuery) {
276  throw new KumbiaException($this->error('Resource invalido para db::field_name'));
277  }
278  }
279 
280  if (($fieldName = oci_field_name($resultQuery, $number + 1)) !== false) {
281  return strtolower($fieldName);
282  } else {
283  throw new KumbiaException($this->error());
284  }
285  }
286 
294  function data_seek($number, $resultQuery=NULL)
295  {
296  if (!$resultQuery) {
297  $resultQuery = $this->last_result_query;
298  if (!$resultQuery) {
299  throw new KumbiaException($this->error('Resource invalido para db::data_seek'));
300  }
301  }
302  if ($this->autocommit) {
303  $commit = OCI_COMMIT_ON_SUCCESS;
304  } else {
305  $commit = OCI_DEFAULT;
306  }
307  if (!@oci_execute($resultQuery, $commit)) {
308  throw new KumbiaException($this->error($php_errormsg . " al ejecutar <em>'{$this->lastQuery}'</em>"));
309  }
310  if ($number) {
311  for ($i = 0; $i <= $number - 1; $i++) {
312  if (!oci_fetch_row($resultQuery)) {
313  return false;
314  }
315  }
316  } else {
317  return true;
318  }
319  return true;
320  }
321 
328  function affected_rows($resultQuery=NULL)
329  {
330 
331  if (!$resultQuery) {
332  $resultQuery = $this->last_result_query;
333  if (!$resultQuery) {
334  return false;
335  }
336  }
337  if (($numberRows = oci_num_rows($resultQuery)) !== false) {
338  return $numberRows;
339  } else {
340  throw new KumbiaException($this->error('Resource invalido para db::affected_rows'));
341  }
342  }
343 
349  function error($err='')
350  {
351  if (!$this->id_connection) {
352  $error = oci_error() ? oci_error() : "[Error Desconocido en Oracle]";
353  if (is_array($error)) {
354  $error['message'].=" > $err ";
355  return $error['message'];
356  } else {
357  //$error.=" $php_errormsg ";
358  return $error;
359  }
360  }
361  $error = oci_error($this->id_connection);
362  if ($error) {
363  $error['message'].=" > $err ";
364  } else {
365  $error['message'] = $err;
366  }
367  return $error['message'];
368  }
369 
375  function no_error()
376  {
377  if (!$this->id_connection) {
378  $error = oci_error() ? oci_error() : 0;
379  if (is_array($error)) {
380  return $error['code'];
381  } else {
382  return $error;
383  }
384  }
385  $error = oci_error($this->id_connection);
386  return $error['code'];
387  }
388 
395  public function limit($sql, $number)
396  {
397  if (!is_numeric($number) || $number < 0) {
398  return $sql;
399  }
400  if (eregi("ORDER[\t\n\r ]+BY", $sql)) {
401  if (stripos($sql, "WHERE")) {
402  return eregi_replace("ORDER[\t\n\r ]+BY", "AND ROWNUM <= $number ORDER BY", $sql);
403  } else {
404  return eregi_replace("ORDER[\t\n\r ]+BY", "WHERE ROWNUM <= $number ORDER BY", $sql);
405  }
406  } else {
407  if (stripos($sql, "WHERE")) {
408  return "$sql AND ROWNUM <= $number";
409  } else {
410  return "$sql WHERE ROWNUM <= $number";
411  }
412  }
413  }
414 
421  public function drop_table($table, $if_exists=true)
422  {
423  if ($if_exists) {
424  if ($this->table_exists($table)) {
425  return $this->query("DROP TABLE $table");
426  } else {
427  return true;
428  }
429  } else {
430  return $this->query("DROP TABLE $table");
431  }
432  }
433 
447  public function create_table($table, $definition, $index=array())
448  {
449  $create_sql = "CREATE TABLE $table (";
450  if (!is_array($definition)) {
451  throw new KumbiaException("Definición invalida para crear la tabla '$table'");
452  }
453  $create_lines = array();
454  $index = array();
455  $unique_index = array();
456  $primary = array();
457  //$not_null = "";
458  //$size = "";
459  foreach ($definition as $field => $field_def) {
460  if (isset($field_def['not_null'])) {
461  $not_null = $field_def['not_null'] ? 'NOT NULL' : '';
462  } else {
463  $not_null = "";
464  }
465  if (isset($field_def['size'])) {
466  $size = $field_def['size'] ? '(' . $field_def['size'] . ')' : '';
467  } else {
468  $size = "";
469  }
470  if (isset($field_def['index'])) {
471  if ($field_def['index']) {
472  $index[] = "INDEX($field)";
473  }
474  }
475  if (isset($field_def['unique_index'])) {
476  if ($field_def['unique_index']) {
477  $index[] = "UNIQUE($field)";
478  }
479  }
480  if (isset($field_def['primary'])) {
481  if ($field_def['primary']) {
482  $primary[] = "$field";
483  }
484  }
485  if (isset($field_def['auto'])) {
486  if ($field_def['auto']) {
487  $this->query("CREATE SEQUENCE {$table}_{$field}_seq START WITH 1");
488  }
489  }
490  if (isset($field_def['extra'])) {
491  $extra = $field_def['extra'];
492  } else {
493  $extra = "";
494  }
495  $create_lines[] = "$field " . $field_def['type'] . $size . ' ' . $not_null . ' ' . $extra;
496  }
497  $create_sql.= join(',', $create_lines);
498  $last_lines = array();
499  if (count($primary)) {
500  $last_lines[] = 'PRIMARY KEY(' . join(",", $primary) . ')';
501  }
502  if (count($index)) {
503  $last_lines[] = join(',', $index);
504  }
505  if (count($unique_index)) {
506  $last_lines[] = join(',', $unique_index);
507  }
508  if (count($last_lines)) {
509  $create_sql.= ',' . join(',', $last_lines) . ')';
510  }
511  return $this->query($create_sql);
512  }
513 
519  function list_tables()
520  {
521  return $this->fetch_all("SELECT table_name FROM all_tables");
522  }
523 
529  public function last_insert_id($table='', $primary_key='')
530  {
531  if (!$this->id_connection) {
532  return false;
533  }
537  if ($table && $primary_key) {
538  $sequence = $table . "_" . $primary_key . "_seq";
539  $value = $this->fetch_one("SELECT $sequence.CURRVAL FROM dual");
540  return $value[0];
541  }
542  return false;
543  }
544 
551  function table_exists($table, $schema='')
552  {
553  $num = $this->fetch_one("SELECT COUNT(*) FROM ALL_TABLES WHERE TABLE_NAME = '" . strtoupper($table) . "'");
554  return $num[0];
555  }
556 
563  public function describe_table($table, $schema='')
564  {
568  $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) . "'");
569  $final_describe = array();
570  foreach ($describe as $key => $value) {
571  $final_describe[] = array(
572  "Field" => $value["field"],
573  "Type" => $value["type"],
574  "Null" => $value["isnull"] == "Y" ? "YES" : "NO",
575  "Key" => $value["key"] == 1 ? "PRI" : ""
576  );
577  }
578  return $final_describe;
579  }
580 
585  public function begin()
586  {
587  //Siempre hay una transaccion
588  //return $this->query("BEGIN WORK");
589  return true;
590  }
591 
597  public function last_sql_query()
598  {
599  return $this->last_query;
600  }
601 
602 }