<commit_before>﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Signum.Utilities;
using Signum.Engine.SchemaInfoTables;
using System.Linq.Expressions;
using Signum.Entities;

namespace Signum.Engine.Maps
{
    public class SchemaAssets
    {
        internal SqlPreCommand Schema_Generating()
        {
            SqlPreCommand views = GenerateViews();
            SqlPreCommand procedures = GenerateProcedures();

            return SqlPreCommand.Combine(Spacing.Triple, views, procedures);
        }

        internal SqlPreCommand Schema_Synchronizing(Replacements replacements)
        {
            SqlPreCommand views = SyncViews(replacements);
            SqlPreCommand procedures = SyncProcedures(replacements);

            return SqlPreCommand.Combine(Spacing.Triple, views, procedures);
        }

        static string Clean(string command)
        {
            return command.Trim(' ', '\r', '\n', ';');
        }

        #region Views
        public class View
        {
            public ObjectName Name;
            public string Definition;

            public SqlPreCommandSimple CreateView()
            {
                return new SqlPreCommandSimple("CREATE VIEW {0} AS ".FormatWith(Name) + Definition) { GoBefore = true };
            }

            public SqlPreCommandSimple AlterView()
            {
                return new SqlPreCommandSimple("ALTER VIEW {0} AS ".FormatWith(Name) + Definition) { GoBefore = true };
            } 
        }

        public View IncludeView(string viewName, string viewDefinition)
        {
            return IncludeView(new ObjectName(SchemaName.Default, viewName), viewDefinition);
        }

        public Dictionary<ObjectName, View> Views = new Dictionary<ObjectName, View>();
        public View IncludeView(ObjectName viewName, string viewDefinition)
        {
            return Views[viewName] = new View
            {
                Name = viewName,
                Definition = viewDefinition
            };
        }

        SqlPreCommand GenerateViews()
        {
            return Views.Values.Select(v => v.CreateView()).Combine(Spacing.Double);
        }

        SqlPreCommand SyncViews(Replacements replacements)
        {
            var oldView = Schema.Current.DatabaseNames().SelectMany(db =>
            {
                using (Administrator.OverrideDatabaseInSysViews(db))
                {<commit_msg>remove automatic 'AS' in  SchemaAssets.IncludeView to support things like WITH SCHEMABINDING<commit_after>﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Signum.Utilities;
using Signum.Engine.SchemaInfoTables;
using System.Linq.Expressions;
using Signum.Entities;

namespace Signum.Engine.Maps
{
    public class SchemaAssets
    {
        internal SqlPreCommand Schema_Generating()
        {
            SqlPreCommand views = GenerateViews();
            SqlPreCommand procedures = GenerateProcedures();

            return SqlPreCommand.Combine(Spacing.Triple, views, procedures);
        }

        internal SqlPreCommand Schema_Synchronizing(Replacements replacements)
        {
            SqlPreCommand views = SyncViews(replacements);
            SqlPreCommand procedures = SyncProcedures(replacements);

            return SqlPreCommand.Combine(Spacing.Triple, views, procedures);
        }

        static string Clean(string command)
        {
            return command.Trim(' ', '\r', '\n', ';');
        }

        #region Views
        public class View
        {
            public ObjectName Name;
            public string Definition;

            public SqlPreCommandSimple CreateView()
            {
                return new SqlPreCommandSimple("CREATE VIEW {0} ".FormatWith(Name) + Definition) { GoBefore = true };
            }

            public SqlPreCommandSimple AlterView()
            {
                return new SqlPreCommandSimple("ALTER VIEW {0} ".FormatWith(Name) + Definition) { GoBefore = true };
            } 
        }

        public View IncludeView(string viewName, string viewDefinition)
        {
            return IncludeView(new ObjectName(SchemaName.Default, viewName), viewDefinition);
        }

        public Dictionary<ObjectName, View> Views = new Dictionary<ObjectName, View>();
        public View IncludeView(ObjectName viewName, string viewDefinition)
        {
            return Views[viewName] = new View
            {
                Name = viewName,
                Definition = viewDefinition
            };
        }

        SqlPreCommand GenerateViews()
        {
            return Views.Values.Select(v => v.CreateView()).Combine(Spacing.Double);
        }

        SqlPreCommand SyncViews(Replacements replacements)
        {
            var oldView = Schema.Current.DatabaseNames().SelectMany(db =>
            {
                using (Administrator.OverrideDatabaseInSysViews(db))
                {
{'fraction_non_alphanumeric': 0.08166969147005444, 'fraction_numerical': 0.0008066142367412785, 'mean_word_length': 2.098063710181137, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '231086', 'n_tokens_mistral': 1258, 'n_tokens_neox': 1203, 'n_words': 284}