SqlServer2KCompatibilityCheck.cs 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. using System;
  2. using System.Collections.ObjectModel;
  3. using System.Text;
  4. namespace System.Data.Linq.SqlClient {
  5. /// <summary>
  6. /// Methods for checking whethe a query was compatible with the
  7. /// server it will be sent to.
  8. /// </summary>
  9. static internal class SqlServerCompatibilityCheck {
  10. /// <summary>
  11. /// Private visitor class checks each node for compatibility annotations.
  12. /// </summary>
  13. private class Visitor : SqlVisitor {
  14. private SqlProvider.ProviderMode provider;
  15. internal SqlNodeAnnotations annotations;
  16. internal Visitor(SqlProvider.ProviderMode provider) {
  17. this.provider = provider;
  18. }
  19. /// <summary>
  20. /// The reasons why this query is not 2K compatible.
  21. /// </summary>
  22. internal Collection<string> reasons = new Collection<string>();
  23. internal override SqlNode Visit(SqlNode node) {
  24. if (annotations.NodeIsAnnotated(node)) {
  25. foreach (SqlNodeAnnotation annotation in annotations.Get(node)) {
  26. SqlServerCompatibilityAnnotation ssca = annotation as SqlServerCompatibilityAnnotation;
  27. if (ssca != null && ssca.AppliesTo(provider)) {
  28. reasons.Add(annotation.Message);
  29. }
  30. }
  31. }
  32. return base.Visit(node);
  33. }
  34. }
  35. /// <summary>
  36. /// Checks whether the given node is supported on the given server.
  37. /// </summary>
  38. internal static void ThrowIfUnsupported(SqlNode node, SqlNodeAnnotations annotations, SqlProvider.ProviderMode provider) {
  39. // Check to see whether there's at least one SqlServerCompatibilityAnnotation.
  40. if (annotations.HasAnnotationType(typeof(SqlServerCompatibilityAnnotation))) {
  41. Visitor visitor = new Visitor(provider);
  42. visitor.annotations = annotations;
  43. visitor.Visit(node);
  44. // If any messages were recorded, then throw an exception.
  45. if (visitor.reasons.Count > 0) {
  46. throw Error.ExpressionNotSupportedForSqlServerVersion(visitor.reasons);
  47. }
  48. }
  49. }
  50. }
  51. }