public string CleanCommandText() { try { SqlPreCommand eager = EagerProjections?.Select(cp => cp.Command).Combine(Spacing.Double); SqlPreCommand lazy = LazyChildProjections?.Select(cp => cp.Command).Combine(Spacing.Double); return(SqlPreCommandConcat.Combine(Spacing.Double, eager == null ? null : new SqlPreCommandSimple("--------- Eager Client Joins ----------------"), eager, eager == null && lazy == null ? null : new SqlPreCommandSimple("--------- MAIN QUERY ------------------------"), MainCommand, lazy == null ? null : new SqlPreCommandSimple("--------- Lazy Client Joins (if needed) -----"), lazy).PlainSql()); } catch { return(MainCommand.Sql); } }
internal static SqlPreCommand SynchronizeWordTemplate(Replacements replacements, WordTemplateEntity template, StringDistance sd) { try { if (template.Template == null || !replacements.Interactive) { return(null); } var queryName = QueryLogic.ToQueryName(template.Query.Key); QueryDescription qd = DynamicQueryManager.Current.QueryDescription(queryName); Console.Clear(); SafeConsole.WriteLineColor(ConsoleColor.White, "WordTemplate: " + template.Name); Console.WriteLine(" Query: " + template.Query.Key); var file = template.Template.Retrieve(); try { SyncronizationContext sc = new SyncronizationContext { ModelType = template.SystemWordTemplate.ToType(), QueryDescription = qd, Replacements = replacements, StringDistance = sd, HasChanges = false, Variables = new ScopedDictionary <string, ValueProviderBase>(null), }; var bytes = template.ProcessOpenXmlPackage(document => { Dump(document, "0.Original.txt"); var parser = new TemplateParser(document, qd, template.SystemWordTemplate.ToType(), template); parser.ParseDocument(); Dump(document, "1.Match.txt"); parser.CreateNodes(); Dump(document, "2.BaseNode.txt"); parser.AssertClean(); foreach (var root in document.AllRootElements()) { foreach (var node in root.Descendants <BaseNode>().ToList()) { node.Synchronize(sc); } } if (sc.HasChanges) { Dump(document, "3.Synchronized.txt"); var variables = new ScopedDictionary <string, ValueProviderBase>(null); foreach (var root in document.AllRootElements()) { foreach (var node in root.Descendants <BaseNode>().ToList()) { node.RenderTemplate(variables); } } Dump(document, "4.Rendered.txt"); } }); if (!sc.HasChanges) { return(null); } file.AllowChange = true; file.BinaryFile = bytes; using (replacements.WithReplacedDatabaseName()) return(Schema.Current.Table <FileEntity>().UpdateSqlSync(file, comment: "WordTemplate: " + template.Name)); } catch (TemplateSyncException ex) { if (ex.Result == FixTokenResult.SkipEntity) { return(null); } if (ex.Result == FixTokenResult.DeleteEntity) { return(SqlPreCommandConcat.Combine(Spacing.Simple, Schema.Current.Table <WordTemplateEntity>().DeleteSqlSync(template), Schema.Current.Table <FileEntity>().DeleteSqlSync(file))); } if (ex.Result == FixTokenResult.ReGenerateEntity) { return(Regenerate(template, replacements)); } throw new InvalidOperationException("Unexcpected {0}".FormatWith(ex.Result)); } finally { Console.Clear(); } } catch (Exception e) { return(new SqlPreCommandSimple("-- Exception in {0}: \r\n{1}".FormatWith(template.BaseToString(), e.Message.Indent(2, '-')))); } }