public override void ToXml(SqlElement sqlElement, XmlElement xmlParent) { base.ToXml(sqlElement, xmlParent); SelectSqlForSubQuery subQuery = sqlElement as SelectSqlForSubQuery; XmlElement xmlFrom = SerializerUtil.AddElement(xmlParent, From.FROM); subQuery.From.ToXml(subQuery.From, xmlFrom); XmlElement xmlJoinCondition = SerializerUtil.AddElement(xmlParent, JoinConditionStatement.JOINCONDITIONSTATEMENT); subQuery.JoinCondition.ToXml(subQuery.JoinCondition, xmlJoinCondition); XmlElement xmlCondition = SerializerUtil.AddElement(xmlParent, ConditionStatement.CONDITIONSTATEMENT); subQuery.Condition.ToXml(subQuery.Condition, xmlCondition); /* * MainFromItem和JoinSubQueryConditionItem只序列化,不反序列化。 * 其反序列化操作已包含在From和JoinCondition的集合中,直接从集合中取即可。 * */ XmlElement xmlMainFromItem = SerializerUtil.AddElement(xmlParent, MAINFROMITEM); subQuery.MainFromItem.ToXml(subQuery.MainFromItem, xmlMainFromItem); XmlElement xmlJoinSubQueryConditionItem = SerializerUtil.AddElement(xmlParent, JOINSUBQUERYCONDITIONITEM); subQuery.JoinSubQueryConditionItem.ToXml(subQuery.JoinSubQueryConditionItem, xmlJoinSubQueryConditionItem); }
/// <summary> /// 克隆 /// </summary> /// <returns>删除SQL语句中子查询语句</returns> public override object Clone() { SelectSqlForSubQuery newValue = base.Clone() as SelectSqlForSubQuery; if (From != null) { newValue.From = From.Clone() as From; } if (MainFromItem != null) { newValue.MainFromItem = MainFromItem.Clone() as FromItem; } if (JoinCondition != null) { newValue.JoinCondition = JoinCondition.Clone() as JoinConditionStatement; } //if (JoinSubQueryConditionItem != null) // newValue.joinSubQueryConditionItem = JoinSubQueryConditionItem.Clone() as JoinConditionItem; if (Condition != null) { newValue.Condition = Condition.Clone() as FilterConditionStatement; } return(newValue); }
/// <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; } }
public override void FromXml(SqlElement sqlElement, XmlElement xmlParent, XmlNamespaceManager xnm) { base.FromXml(sqlElement, xmlParent, xnm); SelectSqlForSubQuery subQuery = sqlElement as SelectSqlForSubQuery; ParserUtil util = new ParserUtil(xnm); XmlElement xmlFrom = util.Child(xmlParent, From.FROM); XmlElement xmlJoinCondition = util.Child(xmlParent, JoinConditionStatement.JOINCONDITIONSTATEMENT); XmlElement xmlCondition = util.Child(xmlParent, ConditionStatement.CONDITIONSTATEMENT); subQuery.From.FromXml(subQuery.From, xmlFrom, xnm); subQuery.JoinCondition.FromXml(subQuery.JoinCondition, xmlJoinCondition, xnm); subQuery.Condition.FromXml(subQuery.Condition, xmlCondition, xnm); /* * MainFromItem和JoinSubQueryConditionItem只序列化,不反序列化。 * 其反序列化操作已包含在From和JoinCondition的集合中,直接从集合中取即可。 * */ subQuery.mainFromItem = this.From.ChildCollection[0] as FromItem; subQuery.joinSubQueryConditionItem = this.JoinCondition.ChildCollection[0] as JoinConditionItem; }
/// <summary> /// 构造跟主表的关联。 /// </summary> /// <param name="currentObject">当前的数据模型对象。</param> /// <param name="sqlBuildInfo">SQL构造的中间变量。</param> /// <param name="querySql">删除过滤子查询SQL。</param> /// <remarks> /// 当前对象若是从对象,则需要建立跟主对象的关联, /// 然后根据主对象的唯一标识,形成删除的Sql。 /// </remarks> private void BuildParentInnerJoin(DomainObject currentObject, SqlBuildingInfo sqlBuildInfo, SelectSqlForSubQuery querySql, SqlBuildingContext context) { //如果是根节点,退出 if (currentObject.IsRootObject || currentObject.ID == sqlBuildInfo.CommonObject.RootDomainObjectID) { return; } #region 子节点(当前节点) var currentDataObject = currentObject.DataObject; var tableName = context.GetTableName(currentDataObject.ID); SqlTable currentTable = base.FindSqlTable(tableName, sqlBuildInfo); if (currentTable == null) { currentTable = new SqlTable(tableName, tableName, tableName); base.RegistSqlTable(tableName, currentTable, sqlBuildInfo); } #endregion #region 父节点 var parentObjecct = currentObject.ParentObject; var parentDataObject = parentObjecct.DataObject; var parentTableName = context.GetTableName(parentDataObject.ID); SqlTable parentTable = base.FindSqlTable(parentTableName, sqlBuildInfo); if (parentTable == null) { parentTable = new SqlTable(parentTableName, parentTableName, parentTableName); base.RegistSqlTable(parentTableName, parentTable, sqlBuildInfo); } FromItem parentFromItem = new FromItem(parentTable); querySql.From.ChildCollection.Add(parentFromItem); #endregion #region 关联关系 //目前只是支持单个主对象结构,因此应该只有一个主从关联 var association = currentObject.Associations.FirstOrDefault(i => i.AssociateType == AssociateType.InnerJoin); foreach (var item in association.Items) { //主从关联中,源节点在子节点中,目标节点在父节点上 var sourceElement = currentObject.Elements.FirstOrDefault(i => i.ID == item.SourceElementID); var targetElement = parentObjecct.Elements.FirstOrDefault(i => i.ID == item.TargetElementID); var childCol = currentDataObject.Columns.FirstOrDefault(i => i.ID == sourceElement.ID); var parentCol = parentDataObject.Columns.FirstOrDefault(i => i.ID == targetElement.ID); JoinConditionItem joinItem = new JoinConditionItem(); joinItem.LeftField = new ConditionField(); joinItem.RightField = new ConditionField(); joinItem.LeftField.Table = currentTable; joinItem.LeftField.FieldName = childCol.ColumnName; joinItem.LeftField.IsUseFieldPrefix = true; joinItem.RightField.Table = parentTable; joinItem.RightField.FieldName = parentCol.ColumnName; joinItem.RightField.IsUseFieldPrefix = true; querySql.JoinCondition.ChildCollection.Add(joinItem); } #endregion BuildParentInnerJoin(parentObjecct, sqlBuildInfo, querySql, context); }