Пример #1
0
 private void VisitValueProperty(IManagedProperty mp, ITableSource mpOwnerTable)
 {
     //如果已经记录了条件的属性,那么当前的 mp 就是用于对比的第二个属性。(A.Code = A.Name 中的 Name)
     if (_propertyResult != null)
     {
         _rightPropertyResult = mpOwnerTable.Column(mp);
     }
     //如果还没有记录属性,说明当前条件要比较的属性就是 mp;(A.Code = 1 中的 Code)
     else
     {
         _propertyResult = mpOwnerTable.Column(mp);
     }
 }
Пример #2
0
        /// <summary>
        /// 在查询对象中查找或者创建指定引用属性对应的连接表对象。
        /// </summary>
        /// <param name="ownerTable">引用属性对应外键所在的表。</param>
        /// <param name="refProperty">指定的引用属性。</param>
        /// <returns></returns>
        internal ITableSource FindOrCreateJoinTable(ITableSource ownerTable, IRefEntityProperty refProperty)
        {
            var query          = this as IQuery;
            var refTableSource = _allJoinTables.FirstOrDefault(
                ts => ts.OwnerTable == ownerTable && ts.Property == refProperty
                );

            if (refTableSource == null)
            {
                var f = QueryFactory.Instance;

                var refEntityType = refProperty.RefEntityType;
                var refRepo       = RepositoryFactoryHost.Factory.FindByEntity(refEntityType);
                var refTable      = f.Table(refRepo, this.NextJoinTableAlias());

                var joinType = refProperty.Nullable ? JoinType.LeftOuter : JoinType.Inner;
                query.From = f.Join(query.From, refTable, f.Constraint(
                                        ownerTable.Column(refProperty.RefIdProperty),
                                        refTable.Column(Entity.IdProperty)
                                        ), joinType);

                refTableSource = new SqlTableSource
                {
                    OwnerTable = ownerTable,
                    Property   = refProperty,
                    RefTable   = refTable,
                };
                _allJoinTables.Add(refTableSource);
            }

            return(refTableSource.RefTable);
        }
Пример #3
0
        /// <summary>
        /// 通过左实体数据源和右实体数据源,来找到它们之间的第一个的引用属性,用以构造一个连接。
        /// </summary>
        /// <param name="left">拥有引用关系的左数据源。</param>
        /// <param name="right">右实体数据源。</param>
        /// <returns></returns>
        public IJoin Join(
            ITableSource left,
            ITableSource right
            )
        {
            //通过实体的对比,找到从左实体到右实体之间的第一个的引用属性。
            var rightEntity = right.EntityRepository.EntityType;
            var properties  = left.EntityRepository.EntityMeta.ManagedProperties.GetCompiledProperties();

            for (int i = 0, c = properties.Count; i < c; i++)
            {
                var refProperty = properties[i] as IRefEntityProperty;
                if (refProperty != null && refProperty.RefEntityType == rightEntity)
                {
                    var condition = this.Constraint(
                        left.Column(refProperty.RefIdProperty),
                        right.Column(Entity.IdProperty)
                        );

                    var joinType = refProperty.Nullable ? JoinType.LeftOuter : JoinType.Inner;

                    return(Join(left, right, condition, joinType));
                }
            }

            throw new InvalidProgramException(string.Format(
                                                  "没有从 {0} 到 {1} 的引用关系,请指定具体的对比条件。", left.GetName(), right.GetName()
                                                  ));
        }
Пример #4
0
        private void VisitValueProperty(IManagedProperty mp, ITableSource mpOwnerTable)
        {
            //接下来,是一般属性的处理
            if (_visitRefProperties)
            {
                throw OperationNotSupported(string.Format("不支持使用属性:{0}。这是因为它的拥有者是一个值属性,值属性只支持直接对比。", mp.Name));
            }

            //如果已经记录了条件的属性,那么当前的 mp 就是用于对比的第二个属性。(A.Code = A.Name 中的 Name)
            if (_propertyResult != null)
            {
                _rightPropertyResult = mpOwnerTable.Column(mp);
            }
            //如果还没有记录属性,说明当前条件要比较的属性就是 mp;(A.Code = 1 中的 Code)
            else
            {
                _propertyResult = mpOwnerTable.Column(mp);
            }
        }
