home *** CD-ROM | disk | FTP | other *** search
/ Complete Linux / Complete Linux.iso / docs / apps / database / postgres / postgre4.z / postgre4 / src / planner / sys / paramutils.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-08-27  |  3.8 KB  |  171 lines

  1. /*     
  2.  *      FILE
  3.  *         storeplan
  4.  *     
  5.  *      DESCRIPTION
  6.  *         Routines to manipulate param nodes in rule plans.
  7.  *     
  8.  */
  9. #include "tmp/postgres.h"
  10.  
  11. #include "nodes/nodes.h"
  12. #include "nodes/pg_lisp.h"
  13. #include "nodes/primnodes.h"
  14. #include "nodes/primnodes.a.h"
  15.  
  16. #include "utils/lsyscache.h"
  17. #include "planner/paramutils.h"
  18.  
  19.  
  20. /* $Header: /private/postgres/src/planner/sys/RCS/paramutils.c,v 1.3 1991/05/23 18:34:54 kemnitz Exp $ */
  21.  
  22. /*
  23.  * EXPORTS
  24.  * find_parameters
  25.  * substitute_parameters
  26.  */
  27.  
  28. /*
  29.  *        find-parameters
  30.  *    
  31.  *        Extracts all paramids from a plan tree.
  32.  *    
  33.  */
  34.  
  35. LispValue
  36.   find_parameters (plan)
  37. LispValue plan ;
  38. {
  39.     return (remove_duplicates (find_all_parameters (plan)));
  40. }
  41.  
  42. /*  .. find-all-parameters, find-parameters   */
  43.  
  44. LispValue
  45.   find_all_parameters (tree)
  46. LispValue tree ;
  47. {
  48.     if(IsA(tree,Param)) 
  49.     return (lispCons(lispString(get_paramname(tree)),
  50.              LispNil));
  51.     else 
  52.       if (consp (tree)) {
  53.       /* MAPCAN  */
  54.       LispValue t_list = LispNil;
  55.       LispValue temp = LispNil;
  56.       
  57.       foreach (temp, tree) 
  58.         t_list = nconc(t_list, find_all_parameters(CAR(temp)));
  59.       return(t_list);
  60.       }
  61.     return(NULL);
  62.   /*
  63.    * XXX Vector plans are not yet supported.  
  64.  
  65.    else if (vectorp (tree)) {
  66.    LispValue plan_list = LispNil;
  67.    dotimes (i (vsize (tree),plan_list),
  68.    plan_list = nconc (find_all_parameters (aref (tree,i)),
  69.    plan_list););
  70.    
  71.    */
  72. }
  73.  
  74. /*
  75.  *        substitute-parameters
  76.  *    
  77.  *        Find param nodes in a structure and change them to constants, as
  78.  *        specified by a list of parameter ids and values.
  79.  *    
  80.  *        'plan' is a query plan (or part of a query plan) which (may) contain
  81.  *            param nodes.
  82.  *        'params' is a list of values to substitute for parameters in the form:
  83.  *            (fixnum consttype constvalue)        for $1 params
  84.  *            ("attname" consttype constvalue)     for $.attname params
  85.  *    
  86.  *        Returns a new copy of the plan data structure with the parameters
  87.  *        substituted in.
  88.  *    
  89.  */
  90. LispValue
  91.   substitute_parameters (plan,params)
  92. LispValue plan,params ;
  93. {
  94.     if (null (plan)) 
  95.       return(LispNil);
  96.     else 
  97.       if (null (params)) 
  98.     return (plan);
  99.     else {
  100.     LispValue temp = LispNil;
  101.     LispValue tmpnode = LispNil;
  102.     LispValue param_alist = LispNil;
  103.     LispValue paramnode = LispNil;
  104.  
  105.     foreach(temp, params) {
  106.         paramnode = CAR(temp);
  107.         tmpnode = lispCons(CAR(paramnode),  /* XXX is this right? */
  108.                    MakeConst(CInteger(CDR(paramnode)),
  109.                      get_typlen(CInteger(CDR(paramnode))),
  110.                      CInteger(CDR(CDR(paramnode))),
  111.                      LispNil));
  112.         param_alist = nappend1(param_alist,tmpnode);
  113.     }
  114.     return(assoc_params (plan,param_alist));
  115.     }
  116. }  /* function end  */
  117.  
  118.  
  119. /*
  120.  *        assoc-params
  121.  *    
  122.  *        Substitutes the const nodes generated by param-alist into the plan.
  123.  *    
  124.  *        Returns a new copy of the plan data structure with the parameters
  125.  *        substituted in.
  126.  *    
  127.  */
  128.  
  129. /*  .. assoc-params, substitute-parameters  */
  130.  
  131. LispValue
  132.   assoc_params (plan,param_alist)
  133. LispValue plan,param_alist ;
  134. {
  135.     if(IsA(plan,Param)) {
  136.     LispValue tmp = LispNil;
  137.     
  138.     /*  ASSOC  */
  139.     foreach (tmp,param_alist) {
  140.         if (equal(CAR(CAR(tmp)), get_paramname(plan)))
  141.           return(CDR(CAR(tmp)));
  142.     }
  143.     return(plan);
  144.     } else if (consp (plan)) {
  145.     LispValue t_list = LispNil;
  146.     LispValue temp = LispNil;
  147.     
  148.     foreach(temp,plan) 
  149.       t_list = nappend1(t_list, assoc_params(CAR(temp),
  150.                         param_alist));
  151.     return(t_list);
  152.     }
  153.     else
  154.       return(plan);
  155.  
  156.     /*
  157.      *  XXX Vector plans not supported yet!
  158.  
  159.      else if (vectorp (plan)) {
  160.      LispValue vector_size = vsize (plan);
  161.      LispValue newplan = new_vector (vector_size);
  162.      dotimes (i (vector_size),setf (aref (newplan,i),assoc_params (aref (plan,i),param_alist)));
  163.      vsetprop (newplan,vprop (plan));
  164.      newplan;
  165.      
  166.      */
  167.  
  168. }  /* function end */     
  169.   
  170.    
  171.