Beispiel #1
0
        public static async Task ScaffoldAsync(RunnerArgs args, ScaffoldCommand command)
        {
            if (command == null)
            {
                throw new RunnerException("Invalid command object.");
            }

            if (string.IsNullOrWhiteSpace(args.Connection))
            {
                throw new RunnerException("Please specify a connection string using the -c|--connection parameter.");
            }

            DatabaseModel       databaseModel;
            IList <TypeMapping> typeMappings;

            if (args.Verbose)
            {
                DotNetJerryHost.WriteHeader();
            }

            using (DbConnection connection = await GetOpenConnectionAsync(args, command))
            {
                DotNetJerryHost.WriteLine("Generating...", ConsoleColor.Yellow);

                databaseModel = await command.GetDatabaseModelAsync(connection);

                typeMappings = command.GetTypeMappings().ToList();
            }

            ScaffoldProject project = ScaffoldProject.FromModel(databaseModel, typeMappings, args);

            await ScaffoldWriter.WriteAsync(project);

            if (args.Verbose)
            {
                ScaffoldWriteVerboseOutput(project);
            }

            int objectCount = project.Files.SelectMany(f => f.Objects).Count();

            string classMoniker = objectCount + " " + (objectCount == 1 ? "class" : "classes");

            if (project.Files.Count == 1)
            {
                DotNetJerryHost.WriteLine($"Created {classMoniker} in {project.Files[0].FileName}.", ConsoleColor.Green);
            }
            else
            {
                DotNetJerryHost.WriteLine($"Created {classMoniker} in {project.Files.Count} files.", ConsoleColor.Green);
            }
        }
Beispiel #2
0
        public static async Task ScaffoldAsync(RunnerArgs info, ScaffoldCommand command)
        {
            if (command == null)
            {
                throw new RunnerException("Invalid command object.");
            }

            if (string.IsNullOrWhiteSpace(info.Connection))
            {
                throw new RunnerException("Please specify a connection string using the -c|--connection parameter.");
            }

            DatabaseModel       databaseModel;
            IList <TypeMapping> typeMappings;

            using (DbConnection connection = command.GetDbConnection())
            {
                try
                {
                    connection.ConnectionString = info.Connection;
                }
                catch (Exception ex)
                {
                    throw new RunnerException("Invalid connection string: " + ex.Message, ex);
                }

                if (!string.IsNullOrEmpty(connection.Database))
                {
                    Program.WriteLine($"Connecting to database '{connection.Database}'...", ConsoleColor.Yellow);
                }
                else
                {
                    Program.WriteLine("Connecting to database...", ConsoleColor.Yellow);
                }

                try
                {
                    connection.Open();
                }
                catch (Exception ex)
                {
                    throw new RunnerException("Unable to open connection: " + ex.Message, ex);
                }

                Program.WriteLine("Generating...", ConsoleColor.Yellow);

                databaseModel = await command.GetDatabaseModelAsync(connection);

                typeMappings = command.GetTypeMappings().ToList();
            }

            ScaffoldProject project = ScaffoldProject.FromModel(databaseModel, typeMappings, info);

            await ScaffoldWriter.WriteAsync(project);

            int tableCount  = project.Files.SelectMany(f => f.Objects).Count();
            int columnCount = project.Files.SelectMany(f => f.Objects).SelectMany(o => o.Properties).Count();

            string tablesMoniker  = tableCount + " " + (tableCount == 1 ? "table" : "tables");
            string columnsMoniker = columnCount + " " + (columnCount == 1 ? "column" : "columns");

            Console.ForegroundColor = ConsoleColor.Green;

            if (project.Files.Count == 1)
            {
                Program.WriteLine($"Generated {tablesMoniker} and {columnsMoniker} in {project.Files[0].FileName}.", ConsoleColor.Green);
            }
            else
            {
                Program.WriteLine($"Generated {tablesMoniker} and {columnsMoniker} in {project.Files.Count} files.", ConsoleColor.Green);
            }

            Console.ResetColor();
        }