33 * out of it's tuple
44 *
55 * IDENTIFICATION
6- * $Header: /cvsroot/pgsql/src/backend/utils/adt/ruleutils.c,v 1.24 1999/08/28 03:59:05 tgl Exp $
6+ * $Header: /cvsroot/pgsql/src/backend/utils/adt/ruleutils.c,v 1.25 1999/09/02 03:04:04 tgl Exp $
77 *
88 * This software is copyrighted by Jan Wieck - Hamburg.
99 *
4343#include "optimizer/clauses.h"
4444#include "optimizer/tlist.h"
4545#include "utils/lsyscache.h"
46- #include "catalog/pg_shadow.h"
4746#include "catalog/pg_index.h"
47+ #include "catalog/pg_operator.h"
48+ #include "catalog/pg_shadow.h"
4849
4950#define BUFSIZE 8192
5051
@@ -1270,6 +1271,7 @@ get_rule_expr(QryHier *qh, int rt_index, Node *node, bool varprefix)
12701271case T_Expr :
12711272{
12721273Expr * expr = (Expr * )node ;
1274+ List * args = expr -> args ;
12731275
12741276/* ----------
12751277 * Expr nodes have to be handled a bit detailed
@@ -1279,41 +1281,91 @@ get_rule_expr(QryHier *qh, int rt_index, Node *node, bool varprefix)
12791281{
12801282case OP_EXPR :
12811283strcat (buf ,"(" );
1282- strcat (buf ,get_rule_expr (qh ,rt_index ,
1283- (Node * )get_leftop (expr ),
1284- varprefix ));
1285- strcat (buf ," " );
1286- strcat (buf ,get_opname (((Oper * )expr -> oper )-> opno ));
1287- strcat (buf ," " );
1288- strcat (buf ,get_rule_expr (qh ,rt_index ,
1289- (Node * )get_rightop (expr ),
1290- varprefix ));
1284+ if (length (args )== 2 )
1285+ {
1286+ /* binary operator */
1287+ strcat (buf ,
1288+ get_rule_expr (qh ,rt_index ,
1289+ (Node * )lfirst (args ),
1290+ varprefix ));
1291+ strcat (buf ," " );
1292+ strcat (buf ,
1293+ get_opname (((Oper * )expr -> oper )-> opno ));
1294+ strcat (buf ," " );
1295+ strcat (buf ,
1296+ get_rule_expr (qh ,rt_index ,
1297+ (Node * )lsecond (args ),
1298+ varprefix ));
1299+ }
1300+ else
1301+ {
1302+ /* unary operator --- but which side? */
1303+ Oid opno = ((Oper * )expr -> oper )-> opno ;
1304+ HeapTuple tp ;
1305+ Form_pg_operator optup ;
1306+
1307+ tp = SearchSysCacheTuple (OPROID ,
1308+ ObjectIdGetDatum (opno ),
1309+ 0 ,0 ,0 );
1310+ Assert (HeapTupleIsValid (tp ));
1311+ optup = (Form_pg_operator )GETSTRUCT (tp );
1312+ switch (optup -> oprkind )
1313+ {
1314+ case 'l' :
1315+ strcat (buf ,get_opname (opno ));
1316+ strcat (buf ," " );
1317+ strcat (buf ,
1318+ get_rule_expr (qh ,rt_index ,
1319+ (Node * )lfirst (args ),
1320+ varprefix ));
1321+ break ;
1322+ case 'r' :
1323+ strcat (buf ,
1324+ get_rule_expr (qh ,rt_index ,
1325+ (Node * )lfirst (args ),
1326+ varprefix ));
1327+ strcat (buf ," " );
1328+ strcat (buf ,get_opname (opno ));
1329+ break ;
1330+ default :
1331+ elog (ERROR ,"get_rule_expr: bogus oprkind" );
1332+ }
1333+ }
12911334strcat (buf ,")" );
12921335return pstrdup (buf );
12931336break ;
12941337
12951338case OR_EXPR :
12961339strcat (buf ,"(" );
12971340strcat (buf ,get_rule_expr (qh ,rt_index ,
1298- (Node * )get_leftop (expr ),
1299- varprefix ));
1300- strcat (buf ," OR " );
1301- strcat (buf ,get_rule_expr (qh ,rt_index ,
1302- (Node * )get_rightop (expr ),
1341+ (Node * )lfirst (args ),
13031342varprefix ));
1343+ /* It's not clear that we can ever see N-argument
1344+ * OR/AND clauses here, but might as well cope...
1345+ */
1346+ while ((args = lnext (args ))!= NIL )
1347+ {
1348+ strcat (buf ," OR " );
1349+ strcat (buf ,get_rule_expr (qh ,rt_index ,
1350+ (Node * )lfirst (args ),
1351+ varprefix ));
1352+ }
13041353strcat (buf ,")" );
13051354return pstrdup (buf );
13061355break ;
13071356
13081357case AND_EXPR :
13091358strcat (buf ,"(" );
13101359strcat (buf ,get_rule_expr (qh ,rt_index ,
1311- (Node * )get_leftop (expr ),
1312- varprefix ));
1313- strcat (buf ," AND " );
1314- strcat (buf ,get_rule_expr (qh ,rt_index ,
1315- (Node * )get_rightop (expr ),
1360+ (Node * )lfirst (args ),
13161361varprefix ));
1362+ while ((args = lnext (args ))!= NIL )
1363+ {
1364+ strcat (buf ," AND " );
1365+ strcat (buf ,get_rule_expr (qh ,rt_index ,
1366+ (Node * )lfirst (args ),
1367+ varprefix ));
1368+ }
13171369strcat (buf ,")" );
13181370return pstrdup (buf );
13191371break ;
@@ -1335,7 +1387,7 @@ get_rule_expr(QryHier *qh, int rt_index, Node *node, bool varprefix)
13351387
13361388default :
13371389printf ("\n%s\n" ,nodeToString (node ));
1338- elog (ERROR ,"Expr type not supported" );
1390+ elog (ERROR ,"get_rule_expr: expr type not supported" );
13391391}
13401392}
13411393break ;