コード例 #1
0
        public static void Main(string[] args)
        {
            var parser = new FluentCommandLineParser <ApplicationArguments> {
                IsCaseSensitive = false
            };

            parser.Setup(arg => arg.ConnectionName).As('n', "connect").Required();
            parser.Setup(arg => arg.NewDataType).As('t', "type").Required();
            parser.Setup(arg => arg.Schema).As('s', "schema");
            parser.Setup(arg => arg.RegexColumn).As('r', "regex");
            parser.Setup(arg => arg.ColumnSet).As('c', "columns");
            parser.Setup(arg => arg.IgnoreObjectsFileName).As('i', "ignore").WithDescription(
                "File containing sprocs, functions, triggers to exclude");
            parser.Setup <bool>(arg => arg.LogDetails).As('l', "log").SetDefault(false).WithDescription("Log Details.");
            parser.SetupHelp("h", "help", "?").Callback(PrintUsage);

            var result = parser.Parse(args);

            if (result.HasErrors == false)
            {
                var newCharacterDataTypeName = new CharacterDataTypeName(parser.Object.NewDataType);

                if (parser.Object.RegexColumn != null && // find all columns that match a given pattern (e.g., .*customer.*id.*) in the database and create sql script to modify those columns
                    parser.Object.Schema != null)
                {
                    var      connectionStringBuilder = BuildSqlConnectionString.GetConnectionString(parser.Object.ConnectionName);
                    string[] parts = parser.Object.Schema.Split('.');

                    if (parts.Length != 2)
                    {
                        throw new ArgumentException("spec", $"schema specification '{parser.Object.Schema}' was not in two-part database.schema format");
                    }

                    var database = parts[0].Trim();
                    var schema   = parts[1].Trim();
                    var regex    = new Regex(parser.Object.RegexColumn, RegexOptions.IgnoreCase);
                    var fileName = parser.Object.IgnoreObjectsFileName;
                    List <DatabaseSchemaObjectIdentifier> ignoreObjectIds = null;
                    if (fileName != null)
                    {
                        if (!ReadObjectIds(fileName, database, schema, out ignoreObjectIds))
                        {
                            throw new ArgumentException($"Ignore File content was not in correct format: sproc:name or trigger:name or func:name");
                        }
                    }

                    // modify columns matching pattern and all other database objects that refer to them
                    var scriptGenerator = new ModifySchemaScriptGenerator(connectionStringBuilder, newCharacterDataTypeName);
                    scriptGenerator.GenerateScriptToAlterSchemaObjects(regex, database, schema, parser.Object.LogDetails, ignoreObjectIds);
                }
                else if (parser.Object.ColumnSet != null)
                {
                    var connectionStringBuilder = BuildSqlConnectionString.GetConnectionString(parser.Object.ConnectionName);
                    var columns = parser.Object.ColumnSet.Select(c => new ColumnIdentifier(c));

                    // modify user-specified columns and all other columns containing foreign key references to these columns
                    var scriptGenerator = new ModifySchemaScriptGenerator(connectionStringBuilder, newCharacterDataTypeName);
                    scriptGenerator.GenerateScriptToAlterColumns(columns);
                }
                else if (result.HelpCalled == false)
                {
                    PrintUsage();
                }
            }
            else if (result.EmptyArgs)
            {
                PrintUsage();
            }
            else
            {
                Console.WriteLine(result.ErrorText);
            }
        }
コード例 #2
0
 public ModifySchemaScriptGenerator(SqlConnectionStringBuilder connectionStringBuilder, CharacterDataTypeName newDatatype)
 {
     ConnectionStringBuilder = connectionStringBuilder;
     NewDataTypeName         = newDatatype;
 }