Ejemplo n.º 1
0
        private TableFilter tFilter; // null if not yet resolved

        #endregion Fields

        #region Constructors

        /**
         * Constructor declaration
         *
         *
         * @param f
         */
        /*		Expression(Function f)
                {
                    iType = FUNCTION;
                    fFunction = f;
                }
        */
        /**
         * Constructor declaration
         *
         *
         * @param e
         */
        public Expression(Expression e)
        {
            iType = e.iType;
            iDataType = e.iDataType;
            eArg = e.eArg;
            eArg2 = e.eArg2;
            cLikeEscape = e.cLikeEscape;
            sSelect = e.sSelect;
            //			fFunction = e.fFunction;
        }
Ejemplo n.º 2
0
 /**
  * Constructor declaration
  *
  *
  * @param s
  */
 public Expression(Select s)
 {
     iType = QUERY;
     sSelect = s;
 }
Ejemplo n.º 3
0
        /**
         * Method declaration
         *
         *
         * @return
         *
         * @throws Exception
         */
        private Select parseSelect()
        {
            Select select = new Select();
            // [email protected] begin changes from 1.50
            select.limitStart = 0;
            select.limitCount = cChannel.getMaxRows();
            // [email protected] end changes from 1.50
            string token = tTokenizer.getstring();

            if (token.Equals("DISTINCT"))
            {
                select.bDistinct = true;
                // [email protected] begin changes from 1.50
            }
            else if( token.Equals("LIMIT"))
            {
                string limStart = tTokenizer.getstring();
                string limEnd = tTokenizer.getstring();
                //System.out.println( "LIMIT used from "+limStart+","+limEnd);
                select.limitStart = limStart.ToInt32();
                select.limitCount = limEnd.ToInt32();
                // [email protected] end changes from 1.50
            }
            else
            {
                tTokenizer.back();
            }

            // parse column list
            ArrayList vcolumn = new ArrayList();

            do
            {
                Expression e = parseExpression();

                token = tTokenizer.getstring();

                if (token.Equals("AS"))
                {
                    e.setAlias(tTokenizer.getName());

                    token = tTokenizer.getstring();
                }
                else if (tTokenizer.wasName())
                {
                    e.setAlias(token);

                    token = tTokenizer.getstring();
                }

                vcolumn.Add(e);
            } while (token.Equals(","));

            if (token.Equals("INTO"))
            {
                select.sIntoTable = tTokenizer.getstring();
                token = tTokenizer.getstring();
            }

            if (!token.Equals("FROM"))
            {
                throw Trace.error(Trace.UNEXPECTED_TOKEN, token);
            }

            Expression condition = null;

            // parse table list
            ArrayList     vfilter = new ArrayList();

            vfilter.Add(parseTableFilter(false));

            while (true)
            {
                token = tTokenizer.getstring();

                if (token.Equals("LEFT"))
                {
                    token = tTokenizer.getstring();

                    if (token.Equals("OUTER"))
                    {
                        token = tTokenizer.getstring();
                    }

                    Trace.check(token.Equals("JOIN"), Trace.UNEXPECTED_TOKEN,
                        token);
                    vfilter.Add(parseTableFilter(true));
                    tTokenizer.getThis("ON");

                    condition = addCondition(condition, parseExpression());
                }
                else if (token.Equals("INNER"))
                {
                    tTokenizer.getThis("JOIN");
                    vfilter.Add(parseTableFilter(false));
                    tTokenizer.getThis("ON");

                    condition = addCondition(condition, parseExpression());
                }
                else if (token.Equals(","))
                {
                    vfilter.Add(parseTableFilter(false));
                }
                else
                {
                    break;
                }
            }

            tTokenizer.back();

            int	    len = vfilter.Count;
            TableFilter[] filter = new TableFilter[len];

            vfilter.CopyTo(filter);

            select.tFilter = filter;

            // expand [table.]* columns
            len = vcolumn.Count;

            for (int i = 0; i < len; i++)
            {
                Expression e = (Expression) (vcolumn[i]);

                if (e.getType() == Expression.ASTERIX)
                {
                    int    current = i;
                    Table  table = null;
                    string n = e.getTableName();

                    for (int t = 0; t < filter.Length; t++)
                    {
                        TableFilter f = filter[t];

                        e.resolve(f);

                        if (n != null &&!n.Equals(f.getName()))
                        {
                            continue;
                        }

                        table = f.getTable();

                        int col = table.getColumnCount();

                        for (int c = 0; c < col; c++)
                        {
                            Expression ins =
                                new Expression(f.getName(),
                                table.getColumnName(c));

                            vcolumn.Insert(current++,ins);

                            // now there is one element more to parse
                            len++;
                        }
                    }

                    Trace.check(table != null, Trace.TABLE_NOT_FOUND, n);

                    // minus the asterix element
                    len--;

                    vcolumn.RemoveAt(current);
                }
                else if (e.getType()==Expression.COLUMN)
                {
                    if (e.getTableName() == null)
                    {
                        for (int filterIndex=0; filterIndex < filter.Length; filterIndex++)
                        {
                            e.resolve(filter[filterIndex]);
                        }
                    }
                }
            }

            select.iResultLen = len;

            // where
            token = tTokenizer.getstring();

            if (token.Equals("WHERE"))
            {
                condition = addCondition(condition, parseExpression());
                token = tTokenizer.getstring();
            }

            select.eCondition = condition;

            if (token.Equals("GROUP"))
            {
                tTokenizer.getThis("BY");

                len = 0;

                do
                {
                    vcolumn.Add(parseExpression());

                    token = tTokenizer.getstring();
                    len++;
                } while (token.Equals(","));

                select.iGroupLen = len;
            }

            if (token.Equals("ORDER"))
            {
                tTokenizer.getThis("BY");

                len = 0;

                do
                {
                    Expression e = parseExpression();

                    if (e.getType() == Expression.VALUE)
                    {

                        // order by 1,2,3
                        if (e.getDataType() == Column.INTEGER)
                        {
                            int i = ((int) e.getValue()).ToInt32();

                            e = (Expression) vcolumn[i - 1];
                        }
                    }
                    else if (e.getType() == Expression.COLUMN
                        && e.getTableName() == null)
                    {

                        // this could be an alias column
                        string s = e.getColumnName();

                        for (int i = 0; i < vcolumn.Count; i++)
                        {
                            Expression ec = (Expression) vcolumn[i];

                            if (s.Equals(ec.getAlias()))
                            {
                                e = ec;

                                break;
                            }
                        }
                    }

                    token = tTokenizer.getstring();

                    if (token.Equals("DESC"))
                    {
                        e.setDescending();

                        token = tTokenizer.getstring();
                    }
                    else if (token.Equals("ASC"))
                    {
                        token = tTokenizer.getstring();
                    }

                    vcolumn.Add(e);

                    len++;
                } while (token.Equals(","));

                select.iOrderLen = len;
            }

            len = vcolumn.Count;
            select.eColumn = new Expression[len];

            vcolumn.CopyTo(select.eColumn);

            if (token.Equals("UNION"))
            {
                token = tTokenizer.getstring();

                if (token.Equals("ALL"))
                {
                    select.iUnionType = Select.UNIONALL;
                }
                else
                {
                    select.iUnionType = Select.UNION;

                    tTokenizer.back();
                }

                tTokenizer.getThis("SELECT");

                select.sUnion = parseSelect();
            }
            else if (token.Equals("INTERSECT"))
            {
                tTokenizer.getThis("SELECT");

                select.iUnionType = Select.INTERSECT;
                select.sUnion = parseSelect();
            }
            else if (token.Equals("EXCEPT") || token.Equals("MINUS"))
            {
                tTokenizer.getThis("SELECT");

                select.iUnionType = Select.EXCEPT;
                select.sUnion = parseSelect();
            }
            else
            {
                tTokenizer.back();
            }

            return select;
        }