Beispiel #1
0
        private static void CreateDatabaseIfRequired(
            string dbName,
            ConnectionStringProvider connectionStringProvider)
        {
            dbName         = dbName.Replace("'", "''");
            using var conn = new MySqlConnection(connectionStringProvider.MasterConnectionString);
            conn.Open();
            using var cmd    = conn.CreateCommand();
            cmd.CommandText  = $"select * from INFORMATION_SCHEMA.SCHEMATA where SCHEMA_NAME = '{dbName}';";
            using var reader = cmd.ExecuteReader();
            if (reader.Read())
            {
                return;
            }

            reader.Close();
            cmd.CommandText = $"create database `{dbName}`;";
            cmd.ExecuteNonQuery();
        }
Beispiel #2
0
        static int Main(string[] args)
        {
            var opts = new Options(args);

            if (opts.ShowedHelp)
            {
                return(0);
            }

            if (!opts.IsValid)
            {
                return(2);
            }

            var connectionStringProvider = new ConnectionStringProvider(opts);

            CreateDatabaseIfRequired(opts.Database, connectionStringProvider);
            RunAllScriptFiles(opts, connectionStringProvider);
            return(0);
        }
Beispiel #3
0
        static void Main(string[] args)
        {
            var opts = new Options(args);

            if (opts.ShowedHelp)
            {
                return;
            }

            var connectionStringProvider = new ConnectionStringProvider(opts);

            opts.Files.ForEach(file =>
            {
                using (var reader = new StatementReader(file))
                {
                    string statement;
                    while ((statement = reader.Next()) != null)
                    {
                        using (var conn = ConnectionFactory.Open(connectionStringProvider.ConnectionString))
                            using (var cmd = conn.CreateCommand())
                            {
                                cmd.CommandText = $"{DISABLE_CONSTRAINTS}{Environment.NewLine}{statement}";
                                LogStatement(opts, statement);
                                try
                                {
                                    cmd.ExecuteNonQuery();
                                }
                                catch (MySqlException ex)
                                {
                                    if (opts.StopOnError)
                                    {
                                        throw;
                                    }

                                    Console.WriteLine($"[FAIL] {ex.Message}");
                                }
                            }
                    }
                }
            });
        }
Beispiel #4
0
        private static void RunAllScriptFiles(Options opts, ConnectionStringProvider connectionStringProvider)
        {
            opts.Files.ForEach((file, idx) =>
            {
                var info         = new FileInfo(file);
                var readBytes    = 0L;
                using var reader = new StatementReader(file);
                string statement;
                while ((statement = reader.Next()) != null)
                {
                    readBytes     += reader.LastReadBytes;
                    using var conn = ConnectionFactory.Open(connectionStringProvider.ConnectionString);

                    using var cmd   = conn.CreateCommand();
                    cmd.CommandText = $"{DISABLE_CONSTRAINTS}{Environment.NewLine}{statement}";
                    LogStatement(opts.Verbose, opts.NoProgress, statement, readBytes, info.Length, idx, opts.Files.Count);
                    try
                    {
                        cmd.ExecuteNonQuery();
                    }
                    catch (MySqlException ex)
                    {
                        if (opts.StopOnError)
                        {
                            throw;
                        }

                        if (!opts.Verbose)
                        {
                            ClearProgress();
                            LogStatement(true, true, statement, readBytes, info.Length, idx, opts.Files.Count);
                        }

                        Console.WriteLine($"[FAIL] {ex.Message}");
                    }
                }
            });
            ClearProgress();
        }