Пример #1
0
        public void CreateVariable(string name, IQueryValue value)
        {
            if (_variables.ContainsKey(name))
                throw new ArgumentException(string.Format("A variable with the name \"{0}\" already exists", name), "name");

            _variables.Add(name, value);
        }
Пример #2
0
        public override IQueryValue EvaluateOperator(IEnvironment env, IQueryValue left, IQueryValue right)
        {
            switch (left.GetIQLType())
            {
                default:
                    throw new ArgumentException(string.Format("Left side of + cannot be of type {0}", left.GetIQLType()));

                case IQLType.Number:
                    switch (right.GetIQLType())
                    {
                        default:
                            throw new ArgumentException(string.Format("Right side of + cannot be of type {0} when left side is a number", right.GetIQLType()));

                        case IQLType.Number:
                            return new NumberValue() {Number = left.Number + right.Number};

                        case IQLType.Color:
                            return new ColorValue() {Color = right.Color.Each(v => left.Number + v)};
                    }

                case IQLType.Color:
                    switch (right.GetIQLType())
                    {
                        default:
                            throw new ArgumentException(string.Format("Right side of + cannot be of type {0} when left side is a color", right.GetIQLType()));

                        case IQLType.Color:
                            return new ColorValue() {Color = left.Color + right.Color};

                        case IQLType.Number:
                            return new ColorValue() {Color = left.Color.Each(v => v + right.Number)};
                    }
            }
        }
Пример #3
0
            public override string BuildValue(IQueryValue value)
            {
                if (value is Query)
                {
                    Query q = (Query)value;
                    if (q.Fields == null || q.Fields.Length != 1)
                    {
                        throw new Exception("Invalid nested query");
                    }
                    string    whereExpression = BuildExpression(q.Condition);
                    string    sortExpression  = dsDalc.BuildSort(q);
                    DataRow[] result          = dsDalc.PersistedDS.Tables[q.Table].Select(whereExpression, sortExpression);
                    if (result.Length == 1)
                    {
                        return(base.BuildValue(new QConst(result[0][q.Fields[0].Name])));
                    }
                    if (result.Length > 1)
                    {
                        // build array
                        object[] resValues = new object[result.Length];
                        for (int i = 0; i < resValues.Length; i++)
                        {
                            resValues[i] = result[i][q.Fields[0].Name];
                        }
                        return(base.BuildValue(new QConst(resValues)));
                    }

                    return("NULL");
                }
                return(base.BuildValue(value));
            }
Пример #4
0
        public static void SetValue(this IQueryValue l, IQueryValue r)
        {
            if (l.GetIQLType() != r.GetIQLType())
            {
                throw new InvalidOperationException("Cannot set query value to disparate type");
            }

            switch (l.GetIQLType())
            {
                case IQLType.Number:
                    l.Number = r.Number;
                    break;

                case IQLType.Boolean:
                    l.Boolean = r.Boolean;
                    break;

                case IQLType.Canvas:
                    l.Canvas = r.Canvas;
                    break;

                case IQLType.Color:
                    l.Color = r.Color.Clone();
                    break;
            }
        }
Пример #5
0
 public static IQueryValue CClamp(IEnvironment env, IQueryValue[] args)
 {
     FunctionUtils.CheckArgumentCountEqual("cclamp", 1, args.Length);
     Color color = args[0].Color;
     color.Clamp();
     return new ColorValue() {Color = color};
 }
Пример #6
0
        public virtual string BuildValue(IQueryValue value)
        {
            if (value == null)
            {
                return(null);
            }

            if (value is QAggregateField qAggrFld)
            {
                return(BuildValue(qAggrFld));
            }

            if (value is QField qFld)
            {
                return(BuildValue(qFld));
            }

            if (value is QConst qConst)
            {
                return(BuildValue(qConst));
            }

            if (value is QRawSql)
            {
                return(((QRawSql)value).SqlText);
            }

            throw new NotSupportedException("Unknown query value: " + value.GetType().ToString());
        }
