private static void GetDatabaseSchema()
        {
            Logger.Log("Retrieving table schema");
            string tableQueryFile   = Path.Combine(TemplateDirectory, "TableSchemaQuery.txt");
            string tableSchemaQuery = File.ReadAllText(tableQueryFile);

            tableSchemaQuery = tableSchemaQuery.Replace("%_DATABASE_%", Database);
            tableSchemaQuery = tableSchemaQuery.Replace("%_SCHEMA_%", currentSchema);
            DatabaseSchema   = SQLInterface.ExecuteQueryIntoDataTable(tableSchemaQuery);
        }
        private static void BuildFunctionClass()
        {
            Logger.Log("Retrieving function schema");
            string outputFileName      = "DbFunctions.cs";
            string functionSchemaQuery = ParseTemplate("FunctionSchemaQuery.txt");

            functionSchemaQuery = functionSchemaQuery.Replace("%_DATABASE_%", Database);
            functionSchemaQuery = functionSchemaQuery.Replace("%_SCHEMA_%", currentSchema);
            var functionSchema = SQLInterface.ExecuteQueryIntoDataTable(functionSchemaQuery);

            if (functionSchema.Rows.Count == 0)
            {
                Logger.Log("No functions found");
                return;
            }

            headerLines.Clear();
            fieldLines.Clear();
            footerLines.Clear();

            string         fileText   = ParseTemplate("FunctionHeader.txt");
            int            i          = 0;
            List <DataRow> parameters = new List <DataRow>();
            DataRow        currentRow;

            while (i < functionSchema.Rows.Count)
            {
                currentRow      = functionSchema.Rows[i];
                currentFunction = currentRow["FunctionName"].ToString();
                parameters.Add(currentRow);
                i++;
                if (i < functionSchema.Rows.Count)
                {
                    currentRow = functionSchema.Rows[i];
                    while (i < functionSchema.Rows.Count &&
                           currentFunction.Equals(currentRow["FunctionName"].ToString()))
                    {
                        parameters.Add(currentRow);
                        i++;
                        if (i < functionSchema.Rows.Count)
                        {
                            currentRow = functionSchema.Rows[i];
                        }
                    }
                }
                fileText += BuildFunctionMethodDefinition(parameters);
            }
            fileText += ParseTemplate("FunctionFooter.txt");
            string outputPath = Path.Combine(OutputDirectory, outputFileName);

            Logger.Log($"Writing code to {outputPath}");
            File.WriteAllText(outputPath, fileText);
        }
Exemplo n.º 3
0
        private static void CheckDatabase()
        {
            string databaseQuery =
                $"SELECT COUNT(*) FROM sys.databases WHERE [name] = '{Database}'";

            var queryResults = SQLInterface.ExecuteQueryIntoDataTable(databaseQuery);
            int count        = (int)queryResults.Rows[0].ItemArray[0];

            if (count == 0)
            {
                string createDatabase = $"CREATE DATABASE [{Database}]";
                SQLInterface.ExecuteNonQuery(createDatabase);
            }
        }
Exemplo n.º 4
0
        private static bool TableExists(string tableName)
        {
            Logger.LogVerbose($"Checking for table {tableName} in database");
            SQLInterface.ExecuteNonQuery(UseDatabase);
            string tableQuery  = $"SELECT COUNT(*) FROM sys.tables WHERE [name] = '{tableName}'";
            var    queryResult = SQLInterface.ExecuteQueryIntoDataTable(tableQuery);
            bool   result      = (int)queryResult.Rows[0].ItemArray[0] == 1;

            if (result)
            {
                Logger.LogVerbose($"Table {tableName} found");
            }
            else
            {
                Logger.LogVerbose($"Table {tableName} not found");
            }
            return(result);
        }
