head     1.9;
branch   ;
access   ;
symbols  Version_2_1:1.9 C_Demo_1:1.6;
locks    ; strict;
comment  @ * @;


1.9
date     89.12.04.11.42.25;  author goh;  state Exp;
branches ;
next     1.8;

1.8
date     89.10.14.15.55.15;  author sp;  state Exp;
branches ;
next     1.7;

1.7
date     89.10.13.17.53.44;  author hong;  state Exp;
branches ;
next     1.6;

1.6
date     89.09.05.17.14.39;  author mao;  state C_Demo_1;
branches ;
next     1.5;

1.5
date     89.08.24.11.52.54;  author ong;  state Exp;
branches ;
next     1.4;

1.4
date     89.08.01.17.52.07;  author goh;  state Exp;
branches ;
next     1.3;

1.3
date     89.08.01.14.21.04;  author goh;  state Exp;
branches ;
next     1.2;

1.2
date     89.07.30.22.29.51;  author goh;  state Exp;
branches ;
next     1.1;

1.1
date     89.07.20.15.36.02;  author goh;  state Exp;
branches ;
next     ;


desc
@@


1.9
log
@ fixing incorrect defaults when append with null fields
@
text
@
/*    
 *    	lsyscache
 *    
 *    	Routines to access information within system caches	
 *    $Header: RCS/lsyscache.c,v 1.8 89/10/14 15:55:15 sp Exp $
 */


/*    
 *    		retrieve-cache-attribute
 *    		op_class
 *    		get_attname
 *    		get_attnum
 *    		get_atttype
 *    		get_opcode
 *    		op_mergesortable
 *    		op_hashjoinable
 *    		get_commutator
 *    		get_negator
 *    		get_oprrest
 *    		get_oprjoin
 *    		get_regproc
 *    		get_relnatts
 *    		get_rel_name
 *    		get_typlen
 *    		get_typbyval
 *    		get_typdefault
 *    
 *     NOTES:
 *    	Eventually, the index information should go through here, too.
 *    
 *    	Most of these routines call SearchSysCacheStruct() and thus simply
 *    	(1) allocate some space for the return struct and (2) call it.
 *    
 */

#include "pg_lisp.h"
#include "syscache.h"
#include "c.h"
#include "cat.h"
#include "att.h"
#include "rel.h"
#include "attnum.h"

/* 
require ("cstructs");
require ("cdefs");
require ("cacheids");
*/

/*    
 *    	AttributeGetAttName
 *    
 *    	Returns a string.
 *    
 */

/*  .. get_attname
 */
Name
AttributeGetAttName (attribute)
     Attribute attribute ;
{
/*
  for (loop = 0;loop + 1(null) = LispNil;(null) = LispNil;) { 
  if ( loop == 16 == val == 0) {
  string (implode (newlist));
  }
  val = attribute_>attname (attribute,loop);
  ;
  if(!val == 0) {
  newlist = append1 (newlist,val);
  }
  }; /*XXX Do List */
  
}

/*    		---------- AMOP CACHES ----------
 */

/*    
 *    	op_class
 *    
 *    	Return t iff operator 'opid' is in operator class 'opclass'.
 *    
 */

/*  .. in-line-lambda%598040608, match-index-orclause
 */
bool
op_class (opid,opclass)
     ObjectId opid;
     int32 opclass ;
{
    AccessMethodOperatorTupleForm amoptup = 
      new(AccessMethodOperatorTupleFormD);

    if (SearchSysCacheStruct (AMOPOPID,amoptup,opclass,opid,0,0))
      return(true);
    else
      return(false);
}

/*    		---------- ATTRIBUTE CACHES ----------
 */

/*    
 *    	get_attname
 *    
 *    	Given the relation id and the attribute number,
 *    	return the "attname" field from the attribute relation.
 *    
 */

/*  .. fix-parsetree-attnums, parameterize, print_var
 *  .. resno-targetlist-entry
 */

Name
get_attname (relid,attnum)
     ObjectId relid;
     AttributeNumber attnum;
{
    Attribute att_tup = new(AttributeTupleFormD);
    Name retval = (Name)NULL;

    if( SearchSysCacheStruct (ATTNUM,att_tup,relid,attnum,0,0)) {
	retval = (Name)palloc(sizeof(att_tup->attname));
	bcopy(&(att_tup->attname),retval,sizeof(att_tup->attname));
	return(retval);
    }
    else
      return((Name)NULL);
}

