コード例 #1
0
 public override string GetObjectName()
 {
     if (_functiondef.Name.IsEmpty())
     {
         return(QueryGeneratorHelper.GetSafeSqlName(_info.Name));
     }
     return(QueryGeneratorHelper.GetSafeSqlName(_functiondef.Name));
 }
コード例 #2
0
        /// <summary>
        ///     Generates set of commands to execute on SqlServer
        /// </summary>
        /// <param name="args"> </param>
        /// <returns> </returns>
        public IEnumerable <string> GenerateScripts(SqlInstallerConsoleProgramArgs args)
        {
            _args = args;
            var assembly   = args.LoadAssembly();
            var sysdepends =
                (assembly.GetReferencedAssemblies().Where(
                     x =>
                     (x.Name.StartsWith("System.") || x.Name.StartsWith("Microsoft.")) &&
                     x.Name != "Microsoft.SqlServer.Types" && x.Name != "System.Data" && x.Name != "System.Xml" &&
                     x.Name != "System" && x.Name != "System.Web")).
                ToList();

            var customdepends =
                assembly.GetReferencedAssemblies().Where(
                    x => !(x.Name.StartsWith("System") || x.Name == "mscorlib" || x.Name.StartsWith("Microsoft.")))
                .ToList();

            foreach (var assemblyName in customdepends.ToArray())
            {
                var a = Assembly.Load(assemblyName);
                foreach (
                    var ccd in
                    a.GetReferencedAssemblies().Where(
                        x =>
                        !(x.Name.StartsWith("System") || x.Name == "mscorlib" || x.Name.StartsWith("Microsoft.")))
                    )
                {
                    if (!customdepends.Any(x => x.Name == ccd.Name))
                    {
                        customdepends.Add(ccd);
                    }
                }
                var subsysdepends =
                    (a.GetReferencedAssemblies().Where(
                         x =>
                         (x.Name.StartsWith("System.") || x.Name.StartsWith("Microsoft.")) &&
                         x.Name != "Microsoft.SqlServer.Types" && x.Name != "System.Data" && x.Name != "System.Xml" &&
                         x.Name != "System" && x.Name != "System.Web")).
                    ToList();
                foreach (var subsysdepend in subsysdepends)
                {
                    if (!sysdepends.Contains(subsysdepend))
                    {
                        sysdepends.Add(subsysdepend);
                    }
                }
            }
            foreach (var assemblyName in customdepends)
            {
                var a = Assembly.Load(assemblyName);
                foreach (
                    var csd in
                    a.GetReferencedAssemblies().Where(
                        x =>
                        x.Name.StartsWith("System") && x.Name != "System.Data" && x.Name != "System.Xml" &&
                        x.Name != "System"))
                {
                    if (!sysdepends.Any(x => x.Name == csd.Name))
                    {
                        sysdepends.Add(csd);
                    }
                }
            }
            if (sysdepends.Any(_ => _.Name == "Microsoft.CSharp"))
            {
                sysdepends.Insert(0, new AssemblyName("system.dynamic"));
            }
            foreach (var customdepend in customdepends.ToArray())
            {
                if (customdepend.Name.Contains("WindowsBase"))
                {
                    sysdepends.Add(customdepend);
                    customdepends.Remove(customdepend);
                }
            }
            sysdepends = sysdepends.GroupBy(_ => _.Name).Select(_ => _.First()).ToList();
            customdepends.Sort((a, b) => {
                var aa = Assembly.Load(a);
                var ba = Assembly.Load(b);
                if (aa.GetReferencedAssemblies().Any(x => x.Name == b.Name))
                {
                    return(1);
                }
                if (ba.GetReferencedAssemblies().Any(x => x.Name == a.Name))
                {
                    return(-1);
                }
                return(0);
            });
            var items  = GetExportedItems(assembly, args.Schema);
            var dbname = QueryGeneratorHelper.GetSafeSqlName(args.Database);

            //1 Prepare database
            if (!args.NoScriptDatabase)
            {
                foreach (var p in GetDatabasePrepareScripts(args, dbname))
                {
                    yield return(p);
                }
            }
            yield return(string.Format(
                             @"--SQLINSTALL: CHECK SCHEMA
if SCHEMA_ID('{0}') is null {1}  
", args.Schema,
                             QueryGeneratorHelper.GenerateExecuteSql("create schema {0}", args.Schema)));

            //2 drop existed items
            foreach (var item in items.Where(x => !(x is UserTypeWrapper)))
            {
                yield return(item.GetDropScript());
            }
            foreach (var item in items.Where(x => (x is UserTypeWrapper)))
            {
                yield return(item.GetDropScript());
            }
            //3 Prepare system libs and drop referenced libs
            foreach (var p in GetCleanupAssembliesScripts(sysdepends, customdepends, assembly))
            {
                yield return(p);
            }
            //4 generate assemblies
            if (!_args.CleanOnly)
            {
                foreach (var p in GetCreateCustomLibrariesScripts(args, customdepends, assembly))
                {
                    yield return(p);
                }
                foreach (var item in items.Where(x => (x is UserTypeWrapper)))
                {
                    yield return(item.GetCreateScript());
                }
                foreach (var item in items.Where(x => !(x is UserTypeWrapper)))
                {
                    yield return(item.GetCreateScript());
                }
            }
        }