ruleutils.c
来自「PostgreSQL 8.1.4的源码 适用于Linux下的开源数据库系统」· C语言 代码 · 共 2,434 行 · 第 1/5 页
C
2,434 行
/* Add the FOR UPDATE/SHARE clause if present */ if (query->rowMarks != NIL) { if (query->forUpdate) appendContextKeyword(context, " FOR UPDATE OF ", -PRETTYINDENT_STD, PRETTYINDENT_STD, 0); else appendContextKeyword(context, " FOR SHARE OF ", -PRETTYINDENT_STD, PRETTYINDENT_STD, 0); sep = ""; foreach(l, query->rowMarks) { int rtindex = lfirst_int(l); RangeTblEntry *rte = rt_fetch(rtindex, query->rtable); appendStringInfo(buf, "%s%s", sep, quote_identifier(rte->eref->aliasname)); sep = ", "; } if (query->rowNoWait) appendStringInfo(buf, " NOWAIT"); }}static voidget_basic_select_query(Query *query, deparse_context *context, TupleDesc resultDesc){ StringInfo buf = context->buf; char *sep; ListCell *l; int colno; /* * Build up the query string - first we say SELECT */ if (PRETTY_INDENT(context)) { context->indentLevel += PRETTYINDENT_STD; appendStringInfoChar(buf, ' '); } appendStringInfo(buf, "SELECT"); /* Add the DISTINCT clause if given */ if (query->distinctClause != NIL) { if (has_distinct_on_clause(query)) { appendStringInfo(buf, " DISTINCT ON ("); sep = ""; foreach(l, query->distinctClause) { SortClause *srt = (SortClause *) lfirst(l); appendStringInfoString(buf, sep); get_rule_sortgroupclause(srt, query->targetList, false, context); sep = ", "; } appendStringInfo(buf, ")"); } else appendStringInfo(buf, " DISTINCT"); } /* Then we tell what to select (the targetlist) */ sep = " "; colno = 0; foreach(l, query->targetList) { TargetEntry *tle = (TargetEntry *) lfirst(l); char *colname; char *attname; if (tle->resjunk) continue; /* ignore junk entries */ appendStringInfoString(buf, sep); sep = ", "; colno++; /* * We special-case Var nodes rather than using get_rule_expr. * This is needed because get_rule_expr will display a whole-row Var * as "foo.*", which is the preferred notation in most contexts, but * at the top level of a SELECT list it's not right (the parser will * expand that notation into multiple columns, yielding behavior * different from a whole-row Var). We want just "foo", instead. */ if (tle->expr && IsA(tle->expr, Var)) { Var *var = (Var *) (tle->expr); char *schemaname; char *refname; get_names_for_var(var, 0, context, &schemaname, &refname, &attname); if (refname && (context->varprefix || attname == NULL)) { if (schemaname) appendStringInfo(buf, "%s.", quote_identifier(schemaname)); if (strcmp(refname, "*NEW*") == 0) appendStringInfoString(buf, "new"); else if (strcmp(refname, "*OLD*") == 0) appendStringInfoString(buf, "old"); else appendStringInfoString(buf, quote_identifier(refname)); if (attname) appendStringInfoChar(buf, '.'); } if (attname) appendStringInfoString(buf, quote_identifier(attname)); else { /* * In the whole-row Var case, refname is what the default AS * name would be. */ attname = refname; } } else { get_rule_expr((Node *) tle->expr, context, true); /* We'll show the AS name unless it's this: */ attname = "?column?"; } /* * Figure out what the result column should be called. In the context * of a view, use the view's tuple descriptor (so as to pick up the * effects of any column RENAME that's been done on the view). * Otherwise, just use what we can find in the TLE. */ if (resultDesc && colno <= resultDesc->natts) colname = NameStr(resultDesc->attrs[colno - 1]->attname); else colname = tle->resname; /* Show AS unless the column's name is correct as-is */ if (colname) /* resname could be NULL */ { if (attname == NULL || strcmp(attname, colname) != 0) appendStringInfo(buf, " AS %s", quote_identifier(colname)); } } /* Add the FROM clause if needed */ get_from_clause(query, " FROM ", context); /* Add the WHERE clause if given */ if (query->jointree->quals != NULL) { appendContextKeyword(context, " WHERE ", -PRETTYINDENT_STD, PRETTYINDENT_STD, 1); get_rule_expr(query->jointree->quals, context, false); } /* Add the GROUP BY clause if given */ if (query->groupClause != NULL) { appendContextKeyword(context, " GROUP BY ", -PRETTYINDENT_STD, PRETTYINDENT_STD, 1); sep = ""; foreach(l, query->groupClause) { GroupClause *grp = (GroupClause *) lfirst(l); appendStringInfoString(buf, sep); get_rule_sortgroupclause(grp, query->targetList, false, context); sep = ", "; } } /* Add the HAVING clause if given */ if (query->havingQual != NULL) { appendContextKeyword(context, " HAVING ", -PRETTYINDENT_STD, PRETTYINDENT_STD, 0); get_rule_expr(query->havingQual, context, false); }}static voidget_setop_query(Node *setOp, Query *query, deparse_context *context, TupleDesc resultDesc){ StringInfo buf = context->buf; bool need_paren; if (IsA(setOp, RangeTblRef)) { RangeTblRef *rtr = (RangeTblRef *) setOp; RangeTblEntry *rte = rt_fetch(rtr->rtindex, query->rtable); Query *subquery = rte->subquery; Assert(subquery != NULL); Assert(subquery->setOperations == NULL); /* Need parens if ORDER BY, FOR UPDATE, or LIMIT; see gram.y */ need_paren = (subquery->sortClause || subquery->rowMarks || subquery->limitOffset || subquery->limitCount); if (need_paren) appendStringInfoChar(buf, '('); get_query_def(subquery, buf, context->namespaces, resultDesc, context->prettyFlags, context->indentLevel); if (need_paren) appendStringInfoChar(buf, ')'); } else if (IsA(setOp, SetOperationStmt)) { SetOperationStmt *op = (SetOperationStmt *) setOp; /* * We force parens whenever nesting two SetOperationStmts. There are * some cases in which parens are needed around a leaf query too, but * those are more easily handled at the next level down (see code * above). */ need_paren = !IsA(op->larg, RangeTblRef); if (need_paren) appendStringInfoChar(buf, '('); get_setop_query(op->larg, query, context, resultDesc); if (need_paren) appendStringInfoChar(buf, ')'); if (!PRETTY_INDENT(context)) appendStringInfoChar(buf, ' '); switch (op->op) { case SETOP_UNION: appendContextKeyword(context, "UNION ", -PRETTYINDENT_STD, PRETTYINDENT_STD, 0); break; case SETOP_INTERSECT: appendContextKeyword(context, "INTERSECT ", -PRETTYINDENT_STD, PRETTYINDENT_STD, 0); break; case SETOP_EXCEPT: appendContextKeyword(context, "EXCEPT ", -PRETTYINDENT_STD, PRETTYINDENT_STD, 0); break; default: elog(ERROR, "unrecognized set op: %d", (int) op->op); } if (op->all) appendStringInfo(buf, "ALL "); if (PRETTY_INDENT(context)) appendContextKeyword(context, "", 0, 0, 0); need_paren = !IsA(op->rarg, RangeTblRef); if (need_paren) appendStringInfoChar(buf, '('); get_setop_query(op->rarg, query, context, resultDesc); if (need_paren) appendStringInfoChar(buf, ')'); } else { elog(ERROR, "unrecognized node type: %d", (int) nodeTag(setOp)); }}/* * Display a sort/group clause. * * Also returns the expression tree, so caller need not find it again. */static Node *get_rule_sortgroupclause(SortClause *srt, List *tlist, bool force_colno, deparse_context *context){ StringInfo buf = context->buf; TargetEntry *tle; Node *expr; tle = get_sortgroupclause_tle(srt, tlist); expr = (Node *) tle->expr; /* * Use column-number form if requested by caller or if expression is a * constant --- a constant is ambiguous (and will be misinterpreted by * findTargetlistEntry()) if we dump it explicitly. */ if (force_colno || (expr && IsA(expr, Const))) { Assert(!tle->resjunk); appendStringInfo(buf, "%d", tle->resno); } else get_rule_expr(expr, context, true); return expr;}/* ---------- * get_insert_query_def - Parse back an INSERT parsetree * ---------- */static voidget_insert_query_def(Query *query, deparse_context *context){ StringInfo buf = context->buf; RangeTblEntry *select_rte = NULL; RangeTblEntry *rte; char *sep; ListCell *l; List *strippedexprs; /* * If it's an INSERT ... SELECT there will be a single subquery RTE for * the SELECT. */ foreach(l, query->rtable) { rte = (RangeTblEntry *) lfirst(l); if (rte->rtekind != RTE_SUBQUERY) continue; if (select_rte) elog(ERROR, "too many RTEs in INSERT"); select_rte = rte; } /* * Start the query with INSERT INTO relname */ rte = rt_fetch(query->resultRelation, query->rtable); Assert(rte->rtekind == RTE_RELATION); if (PRETTY_INDENT(context)) { context->indentLevel += PRETTYINDENT_STD; appendStringInfoChar(buf, ' '); } appendStringInfo(buf, "INSERT INTO %s (", generate_relation_name(rte->relid)); /* * Add the insert-column-names list, and make a list of the actual * assignment source expressions. */ strippedexprs = NIL; sep = ""; foreach(l, query->targetList) { TargetEntry *tle = (TargetEntry *) lfirst(l); if (tle->resjunk) continue; /* ignore junk entries */ appendStringInfoString(buf, sep); sep = ", "; /* * Put out name of target column; look in the catalogs, not at * tle->resname, since resname will fail to track RENAME. */ appendStringInfoString(buf, quote_identifier(get_relid_attribute_name(rte->relid, tle->resno))); /* * Print any indirection needed (subfields or subscripts), and strip * off the top-level nodes representing the indirection assignments. */ strippedexprs = lappend(strippedexprs, processIndirection((Node *) tle->expr, context)); } appendStringInfo(buf, ") "); /* Add the VALUES or the SELECT */ if (select_rte == NULL) { appendContextKeyword(context, "VALUES (", -PRETTYINDENT_STD, PRETTYINDENT_STD, 2); get_rule_expr((Node *) strippedexprs, context, false); appendStringInfoChar(buf, ')'); } else get_query_def(select_rte->subquery, buf, NIL, NULL, context->prettyFlags, context->indentLevel);}/* ---------- * get_update_query_def - Parse back an UPDATE parsetree * ---------- */static voidget_update_query_def(Query *query, deparse_context *context){ StringInfo buf = context->buf; char *sep; RangeTblEntry *rte; ListCell *l; /* * Start the query with UPDATE relname SET */ rte = rt_fetch(query->resultRelation, query->rtable); Assert(rte->rtekind == RTE_RELATION); if (PRETTY_INDENT(context)) { appendStringInfoChar(buf, ' '); context->indentLevel += PRETTYINDENT_STD; } appendStringInfo(buf, "UPDATE %s%s SET ", only_marker(rte), generate_relation_name(rte->relid)); /* Add the comma separated list of 'attname = value' */ sep = ""; foreach(l, query->targetList) { TargetEntry *tle = (TargetEntry *) lfirst(l); Node *expr; if (tle->resjunk) continue; /* ignore junk entries */ appendStringInfoString(buf, sep); sep = ", "; /* * Put out name of target column; look in the catalogs, not at * tle->resname, since resname will fail to track RENAME. */ appendStringInfoString(buf, quote_identifier(get_relid_attribute_name(rte->relid, tle->resno))); /* * Print any indirection needed (subfields or subscripts), and strip * off the top-level nodes representing the indirection assignments. */ expr = processIndirection((Node *) tle->expr, context); appendStringInfo(buf, " = "); get_rule_expr(expr, context, false); } /* Add the FROM clause if needed */ get_from_clause(query, " FROM ", context); /* Finally add a WHERE clause if given */ if (query->jointree->quals != NULL) { appendContextKeyword(context, " WHERE ", -PRETTYINDENT_STD, PRETTYINDENT_STD, 1); get_rule_expr(query->jointree->quals, context, false); }}/* ---------- * get_delete_query_def - Parse back a DELETE parsetree * ---------- */static voidget_delete_query_def(Query *query, deparse_context *context){ StringInfo buf = context->buf; RangeTblEntry *rte; /* * Start the query with DELETE FROM relname */ rte = rt_fetch(query->resultRelation, query->rtable); Assert(rte->rtekind == RTE_RELATION); if (PRETTY_INDENT(context)) { context->indentLevel += PRETTYINDENT_STD; a
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?