Code:
/ Dotnetfx_Win7_3.5.1 / Dotnetfx_Win7_3.5.1 / 3.5.1 / DEVDIV / depot / DevDiv / releases / Orcas / NetFXw7 / ndp / fx / src / DLinq / Dlinq / SqlClient / Query / SqlIdentifier.cs / 1 / SqlIdentifier.cs
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Linq.Mapping;
using System.Data.Linq.Provider;
using System.Data.SqlClient;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
namespace System.Data.Linq.SqlClient {
internal static class SqlIdentifier
{
private static SqlCommandBuilder builder = new SqlCommandBuilder();
const string ParameterPrefix = "@";
const string QuotePrefix = "[";
const string QuoteSuffix = "]";
const string SchemaSeparator = ".";
const char SchemaSeparatorChar = '.';
private static bool IsQuoted(string s) {
if (s == null) {
throw Error.ArgumentNull("s");
}
if (s.Length < 2) {
return false;
}
return s.StartsWith(QuotePrefix, StringComparison.Ordinal)
&& s.EndsWith(QuoteSuffix, StringComparison.Ordinal);
}
// This is MSSQL-specific quoting.
// If the string begins and ends with [ and ], it will be assumed to already be quoted.
// Otherwise periods are assumed to be namespace delimiters, and the string is split on each.
// Each string from the split is then check to see if it is already quoted, and if
// not, it is replaced with the result of SqlCommandBuilder.QuoteIdentifier.
// Then the set of strings is rejoined with periods.
internal static string QuoteCompoundIdentifier(string s) {
if (s == null) {
throw Error.ArgumentNull("s");
}
// if it starts with @, then return unprocessed
if (s.StartsWith(ParameterPrefix, StringComparison.Ordinal)) {
return s;
} else if (IsQuoted(s)) {
return s;
}
else if (!s.StartsWith(QuotePrefix, StringComparison.Ordinal) && s.EndsWith(QuoteSuffix, StringComparison.Ordinal)) {
//A.[B] => [A].[B]
int splitPosition = s.IndexOf(SchemaSeparatorChar);
if (splitPosition < 0){ //no . in the string
return builder.QuoteIdentifier(s);
}
string left = s.Substring(0, splitPosition);
string right = s.Substring(splitPosition + 1, s.Length - splitPosition - 1);
if (!IsQuoted(right)) {
right = builder.QuoteIdentifier(right);
}
return String.Concat(QuoteCompoundIdentifier(left), SchemaSeparatorChar + right);
}
else if (s.StartsWith(QuotePrefix, StringComparison.Ordinal) && !s.EndsWith(QuoteSuffix, StringComparison.Ordinal)) {
//[A].B => [A].[B]
int splitPosition = s.LastIndexOf(SchemaSeparatorChar);
if (splitPosition < 0){ //no . in the string
return builder.QuoteIdentifier(s);
}
string left = s.Substring(0, splitPosition);
if (!IsQuoted(left)) {
left = builder.QuoteIdentifier(left);
}
string right = s.Substring(splitPosition + 1, s.Length - splitPosition - 1);
return String.Concat(left + SchemaSeparatorChar, QuoteCompoundIdentifier(right));
}
else {
int splitPosition = s.IndexOf(SchemaSeparatorChar);
if (splitPosition < 0) { //no . in the string
//A => [A]
return builder.QuoteIdentifier(s);
}
string left = s.Substring(0, splitPosition);
string right = s.Substring(splitPosition + 1, s.Length - splitPosition - 1);
return String.Concat(QuoteCompoundIdentifier(left) + SchemaSeparatorChar, QuoteCompoundIdentifier(right));
}
}
// This is MSSQL-specific quoting.
// This is the same as above, but it doesn't consider anything compound.
internal static string QuoteIdentifier(string s) {
if (s == null) {
throw Error.ArgumentNull("s");
}
// if it starts with @, then return unprocessed
if (s.StartsWith(ParameterPrefix, StringComparison.Ordinal)) {
return s;
} else if (IsQuoted(s)) {
return s;
} else {
return builder.QuoteIdentifier(s);
}
}
// turns "[ABC].[PQR].[XYZ]" into {"[ABC]", "[PQR]", "[XYZ]"}
internal static IEnumerable GetCompoundIdentifierParts(string s) {
if (s == null) {
throw Error.ArgumentNull("s");
}
// can't do this to parameters
if (s.StartsWith(ParameterPrefix, StringComparison.Ordinal)) {
throw Error.ArgumentWrongValue("s");
}
string quotedS = QuoteCompoundIdentifier(s);
string pattern = @"^(?\[([^\]]|\]\])*\])(\.(?\[([^\]]|\]\])*\]))*$";
// This pattern matches "."-delimited quoted SQL identifiers. Here's how:
//
// 1. It is wrapped in "^" and "$", which match the begining and end of text, so it will match
// only the entire text and not any sub-part.
// 2. The group "(?\[([^\]]|\]\])*\])" captures a single quoted segment of the text.
// It's a literal "[" followed by any number of non-"]" characters or "]]" strings, followed
// by a literal "]". The "?" bit names the capture so we can refer to it.
// 3. After the first component, we will allow any number of groups which consist of a literal
// "." followed by a component (and the component part is a repeat of the part described in 2).
Match m = Regex.Match(quotedS, pattern);
if (!m.Success)
{
throw Error.ArgumentWrongValue("s");
}
foreach (Capture cap in m.Groups["component"].Captures)
{
yield return cap.Value;
}
}
}
}
// File provided for Reference Use Only by Microsoft Corporation (c) 2007.
// Copyright (c) Microsoft Corporation. All rights reserved.
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Linq.Mapping;
using System.Data.Linq.Provider;
using System.Data.SqlClient;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
namespace System.Data.Linq.SqlClient {
internal static class SqlIdentifier
{
private static SqlCommandBuilder builder = new SqlCommandBuilder();
const string ParameterPrefix = "@";
const string QuotePrefix = "[";
const string QuoteSuffix = "]";
const string SchemaSeparator = ".";
const char SchemaSeparatorChar = '.';
private static bool IsQuoted(string s) {
if (s == null) {
throw Error.ArgumentNull("s");
}
if (s.Length < 2) {
return false;
}
return s.StartsWith(QuotePrefix, StringComparison.Ordinal)
&& s.EndsWith(QuoteSuffix, StringComparison.Ordinal);
}
// This is MSSQL-specific quoting.
// If the string begins and ends with [ and ], it will be assumed to already be quoted.
// Otherwise periods are assumed to be namespace delimiters, and the string is split on each.
// Each string from the split is then check to see if it is already quoted, and if
// not, it is replaced with the result of SqlCommandBuilder.QuoteIdentifier.
// Then the set of strings is rejoined with periods.
internal static string QuoteCompoundIdentifier(string s) {
if (s == null) {
throw Error.ArgumentNull("s");
}
// if it starts with @, then return unprocessed
if (s.StartsWith(ParameterPrefix, StringComparison.Ordinal)) {
return s;
} else if (IsQuoted(s)) {
return s;
}
else if (!s.StartsWith(QuotePrefix, StringComparison.Ordinal) && s.EndsWith(QuoteSuffix, StringComparison.Ordinal)) {
//A.[B] => [A].[B]
int splitPosition = s.IndexOf(SchemaSeparatorChar);
if (splitPosition < 0){ //no . in the string
return builder.QuoteIdentifier(s);
}
string left = s.Substring(0, splitPosition);
string right = s.Substring(splitPosition + 1, s.Length - splitPosition - 1);
if (!IsQuoted(right)) {
right = builder.QuoteIdentifier(right);
}
return String.Concat(QuoteCompoundIdentifier(left), SchemaSeparatorChar + right);
}
else if (s.StartsWith(QuotePrefix, StringComparison.Ordinal) && !s.EndsWith(QuoteSuffix, StringComparison.Ordinal)) {
//[A].B => [A].[B]
int splitPosition = s.LastIndexOf(SchemaSeparatorChar);
if (splitPosition < 0){ //no . in the string
return builder.QuoteIdentifier(s);
}
string left = s.Substring(0, splitPosition);
if (!IsQuoted(left)) {
left = builder.QuoteIdentifier(left);
}
string right = s.Substring(splitPosition + 1, s.Length - splitPosition - 1);
return String.Concat(left + SchemaSeparatorChar, QuoteCompoundIdentifier(right));
}
else {
int splitPosition = s.IndexOf(SchemaSeparatorChar);
if (splitPosition < 0) { //no . in the string
//A => [A]
return builder.QuoteIdentifier(s);
}
string left = s.Substring(0, splitPosition);
string right = s.Substring(splitPosition + 1, s.Length - splitPosition - 1);
return String.Concat(QuoteCompoundIdentifier(left) + SchemaSeparatorChar, QuoteCompoundIdentifier(right));
}
}
// This is MSSQL-specific quoting.
// This is the same as above, but it doesn't consider anything compound.
internal static string QuoteIdentifier(string s) {
if (s == null) {
throw Error.ArgumentNull("s");
}
// if it starts with @, then return unprocessed
if (s.StartsWith(ParameterPrefix, StringComparison.Ordinal)) {
return s;
} else if (IsQuoted(s)) {
return s;
} else {
return builder.QuoteIdentifier(s);
}
}
// turns "[ABC].[PQR].[XYZ]" into {"[ABC]", "[PQR]", "[XYZ]"}
internal static IEnumerable GetCompoundIdentifierParts(string s) {
if (s == null) {
throw Error.ArgumentNull("s");
}
// can't do this to parameters
if (s.StartsWith(ParameterPrefix, StringComparison.Ordinal)) {
throw Error.ArgumentWrongValue("s");
}
string quotedS = QuoteCompoundIdentifier(s);
string pattern = @"^(?\[([^\]]|\]\])*\])(\.(?\[([^\]]|\]\])*\]))*$";
// This pattern matches "."-delimited quoted SQL identifiers. Here's how:
//
// 1. It is wrapped in "^" and "$", which match the begining and end of text, so it will match
// only the entire text and not any sub-part.
// 2. The group "(?\[([^\]]|\]\])*\])" captures a single quoted segment of the text.
// It's a literal "[" followed by any number of non-"]" characters or "]]" strings, followed
// by a literal "]". The "?" bit names the capture so we can refer to it.
// 3. After the first component, we will allow any number of groups which consist of a literal
// "." followed by a component (and the component part is a repeat of the part described in 2).
Match m = Regex.Match(quotedS, pattern);
if (!m.Success)
{
throw Error.ArgumentWrongValue("s");
}
foreach (Capture cap in m.Groups["component"].Captures)
{
yield return cap.Value;
}
}
}
}
// File provided for Reference Use Only by Microsoft Corporation (c) 2007.
// Copyright (c) Microsoft Corporation. All rights reserved.
Link Menu