Пример #7
0
 public QueryConditionNode(QueryConditionNode node)
 {
     Name       = node.Name;
     _LValue    = node.LValue;
     _Condition = node.Condition;
     _RValue    = node.RValue;
 }
Пример #8
0
 protected IQueryValue TranslateQueryValue(IQueryValue qVal)
 {
     if (qVal is Query)
     {
         return(TranslateSubQuery((Query)qVal));
     }
     return(qVal);
 }
Пример #9
0
        public override string BuildValue(IQueryValue value)
        {
            if (value is Query)
            {
                return(BuildSelectInternal((Query)value, true));
            }

            return(base.BuildValue(value));
        }
Пример #10
0
        public static IQueryValue CTan(IEnvironment env, IQueryValue[] args)
        {
            FunctionUtils.CheckArgumentCountBetween("ctan", 1, 2, args.Length);

            return new ColorValue()
            {
                Color = args[0].Color.Each(v => (float) Math.Tan(v), args.Length == 2 && args[1].Boolean)
            };
        }
Пример #11
0
 public IQuery Add(IQueryValue value)
 {
     if (this.Equals(value))
     {
         throw new ArgumentException("query不能添加自己到query中");
     }
     dic.Add(value, QueryLink.And);
     return(this);
 }
Пример #12
0
        public static IQueryValue PrintAndReturn(IEnvironment env, IQueryValue[] args)
        {
            FunctionUtils.CheckArgumentCountEqual("par", 1, args.Length);

            Console.ForegroundColor = ConsoleColor.Blue;
            Console.WriteLine(args[0].ToString());
            Console.ForegroundColor = ConsoleColor.White;

            return args[0];
        }
Пример #13
0
        public static IQueryValue CRand(IEnvironment env, IQueryValue[] args)
        {
            FunctionUtils.CheckArgumentCountBetween("rand", 0, 1, args.Length);

            return new ColorValue()
            {
                Color =
                    new Color((float) random.NextDouble(), (float) random.NextDouble(), (float) random.NextDouble(),
                        args.Length == 1 ? args[0].Number : (float) random.NextDouble())
            };
        }
Пример #14
0
        public override IQueryValue EvaluateOperator(IEnvironment env, IQueryValue left, IQueryValue right)
        {
            switch (left.GetIQLType())
            {
                default:
                    throw new ArgumentException(string.Format("Arguments to < cannot be of type {0}", left.GetIQLType()));

                case IQLType.Number:
                    return new BooleanValue() { Boolean = left.Number < right.Number };
            }
        }
Пример #15
0
 public override string BuildValue(IQueryValue value)
 {
     if (value is Query)
     {
         return(BuildQueryString((Query)value, true));
     }
     if (value is QRawSql)
     {
         return(BuildValue(((QRawSql)value).SqlText) + ":sql");
     }
     return(base.BuildValue(value));
 }
Пример #16
0
 IQueryValue ApplyFieldMapping(IQueryValue qValue)
 {
     if (qValue is QField)
     {
         var qFld = (QField)qValue;
         if (FieldMapping.ContainsKey(qFld.Name))
         {
             return(new QField(qFld.Name, FieldMapping[qFld.Name]));
         }
     }
     return(qValue);
 }
Пример #17
0
        public static IQueryValue Magnitude(IEnvironment env, IQueryValue[] args)
        {
            FunctionUtils.CheckArgumentCountBetween("magnitude", 1, 2, args.Length);

            Color color = args[0].Color;
            float result = color.R*color.R + color.G*color.G + color.B*color.B;
            if (args.Length == 2 && args[1].Boolean == true)
                result += color.A*color.A;
            result = (float) Math.Sqrt(result);

            return new NumberValue() {Number = result};
        }
Пример #18
0
 protected virtual IQueryValue ApplyFieldMapping(IQueryValue qValue)
 {
     if (qValue is QField)
     {
         var qFld = (QField)qValue;
         if (View.FieldMapping != null && View.FieldMapping.ContainsKey(qFld.Name))
         {
             return(new QField(qFld.Name, View.FieldMapping[qFld.Name]));
         }
     }
     return(qValue);
 }
