Beispiel #1
0
        private static string GetAlterScript(Assembly assembly, DeployParams deployParams)
        {
            StringBuilder sb = new StringBuilder();

            sb.AppendLine(PwrSqlClrCommandBuilder.AlterAssembly_CommandText(assembly, deployParams.PermissionSet, !deployParams.Reference, deployParams.UncheckedData, deployParams.Assembly));

            return(sb.ToString());
        }
Beispiel #2
0
        private static string GetDropScript(Assembly assembly, DeployParams deployParams)
        {
            StringBuilder sb = new StringBuilder();

            if (!deployParams.Reference)
            {
                foreach (Type type in PwrSqlClrObjectsExplorer.GetTypes(assembly))
                {
                    foreach (MethodInfo miTrigger in PwrSqlClrObjectsExplorer.GetTriggers(type))
                    {
                        sb.AppendLine(PwrSqlClrCommandBuilder.DropTrigger_CommandText(miTrigger, deployParams.Schema));
                    }

                    foreach (MethodInfo miProcedure in PwrSqlClrObjectsExplorer.GetProcedures(type))
                    {
                        sb.AppendLine(PwrSqlClrCommandBuilder.DropProcedure_CommandText(miProcedure, deployParams.Schema));
                    }

                    foreach (MethodInfo miFunction in PwrSqlClrObjectsExplorer.GetFunctions(type))
                    {
                        sb.AppendLine(PwrSqlClrCommandBuilder.DropFunction_CommandText(miFunction, deployParams.Schema));
                    }
                }

                foreach (Type userDefinedAggregate in PwrSqlClrObjectsExplorer.GetUserDefinedAggregates(assembly))
                {
                    sb.AppendLine(PwrSqlClrCommandBuilder.DropAggregate_CommandText(userDefinedAggregate, deployParams.Schema));
                }

                foreach (Type userDefinedType in PwrSqlClrObjectsExplorer.GetUserDefinedTypes(assembly))
                {
                    sb.AppendLine(PwrSqlClrCommandBuilder.DropType_CommandText(userDefinedType, deployParams.Schema));
                }
            }

            sb.AppendLine(PwrSqlClrCommandBuilder.DropAssembly_CommandText(assembly, deployParams.Assembly));

            return(sb.ToString());
        }
Beispiel #3
0
        private static void Deploy(string[] args)
        {
            DeployCommand deployCommand = ParseDeployCommand(args[0]);
            string        scriptText    = string.Empty;

            string connectionString = args
                                      .Skip(1)
                                      .Take(args.Length - 2)
                                      .FirstOrDefault(t => t.ToLower().StartsWith(key_Connection))
                                      .With(t => t == null ? null : t.Substring(key_Connection.Length));

            string scriptPath = args
                                .Skip(1)
                                .Take(args.Length - (deployCommand == DeployCommand.Manage ? 1 : 2))
                                .FirstOrDefault(t => t.ToLower().StartsWith(key_Script))
                                .With(t => t == null ? null : t.Substring(key_Script.Length));

            if (deployCommand == DeployCommand.Manage)
            {
                ManageParams manageParams = new ManageParams();

                manageParams.EnableClr = args
                                         .Skip(1)
                                         .Take(args.Length - 2)
                                         .Any(t => t.ToLower().Equals(key_EnableClr));

                manageParams.TrustworthyOn = args
                                             .Skip(1)
                                             .Take(args.Length - 2)
                                             .Any(t => t.ToLower().Equals(key_Trustworthy));

                scriptText = GetManageScript(manageParams);
            }
            else
            {
                DeployParams deployParams = new DeployParams();

                deployParams.PermissionSet = args
                                             .Skip(1)
                                             .Take(args.Length - 2)
                                             .FirstOrDefault(t => t.ToLower().StartsWith(key_Permission))
                                             .With(t => t == null ? default(PermissionSet?) : ParsePermissionSet(t.Substring(key_Permission.Length)));

                deployParams.Owner = args
                                     .Skip(1)
                                     .Take(args.Length - 2)
                                     .FirstOrDefault(t => t.ToLower().StartsWith(key_Owner))
                                     .With(t => t == null ? null : t.Substring(key_Owner.Length));

                deployParams.Schema = args
                                      .Skip(1)
                                      .Take(args.Length - 2)
                                      .FirstOrDefault(t => t.ToLower().StartsWith(key_Schema))
                                      .With(t => t == null ? null : t.Substring(key_Schema.Length));

                deployParams.UncheckedData = args
                                             .Skip(1)
                                             .Take(args.Length - 2)
                                             .Any(t => t.ToLower().Equals(key_Unchecked));

                deployParams.Assembly = args
                                        .Skip(1)
                                        .Take(args.Length - 2)
                                        .FirstOrDefault(t => t.ToLower().StartsWith(key_Assembly))
                                        .With(t => t == null ? null : t.Substring(key_Assembly.Length));

                deployParams.Reference = args
                                         .Skip(1)
                                         .Take(args.Length - 2)
                                         .Any(t => t.ToLower().Equals(key_Invisible));

                deployParams.Strong = args
                                      .Skip(1)
                                      .Take(args.Length - 2)
                                      .Any(t => t.ToLower().Equals(key_Strong));

                string mapPath = args
                                 .Skip(1)
                                 .Take(args.Length - 2)
                                 .FirstOrDefault(t => t.ToLower().StartsWith(map_Script))
                                 .With(t => t == null ? null : t.Substring(map_Script.Length));

                deployParams.Map = string.IsNullOrEmpty(mapPath) ? new Dictionary <string, string>() :
                                   File.ReadAllLines(mapPath)
                                   .Select(s => s.Split(';'))
                                   .Where(t => t.Length == 2 && !string.IsNullOrWhiteSpace(t[0]) && !string.IsNullOrWhiteSpace(t[1]))
                                   .ToDictionary(t => t[0], t => t[1]);

                Assembly assembly = deployParams.Strong ? Assembly.Load(args[args.Length - 1]) : Assembly.LoadFrom(args[args.Length - 1]);

                switch (deployCommand)
                {
                case DeployCommand.Register:
                    scriptText = GetCreateScript(assembly, deployParams);
                    break;

                case DeployCommand.Unregister:
                    scriptText = GetDropScript(assembly, deployParams);
                    break;

                case DeployCommand.Modify:
                    scriptText = GetAlterScript(assembly, deployParams);
                    break;
                }
            }

            if (!string.IsNullOrWhiteSpace(scriptPath))
            {
                bool append = args
                              .Skip(1)
                              .Take(args.Length - 2)
                              .Any(t => t.ToLower().Equals(key_Append));

                string encoding = args
                                  .Skip(1)
                                  .Take(args.Length - 2)
                                  .FirstOrDefault(t => t.ToLower().StartsWith(key_Encoding))
                                  .With(t => t == null ? null : t.Substring(key_Encoding.Length));

                SaveScript(scriptPath, scriptText, encoding, append);
            }

            if (!string.IsNullOrWhiteSpace(connectionString))
            {
                ExecuteScript(connectionString, scriptText);
            }
        }