/*    
 *    	get_attnum
 *    
 *    	Given the relation id and the attribute name,
 *    	return the "attnum" field from the attribute relation.
 *    
 */

/*  .. fix-parsetree-attnums
 */

AttributeNumber
get_attnum (relid,attname)
     ObjectId relid;
     Name attname ;
{
	Attribute  att_tup = new(AttributeTupleFormD);

	if(SearchSysCacheStruct (ATTNAME,att_tup,relid,attname,0,0) ) 
	  return(att_tup->attnum);
	else
	  return(InvalidAttributeNumber);
}

/*    
 *    	get_atttype
 *    
 *    	Given the relation OID and the attribute number with the relation,
 *    	return the attribute type OID.
 *    
 */

/*  .. resno-targetlist-entry
 */

ObjectId
get_atttype (relid,attnum)
     ObjectId relid;
     AttributeNumber attnum ;
{
    Attribute att_tup = (Attribute)palloc(sizeof(*att_tup));

    if( SearchSysCacheStruct (ATTNUM,att_tup,relid,attnum,0,0) ) 
      return(att_tup->atttypid);
    else
      return((ObjectId)NULL);
}

/*    		---------- INDEX CACHE ----------
 */

/*    	watch this space...
 */

/*    		---------- OPERATOR CACHE ----------
 */

/*    
 *    	get_opcode
 *    
 *    	Returns the regproc id of the routine used to implement an
 *    	operator given the operator uid.
 *    
 */

/*  .. create_hashjoin_node, create_mergejoin_node, relation-sortkeys
 *  .. replace_opid, set-temp-tlist-operators
 */


RegProcedure
get_opcode (opid)
     ObjectId opid;
{
    OperatorTupleForm optup = new(OperatorTupleFormD);

    if( SearchSysCacheStruct (OPROID,optup,opid,0,0,0) ) 
      return(optup->oprcode);
    else
      return((RegProcedure)NULL);
}

/*    
 *    	op_mergesortable
 *    
 *    	Returns the left and right sort operators and types corresponding to a
 *    	mergesortable operator, or nil if the operator is not mergesortable.
 *    
 */

/*  .. mergesortop
 */

LispValue
op_mergesortable (opid,ltype,rtype)
     ObjectId opid;
     ObjectId ltype,rtype ;
{
    OperatorTupleForm optup = new(OperatorTupleFormD);

    if(SearchSysCacheStruct (OPROID,optup,opid,0,0,0) &&
       optup->oprlsortop &&
       optup->oprrsortop && 
       optup->oprleft == ltype &&
       optup->oprright == rtype) 
      return (lispCons (optup->oprlsortop,
	               lispCons (optup->oprrsortop,LispNil)));
    else
      return(LispNil);
}

/*    
 *    	op_hashjoinable
 *    
 *    	Returns the hash operator corresponding to a hashjoinable operator, 
 *    	or nil if the operator is not hashjoinable.
 *    
 */

/*  .. hashjoinop
 */
ObjectId
op_hashjoinable (opid,ltype,rtype)
     ObjectId opid,ltype,rtype ;
{
    OperatorTupleForm optup = new(OperatorTupleFormD);

    if (SearchSysCacheStruct (OPROID,optup,opid,0,0,0) && 
	optup->oprcanhash  &&
	optup->oprleft == ltype &&
	optup->oprright == rtype) 
      return(opid);
    else
      return((ObjectId)NULL);
}

/*    
 *    	get_commutator
 *    
 *    	Returns the corresponding commutator of an operator.
 *    
 */

/*  .. in-line-lambda%598040608
 */
ObjectId
get_commutator (opid)
     ObjectId opid ;
{
    OperatorTupleForm optup = (OperatorTupleForm)palloc(sizeof(* optup));

    if(SearchSysCacheStruct (OPROID,optup,opid,0,0,0))
      return(optup->oprcom);
    else
      return((ObjectId)NULL);
}

/*    
 *    	get_negator
 *    
 *    	Returns the corresponding negator of an operator.
 *    
 */

/*  .. push-nots
 */