Пример #19
0
        protected QField ComposeFieldValue(Expression expression)
        {
            IQueryValue fldValue = ComposeValue(expression);

            if (fldValue is QField)
            {
                return((QField)fldValue);
            }
            else
            {
                throw new NotSupportedException();
            }
        }
Пример #20
0
 public override string BuildValue(IQueryValue v)
 {
     if (v is Query)
     {
         // refactoring is needed for subqueries handling. TBD: find better solution without 'buildSubquery' delegate.
         if (BuildSubquery == null)
         {
             throw new NotImplementedException("Subqueries are not supported in this context");
         }
         return(BuildSubquery((Query)v));
     }
     return(base.BuildValue(v));
 }
Пример #21
0
        public virtual Query Parse(string relEx)
        {
            int         endIdx;
            IQueryValue qValue = ParseInternal(relEx, 0, out endIdx);

            if (!(qValue is Query))
            {
                throw new RelExParseException("Invalid expression: result is not a query");
            }
            Query q = (Query)qValue;

            return(q);
        }
Пример #22
0
 public override string BuildValue(IQueryValue v)
 {
     if (v is Query)
     {
         // subqueries handling is a bit weird. TBD: find better solution for that.
         if (BuildSubquery == null)
         {
             throw new NotImplementedException("Subqueries are not supported in this context");
         }
         return(BuildSubquery((Query)v));
     }
     return(base.BuildValue(v));
 }
Пример #23
0
 public override string BuildValue(IQueryValue value)
 {
     if (value is Query)
     {
         return(BuildQueryString((Query)value, true));
     }
     if (value is QRawSql)
     {
         return(BuildValue(((QRawSql)value).SqlText) + ":sql");
     }
     if ((value is QField) && !DataHelper.IsSimpleIdentifier(((QField)value).Name))
     {
         return("\"" + base.BuildValue(value).Replace("\"", "\"\"") + "\":field");
     }
     return(base.BuildValue(value));
 }
Пример #24
0
        public static IQueryValue Print(IEnvironment env, IQueryValue[] args)
        {
            Console.ForegroundColor = ConsoleColor.Blue;
            if (args.Length == 0)
            {
                Console.WriteLine();
            }
            else
            {
                Console.WriteLine(string.Join(" ", args.Select(v => v.ToString()).ToArray()));
            }

            Console.ForegroundColor = ConsoleColor.White;

            return new NullValue();
        }
Пример #25
0
        public override IQueryValue EvaluateOperator(IEnvironment env, IQueryValue left, IQueryValue right)
        {
            if (left.GetIQLType() != right.GetIQLType())
                return new BooleanValue() {Boolean = false};

            switch (left.GetIQLType())
            {
                case IQLType.Number:
                    return new BooleanValue() {Boolean = left.Number != right.Number};

                case IQLType.Color:
                    return new BooleanValue() {Boolean = !left.Color.Equals(right.Color)};

                case IQLType.Canvas:
                    return new BooleanValue() {Boolean = left.Canvas != right.Canvas};

                case IQLType.Boolean:
                    return new BooleanValue() {Boolean = left.Boolean != right.Boolean};
            }

            return new BooleanValue() {Boolean = false};
        }
Пример #26
0
        protected QueryNode ParseCondition(string input, int startIdx, out int endIdx)
        {
            IQueryValue leftValue = ParseInternal(input, startIdx, out endIdx);

            int        nextEndIdx;
            Conditions conditions = Conditions.Equal;

            LexemType nextLexemType = GetLexemType(input, endIdx, out nextEndIdx);

            if (!GetCondition(nextLexemType, input, endIdx, ref nextEndIdx, ref conditions))
            {
                throw new RelExParseException(
                          String.Format("Invalid syntax (position: {0}, expression: {1})", startIdx, input));
            }

            IQueryValue rightValue = ParseInternal(input, nextEndIdx, out endIdx);
            QueryNode   node;

            if (IsNullValue(rightValue))
            {
                if ((conditions & Conditions.Equal) != 0)
                {
                    node = new QueryConditionNode(leftValue, Conditions.Null | (conditions & ~Conditions.Equal), null);
                }
                else
                {
                    throw new RelExParseException(
                              String.Format("Invalid syntax - such condition cannot be used with 'null' (position: {0}, expression: {1})", startIdx, input));
                }
            }
            else
            {
                node = new QueryConditionNode(leftValue, conditions, rightValue);
            }

            return(node);
        }
