Example #1
0
 public override string GetObjectName()
 {
     if (_functiondef.Name.IsEmpty())
     {
         return(QueryGeneratorHelper.GetSafeSqlName(_info.Name));
     }
     return(QueryGeneratorHelper.GetSafeSqlName(_functiondef.Name));
 }
Example #2
0
 private string GetSqlReturnType()
 {
     if (_functiondef.TableDefinition.IsEmpty())
     {
         return(QueryGeneratorHelper.GetSqlType(_info.ReturnType, _schema));
     }
     return(" TABLE ( \r\n" + _functiondef.TableDefinition + "\r\n)");
 }
        private static IEnumerable <string> GetCreateCustomLibrariesScripts(SqlInstallerConsoleProgramArgs args,
                                                                            List <AssemblyName> customdepends,
                                                                            Assembly assembly)
        {
            var dir = Path.GetDirectoryName(Path.GetFullPath(args.AssemblyName + ".dll"));

            foreach (var dep in customdepends)
            {
                var source = QueryGeneratorHelper.GetAssemblyBits(Path.Combine(dir, dep.Name + ".dll"));
                if (args.UseAssemblyPath)
                {
                    source = "'" + Path.Combine(dir, dep.Name + ".dll") + "'";
                }
                yield return
                    (string.Format(
                         @"
--SQLINSTALL: Create assembly {0}
if not exists(select assembly_id from sys.assemblies where name='{0}') 
CREATE ASSEMBLY [{0}]
AUTHORIZATION dbo
FROM {1}
WITH PERMISSION_SET = UNSAFE
else begin
	begin try 
		ALTER ASSEMBLY [{0}]
		FROM {1}
		WITH PERMISSION_SET = UNSAFE
	end try
	begin catch
		declare @message nvarchar(max) set @message = ERROR_MESSAGE()
		if @message not like '%MVID%' begin
			declare @severity int set @severity = ERROR_SEVERITY()
			declare @state int set @state = ERROR_STATE()
			raiserror(@message,@severity,@state)
		end
	end catch	
end
",
                         dep.Name, source));
            }
            var source2 = QueryGeneratorHelper.GetAssemblyBits(Path.Combine(dir, assembly.GetName().Name + ".dll"));

            if (args.UseAssemblyPath)
            {
                source2 = "'" + Path.Combine(dir, assembly.GetName().Name + ".dll") + "'";
            }
            yield return
                (string.Format(
                     @"
--SQLINSTALL: Create assembly {0}
CREATE ASSEMBLY [{0}]
AUTHORIZATION dbo
FROM {1}
WITH PERMISSION_SET = {2}
",
                     assembly.GetName().Name, source2, args.Safe ? "SAFE" : "UNSAFE")
                );
        }
Example #4
0
        public override string GetCreateScript()
        {
            var          assemblyname = "[" + _type.Assembly.GetName().Name + "]";
            var          name         = _schema + "." + GetObjectName();
            var          args         = GetArguments(_type.GetMethod("Accumulate"));
            var          rettype      = QueryGeneratorHelper.GetSqlType(_type.GetMethod("Terminate").ReturnType, _schema);
            var          classname    = GetClassFullName();
            const string pattern      = @"
--SQLINSTALL: CREATE AGGREGATE {0} ({3}.{4})
CREATE AGGREGATE {0} ({1})
RETURNS {2}
EXTERNAL NAME {3}.{4}";

            return(string.Format(pattern, name, args, rettype, assemblyname, classname));
        }
        /// <summary>
        /// </summary>
        /// <returns> </returns>
        protected string GetArguments(MethodInfo method = null)
        {
            var args = new List <string>();

            foreach (var parameterInfo in (method ?? _info).GetParameters())
            {
                var type = QueryGeneratorHelper.GetSqlType(parameterInfo.ParameterType, _schema);
                var def  = " = null";
                if (type.Contains("max") || type == "xml" || type == "geography" || type == "geometry")
                {
                    def = "";
                }
                args.Add(string.Format("@{0} {1}{2}", parameterInfo.Name,
                                       type, def));
            }
            return(string.Join(",", args.ToArray()));
        }
        /// <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());
                }
            }
        }