protected QueryGroupNode TranslateGroupNode(Class dataClass, QueryGroupNode node)
        {
            var group = new QueryGroupNode(node);

            for (int i = 0; i < group.Nodes.Count; i++)
            {
                group.Nodes[i] = TranslateQueryNode(dataClass, group.Nodes[i]);
            }
            return(group);
        }
Example #2
0
            protected override string BuildGroup(QueryGroupNode node)
            {
                var grp = base.BuildGroup(node);

                if (!String.IsNullOrEmpty(node.Name))
                {
                    return(String.Format("(<{0}> {1})", node.Name, grp));
                }
                else
                {
                    return(grp);
                }
            }
Example #3
0
        protected IQueryNode ComposeFromCondition(object fromKey)
        {
            var grpAnd = new QueryGroupNode(GroupType.And);

            grpAnd.Nodes.Add((QField)FromFieldName == new QConst(fromKey));
            if (ExtraKeys != null)
            {
                foreach (var entry in ExtraKeys)
                {
                    grpAnd.Nodes.Add((QField)entry.Key == new QConst(entry.Value));
                }
            }
            return(grpAnd);
        }
Example #4
0
        protected QueryGroupNode ComposeGroupNode(QueryNode node1, QueryNode node2, QueryGroupNodeType groupType)
        {
            QueryGroupNode group1 = node1 as QueryGroupNode, group2 = node2 as QueryGroupNode;

            if (group1 != null && group1.GroupType != groupType)
            {
                group1 = null;
            }
            if (group2 != null && group2.GroupType != groupType)
            {
                group2 = null;
            }

            // don't corrupt named groups
            if (group1 != null && group1.Name != null || group2 != null && group2.Name != null)
            {
                group1 = group2 = null;
            }

            if (group1 == null)
            {
                if (group2 == null)
                {
                    QueryGroupNode group = new QueryGroupNode(groupType);
                    group.Nodes.Add(node1);
                    group.Nodes.Add(node2);
                    return(group);
                }
                else
                {
                    group2.Nodes.Insert(0, node1);
                    return(group2);
                }
            }
            else
            {
                if (group2 == null)
                {
                    group1.Nodes.Add(node2);
                }
                else
                {
                    foreach (QueryNode qn in group2.Nodes)
                    {
                        group1.Nodes.Add(qn);
                    }
                }
                return(group1);
            }
        }
Example #5
0
        protected QueryNode ComposeUidCondition(IDictionary keys)
        {
            if (keys.Count == 0)
            {
                throw new MissingPrimaryKeyException();
            }
            // compose UID condition
            QueryGroupNode uidGroup = new QueryGroupNode(QueryGroupNodeType.And);

            foreach (DictionaryEntry key in keys)
            {
                uidGroup.Nodes.Add(new QField(key.Key.ToString()) == new QConst(key.Value));
            }
            return(uidGroup);
        }
Example #6
0
        protected IQueryNode ComposeLiteralFilter(IList <FieldDescriptor> flds, LiteralFilter[] filters)
        {
            // at least one
            var qNode = new QueryGroupNode(GroupType.Or);

            for (int i = 0; i < flds.Count; i++)
            {
                var fldFlt = ComposeLiteralFilter(flds[i], filters);
                if (fldFlt != null)
                {
                    qNode.Nodes.Add(fldFlt);
                }
            }
            return(qNode.Nodes.Count > 0 ? qNode : null);
        }
        public Query TranslateSubQuery(Query query)
        {
            // is class query?
            var dataClass = Schema.FindClassByID(query.Table.Name);

            if (dataClass != null)
            {
                var tableName = ObjStorage.ObjectTableName;
                if (dataClass.ObjectLocation == ObjectLocationType.SeparateTable)
                {
                    tableName = dataClass.ID;
                }
                var dataClassQuery = new Query(new QTable(tableName, query.Table.Alias));
                dataClassQuery.Condition =
                    (QField)"compact_class_id" == new QConst(dataClass.CompactID)
                    &
                    TranslateQueryNode(dataClass, query.Condition);
                // TBD: add support for any field
                CheckFieldsConstraint(query, dataClass.FindPrimaryKeyProperty().ID);
                dataClassQuery.Fields = query.Fields;
                return(dataClassQuery);
            }
            var relationship = Schema.FindRelationshipByID(query.Table.Name);

            if (relationship != null)
            {
                var relQuery          = new Query(new QTable(ObjStorage.ObjectRelationTableName, query.Table.Alias));
                var relQueryCondition = QueryGroupNode.And((QField)"predicate_class_compact_id" == new QConst(relationship.Predicate.CompactID));
                relQueryCondition.Nodes.Add(TranslateQueryNode(relationship.Predicate, query.Condition));
                relQuery.Condition = relQueryCondition;

                if (query.Fields != null)
                {
                    CheckFieldsConstraint(query, "subject_id", "object_id");
                }

                relQuery.Fields = query.Fields == null ?
                                  new[] { new QField(query.Table.Alias, "subject_id", null), new QField(query.Table.Alias, "object_id", null) }
                                        : query.Fields;

                return(relQuery);
            }

            // if nothing matched keep query as is
            return(query);
        }
        protected QueryNode ApplyRuleConditions(QueryNode node, string tableName, DalcOperation operation)
        {
            var resNode = new QueryGroupNode(QueryGroupNodeType.And);

            resNode.Nodes.Add(node);
            var context = CreatePermissionContext(tableName, operation);

            for (int i = 0; i < Rules.Length; i++)
            {
                var r = Rules[i];
                var extraCondition = r.ComposeCondition(context);
                if (extraCondition != null)
                {
                    resNode.Nodes.Add(extraCondition);
                }
            }
            return(resNode.Nodes.Count > 1 ? resNode : node);
        }
