public string GetStarterClassCode() { StringBuilder sb = new StringBuilder(); var fieldNames = this.Expressions .GroupBy(a => a.Name) .SelectMany(gr => gr.Count() == 1 ? new[] { KVP.Create(gr.SingleEx().Name + "Expression", gr.SingleEx()) } : gr.Select(a => KVP.Create(a.Name + "_" + a.FromType.RemoveChars('<', '>', '.') + "Expression", a)) ).ToDictionaryEx("DynamicExpressions"); var namesToTranslate = this.Expressions.Where(a => a.Translation == DynamicExpressionTranslation.TranslateExpressionName).Select(a => a.Name).Distinct(); if (namesToTranslate.Any()) { sb.AppendLine($"public enum CodeGenExpressionMessage"); sb.AppendLine("{"); foreach (var item in namesToTranslate) { sb.AppendLine(" " + item + ","); } sb.AppendLine("}"); sb.AppendLine(); } sb.AppendLine($"public static class CodeGenExpressionStarter"); sb.AppendLine("{"); foreach (var kvp in fieldNames) { sb.AppendLine($" static Expression<Func<{kvp.Value.FromType}, {kvp.Value.ReturnType}>> {kvp.Key} ="); sb.AppendLine($" e => {kvp.Value.Body};"); sb.AppendLine($" [ExpressionField(\"{kvp.Key}\")]"); sb.AppendLine($" public static {kvp.Value.ReturnType} {kvp.Value.Name}(this {kvp.Value.FromType} e)"); sb.AppendLine($" {{"); sb.AppendLine($" return {kvp.Key}.Evaluate(e);"); sb.AppendLine($" }}"); sb.AppendLine(""); } sb.AppendLine(" public static void Start(SchemaBuilder sb, DynamicQueryManager dqm)"); sb.AppendLine(" {"); foreach (var kvp in fieldNames) { sb.AppendLine($" var ei = dqm.RegisterExpression(({kvp.Value.FromType} e) => e.{kvp.Value.Name}(){GetNiceNameCode(kvp.Value)});"); if (kvp.Value.Format.HasText()) { sb.AppendLine($" ei.ForceFormat = {CSharpRenderer.Value(kvp.Value.Format, typeof(string), new string[0])};"); } if (kvp.Value.Unit.HasText()) { sb.AppendLine($" ei.ForceUnit = {CSharpRenderer.Value(kvp.Value.Unit, typeof(string), new string[0])};"); } } sb.AppendLine(" }"); sb.AppendLine("}"); return(sb.ToString()); }
string Value(object obj) { if (obj is decimal) { obj = (double)(decimal)obj; } return(CSharpRenderer.Value(obj)); }
string Value(object obj) { if (obj is decimal) { obj = (double)(decimal)obj; } return(CSharpRenderer.Value(obj, obj.GetType(), null)); }
protected static void Log(SqlPreCommandSimple pcs) { var log = logger.Value; if (log != null) { log.WriteLine(pcs.Sql); if (pcs.Parameters != null) { log.WriteLine(pcs.Parameters .ToString(p => "{0} {1}: {2}".FormatWith( p.ParameterName, Connector.Current.GetSqlDbType(p), p.Value?.Let(v => CSharpRenderer.Value(v, v.GetType(), null))), "\r\n")); } log.WriteLine(); } }
string Literal(object obj) { return(CSharpRenderer.Value(obj, obj.GetType(), null)); }
public string GetStarterClassCode() { StringBuilder sb = new StringBuilder(); var fieldNames = this.Expressions .GroupBy(a => a.Name) .SelectMany(gr => gr.Count() == 1 ? new[] { KeyValuePair.Create(gr.SingleEx().Name + "Expression", gr.SingleEx()) } : gr.Select(a => KeyValuePair.Create(a.Name + "_" + a.FromType.RemoveChars('<', '>', '.') + "Expression", a)) ).ToDictionaryEx("DynamicExpressions"); var namesToTranslate = this.Expressions.Where(a => a.Translation == DynamicExpressionTranslation.TranslateExpressionName).Select(a => a.Name).Distinct(); if (namesToTranslate.Any()) { sb.AppendLine($"public enum CodeGenExpressionMessage"); sb.AppendLine("{"); foreach (var item in namesToTranslate) { sb.AppendLine(" " + item + ","); } sb.AppendLine("}"); sb.AppendLine(); } sb.AppendLine($"public static class CodeGenExpressionStarter"); sb.AppendLine("{"); foreach (var kvp in fieldNames) { var expressionName = $"{kvp.Value.Name}From{kvp.Value.FromType.BeforeLast("Entity")}Expression".FirstUpper(); sb.AppendLine($" static Expression<Func<{kvp.Value.FromType}, {kvp.Value.ReturnType}>> {expressionName} = "); sb.AppendLine($" e => {kvp.Value.Body};"); sb.AppendLine($" [ExpressionField(\"{expressionName}\")]"); sb.AppendLine($" public static {kvp.Value.ReturnType} {kvp.Value.Name}(this {kvp.Value.FromType} e)"); sb.AppendLine($" {{"); sb.AppendLine($" return {expressionName}.Evaluate(e);"); sb.AppendLine($" }}"); sb.AppendLine(""); } sb.AppendLine(" public static void Start(SchemaBuilder sb)"); sb.AppendLine(" {"); foreach (var kvp in fieldNames) { var entity = kvp.Value; var varName = $"{entity.Name}{entity.FromType.BeforeLast("Entity")}".FirstLower(); sb.AppendLine($" var {varName} = QueryLogic.Expressions.Register(({entity.FromType} e) => e.{entity.Name}(){GetNiceNameCode(entity)});"); if (entity.Format.HasText()) { sb.AppendLine($" {varName}.ForceFormat = {CSharpRenderer.Value(entity.Format)};"); } if (entity.Unit.HasText()) { sb.AppendLine($" {varName}.ForceUnit = {CSharpRenderer.Value(entity.Unit)};"); } } sb.AppendLine(" }"); sb.AppendLine("}"); return(sb.ToString()); }