Package gudusoft.gsqlparser
Class TSourceToken
Object
gudusoft.gsqlparser.TSourceToken
- All Implemented Interfaces:
Cloneable
Represents a source token which is the basic syntactic unit of SQL.
A token can be a key word, an identifier, a quoted identifier, a literal (or constant), or a special character symbol.
Tokens are normally separated by whitespace (space, tab, newline), but need not be if there is no ambiguity
(which is generally only the case if a special character is adjacent to some other token type).
The parse tree node consists of source tokens.
A list of source token will be available after parse or tokenize the input SQL.
TGSqlParser sqlparser = new TGSqlParser(EDbVendor.dbvoracle);
sqlparser.sqltext = "select col from t";
int ret = sqlparser.parse();
if (ret == 0){
for(int i=0;i<sqlparser.sourcetokenlist.size();i++){
TSourceToken st = sqlparser.sourcetokenlist.get(i);
System.out.println(st.tokentype.toString()+" "+st.toString());
}
}else{
System.out.println(sqlparser.getErrormessage());
}
Get a list of source tokens after call the method TGSqlParser.parse()
or just call TGSqlParser.tokenizeSqltext() if you only
need to access tokens of input SQL without generating the full parse tree.
tokencode is the unique id represents the type of token,
some typical tokens are: whitespace, return, keyword, identifier. This value is mainly used by the parser internally.
tokentype uniquely identify the token type in a more meaningful way. It's more easier to use
this field in your program than tokencode.
-
Field Summary
FieldsModifier and TypeFieldDescriptionThe text content of this token.longthe column number of the first character in this tokenContainer for this token which is a list of source token, this is the reference toTGSqlParser.sourcetokenlistStart part of Dollar-quoted String Constants of PostgreSQL.booleanlongthe line number of the first character in this tokenlongToken's offset from the beginning of the input query.intIndex of this token in thecontainer, start from 0intSQL statement that owns this token.intSpace to save a value for temporary useintUnique id of this token used by parser internally.Maintenance the status of this token during lex and parsing.Uniquely identify the token type in a more meaningful way. -
Constructor Summary
ConstructorsConstructorDescriptionClass constructorClass constructor, set a string value. -
Method Summary
Modifier and TypeMethodDescriptionvoidvoidappendText(String text) clone()static voidconcatInChain(TSourceToken st1, TSourceToken st2) Text representation for this token.Token in aTObjectNamehas the same database object type as the objectName.intToken in aTObjectNamehas the same database object type as the objectName.The database vendor which the SQL script includes this token will run againstGets the linked token.A list of nodes whose end token is this token.A list of node whose start token is this tokenintUsed in sql formatter package onlyString literal in SQL usually inside ``.Used in sql formatter package only.Used in sql formatter package onlyvoidinsertANewTokenAfterMe(TSourceToken newToken) voidinsertANewTokenBeforeMe(TSourceToken newToken) voidbooleanbooleanCheck to see if this token is the first token in a line of the input SQLbooleanCheck to see if this token is the last token of in a line in the input SQLbooleanIs this token a solid token or not.static booleanisnonsolidtoken(ETokenType tokentype) Space, return, comments are treated as non-solid token by defaultbooleanIs this token a non-solid token or not.The next token whosetokentypeis not ttreturn,ttwhitespace,ttsimplecomment and ttbracketedcomment.nextSolidToken(boolean treatCommentAsSolidToken) The next token whosetokentypeis not ttreturn,ttwhitespace,ttsimplecomment and ttbracketedcomment.nextSolidToken(int pstep) The previous token whosetokentypeis not ttreturn,ttwhitespace,ttsimplecomment and ttbracketedcomment.prevSolidToken(boolean treatCommentAsSolidToken) The previous token whosetokentypeis not ttreturn,ttwhitespace,ttsimplecomment and ttbracketedcommentvoidvoidsearchToken(int targetTokenCode, int range) searchToken(int targetTokenCode, int range, int stopTokenCode, boolean stopAtSemiColon) Search a token before or after this token in the same source token list.searchToken(String targetTokenText, int range) searchToken(String targetTokenText, int range, int stopTokenCode, boolean stopAtSemiColon) Search a token before or after this token in the same source token list.Search the first non-solid token after the next objectName.voidvoidsetDbObjectType(EDbObjectType dbObjectType) Set the database object type of this tokenvoidsetDbObjType(int dbObjType) Deprecated.voidsetDbvendor(EDbVendor dbvendor) The database vendor which the SQL script includes this token will run againstvoidsetLinkToken(TSourceToken linkToken) Create a link between two tokens.voidsetNextTokenInChain(TSourceToken nextTokenInChain) voidsetPrevTokenInChain(TSourceToken prevTokenInChain) voidsetReplaceToken(TSourceToken replaceToken) Used in sql formatter package onlyvoidset new string of this tokenvoidsetTextWithBackup(String newText) voidsetTokenstatus(ETokenStatus tokenstatus) toScript()The string text of this tokentoString()The original string text for this token.String text with the debug information such as coordinate, token code, token typeDeprecated.since 2.5.3.4voidvoid
-
Field Details
-
location
-
prevTokenCode
-
tokencode
Unique id of this token used by parser internally. check available value start fromTBaseType.cmtslashstar -
lineNo
the line number of the first character in this token -
columnNo
the column number of the first character in this token -
offset
Token's offset from the beginning of the input query.public void testOffset(){ TGSqlParser sqlparser = new TGSqlParser(EDbVendor.dbvoracle); sqlparser.sqltext = "select f from t\n" + "where f>1\n"; assertTrue(sqlparser.parse() == 0); for (int i=0;i<sqlparser.sourcetokenlist.size();i++){ TSourceToken st = sqlparser.sourcetokenlist.get(i); String textFromOffset = sqlparser.sqltext.toString().substring((int)st.offset,(int)st.offset+st.toString().length()); assertTrue(st.toString().equalsIgnoreCase(textFromOffset)); } } -
tokentype
Uniquely identify the token type in a more meaningful way. It's more easier to use this field in your program than tokencode. check available value inETokenType -
container
Container for this token which is a list of source token, this is the reference toTGSqlParser.sourcetokenlist -
posinlist
Index of this token in thecontainer, start from 0public void testPosinList(){ TGSqlParser sqlparser = new TGSqlParser(EDbVendor.dbvoracle); sqlparser.sqltext = "select f from t\n" + "where f>1\n"; assertTrue(sqlparser.parse() == 0); for (int i=0;i<sqlparser.sourcetokenlist.size();i++){ assertTrue(i == sqlparser.sourcetokenlist.get(i).posinlist); } } -
astext
The text content of this token.This public field is maintained for backwards compatibility with existing code. New code should use getter/setter methods instead of direct field access.
Recommended Usage:
- Reading text: Use
toString()method - Writing text: Use
setAstext(String)method
- Since:
- 1.0 (public field maintained for backwards compatibility)
- See Also:
- Reading text: Use
-
tokenstatus
Maintenance the status of this token during lex and parsing. Used by the parser internally. -
dolqstart
Start part of Dollar-quoted String Constants of PostgreSQL. While the standard syntax for specifying string constants is usually convenient, it can be difficult to understand when the desired string contains many single quotes or backslashes, since each of those must be doubled. To allow more readable queries in such situations, PostgreSQL provides another way, called "dollar quoting", to write string constants. A dollar-quoted string constant consists of a dollar sign ($), an optional "tag" of zero or more characters, another dollar sign, an arbitrary sequence of characters that makes up the string content, a dollar sign, the same tag that began this dollar quote, and a dollar sign. For example, here are two different ways to specify the string "Dianne's horse" using dollar quoting:$$Dianne's horse$$ $SomeTag$Dianne's horse$SomeTag$This field will return $$ and $SomeTag$ accordingly. -
insqlpluscmd
-
stmt
SQL statement that owns this token. -
tag
Space to save a value for temporary use
-
-
Constructor Details
-
TSourceToken
public TSourceToken()Class constructor -
TSourceToken
Class constructor, set a string value.- Parameters:
s- the string value this toke represent for.
-
-
Method Details
-
clone
-
concatInChain
-
insertANewTokenAfterMe
-
insertANewTokenBeforeMe
-
updateNodeEndWithThisToken
-
updateNodeStartWithThisToken
-
removeFromChain
-
setPrevTokenInChain
-
setNextTokenInChain
-
getPrevTokenInChain
-
getNextTokenInChain
-
getTokenstatus
-
getQuoteSymbolLength
-
getQuotedString
-
getTextWithoutQuoted
String literal in SQL usually inside ``. Quoted identifier in SQL usually surrounded by [ and ], This method only returns the text inside those surroundings.- Returns:
- text inside `` and []
-
toScript
The string text of this token- Returns:
- the string text of this token
-
appendText
-
appendText
-
insertText
-
setTextWithBackup
-
isChangedInAsCanonical
-
setTokenstatus
-
restoreText
-
getNodesEndWithThisToken
A list of nodes whose end token is this token.- Returns:
- A list of nodes whose end token is this token.
-
getNodesStartFromThisToken
A list of node whose start token is this token- Returns:
- A list of node whose start token is this token
-
setDbObjType
Deprecated.usesetDbObjectType(gudusoft.gsqlparser.EDbObjectType)instead.- Parameters:
dbObjType- the database object type
-
getDbObjType
Token in aTObjectNamehas the same database object type as the objectName. Please useTObjectName.getDbObjectType()instead of this method if possible.- Returns:
- the type of the database object
-
setDbObjectType
Set the database object type of this token- Parameters:
dbObjectType- database object type
-
getDbObjectType
Token in aTObjectNamehas the same database object type as the objectName. Please useTObjectName.getDbObjectType()instead of this method if possible.- Returns:
- the type of the database object
-
setDbvendor
The database vendor which the SQL script includes this token will run against- Parameters:
dbvendor- the database vendor such as Oracle, DB2 and so on.
-
getDbvendor
The database vendor which the SQL script includes this token will run against- Returns:
- dbvendor the database vendor such as Oracle, DB2 and so on.
-
setString
set new string of this token- Parameters:
str- the new string text
-
toString
The original string text for this token. -
toStringDebug
String text with the debug information such as coordinate, token code, token type- Returns:
- the string value with full debug info
-
isnonsolidtoken
Space, return, comments are treated as non-solid token by default- Parameters:
tokentype- token type- Returns:
- true if token type is not one of ttwhitespace,ttreturn,ttsimplecomment,ttbracketedcomment
-
isnonsolidtoken
Is this token a solid token or not.- Returns:
- true if it's a non-solid token.
-
issolidtoken
Is this token a non-solid token or not.- Returns:
- true if it's a solid token.
-
getTokensAfter
Used in sql formatter package only.- Returns:
- source token list
-
getTokensBefore
Used in sql formatter package only- Returns:
- source token list
-
setReplaceToken
Used in sql formatter package only- Parameters:
replaceToken- replaced token
-
getReplaceToken
Used in sql formatter package only- Returns:
- replaced token
-
nextToken
-
searchTokenAtTheEndOfSameLine
-
searchToken
public TSourceToken searchToken(int targetTokenCode, int range, int stopTokenCode, boolean stopAtSemiColon) Search a token before or after this token in the same source token list. The result token should has the tokencode equals to the targetTokenCode in a specified range.- Parameters:
targetTokenCode- , the token code need to be searchedrange- , > 0, search token start from the next token and forward, = 0, just compare with this token, < 0, search from the previous token and backword.- Returns:
- the token with the same token code, otherwise, return null.
-
searchToken
-
searchToken
public TSourceToken searchToken(String targetTokenText, int range, int stopTokenCode, boolean stopAtSemiColon) Search a token before or after this token in the same source token list. The result token should has the string text equals to the targetTokenText in a specified range.- Parameters:
targetTokenText- , the target string textrange- , > 0, search token start from the next token and forward, = 0, just compare with this token, < 0, search from the previous token and backword.- Returns:
- the token with the same token code, otherwise, return null.
-
searchToken
-
searchTokenAfterObjectName
Search the first non-solid token after the next objectName. Take this SQL for example:return new scott.func(x1);
If this token isnew, then call searchTokenAfterObjectName will return(token.- Returns:
- solid token after the next objectName
-
nextSolidToken
-
nextSolidToken
The next token whosetokentypeis not ttreturn,ttwhitespace,ttsimplecomment and ttbracketedcomment.- Returns:
- the next solid token, returns null if not found.
-
nextSolidToken
The next token whosetokentypeis not ttreturn,ttwhitespace,ttsimplecomment and ttbracketedcomment.- Parameters:
treatCommentAsSolidToken- , set to true will treat comment token as a solid token- Returns:
- the solid token if found, otherwise, return null
-
prevSolidToken
The previous token whosetokentypeis not ttreturn,ttwhitespace,ttsimplecomment and ttbracketedcomment.- Returns:
- the solid token if found, otherwise, return null
-
prevSolidToken
The previous token whosetokentypeis not ttreturn,ttwhitespace,ttsimplecomment and ttbracketedcomment- Parameters:
treatCommentAsSolidToken- set to true will treat comment token as a solid token- Returns:
- the solid token if found, otherwise, return null
-
isFirstTokenOfLine
Check to see if this token is the first token in a line of the input SQL- Returns:
- true if this is the first token of a line, otherwise, return false
-
isLastTokenOfLine
Check to see if this token is the last token of in a line in the input SQL- Returns:
- true if this is the last token of a line, otherwise, return false
-
setLinkToken
Create a link between two tokens. Make it easy to access another linked token. Usually, those are two tokens like the parenthesis in this SQL:(select * from t)- Parameters:
linkToken- the token need to be linked
-
getLinkToken
Gets the linked token. Take this SQL for example:(select * from t), if this token is '(', then you call this method will return ')' token.- Returns:
- the paired parenthesis in select, expression
-
toUnQuotedString
Deprecated.since 2.5.3.4Remove double quote"", bracket quote[], left/right brace{}from a delimited identifier and return string text of this identifier.- Returns:
- string text of this token
-
getAstext
Text representation for this token. -
setAstext
-
setDbObjectType(gudusoft.gsqlparser.EDbObjectType)instead.