/*-------------------------------------------------------------------------
 *
 * pg_constraint.c
 *	  routines to support manipulation of the pg_namespace relation
 *
 * Portions Copyright (c) 1996-2001, PostgreSQL Global Development Group
 * Portions Copyright (c) 1994, Regents of the University of California
 *
 *
 * IDENTIFICATION
 *	  $Header$
 *
 *-------------------------------------------------------------------------
 */
#include "postgres.h"

#include "access/heapam.h"
#include "access/genam.h"
#include "catalog/catname.h"
#include "catalog/indexing.h"
#include "catalog/pg_constraint.h"
#include "catalog/pg_namespace.h"
#include "catalog/pg_depend.h"
#include "utils/array.h"
#include "utils/builtins.h"
#include "utils/fmgroids.h"
#include "utils/lsyscache.h"
#include "utils/syscache.h"


/*
 * ConstraintCreate
 *	Create the constraint table portion as well as any dependencies.
 */
Oid
constraintCreate(Oid relId,
				 const char *constraintName,
				 char constraintType,
				 bool isDeferrable,
				 bool isDeferred,
				 const AttrNumber *constraintKey,
				 int constraintNKeys,
				 Oid foreignRelId,
				 const AttrNumber *foreignKey,
				 int foreignNKeys,
				 char foreignUpdateType,
				 char foreignDeleteType,
				 char foreignMatchType,
				 char *conBin,
				 char *conSrc)
{
	Relation	conDesc;
	HeapTuple	tup;
	char		nulls[Natts_pg_constraint];
	Datum		values[Natts_pg_constraint];
	char		cname[NAMEDATALEN];
	int			i = 0;
	Oid			conOid;
	Datum		conkey[constraintNKeys];
	Datum		confkey[foreignNKeys];
	SysScanDesc rcscan;
	ScanKeyData skey[2];
	ObjectAddress	myself,
					dependee;


	/* sanity checks */
	if (!constraintName)
	{
		/* Wasteful of OIDs but it works.. */
		snprintf(cname, sizeof(cname),
				 "Constraint_%u", GetNewObjectId());

	} else
		/* Copy name */
		snprintf(cname, sizeof(cname),
				 constraintName);

	/* make sure there is no existing constraints of the same name */
	ScanKeyEntryInitialize(&skey[i++],
						   0, Anum_pg_constraint_conrelid, F_OIDEQ,
						   ObjectIdGetDatum(relId));
						   
	ScanKeyEntryInitialize(&skey[i++],
						   0, Anum_pg_constraint_conname, F_NAMEEQ,
						   NameGetDatum(&cname));

	conDesc = heap_openr(ConstraintRelationName, RowExclusiveLock);
	rcscan = systable_beginscan(conDesc, ConstraintRelidNameIndex, true,
								SnapshotNow,
								i, skey);

	tup = systable_getnext(rcscan);
	if (HeapTupleIsValid(tup))
		elog(ERROR, "constraint \"%s\" already exists", cname);

	tup = systable_getnext(rcscan);

	/* Build Datum array for ConstraintKey */
	for (i = 0; i < constraintNKeys; i++)
		conkey[i] = Int16GetDatum(constraintKey[i]);

	/*
	 * Build Datum array for foreignKey. Use a
	 * placeholder entry if otherwise NULL.
	 */
	if (foreignNKeys && foreignNKeys > 0)
		for (i = 0; i < foreignNKeys; i++)
			confkey[i] = Int16GetDatum(foreignKey[i]);
	else
		confkey[0] = Int16GetDatum(0);


	/* initialize nulls and values */
	for (i = 0; i < Natts_pg_constraint; i++)
	{
		nulls[i] = ' ';
		values[i] = (Datum) NULL;
	}

	values[Anum_pg_constraint_conrelid - 1] = ObjectIdGetDatum(relId);
	values[Anum_pg_constraint_conname - 1] = NameGetDatum(&cname);
	values[Anum_pg_constraint_contype - 1] = CharGetDatum(constraintType);
	values[Anum_pg_constraint_condeferrable - 1] = BoolGetDatum(isDeferrable);
	values[Anum_pg_constraint_condeferred - 1] = BoolGetDatum(isDeferred);
	values[Anum_pg_constraint_conkey - 1] = PointerGetDatum(construct_array(conkey,
														constraintNKeys,
														true, 2, 'i'));

	/* Record what we were given, or a placeholder if NULL */
	if (foreignRelId)
		values[Anum_pg_constraint_confrelid - 1] = ObjectIdGetDatum(foreignRelId);
	else
		values[Anum_pg_constraint_confrelid - 1] = ObjectIdGetDatum(InvalidOid);

	/* Record what we were given, or a placeholder if NULL */
	values[Anum_pg_constraint_confkey - 1] = PointerGetDatum(construct_array(confkey,
													foreignNKeys > 0 ? foreignNKeys : 1,
													true, 2, 'i'));

	/* Record what we were given, or a placeholder if NULL */
	if (foreignUpdateType)
		values[Anum_pg_constraint_confupdtype - 1] = CharGetDatum(foreignUpdateType);
	else
		values[Anum_pg_constraint_confupdtype - 1] = CharGetDatum(CONSTRAINT_FKEY_RESTRICT);

	/* Record what we were given, or a placeholder if NULL */
	if (foreignDeleteType)
		values[Anum_pg_constraint_confdeltype - 1] = CharGetDatum(foreignDeleteType);
	else
		values[Anum_pg_constraint_confdeltype - 1] = CharGetDatum(CONSTRAINT_FKEY_RESTRICT);

	/* Record what we were given, or a placeholder if NULL */
	if (foreignMatchType)
		values[Anum_pg_constraint_confmatchtype - 1] = CharGetDatum(foreignMatchType);
	else
		values[Anum_pg_constraint_confmatchtype - 1] = CharGetDatum(CONSTRAINT_FKEY_FULL);

	/*
	 * initialize the binary form of the check constraint.
	 */
	if (conBin)
		values[Anum_pg_constraint_conbin - 1] = DirectFunctionCall1(textin,
										CStringGetDatum(conBin));										
	else
		nulls[Anum_pg_constraint_conbin - 1] = 'n';

	/*
	 * initialize the text form of the check constraint
	 */
	if(conSrc)
		values[Anum_pg_constraint_consrc - 1] = DirectFunctionCall1(textin,
										CStringGetDatum(conSrc));	
	else
		nulls[Anum_pg_constraint_consrc - 1] = 'n';

	tup = heap_formtuple(RelationGetDescr(conDesc), values, nulls);
	if (!HeapTupleIsValid(tup))
		elog(ERROR, "ConstraintCreate: heap_formtuple failed");

	conOid = heap_insert(conDesc, tup);
	if (!OidIsValid(conOid))
		elog(ERROR, "ConstraintCreate: heap_insert failed");

	/* Handle Indicies */
	if (RelationGetForm(conDesc)->relhasindex)
	{
		Relation	idescs[Num_pg_constraint_indices];

		CatalogOpenIndices(Num_pg_constraint_indices, Name_pg_constraint_indices, idescs);
		CatalogIndexInsert(idescs, Num_pg_constraint_indices, conDesc, tup);
		CatalogCloseIndices(Num_pg_constraint_indices, idescs);
	}

	/*
	 * Handle Dependencies
	 */
	myself.classId = RelationGetRelid(conDesc);
	myself.objectId = conOid;
	myself.objectSubId = 0;

	/* The constraint depends on the relation */
	dependee.classId = RelOid_pg_class;
	dependee.objectId = relId;
	dependee.objectSubId = 0;
	dependCreate(&myself, &dependee, true);


	/*
	 * The constraint depends on the foreign relation columns
	 *
	 * Relation dependencies are skipped if we depend
	 * directly on ourselves
	 */
	if (foreignNKeys && foreignNKeys > 0
		&& relId != foreignRelId)
	{
		Assert(foreignNKeys = constraintNKeys);

		for (i = 0; i < foreignNKeys; i++)
		{
			ObjectAddress	depender;
			depender.classId = RelOid_pg_class;
			depender.objectId = relId;
			depender.objectSubId = conkey[i];

			dependee.classId = RelOid_pg_class;
			dependee.objectId = foreignRelId;
			dependee.objectSubId = foreignKey[i];
			dependCreate(&depender, &dependee, false);

			dependCreate(&myself, &dependee, true);
		}
	}

	/* Cleanup */
	heap_close(conDesc, RowExclusiveLock);

	return conOid;
}


