Commit b833c3d4 authored by Tom Lane's avatar Tom Lane

Fix pg_get_constraintdef() to ensure CHECK constraints are always shown

with required outer parentheses.  Breakage seems to be leftover from
domain-constraint patches.  This could be smarter about suppressing
extra parens, but at this stage of the release cycle I want certainty
not cuteness.
parent ec1fbbb5
...@@ -3,7 +3,7 @@ ...@@ -3,7 +3,7 @@
* back to source text * back to source text
* *
* IDENTIFICATION * IDENTIFICATION
* $Header: /cvsroot/pgsql/src/backend/utils/adt/ruleutils.c,v 1.156 2003/10/02 22:24:54 tgl Exp $ * $Header: /cvsroot/pgsql/src/backend/utils/adt/ruleutils.c,v 1.157 2003/10/04 18:22:59 tgl Exp $
* *
* This software is copyrighted by Jan Wieck - Hamburg. * This software is copyrighted by Jan Wieck - Hamburg.
* *
...@@ -1048,23 +1048,11 @@ pg_get_constraintdef_worker(Oid constraintId, int prettyFlags) ...@@ -1048,23 +1048,11 @@ pg_get_constraintdef_worker(Oid constraintId, int prettyFlags)
Node *expr; Node *expr;
List *context; List *context;
/* Start off the constraint definition */ /* Fetch constraint expression in parsetree form */
/*
* The consrc for CHECK constraints always seems to be
* bracketed, so we don't add extra brackets here.
*/
appendStringInfo(&buf, "CHECK ");
/* If we're pretty-printing we need to add brackets */
if (prettyFlags != 0)
appendStringInfo(&buf, "(");
/* Fetch constraint source */
val = heap_getattr(tup, Anum_pg_constraint_conbin, val = heap_getattr(tup, Anum_pg_constraint_conbin,
RelationGetDescr(conDesc), &isnull); RelationGetDescr(conDesc), &isnull);
if (isnull) if (isnull)
elog(ERROR, "null consrc for constraint %u", elog(ERROR, "null conbin for constraint %u",
constraintId); constraintId);
conbin = DatumGetCString(DirectFunctionCall1(textout, val)); conbin = DatumGetCString(DirectFunctionCall1(textout, val));
...@@ -1078,29 +1066,32 @@ pg_get_constraintdef_worker(Oid constraintId, int prettyFlags) ...@@ -1078,29 +1066,32 @@ pg_get_constraintdef_worker(Oid constraintId, int prettyFlags)
if (expr && IsA(expr, List)) if (expr && IsA(expr, List))
expr = (Node *) make_ands_explicit((List *) expr); expr = (Node *) make_ands_explicit((List *) expr);
/* Set up deparsing context for Var nodes in constraint */
if (conForm->conrelid != InvalidOid) if (conForm->conrelid != InvalidOid)
/* It's a Relation */ {
/* relation constraint */
context = deparse_context_for(get_rel_name(conForm->conrelid), context = deparse_context_for(get_rel_name(conForm->conrelid),
conForm->conrelid); conForm->conrelid);
}
else else
{
/* /* domain constraint --- can't have Vars */
* Since VARNOs aren't allowed in domain constraints, context = NIL;
* relation context isn't required as anything other }
* than a shell.
*/
context = deparse_context_for(get_typname(conForm->contypid),
InvalidOid);
consrc = deparse_expression_pretty(expr, context, false, false, consrc = deparse_expression_pretty(expr, context, false, false,
prettyFlags, 0); prettyFlags, 0);
/* Append the constraint source */ /*
appendStringInfoString(&buf, consrc); * Now emit the constraint definition. There are cases where
* the constraint expression will be fully parenthesized and
/* If we're pretty-printing we need to add brackets */ * we don't need the outer parens ... but there are other
if (prettyFlags != 0) * cases where we do need 'em. Be conservative for now.
appendStringInfo(&buf, ")"); *
* Note that simply checking for leading '(' and trailing ')'
* would NOT be good enough, consider "(x > 0) AND (y > 0)".
*/
appendStringInfo(&buf, "CHECK (%s)", consrc);
break; break;
} }
......
...@@ -7,7 +7,7 @@ ...@@ -7,7 +7,7 @@
* Portions Copyright (c) 1994, Regents of the University of California * Portions Copyright (c) 1994, Regents of the University of California
* *
* IDENTIFICATION * IDENTIFICATION
* $Header: /cvsroot/pgsql/src/backend/utils/cache/lsyscache.c,v 1.107 2003/08/17 19:58:06 tgl Exp $ * $Header: /cvsroot/pgsql/src/backend/utils/cache/lsyscache.c,v 1.108 2003/10/04 18:22:59 tgl Exp $
* *
* NOTES * NOTES
* Eventually, the index information should go through here, too. * Eventually, the index information should go through here, too.
...@@ -1469,37 +1469,6 @@ get_typtype(Oid typid) ...@@ -1469,37 +1469,6 @@ get_typtype(Oid typid)
return '\0'; return '\0';
} }
/*
* get_typname
* Returns the name of a given type.
*
* Returns a palloc'd copy of the string, or NULL if no such type.
*
* NOTE: since type name is not unique, be wary of code that uses this
* for anything except preparing error messages.
*/
char *
get_typname(Oid typid)
{
HeapTuple tp;
tp = SearchSysCache(TYPEOID,
ObjectIdGetDatum(typid),
0, 0, 0);
if (HeapTupleIsValid(tp))
{
Form_pg_type typtup = (Form_pg_type) GETSTRUCT(tp);
char *result;
result = pstrdup(NameStr(typtup->typname));
ReleaseSysCache(tp);
return result;
}
else
return NULL;
}
/* /*
* get_typ_typrelid * get_typ_typrelid
* *
......
...@@ -6,7 +6,7 @@ ...@@ -6,7 +6,7 @@
* Portions Copyright (c) 1996-2003, PostgreSQL Global Development Group * Portions Copyright (c) 1996-2003, PostgreSQL Global Development Group
* Portions Copyright (c) 1994, Regents of the University of California * Portions Copyright (c) 1994, Regents of the University of California
* *
* $Id: lsyscache.h,v 1.81 2003/08/17 19:58:06 tgl Exp $ * $Id: lsyscache.h,v 1.82 2003/10/04 18:22:59 tgl Exp $
* *
*------------------------------------------------------------------------- *-------------------------------------------------------------------------
*/ */
...@@ -83,7 +83,6 @@ extern char get_typtype(Oid typid); ...@@ -83,7 +83,6 @@ extern char get_typtype(Oid typid);
extern Oid get_typ_typrelid(Oid typid); extern Oid get_typ_typrelid(Oid typid);
extern Oid get_element_type(Oid typid); extern Oid get_element_type(Oid typid);
extern Oid get_array_type(Oid typid); extern Oid get_array_type(Oid typid);
extern char *get_typname(Oid relid);
extern void getTypeInputInfo(Oid type, Oid *typInput, Oid *typElem); extern void getTypeInputInfo(Oid type, Oid *typInput, Oid *typElem);
extern void getTypeOutputInfo(Oid type, Oid *typOutput, Oid *typElem, extern void getTypeOutputInfo(Oid type, Oid *typOutput, Oid *typElem,
bool *typIsVarlena); bool *typIsVarlena);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment