您好,登錄后才能下訂單哦!
本篇內(nèi)容主要講解“PostgreSQL中的Declarations有什么作用”,感興趣的朋友不妨來(lái)看看。本文介紹的方法操作簡(jiǎn)單快捷,實(shí)用性強(qiáng)。下面就讓小編來(lái)帶大家學(xué)習(xí)“PostgreSQL中的Declarations有什么作用”吧!
PG利用Bison對(duì)語(yǔ)法進(jìn)行分析,Bison輸入文件由以下四部分組成:
%{ Declarations %} Definitions %% Productions %% User subroutines
Declarations與Flex類似,Bison會(huì)把這些代碼原樣拷貝到相應(yīng)的c文件中(默認(rèn)為y.tab.c,PG中是gram.c).
名詞解釋:
terminal symbols —> 終結(jié)符
non-terminals symbols —> 非終結(jié)符
reduce —> 折疊動(dòng)作,輸入為符合集合(終結(jié)符/非終結(jié)符),輸出為匹配該pattern的非終結(jié)符
production —> 產(chǎn)生式,比如S -> S E,成為產(chǎn)生式
%{ /*#define YYDEBUG 1*/ /*------------------------------------------------------------------------- * * gram.y * POSTGRESQL BISON rules/actions * * Portions Copyright (c) 1996-2018, PostgreSQL Global Development Group * Portions Copyright (c) 1994, Regents of the University of California * * * IDENTIFICATION * src/backend/parser/gram.y * * HISTORY * AUTHOR DATE MAJOR EVENT * Andrew Yu Sept, 1994 POSTQUEL to SQL conversion * Andrew Yu Oct, 1994 lispy code conversion * * NOTES * CAPITALS are used to represent terminal symbols. * non-capitals are used to represent non-terminals. * * In general, nothing in this file should initiate database accesses * nor depend on changeable state (such as SET variables). If you do * database accesses, your code will fail when we have aborted the * current transaction and are just parsing commands to find the next * ROLLBACK or COMMIT. If you make use of SET variables, then you * will do the wrong thing in multi-query strings like this: * SET constraint_exclusion TO off; SELECT * FROM foo; * because the entire string is parsed by gram.y before the SET gets * executed. Anything that depends on the database or changeable state * should be handled during parse analysis so that it happens at the * right time not the wrong time. * * WARNINGS * If you use a list, make sure the datum is a node so that the printing * routines work. * * Sometimes we assign constants to makeStrings. Make sure we don't free * those. * 注意 * 大寫字母用于表示終結(jié)符號(hào). * 非大寫字母用于表示非終結(jié)符號(hào). --> 文法中的總結(jié)符號(hào)和非終結(jié)符號(hào) * * 通常來(lái)說(shuō),這個(gè)文件中的邏輯不應(yīng)啟用數(shù)據(jù)庫(kù)訪問(wèn),也不應(yīng)該依賴于可更改的狀態(tài)(比如SET變量). * 如果你確實(shí)需要數(shù)據(jù)庫(kù)訪問(wèn),業(yè)務(wù)代碼會(huì)在回滾當(dāng)前事務(wù)后出錯(cuò),然后開(kāi)始解析命令尋找下一個(gè)ROLLBACK/COMMIT. * 如果使用了SET變量,那么會(huì)在多個(gè)查詢串中出現(xiàn)錯(cuò)誤,比如: * SET constraint_exclusion TO off; SELECT * FROM foo; * 因?yàn)檎麄€(gè)字符串會(huì)在SET執(zhí)行前被gram.y解析 * 所有依賴數(shù)據(jù)庫(kù)或可變狀態(tài)的事件應(yīng)該在解析階段處理以便在正確而非錯(cuò)誤的時(shí)間發(fā)生. * *------------------------------------------------------------------------- */ #include "postgres.h" #include <ctype.h> #include <limits.h> #include "catalog/index.h" #include "catalog/namespace.h" #include "catalog/pg_am.h" #include "catalog/pg_trigger.h" #include "commands/defrem.h" #include "commands/trigger.h" #include "nodes/makefuncs.h" #include "nodes/nodeFuncs.h" #include "parser/gramparse.h" #include "parser/parser.h" #include "parser/parse_expr.h" #include "storage/lmgr.h" #include "utils/date.h" #include "utils/datetime.h" #include "utils/numeric.h" #include "utils/xml.h" /* * Location tracking support --- simpler than bison's default, since we only * want to track the start position not the end position of each nonterminal. * 位置跟蹤支持 --- 比bison默認(rèn)的處理要簡(jiǎn)單,因?yàn)槲覀冎恍枰欓_(kāi)始位置而非每個(gè)非終結(jié)符的結(jié)束位置. */ #define YYLLOC_DEFAULT(Current, Rhs, N) \ do { \ if ((N) > 0) \ (Current) = (Rhs)[1]; \ else \ (Current) = (-1); \ } while (0) /* * The above macro assigns -1 (unknown) as the parse location of any * nonterminal that was reduced from an empty rule, or whose leftmost * component was reduced from an empty rule. This is problematic * for nonterminals defined like * OptFooList: / * EMPTY * / { ... } | OptFooList Foo { ... } ; * because we'll set -1 as the location during the first reduction and then * copy it during each subsequent reduction, leaving us with -1 for the * location even when the list is not empty. To fix that, do this in the * action for the nonempty rule(s): * if (@$ < 0) @$ = @2; * (Although we have many nonterminals that follow this pattern, we only * bother with fixing @$ like this when the nonterminal's parse location * is actually referenced in some rule.) * 上面的宏將-1(未知數(shù))指定為所有非終結(jié)符的解析位置, * 這些非終結(jié)符是從空規(guī)則折疊(規(guī)約)而來(lái)的,或者其最左邊的組件是從空規(guī)則折疊而來(lái). * 對(duì)于下面的非終結(jié)符,存在問(wèn)題: * OptFooList: / * EMPTY * / { ... } | OptFooList Foo { ... } ; * 因?yàn)樵诘谝淮握郫B時(shí)將設(shè)置值為-1,然后在接下來(lái)的折疊中拷貝該值, * 這會(huì)讓就算鏈表不為空也會(huì)一直讓位置一直為-1. * 為了修正這一錯(cuò)誤,對(duì)于非空規(guī)則,執(zhí)行這一動(dòng)作: * if (@$ < 0) @$ = @2; * * A cleaner answer would be to make YYLLOC_DEFAULT scan all the Rhs * locations until it's found one that's not -1. Then we'd get a correct * location for any nonterminal that isn't entirely empty. But this way * would add overhead to every rule reduction, and so far there's not been * a compelling reason to pay that overhead. * 更清晰的做法是讓YYLLOC_DEFAULT掃描所有的Rhs位置直至找到不為-1為止. * 然后我們就可以為完全不為空的非終結(jié)符獲取正確的位置. * 但這樣的做法會(huì)增加每個(gè)規(guī)則折疊的負(fù)載,到目前為止,還沒(méi)有一個(gè)令人信服的理由來(lái)增加開(kāi)銷. */ /* * Bison doesn't allocate anything that needs to live across parser calls, * so we can easily have it use palloc instead of malloc. This prevents * memory leaks if we error out during parsing. Note this only works with * bison >= 2.0. However, in bison 1.875 the default is to use alloca() * if possible, so there's not really much problem anyhow, at least if * you're building with gcc. * Bison不會(huì)在解析器調(diào)用期間分配內(nèi)存,因此我們可以很輕松的使用palloc而不是malloc. * 這可以防止在解析期間出錯(cuò)而導(dǎo)致的內(nèi)存泄漏.注意這個(gè)特性只在2.0+才會(huì)起效. * 無(wú)論如何,,在bison 1.875這個(gè)版本,默認(rèn)使用alloca分配內(nèi)存,在使用gcc構(gòu)建時(shí)沒(méi)有太多問(wèn)題. */ #define YYMALLOC palloc #define YYFREE pfree /* Private struct for the result of privilege_target production */ //privilege_target產(chǎn)生式結(jié)果的私有結(jié)構(gòu)體 typedef struct PrivTarget { GrantTargetType targtype; ObjectType objtype; List *objs; } PrivTarget; /* Private struct for the result of import_qualification production */ //私有結(jié)構(gòu)體 --> import_qualification產(chǎn)生式 typedef struct ImportQual { ImportForeignSchemaType type; List *table_names; } ImportQual; /* ConstraintAttributeSpec yields an integer bitmask of these flags: */ //ConstraintAttributeSpec產(chǎn)生這些標(biāo)志的整數(shù)位掩碼 #define CAS_NOT_DEFERRABLE 0x01 #define CAS_DEFERRABLE 0x02 #define CAS_INITIALLY_IMMEDIATE 0x04 #define CAS_INITIALLY_DEFERRED 0x08 #define CAS_NOT_VALID 0x10 #define CAS_NO_INHERIT 0x20 #define parser_yyerror(msg) scanner_yyerror(msg, yyscanner) #define parser_errposition(pos) scanner_errposition(pos, yyscanner) static void base_yyerror(YYLTYPE *yylloc, core_yyscan_t yyscanner, const char *msg); static RawStmt *makeRawStmt(Node *stmt, int stmt_location); static void updateRawStmtEnd(RawStmt *rs, int end_location); static Node *makeColumnRef(char *colname, List *indirection, int location, core_yyscan_t yyscanner); static Node *makeTypeCast(Node *arg, TypeName *typename, int location); static Node *makeStringConst(char *str, int location); static Node *makeStringConstCast(char *str, int location, TypeName *typename); static Node *makeIntConst(int val, int location); static Node *makeFloatConst(char *str, int location); static Node *makeBitStringConst(char *str, int location); static Node *makeNullAConst(int location); static Node *makeAConst(Value *v, int location); static Node *makeBoolAConst(bool state, int location); static RoleSpec *makeRoleSpec(RoleSpecType type, int location); static void check_qualified_name(List *names, core_yyscan_t yyscanner); static List *check_func_name(List *names, core_yyscan_t yyscanner); static List *check_indirection(List *indirection, core_yyscan_t yyscanner); static List *extractArgTypes(List *parameters); static List *extractAggrArgTypes(List *aggrargs); static List *makeOrderedSetArgs(List *directargs, List *orderedargs, core_yyscan_t yyscanner); static void insertSelectOptions(SelectStmt *stmt, List *sortClause, List *lockingClause, Node *limitOffset, Node *limitCount, WithClause *withClause, core_yyscan_t yyscanner); static Node *makeSetOp(SetOperation op, bool all, Node *larg, Node *rarg); static Node *doNegate(Node *n, int location); static void doNegateFloat(Value *v); static Node *makeAndExpr(Node *lexpr, Node *rexpr, int location); static Node *makeOrExpr(Node *lexpr, Node *rexpr, int location); static Node *makeNotExpr(Node *expr, int location); static Node *makeAArrayExpr(List *elements, int location); static Node *makeSQLValueFunction(SQLValueFunctionOp op, int32 typmod, int location); static Node *makeXmlExpr(XmlExprOp op, char *name, List *named_args, List *args, int location); static List *mergeTableFuncParameters(List *func_args, List *columns); static TypeName *TableFuncTypeName(List *columns); static RangeVar *makeRangeVarFromAnyName(List *names, int position, core_yyscan_t yyscanner); static void SplitColQualList(List *qualList, List **constraintList, CollateClause **collClause, core_yyscan_t yyscanner); static void processCASbits(int cas_bits, int location, const char *constrType, bool *deferrable, bool *initdeferred, bool *not_valid, bool *no_inherit, core_yyscan_t yyscanner); static Node *makeRecursiveViewSelect(char *relname, List *aliases, Node *query); %}
到此,相信大家對(duì)“PostgreSQL中的Declarations有什么作用”有了更深的了解,不妨來(lái)實(shí)際操作一番吧!這里是億速云網(wǎng)站,更多相關(guān)內(nèi)容可以進(jìn)入相關(guān)頻道進(jìn)行查詢,關(guān)注我們,繼續(xù)學(xué)習(xí)!
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。