Code:
/ Net / Net / 3.5.50727.3053 / DEVDIV / depot / DevDiv / releases / Orcas / SP / ndp / fx / src / DataEntity / System / Data / Common / DbCommandDefinition.cs / 3 / DbCommandDefinition.cs
//------------------------------------------------------------------------------
//
// Copyright (c) Microsoft Corporation. All rights reserved.
//
//
// @owner [....], [....]
//-----------------------------------------------------------------------------
using System.Diagnostics;
using System.Data.Metadata.Edm;
namespace System.Data.Common {
///
/// A prepared command definition, can be cached and reused to avoid
/// repreparing a command.
///
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Naming", "CA1709:IdentifiersShouldBeCasedCorrectly", MessageId = "Db")]
public class DbCommandDefinition {
private readonly ICloneable _prototype;
///
/// Internal factory method to create the default Command Definition object
/// based on a prototype command. The prototype command is cloned
/// before the protected constructor is invoked
///
/// prototype DbCommand
/// the DbCommandDefinition
internal static DbCommandDefinition CreateCommandDefinition(DbCommand prototype) {
EntityUtil.CheckArgumentNull(prototype, "prototype");
ICloneable cloneablePrototype = prototype as ICloneable;
if (null == cloneablePrototype) {
throw EntityUtil.CannotCloneStoreProvider();
}
DbCommand clonedPrototype = (DbCommand)(cloneablePrototype.Clone());
return new DbCommandDefinition(clonedPrototype);
}
///
/// Protected constructor; the command is assumed to be a prototype
/// that will be cloned on CreateCommand, and the cloned command will be executed.
///
protected DbCommandDefinition(DbCommand prototype) {
EntityUtil.CheckArgumentNull(prototype, "prototype");
_prototype = prototype as ICloneable;
if (null == _prototype) {
throw EntityUtil.CannotCloneStoreProvider();
}
}
///
/// Constructor overload for subclasses to use
///
protected DbCommandDefinition() {
}
///
/// Create a DbCommand object from the definition, that can be executed.
///
///
public virtual DbCommand CreateCommand() {
return (DbCommand)(_prototype.Clone());
}
internal static void PopulateParameterFromTypeUsage(DbParameter parameter, TypeUsage type, bool isOutParam)
{
EntityUtil.CheckArgumentNull(parameter, "parameter");
EntityUtil.CheckArgumentNull(type, "type");
// parameter.IsNullable - from the NullableConstraintAttribute value
parameter.IsNullable = TypeSemantics.IsNullable(type);
// parameter.ParameterName - set by the caller;
// parameter.SourceColumn - not applicable until we have a data adapter;
// parameter.SourceColumnNullMapping - not applicable until we have a data adapter;
// parameter.SourceVersion - not applicable until we have a data adapter;
// parameter.Value - left unset;
// parameter.DbType - determined by the TypeMapping;
// parameter.Precision - from the TypeMapping;
// parameter.Scale - from the TypeMapping;
// parameter.Size - from the TypeMapping;
Debug.Assert(null != type, "no type mapping?");
if (TypeSemantics.IsPrimitiveType(type, Metadata.Edm.PrimitiveTypeKind.Binary))
{
PopulateBinaryParameter(parameter, type, DbType.Binary, isOutParam);
}
else if (TypeSemantics.IsPrimitiveType(type, PrimitiveTypeKind.Boolean))
{
parameter.DbType = DbType.Boolean;
}
else if (TypeSemantics.IsPrimitiveType(type, PrimitiveTypeKind.Byte))
{
parameter.DbType = DbType.Byte;
}
else if (TypeSemantics.IsPrimitiveType(type, PrimitiveTypeKind.DateTime))
{
PopulateDateTimeParameter(parameter, type, DbType.DateTime);
}
else if (TypeSemantics.IsPrimitiveType(type, PrimitiveTypeKind.Time))
{
PopulateDateTimeParameter(parameter, type, DbType.Time);
}
else if (TypeSemantics.IsPrimitiveType(type, PrimitiveTypeKind.DateTimeOffset))
{
PopulateDateTimeParameter(parameter, type, DbType.DateTimeOffset);
}
else if (TypeSemantics.IsPrimitiveType(type, PrimitiveTypeKind.Decimal))
{
PopulateDecimalParameter(parameter, type, DbType.Decimal);
}
else if (TypeSemantics.IsPrimitiveType(type, PrimitiveTypeKind.Double))
{
parameter.DbType = DbType.Double;
}
else if (TypeSemantics.IsPrimitiveType(type, PrimitiveTypeKind.Guid))
{
parameter.DbType = DbType.Guid;
}
else if (TypeSemantics.IsPrimitiveType(type, PrimitiveTypeKind.Single))
{
parameter.DbType = DbType.Single;
}
else if (TypeSemantics.IsPrimitiveType(type, PrimitiveTypeKind.SByte))
{
parameter.DbType = DbType.SByte;
}
else if (TypeSemantics.IsPrimitiveType(type, PrimitiveTypeKind.Int16))
{
parameter.DbType = DbType.Int16;
}
else if (TypeSemantics.IsPrimitiveType(type, PrimitiveTypeKind.Int32))
{
parameter.DbType = DbType.Int32;
}
else if (TypeSemantics.IsPrimitiveType(type, PrimitiveTypeKind.Int64))
{
parameter.DbType = DbType.Int64;
}
else if (TypeSemantics.IsPrimitiveType(type, PrimitiveTypeKind.String))
{
PopulateStringParameter(parameter, type, isOutParam);
}
}
private static void PopulateBinaryParameter(DbParameter parameter, TypeUsage type, DbType dbType, bool isOutParam)
{
parameter.DbType = dbType;
// For each facet, set the facet value only if we have it, note that it's possible to not have
// it in the case the facet value is null
SetParameterSize(parameter, type, isOutParam);
}
private static void PopulateDecimalParameter (DbParameter parameter, TypeUsage type, DbType dbType)
{
parameter.DbType = dbType;
IDbDataParameter dataParameter = (IDbDataParameter)parameter;
// For each facet, set the facet value only if we have it, note that it's possible to not have
// it in the case the facet value is null
byte precision;
byte scale;
if (TypeHelpers.TryGetPrecision(type, out precision))
{
dataParameter.Precision = precision;
}
if (TypeHelpers.TryGetScale(type, out scale))
{
dataParameter.Scale = scale;
}
}
private static void PopulateDateTimeParameter(DbParameter parameter, TypeUsage type, DbType dbType)
{
parameter.DbType = dbType;
IDbDataParameter dataParameter = (IDbDataParameter)parameter;
// For each facet, set the facet value only if we have it, note that it's possible to not have
// it in the case the facet value is null
byte precision;
if (TypeHelpers.TryGetPrecision(type, out precision))
{
dataParameter.Precision = precision;
}
}
private static void PopulateStringParameter(DbParameter parameter, TypeUsage type, bool isOutParam)
{
// For each facet, set the facet value only if we have it, note that it's possible to not have
// it in the case the facet value is null
bool unicode = true;
bool fixedLength = false;
if (!TypeHelpers.TryGetIsFixedLength(type, out fixedLength))
{
// If we can't get the fixed length facet value, then default to fixed length = false
fixedLength = false;
}
if (!TypeHelpers.TryGetIsUnicode(type, out unicode))
{
// If we can't get the unicode facet value, then default to unicode = true
unicode = true;
}
if (fixedLength)
{
parameter.DbType = (unicode ? DbType.StringFixedLength : DbType.AnsiStringFixedLength);
}
else
{
parameter.DbType = (unicode ? DbType.String : DbType.AnsiString);
}
SetParameterSize(parameter, type, isOutParam);
}
private static void SetParameterSize(DbParameter parameter, TypeUsage type, bool isOutParam)
{
// only set the size if the parameter has a specific size value.
Facet maxLengthFacet;
if (type.Facets.TryGetValue(DbProviderManifest.MaxLengthFacetName, true, out maxLengthFacet) && maxLengthFacet.Value != null)
{
// only set size if there is a specific size
if (!Helper.IsUnboundedFacetValue(maxLengthFacet))
{
parameter.Size = (int)maxLengthFacet.Value;
}
else if (isOutParam)
{
// if it is store procedure parameter and it is unbounded set the size to max
parameter.Size = Int32.MaxValue;
}
}
}
}
}
// File provided for Reference Use Only by Microsoft Corporation (c) 2007.
//------------------------------------------------------------------------------
//
// Copyright (c) Microsoft Corporation. All rights reserved.
//
//
// @owner [....], [....]
//-----------------------------------------------------------------------------
using System.Diagnostics;
using System.Data.Metadata.Edm;
namespace System.Data.Common {
///
/// A prepared command definition, can be cached and reused to avoid
/// repreparing a command.
///
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Naming", "CA1709:IdentifiersShouldBeCasedCorrectly", MessageId = "Db")]
public class DbCommandDefinition {
private readonly ICloneable _prototype;
///
/// Internal factory method to create the default Command Definition object
/// based on a prototype command. The prototype command is cloned
/// before the protected constructor is invoked
///
/// prototype DbCommand
/// the DbCommandDefinition
internal static DbCommandDefinition CreateCommandDefinition(DbCommand prototype) {
EntityUtil.CheckArgumentNull(prototype, "prototype");
ICloneable cloneablePrototype = prototype as ICloneable;
if (null == cloneablePrototype) {
throw EntityUtil.CannotCloneStoreProvider();
}
DbCommand clonedPrototype = (DbCommand)(cloneablePrototype.Clone());
return new DbCommandDefinition(clonedPrototype);
}
///
/// Protected constructor; the command is assumed to be a prototype
/// that will be cloned on CreateCommand, and the cloned command will be executed.
///
protected DbCommandDefinition(DbCommand prototype) {
EntityUtil.CheckArgumentNull(prototype, "prototype");
_prototype = prototype as ICloneable;
if (null == _prototype) {
throw EntityUtil.CannotCloneStoreProvider();
}
}
///
/// Constructor overload for subclasses to use
///
protected DbCommandDefinition() {
}
///
/// Create a DbCommand object from the definition, that can be executed.
///
///
public virtual DbCommand CreateCommand() {
return (DbCommand)(_prototype.Clone());
}
internal static void PopulateParameterFromTypeUsage(DbParameter parameter, TypeUsage type, bool isOutParam)
{
EntityUtil.CheckArgumentNull(parameter, "parameter");
EntityUtil.CheckArgumentNull(type, "type");
// parameter.IsNullable - from the NullableConstraintAttribute value
parameter.IsNullable = TypeSemantics.IsNullable(type);
// parameter.ParameterName - set by the caller;
// parameter.SourceColumn - not applicable until we have a data adapter;
// parameter.SourceColumnNullMapping - not applicable until we have a data adapter;
// parameter.SourceVersion - not applicable until we have a data adapter;
// parameter.Value - left unset;
// parameter.DbType - determined by the TypeMapping;
// parameter.Precision - from the TypeMapping;
// parameter.Scale - from the TypeMapping;
// parameter.Size - from the TypeMapping;
Debug.Assert(null != type, "no type mapping?");
if (TypeSemantics.IsPrimitiveType(type, Metadata.Edm.PrimitiveTypeKind.Binary))
{
PopulateBinaryParameter(parameter, type, DbType.Binary, isOutParam);
}
else if (TypeSemantics.IsPrimitiveType(type, PrimitiveTypeKind.Boolean))
{
parameter.DbType = DbType.Boolean;
}
else if (TypeSemantics.IsPrimitiveType(type, PrimitiveTypeKind.Byte))
{
parameter.DbType = DbType.Byte;
}
else if (TypeSemantics.IsPrimitiveType(type, PrimitiveTypeKind.DateTime))
{
PopulateDateTimeParameter(parameter, type, DbType.DateTime);
}
else if (TypeSemantics.IsPrimitiveType(type, PrimitiveTypeKind.Time))
{
PopulateDateTimeParameter(parameter, type, DbType.Time);
}
else if (TypeSemantics.IsPrimitiveType(type, PrimitiveTypeKind.DateTimeOffset))
{
PopulateDateTimeParameter(parameter, type, DbType.DateTimeOffset);
}
else if (TypeSemantics.IsPrimitiveType(type, PrimitiveTypeKind.Decimal))
{
PopulateDecimalParameter(parameter, type, DbType.Decimal);
}
else if (TypeSemantics.IsPrimitiveType(type, PrimitiveTypeKind.Double))
{
parameter.DbType = DbType.Double;
}
else if (TypeSemantics.IsPrimitiveType(type, PrimitiveTypeKind.Guid))
{
parameter.DbType = DbType.Guid;
}
else if (TypeSemantics.IsPrimitiveType(type, PrimitiveTypeKind.Single))
{
parameter.DbType = DbType.Single;
}
else if (TypeSemantics.IsPrimitiveType(type, PrimitiveTypeKind.SByte))
{
parameter.DbType = DbType.SByte;
}
else if (TypeSemantics.IsPrimitiveType(type, PrimitiveTypeKind.Int16))
{
parameter.DbType = DbType.Int16;
}
else if (TypeSemantics.IsPrimitiveType(type, PrimitiveTypeKind.Int32))
{
parameter.DbType = DbType.Int32;
}
else if (TypeSemantics.IsPrimitiveType(type, PrimitiveTypeKind.Int64))
{
parameter.DbType = DbType.Int64;
}
else if (TypeSemantics.IsPrimitiveType(type, PrimitiveTypeKind.String))
{
PopulateStringParameter(parameter, type, isOutParam);
}
}
private static void PopulateBinaryParameter(DbParameter parameter, TypeUsage type, DbType dbType, bool isOutParam)
{
parameter.DbType = dbType;
// For each facet, set the facet value only if we have it, note that it's possible to not have
// it in the case the facet value is null
SetParameterSize(parameter, type, isOutParam);
}
private static void PopulateDecimalParameter (DbParameter parameter, TypeUsage type, DbType dbType)
{
parameter.DbType = dbType;
IDbDataParameter dataParameter = (IDbDataParameter)parameter;
// For each facet, set the facet value only if we have it, note that it's possible to not have
// it in the case the facet value is null
byte precision;
byte scale;
if (TypeHelpers.TryGetPrecision(type, out precision))
{
dataParameter.Precision = precision;
}
if (TypeHelpers.TryGetScale(type, out scale))
{
dataParameter.Scale = scale;
}
}
private static void PopulateDateTimeParameter(DbParameter parameter, TypeUsage type, DbType dbType)
{
parameter.DbType = dbType;
IDbDataParameter dataParameter = (IDbDataParameter)parameter;
// For each facet, set the facet value only if we have it, note that it's possible to not have
// it in the case the facet value is null
byte precision;
if (TypeHelpers.TryGetPrecision(type, out precision))
{
dataParameter.Precision = precision;
}
}
private static void PopulateStringParameter(DbParameter parameter, TypeUsage type, bool isOutParam)
{
// For each facet, set the facet value only if we have it, note that it's possible to not have
// it in the case the facet value is null
bool unicode = true;
bool fixedLength = false;
if (!TypeHelpers.TryGetIsFixedLength(type, out fixedLength))
{
// If we can't get the fixed length facet value, then default to fixed length = false
fixedLength = false;
}
if (!TypeHelpers.TryGetIsUnicode(type, out unicode))
{
// If we can't get the unicode facet value, then default to unicode = true
unicode = true;
}
if (fixedLength)
{
parameter.DbType = (unicode ? DbType.StringFixedLength : DbType.AnsiStringFixedLength);
}
else
{
parameter.DbType = (unicode ? DbType.String : DbType.AnsiString);
}
SetParameterSize(parameter, type, isOutParam);
}
private static void SetParameterSize(DbParameter parameter, TypeUsage type, bool isOutParam)
{
// only set the size if the parameter has a specific size value.
Facet maxLengthFacet;
if (type.Facets.TryGetValue(DbProviderManifest.MaxLengthFacetName, true, out maxLengthFacet) && maxLengthFacet.Value != null)
{
// only set size if there is a specific size
if (!Helper.IsUnboundedFacetValue(maxLengthFacet))
{
parameter.Size = (int)maxLengthFacet.Value;
}
else if (isOutParam)
{
// if it is store procedure parameter and it is unbounded set the size to max
parameter.Size = Int32.MaxValue;
}
}
}
}
}
// 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
- WindowsGraphicsWrapper.cs
- VariableDesigner.xaml.cs
- TimeStampChecker.cs
- CodeEntryPointMethod.cs
- ConfigurationStrings.cs
- DurableServiceAttribute.cs
- GestureRecognitionResult.cs
- PartialTrustVisibleAssembliesSection.cs
- ClientEventManager.cs
- GetMemberBinder.cs
- Subset.cs
- EndOfStreamException.cs
- XmlAnyElementAttributes.cs
- MailWriter.cs
- EventLogTraceListener.cs
- WindowsGraphicsWrapper.cs
- SiteMembershipCondition.cs
- TextElementCollection.cs
- FixedSOMTextRun.cs
- Single.cs
- WindowsListViewItem.cs
- PageClientProxyGenerator.cs
- TextRangeEditTables.cs
- TypeResolver.cs
- coordinatorscratchpad.cs
- DefaultWorkflowTransactionService.cs
- ConfigurationManagerHelper.cs
- DefaultAsyncDataDispatcher.cs
- ImageField.cs
- WindowsListViewGroupHelper.cs
- XmlIncludeAttribute.cs
- BulletedListEventArgs.cs
- ErrorTableItemStyle.cs
- Gdiplus.cs
- AppSettingsExpressionBuilder.cs
- LocatorPart.cs
- ConfigXmlWhitespace.cs
- EDesignUtil.cs
- TextTabProperties.cs
- XmlSchemaSimpleContentRestriction.cs
- ArcSegment.cs
- RuleProcessor.cs
- SapiRecoInterop.cs
- ProviderUtil.cs
- InkCanvasInnerCanvas.cs
- MessageTraceRecord.cs
- DataRow.cs
- ResourcesChangeInfo.cs
- XmlDataSource.cs
- RetrieveVirtualItemEventArgs.cs
- Journal.cs
- SerialReceived.cs
- DataTableMapping.cs
- PictureBoxDesigner.cs
- ResolveNameEventArgs.cs
- UniqueSet.cs
- SelectionRange.cs
- PresentationAppDomainManager.cs
- ImageSourceTypeConverter.cs
- DataMisalignedException.cs
- System.Data_BID.cs
- MainMenu.cs
- FilteredAttributeCollection.cs
- Debug.cs
- SessionPageStatePersister.cs
- RenderData.cs
- ConstraintManager.cs
- SupportingTokenListenerFactory.cs
- CompressionTracing.cs
- DesignerSelectionListAdapter.cs
- WebPartDisplayMode.cs
- X500Name.cs
- METAHEADER.cs
- x509utils.cs
- NonParentingControl.cs
- ToolStripItemGlyph.cs
- UTF7Encoding.cs
- AttributeCollection.cs
- AsmxEndpointPickerExtension.cs
- BidPrivateBase.cs
- InputBuffer.cs
- FixedSOMContainer.cs
- GatewayDefinition.cs
- CodeDOMUtility.cs
- XamlTypeMapperSchemaContext.cs
- CommunicationObjectManager.cs
- WsatConfiguration.cs
- SqlBuffer.cs
- XmlILOptimizerVisitor.cs
- Executor.cs
- Module.cs
- UnaryNode.cs
- TableNameAttribute.cs
- DependencyPropertyChangedEventArgs.cs
- EventDescriptorCollection.cs
- StyleTypedPropertyAttribute.cs
- LinqDataSourceSelectEventArgs.cs
- ExtensibleSyndicationObject.cs
- ScriptDescriptor.cs
- CheckBox.cs