Example #9
0
        protected IQueryNode ComposeLiteralFilter(FieldDescriptor fld, LiteralFilter[] filters)
        {
            if (fld.FkSourceName != null)
            {
                return(null);                // only _literals_
            }
            var qNode = new QueryGroupNode(GroupType.And);

            for (int i = 0; i < filters.Length; i++)
            {
                var cnd = ComposeLiteralCondition(fld, filters[i]);
                if (cnd != null)
                {
                    qNode.Nodes.Add(cnd);
                }
            }
            return(qNode.Nodes.Count > 0 ? qNode : null);
        }
Example #10
0
        protected Query GetUserQueryBySample(User userSample)
        {
            var condition = new QueryGroupNode(QueryGroupNodeType.And);

            if (userSample.Id != null)
            {
                condition.Nodes.Add((QField)ResolveFieldName("Id") == new QConst(userSample.Id));
            }
            else if (userSample.Username != null)
            {
                condition.Nodes.Add((QField)ResolveFieldName("Username") == (QConst)userSample.Username);
            }
            else if (userSample.Email != null)
            {
                condition.Nodes.Add((QField)ResolveFieldName("Email") == (QConst)userSample.Email);
            }
            return(new Query(LoadUserSourceName, condition));
        }
Example #11
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();
        }
 protected IQueryNode ComposeFromCondition(object fromKey)
 {
     var grpAnd = new QueryGroupNode(GroupType.And);
     grpAnd.Nodes.Add((QField)FromFieldName == new QConst(fromKey));
     if (ExtraKeys != null)
         foreach (var entry in ExtraKeys)
             grpAnd.Nodes.Add((QField)entry.Key == new QConst(entry.Value));
     return grpAnd;
 }
Example #13
0
        protected QueryNode ParseConditionGroup(string input, int startIdx, out int endIdx)
        {
            int       nextEndIdx;
            LexemType lexemType = GetLexemType(input, startIdx, out nextEndIdx);
            string    lexem     = GetLexem(input, startIdx, nextEndIdx);

            QueryNode node;

            if (lexemType == LexemType.Delimiter && lexem == "(")
            {
                string nodeName = ParseNodeName(input, nextEndIdx, out endIdx);
                nextEndIdx = endIdx;

                // check for empty group
                lexemType = GetLexemType(input, nextEndIdx, out endIdx);
                if (lexemType == LexemType.Delimiter && GetLexem(input, nextEndIdx, endIdx) == ")")
                {
                    node = null;
                    // push back
                    endIdx = nextEndIdx;
                }
                else
                {
                    node = ParseConditionGroup(input, nextEndIdx, out endIdx);
                }

                if (nodeName != null)
                {
                    if (node == null)
                    {
                        node = new QueryGroupNode(QueryGroupNodeType.And);
                    }
                    if (node is QueryNode)
                    {
                        ((QueryNode)node).Name = nodeName;
                    }
                    // for some reason QueryGroupNode is not derived from QueryNode...
                    if (node is QueryGroupNode)
                    {
                        ((QueryGroupNode)node).Name = nodeName;
                    }
                }

                // read ')'
                lexemType = GetLexemType(input, endIdx, out nextEndIdx);
                if (lexemType != LexemType.Delimiter || GetLexem(input, endIdx, nextEndIdx) != ")")
                {
                    throw new RelExParseException(
                              String.Format("Invalid syntax (position: {0}, expression: {1})", endIdx, input));
                }
                endIdx = nextEndIdx;
            }
            else
            {
                node   = ParseCondition(input, startIdx, out nextEndIdx);
                endIdx = nextEndIdx;
            }

            // check for group
            lexemType = GetLexemType(input, endIdx, out nextEndIdx);
            QueryGroupNodeType groupType = QueryGroupNodeType.And;

            if (GetGroupType(lexemType, input, endIdx, ref nextEndIdx, ref groupType))
            {
                return(ComposeGroupNode(node, ParseConditionGroup(input, nextEndIdx, out endIdx), groupType));
            }

            return(node);
        }
