/// <summary> /// 根据已经解析的SqlSchema,解析生成带数据的Update语句。 /// </summary> /// <param name="sqlSchemata">已经解析的SqlSchema。</param> /// <param name="domainModel">领域对象</param> /// <param name="context">数据上下文</param> /// <returns>Insert语句集合</returns> private SqlStatementCollection ParseUpdateSqlDetail(SqlStatementCollection sqlSchemata, DomainModel.Spi.DomainModel domainModel, DomainModel.Spi.DomainObject domainObject, DataContext dataContext, Dictionary<string, ShardingTarget> routeInfo) { var result = new SqlStatementCollection(); if (!dataContext.Data.ContainsKey(domainObject.ID)) { return result; } var data = dataContext.Data[domainObject.ID]; if (data == null || data.Count == 0) return result; var strategy = new UpdateSqlBuildStrategy(); for (int i = 0; i < dataContext.Data[domainObject.ID].Count; i++) { dataContext.CurrentDataIndex = i; var context = new SqlBuildingContext(domainModel, domainObject, SQLBuilderUtils.GetCurrentDbType(), dataContext); context.RouteInfo = routeInfo; var rowSqls = new SqlStatementCollection(); var sqls = sqlSchemata.Where(s => s.SqlBuildingInfo.CurrentNode.ID == domainObject.ID).ToList(); rowSqls.AddRangeClone(sqls); strategy.BuildTableSqlDetail(rowSqls, context); result.AddRange(rowSqls); } return result; }
/// <summary> /// 将SQL语句中最后修改时间和创建时间字段值替换为获取数据库时间函数。 /// </summary> /// <param name="insertFieldList">插入的字段</param> /// <param name="insertValueList">插入的字段值</param> /// <returns>替换最后修改时间和创建时间后的SQL语句</returns> private string GetFieldValueClause(InsertFieldList insertFieldList, InsertValueList insertValueList) { StringBuilder insertFields = new StringBuilder(); StringBuilder insertValues = new StringBuilder(); List <InsertField> removeFields = new List <InsertField>(); List <SqlElement> removeValues = new List <SqlElement>(); for (int i = 0; i < insertValueList.ChildCollection.Count; i++) { InsertField field = insertFieldList.ChildCollection[i] as InsertField; string val = ((InsertValue)insertValueList.ChildCollection[i]).Value as string; if (val != null) { //将最后修改时间和创建时间两个字段的值替换为获取数据库时间的方法 if (val == SQLBuilderUtils.CREATETIME || val == SQLBuilderUtils.LASTCHANGEDTIME) { insertFields.AppendFormat("{0},", field.FieldName); removeFields.Add(field); removeValues.Add(insertValueList.ChildCollection[i]); insertValues.AppendFormat("{0},", SQLBuilderUtils.GetReallyDbDateTime()); } else { insertFields.AppendFormat("{0},", field.FieldName); insertValues.AppendFormat(":{0},", field.FieldName); } } //对于一些如Byte[]类型的参数,无法转换为string类型,将其直接加到insertFields和insertValues中 else { insertFields.AppendFormat("{0},", field.FieldName); insertValues.AppendFormat(":{0},", field.FieldName); } } //最后修改时间和创建时间两个字段已完成参数替换,因此将它们从insertFieldList中删除 foreach (var field in removeFields) { insertFieldList.ChildCollection.Remove(field); } //最后修改时间和创建时间两个字段已完成参数替换,因此将它们从insertValueList中删除 foreach (var value in removeValues) { insertValueList.ChildCollection.Remove(value); } //去换字符串中最后的那个“,” if (insertFields.ToString().EndsWith(",")) { insertFields.Remove(insertFields.Length - 1, 1); insertValues.Remove(insertValues.Length - 1, 1); } //此时返回的string中有最后修改时间和创建时间两个字段和值, //因为它们没有在insertFields和insertValues中被删除 return(string.Format("({0})VALUES({1})", insertFields, insertValues)); }
/// <summary> /// 构造查询SQL语句的主干结构 /// </summary> /// <param name="dataObject">数据对象</param> /// <returns>查询SQL语句的主干结构</returns> private void ParseDeleteSqlSchema(SqlStatementCollection sqlSchemata, DomainModel.Spi.DomainModel domainModel, DomainModel.Spi.DomainObject domainObject, Dictionary<string, ShardingTarget> routeInfo, DataContext dataContext) { var selectStrategy = new DeleteSqlBuildStrategy(); var context = new SqlBuildingContext(domainModel, domainObject, SQLBuilderUtils.GetCurrentDbType(), dataContext); context.RouteInfo = routeInfo; var nodeSqlSchemata = selectStrategy.BuildTableSqlSchema(context); sqlSchemata.AddRange(nodeSqlSchemata); //递归处理子对象 if (domainObject.ChildDomainObjects.Count == 0) return; foreach (var childModelObject in domainObject.ChildDomainObjects) { ParseDeleteSqlSchema(sqlSchemata, domainModel, childModelObject, routeInfo, dataContext); } }
private void ParseDeleteSqlDetail(SqlStatementCollection sqlSchemata, SqlStatementCollection result, DomainModel.Spi.DomainModel domainModel, DomainModel.Spi.DomainObject domainObject, DataContext dataContext, Dictionary<string, ShardingTarget> routeInfo) { //构造SQL语句的条件 var strategy = new DeleteSqlBuildStrategy(); var context = new SqlBuildingContext(domainModel, domainObject, SQLBuilderUtils.GetCurrentDbType(), dataContext); context.RouteInfo = routeInfo; var rowSqls = new SqlStatementCollection(); var sqls = sqlSchemata.Where(s => s.SqlBuildingInfo.CurrentNode.ID == domainObject.ID).ToList(); rowSqls.AddRangeClone(sqls); strategy.BuildTableSqlDetail(rowSqls, context); result.AddRange(rowSqls); //递归处理子对象 if (domainObject.ChildDomainObjects.Count == 0) return; foreach (var childModelObject in domainObject.ChildDomainObjects) { ParseDeleteSqlDetail(sqlSchemata, result, domainModel, childModelObject, dataContext, routeInfo); } }