001package gudusoft.gsqlparser.nodes;
002
003
004import gudusoft.gsqlparser.TBaseType;
005
006import java.util.HashMap;
007
008/**
009* Collections of {@link TCTE}.
010*/
011public class TCTEList extends TParseTreeNodeList <TCTE>{
012
013    /**
014     * @deprecated please use {@link #size()} and {@link #getCTE(int)} to access the cte in the list
015     */
016    public HashMap<String, TCTE> cteNames = new HashMap<String, TCTE>( );
017
018    public TCTE searchCTEByName(String cteName, int beforeThisPos){
019        // cte name in the list can be the same,
020        // the rule used to search the cte is find the nearest one before it
021        //
022        //        WITH cte_2 AS
023        //                (
024        //                        SELECT ID, "NAME",'cc'
025        //                        FROM SUNNY.HR.TEST
026        //                ),
027        //                cte_2 AS
028        //                (
029        //                        SELECT ID, "NAME",*
030        //                        FROM cte_2
031        //                )
032        //        select * FROM cte_2
033
034        TCTE ret = null;
035        for(int i=size()-1;i>=0;i--){
036            TCTE cte = getCTE(i);
037            if (cte.getStartToken().posinlist >= beforeThisPos) continue;
038            if ( cteName.equalsIgnoreCase(TBaseType.getTextWithoutQuoted(cte.getTableName().toString()))){
039                ret = cte;
040                break;
041            }
042
043        }
044        return ret;
045    }
046
047    public TCTEList()
048    {
049    }
050
051    public void addCTE(TCTE cte)
052    {
053        addElement(cte);
054
055//        if (cteNames.get(TBaseType.getTextWithoutQuoted(cte.getTableName().toString()).toUpperCase()) == null){
056//            // the same name cte is already exists, the new comer is used, the later one is discard
057//            // this sql is verified at snowflake
058//            //            WITH cte_2 AS
059//            //                    (
060//            //                            SELECT ID, "NAME"
061//            //                            FROM SUNNY.HR.TEST
062//            //                    ),
063//            //                    cte_2 AS
064//            //                    (
065//            //                            SELECT ID, "NAME",'bb'
066//            //                            FROM SUNNY.HR.TEST
067//            //                    )
068//            //            select * FROM cte_2
069//
070    //        addElement(cte);
071    //        cteNames.put(TBaseType.getTextWithoutQuoted(cte.getTableName().toString()).toUpperCase(), cte);
072//        }
073
074    }
075
076    public TCTE getCTE(int position)
077    {
078        if (position < size())
079        {
080            return (TCTE)elementAt(position);
081        }else{
082        return null;
083        }
084    }
085
086    void addParseTreeNode(Object arg1){
087        addCTE((TCTE)arg1);
088    }
089
090    public void accept(TParseTreeVisitor v){
091        v.preVisit(this);
092        v.postVisit(this);
093    }
094//
095    public void acceptChildren(TParseTreeVisitor v){
096        v.preVisit(this);
097        for(int i=0;i<this.size();i++){
098            this.getCTE(i).acceptChildren(v);
099        }
100        v.postVisit(this);
101    }
102
103}