ObjectId
get_negator (opid)
     ObjectId opid ;
{
    OperatorTupleForm optup = new(OperatorTupleFormD);

    if(SearchSysCacheStruct (OPROID,optup,opid,0,0,0))
      return(optup->oprnegate);
    else
      return((ObjectId)NULL);
}

/*    
 *    	get_oprrest
 *    
 *    	Returns procedure id for computing selectivity of an operator.
 *    
 */

/*  .. compute_selec
 */
RegProcedure
get_oprrest (opid)
     ObjectId opid ;
{
    OperatorTupleForm optup = new(OperatorTupleFormD);

    if(SearchSysCacheStruct (OPROID,optup,opid,0,0,0))
      return(optup->oprrest );
    else
      return((RegProcedure) NULL);
}

/*    
 *    	get_oprjoin
 *    
 *    	Returns procedure id for computing selectivity of a join.
 *    
 */

/*  .. compute_selec
 */

RegProcedure
get_oprjoin (opid)
     ObjectId opid ;
{
    OperatorTupleForm optup = new(OperatorTupleFormD);

    if(SearchSysCacheStruct (OPROID,optup,opid,0,0,0))
      return(optup->oprjoin);
    else
      return((RegProcedure)NULL);
}

/*    		---------- PROCEDURE CACHE ----------
 */

/*    
 *         get_regproc
 *    
 *         Given the function name
 *         return the "oid" field from the FUNCTION relation.
 *    
 */

ObjectId
get_regproc (funname)
     Name funname ;
{
    return((ObjectId)SearchSysCacheGetAttribute( PRONAME,-3,funname,0,0,0));
}

/*    		---------- RELATION CACHE ----------
 */

/*    
 *    	get_relnatts
 *    
 *    	Returns the number of attributes for a given relation.
 *    
 */

/*  .. expand-targetlist, write-decorate
 */

AttributeNumber
get_relnatts (relid)
     ObjectId relid ;
{
    RelationTupleForm reltup = new(RelationTupleFormD);

    if(SearchSysCacheStruct (RELOID,reltup,relid,0,0,0))
      return(reltup->relnatts);
    else
      return((AttributeNumber)NULL);
}

/*    
 *    	get_rel_name
 *    
 *    	Returns the name of a given relation.
 *    
 */

/*  .. ExecOpenR, new-rangetable-entry
 */

Name
get_rel_name (relid)
     ObjectId relid ;
{
    RelationTupleForm reltup = new(RelationTupleFormD);
    Name retval = (Name)NULL;

    if((SearchSysCacheStruct (RELOID,reltup,relid,0,0,0))) {
	retval = (Name)palloc(sizeof(reltup->relname));
	bcopy(&(reltup->relname),retval,sizeof(reltup->relname));
	return(retval);
    } else
      return((Name)NULL);
}

/*    		---------- TYPE CACHE ----------
 */

/*    
 *    	get_typlen
 *    
 *    	Given the type OID, return the length of the type.
 *    
 */

/*  .. compute-attribute-width, create_tl_element, flatten-tlist
 *  .. resno-targetlist-entry, substitute-parameters
 */

int16
get_typlen (typid)
     ObjectId typid;
{
    TypeTupleForm typtup = new(TypeTupleFormD);

    if (SearchSysCacheStruct (TYPOID,typtup,typid,0,0,0))
      return(typtup->typlen);
    else
      return((int16)NULL);
}

/*    
 *    	get_typbyval
 *    
 *    	Given the type OID, determine whether the type is returned by value or
 *    	not.  Returns 1 if by value, 0 if by reference.
 *    
 */

/*  .. ExecTypeFromTL
 */

bool
get_typbyval (typid)
     ObjectId typid ;
{
    TypeTupleForm typtup = new(TypeTupleFormD);
    if(SearchSysCacheStruct (TYPOID,typtup,typid,0,0,0))
      return((bool)typtup->typbyval);
    else
      return(false);
}

/*    
 *    	get_typdefault
 *    
 *    	Given the type OID, return the default value of the ADT.
 *    
 */

/*  .. resno-targetlist-entry
 */

struct varlena *
get_typdefault (typid)
     ObjectId typid ;
{
    struct varlena *typdefault = 
      (struct varlena *)TypeDefaultRetrieve (typid);
    return(typdefault);
}










@


