c# - SQL Parser:- get table and column names -


i using sql parser parsing sql query , names of table , fields.

i downloaded demo project sqlparser.com, failed compile it. reference library there in project.

source demo :-

using system; using system.collections.generic; using system.text; using system.io;  using gudusoft.gsqlparser; using gudusoft.gsqlparser.units;   namespace gettablecolumn {      class gettablecolumn     {           static list<string> tablelist, columnlist, databaselist, schemalist, functionlist, triggerlist, sequencelist;         static stringbuilder tclist;         static boolean showstructure = false, showlist = true, showlocation = false, showeffect = false;          /**          * sample callback function used sql parser determine          * relationship between table , column sql:          * *          * select          * quantity,b.time,c.description          *          * (select id,time btab) b          * inner join atab on a.id=b.id          * inner join ctab c on a.id=c.id          *           * column: quantity linked table: ctab of          * callback function.          * otherwise, column: quantity linked table: atab default          *           */         private static bool metadatatablecolumn(object sender, string pserver, string pdatabase, string pschema, string ptable, string pcolumn)         {             string[,] columns = new string[3, 5] {                                      { "s1", "d1","dbo", "t1", "c1" },                                      { "s2", "d2","dbo", "t2", "c2" },                                      { "s3", "d3","dbo", "t3", "c3" } };              bool bserver = false, bdatabase = false, bschema = false, btable = false, bcolumn = false;             (int = 0; < 3; i++)             {                 if (pserver == "") bserver = true;                 else bserver = pserver.equals(columns[i, 0], stringcomparison.ordinalignorecase);                 if (!bserver) continue;                  if (pdatabase == "") bdatabase = true;                 else bdatabase = pdatabase.equals(columns[i, 1], stringcomparison.ordinalignorecase);                 if (!bdatabase) continue;                  if (pschema == "") bschema = true;                 else bschema = pschema.equals(columns[i, 2], stringcomparison.ordinalignorecase);                 if (!bschema) continue;                  btable = ptable.equals(columns[i, 3], stringcomparison.ordinalignorecase);                 if (!btable) continue;                  bcolumn = pcolumn.equals(columns[i, 4], stringcomparison.ordinalignorecase);                 if (!bcolumn) continue;                  break;             }             return bcolumn;         }          static void sortandremovedup(list<string> plist)         {             plist.sort();             int32 index = 0;             while (index < plist.count - 1)             {                 if (plist[index] == plist[index + 1])                     plist.removeat(index);                 else                     index++;             }         }          static void main(string[] args)         {             string filename = "", dbvendor = "";               if (args.length == 0)             {                 console.writeline("usage: {0} scriptfile [/d dbvendor] [/v]", environment.getcommandlineargs()[0]);                 console.writeline("\t/d \t\t set database vendor");                 console.writeline("\tdbvendor \t oracle, mssql, mysql or db2");                 console.writeline("\t/v \t\t show detailed information");                 return;             }              array array = array.createinstance(typeof(string), args.length);              (int = 0; < array.length; i++)             {                 array.setvalue(args[i], i);             }              showlocation = array.indexof(array, "/v") != -1;             showeffect = array.indexof(array, "/v") != -1;             showstructure = array.indexof(array, "/v") != -1;              filename = args[0];              int index = array.indexof(array, "/d");              if (index != -1 && args.length > index + 1)             {                 dbvendor = args[index + 1];             }              tdbvendor db = tdbvendor.dbvoracle;               if (dbvendor.equals("mssql"))             {                 db = tdbvendor.dbvmssql;             }             else if (dbvendor.equals("db2"))             {                 db = tdbvendor.dbvdb2;             }             else if (dbvendor.equals("mysql"))             {                 db = tdbvendor.dbvmysql;             }                //if (args.length == 2)             //{             //    bsortcolumn = args[1].equals("-s", stringcomparison.ordinalignorecase);             //}                tgsqlparser sqlparser = new tgsqlparser(db);             sqlparser.sqlfilename = filename;              // please use own function check table column relation using meta             // information database            //  sqlparser.onmetadatabasetablecolumn += metadatatablecolumn;              int ret = sqlparser.parse();              if (ret != 0)             {                 console.writeline(sqlparser.errormessages);                 return;             }              tablelist = new list<string>();             columnlist = new list<string>();             databaselist = new list<string>();             schemalist = new list<string>();             functionlist = new list<string>();             triggerlist = new list<string>();             sequencelist = new list<string>();             tclist = new stringbuilder();               (int = 0; < sqlparser.sqlstatements.count(); i++)             {                 tcustomsqlstatement sql = sqlparser.sqlstatements[i];                 analyzestmt(sql, 0);             }              if (showstructure)             {                 console.writeline(tclist.tostring());             }              showlist = true;              if (showlist)             {                 sortandremovedup(tablelist);                 sortandremovedup(columnlist);                 sortandremovedup(databaselist);                 sortandremovedup(schemalist);                 sortandremovedup(functionlist);                 sortandremovedup(triggerlist);                 sortandremovedup(sequencelist);                  console.writeline("tables:");                 console.writeline(new stringbuilder(string.join("\r\n", tablelist.toarray())).tostring());                 console.writeline("fields:");                 console.writeline(new stringbuilder(string.join("\r\n", columnlist.toarray())).tostring());                 if (databaselist.count > 0)                 {                     console.writeline("database:");                     console.writeline(new stringbuilder(string.join("\r\n", databaselist.toarray())).tostring());                 }                 if (schemalist.count > 0)                 {                     console.writeline("schema:");                     console.writeline(new stringbuilder(string.join("\r\n", schemalist.toarray())).tostring());                 }                 if (functionlist.count > 0)                 {                     console.writeline("functions:");                     console.writeline(new stringbuilder(string.join("\r\n", functionlist.toarray())).tostring());                 }                 if (triggerlist.count > 0)                 {                     console.writeline("triggers:");                     console.writeline(new stringbuilder(string.join("\r\n", triggerlist.toarray())).tostring());                 }                 if (sequencelist.count > 0)                 {                     console.writeline("sequences:");                     console.writeline(new stringbuilder(string.join("\r\n", sequencelist.toarray())).tostring());                 }              }              //console.read();          }          static void analyzestmt(tcustomsqlstatement stmt, int pnest)         {             tclist.appendline("");             tclist.appendline(string.format("{0}{1}", " ".padleft(pnest, ' '), stmt.sqlstatementtype));              string tn = "", tneffect = "";             (int k = 0; k < stmt.tables.count(); k++)             {                 if (stmt.tables[k].tabletype == tlztabletype.lttsubquery)                 {                     tn = "(subquery, alias:" + stmt.tables[k].tablealias + ")";                 }                 else                 {                     tn = stmt.tables[k].tablefullname;                     if (stmt.tables[k].islinktable)                     {                         tn = tn + "(" + stmt.tables[k].linkedtable.tablefullname + ")";                     }                     else if (stmt.tables[k].iscte)                     {                         tn = tn + "(cte)";                     }                  }                 if (!((stmt.tables[k].tabletype == tlztabletype.lttsubquery) || (stmt.tables[k].iscte)))                 {                     tablelist.add(tn);                 }                  if (showeffect)                 {                     tneffect = string.format("{0}({1})", tn, stmt.tables[k].effecttype);                 }                 else                 {                     tneffect = string.format("{0}", tn);                 }                 tclist.appendline(string.format("{0}{1}", " ".padleft(pnest + 1, ' '), tneffect));                   (int m = 0; m < stmt.tables[k].linkedcolumns.count(); m++)                 {                     string columninfo = "";                     if (showlocation)                     {                         columninfo = string.format("{0}({1})", stmt.tables[k].linkedcolumns[m].fieldattrname, stmt.tables[k].linkedcolumns[m].location);                     }                     else                     {                         columninfo = string.format("{0}", stmt.tables[k].linkedcolumns[m].fieldattrname);                     }                     tclist.appendline(string.format("{0}{1}", " ".padleft(pnest + 2, ' '), columninfo));                      if (!((stmt.tables[k].tabletype == tlztabletype.lttsubquery) || (stmt.tables[k].iscte)))                     {                         if (stmt.tables[k].islinktable)                         { //mssql, deleted, inserted table                             columnlist.add(stmt.tables[k].linkedtable.tablefullname + '.' + stmt.tables[k].linkedcolumns[m].fieldattrname);                         }                         else                             columnlist.add(tn + '.' + stmt.tables[k].linkedcolumns[m].fieldattrname);                     }                 }             }              if (stmt.orphancolumns.count() > 0)             {                 tclist.appendline(string.format("{0}{1}", " ".padleft(pnest + 1, ' '), " orphan columns:"));                 (int k = 0; k < stmt.orphancolumns.count(); k++)                 {                     tclist.appendline(string.format("{0}{1}", " ".padleft(pnest + 2, ' '), stmt.orphancolumns[k].astext));                     columnlist.add("missing." + stmt.orphancolumns[k].astext);                 }             }              if (stmt.databasetokens.count() > 0)             {                 (int k = 0; k < stmt.databasetokens.count(); k++)                 {                     databaselist.add(stmt.databasetokens[k].astext);                 }             }              if (stmt.schematokens.count() > 0)             {                 (int k = 0; k < stmt.schematokens.count(); k++)                 {                     schemalist.add(stmt.schematokens[k].astext);                 }             }              if (stmt.functiontokens.count() > 0)             {                 (int k = 0; k < stmt.functiontokens.count(); k++)                 {                     if (stmt.functiontokens[k].parenttoken != null)                     {                         functionlist.add(stmt.functiontokens[k].parenttoken.astext + "." + stmt.functiontokens[k].astext);                     }                     else                     {                         functionlist.add(stmt.functiontokens[k].astext);                     }                 }             }              if (stmt.triggertokens.count() > 0)             {                 (int k = 0; k < stmt.triggertokens.count(); k++)                 {                     if (stmt.triggertokens[k].parenttoken != null)                     {                         triggerlist.add(stmt.triggertokens[k].parenttoken.astext + "." + stmt.triggertokens[k].astext);                     }                     else                     {                         triggerlist.add(stmt.triggertokens[k].astext);                     }                 }             }              if (stmt.sequencetokens.count() > 0)             {                 (int k = 0; k < stmt.sequencetokens.count(); k++)                 {                     sequencelist.add(stmt.sequencetokens[k].astext);                 }             }               (int j = 0; j < stmt.childnodes.count(); j++)             {                 if (stmt.childnodes[j] tcustomsqlstatement)                 {                     analyzestmt(stmt.childnodes[j] tcustomsqlstatement, pnest + 1);                 }             }         }           static string gettokenlocation(tsourcetoken st)         {             if ((st.location == tlzownerlocation.elfieldbyattr)                 || (st.location == tlzownerlocation.elfieldbyexpr)                 )             {                 return "columnresult/select list";             }             else if (st.location == tlzownerlocation.elwhere)             {                 return "where clause";             }             else if (st.location == tlzownerlocation.elhaving)             {                 return "having clause";             }             else if (st.location == tlzownerlocation.elgroupby)             {                 return "group clause";             }             else if (st.location == tlzownerlocation.elorderby)             {                 return "order clause";             }             else if (st.location == tlzownerlocation.eltablefuncarg)             {                 return "from clause";             }             else if (st.location == tlzownerlocation.eljoincondition)             {                 return "join condition";             }             else             {                 return st.location.tostring();             }          }      } } 

the error stack follows:

error   91  'gudusoft.gsqlparser.tcustomsqlstatement' not contain definition 'orphancolumns' , no extension method 'orphancolumns' accepting first argument of type 'gudusoft.gsqlparser.tcustomsqlstatement' found (are missing using directive or assembly reference?)   c:\users\user\downloads\gsqlparser_dotnet_trial_2_6_4 (3)\democollections\gettablecolumn\gettablecolumn.cs  292 22  gettablecolumn error   92  'gudusoft.gsqlparser.tcustomsqlstatement' not contain definition 'orphancolumns' , no extension method 'orphancolumns' accepting first argument of type 'gudusoft.gsqlparser.tcustomsqlstatement' found (are missing using directive or assembly reference?)   c:\users\user\downloads\gsqlparser_dotnet_trial_2_6_4 (3)\democollections\gettablecolumn\gettablecolumn.cs  295 42  gettablecolumn error   93  'gudusoft.gsqlparser.tcustomsqlstatement' not contain definition 'orphancolumns' , no extension method 'orphancolumns' accepting first argument of type 'gudusoft.gsqlparser.tcustomsqlstatement' found (are missing using directive or assembly reference?)   c:\users\user\downloads\gsqlparser_dotnet_trial_2_6_4 (3)\democollections\gettablecolumn\gettablecolumn.cs  297 97  gettablecolumn error   94  'gudusoft.gsqlparser.tcustomsqlstatement' not contain definition 'orphancolumns' , no extension method 'orphancolumns' accepting first argument of type 'gudusoft.gsqlparser.tcustomsqlstatement' found (are missing using directive or assembly reference?)   c:\users\user\downloads\gsqlparser_dotnet_trial_2_6_4 (3)\democollections\gettablecolumn\gettablecolumn.cs  298 52  gettablecolumn error   76  'gudusoft.gsqlparser.tgsqlparser' not contain definition 'onmetadatabasetablecolumn' , no extension method 'onmetadatabasetablecolumn' accepting first argument of type 'gudusoft.gsqlparser.tgsqlparser' found (are missing using directive or assembly reference?)   c:\users\user\downloads\gsqlparser_dotnet_trial_2_6_4 (3)\democollections\gettablecolumn\gettablecolumn.cs  149 23  gettablecolumn error   95  'gudusoft.gsqlparser.tlzownerlocation' not contain definition 'eljoincondition'  c:\users\user\downloads\gsqlparser_dotnet_trial_2_6_4 (3)\democollections\gettablecolumn\gettablecolumn.cs  391 54  gettablecolumn error   81  'gudusoft.gsqlparser.tlztable' not contain definition 'effecttype' , no extension method 'effecttype' accepting first argument of type 'gudusoft.gsqlparser.tlztable' found (are missing using directive or assembly reference?)   c:\users\user\downloads\gsqlparser_dotnet_trial_2_6_4 (3)\democollections\gettablecolumn\gettablecolumn.cs  259 77  gettablecolumn error   79  'gudusoft.gsqlparser.tlztable' not contain definition 'iscte' , no extension method 'iscte' accepting first argument of type 'gudusoft.gsqlparser.tlztable' found (are missing using directive or assembly reference?) c:\users\user\downloads\gsqlparser_dotnet_trial_2_6_4 (3)\democollections\gettablecolumn\gettablecolumn.cs  246 46  gettablecolumn error   80  'gudusoft.gsqlparser.tlztable' not contain definition 'iscte' , no extension method 'iscte' accepting first argument of type 'gudusoft.gsqlparser.tlztable' found (are missing using directive or assembly reference?) c:\users\user\downloads\gsqlparser_dotnet_trial_2_6_4 (3)\democollections\gettablecolumn\gettablecolumn.cs  252 97  gettablecolumn error   86  'gudusoft.gsqlparser.tlztable' not contain definition 'iscte' , no extension method 'iscte' accepting first argument of type 'gudusoft.gsqlparser.tlztable' found (are missing using directive or assembly reference?) c:\users\user\downloads\gsqlparser_dotnet_trial_2_6_4 (3)\democollections\gettablecolumn\gettablecolumn.cs  281 101 gettablecolumn error   77  'gudusoft.gsqlparser.tlztable' not contain definition 'islinktable' , no extension method 'islinktable' accepting first argument of type 'gudusoft.gsqlparser.tlztable' found (are missing using directive or assembly reference?) c:\users\user\downloads\gsqlparser_dotnet_trial_2_6_4 (3)\democollections\gettablecolumn\gettablecolumn.cs  243 40  gettablecolumn error   87  'gudusoft.gsqlparser.tlztable' not contain definition 'islinktable' , no extension method 'islinktable' accepting first argument of type 'gudusoft.gsqlparser.tlztable' found (are missing using directive or assembly reference?) c:\users\user\downloads\gsqlparser_dotnet_trial_2_6_4 (3)\democollections\gettablecolumn\gettablecolumn.cs  283 43  gettablecolumn error   82  'gudusoft.gsqlparser.tlztable' not contain definition 'linkedcolumns' , no extension method 'linkedcolumns' accepting first argument of type 'gudusoft.gsqlparser.tlztable' found (are missing using directive or assembly reference?) c:\users\user\downloads\gsqlparser_dotnet_trial_2_6_4 (3)\democollections\gettablecolumn\gettablecolumn.cs  268 52  gettablecolumn error   83  'gudusoft.gsqlparser.tlztable' not contain definition 'linkedcolumns' , no extension method 'linkedcolumns' accepting first argument of type 'gudusoft.gsqlparser.tlztable' found (are missing using directive or assembly reference?) c:\users\user\downloads\gsqlparser_dotnet_trial_2_6_4 (3)\democollections\gettablecolumn\gettablecolumn.cs  273 79  gettablecolumn error   84  'gudusoft.gsqlparser.tlztable' not contain definition 'linkedcolumns' , no extension method 'linkedcolumns' accepting first argument of type 'gudusoft.gsqlparser.tlztable' found (are missing using directive or assembly reference?) c:\users\user\downloads\gsqlparser_dotnet_trial_2_6_4 (3)\democollections\gettablecolumn\gettablecolumn.cs  273 126 gettablecolumn error   85  'gudusoft.gsqlparser.tlztable' not contain definition 'linkedcolumns' , no extension method 'linkedcolumns' accepting first argument of type 'gudusoft.gsqlparser.tlztable' found (are missing using directive or assembly reference?) c:\users\user\downloads\gsqlparser_dotnet_trial_2_6_4 (3)\democollections\gettablecolumn\gettablecolumn.cs  277 74  gettablecolumn error   89  'gudusoft.gsqlparser.tlztable' not contain definition 'linkedcolumns' , no extension method 'linkedcolumns' accepting first argument of type 'gudusoft.gsqlparser.tlztable' found (are missing using directive or assembly reference?) c:\users\user\downloads\gsqlparser_dotnet_trial_2_6_4 (3)\democollections\gettablecolumn\gettablecolumn.cs  284 107 gettablecolumn error   90  'gudusoft.gsqlparser.tlztable' not contain definition 'linkedcolumns' , no extension method 'linkedcolumns' accepting first argument of type 'gudusoft.gsqlparser.tlztable' found (are missing using directive or assembly reference?) c:\users\user\downloads\gsqlparser_dotnet_trial_2_6_4 (3)\democollections\gettablecolumn\gettablecolumn.cs  287 67  gettablecolumn error   78  'gudusoft.gsqlparser.tlztable' not contain definition 'linkedtable' , no extension method 'linkedtable' accepting first argument of type 'gudusoft.gsqlparser.tlztable' found (are missing using directive or assembly reference?) c:\users\user\downloads\gsqlparser_dotnet_trial_2_6_4 (3)\democollections\gettablecolumn\gettablecolumn.cs  245 54  gettablecolumn error   88  'gudusoft.gsqlparser.tlztable' not contain definition 'linkedtable' , no extension method 'linkedtable' accepting first argument of type 'gudusoft.gsqlparser.tlztable' found (are missing using directive or assembly reference?) c:\users\user\downloads\gsqlparser_dotnet_trial_2_6_4 (3)\democollections\gettablecolumn\gettablecolumn.cs  284 58  gettablecolumn 

answer problem. call exec method in tokenizequery class. table names , column names stored in list.

using system; using system.collections.generic; using system.linq; using system.text;  using gudusoft.gsqlparser; using gudusoft.gsqlparser.units; namespace custom.data {      class tokenizequery {          public static list < string > tablenames = new list < string > ();         public static list < string > columnnames = new list < string > ();         // public static string inputsql = "select * abc";         public void exec(string query) {             tablenames.clear();             columnnames.clear();             gettablecolumn _new = new gettablecolumn();             _new.gettablecolumnname(query);         }          public void print() {             console.writeline("**************************************************************");             console.write("tables\n\n");             tablenames.foreach(i => console.write("{0}\n", i));             console.write("columns\n\n");             columnnames.foreach(i => console.write("{0}\n", i));             console.writeline("**************************************************************");         }           #         region tableinfo         class tableinfo {             public tcustomsqlstatement stmt;             public tsourcetoken database, schema, table, tablealias;              public string getdatabasename() {                 if (database == null) {                     return "";                 } else {                     return database.astext;                 }             }              public string getschemaname() {                 if (schema == null) {                     return "";                 } else {                     return schema.astext;                 }             }              public string gettablename() {                 if (table == null) {                     return "";                 } else {                     return table.astext;                 }             }              public string gettablealiasname() {                 if (tablealias == null) {                     return "";                 } else {                     return tablealias.astext;                 }             }              public tableinfo(tcustomsqlstatement s) {                 stmt = s;             }         }          #         endregion          # region columninfo         class columninfo {             public tableinfo table;             public tsourcetoken column;             public string columnalias;             //public int lineno, columnno;             public tlzownerlocation location;             public tlzexpression columnexpr;             public columninfo(tableinfo t) {                 table = new tableinfo(t.stmt);                 table.database = t.database;                 table.schema = t.schema;                 table.table = t.table;                 table.tablealias = t.tablealias;             }         }          #         endregion          class gettablecolumn {              static list < columninfo > columninfos = null;              static bool bsortcolumn = false;              /**              * sample callback function used sql parser determine              * relationship between table , column sql:              * *              * select              * quantity,b.time,c.description              *              * (select id,time btab) b              * inner join atab on a.id=b.id              * inner join ctab c on a.id=c.id              *               * column: quantity linked table: ctab of              * callback function.              * otherwise, column: quantity linked table: atab default              *               */             private static bool metadatatablecolumn(object sender, string pschema, string ptable, string pcolumn) {                 string[, ] columns = new string[3, 3] {                     {                         "dbo", "subselect2table1", "s2t1a1"                     }, {                         "dbo", "subselect3table1", "quantity2"                     }, {                         "dbo", "subselect3table2", "s3t1a1"                     }                 };                  bool bschema = false, btable = false, bcolumn = false;                 (int = 0; < 3; i++) {                     if (pschema == "") bschema = true;                     else bschema = pschema.equals(columns[i, 0], stringcomparison.ordinalignorecase);                     if (!bschema) continue;                      btable = ptable.equals(columns[i, 1], stringcomparison.ordinalignorecase);                     if (!btable) continue;                      bcolumn = pcolumn.equals(columns[i, 2], stringcomparison.ordinalignorecase);                     if (!bcolumn) continue;                      break;                 }                 return bcolumn;             }              public void gettablecolumnname(string inputsql) {                    columninfos = new list < columninfo > ();                  tgsqlparser sqlparser = new tgsqlparser(tdbvendor.dbvoracle); //dbvmssql                 sqlparser.sqltext.text = inputsql;                  // please use own function check table column relation using meta                 // information database                  //sqlparser.onmetadatabasetablecolumn += metadatatablecolumn;                  int ret = sqlparser.parse();                  if (ret != 0) {                     console.writeline(sqlparser.errormessages);                     return;                 }                    (int = 0; < sqlparser.sqlstatements.count(); i++) {                     tcustomsqlstatement sql = sqlparser.sqlstatements[i];                     printtabletokensinstmt(sql, 0);                      (int j = 0; j < sql.childnodes.count(); j++) {                         if (sql.childnodes[j] tcustomsqlstatement) {                             tablesinstmt(sql.childnodes[j] tcustomsqlstatement, 0);                         }                     }                  }                  if (bsortcolumn) {                     //console.writeline(system.environment.newline + "print columns in same order appear in sql statement");                     columninfos.sort(delegate(columninfo a, columninfo b) {                         int xdiff = a.column.xposition - b.column.xposition;                         if (xdiff != 0) return xdiff;                         return a.column.yposition - b.column.yposition;                     });                      printcolumninfos();                 }                }              static void printcolumninfos() {                  int columnorder = 1;                 //console.writeline("{0,-15}{1,-25}{2,-25}{3,-15}\n{4,-20}{5,-20}{6,-40}", "pos", "column", "table", "t-alias", "schema", "db","loc");                 console.writeline("{0},{1},{2},{3},{4},{5},{6},{7}", "columnorder", "location", "columnname", "tablename", "tablealias", "schema", "databasename", "notes");                 foreach(columninfo c in columninfos) {                      tsourcetoken st = c.column;                     string notes = "";                      if (st.location == tlzownerlocation.elwhere) {                         notes = whereclause(c.table.stmt, st);                     } else if (st.location == tlzownerlocation.elhaving) {                         notes = havingclause((tselectsqlstatement) c.table.stmt, st);                     } else if (st.location == tlzownerlocation.eljoinitemcondition) {                         notes = joincondition((tselectsqlstatement) c.table.stmt, st);                     }                      //  console.writeline("{0},{1},{2},{3},{4},{5},{6},{7}", string.format("[{0,3},{1,3}]", c.column.xposition, c.column.yposition), c.column.astext, c.table.gettablename(), c.table.gettablealiasname(), c.table.getschemaname(), c.table.getdatabasename(), gettokenlocation(c.column).trim());                     console.writeline("{0},{1},{2},{3},{4},{5},{6},{7}", columnorder, gettokenlocation(c.column), c.column.astext, c.table.gettablename(), c.table.gettablealiasname(), c.table.getschemaname(), c.table.getdatabasename(), notes);                     columnorder++;                 }             }              static void tablesinstmt(tcustomsqlstatement stmt, int level) {                  printtabletokensinstmt(stmt, level);                  (int j = 0; j < stmt.childnodes.count(); j++) {                     if (stmt.childnodes[j] tcustomsqlstatement) {                         tablesinstmt(stmt.childnodes[j] tcustomsqlstatement, level + 1);                     }                 }              }              static void printtabletokensinstmt(tcustomsqlstatement stmt, int level) {                 tableinfo tableinfo = new tableinfo(stmt);                  tsourcetokenlist tokenlist = stmt.tabletokens;                 if (!bsortcolumn) {                     string typeinfo = stmt.sqlstatementtype.tostring();                     if (stmt tlzplsql_package) {                         tlzplsql_package package = (tlzplsql_package) stmt;                         typeinfo += '(' + package._package_name.astext + ')';                     } else if (stmt tlzplsql_subprogram) {                         tlzplsql_subprogram procedure = (tlzplsql_subprogram) stmt;                         typeinfo += '(' + procedure._procedure_name.astext + ')';                     } else if (stmt tlzplsql_trigger) {                         tlzplsql_trigger trigger = (tlzplsql_trigger) stmt;                         typeinfo += '(' + trigger._ndtriggername.astext + ')';                     }                     console.writeline(new string(' ', level) + "tables in {0}: {1}  adfafsasfa", typeinfo, tokenlist.count());                  }                    tsourcetoken st = null;                 string tablestr = null;                 string tablealias = null;                  (int = 0; < tokenlist.count(); i++) {                     st = tokenlist[i];                     tableinfo.table = st;                     tablestr = st.astext;                     if (st.relatedtoken != null) {                         tablealias = "alias: " + st.relatedtoken.astext;                         tableinfo.tablealias = st.relatedtoken;                     }                     if (st.parenttoken != null) {                         //schema                         tablestr = st.parenttoken.astext + "." + tablestr;                         tableinfo.schema = st.parenttoken;                          if (st.parenttoken.parenttoken != null) {                             //database                             tablestr = st.parenttoken.parenttoken.astext + "." + tablestr;                             tableinfo.database = st.parenttoken.parenttoken;                         }                     }                     if (!bsortcolumn) {                         tablenames.add(tablestr);                         console.writeline(new string(' ', level + 1) + "table name" + tablestr + gettokenposition(st) + " " + tablealias);                     }                     printcolumnsintabletoken(stmt, st, level + 2, tableinfo);                 }             }              static void printcolumnsintabletoken(tcustomsqlstatement stmt, tsourcetoken st, int level, tableinfo tableinfo) {                  if (st.relatedtoken != null) {                     // declared table alias token                     tsourcetoken rt = st.relatedtoken;                     tsourcetoken rrt = null;                     (int = 0; < rt.relatedtokens.count(); i++) {                         rrt = rt.relatedtokens[i];                         if (rrt.childtoken != null) {                             columninfo columninfo = new columninfo(tableinfo);                             columninfo.column = rrt.childtoken;                             columninfos.add(columninfo);                              if (!bsortcolumn) {                                 columnnames.add(rrt.childtoken.astext);                                 console.writeline(new string(' ', level + 1) + "idhar hu" + rrt.childtoken.astext + gettokenposition(rrt.childtoken) + "," + gettokenlocation(rrt.childtoken));                             }                         }                     }                 }                  tsourcetoken rtt = null;                 (int = 0; < st.relatedtokens.count(); i++) {                     // reference table token                     rtt = st.relatedtokens[i];                     if (rtt.dbobjtype == tdbobjtype.ttobjfield) {                         // field tokens link table token (those token not linked syntax tablename.fieldname)                         // : select f t                          columninfo columninfo = new columninfo(tableinfo);                         columninfo.column = rtt;                         columninfos.add(columninfo);                          if (!bsortcolumn)                             console.writeline(new string(' ', level + 1) + rtt.astext + gettokenposition(rtt) + getcolumntokenisdetermined(rtt) + "," + gettokenlocation(rtt));                     }                     if (rtt.childtoken != null) {                         columninfo columninfo = new columninfo(tableinfo);                         columninfo.column = rtt.childtoken;                         columninfos.add(columninfo);                          if (!bsortcolumn)                             console.writeline(new string(' ', level + 1) + rtt.childtoken.astext + gettokenposition(rtt.childtoken) + getcolumntokenisdetermined(rtt.childtoken) + "," + gettokenlocation(rtt.childtoken));                     }                 }             }              static string getcolumntokenisdetermined(tsourcetoken st) {                 if ((st.dbobjtype == tdbobjtype.ttobjfield)) {                     return " <guessed> ";                 } else {                     return "";                 }             }             static string gettokenposition(tsourcetoken st) {                 return "(" + st.xposition + "," + st.yposition + ")";             }              static string gettokenlocation(tsourcetoken st) {                 if ((st.location == tlzownerlocation.elfieldbyattr) || (st.location == tlzownerlocation.elfieldbyexpr)) {                     return "columnresult/select list";                 } else if (st.location == tlzownerlocation.elwhere) {                     return "where clause";                 } else if (st.location == tlzownerlocation.elhaving) {                     return "having clause";                 } else if (st.location == tlzownerlocation.elgroupby) {                     return "group clause";                 } else if (st.location == tlzownerlocation.elorderby) {                     return "order clause";                 } else if (st.location == tlzownerlocation.eltablefuncarg) {                     return "from clause";                 } else if (st.location == tlzownerlocation.eljoinitemcondition) {                     return "join condition";                 } else {                     return st.location.tostring();                 }              }              static string whereclause(tcustomsqlstatement stmt, tsourcetoken st) {                 string ret = "";                 if (stmt.whereclause == null) {                     return ret;                 }                  ret = stmt.whereclause.astext;                  return ret;             }              static string havingclause(tselectsqlstatement stmt, tsourcetoken st) {                 string ret = "";                 if (stmt.havingclause == null) {                     return ret;                 }                  ret = stmt.havingclause.astext;                  return ret;             }              static string joincondition(tselectsqlstatement stmt, tsourcetoken st) {                 string ret = "";                 if (stmt.jointables.count() == 0) {                     return ret;                 }                  tlzjoin join = stmt.jointables[0];                 if (join.joinitems.count() == 0) {                     return ret;                 }                  foreach(tlzjoinitem joinitem in join.joinitems) {                     (int k = joinitem.joinqual.starttoken.posinlist; k <= joinitem.joinqual.endtoken.posinlist; k++) {                         if (st.posinlist == k) {                             ret = joinitem.joinqual.astext;                             break;                         }                     }                 }                   return ret;             }           }      } } 

Comments