MeaningOfLights_dB2GraphQL_Net/src/DatabaseSchemaViewer/TaskRunner.cs
using System;
using System.IO;
using DatabaseSchemaReader.Data;
using DatabaseSchemaReader.DataSchema;
using DatabaseSchemaReader.SqlGen;

namespace DatabaseSchemaViewer
{
    class TaskRunner
    {
        private readonly DatabaseSchema _databaseSchema;

        public TaskRunner(DatabaseSchema databaseSchema)
        {
            _databaseSchema = databaseSchema;
        }

        public bool RunTableDdl(DirectoryInfo directory, SqlType dialect)
        {
            var tg = new DdlGeneratorFactory(dialect).AllTablesGenerator(_databaseSchema);
            tg.IncludeSchema = false;
            string txt;
            try
            {
                txt = tg.Write();
            }
            catch (Exception exception)
            {
                Message =
                    @"An error occurred while creating the script.\n" + exception.Message;
                return false;
            }
            try
            {
                var path = Path.Combine(directory.FullName, "table.sql");
                File.WriteAllText(path, txt);
                Message = @"Wrote " + path;
                return true;
            }
            catch (Exception exception)
            {
                Message =
                    @"An IO error occurred while writing the file.\n" + exception.Message;
            }
            return false;
        }

        public bool RunSprocs(DirectoryInfo directory, SqlType dialect, DatabaseTable table)
        {
            if (table == null)
            {
                Message = "No table";
                return false;
            }

            var gen = new DdlGeneratorFactory(dialect).ProcedureGenerator(table);
            if (gen == null)
            {
                //there is no sproc provider (SQLite)
                Message = @"There is no sproc generator";
                return false;
            }
            var path = Path.Combine(directory.FullName, table.Name + "_sprocs.sql");
            try
            {
                gen.WriteToScript(path);
                Message = @"Wrote " + path;
                return true;
            }
            catch (Exception exception)
            {
                Message =
                    @"An error occurred while creating the script.\n" + exception.Message;
            }
            return false;
        }

        public string Message { get; private set; }

        public bool RunData(DirectoryInfo directory, SqlType dialect, DatabaseTable table)
        {
            if (table == null)
            {
                Message = "No table";
                return false;
            }
            var path = Path.Combine(directory.FullName, table.Name + "_data.sql");
            try
            {

                var rdr = new Reader(table, table.DatabaseSchema.ConnectionString, table.DatabaseSchema.Provider);
                var dt = rdr.Read();

                var insertWriter = new InsertWriter(table, dt);
                //we have special processing for sqlserver
                if (dialect == SqlType.SqlServer || dialect == SqlType.SqlServerCe 
                    //assume db2 uses "generated by default" not "generated always". 
                    || dialect == SqlType.SQLite
                    //SQLite is fine with identity inserts and MySQL autoincrement is effectively DEFAULT=MAX(col) anyway
                    || dialect == SqlType.Db2 || dialect == SqlType.MySql)
                {
                    insertWriter.IncludeIdentity = true;
                }

                //act
                string txt = insertWriter.Write(dialect);


                File.WriteAllText(path, txt);
                Message = @"Wrote " + path;
                return true;
            }
            catch (Exception exception)
            {
                Message =
                    @"An error occurred while creating the script.\n" + exception.Message;
            }
            return false;

        }
    }
}
