Exemplo n.º 1
0
        static void Main(string[] args)
        {
            start = DateTime.Now;

            Console.WriteLine("SQLMake, Copyright Mitja Golouh 2008-2012, [email protected]");
            Console.WriteLine("Revision 10");

            if (args.Length == 0)
            {
                Help();
                Environment.Exit(0);
            }

            Log.Verbose("program", "Log started -------------------------------------------------------");

            int argIndex = 0;
            bool somethingFound = true;

            // First read swithces
            do
            {
                somethingFound = true;
                switch (peekNextArg(argIndex, args, "Optional parameters").ToUpper())
                {
                    case "/D":
                        argIndex++;
                        Settings.setDebugFlag(true);
                        break;
                    case "/R":
                        argIndex++;
                        Settings.setRecurseFlag(true);
                        break;
                    case "/DB":
                        argIndex++;
                        Settings.setTargetIsDbFlag(true);
                        Settings.setTargetIsFilesystemFlag(false);
                        break;
                    case "/FS":
                        argIndex++;
                        Settings.setTargetIsDbFlag(false);
                        Settings.setTargetIsFilesystemFlag(true);
                        break;
                    default:
                        somethingFound = false;
                        break;
                }
            } while (somethingFound && (argIndex <= args.Length - 1));

            if (Settings.getTargetIsDbFlag(false) && Settings.getTargetIsFilesystemFlag(false))
            {
                throw new ArgumentException("Switches /DB and /FS can not be used at same time");
            }

            OracleSql db;

            // Then read action
            string actionName = getNextArg(ref argIndex, args, "Action");

            //And finally read key/value pairs
            do
            {
                string param = peekNextArg(argIndex, args, "Key=Value");
                if (param != "")
                {
                    somethingFound = true;
                    string keyName;
                    string keyValue;
                    keyName = ""; keyValue = "";
                    int eqSignPosition = param.IndexOf('=');
                    if (eqSignPosition > 0)
                    {
                        keyName = param.Substring(0, eqSignPosition).ToUpper();
                        keyValue = param.Substring(eqSignPosition + 1);

                        switch (keyName.ToUpper())
                        {
                            case "SPOOL":
                                argIndex++;
                                Settings.setSpoolOutput(keyValue);
                                break;
                            case "OUTSCRIPT":
                                argIndex++;
                                Settings.setSpoolOutput(keyValue);
                                break;
                            case "VAR":
                                argIndex++;
                                string varParameter = getNextArg(ref argIndex, args, "SQL*Plus variable name=value");
                                string varName = varParameter.Substring(0, varParameter.IndexOf('='));
                                string varValue = varParameter.Substring(varParameter.IndexOf('=') + 1);
                                Settings.addSqlplusVariable(varName, varValue);
                                // Console.WriteLine("{0}={1}", varName, varValue);
                                break;
                            case "CONFIG":
                                argIndex++;
                                Settings.setConfig(keyValue);
                                if (!File.Exists(Settings.getConfig(true)))
                                {
                                    Log.Error("program", "Config file does not exist: {0}", Settings.getConfig(true));
                                    Program.Exit(1, "SQLMake is unable to start");
                                }
                                break;
                            case "USERID":
                                argIndex++;
                                Settings.setUserId(OracleSql.convertConnectionString2NetSyntax(keyValue));
                                break;
                            case "SANDBOXPATTERN":
                                argIndex++;
                                Settings.setSandboxPattern(keyValue);
                                break;
                            case "SCRIPTS": // this is old alias for srcscriptsdir
                                Console.WriteLine("This is deprecated parameter. Use srcscriptsdir instead.");
                                argIndex++;
                                Settings.setSourceScriptsDir(keyValue);
                                if (!Directory.Exists(Settings.getSourceScriptsDir(true)))
                                {
                                    Log.Error("program", "Source scripts directory does not exist: {0}", Settings.getSourceScriptsDir(true));
                                    Program.Exit(1, "SQLMake is unable to start");
                                }
                                break;
                            case "SRCSCRIPTSDIR":
                                argIndex++;
                                Settings.setSourceScriptsDir(keyValue);
                                if (!Directory.Exists(Settings.getSourceScriptsDir(true)))
                                {
                                    Log.Error("program", "Source scripts directory does not exist: {0}", Settings.getSourceScriptsDir(true));
                                    Program.Exit(1, "SQLMake is unable to start");
                                }
                                break;
                            case "SCRIPT":
                                argIndex++;
                                Settings.setScript(keyValue);
                                if (!File.Exists(Settings.getScript(true)))
                                {
                                    Log.Error("program", "SQL script does not exist: {0}", Settings.getScript(true));
                                    Program.Exit(1, "SQLMake is unable to start");
                                }
                                break;
                            case "UPGRADETO":
                                argIndex++;
                                Settings.setUpgradeTo(keyValue);
                                break;
                            default:
                                Log.Error("program", "Unexpected input parameter {0}", param);
                                Program.Exit(1, "SQLMake is unable to start");
                                break;
                        }
                    }
                    else break;

                }
            } while (somethingFound && (argIndex <= args.Length - 1));

            Settings.loadSettings();

            switch (actionName.ToUpper())
            {
                case "-CHECKCONNECTION":
                    Console.WriteLine("Action: Check Oracle DB connection");
                    Console.WriteLine();

                    CheckConnection.Check(Settings.getUserId(true));

                    break;

                case "-PLSQL":
                    if (!Settings.getTargetIsDbFlag(false) && !Settings.getTargetIsFilesystemFlag(false))
                    {
                        Console.WriteLine("Action: List PLSQL differences between database schema and scripts");
                        Console.WriteLine();
                        PlsqlMake.Run(Settings.getUserId(true), Settings.getSourceScriptsDir(true), Settings.getDebugFlag(true), false, false, "");
                    }

                    if (Settings.getTargetIsDbFlag(false) && !Settings.getTargetIsFilesystemFlag(false))
                    {
                        Console.WriteLine("Action: Sync PL/SQL differences to database");
                        Console.WriteLine();
                        PlsqlMake.Run(Settings.getUserId(true), Settings.getSourceScriptsDir(true), false, false, true, Settings.getSpoolOutput(false));
                    }

                    if (!Settings.getTargetIsDbFlag(false) && Settings.getTargetIsFilesystemFlag(false))
                    {
                        Console.WriteLine("Action: Sync PL/SQL differences to filesystem");
                        Console.WriteLine();
                        PlsqlMake.Run(Settings.getUserId(true), Settings.getSourceScriptsDir(true), false, true, false, "");
                    }

                    break;

                case "-SCANNER":
                    Console.WriteLine("Action: SQLPlus Scanner");
                    Console.WriteLine();
                    RunScanner(Settings.getScript(true));
                    break;
                case "-INSTALL":
                    Console.WriteLine("Action: Installs schema based on install scripts");
                    Console.WriteLine();
                    Install.Go(Settings.getUserId(false), Settings.getSpoolOutput(false), Settings.getSourceScriptsDir(true), Settings.getRecurseFlag(true));
                    break;
                case "-UPGRADE":
                    Console.WriteLine("Action: Upgrades schema based on upgrade scripts");
                    Console.WriteLine();
                    Upgrade.Go(Settings.getUserId(true), Settings.getSpoolOutput(false), Settings.getSourceScriptsDir(true), Settings.getRecurseFlag(true), Settings.getUpgradeTo(false));
                    break;
                case "-STATUS":
                    Console.WriteLine("Action: Prints target schema status");
                    Console.WriteLine();

                    db = new OracleSql();
                    db.OpenMetaConnection(Settings.getUserId(true));
                    RegistryTable.status(db);
                    break;
                case "-LIST_ERRORS":
                    Console.WriteLine("Action: Lists all errors from last install");
                    Console.WriteLine();
                    ListErrorsCmdline.ExecuteTask();

                    break;
                case "-CLEAR_ERRORS":
                    Console.WriteLine("Action: Clear all errors from last install");
                    Console.WriteLine();

                    db = new OracleSql();
                    db.OpenMetaConnection(Settings.getUserId(true));
                    RegistryTable.clearErrors(db);
                    break;
                case "-CLEAR_ERROR":
                    Console.WriteLine("Action: Clear error with [sequenceNumber] from last install");
                    Console.WriteLine();

                    db = new OracleSql();
                    db.OpenMetaConnection(Settings.getUserId(true));
                    RegistryTable.clearError(db, int.Parse(getNextArg(ref argIndex, args, "Error sequence number")));
                    break;
                case "-CRAWL":
                    Console.WriteLine("Action: Crawl and print links");
                    Console.WriteLine();
                    Crawl.Run("START", Settings.getScript(true), Path.GetDirectoryName(Settings.getScript(true)),0);
                    break;
                case "-STATS":
                    Console.WriteLine("Action: Prints stats");
                    Console.WriteLine();
                    //Stats.Print(args[argIndex + 1], includeList, SearchOption.AllDirectories);
                    Stats.BasicStats(Settings.getSourceScriptsDir(true), SearchOption.AllDirectories);
                    break;
                case "-LIST_CHANGES":
                    Console.WriteLine("Action: List all changes of an database object");
                    Console.WriteLine();

                    ListChanges(getNextArg(ref argIndex, args, "Database object name").ToUpper());
                    break;

                case "-GRANTS":
                    db = new OracleSql();
                    db.OpenMetaConnection(Settings.getUserId(true));
                    if (Settings.getTargetIsDbFlag(false)) db.OpenConnection(Settings.getUserId(true));
                    db.setEcho(EchoFlag.on);
                    Grants.RunObjectGrants(db, Settings.getSourceScriptsDir(true));
                    break;

                case "-SYSGRANTS":
                    db = new OracleSql();
                    db.OpenMetaConnection(Settings.getUserId(true));
                    if (Settings.getTargetIsDbFlag(false)) db.OpenConnection(Settings.getUserId(true));
                    db.setEcho(EchoFlag.on);
                    Grants.RunSystemGrants(db, Settings.getSourceScriptsDir(true));
                    break;

                case "-SYNONYMS":
                    db = new OracleSql();
                    db.OpenMetaConnection(Settings.getUserId(true));
                    if (Settings.getTargetIsDbFlag(false)) db.OpenConnection(Settings.getUserId(true));
                    db.setEcho(EchoFlag.on);
                    Synonym.RunPrivateSynonyms(db, Settings.getSourceScriptsDir(true));
                    break;

                case "-TEST":
                    Console.WriteLine("Action: Test");
                    Console.WriteLine();
                    Console.WriteLine(VersionStringManipulation.extractVersionStringFromTextualFile(Settings.getSourceScriptsDir(true), Settings.getRecurseFlag(true), Settings.getDatamodelVersionFilename(true), Settings.getDatamodelVersionSearchPattern(true), Settings.getDatamodelVersionIdDefinition(true))) ;
                    break;

                default:
                    Log.Error("program", "Unknown action: {0}", actionName);
                    Program.Exit(1, "SQLMake is unable to start");
                    break;
            }

            Log.Verbose("program", "Sqlmake finished successfuly");

            DateTime end = DateTime.Now;
            TimeSpan duration = end - start;
            Log.Info("program", "Elapsed time " + duration.TotalSeconds + " sec");
        }