Exemple #1
0
        /// <summary>
        /// This method is overriden to display warning message before deletion. Then just calls base.
        /// </summary>
        /// <param name="hierarchy">Server explorer facade object to be used for Server Explorer hierarchy interaction..</param>
        /// <param name="items">Array with item’s identifiers.</param>
        /// <returns>Always returns null.</returns>
        public override object[] Execute(ServerExplorerFacade hierarchy, int[] items)
        {
            if (hierarchy == null)
            {
                throw new ArgumentNullException("hierarchy");
            }
            if (items == null)
            {
                throw new ArgumentNullException("items");
            }

            // Build object names list
            StringBuilder list = new StringBuilder();

            foreach (int item in items)
            {
                // Extract name for item
                string name = hierarchy.GetName(item);
                if (String.IsNullOrEmpty(name))
                {
                    continue;
                }

                // If builder is not empty, add ', '
                if (list.Length > 0)
                {
                    list.Append(Resources.Comma);
                }

                // Append object name
                list.Append(name);
            }

            // Build warning message
            string message = String.Format(CultureInfo.CurrentCulture, Resources.Warning_ConfirmDelete, list.ToString());

            // First, ask user for confirmation
            if (UIHelper.ShowWarning(message, MessageBoxButtons.YesNo)
                != DialogResult.Yes)
            {
                return(null);
            }

            // Only if user confirmed delete, call to base class to execute command
            return(base.Execute(hierarchy, items));
        }
Exemple #2
0
        /// <summary>
        /// Returns DROP PROCEDURE/FUNCTION statement.
        /// </summary>
        /// <param name="identifier">Database object identifier.</param>
        /// <returns>Returns DROP TABLE statement.</returns>
        public string BuildDropSql(ServerExplorerFacade hierarchy,
                                   int item, object[] identifier)
        {
            if (identifier == null)
            {
                throw new ArgumentNullException("identifier");
            }

            int    parentId   = hierarchy.GetParent(item);
            string parentName = hierarchy.GetName(parentId);

            // Build query
            StringBuilder query = new StringBuilder("DROP ");

            query.Append(parentName == "Stored Procedures" ? "PROCEDURE" : "FUNCTION");
            query.Append(' ');

            QueryBuilder.WriteIdentifier(identifier[1] as string, identifier[2] as string, query);
            return(query.ToString());
        }
Exemple #3
0
        /// <summary>
        /// Drops single item. Uses object descriptor to build DROP statement.
        /// </summary>
        /// <param name="hierarchy">Server explorer facade object to be used for Server Explorer hierarchy interaction.</param>
        /// <param name="item">Item identifier.</param>
        /// <returns>Result of execution. Typicaly null.</returns>
        protected override object ExecuteSingleItem(ServerExplorerFacade hierarchy, int item)
        {
            if (hierarchy == null)
                throw new ArgumentNullException("hierarchy");
            if (item < 0)
                throw new ArgumentOutOfRangeException("item");

            // Extract type name
            string typeName = GetObjectType(hierarchy, item);
            if (String.IsNullOrEmpty(typeName))
            {
                Debug.Fail("Failed to get object type!");
                return false;
            }

            // Extract descriptor
            IObjectDescriptor descriptor = ObjectDescriptorFactory.Instance.CreateDescriptor(typeName);
            if (descriptor == null || !descriptor.CanBeDropped)
            {
                Debug.Fail("Failed to get descriptor or descriptor doesn't support dropping!");
                return null;
            }

            // Extract object identifier
            object[] identifier = hierarchy.GetObjectIdentifier(item);
            if (identifier == null)
            {
                Debug.Fail("Failed to get object identifier!");
                return null;
            }

            // TODO: Note the seconde check for TableData document. It should be replaced in the future
            // by the generic mechanism.
            
            // Check if editor is registered for the database object
            if (hierarchy.HasDocument(typeName, identifier) || hierarchy.HasDocument(TableDataDescriptor.TypeName, identifier))
            {
                UIHelper.ShowError(String.Format(
                    CultureInfo.InvariantCulture,
                    Resources.Error_CantDropBecauseOfEditor,
                    hierarchy.GetName(item)));
                return null;
            }

            // Build DROP query
			string dropQuery = String.Empty;
			if (descriptor is StoredProcDescriptor)
				dropQuery = (descriptor as StoredProcDescriptor).BuildDropSql(
					hierarchy, item, identifier);
			else
				dropQuery = descriptor.BuildDropSql(identifier);

			if (String.IsNullOrEmpty(dropQuery))
            {
                Debug.Fail("Failed to build DROP query!");
                return null;
            }

            // Extract connection
            DataConnectionWrapper connection = hierarchy.Connection;
            if (connection == null)
            {
                Debug.Fail("Failed to extract connection!");
                return null;
            }

            try
            {
                // Execute drop query
                connection.ExecuteScalar(dropQuery);

                // Drop hierarchy node
                hierarchy.DropObjectNode(item);                

                // Notify everybody about object dropping
                connection.ObjectChangeEvents.RaiseObjectRemoved(typeName, identifier);
            }
            catch (DbException e)
            {
                Trace.TraceError("Error during saving object:\n{0}", e.ToString());
                SqlErrorDialog.ShowError(e, connection.GetFullStatus());
            }
            catch (Exception e)
            {
                Trace.TraceError("Error during saving object:\n{0}", e.ToString());
                UIHelper.ShowError(e);
            }

            return null;
        }