Exemplo n.º 5
0
        private static void GetNewUpgradeScripts()
        {
            Logger.Log("Determining upgrade scripts to run");

            NewUpgradeScripts = Directory.GetFiles(UpgradeScriptDirectory)
                                .Where(e => CorrectFileNameRegex.IsMatch(Path.GetFileName(e)) &&
                                       UpgradeScriptFilterRegex.IsMatch(Path.GetFileName(e)))
                                .OrderBy(e => Path.GetFileName(e).Split('_')[0])
                                .Select(e => Path.GetFileName(e))
                                .ToList();
            Logger.LogVerbose($"{NewUpgradeScripts.Count} valid scripts found in source directory");

            string successfulScriptsQuery = $"SELECT DISTINCT ScriptFile FROM {UpgradeScriptRunTableName} WHERE IsSuccessful = 1";
            var    queryResults           = SQLInterface.ExecuteQueryIntoDataTable(successfulScriptsQuery);

            Logger.LogVerbose($"{queryResults.Rows.Count} successful scripts found in database records");
            foreach (DataRow row in queryResults.Rows)
            {
                string upgradeScript = row.ItemArray[0].ToString();
                Logger.LogVerbose($"Removing script {upgradeScript} from list");
                if (NewUpgradeScripts.Contains(upgradeScript))
                {
                    NewUpgradeScripts.Remove(upgradeScript);
                    Logger.LogVerbose("Script removed");
                }
                else
                {
                    Logger.LogVerbose("Script does not exist in directory");
                }
            }

            Logger.LogVerbose($"{NewUpgradeScripts.Count} new scripts found in directory");
            NewUpgradeScripts = NewUpgradeScripts
                                .OrderBy(e => int.Parse(e.Split('_')[0]))
                                .Select(e => Path.Combine(UpgradeScriptDirectory, e))
                                .ToList();
        }
Exemplo n.º 6
0
        public static void ProcessArguments(string[] args)
        {
            SetDefaultConfiguration();
            try
            {
                for (int i = 0; i < args.Length; i++)
                {
                    string arg = args[i].ToLower();
                    switch (arg)
                    {
                    case HelpSwitch:
                        PrintHelpPage();
                        Environment.Exit((int)ExitCode.OK);
                        break;

                    case ConfigFileSwitch:
                        AppConfigReader.LoadConfigurationFromFile(args[++i]);
                        ImportFromConfigFile();
                        break;

                    // Upgrade Script options
                    case UpgradeScriptSwitch:
                        RunUpgradeScripts = true;
                        break;

                    case UpgradeScriptDirectorySwitch:
                        UpgradeScriptDirectory = args[++i];
                        break;

                    case UpgradeScriptFileRegexSwitch:
                        UpgradeScriptFileRegex = args[++i];
                        break;

                    case UpgradeScriptTemplateGuidMonikerSwitch:
                        UpgradeScriptTemplateGuidMonikerPrefix = args[++i];
                        break;

                    // Entity code generation options
                    case EntityCodeGenerationSwitch:
                        RunEntityCodeGeneration = true;
                        break;

                    case TemplateDirectorySwitch:
                        TemplateDirectory = args[++i];
                        break;

                    case DatabaseSchemaListSwitch:
                        DatabaseSchemas = args[++i].Split(';');
                        break;

                    case CodeGenOutputSwitch:
                        EntityCodeOutputDirectory = args[++i];
                        break;

                    // Code build options
                    case CodeBuildSwitch:
                        RunCodeBuild = true;
                        break;

                    case ProjectNameSwitch:
                        ProjectName = args[++i];
                        break;


                    // Logging options
                    case LogFileSwitch:
                        DoLogging = true;
                        SetLogFile(args[++i]);
                        break;

                    case DebugSwitch:
                    case VerboseLoggingSwitch:
                        VerboseLogging = true;
                        break;

                    case LogMessagePrefixSwitch:
                        LogMessagePrefix = args[++i];
                        break;

                    // Database options
                    case DatabaseConnectionStringSwitch:
                        ConnectionString = args[++i];
                        break;

                    case DatabaseSwitch:
                        Database = args[++i];
                        break;
                    }
                }

                CheckDatabase();
                if (DatabaseSchemas.Contains("*"))
                {
                    string schemaQuery =
                        $"USE [{Database}] " +
                        "SELECT DISTINCT s.[name] FROM sys.tables t " +
                        "INNER JOIN sys.schemas s ON s.schema_id = t.schema_id " +
                        "WHERE s.[name] != 'dbo' " +
                        "UNION ALL SELECT 'dbo'";
                    var    queryResults = SQLInterface.ExecuteQueryIntoDataTable(schemaQuery);
                    string result       = "";
                    foreach (DataRow row in queryResults.Rows)
                    {
                        result += row.ItemArray[0].ToString() + ",";
                    }

                    DatabaseSchemas = result.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
                }
            }
            catch (Exception e)
            {
                Console.WriteLine($"ERROR: {e.Message}");
                PrintHelpPage();
                Environment.Exit((int)ExitCode.ArgumentError);
            }
        }