Ejemplo n.º 1
0
        public void DrawObjects()
        {
            if (_dependencyObjectList.Count > 0)
            {
                LongRunningOperationManager.ReportProgress(-1);

                DateTime dt = DateTime.Now;
                MyOutput.NewMessage(EOutputMessageType.PROGRESS, string.Format("Drawing objects..."));

                this.treeListView1.Roots = _dependencyObjectList.Where(m => m.SystemParentId == -1).OrderBy(m => m.ObjectName);
                tsslObjectCount.Text     = string.Format("{0:n0} total, {1:n0} root, {2:n0} dependency, {3:n0} unique", _dependencyObjectList.Count(), _dependencyObjectList.Count(d => d.HierarchyLevel == 0), _dependencyObjectList.Count(d => d.HierarchyLevel > 0), _dependencyObjectList.Distinct(new MySqlObjectEqualityComparer()).Count());

                MyOutput.AppendToLastMessage(EOutputMessageType.INFORMATION, string.Format("Done. {0:n0} objects drawn.", _dependencyObjectList.Count()), (DateTime.Now - dt).TotalSeconds);
            }
        }
        void LongRunningOperationManager_DoWork(object sender, DoWorkEventArgs e)
        {
            LongRunningOperationManager.StartNextStep();
            for (int i = 0; i < 100; i++)
            {
                LongRunningOperationManager.ReportProgress(i + 1);
                Thread.Sleep(100);

                //if (LongRunningOperationManager.CancellationPending)
                //{
                //    e.Cancel = true;
                //    return;
                //}
            }


            LongRunningOperationManager.StartNextStep();
            for (int i = 0; i < 100; i++)
            {
                LongRunningOperationManager.ReportProgress(i + 1);
                Thread.Sleep(50);

                if (LongRunningOperationManager.CancellationPending)
                {
                    e.Cancel = true;
                    return;
                }
            }
            //LongRunningOperationManager.ReportProgress(100);

            LongRunningOperationManager.StartNextStep();
            for (int i = 0; i < 100; i++)
            {
                LongRunningOperationManager.ReportProgress(i + 1);
                Thread.Sleep(20);

                if (LongRunningOperationManager.CancellationPending)
                {
                    e.Cancel = true;
                    return;
                }
            }
        }
        private void lrom_DoWorkConnectToSqlServer(object sender, System.ComponentModel.DoWorkEventArgs e)
        {
            Dictionary <int, SqlConnection> pool = (Dictionary <int, SqlConnection>)e.Argument;
            List <int> succServerObjects         = new List <int>();
            int        counter = 0;

            LongRunningOperationManager.StartNextStep();

            foreach (var c in pool)
            {
                counter++;

                LongRunningOperationManager.ReportProgress(counter / pool.Count);
                SqlConnection conn = c.Value;


                if (conn.State == System.Data.ConnectionState.Open)
                {
                    succServerObjects.Add(c.Key);
                }
                else
                {
                    try
                    {
                        conn.Open();
                        succServerObjects.Add(c.Key);
                    }
                    catch (Exception ex)
                    {
                        MyOutput.NewMessage(EOutputMessageType.ERROR, ex.Message, true);
                    }
                }
            }

            e.Result = succServerObjects.ToArray();
        }
