@@ 4650-4705 (lines=56) @@ | ||
4647 | * |
|
4648 | * @return a recordset |
|
4649 | */ |
|
4650 | public function getConstraintsWithFields($table) |
|
4651 | { |
|
4652 | $c_schema = $this->_schema; |
|
4653 | $this->clean($c_schema); |
|
4654 | $this->clean($table); |
|
4655 | ||
4656 | // get the max number of col used in a constraint for the table |
|
4657 | $sql = "SELECT DISTINCT |
|
4658 | max(SUBSTRING(array_dims(c.conkey) FROM \$patern\$^\\[.*:(.*)\\]$\$patern\$)) as nb |
|
4659 | FROM pg_catalog.pg_constraint AS c |
|
4660 | JOIN pg_catalog.pg_class AS r ON (c.conrelid=r.oid) |
|
4661 | JOIN pg_catalog.pg_namespace AS ns ON (r.relnamespace=ns.oid) |
|
4662 | WHERE |
|
4663 | r.relname = '{$table}' AND ns.nspname='{$c_schema}'"; |
|
4664 | ||
4665 | $rs = $this->selectSet($sql); |
|
4666 | ||
4667 | if ($rs->EOF) { |
|
4668 | $max_col = 0; |
|
4669 | } else { |
|
4670 | $max_col = $rs->fields['nb']; |
|
4671 | } |
|
4672 | ||
4673 | $sql = ' |
|
4674 | SELECT |
|
4675 | c.oid AS conid, c.contype, c.conname, pg_catalog.pg_get_constraintdef(c.oid, true) AS consrc, |
|
4676 | ns1.nspname as p_schema, r1.relname as p_table, ns2.nspname as f_schema, |
|
4677 | r2.relname as f_table, f1.attname as p_field, f1.attnum AS p_attnum, f2.attname as f_field, |
|
4678 | f2.attnum AS f_attnum, pg_catalog.obj_description(c.oid, \'pg_constraint\') AS constcomment, |
|
4679 | c.conrelid, c.confrelid |
|
4680 | FROM |
|
4681 | pg_catalog.pg_constraint AS c |
|
4682 | JOIN pg_catalog.pg_class AS r1 ON (c.conrelid=r1.oid) |
|
4683 | JOIN pg_catalog.pg_attribute AS f1 ON (f1.attrelid=r1.oid AND (f1.attnum=c.conkey[1]'; |
|
4684 | for ($i = 2; $i <= $rs->fields['nb']; $i++) { |
|
4685 | $sql .= " OR f1.attnum=c.conkey[$i]"; |
|
4686 | } |
|
4687 | $sql .= ')) |
|
4688 | JOIN pg_catalog.pg_namespace AS ns1 ON r1.relnamespace=ns1.oid |
|
4689 | LEFT JOIN ( |
|
4690 | pg_catalog.pg_class AS r2 JOIN pg_catalog.pg_namespace AS ns2 ON (r2.relnamespace=ns2.oid) |
|
4691 | ) ON (c.confrelid=r2.oid) |
|
4692 | LEFT JOIN pg_catalog.pg_attribute AS f2 ON |
|
4693 | (f2.attrelid=r2.oid AND ((c.confkey[1]=f2.attnum AND c.conkey[1]=f1.attnum)'; |
|
4694 | for ($i = 2; $i <= $rs->fields['nb']; $i++) { |
|
4695 | $sql .= " OR (c.confkey[$i]=f2.attnum AND c.conkey[$i]=f1.attnum)"; |
|
4696 | } |
|
4697 | ||
4698 | $sql .= sprintf(")) |
|
4699 | WHERE |
|
4700 | r1.relname = '%s' AND ns1.nspname='%s' |
|
4701 | ORDER BY 1", $table, $c_schema); |
|
4702 | ||
4703 | return $this->selectSet($sql); |
|
4704 | } |
|
4705 | ||
4706 | /** |
|
4707 | * Adds a primary key constraint to a table. |
|
4708 | * |
@@ 449-504 (lines=56) @@ | ||
446 | * |
|
447 | * @return a recordset |
|
448 | */ |
|
449 | public function getConstraintsWithFields($table) |
|
450 | { |
|
451 | $c_schema = $this->_schema; |
|
452 | $this->clean($c_schema); |
|
453 | $this->clean($table); |
|
454 | ||
455 | // get the max number of col used in a constraint for the table |
|
456 | $sql = "SELECT DISTINCT |
|
457 | max(SUBSTRING(array_dims(c.conkey) FROM '^\\\\[.*:(.*)\\\\]$')) as nb |
|
458 | FROM pg_catalog.pg_constraint AS c |
|
459 | JOIN pg_catalog.pg_class AS r ON (c.conrelid=r.oid) |
|
460 | JOIN pg_catalog.pg_namespace AS ns ON (r.relnamespace=ns.oid) |
|
461 | WHERE |
|
462 | r.relname = '{$table}' AND ns.nspname='{$c_schema}'"; |
|
463 | ||
464 | $rs = $this->selectSet($sql); |
|
465 | ||
466 | if ($rs->EOF) { |
|
467 | $max_col = 0; |
|
468 | } else { |
|
469 | $max_col = $rs->fields['nb']; |
|
470 | } |
|
471 | ||
472 | $sql = ' |
|
473 | SELECT |
|
474 | c.oid AS conid, c.contype, c.conname, pg_catalog.pg_get_constraintdef(c.oid, true) AS consrc, |
|
475 | ns1.nspname as p_schema, r1.relname as p_table, ns2.nspname as f_schema, |
|
476 | r2.relname as f_table, f1.attname as p_field, f1.attnum AS p_attnum, f2.attname as f_field, |
|
477 | f2.attnum AS f_attnum, pg_catalog.obj_description(c.oid, \'pg_constraint\') AS constcomment, |
|
478 | c.conrelid, c.confrelid |
|
479 | FROM |
|
480 | pg_catalog.pg_constraint AS c |
|
481 | JOIN pg_catalog.pg_class AS r1 ON (c.conrelid=r1.oid) |
|
482 | JOIN pg_catalog.pg_attribute AS f1 ON (f1.attrelid=r1.oid AND (f1.attnum=c.conkey[1]'; |
|
483 | for ($i = 2; $i <= $rs->fields['nb']; $i++) { |
|
484 | $sql .= " OR f1.attnum=c.conkey[$i]"; |
|
485 | } |
|
486 | $sql .= ')) |
|
487 | JOIN pg_catalog.pg_namespace AS ns1 ON r1.relnamespace=ns1.oid |
|
488 | LEFT JOIN ( |
|
489 | pg_catalog.pg_class AS r2 JOIN pg_catalog.pg_namespace AS ns2 ON (r2.relnamespace=ns2.oid) |
|
490 | ) ON (c.confrelid=r2.oid) |
|
491 | LEFT JOIN pg_catalog.pg_attribute AS f2 ON |
|
492 | (f2.attrelid=r2.oid AND ((c.confkey[1]=f2.attnum AND c.conkey[1]=f1.attnum)'; |
|
493 | for ($i = 2; $i <= $rs->fields['nb']; $i++) { |
|
494 | $sql .= " OR (c.confkey[$i]=f2.attnum AND c.conkey[$i]=f1.attnum)"; |
|
495 | } |
|
496 | ||
497 | $sql .= sprintf(")) |
|
498 | WHERE |
|
499 | r1.relname = '%s' AND ns1.nspname='%s' |
|
500 | ORDER BY 1", $table, $c_schema); |
|
501 | ||
502 | return $this->selectSet($sql); |
|
503 | } |
|
504 | ||
505 | // Constraint functions |
|
506 | ||
507 | /** |