Пример #27
0
        public virtual string BuildValue(IQueryValue value)
        {
            if (value == null)
            {
                return(null);
            }

            if (value is QField)
            {
                return(BuildValue((QField)value));
            }

            if (value is QConst)
            {
                return(BuildValue((QConst)value));
            }

            if (value is QRawSql)
            {
                return(((QRawSql)value).SqlText);
            }

            throw new ArgumentException("Invalid query value", value.GetType().ToString());
        }
Пример #28
0
        public IQueryValue Index(IQueryValue x, IQueryValue y)
        {
            if(IndexOperation == null)
                throw new InvalidOperationException("IndexOperation is not set for IndexedValue");

            return IndexOperation(x, y);
        }
Пример #29
0
 public void CreateParameter(string name, IQueryValue value)
 {
     throw new InvalidOperationException("Cannot create a parameter inside this context");
 }
Пример #30
0
 public void SetVariable(string name, IQueryValue value)
 {
     if(_variables.ContainsKey(name))
         _variables[name] = value;
     else
         Parent.SetVariable(name, value);
 }
Пример #31
0
 public QueryConditionNode(string name, IQueryValue lvalue, Conditions conditions, IQueryValue rvalue) :
     this(lvalue, conditions, rvalue)
 {
     Name = name;
 }
Пример #32
0
        public static IQueryValue Sin(IEnvironment env, IQueryValue[] args)
        {
            FunctionUtils.CheckArgumentCountEqual("sin", 1, args.Length);

            return new NumberValue() {Number = (float) Math.Sin(args[0].Number)};
        }
Пример #33
0
        public static IQueryValue Rand(IEnvironment env, IQueryValue[] args)
        {
            FunctionUtils.CheckArgumentCountEqual("rand", 0, args.Length);

            return new NumberValue() {Number = (float) random.NextDouble()};
        }
Пример #34
0
 public ResolveNodeContext(IQueryValue node, IQueryValue compareNode, IDictionary context)
 {
     _Node        = node;
     _CompareNode = compareNode;
     _Context     = context;
 }
Пример #35
0
        public static IQueryValue Abs(IEnvironment env, IQueryValue[] args)
        {
            FunctionUtils.CheckArgumentCountEqual("abs", 1, args.Length);

            return new NumberValue() {Number = Math.Abs(args[0].Number)};
        }
Пример #36
0
 public void SetVariable(string name, IQueryValue value)
 {
     _variables[name] = value;
 }
Пример #37
0
        private QueryNode ComposeValueTableCondition(string objIdFieldName, Property prop, QField fld, Conditions cnd, IQueryValue val)
        {
            var pSrcName    = ObjStorage.DataTypeTableNames[prop.DataType.ID];
            var subQueryCnd = Conditions.In;

            if (cnd == Conditions.Null)
            {
                // special handling for null test
                subQueryCnd = subQueryCnd | Conditions.Not;
                cnd         = Conditions.Not | Conditions.Null;
            }
            return(new QueryConditionNode(
                       new QField(null, objIdFieldName, null),
                       subQueryCnd,
                       new Query(pSrcName,
                                 (QField)"property_compact_id" == new QConst(prop.CompactID)
                                 &
                                 new QueryConditionNode(fld, cnd, val)
                                 )
            {
                Fields = new[] { (QField)"object_id" }
            }
                       ));
        }
Пример #38
0
 public void CreateParameter(string name, IQueryValue value)
 {
     _parameters.Add(name, value);
 }
Пример #39
0
 bool IsMultivalueConst(IQueryValue val)
 {
     return(val is QConst && ((QConst)val).Value is IList);
 }
