public class TSourceToken extends Object
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 |
---|---|
TSourceToken |
alternativeToken
Deprecated.
since v1.8.8.0, use scriptWriter technology to modify the node
|
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
|
long |
offset
Token's offset from the beginning of the input query.
|
int |
posinlist
Index of this token in the
container , start from 0 |
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 |
---|---|
boolean |
firstTokenOfLine()
Check to see if this token is the first token in a line of the input SQL
|
TSourceToken |
getAlternativeToken()
Deprecated.
since v1.8.8.0, use scriptWriter technology to modify the node
If you find a start token of parse tree node has an alternativetoken, then text of this node should be modified by using TParseTreeNode.setString(String sqlSegment)
1. new string will be tokenized into a list of source tokens: stlist 2. link alternativetoken of start token of this node to the first token in stlist generated in step 1. 3. link back alternativetoken of last token in stlist to the last token of this node. |
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
|
TGSqlParser |
getGsqlparser()
The parser that create this token.
|
TSourceToken |
getLinkToken()
Gets the linked token.
|
TParseTreeNodeList |
getNodesEndWithThisToken()
Deprecated.
since v1.8.8.0, use scriptWriter technology to modify the node
A list of nodes whose end token is this token.
|
TParseTreeNodeList |
getNodesStartFromThisToken()
Deprecated.
since v1.8.8.0, use scriptWriter technology to modify the node
A list of node whose start token is this token
|
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
|
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.
|
boolean |
lastTokenOfLine()
Check to see if this token is the last token of in a line in the input SQL
|
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 |
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 |
int |
removeMeFromTokenList()
This method is not used.
|
TSourceToken |
searchToken(int targetTokenCode,
int range)
Search a token before or after this token in the same source token list.
|
TSourceToken |
searchToken(String targetTokenText,
int range)
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.
|
void |
setAlternativeToken(TSourceToken alternativeToken)
Deprecated.
since v1.8.8.0, use scriptWriter technology to modify the node
|
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 |
setGsqlparser(TGSqlParser gsqlparser) |
void |
setLinkToken(TSourceToken linkToken)
Create a link between two tokens.
|
void |
setReplaceToken(TSourceToken replaceToken)
Used in sql formatter package only
|
void |
setString(String str)
set new string of this token
|
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()
Remove double quote
"" , bracket quote [] , left/right brace {}
from a delimited identifier and return string text of this identifier. |
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 String astext
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 alternativeToken
public TSourceToken()
public TSourceToken(String s)
s
- the string value this toke represent for.public String getTextWithoutQuoted()
public String toScript()
public TGSqlParser getGsqlparser()
public void setGsqlparser(TGSqlParser gsqlparser)
public TParseTreeNodeList getNodesEndWithThisToken()
public TParseTreeNodeList 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 setAlternativeToken(TSourceToken alternativeToken)
public TSourceToken getAlternativeToken()
If you find a start token of parse tree node has an alternativetoken,
then text of this node should be modified by using TParseTreeNode.setString(String sqlSegment)
1. new string will be tokenized into a list of source tokens: stlist
2. link alternativetoken of start token of this node to the first token in stlist generated in step 1.
3. link back alternativetoken of last token in stlist to the last token of this node.
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 toString()
public 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 searchToken(int targetTokenCode, int range)
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(String targetTokenText, int range)
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 searchTokenAfterObjectName()
return new scott.func(x1);If this token is
new
, then call searchTokenAfterObjectName will return (
token.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 firstTokenOfLine()
public boolean lastTokenOfLine()
public int removeMeFromTokenList()
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.Copyright © 2019. All rights reserved.