Code:
/ 4.0 / 4.0 / DEVDIV_TFS / Dev10 / Releases / RTMRel / ndp / fx / src / DataEntity / System / Data / Common / Utils / CommandHelper.cs / 1305376 / CommandHelper.cs
//----------------------------------------------------------------------
//
// Copyright (c) Microsoft Corporation. All rights reserved.
//
//
// @owner [....]
// @backupOwner [....]
//---------------------------------------------------------------------
using System.Diagnostics;
using System.Data.Metadata.Edm;
using System.Data.EntityClient;
using System.Data.Objects;
namespace System.Data.Common.Utils
{
///
/// Contains utility methods for construction of DB commands through generic
/// provider interfaces.
///
internal static class CommandHelper
{
///
/// Consumes all rows and result sets from the reader. This allows client to retrieve
/// parameter values and intercept any store exceptions.
///
/// reader to consume
internal static void ConsumeReader(DbDataReader reader)
{
if (null != reader && !reader.IsClosed)
{
while (reader.NextResult())
{
// Note that we only walk through the result sets. We don't need
// to walk through individual rows (though underlying provider
// implementation may do so)
}
}
}
///
/// requires: commandText must not be null
/// The command text must be in the form Container.FunctionImportName.
///
internal static void ParseFunctionImportCommandText(string commandText, string defaultContainerName, out string containerName, out string functionImportName)
{
Debug.Assert(null != commandText);
// Split the string
string[] nameParts = commandText.Split('.');
containerName = null;
functionImportName = null;
if (2 == nameParts.Length)
{
containerName = nameParts[0].Trim();
functionImportName = nameParts[1].Trim();
}
else if (1 == nameParts.Length && null != defaultContainerName)
{
containerName = defaultContainerName;
functionImportName = nameParts[0].Trim();
}
if (string.IsNullOrEmpty(containerName) || string.IsNullOrEmpty(functionImportName))
{
throw EntityUtil.InvalidOperation(System.Data.Entity.Strings.EntityClient_InvalidStoredProcedureCommandText);
}
}
///
/// Given an entity command, returns the associated entity transaction and performs validation
/// to ensure the transaction is consistent.
///
/// Entity command instance. Must not be null.
/// Entity transaction
internal static EntityTransaction GetEntityTransaction(EntityCommand entityCommand)
{
Debug.Assert(null != entityCommand);
EntityTransaction entityTransaction = (EntityTransaction)entityCommand.Transaction;
// Check to make sure that either the command has no transaction associated with it, or it
// matches the one used by the connection
if (entityTransaction != null && entityTransaction != entityCommand.Connection.CurrentTransaction)
{
throw EntityUtil.InvalidOperation(System.Data.Entity.Strings.EntityClient_InvalidTransactionForCommand);
}
// Now we have asserted that EntityCommand either has no transaction or has one that matches the
// one used in the connection, we can simply use the connection's transaction object
entityTransaction = entityCommand.Connection.CurrentTransaction;
return entityTransaction;
}
///
/// Given an entity command and entity transaction, passes through relevant state to store provider
/// command.
///
/// Entity command. Must not be null.
/// Entity transaction. Must not be null.
/// Store provider command that is being setup. Must not be null.
internal static void SetStoreProviderCommandState(EntityCommand entityCommand, EntityTransaction entityTransaction, DbCommand storeProviderCommand)
{
Debug.Assert(null != entityCommand);
Debug.Assert(null != storeProviderCommand);
storeProviderCommand.CommandTimeout = entityCommand.CommandTimeout;
storeProviderCommand.Connection = ((EntityConnection)entityCommand.Connection).StoreConnection;
storeProviderCommand.Transaction = (null != entityTransaction) ? entityTransaction.StoreTransaction : null;
storeProviderCommand.UpdatedRowSource = entityCommand.UpdatedRowSource;
}
///
/// Given an entity command and store provider command, sets all output parameter values on the entity command.
///
/// Entity command on which to set parameter values. Must not be null.
/// Store provider command from which to retrieve parameter values. Must not
/// be null.
internal static void SetEntityParameterValues(EntityCommand entityCommand, DbCommand storeProviderCommand)
{
Debug.Assert(null != entityCommand);
Debug.Assert(null != storeProviderCommand);
foreach (DbParameter storeParameter in storeProviderCommand.Parameters)
{
ParameterDirection direction = storeParameter.Direction;
if (0 != (direction & ParameterDirection.Output))
{
// if the entity command also defines the parameter, propagate store parameter value
// to entity parameter
int parameterOrdinal = entityCommand.Parameters.IndexOf(storeParameter.ParameterName);
if (0 <= parameterOrdinal)
{
EntityParameter entityParameter = entityCommand.Parameters[parameterOrdinal];
entityParameter.Value = storeParameter.Value;
}
}
}
}
// requires: all arguments must be given
internal static EdmFunction FindFunctionImport(MetadataWorkspace workspace, string containerName, string functionImportName)
{
Debug.Assert(null != workspace && null != containerName && null != functionImportName);
// find entity container
EntityContainer entityContainer;
if (!workspace.TryGetEntityContainer(containerName, DataSpace.CSpace, out entityContainer))
{
throw EntityUtil.InvalidOperation(System.Data.Entity.Strings.EntityClient_UnableToFindFunctionImportContainer(
containerName));
}
// find function import
EdmFunction functionImport = null;
foreach (EdmFunction candidate in entityContainer.FunctionImports)
{
if (candidate.Name == functionImportName)
{
functionImport = candidate;
break;
}
}
if (null == functionImport)
{
throw EntityUtil.InvalidOperation(System.Data.Entity.Strings.EntityClient_UnableToFindFunctionImport(
containerName, functionImportName));
}
return functionImport;
}
}
}
// File provided for Reference Use Only by Microsoft Corporation (c) 2007.
//----------------------------------------------------------------------
//
// Copyright (c) Microsoft Corporation. All rights reserved.
//
//
// @owner [....]
// @backupOwner [....]
//---------------------------------------------------------------------
using System.Diagnostics;
using System.Data.Metadata.Edm;
using System.Data.EntityClient;
using System.Data.Objects;
namespace System.Data.Common.Utils
{
///
/// Contains utility methods for construction of DB commands through generic
/// provider interfaces.
///
internal static class CommandHelper
{
///
/// Consumes all rows and result sets from the reader. This allows client to retrieve
/// parameter values and intercept any store exceptions.
///
/// reader to consume
internal static void ConsumeReader(DbDataReader reader)
{
if (null != reader && !reader.IsClosed)
{
while (reader.NextResult())
{
// Note that we only walk through the result sets. We don't need
// to walk through individual rows (though underlying provider
// implementation may do so)
}
}
}
///
/// requires: commandText must not be null
/// The command text must be in the form Container.FunctionImportName.
///
internal static void ParseFunctionImportCommandText(string commandText, string defaultContainerName, out string containerName, out string functionImportName)
{
Debug.Assert(null != commandText);
// Split the string
string[] nameParts = commandText.Split('.');
containerName = null;
functionImportName = null;
if (2 == nameParts.Length)
{
containerName = nameParts[0].Trim();
functionImportName = nameParts[1].Trim();
}
else if (1 == nameParts.Length && null != defaultContainerName)
{
containerName = defaultContainerName;
functionImportName = nameParts[0].Trim();
}
if (string.IsNullOrEmpty(containerName) || string.IsNullOrEmpty(functionImportName))
{
throw EntityUtil.InvalidOperation(System.Data.Entity.Strings.EntityClient_InvalidStoredProcedureCommandText);
}
}
///
/// Given an entity command, returns the associated entity transaction and performs validation
/// to ensure the transaction is consistent.
///
/// Entity command instance. Must not be null.
/// Entity transaction
internal static EntityTransaction GetEntityTransaction(EntityCommand entityCommand)
{
Debug.Assert(null != entityCommand);
EntityTransaction entityTransaction = (EntityTransaction)entityCommand.Transaction;
// Check to make sure that either the command has no transaction associated with it, or it
// matches the one used by the connection
if (entityTransaction != null && entityTransaction != entityCommand.Connection.CurrentTransaction)
{
throw EntityUtil.InvalidOperation(System.Data.Entity.Strings.EntityClient_InvalidTransactionForCommand);
}
// Now we have asserted that EntityCommand either has no transaction or has one that matches the
// one used in the connection, we can simply use the connection's transaction object
entityTransaction = entityCommand.Connection.CurrentTransaction;
return entityTransaction;
}
///
/// Given an entity command and entity transaction, passes through relevant state to store provider
/// command.
///
/// Entity command. Must not be null.
/// Entity transaction. Must not be null.
/// Store provider command that is being setup. Must not be null.
internal static void SetStoreProviderCommandState(EntityCommand entityCommand, EntityTransaction entityTransaction, DbCommand storeProviderCommand)
{
Debug.Assert(null != entityCommand);
Debug.Assert(null != storeProviderCommand);
storeProviderCommand.CommandTimeout = entityCommand.CommandTimeout;
storeProviderCommand.Connection = ((EntityConnection)entityCommand.Connection).StoreConnection;
storeProviderCommand.Transaction = (null != entityTransaction) ? entityTransaction.StoreTransaction : null;
storeProviderCommand.UpdatedRowSource = entityCommand.UpdatedRowSource;
}
///
/// Given an entity command and store provider command, sets all output parameter values on the entity command.
///
/// Entity command on which to set parameter values. Must not be null.
/// Store provider command from which to retrieve parameter values. Must not
/// be null.
internal static void SetEntityParameterValues(EntityCommand entityCommand, DbCommand storeProviderCommand)
{
Debug.Assert(null != entityCommand);
Debug.Assert(null != storeProviderCommand);
foreach (DbParameter storeParameter in storeProviderCommand.Parameters)
{
ParameterDirection direction = storeParameter.Direction;
if (0 != (direction & ParameterDirection.Output))
{
// if the entity command also defines the parameter, propagate store parameter value
// to entity parameter
int parameterOrdinal = entityCommand.Parameters.IndexOf(storeParameter.ParameterName);
if (0 <= parameterOrdinal)
{
EntityParameter entityParameter = entityCommand.Parameters[parameterOrdinal];
entityParameter.Value = storeParameter.Value;
}
}
}
}
// requires: all arguments must be given
internal static EdmFunction FindFunctionImport(MetadataWorkspace workspace, string containerName, string functionImportName)
{
Debug.Assert(null != workspace && null != containerName && null != functionImportName);
// find entity container
EntityContainer entityContainer;
if (!workspace.TryGetEntityContainer(containerName, DataSpace.CSpace, out entityContainer))
{
throw EntityUtil.InvalidOperation(System.Data.Entity.Strings.EntityClient_UnableToFindFunctionImportContainer(
containerName));
}
// find function import
EdmFunction functionImport = null;
foreach (EdmFunction candidate in entityContainer.FunctionImports)
{
if (candidate.Name == functionImportName)
{
functionImport = candidate;
break;
}
}
if (null == functionImport)
{
throw EntityUtil.InvalidOperation(System.Data.Entity.Strings.EntityClient_UnableToFindFunctionImport(
containerName, functionImportName));
}
return functionImport;
}
}
}
// File provided for Reference Use Only by Microsoft Corporation (c) 2007.
Link Menu

