예제 #1
0
        internal static DbProject GetProjectFromArguments(CommandLineApplication cmdApp, string prefix, DbProjectCmdArguments args)
        {
            if (args.Type.Value() == "fs")
            {
                if (string.IsNullOrEmpty(args.Path.Value()))
                {
                    Console.WriteLine($"--{prefix}-fs-path must specify the input directory");
                    cmdApp.ShowHelp();
                    return(null);
                }

                return(DbProjectFileSystem.CreateFromDirectory(args.Path.Value()));
            }
            else if (args.Type.Value() == "db")
            {
                if (string.IsNullOrEmpty(args.ConnectionString.Value()))
                {
                    Console.WriteLine($"--{prefix}-db-cs must specify the MySQL connection string");
                    cmdApp.ShowHelp();
                    return(null);
                }

                var dbInfo = new MySqlArguments()
                {
                    ConnectionString = args.ConnectionString.Value(),
                    UseSshTunnel     = args.SshHost.Values.Count > 0,
                    SshHost          = args.SshHost.Value(),
                    SshPort          = uint.Parse(args.SshHost.Value()),
                    SshUserName      = args.SshUserName.Value(),
                    SshPassword      = args.SshPassword.Value(),
                };

                return(DbProjectMySql.CreateFromDatabase(dbInfo));
            }
            else if (args.Type.Value() == "null")
            {
                return(new DbProject()
                {
                    Statements = new List <Statement>(),
                });
            }
            else
            {
                Console.WriteLine($"Expected --{prefix} fs or --{prefix} db or --{prefix} null");
                cmdApp.ShowHelp();
                return(null);
            }
        }
예제 #2
0
        public static DbProject CreateFromDatabase(MySqlArguments dbInfo)
        {
            using (var ctx = new MySqlContext(dbInfo))
            {
                var conn = ctx.Connection;
                conn.Open();

                var tables     = GetTableSql(conn);
                var procedures = GetProcedureSql(conn);
                var triggers   = GetTriggerSql(conn);

                var result = new List <Statement>();

                foreach (var table in tables)
                {
                    var path = Path.Combine("tables", table.Key + ".sql");
                    result.AddRange(DbProject.ReadSqlFromString(path, table.Value));
                }

                foreach (var table in procedures)
                {
                    var path = Path.Combine("procedures", table.Key + ".sql");
                    result.AddRange(DbProject.ReadSqlFromString(path, table.Value));
                }

                foreach (var table in triggers)
                {
                    var path = Path.Combine("triggers", table.Key + ".sql");
                    result.AddRange(DbProject.ReadSqlFromString(path, table.Value));
                }

                return(new DbProject()
                {
                    Statements = result,
                });
            }
        }