Ejemplo n.º 1
0
        public static async Task ExecuteOptions(CommandOptions options, Database input_database)
        {
            DdlGenerator    generator        = null;
            TypeTransformer type_transformer = new SqliteTypeTransformer();
            bool            rooted_path      = Path.IsPathRooted(options.SqlOutput);

            if (options.InputType == CommandOptions.InType.Ddl)
            {
                if (input_database == null)
                {
                    try {
                        using (FileStream stream = new FileStream(options.Input, FileMode.Open)) {
                            var serializer = new XmlSerializer(typeof(Database));
                            input_database = (Database)serializer.Deserialize(stream);
                        }
                    } catch (Exception e) {
                        writeLineColor("Could not open input DDL file at '" + options.Input + "'.", ConsoleColor.Red);
                        writeLineColor(e.ToString(), ConsoleColor.Red);
                        return;
                    }
                }
                else
                {
                    Console.WriteLine("Using database passed.");
                }
            }
            else if (options.InputType == CommandOptions.InType.DatabaseSqlite)
            {
                generator = new SqliteDdlGenerator(@"Data Source=" + options.Input + ";Version=3;");

                input_database = await generator.GenerateDdl();
            }
            else if (options.InputType == CommandOptions.InType.Mwb || options.InputType == CommandOptions.InType.MwbXml)
            {
                if (File.Exists(options.Input) == false)
                {
                    throw new OptionException("MWB file '" + options.Input + "' specified does not exist.", "input");
                }


                generator = new MySqlMwbDdlGenerator(options.Input, (options.InputType == CommandOptions.InType.MwbXml));

                input_database = await generator.GenerateDdl();
            }

            // Ensure that the base database is initialized.
            input_database.Initialize();

            // Overrides for database variables.
            if (string.IsNullOrWhiteSpace(options.Namespace) == false)
            {
                input_database.Namespace = options.Namespace;
            }

            if (string.IsNullOrWhiteSpace(options.ContextClass) == false)
            {
                input_database.ContextClass = options.ContextClass;
            }

            // Output SQL file if required.
            if (options.SqlOutput != null)
            {
                var sql_code_writer = new SqlTableCreateGenerator(input_database, type_transformer);

                if (options.SqlOutput == "")
                {
                    options.SqlOutput = Path.GetFileNameWithoutExtension(input_database.Name);
                }

                if (Path.HasExtension(options.SqlOutput) == false)
                {
                    options.SqlOutput = Path.ChangeExtension(options.SqlOutput, ".sql");
                }
                using (var fs = new FileStream(options.SqlOutput, FileMode.Create)) {
                    using (var sw = new StreamWriter(fs)) {
                        sw.Write(sql_code_writer.Generate());
                        sw.Flush();
                    }
                }
            }

            // Output code if required.
            if (options.CodeOutput != null)
            {
                if (options.CodeOutput == "")
                {
                    options.CodeOutput = Path.ChangeExtension(Path.GetFileNameWithoutExtension(input_database.Name), ".cs");
                }

                if (Path.HasExtension(options.CodeOutput) == false)
                {
                    options.CodeOutput = Path.ChangeExtension(options.CodeOutput, ".cs");
                }

                // Output code file if required.
                using (var fs = new FileStream(options.CodeOutput, FileMode.Create)) {
                    using (var sw = new StreamWriter(fs)) {
                        sw.Write(new CSharpCodeGenerator(input_database).TransformText());
                        sw.Flush();
                    }
                }
            }

            // Output Ddl if required.
            if (options.DdlOutput != null)
            {
                if (options.DdlOutput == "")
                {
                    options.DdlOutput = Path.GetFileNameWithoutExtension(input_database.Name);
                }

                if (Path.HasExtension(options.DdlOutput) == false)
                {
                    options.DdlOutput = Path.ChangeExtension(options.DdlOutput, ".ddl");
                }

                // Output code file if required.
                using (var fs = new FileStream(options.DdlOutput, FileMode.Create)) {
                    var serializer = new XmlSerializer(typeof(Database));
                    serializer.Serialize(fs, input_database);
                }
            }
        }