Example #14
0
 protected Query GetUserQueryBySample(User userSample)
 {
     var condition = new QueryGroupNode(GroupType.And);
     if (userSample.Id != null)
         condition.Nodes.Add( (QField)ResolveFieldName("Id") == new QConst(userSample.Id) );
     else if (userSample.Username != null)
         condition.Nodes.Add( (QField)ResolveFieldName("Username") == (QConst)userSample.Username );
     else if (userSample.Email != null)
         condition.Nodes.Add( (QField)ResolveFieldName("Email") == (QConst)userSample.Email );
     return new Query(LoadUserSourceName, condition);
 }
Example #15
0
        protected void LoadToSink(SourceDescriptor sourceDescr, IList <object> ids, IList <FieldDescriptor> predFlds, Resource[] vals, StatementSink sink, SelectFilter flt)
        {
            // todo: more effective impl using IDbDalc datareader
            var ds       = new DataSet();
            var q        = new Query(sourceDescr.SourceName);
            var flds     = predFlds ?? sourceDescr.Fields;
            var loadType = flt.Predicates == null || flt.Predicates.Contains(NS.Rdf.typeEntity);

            q.Fields    = new string[flds.Count + 1];
            q.Fields[0] = sourceDescr.IdFieldName;
            for (int i = 0; i < flds.Count; i++)
            {
                q.Fields[i + 1] = flds[i].FieldName;
            }
            // compose query condition
            var condition = new QueryGroupNode(GroupType.And);

            if (ids != null)
            {
                condition.Nodes.Add(ComposeCondition(sourceDescr.IdFieldName, ids.ToArray()));
            }
            if (vals != null && !loadType)
            {
                var orGrp = new QueryGroupNode(GroupType.Or);
                for (int i = 0; i < flds.Count; i++)
                {
                    var valCnd = ComposeCondition(flds[i], vals);
                    if (valCnd != null)
                    {
                        orGrp.Nodes.Add(valCnd);
                    }
                }
                if (orGrp.Nodes.Count == 0)
                {
                    return;                     //values are not for this source
                }
                condition.Nodes.Add(orGrp);
            }
            if (flt.LiteralFilters != null)
            {
                var literalFltCondition = ComposeLiteralFilter(flds, flt.LiteralFilters);
                if (literalFltCondition != null)
                {
                    condition.Nodes.Add(literalFltCondition);
                }
            }
            q.Root = condition;
            if (flt.Limit > 0)
            {
                q.RecordCount = flt.Limit;
            }
            // log
            log.Write(LogEvent.Debug, q);
            Console.WriteLine(q.ToString());
            // query result handler
            Action <IDataReader> loadToSinkAction = delegate(IDataReader dataReader) {
                int recIndex = 0;
                while (dataReader.Read() && (recIndex < q.RecordCount))
                {
                    recIndex++;
                    var itemEntity = GetSourceItemEntity(sourceDescr, dataReader[sourceDescr.IdFieldName]);
                    for (int j = 0; j < flds.Count; j++)
                    {
                        var f   = flds[j];
                        var obj = PrepareResource(f, dataReader[f.FieldName]);
                        if (vals == null || vals.Contains(obj))
                        {
                            // literals post-filter
                            if (flt.LiteralFilters != null && !LiteralFilter.MatchesFilters(obj, flt.LiteralFilters, this))
                            {
                                continue;
                            }
                            if (!sink.Add(new Statement(itemEntity, EntityFieldHash[f], obj)))
                            {
                                return;
                            }
                        }
                    }
                    // type predicate
                    if (loadType && flt.LiteralFilters == null)
                    {
                        if (vals == null || vals.Contains(EntitySourceHash[sourceDescr]))
                        {
                            if (!sink.Add(
                                    new Statement(itemEntity, NS.Rdf.typeEntity, EntitySourceHash[sourceDescr])))
                            {
                                return;
                            }
                        }
                    }
                }
            };

            // DB DALC datareader optimization
            if (Dalc is IDbDalc)
            {
                var  dbDalc    = (IDbDalc)Dalc;
                bool closeConn = false;
                try {
                    if (dbDalc.Connection.State != ConnectionState.Open)
                    {
                        dbDalc.Connection.Open();
                        closeConn = true;
                    }
                    IDataReader rdr = dbDalc.LoadReader(q);
                    try {
                        loadToSinkAction(rdr);
                    } finally {
                        rdr.Close();
                    }
                } finally {
                    if (closeConn)
                    {
                        dbDalc.Connection.Close();
                    }
                }
            }
            else
            {
                Dalc.Load(ds, q);
                var tblRdr = ds.Tables[q.SourceName].CreateDataReader();
                try {
                    loadToSinkAction(tblRdr);
                } finally {
                    tblRdr.Close();
                }
            }
        }