/// <summary> /// Gets the generated Sql string. /// </summary> /// <typeparam name="TEntity"></typeparam> /// <param name="this"></param> /// <returns></returns> public static string ToSql <TEntity>(this IQueryable <TEntity> @this) { if (EFVersion.AtLeast(3, 1)) { throw EFVersion.NeedNewerVersionException; } else if (EFVersion.AtLeast(3, 0)) { var enumerable = @this.Provider.Execute <IEnumerable <TEntity> >(@this.Expression); var reflector_enumerator = enumerable.GetEnumerator().GetReflector(); var factory = reflector_enumerator.DeclaredField <IQuerySqlGeneratorFactory>("_querySqlGeneratorFactory").Value; var sqlGenerator = factory.Create(); var selectExpression = reflector_enumerator.DeclaredField <SelectExpression>("_selectExpression").Value; var command = sqlGenerator.GetCommand(selectExpression); var sql = $"{command.CommandText};{Environment.NewLine}"; return(sql); } else { throw EFVersion.NotSupportedException; } }
public void RandomTest() { using (var mysql = ApplicationDbContext.UseMySql()) { var query = mysql.SimpleModels.Random(2); var sql = query.ToSql(); string expectedSql; if (EFVersion.AtLeast(3, 0)) { expectedSql = @"SELECT `s`.`Id`, `s`.`Age`, `s`.`Birthday`, `s`.`Name`, `s`.`State` FROM `SimpleModels` AS `s` ORDER BY RAND() LIMIT @__p_0; "; } else if (EFVersion.AtLeast(2, 0)) { expectedSql = @"SELECT `x`.`Id`, `x`.`Age`, `x`.`Birthday`, `x`.`Name`, `x`.`State` FROM `SimpleModels` AS `x` ORDER BY RAND() LIMIT 2; "; } else { throw EFVersion.NotSupportedException; } Assert.Equal(expectedSql, sql); } }
/// <summary> /// Gets the generated Sql string. /// </summary> /// <typeparam name="TEntity"></typeparam> /// <param name="this"></param> /// <returns></returns> public static string ToSql <TEntity>(this IQueryable <TEntity> @this) { if (EFVersion.AtLeast(3, 0)) { throw EFVersion.NeedNewerVersionException; } if (EFVersion.AtLeast(2, 1)) { var queryCompiler = @this.Provider.GetReflector <EntityQueryProvider>().DeclaredField <QueryCompiler>("_queryCompiler").Value; var queryCompilerReflector = queryCompiler.GetReflector(); var queryModel = queryCompilerReflector.DeclaredField <QueryModelGenerator>("_queryModelGenerator").Value.ParseQuery(@this.Expression); var dependencies = queryCompilerReflector.DeclaredProperty <Database>("Database").DeclaredProperty <DatabaseDependencies>("Dependencies").Value; var modelVisitor = dependencies.QueryCompilationContextFactory.Create(false).CreateQueryModelVisitor() as RelationalQueryModelVisitor; modelVisitor.CreateQueryExecutor <TEntity>(queryModel); var sql = modelVisitor.Queries.Select(x => $"{x.ToString().TrimEnd(';')};{Environment.NewLine}").Join(""); return(sql); } else if (EFVersion.AtLeast(2, 0)) { var queryCompiler = @this.Provider.GetReflector <EntityQueryProvider>().DeclaredField <QueryCompiler>("_queryCompiler").Value; var queryCompilerReflector = queryCompiler.GetReflector(); var nodeTypeProvider = queryCompilerReflector.DeclaredProperty <INodeTypeProvider>("NodeTypeProvider").Value; var parser = queryCompilerReflector.Method("CreateQueryParser").Call(nodeTypeProvider) as QueryParser; var queryModel = parser.GetParsedQuery(@this.Expression); var dependencies = queryCompilerReflector.DeclaredProperty <Database>("Database").DeclaredProperty <DatabaseDependencies>("Dependencies").Value; var modelVisitor = dependencies.QueryCompilationContextFactory.Create(false).CreateQueryModelVisitor() as RelationalQueryModelVisitor; modelVisitor.CreateQueryExecutor <TEntity>(queryModel); var sql = modelVisitor.Queries.Select(x => $"{x.ToString().TrimEnd(';')};{Environment.NewLine}").Join(""); return(sql); } else { throw EFVersion.NotSupportedException; } }