1.8
log
@get_attnum now expects a 'Name' and not a 'NameData' as its second
argument (which used to confuse SearchSysCache -and dbx too!!!- in
a most glorious way)
Ah! and now it things go wrong it returns a 'InvalidAttributeNumber'
and not a (AttributeNumber) NULL (hm.. which is exactly the same
by the way...)
@
text
@d6 1
a6 1
 *    $Header: RCS/lsyscache.c,v 1.7 89/10/13 17:53:44 hong Exp Locker: sp $
d489 3
a491 11
    if(typid == 19) {
	/*   char16 */
	/*   XXX temporary diddle for char16 */
	return((struct varlena *)malloc(16));
	/*make_string (16,initial_element,space);*/
    } else {
	struct varlena *typdefault = 
	  (struct varlena *)TypeDefaultRetrieve (typid);
	/* if(integerp (typdefault) || vectorip (typdefault) */
	return(typdefault);
    }
@


1.7
log
@fix a bug with use of lispCons()
@
text
@d6 1
a6 1
 *    $Header: RCS/lsyscache.c,v 1.6 89/09/05 17:14:39 mao C_Demo_1 Locker: hong $
d44 1
d151 1
a151 1
     NameData attname ;
d158 1
a158 1
	  return((AttributeNumber)NULL);
@


1.6
log
@Working version of C-only demo
@
text
@d6 1
a6 1
 *    $Header: RCS/lsyscache.c,v 1.5 89/08/24 11:52:54 ong Exp Locker: ong $
d241 2
a242 1
      return (lispCons (optup->oprlsortop,optup->oprrsortop,LispNil) );
@


1.5
log
@copies the tuple returned by SearchSysCache and returns the copy.
@
text
@d6 1
a6 1
 *    $Header: /n/postgres/a/postgres/ong/postgres/src/lib/l-lisp/RCS/lsyscache.c,v 1.4 89/08/01 17:52:07 goh Exp $
@


1.4
log
@*** empty log message ***
@
text
@d6 1
a6 1
 *    $Header: lsyscache.c,v 1.3 89/08/01 14:21:04 goh Locked $
d92 2
a93 1
     LispValue opid,opclass ;
d125 1
d127 5
a131 2
    if( SearchSysCacheStruct (ATTNUM,att_tup,relid,attnum,0,0)) 
	return(AttributeGetAttName (att_tup));
d415 1
d417 5
a421 3
    if(SearchSysCacheStruct (RELOID,reltup,relid,0,0,0))
      return(RelationGetRelationName (reltup));
    else
@


1.3
log
@*** empty log message ***
@
text
@d6 1
a6 1
 *    $Header: lsyscache.c,v 1.2 89/07/30 22:29:51 goh Locked $
d38 1
@


1.2
log
@*** empty log message ***
@
text
@d6 1
a6 1
 *    $Header: lsyscache.c,v 1.1 89/07/20 15:36:02 goh Locked $
d94 1
a94 1
      ALLOCATE(AccessMethodOperatorTupleForm);
d122 1
a122 1
    Attribute att_tup = ALLOCATE(Attribute);
d146 1
a146 1
	Attribute  att_tup = ALLOCATE(Attribute);
d204 1
a204 1
    OperatorTupleForm optup = ALLOCATE(OperatorTupleForm);
d228 1
a228 1
    OperatorTupleForm optup = ALLOCATE(OperatorTupleForm);
d254 1
a254 1
    OperatorTupleForm optup = ALLOCATE(OperatorTupleForm);
d300 1
a300 1
    OperatorTupleForm optup = ALLOCATE(OperatorTupleForm);
d321 1
a321 1
    OperatorTupleForm optup = ALLOCATE(OperatorTupleForm);
d343 1
a343 1
    OperatorTupleForm optup = ALLOCATE(OperatorTupleForm);
d386 1
a386 1
    RelationTupleForm reltup = ALLOCATE(RelationTupleForm);
d408 1
a408 1
    RelationTupleForm reltup = ALLOCATE(RelationTupleForm);
d434 1
a434 1
    TypeTupleForm typtup = ALLOCATE(TypeTupleForm);
d457 1
a457 1
    TypeTupleForm typtup = ALLOCATE(TypeTupleForm);
@


1.1
log
@Initial revision
@
text
@d6 1
a6 1
 *    $Header:$
d235 1
a235 1
      return (list (optup->oprlsortop,optup->oprrsortop));
@