Exemple #4
0
        /// <summary>
        /// This method is overriden to display warning message before deletion. Then just calls base.
        /// </summary>
        /// <param name="hierarchy">Server explorer facade object to be used for Server Explorer hierarchy interaction..</param>
        /// <param name="items">Array with item’s identifiers.</param>
        /// <returns>Always returns null.</returns>
        public override object[] Execute(ServerExplorerFacade hierarchy, int[] items)
        {
            if (hierarchy == null)
                throw new ArgumentNullException("hierarchy");
            if (items == null)
                throw new ArgumentNullException("items");

            // Build object names list
            StringBuilder list = new StringBuilder();
            foreach (int item in items)
            {
                // Extract name for item
                string name = hierarchy.GetName(item);
                if (String.IsNullOrEmpty(name))
                    continue;

                // If builder is not empty, add ', '
                if (list.Length > 0)
                    list.Append(Resources.Comma);

                // Append object name
                list.Append(name);
            }

            // Build warning message
            string message = String.Format(CultureInfo.CurrentCulture, Resources.Warning_ConfirmDelete, list.ToString());

            // First, ask user for confirmation
            if (UIHelper.ShowWarning(message, MessageBoxButtons.YesNo)
                != DialogResult.Yes)
                return null;

            // Only if user confirmed delete, call to base class to execute command
            return base.Execute(hierarchy, items);
        } 
        /// <summary>
        /// Returns DROP PROCEDURE/FUNCTION statement.
        /// </summary>
        /// <param name="identifier">Database object identifier.</param>
        /// <returns>Returns DROP TABLE statement.</returns>
        public string BuildDropSql(ServerExplorerFacade hierarchy, 
			int item, object[] identifier)
        {
            if (identifier == null)
                throw new ArgumentNullException("identifier");

			int parentId = hierarchy.GetParent(item);
			string parentName = hierarchy.GetName(parentId);

            // Build query
            StringBuilder query = new StringBuilder("DROP ");
            query.Append(parentName == "Stored Procedures" ? "PROCEDURE" : "FUNCTION");
            query.Append(' ');

            QueryBuilder.WriteIdentifier(identifier[1] as string, identifier[2] as string, query);
            return query.ToString();
        }
Exemple #6
0
        /// <summary>
        /// Drops single item. Uses object descriptor to build DROP statement.
        /// </summary>
        /// <param name="hierarchy">Server explorer facade object to be used for Server Explorer hierarchy interaction.</param>
        /// <param name="item">Item identifier.</param>
        /// <returns>Result of execution. Typicaly null.</returns>
        protected override object ExecuteSingleItem(ServerExplorerFacade hierarchy, int item)
        {
            if (hierarchy == null)
            {
                throw new ArgumentNullException("hierarchy");
            }
            if (item < 0)
            {
                throw new ArgumentOutOfRangeException("item");
            }

            // Extract type name
            string typeName = GetObjectType(hierarchy, item);

            if (String.IsNullOrEmpty(typeName))
            {
                Debug.Fail("Failed to get object type!");
                return(false);
            }

            // Extract descriptor
            IObjectDescriptor descriptor = ObjectDescriptorFactory.Instance.CreateDescriptor(typeName);

            if (descriptor == null || !descriptor.CanBeDropped)
            {
                Debug.Fail("Failed to get descriptor or descriptor doesn't support dropping!");
                return(null);
            }

            // Extract object identifier
            object[] identifier = hierarchy.GetObjectIdentifier(item);
            if (identifier == null)
            {
                Debug.Fail("Failed to get object identifier!");
                return(null);
            }

            // TODO: Note the seconde check for TableData document. It should be replaced in the future
            // by the generic mechanism.

            // Check if editor is registered for the database object
            if (hierarchy.HasDocument(typeName, identifier) || hierarchy.HasDocument(TableDataDescriptor.TypeName, identifier))
            {
                UIHelper.ShowError(String.Format(
                                       CultureInfo.InvariantCulture,
                                       Resources.Error_CantDropBecauseOfEditor,
                                       hierarchy.GetName(item)));
                return(null);
            }

            // Build DROP query
            string dropQuery = String.Empty;

            if (descriptor is StoredProcDescriptor)
            {
                dropQuery = (descriptor as StoredProcDescriptor).BuildDropSql(
                    hierarchy, item, identifier);
            }
            else
            {
                dropQuery = descriptor.BuildDropSql(identifier);
            }

            if (String.IsNullOrEmpty(dropQuery))
            {
                Debug.Fail("Failed to build DROP query!");
                return(null);
            }

            // Extract connection
            DataConnectionWrapper connection = hierarchy.Connection;

            if (connection == null)
            {
                Debug.Fail("Failed to extract connection!");
                return(null);
            }

            try
            {
                // Execute drop query
                connection.ExecuteScalar(dropQuery);

                // Drop hierarchy node
                hierarchy.DropObjectNode(item);

                // Notify everybody about object dropping
                connection.ObjectChangeEvents.RaiseObjectRemoved(typeName, identifier);
            }
            catch (DbException e)
            {
                Trace.TraceError("Error during saving object:\n{0}", e.ToString());
                SqlErrorDialog.ShowError(e, connection.GetFullStatus());
            }
            catch (Exception e)
            {
                Trace.TraceError("Error during saving object:\n{0}", e.ToString());
                UIHelper.ShowError(e);
            }

            return(null);
        }