Exemplo n.º 1
0
        /// <summary>
        /// The main entry point for the application.
        /// Handles processing of the command line args.
        /// </summary>
        /// <param name="args">Passes arguments string array into the appropriate methods and functions contained within the class.</param>
        static void Main(string[] args)
        {
            if (args.Length > 0)
            {
                // TODO: add additional commandline args functions
                foreach (string arg in args)
                {
                    if (arg.Split('=').Length == 2)
                    {
                        string command  = arg.Split('=')[0].Trim().ToLower();
                        string argument = arg.Split('=')[1].Trim();
                        if (command.IndexOf("/src_server") >= 0)
                        {
                            _src_server = argument;
                        }
                        else if (command.IndexOf("/src_user") >= 0 || command.IndexOf("/src_uid") >= 0)
                        {
                            _src_user = argument;
                        }
                        else if (command.IndexOf("/src_password") >= 0 || command.IndexOf("/src_pwd") >= 0)
                        {
                            _src_password = argument;
                        }
                        else if (command.IndexOf("/src_database") >= 0 || command.IndexOf("/src_catalog") >= 0)
                        {
                            _src_db = argument;
                        }
                        else if (command.IndexOf("/src_file") >= 0)
                        {
                            _src_file  = argument;
                            _Translate = true;
                        }
                        else if (command.IndexOf("/src_schema") >= 0)
                        {
                            _src_schema = argument;
                        }
                        if (command.IndexOf("/dest_server") >= 0)
                        {
                            _dest_server = argument;
                        }
                        else if (command.IndexOf("/dest_user") >= 0 || command.IndexOf("/dest_uid") >= 0)
                        {
                            _dest_user = argument;
                        }
                        else if (command.IndexOf("/dest_password") >= 0 || command.IndexOf("/dest_pwd") >= 0)
                        {
                            _dest_password = argument;
                        }
                        else if (command.IndexOf("/dest_database") >= 0 || command.IndexOf("/dest_catalog") >= 0)
                        {
                            _dest_db = argument;
                        }
                        else if (command.IndexOf("/dest_file") >= 0)
                        {
                            _dest_file = argument;
                            _Translate = true;
                        }
                        else if (command.IndexOf("/dest_schema") >= 0)
                        {
                            _dest_schema = argument;
                        }
                        else if (command.IndexOf("/diffxmlfile") >= 0)
                        {
                            _diffXMLFile = argument;
                            _Translate   = true;
                        }
                        else if (command.IndexOf("/diffsqlfile") >= 0)
                        {
                            _diffName  = argument;
                            _Translate = true;
                        }
                        else if (command.IndexOf("/createxmlfile") >= 0)
                        {
                            _createXMLFile = argument;
                            _Translate     = true;
                        }
                        else if (command.IndexOf("/createsqlfile") >= 0)
                        {
                            _createName = argument;
                            _Translate  = true;
                        }
                        else if (command.IndexOf("/customxslt") >= 0)
                        {
                            _customXSLT = argument;
                        }
                        else if (command.IndexOf("/sqlobjectmask") >= 0)
                        {
                            try
                            {
                                _SqlObjectsToCompare = Convert.ToByte(argument);
                            }
                            catch
                            {
                                logger.Debug(string.Format("Incorrect argument entered for Sql Object Mask parameter: {0}\n Defaulting to all SQL objects.", argument));
                            }
                        }
                    }
                    if (arg.Split('=').Length == 1 && arg.Length > 0)
                    {
                        if (arg.IndexOf("/?") >= 0 || arg.ToLower().IndexOf("/help") >= 0)
                        {
                            HelpText.DisplayDefaultHelp("About The SQL Schema Commandline Tool");
                            return;
                        }
                        else if (arg.ToLower().IndexOf("/src_trusted") >= 0)
                        {
                            _src_trusted = "true";
                        }
                        else if (arg.ToLower().IndexOf("/dest_trusted") >= 0)
                        {
                            _dest_trusted = "true";
                        }
                        else if (arg.ToLower().IndexOf("/comparesproctext") >= 0)
                        {
                            _CompareSprocText = true;
                        }
                        else if (arg.ToLower().IndexOf("/compareviewtext") >= 0)
                        {
                            _CompareViewText = true;
                        }
                        else if (arg.ToLower().IndexOf("/transform") >= 0)
                        {
                            _Translate = true;
                        }
                        else if (arg.ToLower().IndexOf("/translate") >= 0) // this is for my benefit since I keep typing it in wrong on the commandline.
                        {
                            _Translate = true;
                        }
                        else if (arg.ToLower().IndexOf("/primary") >= 0)
                        {
                            _Primary = true;
                        }
                        else if (arg.ToLower().IndexOf("/nothreads") >= 0)
                        {
                            _useThreads = false;
                        }
                    }
                }
            }
            else
            {
                HelpText.DisplayDefaultHelp("About The SQL Schema Commandline Tool");
                return;
            }

            if (_src_trusted.ToLower() == "true")
            {
                _src_user     = null;
                _src_password = null;
            }
            else
            {
                if (_src_server != null && _src_db != null && (_src_user == null || _src_password == null))
                {
                    logger.Warn("You must enter the trusted argument or a user name and password for the source SQL server in the commandline arguments.");
                    return;
                }
            }

            if (_dest_trusted.ToLower() == "true")
            {
                _dest_user     = null;
                _dest_password = null;
            }
            else
            {
                if (_dest_server != null && _dest_db != null && (_dest_user == null || _dest_password == null))
                {
                    logger.Warn("You must enter the trusted argument or a user name and password for the source SQL server in the commandline arguments.");
                    return;
                }
            }
            if (_src_server != null && _src_db != null && _dest_server != null && _dest_db != null)
            {
                if (_src_server.ToLower() == _dest_server.ToLower() && _src_db.ToLower() == _dest_db.ToLower())
                {
                    logger.Warn("Thats silly, you can't compare the same SQL server and database against itself!");
                    return;
                }
            }

            if (_src_schema != null && _dest_schema != null)
            {
                if (_src_schema.ToLower() == _dest_schema.ToLower())
                {
                    logger.Warn("Thats silly, you can't compare the same SQL server and database XML file against itself!");
                    return;
                }
            }

            // TODO: add the ability to exclude database objects by name or regex string
            // TODO: add error checks for file.exists on all passed in file names for files that are supposed to already exist
            DateTime startTime = DateTime.Now;

            logger.Info("SST Started on {0}\nStarting SQL Schema Processing, Please wait...", startTime);

            Thread newSourceThread = null;
            Thread newDestThread   = null;

            if (_src_server != null && _src_db != null && _src_schema == null)
            {
                if (_useThreads)
                {
                    newSourceThread = new Thread(new ThreadStart(SourceThreadMethod));
                    newSourceThread.SetApartmentState(ApartmentState.MTA);
                    newSourceThread.Start();
                    Thread.SpinWait(20);
                }
                else
                {
                    _src_schema = SQLSchemaTool.SerializeDB(_src_server, _src_db, _src_user, _src_password, _src_file, _Translate, _Primary, null, _SqlObjectsToCompare, _customXSLT);
                }
            }

            if (_dest_server != null && _dest_db != null && _dest_schema == null)
            {
                if (_useThreads)
                {
                    newDestThread = new Thread(new ThreadStart(DestThreadMethod));
                    newDestThread.SetApartmentState(ApartmentState.MTA);
                    newDestThread.Start();
                    Thread.SpinWait(20);
                }
                else
                {
                    _dest_schema = SQLSchemaTool.SerializeDB(_dest_server, _dest_db, _dest_user, _dest_password, _dest_file, _Translate, _Primary, null, _SqlObjectsToCompare, _customXSLT);
                }
            }

            if (_useThreads && newSourceThread != null && newDestThread != null)
            {
                uint loops = 0;
                while (newSourceThread.IsAlive || newDestThread.IsAlive)
                {
                    if (Environment.ProcessorCount == 1 || (++loops % 100) == 0)
                    {
                        Thread.Sleep(1);
                        if ((loops % 10000) == 0)
                        {
                            Console.Write('.');
                        }
                    }
                    else
                    {
                        Thread.SpinWait(20);
                        if ((loops % 10000) == 0)
                        {
                            Console.Write('.');
                        }
                    }
                }
            }

            if (_src_schema != null && _dest_schema != null)
            {
                string diffName = null;
                // some logic to create default diff XML file name
                if (_diffXMLFile == null || _diffXMLFile.Length == 0)
                {
                    if (_src_server != null && _src_db != null && _dest_server != null && _dest_db != null)
                    {
                        diffName = string.Format("{0}_{1}_DIFF_{2}_{3}_SCHEMA.xml", _src_server.Replace("\\", "_").Replace(":", "-"), _src_db, _dest_server.Replace("\\", "_").Replace(":", "-"), _dest_db);
                    }
                    else
                    {
                        string src  = XMLFileName(Path.GetFileNameWithoutExtension(_src_schema)).ToLower().Replace("_schema", "").Replace(".xml", "");
                        string dest = XMLFileName(Path.GetFileNameWithoutExtension(_dest_schema)).Split(',')[0].ToLower().Replace("_schema", "").Replace(".xml", "");
                        diffName = string.Format(SQLSchemaTool._DIFFFILE, src, dest);
                    }
                }
                else
                {
                    diffName = _diffXMLFile;
                }
                string SQLfile = diffName.ToLower().Replace(".xml", ".sql");
                SQLfile = !SQLfile.EndsWith(".sql") ? SQLfile + ".sql" : SQLfile;

                // do the compare
                SQLSchemaTool.CompareSchema(XMLFileName(_src_schema), XMLFileName(_dest_schema), diffName, SQLfile, _CompareSprocText, _CompareViewText, _Translate, _SqlObjectsToCompare, _customXSLT);
                DateTime endTime = DateTime.Now;
                logger.Info("Finished on {0}\nLapsed time = {1}", endTime, endTime.Subtract(startTime));
                return;
            }

            if (_diffXMLFile != null && _diffXMLFile.Length > 0 && _src_schema == null && _dest_schema == null && _Translate)
            {
                // perform garbage collection to free up memory
                GC.Collect();

                logger.Info("\nPlease wait. Beginning SQL transformation of DiffGram XML...");
                string diffName = _diffName == null?_diffXMLFile.ToLower().Replace(".xml", ".sql") : _diffName;

                diffName = !diffName.EndsWith(".sql") ? diffName + ".sql" : diffName;

                XsltHelper.SQLTransform(_diffXMLFile, XsltHelper.SQLDIFFXSLT, diffName);

                logger.Info("\nSQL Diff Schema has been saved to " + diffName + ".");
                DateTime endTime = DateTime.Now;

                logger.Info("Finished on {0}\nLapsed time = {1}", endTime, endTime.Subtract(startTime));
                return;
            }

            if (_createXMLFile != null && _createXMLFile.Length > 0 && _src_schema == null && _dest_schema == null && _Translate)
            {
                // perform garbage collection to free up memory
                GC.Collect();

                logger.Info("\nPlease wait. Beginning SQL transformation of schema XML...");

                string createName = _createName == null?_createXMLFile.ToLower().Replace(".xml", ".sql") : _createName;

                createName = !createName.EndsWith(".sql") ? createName + ".sql" : createName;

                XsltHelper.SQLTransform(_createXMLFile, XsltHelper.SQLCREATEXSLT, createName);

                logger.Info("\nSQL Create Schema has been saved to " + createName + ".");
                DateTime endTime = DateTime.Now;

                logger.Info("Finished on {0}\nLapsed time = {1}", endTime, endTime.Subtract(startTime));
                return;
            }

            if (_src_schema != null && _src_schema.Length > 0 && _createXMLFile == null && _dest_schema == null && _Translate)
            {
                logger.Info("\nSQL Create Schema has been saved to " + XMLFileName(_src_schema) + ".");

                DateTime endTime = DateTime.Now;
                logger.Info("Finished on {0}\nLapsed time = {1}", endTime, endTime.Subtract(startTime));
                return;
            }
        }