Пример #5
0
        /// <summary>
        /// 通过左数据源和右实体数据源,以及从左到右的引用属性,来构造一个连接。
        /// </summary>
        /// <param name="left">左实体数据源。</param>
        /// <param name="right">右实体数据源。</param>
        /// <param name="leftToRight">从左到右的引用属性。</param>
        /// <returns></returns>
        public IJoin Join(
            ITableSource left,
            ITableSource right,
            IRefProperty leftToRight
            )
        {
            var condition = this.Constraint(
                left.Column(leftToRight),
                right.Column(Entity.IdProperty)
                );

            var joinType = leftToRight.Nullable ? JoinType.LeftOuter : JoinType.Inner;

            return(Join(left, right, condition, joinType));
        }
Пример #6
0
        /// <summary>
        /// 构建权限过滤条件
        /// </summary>
        /// <param name="mainTable"></param>
        /// <param name="query"></param>
        /// <returns></returns>
        protected override IConstraint BuildConstraintCore(ITableSource mainTable, IQuery query)
        {
            if (UserIdProperty == null)
            {
                throw new ArgumentNullException("this.UserIdProperty");
            }
            var currentUser    = AccountContext.CurrentUser;
            var currentUserid  = currentUser != null ? currentUser.Id : 0;//如果是匿名用户,则这个条件永远返回 False。
            var userIdProperty = mainTable.EntityRepository.EntityMeta.Property(this.UserIdProperty);

            if (userIdProperty == null)
            {
                throw new InvalidProgramException();
            }
            return(mainTable.Column(userIdProperty.ManagedProperty).Equal(currentUserid));
        }
        /// <summary>
        /// 构建权限过滤条件
        /// </summary>
        /// <param name="mainTable"></param>
        /// <param name="query"></param>
        /// <returns></returns>
        protected override IConstraint BuildConstraintCore(ITableSource mainTable, IQuery query)
        {
            if (this.GroupIdProperty == null)
            {
                throw new ArgumentNullException("this.GroupIdProperty");
            }
            var currentUser     = AccountContext.CurrentUser;
            var currentUserid   = currentUser != null ? currentUser.Id : 0;
            var groupIdList     = GetGroupListByUserId(currentUserid, IsIncludeChildGroup);
            var groupIdProperty = mainTable.EntityRepository.EntityMeta.Property(this.GroupIdProperty);

            if (groupIdProperty == null)
            {
                throw new InvalidProgramException();
            }
            return(mainTable.Column(groupIdProperty.ManagedProperty).In(groupIdList));
        }
Пример #8
0
        private IConstraint CreatePropertyConstraint(string property, string comparison, string value)
        {
            var mp = _properties.Find(property, true);

            if (mp == null)
            {
                throw new InvalidProgramException(string.Format("查询条件解析出错,没有找到名称为 {0} 的属性。", property));
            }

            var column = _mainTable.Column(mp);

            var op = PropertyOperator.Equal;

            switch (comparison.ToLower())
            {
            case "eq":
                op = PropertyOperator.Equal;
                break;

            case "ne":
                op = PropertyOperator.NotEqual;
                break;

            case "gt":
                op = PropertyOperator.Greater;
                break;

            case "ge":
                op = PropertyOperator.GreaterEqual;
                break;

            case "lt":
                op = PropertyOperator.Less;
                break;

            case "le":
                op = PropertyOperator.LessEqual;
                break;

            default:
                throw new NotSupportedException("不支持这个操作符:" + comparison + "。");
            }

            return(_f.Constraint(column, op, value));
        }
Пример #9
0
        /// <summary>
        /// 为查询添加一个对应的约束条件,并以 And 与原条件进行连接。
        /// </summary>
        /// <param name="query">查询.</param>
        /// <param name="property">要约束的属性.</param>
        /// <param name="op">约束条件操作符.</param>
        /// <param name="value">对比的值。</param>
        /// <param name="propertySource">指定该属性所属的实体数据源。</param>
        /// <returns></returns>
        public static IQuery AddConstraint(this IQuery query, IManagedProperty property, PropertyOperator op, object value, ITableSource propertySource)
        {
            var f = QueryFactory.Instance;

            var propertyNode = propertySource.Column(property);

            var where = f.Constraint(propertyNode, op, value);
            if (query.Where == null)
            {
                query.Where = where;
            }
            else
            {
                query.Where = f.And(query.Where, where);
            }

            return(query);
        }
Пример #10
0
        /// <summary>
        /// 为查询添加一个对应的约束条件,并以 And 与原条件进行连接。
        /// </summary>
        /// <param name="query">查询.</param>
        /// <param name="property">要约束的属性.</param>
        /// <param name="op">约束条件操作符.</param>
        /// <param name="value">对比的值。</param>
        /// <param name="propertySource">指定该属性所属的实体数据源。</param>
        /// <returns></returns>
        public static IQuery AddConstraint(this IQuery query, IManagedProperty property, PropertyOperator op, object value, ITableSource propertySource)
        {
            var f = QueryFactory.Instance;

            var propertyNode = propertySource.Column(property);

            var where = f.Constraint(propertyNode, op, value);
            if (query.Where == null)
            {
                query.Where = where;
            }
            else
            {
                query.Where = f.And(query.Where, where);
            }

            return query;
        }