Пример #40
0
 public QueryConditionNode(IQueryValue lvalue, Conditions conditions, IQueryValue rvalue)
 {
     _RValue    = rvalue;
     _Condition = conditions;
     _LValue    = lvalue;
 }
Пример #41
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();
        }
Пример #42
0
 public static IQueryValue Null(IEnvironment env, IQueryValue[] args)
 {
     return new NullValue();
 }
Пример #43
0
        public static IQueryValue Clamp(IEnvironment env, IQueryValue[] args)
        {
            FunctionUtils.CheckArgumentCountEqual("clamp", 3, args.Length);

            return new NumberValue() {Number = args[0].Number.Clamp(args[1].Number, args[2].Number)};
        }
 public void CreateVariable(string name, IQueryValue value)
 {
     Parent.CreateVariable(name, value);
 }
Пример #45
0
 protected bool IsNullValue(IQueryValue value)
 {
     return((value is QField) && ((QField)value).Name.ToLower() == nullField);
 }
 public abstract IQueryValue EvaluateOperator(IEnvironment env, IQueryValue left, IQueryValue right);
        public void SetVariable(string name, IQueryValue value)
        {
            switch (name)
            {
                case "width":
                case "height":
                case "x":
                case "y":
                case "color":
                case "red":
                case "green":
                case "blue":
                case "alpha":
                case "r":
                case "g":
                case "b":
                case "a":
                    throw new ArgumentException(string.Format("Cannot set {0} as it is protected in this context", name), "name");

                default:
                    Parent.SetVariable(name, value);
                    break;
            }
        }
Пример #48
0
        protected bool EvaluateInternal(IDictionary context, QueryNode node)
        {
            if (node is QueryConditionNode)
            {
                var condNode = (QueryConditionNode)node;

                IQueryValue lValue = condNode.LValue;
                IQueryValue rValue = condNode.RValue;

                bool negate = (condNode.Condition & Conditions.Not) == Conditions.Not;
                bool isLike = (condNode.Condition & Conditions.Like) == Conditions.Like;
                bool isIn   = (condNode.Condition & Conditions.In) == Conditions.In;
                bool isNull = (condNode.Condition & Conditions.Null) == Conditions.Null;

                ResolveNodeContext lValueContext = new ResolveNodeContext(lValue, rValue, context);
                ResolveNodeContext rValueContext = new ResolveNodeContext(rValue, lValue, context);

                bool compareResult = true;
                if (!isLike && !isIn && !isNull)
                {
                    var leftVal   = ResolveNodeValue(lValueContext);
                    var rightVal  = ResolveNodeValue(rValueContext);
                    var cmpResult = Compare(leftVal, rightVal);

                    compareResult = false;
                    if ((condNode.Condition & Conditions.Equal) == Conditions.Equal && cmpResult == 0)
                    {
                        compareResult = true;
                    }
                    if ((condNode.Condition & Conditions.GreaterThan) == Conditions.GreaterThan && cmpResult > 0)
                    {
                        compareResult = true;
                    }
                    if ((condNode.Condition & Conditions.LessThan) == Conditions.LessThan && cmpResult < 0)
                    {
                        compareResult = true;
                    }
                    if ((condNode.Condition & Conditions.Not) == Conditions.Not)
                    {
                        compareResult = !compareResult;
                    }
                }
                else if (isLike)
                {
                    string lString       = Convert.ToString(ResolveNodeValue(lValueContext));
                    string rString       = Convert.ToString(ResolveNodeValue(rValueContext));
                    bool   startWildcard = rString.StartsWith("%");
                    bool   endWildcard   = rString.EndsWith("%");
                    if (startWildcard)
                    {
                        rString = rString.Substring(1);
                    }
                    if (endWildcard)
                    {
                        rString = rString.Substring(0, rString.Length - 1);
                    }

                    if (startWildcard && endWildcard)
                    {
                        compareResult = lString.Contains(rString);
                    }
                    else if (startWildcard)
                    {
                        compareResult = lString.EndsWith(rString);
                    }
                    else
                    {
                        compareResult = lString.StartsWith(rString);
                    }
                }
                else if (isIn)
                {
                    object lObj = ResolveNodeValue(lValueContext);
                    object rObj = ResolveNodeValue(rValueContext);
                    if (!(rObj is IList))
                    {
                        throw new Exception("Condition 'In' expects IList as right operand");
                    }
                    compareResult = ((IList)rObj).Contains(lObj);
                }
                else if (isNull)
                {
                    object lObj = ResolveNodeValue(lValueContext);
                    compareResult = lObj == null || lObj == DBNull.Value;
                }
                else
                {
                    throw new Exception("Cannot apply conditions: " + condNode.Condition.ToString());
                }

                return(negate ? !compareResult : compareResult);
            }
            if (node is QueryGroupNode)
            {
                var  groupNode   = (QueryGroupNode)node;
                bool groupResult = groupNode.GroupType == QueryGroupNodeType.And ? true : false;
                foreach (QueryNode groupChildNode in groupNode.Nodes)
                {
                    bool childResult = EvaluateInternal(context, groupChildNode);
                    if (groupNode.GroupType == QueryGroupNodeType.And)
                    {
                        groupResult = groupResult && childResult;
                    }
                    if (groupNode.GroupType == QueryGroupNodeType.Or)
                    {
                        groupResult = groupResult || childResult;
                    }
                }
                return(groupResult);
            }
            throw new Exception("Cannot resolve query node type: " + node.GetType().ToString());
        }
