Ejemplo n.º 1
0
        /// <summary>
        /// 贪婪加载某个属性
        /// </summary>
        /// <param name="property">需要贪婪加载的托管属性。可以是一个引用属性,也可以是一个组合子属性。</param>
        private void EagerLoad(ConcreteProperty property)
        {
            if (this.EagerLoadOptions == null)
            {
                this.EagerLoadOptions = new EagerLoadOptions();
            }

            this.EagerLoadOptions.CoreList.Add(property);
        }
Ejemplo n.º 2
0
        public override void RefreshRedundancy(ConcreteProperty redundancy)
        {
            /*********************** 代码块解释 *********************************
             * http://blog.csdn.net/peng790/article/details/6585202
             *
             * 假定场景是:
             * D(CRef,AName) -> C(BRef) -> B(ARef) -> A(Name);
             * 则对应生成的 SQL 是:
             * update D set AName = @AName where CId in (
             *     select id from C where BId in (
             *          select id from B where AId = @AId
             *     )
             * )
             * 如果 B、C 表也有冗余属性,对应的 SQL 则是:
             * update B Set AName = @AName where AId = @AId
             * update C set AName = @AName where BId in (
             *     select id from B where AId = @AId
             * )
             *
            **********************************************************************/

            //update B set AName = A.Name FROM A WHERE A.ID = b.AId
            //update C set AName = A.Name FROM A INNER JOIN B ON A.Id = B.AId WHERE B.Id = C.BId
            //update D set AName = A.Name FROM A INNER JOIN B ON A.Id = B.AId INNER JOIN C ON B.Id = c.BId WHERE C.Id = D.CId

            ////准备所有用到的 DbTable
            //var table = RdbTableFinder.TableFor(path.Redundancy.Owner);
            //var refTables = new RefPropertyTable[refPathes.Count];
            //for (int i = 0, c = refPathes.Count; i < c; i++)
            //{
            //    var refProperty = refPathes[i];
            //    var refTable = RdbTableFinder.TableFor(refProperty.Owner);
            //    if (refTable == null)
            //    {
            //        ORMHelper.ThrowBasePropertyNotMappedException(refProperty.Name, refProperty.Owner);
            //    }

            //    refTables[i] = new RefPropertyTable
            //    {
            //        RefProperty = refProperty,
            //        OwnerTable = refTable
            //    };
            //}

            //var sql = new ConditionalSql();
            ////SQL: UPDATE D SET AName = {0} WHERE
            //sql.Append("UPDATE ").AppendQuoteName(table)
            //    .Append(" SET ").AppendQuote(table, table.Translate(redundancy.Property))
            //    .Append(" = ").AppendParameter(newValue).Append(" WHERE ");

            //int quoteNeeded = 0;
            //if (refTables.Length > 1)
            //{
            //    //中间的都生成 Where XX in
            //    var inWherePathes = refTables.Take(refTables.Length - 1).ToArray();
            //    for (int i = 0; i < inWherePathes.Length; i++)
            //    {
            //        var inRef = inWherePathes[i];

            //        //SQL: CId In (
            //        sql.AppendQuote(table, inRef.OwnerTable.Translate(inRef.RefProperty.Property))
            //            .Append(" IN (").AppendLine();
            //        quoteNeeded++;

            //        var nextRef = refTables[i + 1];

            //        //SQL: SELECT Id FROM C WHERE
            //        var nextRefOwnerTable = nextRef.OwnerTable;
            //        sql.Append(" SELECT ").AppendQuote(nextRefOwnerTable, nextRefOwnerTable.PKColumn.Name)
            //            .Append(" FROM ").AppendQuoteName(nextRefOwnerTable)
            //            .Append(" WHERE ");
            //    }
            //}

            ////最后一个,生成SQL: BId = {1}
            //var lastRef = refTables[refTables.Length - 1];
            //sql.AppendQuote(table, lastRef.OwnerTable.Translate(lastRef.RefProperty.Property))
            //    .Append(" = ").AppendParameter(lastRefId);

            //while (quoteNeeded > 0)
            //{
            //    sql.AppendLine(")");
            //    quoteNeeded--;
            //}

            ////执行最终的 SQL 语句
            //using (var dba = _dataProvider.CreateDbAccesser())
            //{
            //    dba.ExecuteText(sql, sql.Parameters);
            //}
        }
Ejemplo n.º 3
0
        protected override void UpdateRedundancy(Entity entity, ConcreteProperty redundancy, object newValue, IList<ConcreteProperty> refPathes, object lastRefId)
        {
            /*********************** 代码块解释 *********************************
             *
             * 假定场景是:
             * refPathes: D(CRef,AName) -> C(BRef) -> B(ARef),
             * lastRefId: AId。(B.ARef 是最后一个引用属性)
             * 则对应生成的 SQL 是:
             * update D set AName = @AName where CId in (
             *     select id from C where BId in (
             *          select id from B where AId = @AId
             *     )
             * )
             * 如果 B、C 表也有冗余属性,对应的 SQL 则是:
             * update B Set AName = @AName where AId = @BId
             * update C set AName = @AName where BId in (
             *     select id from B where AId = @AId
             * )
             *
            **********************************************************************/

            //准备所有用到的 DbTable
            var table = RdbTableFinder.TableFor(redundancy.Owner);
            var refTables = new RefPropertyTable[refPathes.Count];
            for (int i = 0, c = refPathes.Count; i < c; i++)
            {
                var refProperty = refPathes[i];
                var refTable = RdbTableFinder.TableFor(refProperty.Owner);
                if (refTable == null)
                {
                    ORMHelper.ThrowBasePropertyNotMappedException(refProperty.Name, refProperty.Owner);
                }

                refTables[i] = new RefPropertyTable
                {
                    RefProperty = refProperty,
                    OwnerTable = refTable
                };
            }

            var sql = new ConditionalSql();
            //SQL: UPDATE D SET AName = {0} WHERE
            sql.Append("UPDATE ").AppendQuoteName(table)
                .Append(" SET ").AppendQuote(table, table.Translate(redundancy.Property))
                .Append(" = ").AppendParameter(newValue).Append(" WHERE ");

            int quoteNeeded = 0;
            if (refTables.Length > 1)
            {
                //中间的都生成 Where XX in
                var inWherePathes = refTables.Take(refTables.Length - 1).ToArray();
                for (int i = 0; i < inWherePathes.Length; i++)
                {
                    var inRef = inWherePathes[i];

                    //SQL: CId In (
                    sql.AppendQuote(table, inRef.OwnerTable.Translate(inRef.RefProperty.Property))
                        .Append(" IN (").AppendLine();
                    quoteNeeded++;

                    var nextRef = refTables[i + 1];

                    //SQL: SELECT Id FROM C WHERE
                    var nextRefOwnerTable = nextRef.OwnerTable;
                    sql.Append(" SELECT ").AppendQuote(nextRefOwnerTable, nextRefOwnerTable.PKColumn.Name)
                        .Append(" FROM ").AppendQuoteName(nextRefOwnerTable)
                        .Append(" WHERE ");
                }
            }

            //最后一个,生成SQL: BId = {1}
            var lastRef = refTables[refTables.Length - 1];
            sql.AppendQuote(table, lastRef.OwnerTable.Translate(lastRef.RefProperty.Property))
                .Append(" = ").AppendParameter(lastRefId);

            while (quoteNeeded > 0)
            {
                sql.AppendLine(")");
                quoteNeeded--;
            }

            //执行最终的 SQL 语句
            using (var dba = _dataProvider.CreateDbAccesser())
            {
                dba.ExecuteText(sql, sql.Parameters);
            }
        }
Ejemplo n.º 4
0
 /// <summary>
 /// 更新某个冗余属性
 /// </summary>
 /// <param name="entity">The entity.</param>
 /// <param name="redundancy">更新指定的冗余属性</param>
 /// <param name="newValue">冗余属性的新值</param>
 /// <param name="refPathes">从冗余属性声明类型开始的一个引用属性集合,
 /// 将会为这个集合路径生成更新的 Where 条件。</param>
 /// <param name="lastRefId">引用路径中最后一个引用属性对应的值。这个值将会作为 Where 条件的值。</param>
 protected abstract void UpdateRedundancy(Entity entity, ConcreteProperty redundancy, object newValue, IList<ConcreteProperty> refPathes, object lastRefId);