ruleutils.c
来自「postgresql8.3.4源码,开源数据库」· C语言 代码 · 共 2,446 行 · 第 1/5 页
C
2,446 行
/*------------------------------------------------------------------------- * * ruleutils.c * Functions to convert stored expressions/querytrees back to * source text * * Portions Copyright (c) 1996-2008, PostgreSQL Global Development Group * Portions Copyright (c) 1994, Regents of the University of California * * * IDENTIFICATION * $PostgreSQL: pgsql/src/backend/utils/adt/ruleutils.c,v 1.269.2.2 2008/06/06 17:59:37 tgl Exp $ * *------------------------------------------------------------------------- */#include "postgres.h"#include <unistd.h>#include <fcntl.h>#include "access/genam.h"#include "catalog/dependency.h"#include "catalog/indexing.h"#include "catalog/pg_authid.h"#include "catalog/pg_constraint.h"#include "catalog/pg_depend.h"#include "catalog/pg_opclass.h"#include "catalog/pg_operator.h"#include "catalog/pg_trigger.h"#include "commands/defrem.h"#include "commands/tablespace.h"#include "executor/spi.h"#include "funcapi.h"#include "nodes/makefuncs.h"#include "optimizer/clauses.h"#include "optimizer/tlist.h"#include "parser/gramparse.h"#include "parser/keywords.h"#include "parser/parse_expr.h"#include "parser/parse_func.h"#include "parser/parse_oper.h"#include "parser/parsetree.h"#include "rewrite/rewriteHandler.h"#include "rewrite/rewriteManip.h"#include "rewrite/rewriteSupport.h"#include "utils/fmgroids.h"#include "utils/lsyscache.h"#include "utils/typcache.h"#include "utils/xml.h"/* ---------- * Pretty formatting constants * ---------- *//* Indent counts */#define PRETTYINDENT_STD 8#define PRETTYINDENT_JOIN 13#define PRETTYINDENT_JOIN_ON (PRETTYINDENT_JOIN-PRETTYINDENT_STD)#define PRETTYINDENT_VAR 4/* Pretty flags */#define PRETTYFLAG_PAREN 1#define PRETTYFLAG_INDENT 2/* macro to test if pretty action needed */#define PRETTY_PAREN(context) ((context)->prettyFlags & PRETTYFLAG_PAREN)#define PRETTY_INDENT(context) ((context)->prettyFlags & PRETTYFLAG_INDENT)/* ---------- * Local data types * ---------- *//* Context info needed for invoking a recursive querytree display routine */typedef struct{ StringInfo buf; /* output buffer to append to */ List *namespaces; /* List of deparse_namespace nodes */ int prettyFlags; /* enabling of pretty-print functions */ int indentLevel; /* current indent level for prettyprint */ bool varprefix; /* TRUE to print prefixes on Vars */} deparse_context;/* * Each level of query context around a subtree needs a level of Var namespace. * A Var having varlevelsup=N refers to the N'th item (counting from 0) in * the current context's namespaces list. * * The rangetable is the list of actual RTEs from the query tree. * * For deparsing plan trees, we provide for outer and inner subplan nodes. * The tlists of these nodes are used to resolve OUTER and INNER varnos. */typedef struct{ List *rtable; /* List of RangeTblEntry nodes */ Plan *outer_plan; /* OUTER subplan, or NULL if none */ Plan *inner_plan; /* INNER subplan, or NULL if none */} deparse_namespace;/* ---------- * Global data * ---------- */static SPIPlanPtr plan_getrulebyoid = NULL;static const char *query_getrulebyoid = "SELECT * FROM pg_catalog.pg_rewrite WHERE oid = $1";static SPIPlanPtr plan_getviewrule = NULL;static const char *query_getviewrule = "SELECT * FROM pg_catalog.pg_rewrite WHERE ev_class = $1 AND rulename = $2";/* ---------- * Local functions * * Most of these functions used to use fixed-size buffers to build their * results. Now, they take an (already initialized) StringInfo object * as a parameter, and append their text output to its contents. * ---------- */static char *deparse_expression_pretty(Node *expr, List *dpcontext, bool forceprefix, bool showimplicit, int prettyFlags, int startIndent);static char *pg_get_viewdef_worker(Oid viewoid, int prettyFlags);static void decompile_column_index_array(Datum column_index_array, Oid relId, StringInfo buf);static char *pg_get_ruledef_worker(Oid ruleoid, int prettyFlags);static char *pg_get_indexdef_worker(Oid indexrelid, int colno, bool showTblSpc, int prettyFlags);static char *pg_get_constraintdef_worker(Oid constraintId, bool fullCommand, int prettyFlags);static char *pg_get_expr_worker(text *expr, Oid relid, char *relname, int prettyFlags);static void make_ruledef(StringInfo buf, HeapTuple ruletup, TupleDesc rulettc, int prettyFlags);static void make_viewdef(StringInfo buf, HeapTuple ruletup, TupleDesc rulettc, int prettyFlags);static void get_query_def(Query *query, StringInfo buf, List *parentnamespace, TupleDesc resultDesc, int prettyFlags, int startIndent);static void get_values_def(List *values_lists, deparse_context *context);static void get_select_query_def(Query *query, deparse_context *context, TupleDesc resultDesc);static void get_insert_query_def(Query *query, deparse_context *context);static void get_update_query_def(Query *query, deparse_context *context);static void get_delete_query_def(Query *query, deparse_context *context);static void get_utility_query_def(Query *query, deparse_context *context);static void get_basic_select_query(Query *query, deparse_context *context, TupleDesc resultDesc);static void get_target_list(List *targetList, deparse_context *context, TupleDesc resultDesc);static void get_setop_query(Node *setOp, Query *query, deparse_context *context, TupleDesc resultDesc);static Node *get_rule_sortgroupclause(SortClause *srt, List *tlist, bool force_colno, deparse_context *context);static char *get_variable(Var *var, int levelsup, bool showstar, deparse_context *context);static RangeTblEntry *find_rte_by_refname(const char *refname, deparse_context *context);static const char *get_simple_binary_op_name(OpExpr *expr);static bool isSimpleNode(Node *node, Node *parentNode, int prettyFlags);static void appendStringInfoSpaces(StringInfo buf, int count);static void appendContextKeyword(deparse_context *context, const char *str, int indentBefore, int indentAfter, int indentPlus);static void get_rule_expr(Node *node, deparse_context *context, bool showimplicit);static void get_oper_expr(OpExpr *expr, deparse_context *context);static void get_func_expr(FuncExpr *expr, deparse_context *context, bool showimplicit);static void get_agg_expr(Aggref *aggref, deparse_context *context);static void get_coercion_expr(Node *arg, deparse_context *context, Oid resulttype, int32 resulttypmod, Node *parentNode);static void get_const_expr(Const *constval, deparse_context *context, int showtype);static void get_sublink_expr(SubLink *sublink, deparse_context *context);static void get_from_clause(Query *query, const char *prefix, deparse_context *context);static void get_from_clause_item(Node *jtnode, Query *query, deparse_context *context);static void get_from_clause_alias(Alias *alias, RangeTblEntry *rte, deparse_context *context);static void get_from_clause_coldeflist(List *names, List *types, List *typmods, deparse_context *context);static void get_opclass_name(Oid opclass, Oid actual_datatype, StringInfo buf);static Node *processIndirection(Node *node, deparse_context *context, bool printit);static void printSubscripts(ArrayRef *aref, deparse_context *context);static char *generate_relation_name(Oid relid);static char *generate_function_name(Oid funcid, int nargs, Oid *argtypes);static char *generate_operator_name(Oid operid, Oid arg1, Oid arg2);static text *string_to_text(char *str);static char *flatten_reloptions(Oid relid);#define only_marker(rte) ((rte)->inh ? "" : "ONLY ")/* ---------- * get_ruledef - Do it all and return a text * that could be used as a statement * to recreate the rule * ---------- */Datumpg_get_ruledef(PG_FUNCTION_ARGS){ Oid ruleoid = PG_GETARG_OID(0); PG_RETURN_TEXT_P(string_to_text(pg_get_ruledef_worker(ruleoid, 0)));}Datumpg_get_ruledef_ext(PG_FUNCTION_ARGS){ Oid ruleoid = PG_GETARG_OID(0); bool pretty = PG_GETARG_BOOL(1); int prettyFlags; prettyFlags = pretty ? PRETTYFLAG_PAREN | PRETTYFLAG_INDENT : 0; PG_RETURN_TEXT_P(string_to_text(pg_get_ruledef_worker(ruleoid, prettyFlags)));}static char *pg_get_ruledef_worker(Oid ruleoid, int prettyFlags){ Datum args[1]; char nulls[1]; int spirc; HeapTuple ruletup; TupleDesc rulettc; StringInfoData buf; /* * Do this first so that string is alloc'd in outer context not SPI's. */ initStringInfo(&buf); /* * Connect to SPI manager */ if (SPI_connect() != SPI_OK_CONNECT) elog(ERROR, "SPI_connect failed"); /* * On the first call prepare the plan to lookup pg_rewrite. We read * pg_rewrite over the SPI manager instead of using the syscache to be * checked for read access on pg_rewrite. */ if (plan_getrulebyoid == NULL) { Oid argtypes[1]; SPIPlanPtr plan; argtypes[0] = OIDOID; plan = SPI_prepare(query_getrulebyoid, 1, argtypes); if (plan == NULL) elog(ERROR, "SPI_prepare failed for \"%s\"", query_getrulebyoid); plan_getrulebyoid = SPI_saveplan(plan); } /* * Get the pg_rewrite tuple for this rule */ args[0] = ObjectIdGetDatum(ruleoid); nulls[0] = ' '; spirc = SPI_execute_plan(plan_getrulebyoid, args, nulls, true, 1); if (spirc != SPI_OK_SELECT) elog(ERROR, "failed to get pg_rewrite tuple for rule %u", ruleoid); if (SPI_processed != 1) appendStringInfo(&buf, "-"); else { /* * Get the rule's definition and put it into executor's memory */ ruletup = SPI_tuptable->vals[0]; rulettc = SPI_tuptable->tupdesc; make_ruledef(&buf, ruletup, rulettc, prettyFlags); } /* * Disconnect from SPI manager */ if (SPI_finish() != SPI_OK_FINISH) elog(ERROR, "SPI_finish failed"); return buf.data;}/* ---------- * get_viewdef - Mainly the same thing, but we * only return the SELECT part of a view * ---------- */Datumpg_get_viewdef(PG_FUNCTION_ARGS){ /* By OID */ Oid viewoid = PG_GETARG_OID(0); PG_RETURN_TEXT_P(string_to_text(pg_get_viewdef_worker(viewoid, 0)));}Datumpg_get_viewdef_ext(PG_FUNCTION_ARGS){ /* By OID */ Oid viewoid = PG_GETARG_OID(0); bool pretty = PG_GETARG_BOOL(1); int prettyFlags; prettyFlags = pretty ? PRETTYFLAG_PAREN | PRETTYFLAG_INDENT : 0; PG_RETURN_TEXT_P(string_to_text(pg_get_viewdef_worker(viewoid, prettyFlags)));}Datumpg_get_viewdef_name(PG_FUNCTION_ARGS){ /* By qualified name */ text *viewname = PG_GETARG_TEXT_P(0); RangeVar *viewrel; Oid viewoid; viewrel = makeRangeVarFromNameList(textToQualifiedNameList(viewname)); viewoid = RangeVarGetRelid(viewrel, false); PG_RETURN_TEXT_P(string_to_text(pg_get_viewdef_worker(viewoid, 0)));}Datumpg_get_viewdef_name_ext(PG_FUNCTION_ARGS){ /* By qualified name */ text *viewname = PG_GETARG_TEXT_P(0); bool pretty = PG_GETARG_BOOL(1); int prettyFlags; RangeVar *viewrel; Oid viewoid; prettyFlags = pretty ? PRETTYFLAG_PAREN | PRETTYFLAG_INDENT : 0; viewrel = makeRangeVarFromNameList(textToQualifiedNameList(viewname)); viewoid = RangeVarGetRelid(viewrel, false); PG_RETURN_TEXT_P(string_to_text(pg_get_viewdef_worker(viewoid, prettyFlags)));}/* * Common code for by-OID and by-name variants of pg_get_viewdef */static char *pg_get_viewdef_worker(Oid viewoid, int prettyFlags){ Datum args[2]; char nulls[2]; int spirc; HeapTuple ruletup; TupleDesc rulettc; StringInfoData buf; /* * Do this first so that string is alloc'd in outer context not SPI's. */ initStringInfo(&buf); /* * Connect to SPI manager */ if (SPI_connect() != SPI_OK_CONNECT) elog(ERROR, "SPI_connect failed"); /* * On the first call prepare the plan to lookup pg_rewrite. We read * pg_rewrite over the SPI manager instead of using the syscache to be * checked for read access on pg_rewrite. */ if (plan_getviewrule == NULL) { Oid argtypes[2]; SPIPlanPtr plan; argtypes[0] = OIDOID; argtypes[1] = NAMEOID; plan = SPI_prepare(query_getviewrule, 2, argtypes); if (plan == NULL) elog(ERROR, "SPI_prepare failed for \"%s\"", query_getviewrule); plan_getviewrule = SPI_saveplan(plan); } /* * Get the pg_rewrite tuple for the view's SELECT rule */ args[0] = ObjectIdGetDatum(viewoid); args[1] = PointerGetDatum(ViewSelectRuleName); nulls[0] = ' '; nulls[1] = ' '; spirc = SPI_execute_plan(plan_getviewrule, args, nulls, true, 2); if (spirc != SPI_OK_SELECT) elog(ERROR, "failed to get pg_rewrite tuple for view %u", viewoid); if (SPI_processed != 1) appendStringInfo(&buf, "Not a view"); else { /* * Get the rule's definition and put it into executor's memory */ ruletup = SPI_tuptable->vals[0]; rulettc = SPI_tuptable->tupdesc; make_viewdef(&buf, ruletup, rulettc, prettyFlags); } /* * Disconnect from SPI manager */ if (SPI_finish() != SPI_OK_FINISH) elog(ERROR, "SPI_finish failed"); return buf.data;}/* ---------- * get_triggerdef - Get the definition of a trigger * ---------- */Datumpg_get_triggerdef(PG_FUNCTION_ARGS){ Oid trigid = PG_GETARG_OID(0); HeapTuple ht_trig; Form_pg_trigger trigrec; StringInfoData buf; Relation tgrel; ScanKeyData skey[1]; SysScanDesc tgscan; int findx = 0; char *tgname; /* * Fetch the pg_trigger tuple by the Oid of the trigger */ tgrel = heap_open(TriggerRelationId, AccessShareLock); ScanKeyInit(&skey[0], ObjectIdAttributeNumber, BTEqualStrategyNumber, F_OIDEQ, ObjectIdGetDatum(trigid)); tgscan = systable_beginscan(tgrel, TriggerOidIndexId, true, SnapshotNow, 1, skey); ht_trig = systable_getnext(tgscan); if (!HeapTupleIsValid(ht_trig)) elog(ERROR, "could not find tuple for trigger %u", trigid); trigrec = (Form_pg_trigger) GETSTRUCT(ht_trig); /* * Start the trigger definition. Note that the trigger's name should never * be schema-qualified, but the trigger rel's name may be. */ initStringInfo(&buf); tgname = NameStr(trigrec->tgname); appendStringInfo(&buf, "CREATE %sTRIGGER %s ", trigrec->tgisconstraint ? "CONSTRAINT " : "", quote_identifier(tgname)); if (TRIGGER_FOR_BEFORE(trigrec->tgtype)) appendStringInfo(&buf, "BEFORE"); else appendStringInfo(&buf, "AFTER"); if (TRIGGER_FOR_INSERT(trigrec->tgtype)) { appendStringInfo(&buf, " INSERT"); findx++; } if (TRIGGER_FOR_DELETE(trigrec->tgtype)) { if (findx > 0) appendStringInfo(&buf, " OR DELETE"); else
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?