public class TSourceToken extends Object implements Cloneable
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.
Modifier and Type | Field and Description |
---|---|
String |
astext
Text representation for this token.
|
long |
columnNo
the column number of the first character in this token
|
TSourceTokenList |
container
Container for this token which is a list of source token, this is the reference to
TGSqlParser.sourcetokenlist |
String |
dolqstart
Start part of Dollar-quoted String Constants of PostgreSQL.
|
long |
lineNo
the line number of the first character in this token
|
ESqlClause |
location |
long |
offset
Token's offset from the beginning of the input query.
|
int |
posinlist
Index of this token in the
container , start from 0 |
int |
prevTokenCode |
TCustomSqlStatement |
stmt
SQL statement that owns this token.
|
int |
tag
Space to save a value for temporary use
|
int |
tokencode
Unique id of this token used by parser internally.
|
ETokenStatus |
tokenstatus
Maintenance the status of this token during lex and parsing.
|
ETokenType |
tokentype
Uniquely identify the token type in a more meaningful way.
|
Constructor and Description |
---|
TSourceToken()
Class constructor
|
TSourceToken(String s)
Class constructor, set a string value.
|
Modifier and Type | Method and Description |
---|---|
void |
appendText(String text) |
void |
appendText(TSourceToken st) |
TSourceToken |
clone() |
static void |
concatInChain(TSourceToken st1,
TSourceToken st2) |
EDbObjectType |
getDbObjectType()
Token in a
TObjectName has the same database object type as the objectName. |
int |
getDbObjType()
Token in a
TObjectName has the same database object type as the objectName. |
EDbVendor |
getDbvendor()
The database vendor which the SQL script includes this token will run against
|
TSourceToken |
getLinkToken()
Gets the linked token.
|
TSourceToken |
getNextTokenInChain() |
Stack<TParseTreeNode> |
getNodesEndWithThisToken()
A list of nodes whose end token is this token.
|
Stack<TParseTreeNode> |
getNodesStartFromThisToken()
A list of node whose start token is this token
|
TSourceToken |
getPrevTokenInChain() |
String |
getQuotedString() |
int |
getQuoteSymbolLength() |
TSourceToken |
getReplaceToken()
Used in sql formatter package only
|
String |
getTextWithoutQuoted()
String literal in SQL usually inside ``.
|
TSourceTokenList |
getTokensAfter()
Used in sql formatter package only.
|
TSourceTokenList |
getTokensBefore()
Used in sql formatter package only
|
ETokenStatus |
getTokenstatus() |
void |
insertANewTokenAfterMe(TSourceToken newToken) |
void |
insertANewTokenBeforeMe(TSourceToken newToken) |
void |
insertText(TSourceToken st) |
boolean |
isChangedInAsCanonical() |
boolean |
isFirstTokenOfLine()
Check to see if this token is the first token in a line of the input SQL
|
boolean |
isLastTokenOfLine()
Check to see if this token is the last token of in a line in the input SQL
|
boolean |
isnonsolidtoken()
Is this token a solid token or not.
|
static boolean |
isnonsolidtoken(ETokenType tokentype)
Space, return, comments are treated as non-solid token by default
|
boolean |
issolidtoken()
Is this token a non-solid token or not.
|
TSourceToken |
nextSolidToken()
The next token whose
tokentype is not ttreturn,ttwhitespace,ttsimplecomment and ttbracketedcomment. |
TSourceToken |
nextSolidToken(boolean treatCommentAsSolidToken)
The next token whose
tokentype is not ttreturn,ttwhitespace,ttsimplecomment and ttbracketedcomment. |
TSourceToken |
nextSolidToken(int pstep) |
TSourceToken |
nextToken() |
TSourceToken |
prevSolidToken()
The previous token whose
tokentype is not ttreturn,ttwhitespace,ttsimplecomment and ttbracketedcomment. |
TSourceToken |
prevSolidToken(boolean treatCommentAsSolidToken)
The previous token whose
tokentype is not ttreturn,ttwhitespace,ttsimplecomment and ttbracketedcomment |
void |
removeFromChain() |
void |
restoreText() |
TSourceToken |
searchToken(int targetTokenCode,
int range) |
TSourceToken |
searchToken(int targetTokenCode,
int range,
int stopTokenCode,
boolean stopAtSemiColon)
Search a token before or after this token in the same source token list.
|
TSourceToken |
searchToken(String targetTokenText,
int range) |
TSourceToken |
searchToken(String targetTokenText,
int range,
int stopTokenCode,
boolean stopAtSemiColon)
Search a token before or after this token in the same source token list.
|
TSourceToken |
searchTokenAfterObjectName()
Search the first non-solid token after the next objectName.
|
TSourceToken |
searchTokenAtTheEndOfSameLine() |
void |
setDbObjectType(EDbObjectType dbObjectType)
Set the database object type of this token
|
void |
setDbObjType(int dbObjType)
Deprecated.
use
setDbObjectType(gudusoft.gsqlparser.EDbObjectType) instead. |
void |
setDbvendor(EDbVendor dbvendor)
The database vendor which the SQL script includes this token will run against
|
void |
setLinkToken(TSourceToken linkToken)
Create a link between two tokens.
|
void |
setNextTokenInChain(TSourceToken nextTokenInChain) |
void |
setPrevTokenInChain(TSourceToken prevTokenInChain) |
void |
setReplaceToken(TSourceToken replaceToken)
Used in sql formatter package only
|
void |
setString(String str)
set new string of this token
|
void |
setTextWithBackup(String newText) |
void |
setTokenstatus(ETokenStatus tokenstatus) |
String |
toScript()
The string text of this token
|
String |
toString()
The original string text for this token.
|
String |
toStringDebug()
String text with the debug information such as coordinate, token code, token type
|
String |
toUnQuotedString()
Deprecated.
since 2.5.3.4
|
void |
updateNodeEndWithThisToken() |
void |
updateNodeStartWithThisToken() |
public ESqlClause location
public int prevTokenCode
public int tokencode
TBaseType.cmtslashstar
public long lineNo
public long columnNo
public long offset
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));
}
}
public ETokenType tokentype
ETokenType
public TSourceTokenList container
TGSqlParser.sourcetokenlist
public int posinlist
container
, start from 0
public 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);
}
}
public ETokenStatus tokenstatus
public String dolqstart
$$Dianne's horse$$ $SomeTag$Dianne's horse$SomeTag$This field will return $$ and $SomeTag$ accordingly.
public TCustomSqlStatement stmt
public int tag
public TSourceToken()
public TSourceToken(String s)
s
- the string value this toke represent for.public TSourceToken clone()
public static void concatInChain(TSourceToken st1, TSourceToken st2)
public void insertANewTokenAfterMe(TSourceToken newToken)
public void insertANewTokenBeforeMe(TSourceToken newToken)
public void updateNodeEndWithThisToken()
public void updateNodeStartWithThisToken()
public void removeFromChain()
public void setPrevTokenInChain(TSourceToken prevTokenInChain)
public void setNextTokenInChain(TSourceToken nextTokenInChain)
public TSourceToken getPrevTokenInChain()
public TSourceToken getNextTokenInChain()
public ETokenStatus getTokenstatus()
public int getQuoteSymbolLength()
public String getQuotedString()
public String getTextWithoutQuoted()
public String toScript()
public void appendText(TSourceToken st)
public void appendText(String text)
public void insertText(TSourceToken st)
public void setTextWithBackup(String newText)
public boolean isChangedInAsCanonical()
public void setTokenstatus(ETokenStatus tokenstatus)
public void restoreText()
public Stack<TParseTreeNode> getNodesEndWithThisToken()
public Stack<TParseTreeNode> getNodesStartFromThisToken()
public void setDbObjType(int dbObjType)
setDbObjectType(gudusoft.gsqlparser.EDbObjectType)
instead.dbObjType
- the database object typepublic int getDbObjType()
TObjectName
has the same database object type as the objectName.
Please use TObjectName.getDbObjectType()
instead of this method if possible.public void setDbObjectType(EDbObjectType dbObjectType)
dbObjectType
- database object typepublic EDbObjectType getDbObjectType()
TObjectName
has the same database object type as the objectName.
Please use TObjectName.getDbObjectType()
instead of this method if possible.public void setDbvendor(EDbVendor dbvendor)
dbvendor
- the database vendor such as Oracle, DB2 and so on.public EDbVendor getDbvendor()
public void setString(String str)
str
- the new string textpublic String toStringDebug()
public static boolean isnonsolidtoken(ETokenType tokentype)
tokentype
- token typepublic boolean isnonsolidtoken()
public boolean issolidtoken()
public TSourceTokenList getTokensAfter()
public TSourceTokenList getTokensBefore()
public void setReplaceToken(TSourceToken replaceToken)
replaceToken
- replaced tokenpublic TSourceToken getReplaceToken()
public TSourceToken nextToken()
public TSourceToken searchTokenAtTheEndOfSameLine()
public TSourceToken searchToken(int targetTokenCode, int range, int stopTokenCode, boolean stopAtSemiColon)
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.public TSourceToken searchToken(int targetTokenCode, int range)
public TSourceToken searchToken(String targetTokenText, int range, int stopTokenCode, boolean stopAtSemiColon)
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.public TSourceToken searchToken(String targetTokenText, int range)
public TSourceToken searchTokenAfterObjectName()
return new scott.func(x1);If this token is
new
, then call searchTokenAfterObjectName will return (
token.public TSourceToken nextSolidToken(int pstep)
public TSourceToken nextSolidToken()
tokentype
is not ttreturn,ttwhitespace,ttsimplecomment and ttbracketedcomment.public TSourceToken nextSolidToken(boolean treatCommentAsSolidToken)
tokentype
is not ttreturn,ttwhitespace,ttsimplecomment and ttbracketedcomment.treatCommentAsSolidToken,
- set to true will treat comment token as a solid tokenpublic TSourceToken prevSolidToken()
tokentype
is not ttreturn,ttwhitespace,ttsimplecomment and ttbracketedcomment.public TSourceToken prevSolidToken(boolean treatCommentAsSolidToken)
tokentype
is not ttreturn,ttwhitespace,ttsimplecomment and ttbracketedcommenttreatCommentAsSolidToken
- set to true will treat comment token as a solid tokenpublic boolean isFirstTokenOfLine()
public boolean isLastTokenOfLine()
public void setLinkToken(TSourceToken linkToken)
(select * from t)
linkToken
- the token need to be linkedpublic TSourceToken getLinkToken()
(select * from t)
, if this token is '(', then you call this method will return ')' token.public String toUnQuotedString()
""
, bracket quote []
, left/right brace {}
from a delimited identifier and return string text of this identifier.