/// <summary>
        /// 批量删除
        /// </summary>
        /// <remarks>按主键数据作为查询依据</remarks>
        /// <param name="domainModel">领域模型</param>
        /// <param name="domainObject">领域对象</param>
        /// <param name="dataID">主键数据</param>
        /// <param name="shardingKeyValue">分库分表键值对</param>
        /// <returns>删除SQL</returns>
        public void DeleteByIDs(DomainModel.Spi.DomainModel domainModel, DomainModel.Spi.DomainObject domainObject, IEnumerable <string> dataIDs, ShardingValue shardingKeyValue = null)
        {
            if (domainModel == null)
            {
                throw new ArgumentNullException("DataDeleteService.DeleteByID.domainModel");
            }
            if (domainObject == null)
            {
                throw new ArgumentNullException("DataDeleteService.DeleteByID.domainObject");
            }
            if (dataIDs == null || dataIDs.Count() == 0)
            {
                throw new ArgumentNullException("DataDeleteService.DeleteByID.dataIDs");
            }

            var sqlList = new SqlStatementCollection();

            foreach (var dataID in dataIDs)
            {
                var sqls = SQLBuilderFactory.CreateSQLBuilder().ParseDeleteSqlByID(domainModel, domainObject, dataID, shardingKeyValue);
                sqlList.AddRange(sqls);
            }

            var db = DatabaseFactory.CreateDefaultDatabase();

            db.ExecuteSQLWithTransaction(sqlList);
        }
Example #2
0
        /// <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;
        }
Example #3
0
        /// <summary>
        /// 保存领域模型数据
        /// </summary>
        /// <param name="domainModel">领域模型</param>
        /// <param name="instanceList">对象实例集合</param>
        /// <param name="shardingValueList">分区分表键值对集合</param>
        public void SaveBatch(DomainModel.Spi.DomainModel domainModel, List <object> instanceList, List <ShardingValue> shardingValueList = null)
        {
            if (domainModel == null)
            {
                throw new ArgumentNullException("SaveService.Save.domainModel");
            }
            if (instanceList == null)
            {
                throw new ArgumentNullException("SaveService.Save.instanceList");
            }

            var sqls = new SqlStatementCollection();

            for (int i = 0; i < instanceList.Count; i++)
            {
                var           currentObj    = instanceList[i];
                ShardingValue shardingValue = null;
                if (shardingValueList != null)
                {
                    shardingValue = shardingValueList[i];
                }

                var sqlstatements = SQLBuilderFactory.CreateSQLBuilder().ParseInsertSql(domainModel, currentObj, shardingValue);

                sqls.AddRange(sqlstatements);
            }

            var db = DatabaseFactory.CreateDefaultDatabase();

            db.ExecuteSQLWithTransaction(sqls);
        }
Example #4
0
        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, domainObject.DataObject.DataSource.DbType, 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);
            }
        }
Example #5
0
        /// <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, DataContext context, Dictionary<string, ShardingTarget> routeInfo)
        {
            var result = new SqlStatementCollection();

            foreach (var node in domainModel.DomainObjects)
            {
                var data = context.Data[node.ID];
                if (data == null || data.Count == 0) continue;

                context.ResetCurrentDataIndex();
                var nodeSqls = ParseUpdateSqlDetail(sqlSchemata, domainModel, node, context, routeInfo);
                result.AddRange(nodeSqls);
            }

            return result;
        }
Example #6
0
        /// <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);
            }
        }
Example #7
0
        /// <summary>
        /// 解析生成Insert语句的主干部分。
        /// </summary>
        /// <param name="sqlSchemata">Insert语句的主干。</param>
        /// <param name="co">通用中间对象。</param>
        /// <param name="node">通用中间对象的节点。</param>
        /// <returns>Insert语句的主干部分。</returns>
        private void ParseInsertSqlSchema(SqlStatementCollection sqlSchemata, DomainModel.Spi.DomainModel domainModel, DomainModel.Spi.DomainObject domainObject,
                                          Dictionary <string, ShardingTarget> routeInfo)
        {
            var insertStrategy = new InsertSqlBuildStrategy();
            var context        = new SqlBuildingContext(domainModel, domainObject, domainObject.DataObject.DataSource.DbType);

            context.RouteInfo = routeInfo;

            var nodeSqlSchemata = insertStrategy.BuildTableSqlSchema(context);

            sqlSchemata.AddRange(nodeSqlSchemata);

            //递归处理子对象
            if (context.Node.ChildDomainObjects.Count == 0)
            {
                return;
            }

            foreach (var childModelObject in context.Node.ChildDomainObjects)
            {
                ParseInsertSqlSchema(sqlSchemata, domainModel, childModelObject, routeInfo);
            }
        }
Example #8
0
        /// <summary>
        /// 解析生成Insert语句的主干部分。
        /// </summary>
        /// <param name="sqlSchemata">Insert语句的主干。</param>
        /// <param name="co">通用中间对象。</param>
        /// <param name="node">通用中间对象的节点。</param>
        /// <returns>Insert语句的主干部分。</returns>
        private void ParseUpdateSqlSchema(SqlStatementCollection sqlSchemata, DomainModel.Spi.DomainModel domainModel, DomainModel.Spi.DomainObject domainObject,
                                          Dictionary <string, ShardingTarget> routeInfo, DataContext dataContext)
        {
            var updateStrategy = new UpdateSqlBuildStrategy();
            var context        = new SqlBuildingContext(domainModel, domainObject, domainObject.DataObject.DataSource.DbType);

            context.RouteInfo = routeInfo;
            if (dataContext.GetCurrentDataContextItem(domainObject.ID) != null)
            {
                var nodeSqlSchemata = updateStrategy.BuildTableSqlSchema(context);
                sqlSchemata.AddRange(nodeSqlSchemata);
            }

            //递归处理子对象
            if (context.Node.ChildDomainObjects.Count == 0)
            {
                return;
            }

            foreach (var childModelObject in context.Node.ChildDomainObjects)
            {
                ParseUpdateSqlSchema(sqlSchemata, domainModel, childModelObject, routeInfo, dataContext);
            }
        }