private string ConvertToUninstallScript()

in Configurator/Core/Server/ServerConfigurationController.cs [1940:2025]


    private string ConvertToUninstallScript(string databaseToUse, params string[] scripts)
    {
      if (scripts == null
          || scripts.Length == 0)
      {
        return null;
      }

      var builder = new StringBuilder();
      if (!string.IsNullOrEmpty(databaseToUse))
      {
        builder.AppendLine($"USE {databaseToUse};");
      }

      try
      {
        foreach (var script in scripts)
        {
          using (var stringReader = new StringReader(script))
          {
            string line;
            while ((line = stringReader.ReadLine()) != null)
            {
              line = line.Replace("(", " (");
              var tokens = line.Split(' ');
              if (tokens == null
                  || tokens.Length < 3)
              {
                continue;
              }

              if (line.StartsWith("CREATE TABLE"))
              {
                var tableName = tokens[2];
                if (line.Contains("IF NOT EXISTS")
                    && tokens.Length > 5)
                {
                  tableName = tokens[5];
                }
                else
                {
                  var command = new StringBuilder(line);
                  var newLine = string.Empty;
                  while(!newLine.EndsWith(";"))
                  {
                    newLine = stringReader.ReadLine();
                    command.Append($" {newLine}");
                  }

                  line = command.ToString().Replace("(", " (");
                  tokens = line.Split(' ');
                  tableName = tokens.Length > 5
                              ? tokens[5]
                              : null;
                }

                builder.AppendLine($"DROP TABLE IF EXISTS {tableName};");
              }
              else if (line.StartsWith("CREATE FUNCTION")
                       || line.StartsWith("CREATE AGGREGATE FUNCTION"))
              {
                var functionName = tokens[2];
                if (line.StartsWith("CREATE AGGREGATE FUNCTION"))
                {
                  functionName = tokens[3];
                }

                builder.AppendLine($"DROP FUNCTION IF EXISTS {functionName};");
              }
              else if (line.StartsWith("CREATE PROCEDURE"))
              {
                var procedureName = tokens[2];
                builder.AppendLine($"DROP PROCEDURE IF EXISTS {procedureName};");
              }
            }
          }
        }

        return builder.ToString();
      }
      catch (Exception ex)
      {
        Logger.LogException(ex);
        return null;
      }
    }