Пример #49
0
 public void CreateParameter(string name, IQueryValue value)
 {
     Parent.CreateParameter(name, value);
 }
Пример #50
0
        protected QueryNode ComposeRelatedPropertyCondition(Class dataClass, Relationship rel, QField fld, Conditions cnd, IQueryValue val)
        {
            var relationship = rel;

            if (!rel.Inferred && rel.Object == dataClass)
            {
                var revRelationship = dataClass.FindRelationship(rel.Predicate, rel.Subject, true);
                if (revRelationship == null)
                {
                    throw new ArgumentException(
                              String.Format("Relationship {0} cannot be used in reverse direction", fld.Prefix));
                }
                relationship = revRelationship;
            }
            if (relationship.Subject != dataClass)
            {
                throw new ArgumentException(String.Format("Relationship {0} cannot be used with {1}", fld.Prefix, dataClass.ID));
            }

            var p = relationship.Object.FindPropertyByID(fld.Name);

            if (p == null)
            {
                throw new ArgumentException(
                          String.Format("Related field {0} referenced by relationship {1} doesn't exist",
                                        fld.Name, fld.Prefix));
            }

            var    pLoc              = p.GetLocation(relationship.Object);
            QField pFld              = null;
            Query  propQuery         = null;
            var    subQueryCondition = Conditions.In;

            if (cnd == Conditions.Null)
            {
                subQueryCondition = subQueryCondition | Conditions.Not;
                cnd = Conditions.Null | Conditions.Not;
            }

            // filter by derived property handling
            if (pLoc.Location == PropertyValueLocationType.Derived)
            {
                if (GetDerivedField == null)
                {
                    throw new NotSupportedException(String.Format("Derived property {0} is not supported", pLoc.ToString()));
                }

                if (pLoc.DerivedFrom.Location == PropertyValueLocationType.TableColumn)
                {
                    pFld = GetDerivedField(pLoc, pLoc.DerivedFrom.TableColumnName);
                }
                else if (pLoc.DerivedFrom.Location == PropertyValueLocationType.ValueTable)
                {
                    pFld = GetDerivedField(pLoc, "value");
                }
                else
                {
                    throw new NotSupportedException("DerivedFrom cannot be derived property");
                }
                pLoc = pLoc.DerivedFrom;
            }

            if (pLoc.Location == PropertyValueLocationType.ValueTable)
            {
                var pSrcName = ObjStorage.DataTypeTableNames[pLoc.Property.DataType.ID];
                propQuery = new Query(pSrcName,
                                      (QField)"property_compact_id" == new QConst(pLoc.Property.CompactID)
                                      &
                                      new QueryConditionNode(pFld ?? (QField)"value", cnd, val)
                                      )
                {
                    Fields = new[] { (QField)"object_id" }
                };
            }
            else if (pLoc.Location == PropertyValueLocationType.TableColumn)
            {
                //TBD: handle separate table location
                propQuery = new Query(ObjStorage.ObjectTableName,
                                      new QueryConditionNode(pFld ?? (QField)pLoc.TableColumnName, cnd, val))
                {
                    Fields = new[] { (QField)"id" }
                };
            }

            var reverseRelSequence = relationship.Inferred ? relationship.InferredByRelationships.Reverse() : new[] { relationship };

            foreach (var r in reverseRelSequence)
            {
                propQuery = new Query(
                    new QTable(ObjStorage.ObjectRelationTableName),
                    (QField)"predicate_class_compact_id" == new QConst(r.Predicate.CompactID)
                    &
                    new QueryConditionNode(new QField(r.Reversed ? "subject_id" : "object_id"), Conditions.In,
                                           propQuery
                                           )
                    )
                {
                    Fields = new[] { new QField(r.Reversed ? "object_id" : "subject_id") }
                };
            }

            return(new QueryConditionNode((QField)dataClass.FindPrimaryKeyProperty().ID, subQueryCondition, propQuery));
        }