void
DropConstraintById(Oid conId, int behavior)
{
	ObjectAddress	myself;
	Relation		conDesc;
	HeapTuple		tup;
	ScanKeyData		skey[1];
	SysScanDesc 	rcscan;
	int				i = 0;
	Relation		ridescs[Num_pg_class_indices];
	Form_pg_constraint	con;

	/* Better be a valid Id */
	Assert(OidIsValid(conId));

	/* CONSTRAINT */
	ScanKeyEntryInitialize(&skey[i++],
						   0, ObjectIdAttributeNumber, F_OIDEQ,
						   ObjectIdGetDatum(conId));

	conDesc = heap_openr(ConstraintRelationName, RowExclusiveLock);
	rcscan = systable_beginscan(conDesc, ConstraintOidIndex, true,
								SnapshotNow,
								i, skey);

	tup = systable_getnext(rcscan);


	/*
	 * Due to circular constraint dependencies we simply
	 * skip the drop when we don't find the constraint rather
	 * than the below:
	 *
	 * 
	 */
	if (!HeapTupleIsValid(tup))
		elog(ERROR, "Constraint OID %d missing", conId);

	con = (Form_pg_constraint) GETSTRUCT(tup);

	/*
	 * Now we need to update the relcheck count
	 * if it was a check constraint being dropped
	 */
	if (con->contype == CONSTRAINT_CHECK)
	{
		Relation		rel;
		HeapTuple		relTup;
		
		rel = heap_openr(RelationRelationName, RowExclusiveLock);

		relTup = SearchSysCache(RELOID,
								ObjectIdGetDatum(con->conrelid),
								0, 0, 0);
		if (!HeapTupleIsValid(relTup))
			elog(ERROR, "DropConstraintById: Relation Tuple non-existant");

		((Form_pg_class) GETSTRUCT(relTup))->relchecks -= 1;

		simple_heap_update(rel, &relTup->t_self, relTup);
		CatalogOpenIndices(Num_pg_class_indices, Name_pg_class_indices, ridescs);
		CatalogIndexInsert(ridescs, Num_pg_class_indices, rel, relTup);
		CatalogCloseIndices(Num_pg_class_indices, ridescs);

		ReleaseSysCache(relTup);

		heap_close(rel, RowExclusiveLock);
	}

	/* Fry the constraint itself*/
	simple_heap_delete(conDesc, &tup->t_self);

	/* Clean up */
	systable_endscan(rcscan);
	heap_close(conDesc, RowExclusiveLock);

	/* Deal with dependencies */
	myself.classId = get_relname_relid(ConstraintRelationName, PG_CATALOG_NAMESPACE);
	myself.objectId = conId;
	myself.objectSubId = 0;
	dependDelete(&myself, behavior);
};
