public static void CompareSql(DbContext db, DbOperateBase ope, string sql) { DbOperateBase operate = (DbOperateBase)ope; var comands = new DbOperateCommandCollection(operate.Executor); comands.NextCommand(); var generatesql = ope.GenerateSql(); CompareSql(sql, generatesql); }
public static void CompareSql(DbContext db, Expression exp, string sql) { var itemtype = exp.Type; if (itemtype.IsGenericType && typeof(IEnumerable).IsAssignableFrom(itemtype)) { itemtype = itemtype.GetGenericArguments()[0]; } var _Operate = new DbQueryCollectionOperate(db, exp, itemtype); var expression = db.Configuration.Translator.Translate(_Operate); var comands = new DbOperateCommandCollection(_Operate.Executor); comands.NextCommand(); var generatesql = db.Database.Generator.Generate(_Operate, expression); CompareSql(sql, generatesql); }
//根据操作集合生成执行命令集合对象。 private DbOperateCommandCollection GenerateCommands(IEnumerable <DbOperateBase> operateCollection) { var operates = operateCollection.ToArray(); var commands = new DbOperateCommandCollection(this); var database = Context.Database; foreach (var operate in operates) { int parametercount = database.Feature.MaxParameterCountForOperate; if (operate is IDbSplitObjectsOperate itemoperate) { var current = commands.CheckParameterCount(parametercount); parametercount = itemoperate.ItemParameterCount * itemoperate.Count; if (operate is IConcurrencyCheckOperate concurrency) { if (concurrency.NeedCheck && current.ConcurrencyExpectCount == 0 && current.Any()) { //如果当前操作需要参与并发检查,但是当前命令中已包含非并发检查操作,则移动到下一个命令中。 current = commands.NextCommand(); } } if (parametercount > current.ParameterCount) { int index = 0, length = 0, count = 0, sumcount = itemoperate.Count; do { if (index > 0) { current = commands.NextCommand(); } count = current.ParameterCount / itemoperate.ItemParameterCount; length = Math.Min(count, sumcount - index); itemoperate.Split(index, length, () => current.AddOperate(itemoperate, operate.GenerateSql(), index, length)); if (operate is IConcurrencyCheckOperate concurrency2 && concurrency2.NeedCheck) { //累加并发检查期望数 current.ConcurrencyExpectCount += concurrency2.ExpectCount; } index += length; } while (index < sumcount - 1); } else { if (current.ConcurrencyExpectCount > 0) { //如果当前命令包含并发检查操作,则移动到下一个命令中。 current = commands.NextCommand(); } current.AddOperate(operate, operate.GenerateSql()); if (operate is IConcurrencyCheckOperate concurrency2 && concurrency2.NeedCheck) { current.ConcurrencyExpectCount += concurrency2.ExpectCount; } } } else { var command = commands.CheckParameterCount(parametercount); if (command.ConcurrencyExpectCount > 0) { //如果当前命令包含并发检查操作,则移动到下一个命令中。 command = commands.NextCommand(); } command.AddOperate(operate, operate.GenerateSql()); } } return(commands); }