Ejemplo n.º 4
0
        private void LongRunningOperationManager_DoWorkExecQuery(object sender, DoWorkEventArgs e)
        {
            LongRunningOperationManager.StartNextStep();
            LongRunningOperationManager.ReportProgress(-1);

            MyOutput.NewMessage(EOutputMessageType.PROGRESS, string.Format("Reading data from {0}... ", this._mySqlObject.ObjectNameFull));

            DateTime dt = DateTime.Now;

            string query = string.Format("SELECT TOP 1000 * FROM [{0}].[{1}] WITH (NOLOCK)", _mySqlObject.SchemaName, _mySqlObject.ObjectName);

            _mySqlServer.ConnectionPool.GetConnection(_mySqlObject.ServerObjectId).ChangeDatabase(_mySqlObject.DatabaseName);
            cmd                = (SqlCommand)_mySqlServer.ConnectionPool.GetConnection(_mySqlObject.ServerObjectId).CreateCommand();
            cmd.CommandText    = query;
            cmd.CommandTimeout = UserSettings.Default.CommandTimeout;
            cmd.CommandType    = CommandType.Text;

            SqlDataReader rdr = null;

            try
            {
                rdr = cmd.ExecuteReader(CommandBehavior.SingleResult);
            }
            catch (Exception ex1)
            {
                if (!LongRunningOperationManager.CancellationPending)
                {
                    MyOutput.AppendToLastMessage(EOutputMessageType.ERROR, string.Format("{0}", ex1.Message));
                    MessageBox.Show(ex1.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
                else
                {
                    MyOutput.AppendToLastMessage(EOutputMessageType.WARNING, string.Format("Done. Cancelled by user."), (DateTime.Now - dt).TotalSeconds);
                }
            }

            if (rdr == null)
            {
                e.Cancel = true;
                return;
            }

            DataTable dataTable = new DataTable();

            dataTable.Load(rdr);
            rdr.Close();

            // Get all byte columns
            List <DataColumn> byteColumns = dataTable.Columns.Cast <DataColumn>().Where(dc => dc.DataType == typeof(byte[])).ToList();

            foreach (DataColumn dc in byteColumns)
            {
                int    colOrdinal = dc.Ordinal;
                string colName    = String.Format("{0} (Hex)", dc.ColumnName);

                DataColumn tmpCol = new DataColumn(colName, typeof(String));
                dataTable.Columns.Add(tmpCol);
                foreach (DataRow row in dataTable.Rows)
                {
                    row[tmpCol] = byteArrayToHexString((byte[])row[dc]);
                }
                dataTable.Columns.Remove(dc);
                dataTable.Columns[colName].SetOrdinal(colOrdinal);
            }

            e.Result = dataTable;

            MyOutput.AppendToLastMessage(EOutputMessageType.INFORMATION, string.Format("Done. Loaded {0} rows.", dataTable.Rows.Count), (DateTime.Now - dt).TotalSeconds);
        }
Ejemplo n.º 5
0
        public void Load(bool forceReload)
        {
            DateTime dt;
            int      dbCount;
            int      counter;

            this._status = ESqlServerStatus.REFRESHING;

            OnDataChanged?.Invoke(this, null);

            // Make backup of all lists in case of use cancells execution
            MySqlDatabase[]             oldLoadedDatabases           = new MySqlDatabase[_sqlLoadedDatabases.Count];
            MySqlObject[]               oldSqlObjects                = new MySqlObject[_sqlObjects.Count];
            MySqlExpressionDependency[] oldSqlExpressionDependencies = new MySqlExpressionDependency[_sqlExpressionDependencies.Count];

            _sqlLoadedDatabases.CopyTo(oldLoadedDatabases);
            _sqlObjects.CopyTo(oldSqlObjects);
            _sqlExpressionDependencies.CopyTo(oldSqlExpressionDependencies);


            if (forceReload)
            {
                _sqlLoadedDatabases.Clear();
                _sqlObjects.Clear();
                _sqlExpressionDependencies.Clear();
            }

            MySqlDatabase[] newSqlDatabases = SqlCheckedDatabases.Except(SqlLoadedDatabases).ToArray();
            MySqlDatabase[] oldSqlDatabases = SqlLoadedDatabases.Except(SqlCheckedDatabases).ToArray();

            counter = 0;
            dbCount = newSqlDatabases.Length * 2 + oldSqlDatabases.Length;

            // Remove unnecessary objects from unused database
            if (oldSqlDatabases.Length > 0)
            {
                foreach (MySqlDatabase oldDb in oldSqlDatabases)
                {
                    if (LongRunningOperationManager.CancellationPending)
                    {
                        break;
                    }

                    dt = DateTime.Now;
                    MyOutput.NewMessage(EOutputMessageType.PROGRESS, string.Format("Removing unnecessary objects for server '{0}', database '{1}' from internal database... ", oldDb.ServerObjectName, oldDb.DatabaseName));
                    int deletedObjectCount = _sqlObjects.RemoveAll(o => o.ServerObjectId == oldDb.ServerObjectId && o.DatabaseId == oldDb.DatabaseId);
                    MyOutput.AppendToLastMessage(EOutputMessageType.INFORMATION, string.Format("Done. Removed {0:n0} objects.", deletedObjectCount), (DateTime.Now - dt).TotalSeconds);

                    dt = DateTime.Now;
                    MyOutput.NewMessage(EOutputMessageType.PROGRESS, string.Format("Removing unnecessary dependencies for server '{0}', database '{1}' from internal database... ", oldDb.ServerObjectName, oldDb.DatabaseName));
                    int deletedExpressionDependencyCount = _sqlExpressionDependencies.RemoveAll(o => o.ServerObjectId == oldDb.ServerObjectId && o.DatabaseId == oldDb.DatabaseId);
                    MyOutput.AppendToLastMessage(EOutputMessageType.INFORMATION, string.Format("Done. Removed {0:n0} dependencies.", deletedExpressionDependencyCount), (DateTime.Now - dt).TotalSeconds);

                    LongRunningOperationManager.ReportProgress(100 * ++counter / dbCount);
                }
            }

            if (newSqlDatabases.Length > 0)
            {
                foreach (MySqlDatabase newDb in newSqlDatabases)
                {
                    if (LongRunningOperationManager.CancellationPending)
                    {
                        break;
                    }

                    initSqlObjects(newDb.ServerObjectId, newDb.DatabaseId);
                    //loadDatabaseObjects(newDb.ServerObjectName, newDb.DatabaseName);

                    LongRunningOperationManager.ReportProgress(100 * ++counter / dbCount);
                }
                MyOutput.NewMessage(EOutputMessageType.INFORMATION, string.Format("Totally {0:n0} objects loaded from {1:n0} server(s) and {2:n0} database(s).", _sqlObjects.Count, SqlCheckedDatabases.Select(s => s.ServerObjectName).Distinct().Count(), SqlCheckedDatabases.Length));

                // ALL OBJECTS FROM ALL SERVERS / DATABASES MUST BE LOADED FIRST!
                foreach (MySqlDatabase newDb in newSqlDatabases)
                {
                    if (LongRunningOperationManager.CancellationPending)
                    {
                        break;
                    }

                    initSqlExpressionDependencies(newDb.ServerObjectId, newDb.DatabaseId);

                    LongRunningOperationManager.ReportProgress(100 * ++counter / dbCount);
                }
                MyOutput.NewMessage(EOutputMessageType.INFORMATION, string.Format("Totally {0:n0} dependencies loaded from {1:n0} server(s) and {2:n0} database(s).", _sqlExpressionDependencies.Count, SqlCheckedDatabases.Select(s => s.ServerObjectName).Distinct().Count(), SqlCheckedDatabases.Length));
            }

            if (!LongRunningOperationManager.CancellationPending)
            {
                _sqlLoadedDatabases.Clear();
                _sqlLoadedDatabases.AddRange(SqlCheckedDatabases);

                if (oldSqlDatabases.Length > 0 || newSqlDatabases.Length > 0)
                {
                    this._lastDataLoad = DateTime.Now;
                }
            }
            else
            {
                _sqlLoadedDatabases.Clear();
                _sqlObjects.Clear();
                _sqlExpressionDependencies.Clear();

                _sqlLoadedDatabases.AddRange(oldLoadedDatabases);
                _sqlObjects.AddRange(oldSqlObjects);
                _sqlExpressionDependencies.AddRange(oldSqlExpressionDependencies);
            }

            this._status = ESqlServerStatus.READY;


            // Prepare EX lists

            /*
             * SqlExpressionDependenciesEx.Clear();
             * foreach (var ed in _sqlExpressionDependencies)
             * {
             *  var keys = new MyKeyList();
             *  keys.Add(ed.ParentServerObjectId);
             *  keys.Add(ed.ParentDatabaseId);
             *  keys.Add(ed.ParentObjectId);
             *  keys.Add(ed.ChildServerObjectId);
             *  keys.Add(ed.ChildDatabaseId);
             *  keys.Add(ed.ChildObjectId);
             *  SqlExpressionDependenciesEx.Add(keys, ed);
             * }
             */

            OnDataChanged?.Invoke(this, null);
        }
Ejemplo n.º 6
0
        private int loadDependentObjects(MySqlObject masterParentObject, MySqlObject[] objs, bool isTopDown, ref int refObjectId, int level)
        {
            int  totalMaxLevel  = -1;
            bool hasNextObjects = false;
            int  counter        = 0;

            IEnumerable <MySqlObject> nextObjects;
            IEnumerable <MySqlObject> nextSynonymObjects;


            foreach (MySqlObject obj in objs)
            {
                if (LongRunningOperationManager.CancellationPending)
                {
                    //e.Cancel = true;
                    break;
                }

                var singleObjectMaxLevel = -1;

                // Get next objects which are NOT synonyms
                if (isTopDown)
                {
                    nextObjects = from o in _mySqlServer.SqlObjects
                                  join d in _mySqlServer.SqlExpressionDependencies.Where(d => d.ChildObjectExists).Where(d => d.ChildObjectId != d.ParentObjectId)
                                  on      new { ServerId = o.ServerObjectId, DatabaseId = o.DatabaseId, ObjectId = o.ObjectId }
                    equals  new { ServerId = d.ChildServerObjectId, DatabaseId = d.ChildDatabaseId, ObjectId = d.ChildObjectId }
                    where obj.ServerObjectId == d.ParentServerObjectId &&
                    obj.DatabaseId == d.ParentDatabaseId &&
                    obj.ObjectId == d.ParentObjectId &&
                    obj.ObjectType != "SYNONYM"
                    select o;

                    var nonExistingNextObjects = from d in _mySqlServer.SqlExpressionDependencies.Where(d => !d.ChildObjectExists).Where(d => d.ChildObjectId != d.ParentObjectId)
                                                 where obj.ServerObjectId == d.ParentServerObjectId &&
                                                 obj.DatabaseId == d.ParentDatabaseId &&
                                                 obj.ObjectId == d.ParentObjectId &&
                                                 obj.ObjectType != "SYNONYM"
                                                 select new MySqlObject
                    {
                        ServerObjectName = d.ChildServerObjectName,
                        DatabaseName     = d.ChildDatabaseName,
                        SchemaName       = d.ChildSchemaName,
                        ObjectName       = d.ChildObjectName,

                        ServerObjectId = -1,
                        DatabaseId     = -1,
                        SchemaId       = -1,
                        ObjectId       = -1,
                        ObjectNameFull = string.Format("[{0}].[{1}].[{2}].[{3}]", d.ChildServerObjectName, d.ChildDatabaseName, d.ChildSchemaName, d.ChildObjectName),
                        ObjectTypeId   = "UNRESOLVED_ENTITY",
                        ObjectType     = "UNRESOLVED_ENTITY"
                    };

                    nextObjects = nextObjects.Union(nonExistingNextObjects);

                    if (!string.IsNullOrEmpty(obj.BaseObjectNameFull))
                    {
                        string[] bo           = obj.BaseObjectNameFull.Replace("[", string.Empty).Replace("]", string.Empty).Split('.');
                        string   serverName   = bo[0];
                        string   databaseName = bo[1];

                        if (
                            !_mySqlServer.SqlLoadedDatabases.Any(
                                db => db.ServerObjectName.Equals(serverName, StringComparison.InvariantCultureIgnoreCase) &&
                                db.DatabaseName.Equals(databaseName, StringComparison.InvariantCultureIgnoreCase)))
                        {
                            _recommendedDatabases.Add(new KeyValuePair <MySqlObject, MySqlDatabase>(obj, new MySqlDatabase {
                                ServerObjectId = -1, ServerObjectName = serverName, DatabaseId = -1, DatabaseName = databaseName
                            }));
                        }
                    }

                    nextSynonymObjects = from o in _mySqlServer.SqlObjects
                                         where o.ObjectNameFull.Equals(obj.BaseObjectNameFull, StringComparison.InvariantCultureIgnoreCase) &&
                                         obj.ObjectType == "SYNONYM"
                                         select o;
                }
                else
                {
                    nextObjects = from o in _mySqlServer.SqlObjects
                                  join d in _mySqlServer.SqlExpressionDependencies.Where(d => d.ChildObjectId != d.ParentObjectId) on new { ServerId = o.ServerObjectId, o.DatabaseId, o.ObjectId } equals new { ServerId = d.ParentServerObjectId, DatabaseId = d.ParentDatabaseId, ObjectId = d.ParentObjectId }
                    where obj.ServerObjectId == d.ChildServerObjectId &&
                    obj.DatabaseId == d.ChildDatabaseId &&
                    obj.ObjectId == d.ChildObjectId &&
                    o.ObjectType != "SYNONYM"
                    select o;

                    // non-existing objects

                    //var a1 = from d in _inMemoryDatabase.SqlDependencyList.Where(d => !d.ChildObjectExists).Where(d => d.ChildObjectId != d.ParentObjectId)
                    //         where obj.ServerName.Equals(d.ChildServerName, StringComparison.InvariantCultureIgnoreCase) &&
                    //         obj.DatabaseName.Equals(d.ChildDatabaseName, StringComparison.InvariantCultureIgnoreCase) &&
                    //         obj.SchemaName.Equals(d.ChildSchemaName, StringComparison.InvariantCultureIgnoreCase) &&
                    //         obj.ObjectName.Equals(d.ChildObjectName, StringComparison.InvariantCultureIgnoreCase)
                    //         select d;



                    var nonExistingNextObjects = from o in _mySqlServer.SqlObjects
                                                 join d in _mySqlServer.SqlExpressionDependencies.Where(d => !d.ChildObjectExists).Where(d => d.ChildObjectId != d.ParentObjectId) on new { ServerId = o.ServerObjectId, o.DatabaseId, o.ObjectId } equals new { ServerId = d.ParentServerObjectId, DatabaseId = d.ParentDatabaseId, ObjectId = d.ParentObjectId }
                    where obj.ServerObjectName.Equals(d.ChildServerObjectName, StringComparison.InvariantCultureIgnoreCase) &&
                    obj.DatabaseName.Equals(d.ChildDatabaseName, StringComparison.InvariantCultureIgnoreCase) &&
                    obj.SchemaName.Equals(d.ChildSchemaName, StringComparison.InvariantCultureIgnoreCase) &&
                    obj.ObjectName.Equals(d.ChildObjectName, StringComparison.InvariantCultureIgnoreCase) &&
                    o.ObjectType != "SYNONYM"
                    select o;

                    nextObjects = nextObjects.Union(nonExistingNextObjects);

                    nextSynonymObjects = from o in _mySqlServer.SqlObjects
                                         where obj.ObjectNameFull.Equals(o.BaseObjectNameFull, StringComparison.InvariantCultureIgnoreCase) &&
                                         o.ObjectType == "SYNONYM"
                                         select o;
                }


                var allNextObjects = nextObjects.Union(nextSynonymObjects);

                if (allNextObjects.Any())
                {
                    hasNextObjects = true;
                }

                if (masterParentObject == null && allNextObjects.Any() || masterParentObject != null)
                {
                    obj.SystemId          = ++refObjectId;
                    obj.SystemParentId    = (masterParentObject == null) ? -1 : masterParentObject.SystemId;
                    obj.HierarchyLevel    = (masterParentObject == null) ? 0 : masterParentObject.HierarchyLevel + 1;
                    obj.IsSelfReferencing = _mySqlServer.SqlExpressionDependencies.Any(d => d.ParentObjectId == obj.ObjectId && d.ParentObjectId == d.ChildObjectId) ? "Yes" : "No";

                    int nextObjectsMaxLevel = loadDependentObjects(obj, allNextObjects.ToArray(), isTopDown, ref refObjectId, level + 1);

                    if (nextObjectsMaxLevel > totalMaxLevel)
                    {
                        totalMaxLevel = nextObjectsMaxLevel;
                    }
                    if (nextObjectsMaxLevel > singleObjectMaxLevel)
                    {
                        singleObjectMaxLevel = nextObjectsMaxLevel;
                    }

                    obj.MaximumUnderlyingLevels = singleObjectMaxLevel - obj.HierarchyLevel;
                    _dependencyObjectList.Add((MySqlObject)obj.Clone());
                }

                if (level == 0)
                {
                    LongRunningOperationManager.ReportProgress(100 * ++counter / objs.Length);
                }
            }
            return(Math.Max(totalMaxLevel, (hasNextObjects) ? level : level - 1));

            //MyOutput.NewMessage(EOutputMessageType.INFORMATION, string.Format("Done. Loaded {0:n0} dependencies in {1:0.00} seconds.", _sqlDependencyList.Count - initialNumberOfRows, (DateTime.Now - dt).TotalSeconds));
        }