61 private $autocommit =
false;
67 private $num_rows =
false;
133 if (!extension_loaded(
'oci8')) {
134 throw new KumbiaException(
'Debe cargar la extensión de PHP llamada php_oci8');
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'");
156 $this->
debug($sqlQuery);
162 $this->last_query = $sqlQuery;
163 $resultQuery = @oci_parse($this->id_connection, $sqlQuery);
165 $this->last_result_query = $resultQuery;
169 if ($this->autocommit) {
170 $commit = OCI_COMMIT_ON_SUCCESS;
172 $commit = OCI_DEFAULT;
175 if (!@oci_execute($resultQuery, $commit)) {
186 if ($this->id_connection) {
187 return oci_close($this->id_connection);
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;
213 return $result_to_lower;
248 if ($this->autocommit) {
249 $commit = OCI_COMMIT_ON_SUCCESS;
251 $commit = OCI_DEFAULT;
253 if (!@oci_execute($resultQuery, $commit)) {
254 throw new KumbiaException($this->
error($php_errormsg .
" al ejecutar <em>'{$this->lastQuery}'</em>"));
257 $this->
num_rows = oci_fetch_all($resultQuery, $tmp);
259 @oci_execute($resultQuery, $commit);
260 return $this->num_rows;
280 if (($fieldName = oci_field_name($resultQuery, $number + 1)) !==
false) {
281 return strtolower($fieldName);
302 if ($this->autocommit) {
303 $commit = OCI_COMMIT_ON_SUCCESS;
305 $commit = OCI_DEFAULT;
307 if (!@oci_execute($resultQuery, $commit)) {
308 throw new KumbiaException($this->
error($php_errormsg .
" al ejecutar <em>'{$this->lastQuery}'</em>"));
311 for ($i = 0; $i <= $number - 1; $i++) {
312 if (!oci_fetch_row($resultQuery)) {
337 if (($numberRows = oci_num_rows($resultQuery)) !==
false) {
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'];
361 $error = oci_error($this->id_connection);
363 $error[
'message'].=
" > $err ";
365 $error[
'message'] = $err;
367 return $error[
'message'];
377 if (!$this->id_connection) {
378 $error = oci_error() ? oci_error() : 0;
379 if (is_array($error)) {
380 return $error[
'code'];
385 $error = oci_error($this->id_connection);
386 return $error[
'code'];
395 public function limit($sql, $number)
397 if (!is_numeric($number) || $number < 0) {
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);
404 return eregi_replace(
"ORDER[\t\n\r ]+BY",
"WHERE ROWNUM <= $number ORDER BY", $sql);
407 if (stripos($sql,
"WHERE")) {
408 return "$sql AND ROWNUM <= $number";
410 return "$sql WHERE ROWNUM <= $number";
425 return $this->
query(
"DROP TABLE $table");
430 return $this->
query(
"DROP TABLE $table");
449 $create_sql =
"CREATE TABLE $table (";
450 if (!is_array($definition)) {
451 throw new KumbiaException(
"Definición invalida para crear la tabla '$table'");
453 $create_lines = array();
455 $unique_index = array();
459 foreach ($definition as $field => $field_def) {
460 if (isset($field_def[
'not_null'])) {
461 $not_null = $field_def[
'not_null'] ?
'NOT NULL' :
'';
465 if (isset($field_def[
'size'])) {
466 $size = $field_def[
'size'] ?
'(' . $field_def[
'size'] .
')' :
'';
470 if (isset($field_def[
'index'])) {
471 if ($field_def[
'index']) {
472 $index[] =
"INDEX($field)";
475 if (isset($field_def[
'unique_index'])) {
476 if ($field_def[
'unique_index']) {
477 $index[] =
"UNIQUE($field)";
480 if (isset($field_def[
'primary'])) {
481 if ($field_def[
'primary']) {
482 $primary[] =
"$field";
485 if (isset($field_def[
'auto'])) {
486 if ($field_def[
'auto']) {
487 $this->
query(
"CREATE SEQUENCE {$table}_{$field}_seq START WITH 1");
490 if (isset($field_def[
'extra'])) {
491 $extra = $field_def[
'extra'];
495 $create_lines[] =
"$field " . $field_def[
'type'] . $size .
' ' . $not_null .
' ' . $extra;
497 $create_sql.= join(
',', $create_lines);
498 $last_lines = array();
499 if (count($primary)) {
500 $last_lines[] =
'PRIMARY KEY(' . join(
",", $primary) .
')';
503 $last_lines[] = join(
',', $index);
505 if (count($unique_index)) {
506 $last_lines[] = join(
',', $unique_index);
508 if (count($last_lines)) {
509 $create_sql.=
',' . join(
',', $last_lines) .
')';
511 return $this->
query($create_sql);
521 return $this->
fetch_all(
"SELECT table_name FROM all_tables");
531 if (!$this->id_connection) {
537 if ($table && $primary_key) {
538 $sequence = $table .
"_" . $primary_key .
"_seq";
539 $value = $this->
fetch_one(
"SELECT $sequence.CURRVAL FROM dual");
553 $num = $this->
fetch_one(
"SELECT COUNT(*) FROM ALL_TABLES WHERE TABLE_NAME = '" . strtoupper($table) .
"'");
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" :
""
578 return $final_describe;