Package gudusoft.gsqlparser.nodes
Class TParseTreeNode
Object
gudusoft.gsqlparser.nodes.TParseTreeNode
- All Implemented Interfaces:
Visitable,Iterator<TSourceToken>
- Direct Known Subclasses:
TAbortSqlNode,TAliasClause,TAlterAccessPolicySqlNode,TAlterAuthenticationSqlNode,TAlterDatabaseSqlNode,TAlterFaultGroupSqlNode,TAlterFunctionSqlNode,TAlterIndexSqlNode,TAlterLibrarySqlNode,TAlterNodeSqlNode,TAlterPartitionFunctionSqlNode,TAlterPartitionSchemeSqlNode,TAlterProcedureSqlNode,TAlterRoleSqlNode,TAlterSchemaSqlNode,TAlterSequenceSqlNode,TAlterSessionSqlNode,TAlterTableOption,TAlterTableSqlNode,TAlterTriggerSqlNode,TAlterTypeOption,TAlterTypeSqlNode,TAlterViewSqlNode,TAnalyticFunction,TAnalyzeSqlNode,TArrayAccess,TArrayConstruct,TAtBeforeClause,TAttributeOption,TAttributeOrMethod,TAuthorizationClause,TAutomaticProperty,TBaseTablePartition,TBeginTranSqlNode,TBindArgument,TBinding,TBreakSqlNode,TBuildIndexesSqlNode,TCallSpec,TCallSqlNode,TCaseExpression,TCaseJoinClause,TCaseJoinItem,TCharacterDatatypeProperty,TCloseSqlNode,TClusterBy,TCollectColumnIndex,TCollectFromOption,TCollectStatisticsSqlNode,TColumnAttribute,TColumnDefinition,TColumnGeneratedClause,TColumnReference,TColumnStorage,TColumnWithSortOrder,TCommentSqlNode,TCommitSqlNode,TCompoundSqlNode,TCompoundTriggerBodyNode,TComputeClause,TComputeClauseItem,TComputeExpr,TConnectByClause,TConnectSqlNode,TConstant,TConstraint,TConstructorBody,TConstructorSpec,TContainsTable,TContinueSqlNode,TCopyIntoNode,TCopyIntoSqlNode,TCopySqlNode,TCreateAccessPolicySqlNode,TCreateDatabaseSqlNode,TCreateDirectorySqlNode,TCreateFunctionOption,TCreateFunctionSqlNode,TCreateIndexSqlNode,TCreateMacroSqlNode,TCreateMaterializedViewLogSqlNode,TCreateMaterializedViewSqlNode,TCreateModelSqlNode,TCreatePackageSqlNode,TCreatePartitionFunctionSqlNode,TCreatePartitionSchemeSqlNode,TCreatePipeSqlNode,TCreateProcedureSqlNode,TCreateRoleSqlNode,TCreateRowTypeSqlNode,TCreateSchemaSqlNode,TCreateSecurityPolicySqlNode,TCreateSemanticViewSqlNode,TCreateSequenceSqlNode,TCreateStageSqlNode,TCreateStreamSqlNode,TCreateSynonymSqlNode,TCreateTableOption,TCreateTableSqlNode,TCreateTaskSqlNode,TCreateTriggerSqlNode,TCreateTypeSqlNode,TCreateUserSqlNode,TCreateViewSqlNode,TCustomForXMLItem,TCustomSqlStatement,TDataChangeTable,TDataConversion,TDataConversionItem,TDataDefinition,TDatatypeAttribute,TDatetimeExpression,TDaxExprPair,TDaxNameExpression,TDaxOrderByExpr,TDaxVar,TDeallocatePrepareSqlNode,TDeclareSqlNode,TDeclareVariable,TDeleteSqlNode,TDeleteStatisticsSqlNode,TDescribeSqlNode,TDisableEnableTriggerSqlNode,TDistributeBy,TDoExecuteBlockSqlNode,TDropDatabaseSqlNode,TDropDbObjectSqlNode,TDropFunctionSqlNode,TDropIndexItem,TDropIndexSqlNode,TDropProcedureSqlNode,TDropSchemaSqlNode,TDropSequenceSqlNode,TDropTableSqlNode,TDropViewSqlNode,TDummy,TElseIfSqlNode,TEndTranSqlNode,TErrorLoggingClause,TExceptionClause,TExceptionHandler,TExceptReplaceClause,TExecImmeNode,TExecParameter,TExecuteAsClause,TExecuteAsSqlNode,TExecuteOption,TExecuteSqlNode,TExpandOnClause,TExplicitDataTypeConversion,TExportDataSqlNode,TExportToVerticaSqlNode,TExpression,TExpressionCallTarget,TExternalTableOption,TFetchFirstClause,TFetchSqlNode,TFileFormatSqlNode,TFlashback,TForSqlNode,TForUpdate,TForXMLClause,TFrameExclusionClause,TFunctionCall,TFunctionHeader,TGenerateStatisticsSqlNode,TGetDiagSqlNode,TGrantSqlNode,TGroomTableSqlNode,TGroupBy,TGroupByItem,TGroupConcatParam,TGroupingExpressionItem,TGroupingSet,TGroupingSetItem,THandlerForCondition,THashByClause,THavingClause,THierarchical,THintClause,THiveCreateRoleSqlNode,THiveDescribeSqlNode,THiveDescTablePartition,THiveDescTabType,THiveDropDBSqlNode,THiveDropFunctionSqlNode,THiveDropRoleSqlNode,THiveExplainSqlNode,THiveExportSqlNode,THiveFromQuerySqlNode,THiveGrantRoleSqlNode,THiveGrantSqlNode,THiveHintClause,THiveHintItem,THiveImportSqlNode,THiveIndexProperties,THiveKeyValueProperty,THiveLoadSqlNode,THivePartitionedTableFunction,THivePrincipalName,THivePrivilegeDef,THiveRecordReader,THiveRecordWriter,THiveRowFormat,THiveShowGrantSqlNode,THiveShowRoleGrantSqlNode,THiveShowSqlNode,THiveSwitchDBSqlNode,THiveTableBuckets,THiveTableFileFormat,THiveTablePartition,THiveTableProperties,THiveTableSkewed,THiveTerminatedIdentifier,THiveTransformClause,THiveVariable,THiveWithDBPropertiesClause,TIdentityClause,TIfSqlNode,TIncludeColumns,TIndexColName,TIndexDefinition,TIndexKeyTerm,TIndexRef,TIndices,TIndirection,TInExpr,TInferKeyspaceSqlNode,TInformixOuterClause,TInheritsClause,TInsertBulkSqlNode,TInsertCondition,TInsertIntoValue,TInsertSqlNode,TIntervalExpression,TIntoClause,TIntoTableClause,TInvokerRightsClause,TIsolationClause,TIterateSqlNode,TJoinItem,TJsonObjectKeyValue,TKeepDenseRankClause,TKeyAction,TKeyReference,TKeyspaceRef,TKeyValueSqlNode,TLateralView,TLeaveSqlNode,TLimitClause,TListaggOverflow,TListSubpartitionDesc,TListValuesClause,TLockingClause,TLockSqlNode,TMaterializedViewProps,TMdxAlterCubeNode,TMdxAxis,TMdxCalcPropNode,TMdxCallNode,TMdxCreateMeasureNode,TMdxCreateMemberNode,TMdxCreateSessionCubeNode,TMdxCreateSetNode,TMdxCreateSubCubeNode,TMdxDimContentNode,TMdxDrillthroughNode,TMdxExpNode,TMdxKeySegment,TMdxLevelContentNode,TMdxNameSegment,TMdxObjectNode,TMdxScopeNode,TMdxSelectNode,TMdxWhereNode,TMdxWithNode,TMergeActionClause,TMergeSqlNode,TMergeWhenClause,TModeChoice,TMoveSqlNode,TMSCKSqlNode,TMssqlBeginConversationTimerSqlNode,TMssqlBeginDialogSqlNode,TMssqlBulkInsertSqlNode,TMssqlCreateTriggerUpdateColumn,TMssqlCreateTypeSqlNode,TMssqlCreateXmlSchemaCollectionSqlNode,TMssqlDeallocateSqlNode,TMssqlEndConversationSqlNode,TMssqlGotoSqlNode,TMssqlLabelSqlNode,TMssqlRaiserrorSqlNode,TMssqlReconfigureSqlNode,TMssqlRevertSqlNode,TMssqlSendOnConversationSqlNode,TMssqlSetSqlNode,TMssqlStmtStubSqlNode,TMssqlThrowSqlNode,TMssqlUpdateTextSqlNode,TMultiTarget,TMySQLCreateTableOption,TMySQLIndexOption,TMySQLIndexStorageType,TNameValuePair,TNewVariantTypeArgument,TNodeWithAliasClause,TNodeWithLabel,TNullSqlNode,TObjectAccess,TObjectConstruct,TObjectName,TObjectReference,TOffsetClause,TOpenDatasource,TOpenQuery,TOpenRowSet,TOpenSqlNode,TOpenXML,TOptimizeForClause,TOptionClause,TOracleBuildClause,TOracleCacheClause,TOracleCreateLibrarySqlNode,TOracleCreateMvRefresh,TOracleCreateMvRefreshOption,TOracleLoggingClause,TOracleParallelClause,TOracleUsingIndex,TOrderBy,TOrderByItem,TOutputClause,TOutputFormatPhrase,TPair,TParallelEnableClause,TParameterDeclaration,TParameterMode,TParseErrorSqlNode,TParseTreeNodeList,TPartitionBoundSpecSqlNode,TPartitionByClause,TPartitionClause,TPartitionDefinition,TPartitionExtensionClause,TPartitioningLevel,TPartitionSplitMergeClause,TPathSqlNode,TPerformSqlNode,TPeriodForClause,TPhysicalAttributesClause,TPhysicalAttributesItem,TPhysicalProperties,TPivotedTable,TPivotInClause,TPrecisionScale,TPredictArgument,TPrepareSqlNode,TProcedureOption,TPTNodeList,TPxGranule,TQualifyClause,TQueryHint,TRangeNFunctionItem,TRangeSubpartitionDesc,TRangeValuesClause,TReclaimChoice,TRedshiftTableAttributes,TRelationExpr,TReplaceExprAsIdentifier,TResetSqlNode,TRestrictionClause,TResultCacheClause,TResultSetDefinition,TReturningClause,TReturnSqlNode,TRevokeSqlNode,TRoleOption,TRoleSpec,TRollbackSqlNode,TRollupCube,TSampleClause,TScriptSqlNode,TSegmentAttributesClause,TSegmentAttributesItem,TSelectDistinct,TSelectModifier,TSelectSqlNode,TSemanticAttributeDef,TSemanticRelationshipDef,TSemanticTableDef,TSemanticViewClause,TSequenceOption,TSetAssignment,TSetRowCountSqlNode,TSetSessionSqlNode,TSetSqlNode,TSignalSqlNode,TSizeClause,TSortBy,TStageLocation,TStageReference,TStatementList,TStatementSqlNode,TStorageClause,TStorageItem,TStreamingClause,TStubStmtSqlNode,TSubPartitionByClause,TSubscripts,TSupplementalLogging,TSybaseDumpTranSqlNode,TSybaseUpdateIndexStatisticsSqlNode,TTableElement,TTableHint,TTablePartitionItem,TTableProperties,TTableReference,TTableSample,TTableSamplePart,TTaskOption,TTDUnpivot,TTeradataGiveSqlNode,TTeradataLockClause,TTeradataLockSqlNode,TTeradataStmtStubSqlNode,TTeradataUsingSqlNode,TTeradataWithClause,TTeradataWithClauseItem,TTimeSeries,TTimeTravel,TTimingPoint,TTopClause,TTriggerAction,TTriggerEventClause,TTriggerEventItem,TTriggeringClause,TTriggerReferencingClause,TTriggerReferencingItem,TTrimArgument,TTruncateTableSqlNode,TTryCatchSqlNode,TTypeAttribute,TTypeName,TUnloadSqlNode,TUnnestClause,TUnpackOption,TUnpivotInClause,TUnpivotInClauseItem,TUnsetSqlNode,TUpdateFor,TUpdateSqlNode,TUpsertSqlNode,TUseKeyIndex,TVacuumSqlNode,TValueClause,TValueRowItem,TViewAliasClause,TViewAliasItem,TWhenClauseItem,TWhereClause,TWindowClause,TWindowDef,TWindowDefinition,TWindowFrame,TWindowFrameBoundary,TWindowPartitioningSpec,TWindowSpecification,TWithinGroup,TWithTableLock,TWritetextSqlNode,TXMLAttributesClause,TXmlElement,TXMLPassingClause,TXmlRoot,TXmlSerialize,TXmlTable,TXmlTableParameter
TParseTreeNode - Core Parse Tree Node Implementation
Overview
This class is the root class for all syntax/parse tree nodes in the GSP SQL Parser. A parse tree node represents a syntactic element of a SQL statement, composed of a sequence of tokens generated by the lexer. The node maintains references to its start and end tokens in a double-linked token chain, enabling both traversal and modification of the SQL text.Architecture and Design
Token Chain Structure
The parse tree is built on a double-linked list ofTSourceToken objects. Each node:
- Has a
startTokenandendTokenthat define its boundaries - Can iterate through its tokens using the token chain links
- Maintains relationships with other nodes that may share the same tokens
Critical: Node Nesting and Token Sharing
Nodes in the parse tree are nested and share tokens. This is fundamental to the design:- A WHERE clause node contains an expression node, both sharing the same tokens
- A SELECT statement contains multiple clauses, all sharing tokens from the same chain
- Each token maintains stacks of nodes that start/end on it (via
nodesStartFromThisTokenandnodesEndWithThisToken)
Token Sharing Example:
SQL: "SELECT * FROM table WHERE id = 1"
- SelectStatement node: [SELECT...1]
- SelectList node: [*]
- FromClause node: [FROM table]
- WhereClause node: [WHERE id = 1]
- Expression node: [id = 1]
- LeftOperand: [id]
- Operator: [=]
- RightOperand: [1]
Modification Strategy for Nested Nodes
Core Rules:
- Leaf-First Modification: Always modify the most specific (leaf) node when possible
- Status Propagation: When a node is modified, all overlapping nodes are updated:
nsDetached: Node's tokens were partially modifiednsPartitial: Node contains modified sub-nodesnsRemoved: Node's tokens were completely removed
- Chain Integrity: The token chain must remain valid after any modification
Modification Scenarios and Handling:
Scenario 1: Modifying a Leaf Node
// Original: WHERE id = 1
// Modify the right operand from "1" to "100"
rightOperandNode.setString("100");
Result:
- RightOperand node: replaced with new token "100"
- Expression node: status = nsPartitial (contains modified child)
- WhereClause node: status = nsPartitial (contains modified descendant)
Scenario 2: Modifying a Parent Node
// Original: WHERE id = 1
// Replace entire WHERE clause
whereClauseNode.setString("WHERE status = 'active'");
Result:
- All child nodes (Expression, operands): status = nsDetached
- WhereClause node: completely replaced with new tokens
- SelectStatement: updated to reference new WHERE tokens
Scenario 3: Removing a Node
// Remove WHERE clause entirely whereClauseNode.removeTokens(); Result: - WhereClause and all children: status = nsRemoved - Tokens removed from chain - Parent SelectStatement: adjusted boundaries
Best Practices for API Users
- Check Node Status Before Modification:
if (node.getNodeStatus() == ENodeStatus.nsNormal) { // Safe to modify node.setString(newText); } - Prefer Leaf Node Modifications:
// GOOD: Modify specific value tableNameNode.setString("new_table"); // AVOID: Replacing entire FROM clause when only table name needs change fromClause.setString("FROM new_table"); - Use toScript() for Complex Modifications:
// After multiple modifications, use script generator if (statement.isChanged()) { String result = statement.toScript(); } - Understand Cascading Effects:
// Modifying a node affects all overlapping nodes expression.setString("new_condition"); // Parent WHERE clause is now marked as modified assert whereClause.getNodeStatus() == ENodeStatus.nsPartitial;
Text Modification Mechanism
The node supports dynamic text modification through thesetString(String) method:
- Tokenization: New text is tokenized using
TSingletonParserto create new tokens - Chain Replacement: Old tokens are removed from the chain and replaced with new ones
- Relationship Updates: All nodes sharing the modified tokens are updated:
updateNodeWithTheSameStartToken(): Updates nodes starting at modified positionupdateMeNodeWithTheSameEndToken(): Updates nodes ending at modified positionupdateStatusOfNodeShareSameTokens(): Propagates status changes
- Status Tracking: Node status changes to track modifications
String Reconstruction (toString())
The toString() method uses a sophisticated two-pass algorithm when doubleLinkedTokenListToString is true:
- First Pass: Detects chain modifications and marks redundant newlines for deletion
- Second Pass: Builds the final string by concatenating non-deleted tokens
Current Implementation Trade-offs
Design Decisions (Simplicity over Features)
- Re-tokenization on Every Modification: Simple but potentially expensive. Accepted trade-off for reliability and consistency.
- No Partial Token Modification: Cannot modify part of a token; must replace entire token. Keeps token integrity and chain management simple.
- Status-based Conflict Resolution: When nodes overlap, status flags determine behavior. Simple rule-based system rather than complex conflict resolution.
- Single Token Chain: All nodes reference the same global chain. Simpler than managing multiple chains but requires careful synchronization.
Limitations by Design
- Cannot Preserve Comments in Modified Nodes: When a node is replaced via setString(), internal comments are lost. This is acceptable for most use cases.
- No Undo/Redo: Modifications are permanent. Applications must manage their own undo stack.
- Thread Safety: Not thread-safe by design. Simpler implementation, caller must synchronize.
- Memory vs Speed: Keeps all tokens in memory for fast access. Not optimized for very large SQLs.
Implementation Details
Token Sharing Management
When modifying a node with shared tokens:- Calculate token count for all affected nodes (
refreshAllNodesTokenCount()) - Identify nodes with same start/end tokens
- Update node boundaries based on token count comparison:
- Same count = sibling node, needs same update
- Larger count = parent node, needs boundary adjustment
- Smaller count = child node, may become detached
- Splice new tokens into chain maintaining prev/next links
Status Flags and Their Meanings
nsNormal: Unmodified node with original tokensnsFlattened: Node created from user text (no original tokens)nsPartitial: Contains modified child nodes but own tokens unchangednsDetached: Tokens modified by parent/sibling operationnsRemoved: Tokens removed from chain, node is defunct
Proposed Future Improvements
1. Decouple Lexer Reference
// Instead of token->lexer reference, use a TokenContext:
class TokenContext {
EDbVendor vendor;
SourceBuffer sourceBuffer;
int sourceId; // Identifies original vs modified source
}
2. Implement SourceBuffer System
class SourceBuffer {
List<TextSegment> segments;
class TextSegment {
String text;
SourceType type; // ORIGINAL, USER_MODIFIED, GENERATED
int startOffset;
int endOffset;
}
String getText(int segmentId, int start, int end);
int addSegment(String text, SourceType type);
}
3. Token Source Tracking
class TSourceToken {
int sourceBufferId;
int segmentId;
int startOffset;
int endOffset;
SourceType sourceType;
String getText() {
return SourceBufferManager.getBuffer(sourceBufferId)
.getText(segmentId, startOffset, endOffset);
}
}
4. Lazy Token Creation
- Don't re-tokenize for simple text replacements
- Create synthetic tokens only when necessary
- Reuse existing tokens when possible
5. Unified Modification API
class NodeModification {
ModificationType type; // REPLACE, INSERT, DELETE
String newText;
boolean preserveFormatting;
boolean retokenize;
}
public void modifyNode(NodeModification mod) {
// Single entry point for all modifications
}
Critical Methods Documentation
setString(String)
Main entry point for modifying node text. Process:
- Tokenizes new text using TSingletonParser
- Creates new token chain from tokenized text
- Replaces old tokens in the chain
- Updates all affected node relationships
toString()
Reconstructs text from token chain. Two-pass algorithm:
- Pass 1: Detect modifications and mark redundant formatting
- Pass 2: Build string from valid tokens
setText(TSourceToken, TSourceToken)
Low-level token replacement. Core of the modification system:
- Updates token chain links
- Maintains node relationships
- Handles edge cases (null tokens, chain boundaries)
Usage Patterns
Reading Original Text
TParseTreeNode node = ...; String originalText = node.toString(); // Get text from token chain
Modifying Node Text
node.setString("new SQL text"); // Replace node's text
String modifiedText = node.toString(); // Get modified text
Checking Modifications
if (node.isChanged()) {
String newText = node.toScript(); // Use script generator for complex mods
}
Thread Safety
This class is NOT thread-safe. Concurrent modifications to the token chain or node structure may result in inconsistent state or corruption.Performance Considerations
- Token chain traversal is O(n) where n is number of tokens
- Text modification requires re-tokenization (expensive)
- toString() performs two passes over tokens
- Node relationship updates can cascade to many nodes
- Since:
- 1.0
- See Also:
-
Field Summary
FieldsModifier and TypeFieldDescriptionSQL dialect of this statement.static booleanstatic final intstatic final intstatic final intstatic final intstatic final intstatic final intstatic final intstatic final int -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionvoidDeprecated.voidAccept a visitor to iterate this class and sub-nodes of this classintaddAllMyTokensToTokenList(TSourceTokenList targetList, int index) Deprecated.since v1.8.8.0, use scriptWriter technology to modify the node Inserts tokens(from start token to end token ) of this parse tree node at the specified position in this list.voidaddToTokenChain(TSourceToken anchorToken, boolean beforeAnchorToken) voidappendNewNode(TParseTreeNode newNode, boolean needCommaBefore) voidprotected voiddoAppendNewNode(TParseTreeNode newNode, TParseTreeNode anchorNode, boolean needCommaBefore) voiddoParse(TCustomSqlStatement psql, ESqlClause plocation) Analyze the sub-nodes inside this node.voidfastSetString(String sqlSegment) longColumn position of the first token of this nodereturn null if no comment is found before this node.representation of this node by removing all spaces, return and commentsintA temporary value can be used for any purposesThe last token of the nodeClass<?>Support String, Integer, Double and Boolean typeThe parser that generate this node.longLine number of the first token of this nodeSQL clause that include this objectName such as select list, from clause, set clausegetMd5()md5 hash code of the string representation of this node.intA unique value to distinguish this node from othersThe first token in this parse tree nodeintbooleanhasNext()voidInitialize a query tree node.voidvoidvoidvoidvoidvoidinsertAfterAToken(TSourceToken anchorToken) voidinsertNewNodeBeforeMe(TParseTreeNode newNode, boolean needCommaBefore) booleanDetect wether this node is modified by checking all tokens included in this node.booleannext()voidvoidremove()protected intremoveAllMyTokensFromTokenList(TSourceToken additionalToken) Deprecated.since v1.8.8.0, use scriptWriter technology to modify the nodevoidstatic voidremoveTokensBetweenNodes(TParseTreeNode startNode, TParseTreeNode endNode) static voidremoveTokensBetweenToken(TSourceToken startToken, TSourceToken endToken) both begin and end token will be removed from the chainvoidreplaceWithNewNode(TParseTreeNode newNode) voidvoidsetAnchorNode(TParseTreeNode anchorNode) voidvoidsetDummyTag(int dummyTag) voidsetEndToken(TFromTableList endNode) voidsetEndToken(TParseTreeNode endNode) voidsetEndToken(TSourceToken newEndToken) voidsetEndToken(TSourceTokenList endTokenList) voidsetEndToken(ArrayList endNode) voidsetEndTokenDirectly(TSourceToken newEndToken) voidsetEvaluateDatatype(Class<?> evaluateDatatype) voidsetEvalValue(Object evalValue) voidsetGsqlparser(TGSqlParser gsqlparser) voidsetIncludingComment(boolean includingComment) voidsetLocation(ESqlClause location) voidsetNewSubNode(TParseTreeNode oldSubNode, TParseTreeNode newSubNode, TParseTreeNode anchorNode) voidsetNodeStatus(ENodeStatus nodeStatus) voidsetNodeType(int nodeType) Set the node type for this node.voidsetParent(TParseTreeNode parent) voidsetPlainText(String plainText) voidsetStartToken(TParseTreeNode startNode) voidsetStartToken(TSourceToken newStartToken) voidsetStartToken(TSourceTokenList startTokenList) voidsetStartToken(ArrayList startNode) voidsetStartTokenDirectly(TSourceToken newStartToken) voidSet the text of the node and update the corresponding source tokens synchronously.voidsetString2(String sqlSegment) Deprecated.since v1.8.8.0, use scriptWriter technology to modify the node Set the new string text of a node will destroy all the sub-node structurestatic booleansubNodeInNode(TParseTreeNode subNode, TParseTreeNode wholeNode) toScript()Return the text string of this node, the return value is the same astoString()if this node is not modified manually after created by parser.toString()将一个语法树节点(TParseTreeNode)转换回它对应的原始SQL字符串。 它通过遍历一个由startToken(起始词法单元)和endToken(结束词法单元)界定的双向链表来实现这一功能。String representation of this parse tree node.Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, waitMethods inherited from interface java.util.Iterator
forEachRemaining
-
Field Details
-
doubleLinkedTokenListToString
-
nodeActionUnknown
- See Also:
-
nodeActionRemove
- See Also:
-
nodeActionInsert
- See Also:
-
nodeActionUpdate
- See Also:
-
nodeActionUpdateText
- See Also:
-
nodeActionAppend
- See Also:
-
nodeChangeStartToken
- See Also:
-
nodeChangeEndToken
- See Also:
-
dbvendor
SQL dialect of this statement.
-
-
Constructor Details
-
TParseTreeNode
public TParseTreeNode()
-
-
Method Details
-
setEvaluateDatatype
-
getEvaluateDatatype
Support String, Integer, Double and Boolean type- Returns:
-
setEvalValue
-
getEvalValue
-
setPlainText
-
getPlainText
-
setLocation
-
getLocation
SQL clause that include this objectName such as select list, from clause, set clause- Returns:
- SQL clause that include this objectName
-
getCompactString
representation of this node by removing all spaces, return and comments- Returns:
- string representation of this node by removing all spaces, return and comments
-
getMd5
md5 hash code of the string representation of this node. If the string of this node is empty, return the md5 value of the class name of this node. 1. remove all spaces, return, comments inside the node
2. turn number constant in where clause into ? character.
3. turn string constant in where clause into ?? character.
4. turn all string of the token into uppercase if it's not a delimited identifier.- Returns:
- md5 hash code of the string representation of this node
-
getParentObjectName
-
setParent
-
insertAfterAToken
-
insertNewNodeBeforeMe
-
appendNewNode
-
replaceWithNewNode
-
resetIterator
-
hasNext
- Specified by:
hasNextin interfaceIterator<TSourceToken>
-
next
- Specified by:
nextin interfaceIterator<TSourceToken>
-
remove
- Specified by:
removein interfaceIterator<TSourceToken>
-
setNodeStatus
-
getNodeStatus
-
addToTokenChain
-
getTokenCount
-
refreshAllNodesTokenCount
-
calculateTokenCount
-
removeTokensBetweenToken
both begin and end token will be removed from the chain- Parameters:
startToken-endToken-
-
removeTokensBetweenNodes
-
removeTokens
-
getGsqlparser
The parser that generate this node.- Returns:
- parser that generate this node
-
setIncludingComment
-
setGsqlparser
-
setDummyTag
-
getDummyTag
A temporary value can be used for any purposes- Returns:
- a temporary value used for any purposes
-
getStartToken
The first token in this parse tree node- Returns:
- the first token of node
-
getEndToken
The last token of the node- Returns:
- the last token of node
-
getColumnNo
Column position of the first token of this node- Returns:
- column position
-
getLineNo
Line number of the first token of this node- Returns:
- line number
-
setNodeType
Set the node type for this node.- Parameters:
nodeType- The node type.
-
getNodeType
A unique value to distinguish this node from others- Returns:
- enum value of node type
- See Also:
-
init
Initialize a query tree node. Used internally- Parameters:
arg1- first argument
-
init
-
init
-
init
-
init
-
init
-
doParse
Analyze the sub-nodes inside this node. Such as build the relationship between table and column.- Parameters:
psql- SQL statement this node belongs toplocation- SQL clause this node belongs to
-
setStartToken
-
setEndToken
-
setStartToken
-
setStartToken
-
setStartToken
-
setStartTokenDirectly
-
setEndTokenDirectly
-
setEndToken
-
setEndToken
-
setEndToken
-
setEndToken
-
setString2
Deprecated.since v1.8.8.0, use scriptWriter technology to modify the node Set the new string text of a node will destroy all the sub-node structure- Parameters:
sqlSegment- that override original text of this node.
-
setString
Set the text of the node and update the corresponding source tokens synchronously. This way, when the toString() method of the node itself or its parent node is called, it will return the modified text.- Parameters:
sqlSegment-
-
fastSetString
-
getCommentBeforeNode
return null if no comment is found before this node.- Returns:
- the comment before this node
-
getCommentAfterNode
-
toString2
String representation of this parse tree node.This string was generated by collecting text from the start token of this node to the end token of this node. If this node was modified, then use
toScript()to get string representation of this node.- Returns:
- string representation of this parse tree node
-
toString
将一个语法树节点(
TParseTreeNode)转换回它对应的原始SQL字符串。 它通过遍历一个由startToken(起始词法单元)和endToken(结束词法单元)界定的双向链表来实现这一功能。这个方法的设计采用了“两遍扫描”(Two-Pass)的策略,也就是使用了两个
while循环。 第一个循环是预处理阶段,用于清理格式;第二个循环则负责构建最终的字符串。第一个
while循环这个循环的主要目标是识别并“软删除”多余的换行符(newline tokens)。这种情况尤其在SQL被程序动态修改后容易出现。 这个循环本身不构建字符串,只负责分析和更新词法单元的状态。 其工作原理是:
- 遍历: 它从
startToken开始,通过getNextTokenInChain()方法遍历到endToken。 - 检测链表变化: 它通过比较当前和前一个词法单元的
posinlist(词法单元在原始完整列表中的位置索引)来判断链表是否“自然”。 如果不连续,意味着词法单元被插入或重排了,此时isChainModified标志位会被设为true。 - 标记换行符: 当遇到第一个换行符时,它会设置一个标志。如果紧接着又遇到了另一个换行符,并且此时链表已经被修改过, 那么它会将这个多余的换行符标记为待删除状态。这个逻辑能有效地将代码被修改后可能产生的多个连续换行压缩成一个。
第二个
while循环这个循环负责从(经过第一步清理后的)词法单元流中实际地构建最终的输出字符串。 其工作原理是:
- 遍历: 它再次遍历完全相同的词法单元序列。
- 构建字符串: 在将每个词法单元追加到
StringBuffer之前,它会进行检查。 - 跳过特定单元: 如果一个词法单元在第一个循环中被标记为待删除,或者它是一个注释(且配置为不包含注释),那么它将被跳过。
- 拼接字符串: 对于所有未被跳过的词法单元,它会将其字符串值追加到
StringBuffer中。
StringBuffer就包含了该语法树节点的最终、重构后的SQL文本。 - 遍历: 它从
-
isTokensInChain
-
addAllMyTokensToTokenList
Deprecated.since v1.8.8.0, use scriptWriter technology to modify the node Inserts tokens(from start token to end token ) of this parse tree node at the specified position in this list. Shifts the element currently at that position (if any) and any subsequent elements to the right .- Parameters:
targetList- the new list of source tokensindex- the position insert the targetList- Returns:
- length of the source token list after insert targetList
-
removeAllMyTokensFromTokenList
Deprecated.since v1.8.8.0, use scriptWriter technology to modify the node- Parameters:
additionalToken- usually was comma before or after this parse tree node that also need to be deleted- Returns:
- length of the source token list after remove tokens
-
accept
Deprecated.Accept a visitor- Specified by:
acceptin interfaceVisitable- Parameters:
v- visitor is a descendant class ofTParseTreeVisitor
-
acceptChildren
Accept a visitor to iterate this class and sub-nodes of this class- Specified by:
acceptChildrenin interfaceVisitable- Parameters:
v- visitor is a descendant class ofTParseTreeVisitor
-
toScript
Return the text string of this node, the return value is the same astoString()if this node is not modified manually after created by parser.
If this node is modified, then use this method to get string representation instead of thetoString()method.- Returns:
- text string of this node
-
setChanged
-
isChanged
Detect wether this node is modified by checking all tokens included in this node.- Returns:
- true if this node is modified.
-
doAppendNewNode
protected void doAppendNewNode(TParseTreeNode newNode, TParseTreeNode anchorNode, boolean needCommaBefore) -
setAnchorNode
-
getAnchorNode
-
setNewSubNode
public void setNewSubNode(TParseTreeNode oldSubNode, TParseTreeNode newSubNode, TParseTreeNode anchorNode) -
subNodeInNode
-