private static void DoReverseEngineer( CommandLineOptions options, DashingSettings dashingSettings, ReverseEngineerSettings reverseEngineerSettings, ConnectionStringSettings connectionString) { // overwrite the path with the default if necessary if (string.IsNullOrEmpty(options.Location)) { options.Location = dashingSettings.DefaultSavePath; } // if it is still empty, ... if (string.IsNullOrEmpty(options.Location) && options.ReverseEngineer) { throw new CatchyException("You must specify a location for generated files to be saved"); } // require a generated namespace if (string.IsNullOrEmpty(reverseEngineerSettings.GeneratedNamespace)) { throw new CatchyException("You must specify a GeneratedNamespace in the Project ini file"); } DatabaseSchema schema; var engineer = new Engineer(reverseEngineerSettings.ExtraPluralizationWords); var databaseReader = new DatabaseReader(connectionString.ConnectionString, connectionString.ProviderName); schema = databaseReader.ReadAll(); var maps = engineer.ReverseEngineer( schema, new DialectFactory().Create(connectionString.ToSystem()), reverseEngineerSettings.GetTablesToIgnore(), consoleAnswerProvider, true); var reverseEngineer = new ModelGenerator(); var sources = reverseEngineer.GenerateFiles(maps, schema, reverseEngineerSettings.GeneratedNamespace, consoleAnswerProvider); foreach (var source in sources) { File.WriteAllText(options.Location + "\\" + source.Key + ".cs", source.Value); } }
private static void CreateDatabaseIfNotExists(ConnectionStringSettings connectionStringSettings, DbProviderFactory factory, ISqlDialect dialect) { using (new TimedOperation("-- Checking for Existence of Database...")) { var connectionStringManipulator = new ConnectionStringManipulator(connectionStringSettings.ToSystem()); using (var connection = factory.CreateConnection()) { connection.ConnectionString = connectionStringManipulator.GetRootConnectionString().ConnectionString; connection.Open(); var databaseName = connectionStringManipulator.GetDatabaseName(); Trace("Looking for {0}", databaseName); if (!connection.Query(dialect.CheckDatabaseExists(databaseName)).Any()) { Trace("Not Found"); Trace("Creating"); connection.Execute(dialect.CreateDatabase(databaseName)); Trace("Created"); } else { Trace("Found!"); } } } }
private static string GenerateMigrationScript( ConnectionStringSettings connectionStringSettings, ReverseEngineerSettings reverseEngineerSettings, IConfiguration configuration, bool naive, out IEnumerable <string> warnings, out IEnumerable <string> errors) { // fetch the from state var dialectFactory = new DialectFactory(); var dialect = dialectFactory.Create(connectionStringSettings.ToSystem()); var factory = DbProviderFactories.GetFactory(connectionStringSettings.ProviderName); // create database if not exists CreateDatabaseIfNotExists(connectionStringSettings, factory, dialect); DatabaseSchema schema; using (new TimedOperation("-- Reading database contents...")) { var databaseReader = new DatabaseReader(connectionStringSettings.ConnectionString, connectionStringSettings.ProviderName); schema = databaseReader.ReadAll(); } IEnumerable <IMap> fromMaps; using (new TimedOperation("-- Reverse engineering...")) { Console.WriteLine(); var engineer = new Engineer(reverseEngineerSettings.ExtraPluralizationWords); fromMaps = engineer.ReverseEngineer(schema, dialect, reverseEngineerSettings.GetTablesToIgnore(), consoleAnswerProvider, false); Console.Write("-- "); } using (var connection = factory.CreateConnection()) { connection.ConnectionString = connectionStringSettings.ConnectionString; // set up migrator IMigrator migrator; if (naive) { throw new NotSupportedException("The Naive Migrator is no longer supported"); } migrator = new Migrator( dialect, new CreateTableWriter(dialect), new AlterTableWriter(dialect), new DropTableWriter(dialect), new StatisticsProvider(connection, dialect)); // run the migrator string script; using (new TimedOperation("-- Generating diff...")) { script = migrator.GenerateSqlDiff( fromMaps, configuration.Maps, consoleAnswerProvider, new ConsoleLogger(isVerbose), reverseEngineerSettings.GetIndexesToIgnore(), out warnings, out errors); // TODO: do things with warnings and errors return(script); } } }
private static string GenerateMigrationScript( ConnectionStringSettings connectionStringSettings, ReverseEngineerSettings reverseEngineerSettings, IConfiguration configuration, bool naive, out IEnumerable<string> warnings, out IEnumerable<string> errors) { // fetch the from state var dialectFactory = new DialectFactory(); var dialect = dialectFactory.Create(connectionStringSettings.ToSystem()); var factory = DbProviderFactories.GetFactory(connectionStringSettings.ProviderName); // create database if not exists CreateDatabaseIfNotExists(connectionStringSettings, factory, dialect); DatabaseSchema schema; using (new TimedOperation("-- Reading database contents...")) { var databaseReader = new DatabaseReader(connectionStringSettings.ConnectionString, connectionStringSettings.ProviderName); schema = databaseReader.ReadAll(); } IEnumerable<IMap> fromMaps; using (new TimedOperation("-- Reverse engineering...")) { Console.WriteLine(); var engineer = new Engineer(reverseEngineerSettings.ExtraPluralizationWords); fromMaps = engineer.ReverseEngineer(schema, dialect, reverseEngineerSettings.GetTablesToIgnore(), consoleAnswerProvider, false); Console.Write("-- "); } using (var connection = factory.CreateConnection()) { connection.ConnectionString = connectionStringSettings.ConnectionString; // set up migrator IMigrator migrator; if (naive) { throw new NotSupportedException("The Naive Migrator is no longer supported"); } migrator = new Migrator( dialect, new CreateTableWriter(dialect), new AlterTableWriter(dialect), new DropTableWriter(dialect), new StatisticsProvider(connection, dialect)); // run the migrator string script; using (new TimedOperation("-- Generating diff...")) { script = migrator.GenerateSqlDiff( fromMaps, configuration.Maps, consoleAnswerProvider, new ConsoleLogger(isVerbose), reverseEngineerSettings.GetIndexesToIgnore(), out warnings, out errors); // TODO: do things with warnings and errors return script; } } }