Exemple #1
0
        private static void AutoCastSystemOperator(FunctionExpression exp, IList<Expression> parameters, List<SqlType> paramTypes)
        {
            SqlType t1 = paramTypes[0];
            SqlType t2 = paramTypes[1];

            // If both the types are identical, we good to go,
            if (!t1.IsComparableTo(t2))
                throw InvalidTypes(t1, t2, exp);

            // Types are compatible,
            // If they are numeric,

            if (t1.IsNumeric) {
                // The encoding is different, so now we do a static check
                if (!t1.Equals(t2)) {
                    // TODO: We should do a check on each parameter by walking the tree
                    //   to determine if it's static or not.

                    Expression exp1 = parameters[0];
                    Expression exp2 = parameters[1];

                    int staticop;
                    int varop;

                    // If the left or right is FETCHSTATIC,
                    if (exp1 is FetchStaticExpression) {
                        staticop = 0;
                        varop = 1;
                    } else if (exp2 is FetchStaticExpression) {
                        staticop = 1;
                        varop = 0;
                    } else {
                        // Neither static, so report error,
                        throw InvalidTypes(t1, t2, exp);
                    }

                    // The type of the variable and static sides,
                    SqlType varType = paramTypes[varop];

                    SqlObject castType = new SqlObject(varType.ToString());
                    FetchStaticExpression castExp = new FetchStaticExpression(castType);
                    castExp.ReturnType = SqlType.GetSqlType(typeof(string));

                    // Cast the static type to the variable type,
                    FunctionExpression newStaticExp = new FunctionExpression("@cast", new Expression[] {parameters[staticop], castExp});
                    newStaticExp.ReturnType = varType;
                    exp.Parameters[staticop] = newStaticExp;

                }
            }
        }
Exemple #2
0
        private SqlObject[] EvaluateExpression(Expression expression)
        {
            if (expression is FetchStaticExpression)
                return ((FetchStaticExpression)expression).Values;

            if (expression is FunctionExpression) {
                FunctionExpression functionExp = (FunctionExpression) expression;

                // Check all the parameters are static
                int sz = functionExp.Parameters.Count;
                Expression[] functionArgs = new Expression[sz];
                for (int i = 0; i < sz; ++i) {
                    object ob = functionExp.Parameters[i];
                    if (ob != null) {
                        if (ob is Expression) {
                            // Try and evaluate it
                            SqlObject[] result = EvaluateExpression((Expression)ob);
                            if (result == null)
                                return null;

                            functionArgs[i] = new FetchStaticExpression(result);
                        }
                    }
                }

                // Go ahead an evaluate the function,
                string functionName = functionExp.Name;
                if (IsSimpleEvaluable(functionName)) {
                    // Evaluate the simple function and return the result
                    ITable result = transaction.FunctionManager.Evaluate(functionName, simpleProcessor, functionArgs);
                    return QueryProcessor.Result(result);
                }

                // Not a simple evaluatable function, so return null
                return null;
            }

            return null;
        }