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));
            }
        }
Example #2
0
        public void QField()
        {
            var f = new QField("simple");

            Assert.Equal("simple", f.Name);
            Assert.Null(f.Expression);
            Assert.Null(f.Prefix);

            f = new QField("t.field");
            Assert.Equal("field", f.Name);
            Assert.Null(f.Expression);
            Assert.Equal("t", f.Prefix);

            f = new QField("dbo.t.field");
            Assert.Equal("field", f.Name);
            Assert.Null(f.Expression);
            Assert.Equal("dbo.t", f.Prefix);

            f = new QField("sum(field)");
            Assert.Equal("sum(field)", f.Name);
            Assert.Equal("sum(field)", f.Expression);
            Assert.Null(f.Prefix);

            f = new QField("sum(t.field)");
            Assert.Equal("sum(t.field)", f.Name);
            Assert.Equal("sum(t.field)", f.Expression);
            Assert.Null(f.Prefix);

            f = new QField("sum(field) as  fld_test");
            Assert.Equal("fld_test", f.Name);
            Assert.Equal("sum(field)", f.Expression);
            Assert.Null(f.Prefix);

            f = new QField("CAST(field as nvarchar)");
            Assert.Equal("CAST(field as nvarchar)", f.Name);
            Assert.Equal("CAST(field as nvarchar)", f.Expression);
            Assert.Null(f.Prefix);
        }
Example #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();
        }
Example #4
0
        public static void LoadMetaField(QObject qObject, MetaClass currentClass)
        {
            foreach (MetaField field in currentClass.UserMetaFields)
            {
                if (qObject.Fields[string.Format("{1}", qObject.OwnerTable, field.Name)] != null)
                {
                    continue;
                }

                switch (field.DataType)
                {
                case MetaDataType.DictionaryMultivalue:
                case MetaDataType.EnumMultivalue:
                    QField MFieldValue = new QField(string.Format("{1}", qObject.OwnerTable, field.Name),
                                                    field.FriendlyName,
                                                    "Value",
                                                    DbType.String,
                                                    QFieldUsingType.Field | QFieldUsingType.Grouping | QFieldUsingType.Filter,
                                                    new QFieldJoinRelation[] {
                        new QFieldJoinRelation(qObject.OwnerTable, currentClass.TableName, qObject.KeyField.DBName, "ObjectId"),
                        new QFieldJoinRelation(currentClass.TableName, "MetaMultivalueDictionary", field.Name, "MetaKey"),
                        new QFieldJoinRelation("MetaMultivalueDictionary", "MetaDictionary", "MetaDictionaryId", "MetaDictionaryId", new SimpleFilterCondition(new QField("MetaFieldId"), field.Id.ToString(), SimpleFilterType.Equal))
                    }
                                                    );

                    QField MFieldId = new QField(string.Format("{1}Id", qObject.OwnerTable, field.Name),
                                                 field.FriendlyName,
                                                 "MetaDictionaryId",
                                                 DbType.String,
                                                 QFieldUsingType.Abstract,
                                                 new QFieldJoinRelation[] {
                        new QFieldJoinRelation(qObject.OwnerTable, currentClass.TableName, qObject.KeyField.DBName, "ObjectId"),
                        new QFieldJoinRelation(currentClass.TableName, "MetaMultivalueDictionary", field.Name, "MetaKey"),
                        new QFieldJoinRelation("MetaMultivalueDictionary", "MetaDictionary", "MetaDictionaryId", "MetaDictionaryId", new SimpleFilterCondition(new QField("MetaFieldId"), field.Id.ToString(), SimpleFilterType.Equal))
                    }
                                                 );

                    qObject.Fields.Add(MFieldValue);
                    qObject.Fields.Add(MFieldId);

                    qObject.Dictionary.Add(new QDictionary(MFieldId, MFieldValue, string.Format("SELECT MetaDictionaryId as Id, Value FROM MetaDictionary WHERE MetaFieldId = {0}", field.Id)));
                    break;

                case MetaDataType.DictionarySingleValue:
                case MetaDataType.EnumSingleValue:
                    QField SFieldValue = new QField(string.Format("{1}", qObject.OwnerTable, field.Name),
                                                    field.FriendlyName,
                                                    "Value",
                                                    DbType.String,
                                                    QFieldUsingType.Field | QFieldUsingType.Grouping | QFieldUsingType.Filter | QFieldUsingType.Sort,
                                                    new QFieldJoinRelation[] {
                        new QFieldJoinRelation(qObject.OwnerTable, currentClass.TableName, qObject.KeyField.DBName, "ObjectId"),
                        new QFieldJoinRelation(currentClass.TableName, "MetaDictionary", field.Name, "MetaDictionaryId", new SimpleFilterCondition(new QField("MetaFieldId"), field.Id.ToString(), SimpleFilterType.Equal))
                    }
                                                    );

                    QField SFieldId = new QField(string.Format("{1}Id", qObject.OwnerTable, field.Name),
                                                 field.FriendlyName,
                                                 "MetaDictionaryId",
                                                 DbType.String,
                                                 QFieldUsingType.Abstract,
                                                 new QFieldJoinRelation[] {
                        new QFieldJoinRelation(qObject.OwnerTable, currentClass.TableName, qObject.KeyField.DBName, "ObjectId"),
                        new QFieldJoinRelation(currentClass.TableName, "MetaDictionary", field.Name, "MetaDictionaryId", new SimpleFilterCondition(new QField("MetaFieldId"), field.Id.ToString(), SimpleFilterType.Equal))
                    }
                                                 );

                    qObject.Fields.Add(SFieldValue);
                    qObject.Fields.Add(SFieldId);

                    qObject.Dictionary.Add(new QDictionary(SFieldId, SFieldValue, string.Format("SELECT MetaDictionaryId as Id, Value FROM MetaDictionary WHERE MetaFieldId = {0}", field.Id)));
                    break;

                case MetaDataType.Image:
                case MetaDataType.Binary:
                    // Ignory [12/8/2004]
                    break;

                case MetaDataType.File:
                case MetaDataType.ImageFile:
                    qObject.Fields.Add(new QField(string.Format("{1}", qObject.OwnerTable, field.Name),
                                                  field.FriendlyName,
                                                  "FileName",
                                                  DbType.String,
                                                  QFieldUsingType.Field | QFieldUsingType.Grouping,
                                                  new QFieldJoinRelation[] {
                        new QFieldJoinRelation(qObject.OwnerTable, currentClass.TableName, qObject.KeyField.DBName, "ObjectId"),
                        new QFieldJoinRelation(currentClass.TableName, "MetaFileValue", field.Name, "MetaKey")
                    }
                                                  ));

                    break;

                case MetaDataType.LongHtmlString:
                case MetaDataType.LongString:
                    qObject.Fields.Add(new QField(string.Format("{1}", qObject.OwnerTable, field.Name),
                                                  field.FriendlyName,
                                                  field.Name,
                                                  MetaDataType2DbType(field.DataType),
                                                  QFieldUsingType.Field | QFieldUsingType.Grouping | QFieldUsingType.Sort,
                                                  new QFieldJoinRelation(qObject.OwnerTable, currentClass.TableName, qObject.KeyField.DBName, "ObjectId"))
                                       );
                    break;

                default:
                    qObject.Fields.Add(new QField(string.Format("{1}", qObject.OwnerTable, field.Name),
                                                  field.FriendlyName,
                                                  field.Name,
                                                  MetaDataType2DbType(field.DataType),
                                                  QFieldUsingType.Field | QFieldUsingType.Grouping | QFieldUsingType.Filter | QFieldUsingType.Sort,
                                                  new QFieldJoinRelation(qObject.OwnerTable, currentClass.TableName, qObject.KeyField.DBName, "ObjectId"))
                                       );
                    break;
                }
            }
        }
 bool IsClassPropertyField(Class dataClass, QField fld)
 {
     return(dataClass.FindPropertyByID(fld.Name) != null);
 }
        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" }
            }
                       ));
        }
        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));
        }
