Beispiel #1
0
        internal SelectQuery GetSelectViewModel(Type type,
                                                QueryConditions tableConditions = null,
                                                FilterRequest filter            = null)
        {
            var reflectionHelper = new TablesReflectionHelper();
            var root             = reflectionHelper.GetReflectionTable(type);
            var query            = CreateFullSelectQuery(root, tableConditions, filter);

            return(query);
        }
Beispiel #2
0
        internal string GetAbstractQuery(Type type,
                                         string whereTerm     = null,
                                         FilterRequest filter = null)
        {
            var reflectionHelper = new TablesReflectionHelper();
            var node             = reflectionHelper.GetReflectionTable(type);
            var result           = new AbstractQuery()
            {
                SelectTerm = GetSelectTerm($"[{node.TableName}].[{node.PrimaryKey.Name}]", node.TableName, node.Cols),
                Root       = node,
                WhereTerm  = whereTerm
            };

            (result.WhereTerm, result.OrderByTerm) =
                GetOrderByTerm(result.WhereTerm, filter, node.TableName, node.Cols);
            node.UniqName = node.TableName;
            var que = new Queue <SelectStackItem>();

            if (node.JoinTables != null)
            {
                foreach (var joinTable in node.JoinTables
                         .Where(q => q.SelectedForAbstract))
                {
                    if (!joinTable.IsOneToMany && !joinTable.IsInverse)
                    {
                        que.Enqueue(new SelectStackItem()
                        {
                            Table          = joinTable,
                            ParentUniqName = node.UniqName
                        });
                    }
                }

                while (que.Any())
                {
                    var item = que.Dequeue();
                    node = item.Table.ReflectionTable;
                    // سلمت
                    result.SelectTerm = GetSelectTerm(result.SelectTerm, node.UniqName,
                                                      node.Cols);
                    // جوین ها
                    result.JoinTerm += Environment.NewLine +
                                       $" left join [{node.TableName}] as {node.UniqName} " +
                                       $"on [{item.ParentUniqName}].[{item.Table.ForeignKey}] = [{node.UniqName}].{node.PrimaryKey.Name} ";

                    // فیلتر
                    if (filter != null && string.IsNullOrEmpty(result.OrderByTerm))
                    {
                        (result.WhereTerm, result.OrderByTerm) =
                            GetOrderByTerm(result.WhereTerm, filter, node.UniqName, node.Cols);
                    }

                    // زیر مجموعه
                    if (node.JoinTables != null)
                    {
                        foreach (var joinTable in node.JoinTables
                                 .Where(q => q.SelectedForAbstract))
                        {
                            if (!joinTable.IsOneToMany && !joinTable.IsInverse)
                            {
                                que.Enqueue(new SelectStackItem()
                                {
                                    Table          = joinTable,
                                    ParentUniqName = node.UniqName
                                });
                            }
                        }
                    }
                }
            }

            if (!string.IsNullOrEmpty(result.WhereTerm))
            {
                result.WhereTerm = "Where " + result.WhereTerm;
            }
            var nodeQuery = $@"select {result.SelectTerm} 
                               from [{result.Root.TableName}] 
                               {result.JoinTerm}
                               {result.WhereTerm}
                               {result.OrderByTerm}";

            nodeQuery += Environment.NewLine;

            return(nodeQuery);
        }