This book is available now!
Buy at Amazon US or
Buy at Amazon UK
- PenThreadPool.cs
- assemblycache.cs
- ColumnResult.cs
- XamlBuildTaskServices.cs
- XmlSchemaObjectCollection.cs
- InProcStateClientManager.cs
- UrlPath.cs
- ToolStripDropDown.cs
- CodeMethodReturnStatement.cs
- SafeCryptContextHandle.cs
- SchemaComplexType.cs
- XmlBoundElement.cs
- DataControlHelper.cs
- SettingsContext.cs
- LogEntryHeaderv1Deserializer.cs
- ApplicationTrust.cs
- AppSettingsReader.cs
- RequestQueryProcessor.cs
- EventItfInfo.cs
- ClientTargetCollection.cs
- DateTimeValueSerializerContext.cs
- InstanceData.cs
- PtsHelper.cs
- MeasureData.cs
- ConfigXmlComment.cs
- _ConnectStream.cs
- DesignerRegionMouseEventArgs.cs
- SHA512Managed.cs
- Application.cs
- SaveWorkflowAsyncResult.cs
- WebPartConnectionsCloseVerb.cs
- StatusStrip.cs
- DbTransaction.cs
- PresentationSource.cs
- TextProperties.cs
- WebPartTransformer.cs
- tooltip.cs
- HostDesigntimeLicenseContext.cs
- InputScopeNameConverter.cs
- ChunkedMemoryStream.cs
- FormViewPageEventArgs.cs
- Simplifier.cs
- MailAddress.cs
- DivideByZeroException.cs
- DbConnectionClosed.cs
- Tuple.cs
- UIElementAutomationPeer.cs
- OdbcPermission.cs
- DescendantOverDescendantQuery.cs
- ServicesExceptionNotHandledEventArgs.cs
- SmtpSection.cs
- XPathNodeIterator.cs
- GlyphTypeface.cs
- Page.cs
- TraceSection.cs
- BaseServiceProvider.cs
- FrameworkContextData.cs
- ContentElementAutomationPeer.cs
- VirtualPath.cs
- VariableAction.cs
- FixedPageAutomationPeer.cs
- ManualResetEvent.cs
- Encoder.cs
- AppendHelper.cs
- ClientTargetCollection.cs
- DecimalAnimationUsingKeyFrames.cs
- SchemaImporterExtension.cs
- ClientWindowsAuthenticationMembershipProvider.cs
- MutexSecurity.cs
- Switch.cs
- EventLogRecord.cs
- Socket.cs
- IResourceProvider.cs
- CustomErrorsSection.cs
- UidManager.cs
- ResolveNameEventArgs.cs
- ValidateNames.cs
- SecUtil.cs
- XMLUtil.cs
- ListDictionary.cs
- HtmlHead.cs
- ConfigurationConverterBase.cs
- MaskedTextBoxDesignerActionList.cs
- CRYPTPROTECT_PROMPTSTRUCT.cs
- PropertyMappingExceptionEventArgs.cs
- RC2CryptoServiceProvider.cs
- ComNativeDescriptor.cs
- HMACSHA384.cs
- ChtmlFormAdapter.cs
- DataAdapter.cs
- ControlBuilder.cs
- cookieexception.cs
- DetailsViewUpdatedEventArgs.cs
- SqlMethodTransformer.cs
- SimpleWorkerRequest.cs
- CryptographicAttribute.cs
- DoubleLinkList.cs
- SystemBrushes.cs
- SafeSecurityHelper.cs
- BamlResourceContent.cs