pg_get_constraintdef() for >= 70400.
Rod Taylor <rbt@rbt.ca>
* back to source text
*
* IDENTIFICATION
- * $Header: /cvsroot/pgsql/src/backend/utils/adt/ruleutils.c,v 1.141 2003/05/28 16:03:59 tgl Exp $
+ * $Header: /cvsroot/pgsql/src/backend/utils/adt/ruleutils.c,v 1.142 2003/06/25 03:56:30 momjian Exp $
*
* This software is copyrighted by Jan Wieck - Hamburg.
*
{
Datum val;
bool isnull;
+ char *conbin;
+ char *consrc;
+ Node *expr;
+ List *context;
/* Start off the constraint definition */
/* The consrc for CHECK constraints always seems to be
appendStringInfo(&buf, "CHECK ");
/* Fetch constraint source */
- val = heap_getattr(tup, Anum_pg_constraint_consrc,
+ val = heap_getattr(tup, Anum_pg_constraint_conbin,
RelationGetDescr(conDesc), &isnull);
if (isnull)
elog(ERROR, "pg_get_constraintdef: Null consrc for constraint %u",
constraintId);
+ conbin = DatumGetCString(DirectFunctionCall1(textout, val));
+ expr = stringToNode(conbin);
+
+ /*
+ * If top level is a List, assume it is an implicit-AND structure, and
+ * convert to explicit AND. This is needed for partial index
+ * predicates.
+ */
+ if (expr && IsA(expr, List))
+ expr = (Node *) make_ands_explicit((List *) expr);
+
+ if (conForm->conrelid != InvalidOid)
+ /* It's a Relation */
+ context = deparse_context_for(get_rel_name(conForm->conrelid),
+ conForm->conrelid);
+ else
+ /*
+ * Since VARNOs aren't allowed in domain constraints, relation context
+ * isn't required as anything other than a shell.
+ */
+ context = deparse_context_for(get_typname(conForm->contypid),
+ InvalidOid);
+
+ consrc = deparse_expression(expr, context, false, false);
+
/* Append the constraint source */
- appendStringInfoString(&buf, DatumGetCString(DirectFunctionCall1(textout, val)));
+ appendStringInfoString(&buf, consrc);
break;
}
* Portions Copyright (c) 1994, Regents of the University of California
*
* IDENTIFICATION
- * $Header: /cvsroot/pgsql/src/backend/utils/cache/lsyscache.c,v 1.97 2003/06/24 23:14:46 momjian Exp $
+ * $Header: /cvsroot/pgsql/src/backend/utils/cache/lsyscache.c,v 1.98 2003/06/25 03:56:31 momjian Exp $
*
* NOTES
* Eventually, the index information should go through here, too.
return '\0';
}
+/*
+ * get_typname
+ * Returns the name of a given type.
+ *
+ * Returns a palloc'd copy of the string, or NULL if no such relation.
+ *
+ * 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
*
* by PostgreSQL
*
* IDENTIFICATION
- * $Header: /cvsroot/pgsql/src/bin/pg_dump/pg_dump.c,v 1.333 2003/06/11 16:29:42 tgl Exp $
+ * $Header: /cvsroot/pgsql/src/bin/pg_dump/pg_dump.c,v 1.334 2003/06/25 03:56:31 momjian Exp $
*
*-------------------------------------------------------------------------
*/
/*
* Fetch and process CHECK constraints for the domain
*/
- appendPQExpBuffer(chkquery, "SELECT conname, consrc "
- "FROM pg_catalog.pg_constraint "
- "WHERE contypid = '%s'::pg_catalog.oid",
- tinfo->oid);
+ if (g_fout->remoteVersion >= 70400)
+ appendPQExpBuffer(chkquery, "SELECT conname,"
+ "pg_catalog.pg_get_constraintdef(oid) AS consrc "
+ "FROM pg_catalog.pg_constraint "
+ "WHERE contypid = '%s'::pg_catalog.oid",
+ tinfo->oid);
+ else
+ appendPQExpBuffer(chkquery, "SELECT conname, 'CHECK (' || consrc || ')'"
+ "FROM pg_catalog.pg_constraint "
+ "WHERE contypid = '%s'::pg_catalog.oid",
+ tinfo->oid);
res = PQexec(g_conn, chkquery->data);
if (!res ||
conname = PQgetvalue(res, i, PQfnumber(res, "conname"));
consrc = PQgetvalue(res, i, PQfnumber(res, "consrc"));
- appendPQExpBuffer(q, "\n\tCONSTRAINT %s CHECK %s",
+ appendPQExpBuffer(q, "\n\tCONSTRAINT %s %s",
fmtId(conname), consrc);
}
tbinfo->relname);
resetPQExpBuffer(query);
- if (g_fout->remoteVersion >= 70300)
- appendPQExpBuffer(query, "SELECT conname, consrc"
+ if (g_fout->remoteVersion >= 70400)
+ appendPQExpBuffer(query, "SELECT conname, "
+ " pg_catalog.pg_get_constraintdef(c1.oid) AS consrc "
+ " from pg_catalog.pg_constraint c1"
+ " where conrelid = '%s'::pg_catalog.oid "
+ " and contype = 'c' "
+ " and not exists "
+ " (select 1 from "
+ " pg_catalog.pg_constraint c2, "
+ " pg_catalog.pg_inherits i "
+ " where i.inhrelid = c1.conrelid "
+ " and (c2.conname = c1.conname "
+ " or (c2.conname[0] = '$' "
+ " and c1.conname[0] = '$')"
+ " )"
+ " and pg_catalog.pg_get_constraintdef(c2.oid) "
+ " = pg_catalog.pg_get_constraintdef(c1.oid) "
+ " and c2.conrelid = i.inhparent) "
+ " order by conname ",
+ tbinfo->oid);
+ else if (g_fout->remoteVersion >= 70300)
+ appendPQExpBuffer(query, "SELECT conname, "
+ " 'CHECK (' || consrc || ')'"
" from pg_catalog.pg_constraint c1"
" where conrelid = '%s'::pg_catalog.oid "
" and contype = 'c' "
" order by conname ",
tbinfo->oid);
else
- appendPQExpBuffer(query, "SELECT rcname as conname, rcsrc as consrc"
+ appendPQExpBuffer(query, "SELECT rcname as conname,"
+ " 'CHECK (' || rcsrc || ')' as consrc"
" from pg_relcheck c1"
" where rcrelid = '%s'::oid "
" and not exists "
if (name[0] != '$')
appendPQExpBuffer(q, "CONSTRAINT %s ",
fmtId(name));
- appendPQExpBuffer(q, "CHECK (%s)", expr);
+ appendPQExpBuffer(q, "%s", expr);
}
PQclear(res2);
}
* Portions Copyright (c) 1996-2002, PostgreSQL Global Development Group
* Portions Copyright (c) 1994, Regents of the University of California
*
- * $Id: lsyscache.h,v 1.72 2003/06/24 23:14:49 momjian Exp $
+ * $Id: lsyscache.h,v 1.73 2003/06/25 03:56:31 momjian Exp $
*
*-------------------------------------------------------------------------
*/
extern Oid get_typ_typrelid(Oid typid);
extern Oid get_element_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 getTypeOutputInfo(Oid type, Oid *typOutput, Oid *typElem,
bool *typIsVarlena);