PostgreSQL中的Declarations有什么作用
本篇内容主要讲解“PostgreSQL中的Declarations有什么作用”,感兴趣的朋友不妨来看看。本文介绍的方法操作简单快捷,实用性强。下面就让小编来带大家学习“PostgreSQL中的Declarations有什么作用”吧!
PG利用Bison对语法进行分析,Bison输入文件由以下四部分组成:
%{Declarations%}Definitions%%Productions%%UsersubroutinesDeclarations
Declarations与Flex类似,Bison会把这些代码原样拷贝到相应的c文件中(默认为y.tab.c,PG中是gram.c).
名词解释:
terminal symbols —> 终结符
non-terminals symbols —> 非终结符
reduce —> 折叠动作,输入为符合集合(终结符/非终结符),输出为匹配该pattern的非终结符
production —> 产生式,比如S -> S E,成为产生式
%{/*#defineYYDEBUG1*//*-------------------------------------------------------------------------**gram.y*POSTGRESQLBISONrules/actions**PortionsCopyright(c)1996-2018,PostgreSQLGlobalDevelopmentGroup*PortionsCopyright(c)1994,RegentsoftheUniversityofCalifornia***IDENTIFICATION*src/backend/parser/gram.y**HISTORY*AUTHORDATEMAJOREVENT*AndrewYuSept,1994POSTQUELtoSQLconversion*AndrewYuOct,1994lispycodeconversion**NOTES*CAPITALSareusedtorepresentterminalsymbols.*non-capitalsareusedtorepresentnon-terminals.**Ingeneral,nothinginthisfileshouldinitiatedatabaseaccesses*nordependonchangeablestate(suchasSETvariables).Ifyoudo*databaseaccesses,yourcodewillfailwhenwehaveabortedthe*currenttransactionandarejustparsingcommandstofindthenext*ROLLBACKorCOMMIT.IfyoumakeuseofSETvariables,thenyou*willdothewrongthinginmulti-querystringslikethis:*SETconstraint_exclusionTOoff;SELECT*FROMfoo;*becausetheentirestringisparsedbygram.ybeforetheSETgets*executed.Anythingthatdependsonthedatabaseorchangeablestate*shouldbehandledduringparseanalysissothatithappensatthe*righttimenotthewrongtime.**WARNINGS*Ifyouusealist,makesurethedatumisanodesothattheprinting*routineswork.**SometimesweassignconstantstomakeStrings.Makesurewedon'tfree*those.*注意*大写字母用于表示终结符号.*非大写字母用于表示非终结符号.-->文法中的总结符号和非终结符号**通常来说,这个文件中的逻辑不应启用数据库访问,也不应该依赖于可更改的状态(比如SET变量).*如果你确实需要数据库访问,业务代码会在回滚当前事务后出错,然后开始解析命令寻找下一个ROLLBACK/COMMIT.*如果使用了SET变量,那么会在多个查询串中出现错误,比如:*SETconstraint_exclusionTOoff;SELECT*FROMfoo;*因为整个字符串会在SET执行前被gram.y解析*所有依赖数据库或可变状态的事件应该在解析阶段处理以便在正确而非错误的时间发生.**-------------------------------------------------------------------------*/#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"/**Locationtrackingsupport---simplerthanbison'sdefault,sinceweonly*wanttotrackthestartpositionnottheendpositionofeachnonterminal.*位置跟踪支持---比bison默认的处理要简单,因为我们只需要跟踪开始位置而非每个非终结符的结束位置.*/#defineYYLLOC_DEFAULT(Current,Rhs,N)\do{\if((N)>0)\(Current)=(Rhs)[1];\else\(Current)=(-1);\}while(0)/**Theabovemacroassigns-1(unknown)astheparselocationofany*nonterminalthatwasreducedfromanemptyrule,orwhoseleftmost*componentwasreducedfromanemptyrule.Thisisproblematic*fornonterminalsdefinedlike*OptFooList:/*EMPTY*/{...}|OptFooListFoo{...};*becausewe'llset-1asthelocationduringthefirstreductionandthen*copyitduringeachsubsequentreduction,leavinguswith-1forthe*locationevenwhenthelistisnotempty.Tofixthat,dothisinthe*actionforthenonemptyrule(s):*if(@$<0)@$=@2;*(Althoughwehavemanynonterminalsthatfollowthispattern,weonly*botherwithfixing@$likethiswhenthenonterminal'sparselocation*isactuallyreferencedinsomerule.)*上面的宏将-1(未知数)指定为所有非终结符的解析位置,*这些非终结符是从空规则折叠(规约)而来的,或者其最左边的组件是从空规则折叠而来.*对于下面的非终结符,存在问题:*OptFooList:/*EMPTY*/{...}|OptFooListFoo{...};*因为在第一次折叠时将设置值为-1,然后在接下来的折叠中拷贝该值,*这会让就算链表不为空也会一直让位置一直为-1.*为了修正这一错误,对于非空规则,执行这一动作:*if(@$<0)@$=@2;**AcleaneranswerwouldbetomakeYYLLOC_DEFAULTscanalltheRhs*locationsuntilit'sfoundonethat'snot-1.Thenwe'dgetacorrect*locationforanynonterminalthatisn'tentirelyempty.Butthisway*wouldaddoverheadtoeveryrulereduction,andsofarthere'snotbeen*acompellingreasontopaythatoverhead.*更清晰的做法是让YYLLOC_DEFAULT扫描所有的Rhs位置直至找到不为-1为止.*然后我们就可以为完全不为空的非终结符获取正确的位置.*但这样的做法会增加每个规则折叠的负载,到目前为止,还没有一个令人信服的理由来增加开销.*//**Bisondoesn'tallocateanythingthatneedstoliveacrossparsercalls,*sowecaneasilyhaveitusepallocinsteadofmalloc.Thisprevents*memoryleaksifweerroroutduringparsing.Notethisonlyworkswith*bison>=2.0.However,inbison1.875thedefaultistousealloca()*ifpossible,sothere'snotreallymuchproblemanyhow,atleastif*you'rebuildingwithgcc.*Bison不会在解析器调用期间分配内存,因此我们可以很轻松的使用palloc而不是malloc.*这可以防止在解析期间出错而导致的内存泄漏.注意这个特性只在2.0+才会起效.*无论如何,,在bison1.875这个版本,默认使用alloca分配内存,在使用gcc构建时没有太多问题.*/#defineYYMALLOCpalloc#defineYYFREEpfree/*Privatestructfortheresultofprivilege_targetproduction*///privilege_target产生式结果的私有结构体typedefstructPrivTarget{GrantTargetTypetargtype;ObjectTypeobjtype;List*objs;}PrivTarget;/*Privatestructfortheresultofimport_qualificationproduction*///私有结构体-->import_qualification产生式typedefstructImportQual{ImportForeignSchemaTypetype;List*table_names;}ImportQual;/*ConstraintAttributeSpecyieldsanintegerbitmaskoftheseflags:*///ConstraintAttributeSpec产生这些标志的整数位掩码#defineCAS_NOT_DEFERRABLE0x01#defineCAS_DEFERRABLE0x02#defineCAS_INITIALLY_IMMEDIATE0x04#defineCAS_INITIALLY_DEFERRED0x08#defineCAS_NOT_VALID0x10#defineCAS_NO_INHERIT0x20#defineparser_yyerror(msg)scanner_yyerror(msg,yyscanner)#defineparser_errposition(pos)scanner_errposition(pos,yyscanner)staticvoidbase_yyerror(YYLTYPE*yylloc,core_yyscan_tyyscanner,constchar*msg);staticRawStmt*makeRawStmt(Node*stmt,intstmt_location);staticvoidupdateRawStmtEnd(RawStmt*rs,intend_location);staticNode*makeColumnRef(char*colname,List*indirection,intlocation,core_yyscan_tyyscanner);staticNode*makeTypeCast(Node*arg,TypeName*typename,intlocation);staticNode*makeStringConst(char*str,intlocation);staticNode*makeStringConstCast(char*str,intlocation,TypeName*typename);staticNode*makeIntConst(intval,intlocation);staticNode*makeFloatConst(char*str,intlocation);staticNode*makeBitStringConst(char*str,intlocation);staticNode*makeNullAConst(intlocation);staticNode*makeAConst(Value*v,intlocation);staticNode*makeBoolAConst(boolstate,intlocation);staticRoleSpec*makeRoleSpec(RoleSpecTypetype,intlocation);staticvoidcheck_qualified_name(List*names,core_yyscan_tyyscanner);staticList*check_func_name(List*names,core_yyscan_tyyscanner);staticList*check_indirection(List*indirection,core_yyscan_tyyscanner);staticList*extractArgTypes(List*parameters);staticList*extractAggrArgTypes(List*aggrargs);staticList*makeOrderedSetArgs(List*directargs,List*orderedargs,core_yyscan_tyyscanner);staticvoidinsertSelectOptions(SelectStmt*stmt,List*sortClause,List*lockingClause,Node*limitOffset,Node*limitCount,WithClause*withClause,core_yyscan_tyyscanner);staticNode*makeSetOp(SetOperationop,boolall,Node*larg,Node*rarg);staticNode*doNegate(Node*n,intlocation);staticvoiddoNegateFloat(Value*v);staticNode*makeAndExpr(Node*lexpr,Node*rexpr,intlocation);staticNode*makeOrExpr(Node*lexpr,Node*rexpr,intlocation);staticNode*makeNotExpr(Node*expr,intlocation);staticNode*makeAArrayExpr(List*elements,intlocation);staticNode*makeSQLValueFunction(SQLValueFunctionOpop,int32typmod,intlocation);staticNode*makeXmlExpr(XmlExprOpop,char*name,List*named_args,List*args,intlocation);staticList*mergeTableFuncParameters(List*func_args,List*columns);staticTypeName*TableFuncTypeName(List*columns);staticRangeVar*makeRangeVarFromAnyName(List*names,intposition,core_yyscan_tyyscanner);staticvoidSplitColQualList(List*qualList,List**constraintList,CollateClause**collClause,core_yyscan_tyyscanner);staticvoidprocessCASbits(intcas_bits,intlocation,constchar*constrType,bool*deferrable,bool*initdeferred,bool*not_valid,bool*no_inherit,core_yyscan_tyyscanner);staticNode*makeRecursiveViewSelect(char*relname,List*aliases,Node*query);%}
到此,相信大家对“PostgreSQL中的Declarations有什么作用”有了更深的了解,不妨来实际操作一番吧!这里是亿速云网站,更多相关内容可以进入相关频道进行查询,关注我们,继续学习!
声明:本站所有文章资源内容,如无特殊说明或标注,均为采集网络资源。如若本站内容侵犯了原著者的合法权益,可联系本站删除。