/// <summary>
        /// Process the request using the provided connection context
        /// </summary>
        /// <param name="databaseName">the name of the database to retrieve</param>
        private void ProcessWithContext(IServerDataServiceContext context)
        {
            string databaseName = GetDatabaseName();

            try
            {
                if (databaseName != null)
                {
                    // Retrieve the database with the specified name
                    this.WriteObject(context.GetDatabase(databaseName), true);
                }
                else
                {
                    // No name specified, retrieve all databases in the server
                    this.WriteObject(context.GetDatabases(), true);
                }
            }
            catch (Exception ex)
            {
                SqlDatabaseExceptionHandler.WriteErrorDetails(
                    this,
                    context.ClientRequestId,
                    ex);
            }
        }
        /// <summary>
        /// Queries the server until the database assignment succeeds or there is an error.
        /// </summary>
        /// <param name="context">The context upon which to perform the action</param>
        /// <param name="response">The database object.</param>
        /// <returns>Returns the response from the server</returns>
        internal static Services.Server.Database WaitForDatabaseOperation(PSCmdlet cmdlet, IServerDataServiceContext context, Services.Server.Database response, string databaseName, bool isCreate)
        {
            // Duration to sleep: 2 second
            TimeSpan sleepDuration = TimeSpan.FromSeconds(2.0);

            // Poll for a maximum of 10 minutes;
            TimeSpan maximumPollDuration = TimeSpan.FromMinutes(10.0);

            // Text to display to the user while they wait.
            string pendingText   = "Pending";
            string textToDisplay = "";

            // Start the timer
            Stopwatch watch = Stopwatch.StartNew();

            while (watch.Elapsed < maximumPollDuration)
            {
                if (response == null)
                {
                    throw new Exception("An unexpected error occured. The response from the server was invalid, please try your request again.");
                }

                // Check to see if the database is ready for use.
                if ((isCreate && (response.Status != (int)DatabaseStatus.Creating)) || // The database is done being created
                    (!isCreate && (response.ServiceObjectiveAssignmentState != 0)))    // The database is done with SLO upgrade
                {
                    break;
                }

                // Wait before next poll.
                Thread.Sleep(sleepDuration);

                // Display that the status is pending and how long the operation has been waiting
                textToDisplay = string.Format("{0}: {1}", pendingText, watch.Elapsed.ToString("%s' sec.'"));
                cmdlet.WriteProgress(new ProgressRecord(0, "Waiting for database creation completion.", textToDisplay));

                // Poll the server for the database status.
                response = context.GetDatabase(databaseName);
            }

            return(response);
        }
        /// <summary>
        /// Queries the server until the database assignment succeeds or there is an error.
        /// </summary>
        /// <param name="context">The context upon which to perform the action</param>
        /// <param name="response">The database object.</param>
        /// <returns>Returns the response from the server</returns>
        internal static Database WaitForDatabaseOperation(PSCmdlet cmdlet, IServerDataServiceContext context, Database response, string databaseName, bool isCreate)
        {
            // Duration to sleep: 2 second
            TimeSpan sleepDuration = TimeSpan.FromSeconds(2.0);

            // Poll for a maximum of 10 minutes;
            TimeSpan maximumPollDuration = TimeSpan.FromMinutes(10.0);

            // Text to display to the user while they wait.
            string pendingText = "Pending";
            string textToDisplay = "";

            // Start the timer
            Stopwatch watch = Stopwatch.StartNew();

            while (watch.Elapsed < maximumPollDuration)
            {
                if (response == null)
                {
                    throw new Exception("An unexpected error occured. The response from the server was invalid, please try your request again.");
                }

                // Check to see if the database is ready for use.
                if ((isCreate && (response.Status != (int)DatabaseStatus.Creating)) || // The database is done being created
                    (!isCreate && (response.ServiceObjectiveAssignmentState != 0)))     // The database is done with SLO upgrade
                {
                    break;
                }

                // Wait before next poll.
                Thread.Sleep(sleepDuration);

                // Display that the status is pending and how long the operation has been waiting
                textToDisplay = string.Format("{0}: {1}", pendingText, watch.Elapsed.ToString("%s' sec.'"));
                cmdlet.WriteProgress(new ProgressRecord(0, "Waiting for database creation completion.", textToDisplay));

                // Poll the server for the database status.
                response = context.GetDatabase(databaseName);
            }

            return response;
        }
        /// <summary>
        /// Process the request using the provided connection context
        /// </summary>
        /// <param name="context">The connection context</param>
        private void ProcessWithContext(IServerDataServiceContext context)
        {
            // This is to enforce the mutual exclusivity of the parameters: Database
            // and DatabaseName.  This can't be done with parameter sets without changing
            // existing behaviour of the cmdlet.
            if (
                this.MyInvocation.BoundParameters.ContainsKey(DatabaseParameter) &&
                this.MyInvocation.BoundParameters.ContainsKey(DatabaseNameParameter))
            {
                this.WriteError(new ErrorRecord(
                    new PSArgumentException(
                        string.Format(Resources.InvalidParameterCombination, DatabaseParameter, DatabaseNameParameter)),
                    string.Empty,
                    ErrorCategory.InvalidArgument,
                    null));
            }

            // ... and similarly for RestorableDroppedDatabase and DatabaseName / DatabaseDeletionDate
            if (
                this.MyInvocation.BoundParameters.ContainsKey(RestorableDroppedDatabaseParameter) &&
                this.MyInvocation.BoundParameters.ContainsKey(DatabaseNameParameter))
            {
                this.WriteError(new ErrorRecord(
                    new PSArgumentException(
                        string.Format(Resources.InvalidParameterCombination, RestorableDroppedDatabaseParameter, DatabaseNameParameter)),
                    string.Empty,
                    ErrorCategory.InvalidArgument,
                    null));
            }
            if (
                this.MyInvocation.BoundParameters.ContainsKey(RestorableDroppedDatabaseParameter) &&
                this.MyInvocation.BoundParameters.ContainsKey(DatabaseDeletionDateParameter))
            {
                this.WriteError(new ErrorRecord(
                    new PSArgumentException(
                        string.Format(Resources.InvalidParameterCombination, RestorableDroppedDatabaseParameter, DatabaseDeletionDateParameter)),
                    string.Empty,
                    ErrorCategory.InvalidArgument,
                    null));
            }

            // The DatabaseDeletionDate parameter can only be used if the RestorableDropped switch is also present
            if (!this.RestorableDropped.IsPresent && this.MyInvocation.BoundParameters.ContainsKey(DatabaseDeletionDateParameter))
            {
                throw new PSArgumentException(
                    string.Format(Resources.RestorableDroppedSwitchNotSpecified, DatabaseDeletionDateParameter));
            }

            // The Database parameter can only be used if the RestorableDropped switch is not present
            if (this.RestorableDropped.IsPresent && this.MyInvocation.BoundParameters.ContainsKey(DatabaseParameter))
            {
                throw new PSArgumentException(
                    string.Format(Resources.InvalidParameterCombination, RestorableDroppedParameter, DatabaseParameter));
            }

            // If the RestorableDropped switch is present, then either both the DatabaseName and DatabaseDeletionDate parameters must be present, or neither of them should be present.
            if (
                this.RestorableDropped.IsPresent && (
                    (this.MyInvocation.BoundParameters.ContainsKey(DatabaseNameParameter) && !this.MyInvocation.BoundParameters.ContainsKey(DatabaseDeletionDateParameter)) ||
                    (!this.MyInvocation.BoundParameters.ContainsKey(DatabaseNameParameter) && this.MyInvocation.BoundParameters.ContainsKey(DatabaseDeletionDateParameter))))
            {
                throw new PSArgumentException(Resources.BothDatabaseNameAndDeletionDateNeedToBeSpecified);
            }

            // Obtain the database name from the given parameters.
            string databaseName = null;
            if (this.MyInvocation.BoundParameters.ContainsKey(DatabaseParameter))
            {
                databaseName = this.Database.Name;
            }
            else if (this.MyInvocation.BoundParameters.ContainsKey(RestorableDroppedDatabaseParameter))
            {
                databaseName = this.RestorableDroppedDatabase.Name;
            }
            else if (this.MyInvocation.BoundParameters.ContainsKey(DatabaseNameParameter))
            {
                databaseName = this.DatabaseName;
            }

            DateTime databaseDeletionDate = default(DateTime);
            if (this.MyInvocation.BoundParameters.ContainsKey(RestorableDroppedDatabaseParameter))
            {
                databaseDeletionDate = this.RestorableDroppedDatabase.DeletionDate;
            }
            else if (this.MyInvocation.BoundParameters.ContainsKey(DatabaseDeletionDateParameter))
            {
                databaseDeletionDate = this.DatabaseDeletionDate;
            }
            databaseDeletionDate = CmdletCommon.NormalizeToUtc(databaseDeletionDate);

            try
            {
                if (!this.RestorableDropped.IsPresent && this.RestorableDroppedDatabase == null)
                {
                    // Live databases

                    if (databaseName != null)
                    {
                        // Retrieve the database with the specified name
                        this.WriteObject(context.GetDatabase(databaseName), true);
                    }
                    else
                    {
                        // No name specified, retrieve all databases in the server
                        this.WriteObject(context.GetDatabases(), true);
                    }
                }

                else
                {
                    // Dropped databases

                    if (databaseName != null)
                    {
                        // Retrieve the database with the specified name
                        this.WriteObject(context.GetRestorableDroppedDatabase(databaseName, databaseDeletionDate), true);
                    }
                    else
                    {
                        // No name specified, retrieve all databases in the server
                        this.WriteObject(context.GetRestorableDroppedDatabases(), true);
                    }
                }
            }
            catch (Exception ex)
            {
                SqlDatabaseExceptionHandler.WriteErrorDetails(
                    this,
                    context.ClientRequestId,
                    ex);
            }
        }
 /// <summary>
 /// Process the request using the provided connection context
 /// </summary>
 /// <param name="databaseName">the name of the database to retrieve</param>
 private void ProcessWithContext(IServerDataServiceContext context)
 {
     string databaseName = GetDatabaseName();
     try
     {
         if (databaseName != null)
         {
             // Retrieve the database with the specified name
             this.WriteObject(context.GetDatabase(databaseName), true);
         }
         else
         {
             // No name specified, retrieve all databases in the server
             this.WriteObject(context.GetDatabases(), true);
         }
     }
     catch (Exception ex)
     {
         SqlDatabaseExceptionHandler.WriteErrorDetails(
             this,
             context.ClientRequestId,
             ex);
     }
 }
        /// <summary>
        /// Process the request using the provided connection context
        /// </summary>
        /// <param name="context">The connection context</param>
        private void ProcessWithContext(IServerDataServiceContext context)
        {
            // This is to enforce the mutual exclusivity of the parameters: Database
            // and DatabaseName.  This can't be done with parameter sets without changing
            // existing behaviour of the cmdlet.
            if (
                this.MyInvocation.BoundParameters.ContainsKey(DatabaseParameter) &&
                this.MyInvocation.BoundParameters.ContainsKey(DatabaseNameParameter))
            {
                this.WriteError(new ErrorRecord(
                                    new PSArgumentException(
                                        string.Format(Resources.InvalidParameterCombination, DatabaseParameter, DatabaseNameParameter)),
                                    string.Empty,
                                    ErrorCategory.InvalidArgument,
                                    null));
            }

            // ... and similarly for RestorableDroppedDatabase and DatabaseName / DatabaseDeletionDate
            if (
                this.MyInvocation.BoundParameters.ContainsKey(RestorableDroppedDatabaseParameter) &&
                this.MyInvocation.BoundParameters.ContainsKey(DatabaseNameParameter))
            {
                this.WriteError(new ErrorRecord(
                                    new PSArgumentException(
                                        string.Format(Resources.InvalidParameterCombination, RestorableDroppedDatabaseParameter, DatabaseNameParameter)),
                                    string.Empty,
                                    ErrorCategory.InvalidArgument,
                                    null));
            }
            if (
                this.MyInvocation.BoundParameters.ContainsKey(RestorableDroppedDatabaseParameter) &&
                this.MyInvocation.BoundParameters.ContainsKey(DatabaseDeletionDateParameter))
            {
                this.WriteError(new ErrorRecord(
                                    new PSArgumentException(
                                        string.Format(Resources.InvalidParameterCombination, RestorableDroppedDatabaseParameter, DatabaseDeletionDateParameter)),
                                    string.Empty,
                                    ErrorCategory.InvalidArgument,
                                    null));
            }

            // The DatabaseDeletionDate parameter can only be used if the RestorableDropped switch is also present
            if (!this.RestorableDropped.IsPresent && this.MyInvocation.BoundParameters.ContainsKey(DatabaseDeletionDateParameter))
            {
                throw new PSArgumentException(
                          string.Format(Resources.RestorableDroppedSwitchNotSpecified, DatabaseDeletionDateParameter));
            }

            // The Database parameter can only be used if the RestorableDropped switch is not present
            if (this.RestorableDropped.IsPresent && this.MyInvocation.BoundParameters.ContainsKey(DatabaseParameter))
            {
                throw new PSArgumentException(
                          string.Format(Resources.InvalidParameterCombination, RestorableDroppedParameter, DatabaseParameter));
            }

            // If the RestorableDropped switch is present, then either both the DatabaseName and DatabaseDeletionDate parameters must be present, or neither of them should be present.
            if (
                this.RestorableDropped.IsPresent && (
                    (this.MyInvocation.BoundParameters.ContainsKey(DatabaseNameParameter) && !this.MyInvocation.BoundParameters.ContainsKey(DatabaseDeletionDateParameter)) ||
                    (!this.MyInvocation.BoundParameters.ContainsKey(DatabaseNameParameter) && this.MyInvocation.BoundParameters.ContainsKey(DatabaseDeletionDateParameter))))
            {
                throw new PSArgumentException(Resources.BothDatabaseNameAndDeletionDateNeedToBeSpecified);
            }

            // Obtain the database name from the given parameters.
            string databaseName = null;

            if (this.MyInvocation.BoundParameters.ContainsKey(DatabaseParameter))
            {
                databaseName = this.Database.Name;
            }
            else if (this.MyInvocation.BoundParameters.ContainsKey(RestorableDroppedDatabaseParameter))
            {
                databaseName = this.RestorableDroppedDatabase.Name;
            }
            else if (this.MyInvocation.BoundParameters.ContainsKey(DatabaseNameParameter))
            {
                databaseName = this.DatabaseName;
            }

            DateTime databaseDeletionDate = default(DateTime);

            if (this.MyInvocation.BoundParameters.ContainsKey(RestorableDroppedDatabaseParameter))
            {
                databaseDeletionDate = this.RestorableDroppedDatabase.DeletionDate;
            }
            else if (this.MyInvocation.BoundParameters.ContainsKey(DatabaseDeletionDateParameter))
            {
                databaseDeletionDate = this.DatabaseDeletionDate;
            }
            databaseDeletionDate = CmdletCommon.NormalizeToUtc(databaseDeletionDate);

            try
            {
                if (!this.RestorableDropped.IsPresent && this.RestorableDroppedDatabase == null)
                {
                    // Live databases

                    if (databaseName != null)
                    {
                        // Retrieve the database with the specified name
                        this.WriteObject(context.GetDatabase(databaseName), true);
                    }
                    else
                    {
                        // No name specified, retrieve all databases in the server
                        this.WriteObject(context.GetDatabases(), true);
                    }
                }

                else
                {
                    // Dropped databases

                    if (databaseName != null)
                    {
                        // Retrieve the database with the specified name
                        this.WriteObject(context.GetRestorableDroppedDatabase(databaseName, databaseDeletionDate), true);
                    }
                    else
                    {
                        // No name specified, retrieve all databases in the server
                        this.WriteObject(context.GetRestorableDroppedDatabases(), true);
                    }
                }
            }
            catch (Exception ex)
            {
                SqlDatabaseExceptionHandler.WriteErrorDetails(
                    this,
                    context.ClientRequestId,
                    ex);
            }
        }