This book is available now!
Buy at Amazon US or
Buy at Amazon UK
- DataRowComparer.cs
- ProxyWebPartManagerDesigner.cs
- DirectoryNotFoundException.cs
- Partitioner.cs
- StickyNoteContentControl.cs
- UnsafeNativeMethodsMilCoreApi.cs
- ReachUIElementCollectionSerializerAsync.cs
- PairComparer.cs
- GetLedgerEntryForRecipientRequest.cs
- ClientSideQueueItem.cs
- ToolStripItemTextRenderEventArgs.cs
- FileDialog.cs
- SoapObjectReader.cs
- DataTableTypeConverter.cs
- XmlAutoDetectWriter.cs
- XmlDownloadManager.cs
- WorkflowValidationFailedException.cs
- StringHelper.cs
- COM2PropertyDescriptor.cs
- CompositeFontFamily.cs
- CodeSnippetTypeMember.cs
- CodeGroup.cs
- MaxSessionCountExceededException.cs
- LongValidator.cs
- SQLInt32Storage.cs
- AlphaSortedEnumConverter.cs
- SchemaTypeEmitter.cs
- ProfileSettingsCollection.cs
- HttpException.cs
- DockProviderWrapper.cs
- SafeReversePInvokeHandle.cs
- StaticExtension.cs
- AuthenticationConfig.cs
- NotifyCollectionChangedEventArgs.cs
- BooleanStorage.cs
- FontUnit.cs
- TreeNodeSelectionProcessor.cs
- LayoutEditorPart.cs
- DeadCharTextComposition.cs
- ProviderException.cs
- ButtonBaseAdapter.cs
- AppDomainUnloadedException.cs
- CleanUpVirtualizedItemEventArgs.cs
- FileLoadException.cs
- ExtractedStateEntry.cs
- NonceCache.cs
- ListSourceHelper.cs
- ToolStripPanelRow.cs
- Utils.cs
- ellipse.cs
- TextProperties.cs
- ListenerElementsCollection.cs
- EmptyElement.cs
- SqlClientPermission.cs
- LinqDataSourceView.cs
- IPEndPointCollection.cs
- Localizer.cs
- EntityDataSourceWrapperCollection.cs
- ProcessHostServerConfig.cs
- ListSourceHelper.cs
- PeerReferralPolicy.cs
- WindowsListViewItemCheckBox.cs
- LocalizedNameDescriptionPair.cs
- ContentFilePart.cs
- TextTrailingCharacterEllipsis.cs
- DtdParser.cs
- EntityViewGenerationConstants.cs
- ItemList.cs
- ReadOnlyTernaryTree.cs
- BaseAddressElementCollection.cs
- XsltContext.cs
- WorkflowRuntimeServiceElementCollection.cs
- SystemIPGlobalProperties.cs
- WindowsGraphicsWrapper.cs
- HtmlTernaryTree.cs
- SafeNativeMethods.cs
- SHA1Managed.cs
- CodeIterationStatement.cs
- HttpProfileBase.cs
- TriggerAction.cs
- TrackingProfile.cs
- DPAPIProtectedConfigurationProvider.cs
- FormatterServices.cs
- ValueTypeFixupInfo.cs
- Claim.cs
- JavaScriptSerializer.cs
- PixelShader.cs
- LogEntryHeaderv1Deserializer.cs
- WebPartManagerInternals.cs
- PartialCachingControl.cs
- _ConnectStream.cs
- HtmlTernaryTree.cs
- SecureUICommand.cs
- XmlTypeAttribute.cs
- ResourceReferenceExpression.cs
- TypeNameConverter.cs
- RectConverter.cs
- Error.cs
- CodeLabeledStatement.cs
- AddInAttribute.cs