Exemple #1
0
        static async Task DumpTableToStream(string table, bool skipSchema, bool truncate, OutputFormatEnum outputFormat, string selectStatement, MySqlConnection connection, Stream stream)
        {
            BaseDumper dumper = outputFormat switch
            {
                OutputFormatEnum.mysql => new MySqlDumper(connection),
                OutputFormatEnum.postgres => new PostgresDumper(connection),
                OutputFormatEnum.csv => new CsvDumper(connection),
                _ => throw new ArgumentOutOfRangeException(nameof(outputFormat), outputFormat, null)
            };

            if (!skipSchema)
            {
                await dumper.WriteTableSchemaAsync(table, stream);
            }

            await dumper.WriteAutoIncrementAsync(table, stream);

            await stream.WriteToStreamAsync("SET SESSION time_zone = \"+00:00\";\n");

            await stream.WriteToStreamAsync("SET SESSION FOREIGN_KEY_CHECKS = 0;\n");

            if (truncate)
            {
                await dumper.WriteTruncateAsync(table, stream);
            }

            await using (var transaction = await connection.BeginTransactionAsync(IsolationLevel.RepeatableRead))
            {
                await dumper.WriteInsertQueries(table, selectStatement, stream, transaction);
            }
        }
Exemple #2
0
        static async Task DumpMultipleTables(string multiTableArgument, bool skipSchema, bool truncate, OutputFormatEnum outputFormat, string selectStatement, string connectionString, string outputFolder, bool stdOut)
        {
            var dateTime = DateTime.Now;

            await using var connection = new MySqlConnection(connectionString);

            await connection.OpenAsync();

            await BaseDumper.InitializeConnection(connection);

            string[] tables;

            if (multiTableArgument == "*")
            {
                tables = await GetAllTablesFromDatabase(connection);
            }
            else
            {
                tables = multiTableArgument.Split(',', StringSplitOptions.RemoveEmptyEntries);
            }

            if (!stdOut)
            {
                foreach (var table in tables)
                {
                    string dumpFileName = $"dump_{dateTime:yyyy-MM-dd_hh-mm-ss}_{table}.sql";

                    await using (var stream = new FileStream(Path.Combine(outputFolder, dumpFileName), FileMode.CreateNew))
                    {
                        string formattedQuery = selectStatement.Replace("{table}", table);

                        await DumpTableToStream(table, skipSchema, truncate, outputFormat, formattedQuery, connection, stream);
                    }
                }
            }
            else
            {
                var stream = Console.OpenStandardOutput();

                using var streamWriter = new StreamWriter(stream, Utility.NoBomUtf8, 4096, true);

                foreach (var table in tables)
                {
                    string formattedQuery = selectStatement.Replace("{table}", table);

                    await DumpTableToStream(table, skipSchema, truncate, outputFormat, formattedQuery, connection, stream);

                    await streamWriter.WriteAsync("\n\n\n");

                    await streamWriter.FlushAsync();
                }
            }
        }
Exemple #3
0
        static async Task DumpSingleTable(string table, bool skipSchema, bool truncate, OutputFormatEnum outputFormat, string selectStatement, string connectionString, string outputFile, bool standardOut, bool append)
        {
            var stream = standardOut
                                ? Console.OpenStandardOutput()
                                : new FileStream(outputFile, append ? FileMode.Append : FileMode.Create);

            string formattedQuery = selectStatement.Replace("{table}", table);

            await using (var connection = new MySqlConnection(connectionString))
            {
                await connection.OpenAsync();

                await BaseDumper.InitializeConnection(connection);

                await DumpTableToStream(table, skipSchema, truncate, outputFormat, formattedQuery, connection, stream);
            }

            await stream.FlushAsync();

            if (!standardOut)
            {
                await stream.DisposeAsync();
            }
        }