/// <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); }
/// <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); }
/// <summary> /// 获取对象数据 /// </summary> /// <param name="domainModel">领域模型</param> /// <param name="domainObject">领域对象</param> /// <param name="queryFilter">查询条件</param> /// <returns>查询结果</returns> public QueryResultSet GetData(NSharding.DomainModel.Spi.DomainModel domainModel, DomainObject domainObject, QueryFilter queryFilter) { if (domainModel == null) throw new ArgumentNullException("DataQueryService.GetData.domainModel"); if (queryFilter == null) throw new ArgumentNullException("DataQueryService.GetData.queryFilter"); var sqls = SQLBuilderFactory.CreateSQLBuilder(domainModel).ParseQuerySqlByFilter(domainModel, domainObject, queryFilter); var db = DatabaseFactory.CreateDatabase(domainModel); var dts = db.GetDataCollection(sqls); return new QueryResultSet { ShardingInfo = sqls.ShardingInfo, DataTables = dts }; }
/// <summary> /// 获取对象数据 /// </summary> /// <param name="domainModel">领域模型</param> /// <param name="dataID">数据唯一标识</param> /// <param name="shardingValue">分库分表键值对</param> /// <returns>对象数据</returns> public QueryResultSet GetData(NSharding.DomainModel.Spi.DomainModel domainModel, string dataID, ShardingValue shardingValue = null) { if (domainModel == null) throw new ArgumentNullException("DataQueryService.GetData.domainModel"); if (string.IsNullOrWhiteSpace(dataID)) throw new ArgumentNullException("DataQueryService.GetData.dataID"); var sqls = SQLBuilderFactory.CreateSQLBuilder(domainModel).ParseQuerySqlByID(domainModel, dataID, shardingValue); var db = DatabaseFactory.CreateDatabase(domainModel); var dts = db.GetDataCollection(sqls); return new QueryResultSet { ShardingInfo = sqls.ShardingInfo, DataTables = dts }; }
/// <summary> /// 获取对象数据 /// </summary> /// <param name="domainModel">领域模型</param> /// <param name="dataID">数据唯一标识</param> /// <param name="shardingValue">分库分表键值对</param> /// <returns>对象数据</returns> public List <DataTable> GetData(DomainModel.Spi.DomainModel domainModel, string dataID, ShardingValue shardingValue = null) { if (domainModel == null) { throw new ArgumentNullException("DataQueryService.GetData.domainModel"); } if (string.IsNullOrWhiteSpace(dataID)) { throw new ArgumentNullException("DataQueryService.GetData.dataID"); } var sqls = SQLBuilderFactory.CreateSQLBuilder().ParseQuerySqlByID(domainModel, dataID, shardingValue); var db = DatabaseFactory.CreateDefaultDatabase(); return(db.GetDataCollection(sqls)); }
/// <summary> /// 更新领域模型数据 /// </summary> /// <param name="domainModel">领域模型</param> /// <param name="instance">对象实例</param> /// <param name="shardingValue">分区分表键值对</param> public void Update(DomainModel.Spi.DomainModel domainModel, object instance, ShardingValue shardingValue = null) { if (domainModel == null) { throw new ArgumentNullException("DataUpdateService.Update.domainModel"); } if (instance == null) { throw new ArgumentNullException("DataUpdateService.Update.instance"); } var sqls = SQLBuilderFactory.CreateSQLBuilder().ParseUpdateSql(domainModel, instance, shardingValue); var db = DatabaseFactory.CreateDefaultDatabase(); db.ExecuteSQLWithTransaction(sqls); }
/// <summary> /// 解析生成删除SQL /// </summary> /// <remarks>按主键数据作为查询依据</remarks> /// <param name="domainModel">领域模型</param> /// <param name="dataID">主键数据</param> /// <param name="shardingKeyValue">分库分表键值对</param> /// <returns>删除SQL</returns> public void DeleteByID(DomainModel.Spi.DomainModel domainModel, string dataID, ShardingValue shardingKeyValue = null) { if (domainModel == null) { throw new ArgumentNullException("DataDeleteService.DeleteByID.domainModel"); } if (string.IsNullOrWhiteSpace(dataID)) { throw new ArgumentNullException("DataDeleteService.DeleteByID.dataID"); } var sqls = SQLBuilderFactory.CreateSQLBuilder().ParseDeleteSqlByID(domainModel, dataID, shardingKeyValue); var db = DatabaseFactory.CreateDefaultDatabase(); db.ExecuteSQLWithTransaction(sqls); }