Last active
July 4, 2017 06:41
-
-
Save alikon/f0325cd4ec11eeb826c33ffecdac5278 to your computer and use it in GitHub Desktop.
getTableForeginKeys
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public function getTableForeignKeys($table) | |
{ | |
$this->connect(); | |
$query = "SELECT c.conname AS constraint_name, | |
CASE c.contype | |
WHEN 'c' THEN 'CHECK' | |
WHEN 'f' THEN 'FOREIGN KEY' | |
WHEN 'p' THEN 'PRIMARY KEY' | |
WHEN 'u' THEN 'UNIQUE' | |
END AS constraint_type, | |
CASE WHEN c.condeferrable = 'f' THEN 0 ELSE 1 END AS is_deferrable, | |
CASE WHEN c.condeferred = 'f' THEN 0 ELSE 1 END AS is_deferred, | |
t.relname AS table_name, | |
array_to_string(c.conkey, ' ') AS constraint_key, | |
CASE confupdtype | |
WHEN 'a' THEN 'NO ACTION' | |
WHEN 'r' THEN 'RESTRICT' | |
WHEN 'c' THEN 'CASCADE' | |
WHEN 'n' THEN 'SET NULL' | |
WHEN 'd' THEN 'SET DEFAULT' | |
END AS on_update, | |
CASE confdeltype | |
WHEN 'a' THEN 'NO ACTION' | |
WHEN 'r' THEN 'RESTRICT' | |
WHEN 'c' THEN 'CASCADE' | |
WHEN 'n' THEN 'SET NULL' | |
WHEN 'd' THEN 'SET DEFAULT' | |
END AS on_delete, | |
CASE confmatchtype | |
WHEN 'u' THEN 'UNSPECIFIED' | |
WHEN 'f' THEN 'FULL' | |
WHEN 'p' THEN 'PARTIAL' | |
END AS match_type, | |
t2.relname AS references_table, | |
array_to_string(c.confkey, ' ') AS fk_constraint_key | |
FROM pg_constraint c | |
LEFT JOIN pg_class t ON c.conrelid = t.oid | |
LEFT JOIN pg_class t2 ON c.confrelid = t2.oid | |
WHERE t.relname = " . $this->quote($table) . " and c.contype ='f'"; | |
$this->setQuery($query); | |
try | |
{ | |
$keys = $this->loadObjectList(); | |
} | |
catch (Exception $e) | |
{ | |
JLog::add(JText::sprintf('JLIB_DATABASE_QUERY_FAILED', $this->errorNum, $this->errorMsg), JLog::ERROR, 'database'); | |
throw new JDatabaseExceptionExecuting($query, $this->errorMsg, $this->errorNum); | |
} | |
return $keys; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment