/// <summary>
        /// 形成包含数据的SQL语句过滤条件。
        /// </summary>
        /// <param name="sql">构造过滤条件的DeleteSql。</param>
        /// <param name="context">SQL构造的上下文信息。</param>
        protected void HandlingCondition(DeleteSqlStatement sql, SqlBuildingContext context)
        {
            var sqlInfo = sql.SqlBuildingInfo;
            var currentPrimaryKeyData = context.DataContext.GetCurrentPrimaryKeyData();

            //当前是从节点
            if (!sqlInfo.CurrentNode.IsRootObject || sqlInfo.CommonObject.RootDomainObjectID != sqlInfo.CurrentNode.ID)
            {
                DomainObject currentObject = null;

                //判断下context.DataContext.PrimaryKeyData数据来自于哪个节点
                DataObject currentDataObject = null;

                foreach (KeyValuePair <string, object> data in currentPrimaryKeyData)
                {
                    foreach (var modelObject in sqlInfo.CommonObject.DomainObjects)
                    {
                        var element = modelObject.Elements.FirstOrDefault(i => i.ID == data.Key);
                        if (element != null)
                        {
                            currentObject     = modelObject;
                            currentDataObject = modelObject.DataObject;
                            break;
                        }
                    }
                }

                if (currentObject == null || currentDataObject == null)
                {
                    var e = new Exception("DomainModel Error PrimaryKeyData! DomainModelID" + sqlInfo.CommonObject.ID);
                    if (currentPrimaryKeyData != null)
                    {
                        foreach (var data in currentPrimaryKeyData)
                        {
                            e.Data.Add(data.Key, data.Value);
                        }
                    }

                    throw e;
                }

                //添加根节点的主键值条件
                sql.SubQuerySql.Condition.ChildCollection.Add(base.GetPrimaryKeyConditions(context,
                                                                                           sql.SqlBuildingInfo,
                                                                                           currentObject,
                                                                                           currentPrimaryKeyData, true));
            }
            else
            {
                // 生成SqlSchema时,形成了子查询。但使用时context.IsUsePrimaryCondition== true,则将子查询置空。
                sql.SubQuerySql = null;
                context.IsUsePrimaryCondition = true;
                sql.Conditions.ChildCollection.Add(base.GetPrimaryKeyConditions(context,
                                                                                sql.SqlBuildingInfo,
                                                                                sql.SqlBuildingInfo.CurrentNode, currentPrimaryKeyData));
            }
        }
예제 #2
0
        public override void ToXml(SqlElement sqlElement, XmlElement xmlParent)
        {
            base.ToXml(sqlElement, xmlParent);

            DeleteSqlStatement deleteSql    = sqlElement as DeleteSqlStatement;
            XmlElement         xmlCondition = SerializerUtil.AddElement(xmlParent, ConditionStatement.CONDITIONSTATEMENT);

            deleteSql.Condition.ToXml(deleteSql.Condition, xmlCondition);
            if (deleteSql.SubQuerySql != null)// SubQuerySql 可以为空
            {
                XmlElement xmlSubQuerySql = SerializerUtil.AddElement(xmlParent, SUBQUERYSQL);
                deleteSql.SubQuerySql.ToXml(deleteSql.SubQuerySql, xmlSubQuerySql);
            }
        }
예제 #3
0
        /// <summary>
        /// 克隆
        /// </summary>
        /// <returns>Delete删除SQL语句</returns>
        public override object Clone()
        {
            DeleteSqlStatement newObject = base.Clone() as DeleteSqlStatement;

            if (Condition != null)
            {
                newObject.Condition = Condition.Clone() as ConditionStatement;
            }
            if (SubQuerySql != null)
            {
                newObject.SubQuerySql = SubQuerySql.Clone() as SelectSqlForSubQuery;
            }

            return(newObject);
        }
        /// <summary>
        ///  根据SQL构造上下文信息,构造DeleteSql的过滤条件。
        /// </summary>
        /// <param name="context">SQL构造的上下文信息。</param>
        /// <param name="sql">构造过滤条件的DeleteSql。</param>
        protected void HandlingJoin(DeleteSqlStatement sql, SqlBuildingContext context)
        {
            var currentObject = sql.SqlBuildingInfo.CurrentNode;

            //1.如果是根节点
            if (currentObject.IsRootObject || currentObject.ID == sql.SqlBuildingInfo.CommonObject.RootDomainObjectID ||
                context.IsUsePrimaryCondition)
            {
                sql.SubQuerySql = null;
                return;
            }
            //2.如果是从节点或者从从节点
            else
            {
                var sqlInfo  = sql.SqlBuildingInfo;
                var querySql = new SelectSqlForSubQuery();
                //获取当前节点的SQLTable
                var      currentTableName = context.TableName;
                SqlTable table            = base.FindSqlTable(currentTableName, sqlInfo);
                if (table == null)
                {
                    table = new SqlTable(currentTableName, currentTableName, currentTableName);
                    base.RegistSqlTable(currentTableName, table, sql.SqlBuildingInfo);
                }
                //指定为查询的主表
                //querySql.MainFromItem.Table = table;

                //构造和根节点的关联关系
                BuildParentInnerJoin(currentObject, sqlInfo, querySql, context);

                // 构造删除的主表和子查询的之间的关联关系,支持复合主键
                //var rTable = base.FindSqlTable(currentTableName, sql.SqlBuildingInfo);
                //foreach (var pkColumn in context.DataObject.PKColumns)
                //{
                //    querySql.JoinSubQueryConditionItem.LeftField.Table = sql.SqlBuildingInfo.CurrentSqlTable;
                //    querySql.JoinSubQueryConditionItem.RightField.Table = rTable;
                //    querySql.JoinSubQueryConditionItem.LeftField.IsUseFieldPrefix = true;
                //    querySql.JoinSubQueryConditionItem.RightField.IsUseFieldPrefix = true;
                //    querySql.JoinSubQueryConditionItem.LeftField.FieldName = pkColumn.ColumnName;
                //    querySql.JoinSubQueryConditionItem.RightField.FieldName = pkColumn.ColumnName;
                //    //先只循环一次
                //    break;
                //}

                sql.SubQuerySql = querySql;
            }
        }
예제 #5
0
        public override void FromXml(SqlElement sqlElement, XmlElement xmlParent, XmlNamespaceManager xnm)
        {
            base.FromXml(sqlElement, xmlParent, xnm);

            DeleteSqlStatement deleteSql    = sqlElement as DeleteSqlStatement;
            ParserUtil         util         = new ParserUtil(xnm);
            XmlElement         xmlCondition = util.Child(xmlParent, ConditionStatement.CONDITIONSTATEMENT);

            deleteSql.Condition.FromXml(deleteSql.Condition, xmlCondition, xnm);
            XmlElement xmlSubQuerySql = util.Child(xmlParent, SUBQUERYSQL);

            if (xmlSubQuerySql != null)
            {
                deleteSql.SubQuerySql = new SelectSqlForSubQuery();
                deleteSql.SubQuerySql.FromXml(deleteSql.SubQuerySql, xmlSubQuerySql, xnm);
            }
        }