Exemplo n.º 1
0
        /// <summary>
        /// Enumerates items for a set of data objects of the specified type
        /// with the specified restrictions and sort string, if supported.
        /// </summary>
        /// <param name="typeName">Name of the type of the object to enumerate.</param>
        /// <param name="items">
        /// The set of items to enumerate, specified as strings where named items are available,
        /// otherwise as indexes. In cases in which a data provider does not support items
        /// filtering, this parameter is ignored.
        /// NOT SUPPORTED.
        /// </param>
        /// <param name="restrictions">
        /// A set of filtering restrictions to apply to the set of returned objects.
        /// </param>
        /// <param name="sort">
        /// A sort string, which follows syntax for the SQL Server ORDER BY clause.
        /// The actual sort order should be source-based; that is, if the client is
        /// English and the source is Chinese, the sort should be applied in Chinese.
        /// NOT SUPPORTED.
        /// </param>
        /// <param name="parameters">
        /// An array whose contents are defined by the given implementation of
        /// EnumerateObjects, and which is specified by the Data Object Support
        /// XML file. Information supplied in this parameter can be used to provide
        /// extra data indicating how to perform the enumeration, allowing
        /// implementations of this method to be more data driven.
        /// NOT USED.
        /// </param>
        /// <returns>
        /// Returns a DataReader object containing the results of the enumeration call.
        /// </returns>
        public override DataReader EnumerateObjects(
            string typeName,
            object[] items,
            object[] restrictions,
            string sort,
            object[] parameters)
        {
            if (typeName == null)
            {
                throw new ArgumentNullException("typeName");
            }

            // Chose restricitions array
            object[] appliedRestrictions;
            if (typeName.Equals(RootDescriptor.TypeName, StringComparison.InvariantCultureIgnoreCase))
            {
                appliedRestrictions = new object[] {
                    ConnectionWrapper.ServerName,
                    ConnectionWrapper.Schema
                };
            }
            else
            {
                appliedRestrictions = restrictions;
            }

            DataTable table;

            try
            {
                // Enumerate objects into table
                table = ObjectDescriptor.EnumerateObjects(ConnectionWrapper, typeName, appliedRestrictions, sort);
            }
            catch (DbException e)
            {
                SqlErrorDialog.ShowError(e, ConnectionWrapper.GetFullStatus());
                throw;
            }
            catch (Exception e)
            {
                UIHelper.ShowError(e);
                throw;
            }

            // Validate table
            if (table == null)
            {
                Debug.Fail("Failed to enumerate objects of type '" + typeName + "'!");
                return(null);
            }

            // Enumerete objects in to DataReader
            return(new AdoDotNetDataTableReader(table));
        }
Exemplo n.º 2
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);
        }