121 if (!extension_loaded(
'pgsql')) {
122 throw new KumbiaException(
'Debe cargar la extensión de PHP llamada php_pgsql');
129 if ($this->id_connection = pg_connect(
"host={$config['host']} user={$config['username']} password={$config['password']} dbname={$config['name']} port={$config['port']}", PGSQL_CONNECT_FORCE_NEW)) {
144 $this->
debug($sqlQuery);
149 $this->last_query = $sqlQuery;
150 if ($resultQuery = @pg_query($this->id_connection, $sqlQuery)) {
151 $this->last_result_query = $resultQuery;
163 if ($this->id_connection) {
164 return pg_close($this->id_connection);
186 return pg_fetch_array($resultQuery, NULL, $opt);
211 if (($numberRows = pg_num_rows($resultQuery)) !==
false) {
234 if (($fieldName = pg_field_name($resultQuery, $number)) !==
false) {
256 if (($success = pg_result_seek($resultQuery, $number)) !==
false) {
278 if (($numberRows = pg_affected_rows($resultQuery)) !==
false) {
292 if (!$this->id_connection) {
293 $this->last_error = @pg_last_error() ? @pg_last_error() . $err :
"[Error Desconocido en PostgreSQL \"$err\"]";
299 $this->last_error = @pg_last_error() ? @pg_last_error() . $err :
"[Error Desconocido en PostgreSQL: $err]";
300 $this->last_error.= $err;
304 return pg_last_error($this->id_connection) . $err;
326 $last_id = $this->
fetch_one(
"SELECT CURRVAL('{$table}_{$primary_key}_seq')");
338 $table = addslashes(strtolower($table));
339 if (strpos($table,
".")) {
340 list($schema, $table) = explode(
".", $table);
343 $num = $this->
fetch_one(
"SELECT COUNT(*) FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_SCHEMA = 'public' AND TABLE_NAME ='$table'");
345 $schema = addslashes(strtolower($schema));
346 $num = $this->
fetch_one(
"SELECT COUNT(*) FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_SCHEMA = '$schema' AND TABLE_NAME ='$table'");
362 if (isset($params[
'limit']) && is_numeric($params[
'limit'])) {
363 $sql_new.=
" LIMIT $params[limit]";
366 if (isset($params[
'offset']) && is_numeric($params[
'offset'])) {
367 $sql_new.=
" OFFSET $params[offset]";
383 return $this->
query(
"DROP TABLE $table");
388 return $this->
query(
"DROP TABLE $table");
407 $create_sql =
"CREATE TABLE $table (";
408 if (!is_array($definition)) {
409 throw new KumbiaException(
"Definición invalida para crear la tabla '$table'");
411 $create_lines = array();
413 $unique_index = array();
417 foreach ($definition as $field => $field_def) {
418 if (isset($field_def[
'not_null'])) {
419 $not_null = $field_def[
'not_null'] ?
'NOT NULL' :
'';
423 if (isset($field_def[
'size'])) {
424 $size = $field_def[
'size'] ?
'(' . $field_def[
'size'] .
')' :
'';
428 if (isset($field_def[
'index'])) {
429 if ($field_def[
'index']) {
430 $index[] =
"INDEX($field)";
433 if (isset($field_def[
'unique_index'])) {
434 if ($field_def[
'unique_index']) {
435 $index[] =
"UNIQUE($field)";
438 if (isset($field_def[
'primary'])) {
439 if ($field_def[
'primary']) {
440 $primary[] =
"$field";
443 if (isset($field_def[
'auto'])) {
444 if ($field_def[
'auto']) {
445 $field_def[
'type'] =
"SERIAL";
448 if (isset($field_def[
'extra'])) {
449 $extra = $field_def[
'extra'];
453 $create_lines[] =
"$field " . $field_def[
'type'] . $size .
' ' . $not_null .
' ' . $extra;
455 $create_sql.= join(
',', $create_lines);
456 $last_lines = array();
457 if (count($primary)) {
458 $last_lines[] =
'PRIMARY KEY(' . join(
",", $primary) .
')';
461 $last_lines[] = join(
',', $index);
463 if (count($unique_index)) {
464 $last_lines[] = join(
',', $unique_index);
466 if (count($last_lines)) {
467 $create_sql.=
',' . join(
',', $last_lines) .
')';
469 return $this->
query($create_sql);
479 return $this->
fetch_all(
"SELECT c.relname AS table FROM pg_class c, pg_user u "
480 .
"WHERE c.relowner = u.usesysid AND c.relkind = 'r' "
481 .
"AND NOT EXISTS (SELECT 1 FROM pg_views WHERE viewname = c.relname) "
482 .
"AND c.relname !~ '^(pg_|sql_)' UNION "
483 .
"SELECT c.relname AS table_name FROM pg_class c "
484 .
"WHERE c.relkind = 'r' "
485 .
"AND NOT EXISTS (SELECT 1 FROM pg_views WHERE viewname = c.relname) "
486 .
"AND NOT EXISTS (SELECT 1 FROM pg_user WHERE usesysid = c.relowner) "
487 .
"AND c.relname !~ '^pg_'");
498 $describe = $this->
fetch_all(
"SELECT a.attname AS Field, t.typname AS Type,
499 CASE WHEN attnotnull=false THEN 'YES' ELSE 'NO' END AS Null,
500 CASE WHEN (select cc.contype FROM pg_catalog.pg_constraint cc WHERE
501 cc.conrelid = c.oid AND cc.conkey[1] = a.attnum limit 1)='p' THEN 'PRI' ELSE ''
502 END AS Key, CASE WHEN atthasdef=true THEN TRUE ELSE NULL END AS Default
503 FROM pg_catalog.pg_class c, pg_catalog.pg_attribute a,
504 pg_catalog.pg_type t WHERE c.relname = '$table' AND c.oid = a.attrelid
505 AND a.attnum > 0 AND t.oid = a.atttypid order by a.attnum");
506 $final_describe = array();
507 foreach ($describe as $key => $value) {
508 $final_describe[] = array(
509 "Field" => $value[
"field"],
510 "Type" => $value[
"type"],
511 "Null" => $value[
"null"],
512 "Key" => $value[
"key"],
513 "Default" => $value[
"default"]
516 return $final_describe;
528 if (!$query_result) {
531 return pg_fetch_object($query_result, null, $class);