Example #8
0
 public void AddField(QField field)
 {
     field.Index         = Columns.Count;
     Columns[field.Name] = field;
 }
Example #9
0
        protected string MakeFilterText(IBNReportTemplate repTemp, QObject qItem)
        {
            string retval = "";

            foreach (FilterInfo fi in repTemp.Filters)
            {
                QField qTemp = qItem.Fields[fi.FieldName];
                if (qTemp == null)
                {
                    continue;
                }
                QDictionary qDTemp = qItem.GetDictionary(qTemp);
                if (qDTemp != null)
                {
                    if (fi.Values.Count > 0)
                    {
                        retval += qTemp.FriendlyName + " = ";
                        string sqlCommand = qDTemp.GetSQLQuery(Security.CurrentUser.LanguageId);
                        using (IDataReader reader = Report.GetQDictionary(sqlCommand))
                        {
                            ArrayList alDicVal = new ArrayList();
                            foreach (string _s in fi.Values)
                            {
                                alDicVal.Add(_s);
                            }
                            while (reader.Read())
                            {
                                if (alDicVal.Contains(reader["Id"].ToString()))
                                {
                                    retval += "<font color='red'>" + CommonHelper.GetResFileString(reader["Value"].ToString()) + "</font>,&nbsp;";
                                }
                            }
                        }
                        retval = retval.Remove(retval.Length - 7, 7) + "<br>";
                    }
                }
                else
                {
                    switch (qTemp.DataType)
                    {
                    case DbType.Decimal:
                    case DbType.Int32:
                        if (fi.Values.Count > 0)
                        {
                            retval += qTemp.FriendlyName;
                            switch (fi.Values[0])
                            {
                            case "0":
                                retval += "&nbsp;=<font color='red'>&nbsp;" + fi.Values[1] + "</font><br>";
                                break;

                            case "1":
                                retval += "&nbsp;&gt;<font color='red'>&nbsp;" + fi.Values[1] + "</font><br>";
                                break;

                            case "2":
                                retval += "&nbsp;&lt;<font color='red'>&nbsp;" + fi.Values[1] + "</font><br>";
                                break;

                            case "3":
                                retval += "&nbsp;<font color='red'>" + LocRM.GetString("tBetween") + "&nbsp;" + fi.Values[1] + "&nbsp;-&nbsp;" + fi.Values[2] + "</font><br>";
                                break;
                            }
                        }
                        break;

                    case DbType.DateTime:
                    case DbType.Date:
                        if (fi.Values.Count > 0)
                        {
                            retval += qTemp.FriendlyName;
                            switch (fi.Values[0])
                            {
                            case "1":
                                retval += "&nbsp;=&nbsp;<font color='red'>" + LocRM.GetString("tToday") + "</font><br>";
                                break;

                            case "2":
                                retval += "&nbsp;=&nbsp;<font color='red'>" + LocRM.GetString("tYesterday") + "</font><br>";
                                break;

                            case "3":
                                retval += "&nbsp;=&nbsp;<font color='red'>" + LocRM.GetString("tThisWeek") + "</font><br>";
                                break;

                            case "4":
                                retval += "&nbsp;=&nbsp;<font color='red'>" + LocRM.GetString("tLastWeek") + "</font><br>";
                                break;

                            case "5":
                                retval += "&nbsp;=&nbsp;<font color='red'>" + LocRM.GetString("tThisMonth") + "</font><br>";
                                break;

                            case "6":
                                retval += "&nbsp;=&nbsp;<font color='red'>" + LocRM.GetString("tLastMonth") + "</font><br>";
                                break;

                            case "7":
                                retval += "&nbsp;=&nbsp;<font color='red'>" + LocRM.GetString("tThisYear") + "</font><br>";
                                break;

                            case "8":
                                retval += "&nbsp;=&nbsp;<font color='red'>" + LocRM.GetString("tLastYear") + "</font><br>";
                                break;

                            case "9":
                                if (DateTime.Parse(fi.Values[1]) == DateTime.MinValue)
                                {
                                    retval += "&nbsp;<font color='red'>" + LocRM.GetString("tLess") + "&nbsp;" + DateTime.Parse(fi.Values[2]).ToShortDateString() + "</font><br>";
                                }
                                else if (DateTime.Parse(fi.Values[2]) >= DateTime.MaxValue.Date)
                                {
                                    retval += "&nbsp;<font color='red'>" + LocRM.GetString("tGreater") + "&nbsp;" + DateTime.Parse(fi.Values[1]).ToShortDateString() + "</font><br>";
                                }
                                else
                                {
                                    retval += "&nbsp;<font color='red'>" + LocRM.GetString("tBetween") + "&nbsp;" + DateTime.Parse(fi.Values[1]).ToShortDateString() + "&nbsp;-&nbsp;" + DateTime.Parse(fi.Values[2]).ToShortDateString() + "</font><br>";
                                }
                                break;
                            }
                        }
                        break;

                    case DbType.String:
                        if (fi.Values.Count > 0)
                        {
                            retval += qTemp.FriendlyName + "&nbsp;=&nbsp;<font color='red'>" + fi.Values[0] + "</font><br>";
                        }
                        break;

                    case DbType.Time:
                        retval += qTemp.FriendlyName;
                        if (fi.Values.Count > 0)
                        {
                            switch (fi.Values[0])
                            {
                            case "0":
                                retval += "&nbsp;=<font color='red'>&nbsp;" + CommonHelper.GetHours(int.Parse(fi.Values[1])) + "</font><br>";
                                break;

                            case "1":
                                retval += "&nbsp;&gt;<font color='red'>&nbsp;" + CommonHelper.GetHours(int.Parse(fi.Values[1])) + "</font><br>";
                                break;

                            case "2":
                                retval += "&nbsp;&lt;<font color='red'>&nbsp;" + CommonHelper.GetHours(int.Parse(fi.Values[1])) + "</font><br>";
                                break;

                            case "3":
                                retval += "&nbsp;<font color='red'>" + LocRM.GetString("tBetween") + "&nbsp;" + CommonHelper.GetHours(int.Parse(fi.Values[1])) + "&nbsp;-&nbsp;" + CommonHelper.GetHours(int.Parse(fi.Values[2])) + "</font><br>";
                                break;
                            }
                        }
                        else
                        {
                            retval += "<br>";
                        }
                        break;
                    }
                }
            }
            return(retval);
        }