Пример #51
0
        protected QueryNode ComposePropertyCondition(Class dataClass, Property prop, Conditions cnd, IQueryValue val)
        {
            var propLocation = prop.GetLocation(dataClass);

            QField fld = null;

            if (propLocation.Location == PropertyValueLocationType.Derived)
            {
                if (GetDerivedField == null)
                {
                    throw new NotSupportedException(String.Format("Derived property {0}.{1} is not supported", propLocation.Class.ID, propLocation.Property.ID));
                }

                if (propLocation.DerivedFrom.Location == PropertyValueLocationType.TableColumn)
                {
                    fld = GetDerivedField(propLocation, propLocation.DerivedFrom.TableColumnName);
                }
                else if (propLocation.DerivedFrom.Location == PropertyValueLocationType.ValueTable)
                {
                    fld = GetDerivedField(propLocation, "value");
                }
                else
                {
                    throw new NotSupportedException("DerivedFrom cannot be derived property");
                }
                propLocation = propLocation.DerivedFrom;
            }

            if (propLocation.Location == PropertyValueLocationType.TableColumn)
            {
                return(new QueryConditionNode(fld ?? (QField)propLocation.TableColumnName, cnd, val));
            }
            else if (propLocation.Location == PropertyValueLocationType.ValueTable)
            {
                return(ComposeValueTableCondition(
                           propLocation.Class.FindPrimaryKeyProperty().ID,
                           propLocation.Property, fld ?? (QField)"value", cnd, val));
            }
            else
            {
                throw new NotSupportedException(String.Format("Unsupported location of class property {0}.{1}: {2}",
                                                              propLocation.Class.ID, propLocation.Property.ID,
                                                              propLocation.Location));
            }
        }
Пример #52
0
        public virtual void SetVariable(string name, IQueryValue value)
        {
            switch (name)
            {
                case "r":
                case "red":
                case "g":
                case "green":
                case "b":
                case "blue":
                case "a":
                case "alpha":
                    throw new ArgumentException(string.Format("Cannot set {0} as it is protected in this context", name), "name");

                default:
                    throw new KeyNotFoundException(string.Format("Variable {0} does not exist in this context", name));
            }
        }
Пример #53
0
 // ??
 protected bool IsNullValue(IQueryValue value)
 {
     return ((value is QField) && ((QField)value).Name.ToLower()==nullField);
 }
Пример #54
0
 public void CreateVariable(string name, IQueryValue value)
 {
     throw new InvalidOperationException("Cannot create a variable inside this context");
 }
Пример #55
0
 public IQueryValue Index(IQueryValue x, IQueryValue y)
 {
     throw new InvalidOperationException("Cannot index object of type canvas");
 }