protected function getConstraintKeys($schemaName, $tableName)
{
$sql = <<<EOD
\tSELECT conname, consrc, contype, indkey, indisclustered FROM (
\t\t\tSELECT
\t\t\t\t\tconname,
\t\t\t\t\tCASE WHEN contype='f' THEN
\t\t\t\t\t\t\tpg_catalog.pg_get_constraintdef(oid)
\t\t\t\t\tELSE
\t\t\t\t\t\t\t'CHECK (' || consrc || ')'
\t\t\t\t\tEND AS consrc,
\t\t\t\t\tcontype,
\t\t\t\t\tconrelid AS relid,
\t\t\t\t\tNULL AS indkey,
\t\t\t\t\tFALSE AS indisclustered
\t\t\tFROM
\t\t\t\t\tpg_catalog.pg_constraint
\t\t\tWHERE
\t\t\t\t\tcontype IN ('f', 'c')
\t\t\tUNION ALL
\t\t\tSELECT
\t\t\t\t\tpc.relname,
\t\t\t\t\tNULL,
\t\t\t\t\tCASE WHEN indisprimary THEN
\t\t\t\t\t\t\t'p'
\t\t\t\t\tELSE
\t\t\t\t\t\t\t'u'
\t\t\t\t\tEND,
\t\t\t\t\tpi.indrelid,
\t\t\t\t\tindkey,
\t\t\t\t\tpi.indisclustered
\t\t\tFROM
\t\t\t\t\tpg_catalog.pg_class pc,
\t\t\t\t\tpg_catalog.pg_index pi
\t\t\tWHERE
\t\t\t\t\tpc.oid=pi.indexrelid
\t\t\t\t\tAND EXISTS (
\t\t\t\t\t\t\tSELECT 1 FROM pg_catalog.pg_depend d JOIN pg_catalog.pg_constraint c
\t\t\t\t\t\t\tON (d.refclassid = c.tableoid AND d.refobjid = c.oid)
\t\t\t\t\t\t\tWHERE d.classid = pc.tableoid AND d.objid = pc.oid AND d.deptype = 'i' AND c.contype IN ('u', 'p')
\t\t\t)
\t) AS sub
\tWHERE relid = (SELECT oid FROM pg_catalog.pg_class WHERE relname=:table
\t\t\t\t\tAND relnamespace = (SELECT oid FROM pg_catalog.pg_namespace
\t\t\t\t\tWHERE nspname=:schema))
\tORDER BY
\t\t\t1
EOD;
$this->getDbConnection()->setActive(true);
$command = $this->getDbConnection()->createCommand($sql);
$command->bindValue(':table', $tableName);
$command->bindValue(':schema', $schemaName);
$primary = array();
$foreign = array();
foreach ($command->query() as $row) {
switch ($row['contype']) {
case 'p':
$primary = $this->getPrimaryKeys($tableName, $schemaName, $row['indkey']);
break;
case 'f':
if (($fkey = $this->getForeignKeys($row['consrc'])) !== null) {
$foreign[] = $fkey;
}
break;
}
}
return array($primary, $foreign);
}