Пример #11
0
        private SqlTableSource AddJoinTable(ITableSource fkTable, IRefProperty parentRef, ITableSource pkTable, ITableSource joinTo)
        {
            var f = QueryFactory.Instance;

            var joinType = parentRef.Nullable ? JoinType.LeftOuter : JoinType.Inner;
            var query    = this as IQuery;

            query.From = f.Join(query.From, joinTo, f.Constraint(
                                    fkTable.Column(parentRef.RefIdProperty),
                                    pkTable.IdColumn
                                    ), joinType);

            var refTableSource = new SqlTableSource
            {
                ForeignKeyTable = fkTable,
                RefProperty     = parentRef,
                PrimaryKeyTable = pkTable,
            };

            _allJoinTables.Add(refTableSource);

            return(refTableSource);
        }
Пример #12
0
        private void DealConstraintWord(string part)
        {
            //part 表示列名
            if (_column == null)
            {
                //可能使用了引用属性,例如表达式:User.Name eq 'huqf'
                var properties = part.Split(new char[] { '.' }, StringSplitOptions.RemoveEmptyEntries);
                if (properties.Length > 1)
                {
                    ITableSource lastTable = _mainTable;
                    for (int i = 0; i < properties.Length; i++)
                    {
                        var property = properties[i];
                        var mp       = lastTable.EntityRepository.EntityMeta.ManagedProperties.GetCompiledProperties().Find(property);
                        if (mp == null)
                        {
                            throw new InvalidProgramException(string.Format("查询条件解析出错,没有找到名称为 {0} 的属性。", property));
                        }

                        var refProperty = mp as IRefEntityProperty;
                        if (refProperty != null)
                        {
                            lastTable = _f.FindOrCreateJoinTable(_query, lastTable, refProperty);
                        }
                        else
                        {
                            _column = lastTable.Column(mp);
                        }
                    }
                    if (_column == null)
                    {
                        throw new InvalidProgramException(string.Format("{0} 查询条件出错:属性表达式不能以引用实体属性结尾。", part));
                    }
                }
                else
                {
                    var mp = _properties.Find(part, true);
                    if (mp == null)
                    {
                        throw new InvalidProgramException(string.Format("查询条件解析出错,没有找到名称为 {0} 的属性。", part));
                    }
                    _column = _mainTable.Column(mp);
                }
            }
            //part 表示操作符
            else if (_comparison == null)
            {
                _comparison = part;
            }
            //part 表示值
            else
            {
                var propertyConstraint = CreateColumnConstraint(_comparison, part);
                if (_concat.HasValue && _current != null)
                {
                    _current = _f.Binary(_current, _concat.Value, propertyConstraint);
                    _concat  = null;
                }
                else
                {
                    _current = propertyConstraint;
                }

                _column     = null;
                _comparison = null;
            }
        }
Пример #13
0
 private void VisitValueProperty(IManagedProperty mp, ITableSource mpOwnerTable)
 {
     //如果已经记录了条件的属性,那么当前的 mp 就是用于对比的第二个属性。(A.Code = A.Name 中的 Name)
     if (_propertyResult != null)
     {
         _rightPropertyResult = mpOwnerTable.Column(mp);
     }
     //如果还没有记录属性,说明当前条件要比较的属性就是 mp;(A.Code = 1 中的 Code)
     else
     {
         _propertyResult = mpOwnerTable.Column(mp);
     }
 }
Пример #14
0
        private SqlTableSource AddJoinTable(ITableSource fkTable, IRefProperty parentRef, ITableSource pkTable, ITableSource joinTo)
        {
            var f = QueryFactory.Instance;

            var joinType = parentRef.Nullable ? JoinType.LeftOuter : JoinType.Inner;
            var query = this as IQuery;
            query.From = f.Join(query.From, joinTo, f.Constraint(
                fkTable.Column(parentRef.RefIdProperty),
                pkTable.IdColumn
                ), joinType);

            var refTableSource = new SqlTableSource
            {
                ForeignKeyTable = fkTable,
                RefProperty = parentRef,
                PrimaryKeyTable = pkTable,
            };
            _allJoinTables.Add(refTableSource);

            return refTableSource;
        }