mirror of https://github.com/citusdata/citus.git
Implement ConstraintWithNameIsOfType (#4451)
parent
c0e7f31eb0
commit
e91e745dbc
|
@ -741,31 +741,53 @@ TableReferencing(Oid relationId)
|
||||||
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* ConstraintIsAForeignKey is a wrapper around GetForeignKeyOidByName that
|
* ConstraintIsAForeignKey is a wrapper around ConstraintWithNameIsOfType that returns true
|
||||||
* returns true if the given constraint name identifies a foreign key
|
* if given constraint name identifies a foreign key constraint.
|
||||||
* constraint defined on relation with relationId.
|
|
||||||
*/
|
*/
|
||||||
bool
|
bool
|
||||||
ConstraintIsAForeignKey(char *inputConstaintName, Oid relationId)
|
ConstraintIsAForeignKey(char *inputConstaintName, Oid relationId)
|
||||||
{
|
{
|
||||||
Oid foreignKeyId = GetForeignKeyOidByName(inputConstaintName, relationId);
|
return ConstraintWithNameIsOfType(inputConstaintName, relationId, CONSTRAINT_FOREIGN);
|
||||||
return OidIsValid(foreignKeyId);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* GetForeignKeyOidByName returns OID of the foreign key with name and defined
|
* ConstraintWithNameIsOfType is a wrapper around get_relation_constraint_oid that
|
||||||
* on relation with relationId. If there is no such foreign key constraint, then
|
* returns true if given constraint name identifies a valid constraint defined
|
||||||
* this function returns InvalidOid.
|
* on relation with relationId and it's type matches the input constraint type.
|
||||||
*/
|
*/
|
||||||
Oid
|
bool
|
||||||
GetForeignKeyOidByName(char *inputConstaintName, Oid relationId)
|
ConstraintWithNameIsOfType(char *inputConstaintName, Oid relationId,
|
||||||
|
char targetConstraintType)
|
||||||
{
|
{
|
||||||
int flags = INCLUDE_REFERENCING_CONSTRAINTS | INCLUDE_ALL_TABLE_TYPES;
|
bool missingOk = true;
|
||||||
List *foreignKeyOids = GetForeignKeyOids(relationId, flags);
|
Oid constraintId =
|
||||||
|
get_relation_constraint_oid(relationId, inputConstaintName, missingOk);
|
||||||
|
return ConstraintWithIdIsOfType(constraintId, targetConstraintType);
|
||||||
|
}
|
||||||
|
|
||||||
Oid foreignKeyId = FindForeignKeyOidWithName(foreignKeyOids, inputConstaintName);
|
|
||||||
return foreignKeyId;
|
/*
|
||||||
|
* ConstraintWithIdIsOfType returns true if constraint with constraintId exists
|
||||||
|
* and is of type targetConstraintType.
|
||||||
|
*/
|
||||||
|
bool
|
||||||
|
ConstraintWithIdIsOfType(Oid constraintId, char targetConstraintType)
|
||||||
|
{
|
||||||
|
HeapTuple heapTuple = SearchSysCache1(CONSTROID, ObjectIdGetDatum(constraintId));
|
||||||
|
if (!HeapTupleIsValid(heapTuple))
|
||||||
|
{
|
||||||
|
/* no such constraint */
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
Form_pg_constraint constraintForm = (Form_pg_constraint) GETSTRUCT(heapTuple);
|
||||||
|
char constraintType = constraintForm->contype;
|
||||||
|
bool constraintTypeMatches = (constraintType == targetConstraintType);
|
||||||
|
|
||||||
|
ReleaseSysCache(heapTuple);
|
||||||
|
|
||||||
|
return constraintTypeMatches;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -17,6 +17,7 @@
|
||||||
#include "access/xact.h"
|
#include "access/xact.h"
|
||||||
#include "catalog/index.h"
|
#include "catalog/index.h"
|
||||||
#include "catalog/pg_class.h"
|
#include "catalog/pg_class.h"
|
||||||
|
#include "catalog/pg_constraint.h"
|
||||||
#include "commands/tablecmds.h"
|
#include "commands/tablecmds.h"
|
||||||
#include "distributed/citus_ruleutils.h"
|
#include "distributed/citus_ruleutils.h"
|
||||||
#include "distributed/colocation_utils.h"
|
#include "distributed/colocation_utils.h"
|
||||||
|
@ -461,7 +462,9 @@ PreprocessAlterTableStmt(Node *node, const char *alterTableCommand)
|
||||||
*/
|
*/
|
||||||
Assert(list_length(commandList) == 1);
|
Assert(list_length(commandList) == 1);
|
||||||
|
|
||||||
Oid foreignKeyId = GetForeignKeyOidByName(constraintName, leftRelationId);
|
bool missingOk = false;
|
||||||
|
Oid foreignKeyId = get_relation_constraint_oid(leftRelationId,
|
||||||
|
constraintName, missingOk);
|
||||||
rightRelationId = GetReferencedTableId(foreignKeyId);
|
rightRelationId = GetReferencedTableId(foreignKeyId);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -168,7 +168,9 @@ extern bool HasForeignKeyToReferenceTable(Oid relationOid);
|
||||||
extern bool TableReferenced(Oid relationOid);
|
extern bool TableReferenced(Oid relationOid);
|
||||||
extern bool TableReferencing(Oid relationOid);
|
extern bool TableReferencing(Oid relationOid);
|
||||||
extern bool ConstraintIsAForeignKey(char *inputConstaintName, Oid relationOid);
|
extern bool ConstraintIsAForeignKey(char *inputConstaintName, Oid relationOid);
|
||||||
extern Oid GetForeignKeyOidByName(char *inputConstaintName, Oid relationId);
|
extern bool ConstraintWithNameIsOfType(char *inputConstaintName, Oid relationId,
|
||||||
|
char targetConstraintType);
|
||||||
|
extern bool ConstraintWithIdIsOfType(Oid constraintId, char targetConstraintType);
|
||||||
extern void ErrorIfTableHasExternalForeignKeys(Oid relationId);
|
extern void ErrorIfTableHasExternalForeignKeys(Oid relationId);
|
||||||
extern List * GetForeignKeyOids(Oid relationId, int flags);
|
extern List * GetForeignKeyOids(Oid relationId, int flags);
|
||||||
extern Oid GetReferencedTableId(Oid foreignKeyId);
|
extern Oid GetReferencedTableId(Oid foreignKeyId);
|
||||||
|
|
Loading…
Reference in New Issue