Relationship between source token and database object
After parsing sql statement, database object information was stored in source token, you can check property DBObjType of TSourceToken which is type of TDBObjType to find out which database object is that source token represent for if any.
Since there are lots of types of database object, let's begin with table and column.
Taking this simple sql for example:
select table1.f1, t1.f2 as t1f2 from table1 t1 where t1.f3 = 1
Token 'table1' in from clause is a declarative table token, while token 'table1' in select list is a reference table token.
Token 't1' in from clause is a declarative table alias token, while token 't1' in select list and where clause is a reference table alias.
How those database objects represented by source token? Here it is:
st1: table1 source token in select list, st1.DBObjType is ttobjTable.
st2: f1 source token in select list,st2.DBObjType is ttObjField.
st3: t1 source token in select list,st3.DBObjType is ttObjTableAlias.
st4: f2 source token in select list,st4.DBObjType is ttObjField.
st5: table1 source token in from clause,st5.DBObjType is ttobjTable.
st6: t1 source token in from clause,st6.DBObjType is ttObjTableAlias.
st7: t1 source token in where clause,st7.DBObjType is ttObjTableAlias.
st8: f3 source token in where clause,st8.DBObjType is ttObjField.
st9: t1f2 source token in select list, st9.DBObjType is ttObjFieldAlias
Sometimes we may need something more, such as relationship between table1 token in select list and from clause;
Which table is f3 token in where clause belong to, and etc. This is where the following properties of TSourceToken come from.
ParentToken, RelatedToken,ChildToken, RelatedTokens,RelatedSubquery.
Those properties have different meanings while source token stands for different database,and there are also depends
on whether source token is declarative or reference.
Let's continue of previous example to explain meanings of those properties.
st5 is a declarative table token:
st5.ParentToken, schema token if any, here it is null.
st5.RelatedToken, table alias of declarative table token if any, here it is st6.
st5.ChildToken, always be null.
st5.RelatedTokens, reference token of declarative table. here it is st1.
Or Fields attach to declarative table in-directly. (such as field in create table)
st1 is a reference table token:
st1.ParentToken, schema token if any, here it is null.
st1.RelatedToken, declarative table token, here it is st5.
st1.ChildToken, field attach to this table token, here it is f1.
Since both declarative table token and reference table token have the same DBObjType (ttobjTable),How to distinguish between those 2 table tokens?
The answer is RelatedToken property of this token, if it is null, then it must be declarative token, since reference token always has RelatedToken points to declarative table token. if it is not null, then we can check DBObjType property of RelatedToken, if RelatedToken.DBObjType is ttobjTable, then it's a reference token, if RelatedToken.DBObjType is ttObjTableAlias, then it's a declarative token.
st6 is a declarative table alias token:
st6.RelatedToken, declarative table token, here it is st5.
st6.RelatedTokens, reference tokens related to this table alias token, here it is st3,st7.
st6.RelatedSubquery, subquery if this table alias is link to a subquery in from clause, here it is null.
st3,st7 are reference table alias token:
st3.ChildToken, field attach to this table alias token, here it is f2(st4).
st7.ChildToken, field attach to this table alias token, here it is f3(st8).
Now, let's take a look at column.
st2 represents f1 in select list,it DBObjType is ttObjField. and it's ParentToken is st1 which is a reference table token that represents table1.
st4 represents f2 in select list, it DBObjType is ttObjField, and it's ParentToken is st3 which is a reference table alias token that
represent t1.
st9 represents t1f2 in select list, it DBObjType is ttObjFieldAlias