Esempio n. 1
0
            protected override string BuildValue(QConst qConst)
            {
                if (qConst is QVar qVar && !qVar.HasValue)
                {
                    return(BuildValue(qVar.Name) + ":var");
                }

                object constValue = qConst.Value;

                if (constValue == null)
                {
                    return("null");
                }

                // special processing for arrays
                if (constValue is IList)
                {
                    return(BuildValue((IList)constValue));
                }
                if (constValue is string && qConst.Type == TypeCode.String)
                {
                    return(BuildValue((string)constValue));
                }

                TypeCode constTypeCode = qConst.Type;
                string   typeSuffix    = constTypeCode != TypeCode.Empty && constTypeCode != TypeCode.Object ? ":" + constTypeCode.ToString() : String.Empty;

                return(BuildValue(Convert.ToString(constValue, CultureInfo.InvariantCulture)) + typeSuffix);
            }
Esempio n. 2
0
 protected override string BuildValue(QConst value)
 {
     if (SqlClientFactory.ConstOptimization)
     {
         if (value.Type == TypeCode.String && (value.Value is string) && IsAsciiConst((string)value.Value))
         {
             return(String.Format(AsciiConstFormatStr, value.Value));
         }
         if (value.Value != null && !DBNull.Value.Equals(value.Value) &&
             (value.Type == TypeCode.Int16 || value.Type == TypeCode.Int32 || value.Type == TypeCode.Int64 ||
              value.Type == TypeCode.UInt16 || value.Type == TypeCode.UInt32 || value.Type == TypeCode.UInt64))
         {
             return(value.Value.ToString());
         }
     }
     return(base.BuildValue(value));
 }
Esempio n. 3
0
        protected QueryNode ComposeCondition(Expression expression)
        {
            if (expression is UnaryExpression)
            {
                UnaryExpression unExpr = (UnaryExpression)expression;
                QueryNode       qNode  = ComposeCondition(unExpr.Operand);
                return(unExpr.NodeType == ExpressionType.Not ? new QueryNegationNode(qNode) : qNode);
            }
            if (expression is LambdaExpression)
            {
                LambdaExpression lambdaExpr = (LambdaExpression)expression;
                return(ComposeCondition(lambdaExpr.Body));
            }
            if (expression is BinaryExpression)
            {
                BinaryExpression binExpr = (BinaryExpression)expression;
                if (binExpr.NodeType == ExpressionType.AndAlso || binExpr.NodeType == ExpressionType.OrElse)
                {
                    QueryGroupNode qGroup = new QueryGroupNode(binExpr.NodeType == ExpressionType.AndAlso ? QueryGroupNodeType.And : QueryGroupNodeType.Or);
                    qGroup.Nodes.Add(ComposeCondition(binExpr.Left));
                    qGroup.Nodes.Add(ComposeCondition(binExpr.Right));
                    return(qGroup);
                }
                if (conditionMapping.ContainsKey(binExpr.NodeType))
                {
                    IQueryValue rightValue = ComposeValue(binExpr.Right);
                    IQueryValue leftValue  = ComposeValue(binExpr.Left);

                    Conditions qCond = conditionMapping[binExpr.NodeType];

                    // process == and != null/DBNull.Value specifically
                    if (rightValue is QConst
                        &&
                        (Convert.IsDBNull(((QConst)rightValue).Value) || ((QConst)rightValue).Value == null))
                    {
                        qCond = nullConditionMapping[binExpr.NodeType];
                    }

                    QueryConditionNode qCondNode = new QueryConditionNode(leftValue, qCond, rightValue);
                    return(qCondNode);
                }
            }
            else if (expression is MethodCallExpression)
            {
                MethodCallExpression methodExpr = (MethodCallExpression)expression;
                // check for special method call like 'In' or 'Like'
                if (methodExpr.Method.Name == "In")
                {
                    QField      fldValue = ComposeFieldValue(methodExpr.Object);
                    IQueryValue inValue  = ComposeValue(methodExpr.Arguments[0]);
                    // possible conversion to IList
                    if (inValue is QConst)
                    {
                        var inConstValue = (QConst)inValue;
                        if (!(inConstValue.Value is IList))
                        {
                            IList constList = new ArrayList();
                            foreach (object o in ((IEnumerable)inConstValue.Value))
                            {
                                constList.Add(o);
                            }
                            if (constList.Count == 0)                           // means 'nothing'?
                            {
                                return(new QueryConditionNode((QConst)"1", Conditions.Equal, (QConst)"2"));
                            }
                            inValue = new QConst(constList);
                        }
                    }
                    return(new QueryConditionNode(fldValue, Conditions.In, inValue));
                }
                else if (methodExpr.Method.Name == "Like")
                {
                    QField      fldValue  = ComposeFieldValue(methodExpr.Object);
                    IQueryValue likeValue = ComposeValue(methodExpr.Arguments[0]);
                    return(new QueryConditionNode(fldValue, Conditions.Like, likeValue));
                }
            }

            throw new NotSupportedException();
        }