//
// Copyright (c) Microsoft. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
//
// This code is copied from the source described in the comment below.
// =======================================================================================
// Microsoft Windows Server AppFabric Customer Advisory Team (CAT) Best Practices Series
//
// This sample is supplemental to the technical guidance published on the community
// blog at http://blogs.msdn.com/appfabriccat/ and copied from
// sqlmain ./sql/manageability/mfx/common/
//
// =======================================================================================
// Copyright © 2012 Microsoft Corporation. All rights reserved.
//
// THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER
// EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED WARRANTIES OF
// MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. YOU BEAR THE RISK OF USING IT.
// =======================================================================================
// namespace Microsoft.AppFabricCAT.Samples.Azure.TransientFaultHandling.SqlAzure
// namespace Microsoft.SqlServer.Management.Common
using System;
using System.Data;
using System.Data.Common;
using Microsoft.Data.SqlClient;
using System.Diagnostics.Contracts;
namespace Microsoft.SqlTools.CoreServices.Connection.ReliableConnection
{
///
/// Provides a reliable way of opening connections to and executing commands
/// taking into account potential network unreliability and a requirement for connection retry.
///
internal sealed partial class ReliableSqlConnection
{
internal class ReliableSqlCommand : DbCommand
{
private const int Dummy = 0;
private readonly SqlCommand _command;
// connection is settable
private ReliableSqlConnection _connection;
public ReliableSqlCommand()
: this(null, Dummy)
{
}
public ReliableSqlCommand(ReliableSqlConnection connection)
: this(connection, Dummy)
{
Contract.Requires(connection != null);
}
private ReliableSqlCommand(ReliableSqlConnection connection, int dummy)
{
if (connection != null)
{
_connection = connection;
_command = connection.CreateSqlCommand();
}
else
{
_command = new SqlCommand();
}
}
protected override void Dispose(bool disposing)
{
if (disposing)
{
_command.Dispose();
}
}
///
/// Gets or sets the text command to run against the data source.
///
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Security", "CA2100:Review SQL queries for security vulnerabilities")]
public override string CommandText
{
get { return _command.CommandText; }
set { _command.CommandText = value; }
}
///
/// Gets or sets the wait time before terminating the attempt to execute a command and generating an error.
///
public override int CommandTimeout
{
get { return _command.CommandTimeout; }
set { _command.CommandTimeout = value; }
}
///
/// Gets or sets a value that specifies how the property is interpreted.
///
public override CommandType CommandType
{
get { return _command.CommandType; }
set { _command.CommandType = value; }
}
///
/// Gets or sets the used by this .
///
protected override DbConnection DbConnection
{
get
{
return _connection;
}
set
{
if (value == null)
{
throw new ArgumentNullException("value");
}
ReliableSqlConnection newConnection = value as ReliableSqlConnection;
if (newConnection == null)
{
throw new InvalidOperationException(Resources.OnlyReliableConnectionSupported);
}
_connection = newConnection;
_command.Connection = _connection._underlyingConnection;
}
}
///
/// Gets the .
///
protected override DbParameterCollection DbParameterCollection
{
get { return _command.Parameters; }
}
///
/// Gets or sets the transaction within which the Command object of a .NET Framework data provider executes.
///
protected override DbTransaction DbTransaction
{
get { return _command.Transaction; }
set { _command.Transaction = value as SqlTransaction; }
}
///
/// Gets or sets a value indicating whether the command object should be visible in a customized interface control.
///
public override bool DesignTimeVisible
{
get { return _command.DesignTimeVisible; }
set { _command.DesignTimeVisible = value; }
}
///
/// Gets or sets how command results are applied to the System.Data.DataRow when
/// used by the System.Data.IDataAdapter.Update(System.Data.DataSet) method of
/// a .
///
public override UpdateRowSource UpdatedRowSource
{
get { return _command.UpdatedRowSource; }
set { _command.UpdatedRowSource = value; }
}
///
/// Attempts to cancels the execution of an .
///
public override void Cancel()
{
_command.Cancel();
}
///
/// Creates a new instance of an object.
///
/// An object.
protected override DbParameter CreateDbParameter()
{
return _command.CreateParameter();
}
///
/// Executes an SQL statement against the Connection object of a .NET Framework
/// data provider, and returns the number of rows affected.
///
/// The number of rows affected.
public override int ExecuteNonQuery()
{
ValidateConnectionIsSet();
return _connection.ExecuteNonQuery(_command);
}
///
/// Executes the against the
/// and builds an using one of the values.
///
/// One of the values.
/// An object.
protected override DbDataReader ExecuteDbDataReader(CommandBehavior behavior)
{
ValidateConnectionIsSet();
return (DbDataReader)_connection.ExecuteReader(_command, behavior);
}
///
/// Executes the query, and returns the first column of the first row in the
/// resultset returned by the query. Extra columns or rows are ignored.
///
/// The first column of the first row in the resultset.
public override object ExecuteScalar()
{
ValidateConnectionIsSet();
return _connection.ExecuteScalar(_command);
}
///
/// Creates a prepared (or compiled) version of the command on the data source.
///
public override void Prepare()
{
_command.Prepare();
}
internal SqlCommand GetUnderlyingCommand()
{
return _command;
}
internal void ValidateConnectionIsSet()
{
if (_connection == null)
{
throw new InvalidOperationException(Resources.ConnectionPropertyNotSet);
}
}
}
}
}