SqlIdentifier.cs 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Data;
  4. using System.Data.Linq.Mapping;
  5. using System.Data.Linq.Provider;
  6. using System.Data.SqlClient;
  7. using System.Globalization;
  8. using System.Linq;
  9. using System.Text;
  10. using System.Text.RegularExpressions;
  11. namespace System.Data.Linq.SqlClient {
  12. internal static class SqlIdentifier
  13. {
  14. private static SqlCommandBuilder builder = new SqlCommandBuilder();
  15. const string ParameterPrefix = "@";
  16. const string QuotePrefix = "[";
  17. const string QuoteSuffix = "]";
  18. const string SchemaSeparator = ".";
  19. const char SchemaSeparatorChar = '.';
  20. private static bool IsQuoted(string s) {
  21. if (s == null) {
  22. throw Error.ArgumentNull("s");
  23. }
  24. if (s.Length < 2) {
  25. return false;
  26. }
  27. return s.StartsWith(QuotePrefix, StringComparison.Ordinal)
  28. && s.EndsWith(QuoteSuffix, StringComparison.Ordinal);
  29. }
  30. // This is MSSQL-specific quoting.
  31. // If the string begins and ends with [ and ], it will be assumed to already be quoted.
  32. // Otherwise periods are assumed to be namespace delimiters, and the string is split on each.
  33. // Each string from the split is then check to see if it is already quoted, and if
  34. // not, it is replaced with the result of SqlCommandBuilder.QuoteIdentifier.
  35. // Then the set of strings is rejoined with periods.
  36. internal static string QuoteCompoundIdentifier(string s) {
  37. if (s == null) {
  38. throw Error.ArgumentNull("s");
  39. }
  40. // if it starts with @, then return unprocessed
  41. if (s.StartsWith(ParameterPrefix, StringComparison.Ordinal)) {
  42. return s;
  43. } else if (IsQuoted(s)) {
  44. return s;
  45. }
  46. else if (!s.StartsWith(QuotePrefix, StringComparison.Ordinal) && s.EndsWith(QuoteSuffix, StringComparison.Ordinal)) {
  47. //A.[B] => [A].[B]
  48. int splitPosition = s.IndexOf(SchemaSeparatorChar);
  49. if (splitPosition < 0){ //no . in the string
  50. return builder.QuoteIdentifier(s);
  51. }
  52. string left = s.Substring(0, splitPosition);
  53. string right = s.Substring(splitPosition + 1, s.Length - splitPosition - 1);
  54. if (!IsQuoted(right)) {
  55. right = builder.QuoteIdentifier(right);
  56. }
  57. return String.Concat(QuoteCompoundIdentifier(left), SchemaSeparatorChar + right);
  58. }
  59. else if (s.StartsWith(QuotePrefix, StringComparison.Ordinal) && !s.EndsWith(QuoteSuffix, StringComparison.Ordinal)) {
  60. //[A].B => [A].[B]
  61. int splitPosition = s.LastIndexOf(SchemaSeparatorChar);
  62. if (splitPosition < 0){ //no . in the string
  63. return builder.QuoteIdentifier(s);
  64. }
  65. string left = s.Substring(0, splitPosition);
  66. if (!IsQuoted(left)) {
  67. left = builder.QuoteIdentifier(left);
  68. }
  69. string right = s.Substring(splitPosition + 1, s.Length - splitPosition - 1);
  70. return String.Concat(left + SchemaSeparatorChar, QuoteCompoundIdentifier(right));
  71. }
  72. else {
  73. int splitPosition = s.IndexOf(SchemaSeparatorChar);
  74. if (splitPosition < 0) { //no . in the string
  75. //A => [A]
  76. return builder.QuoteIdentifier(s);
  77. }
  78. string left = s.Substring(0, splitPosition);
  79. string right = s.Substring(splitPosition + 1, s.Length - splitPosition - 1);
  80. return String.Concat(QuoteCompoundIdentifier(left) + SchemaSeparatorChar, QuoteCompoundIdentifier(right));
  81. }
  82. }
  83. // This is MSSQL-specific quoting.
  84. // This is the same as above, but it doesn't consider anything compound.
  85. internal static string QuoteIdentifier(string s) {
  86. if (s == null) {
  87. throw Error.ArgumentNull("s");
  88. }
  89. // if it starts with @, then return unprocessed
  90. if (s.StartsWith(ParameterPrefix, StringComparison.Ordinal)) {
  91. return s;
  92. } else if (IsQuoted(s)) {
  93. return s;
  94. } else {
  95. return builder.QuoteIdentifier(s);
  96. }
  97. }
  98. // turns "[ABC].[PQR].[XYZ]" into {"[ABC]", "[PQR]", "[XYZ]"}
  99. internal static IEnumerable<string> GetCompoundIdentifierParts(string s) {
  100. if (s == null) {
  101. throw Error.ArgumentNull("s");
  102. }
  103. // can't do this to parameters
  104. if (s.StartsWith(ParameterPrefix, StringComparison.Ordinal)) {
  105. throw Error.ArgumentWrongValue("s");
  106. }
  107. string quotedS = QuoteCompoundIdentifier(s);
  108. string pattern = @"^(?<component>\[([^\]]|\]\])*\])(\.(?<component>\[([^\]]|\]\])*\]))*$";
  109. // This pattern matches "."-delimited quoted SQL identifiers. Here's how:
  110. //
  111. // 1. It is wrapped in "^" and "$", which match the begining and end of text, so it will match
  112. // only the entire text and not any sub-part.
  113. // 2. The group "(?<component>\[([^\]]|\]\])*\])" captures a single quoted segment of the text.
  114. // It's a literal "[" followed by any number of non-"]" characters or "]]" strings, followed
  115. // by a literal "]". The "?<component>" bit names the capture so we can refer to it.
  116. // 3. After the first component, we will allow any number of groups which consist of a literal
  117. // "." followed by a component (and the component part is a repeat of the part described in 2).
  118. Match m = Regex.Match(quotedS, pattern);
  119. if (!m.Success)
  120. {
  121. throw Error.ArgumentWrongValue("s");
  122. }
  123. foreach (Capture cap in m.Groups["component"].Captures)
  124. {
  125. yield return cap.Value;
  126. }
  127. }
  128. }
  129. }