1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705
|
//---------------------------------------------------------------------
// <copyright file="Sql8SupportabilityVisitor.cs" company="Microsoft">
// Copyright (c) Microsoft Corporation. All rights reserved.
// </copyright>
//
// @owner Microsoft
// @backupOwner Microsoft
//---------------------------------------------------------------------
namespace System.Data.SqlClient.SqlGen
{
using System;
using System.Collections.Generic;
using System.Data.Common.CommandTrees;
/// <summary>
/// The Sql8ConformanceChecker walks a DbExpression tree and determines whether
/// it should be rewritten in order to be translated to SQL appropriate for SQL Server 2000.
/// The tree should be rewritten if it contains any of the following expressions:
/// <list type="bullet">
/// <item><see cref="DbExceptExpression"/></item>
/// <item><see cref="DbIntersectExpression"/></item>
/// <item><see cref="DbSkipExpression"/></item>
/// </list>
///
/// Also, it throws if it determines that the tree can not
/// be translated into SQL appropriate for SQL Server 2000.
/// This happens if:
/// <list type="bullet">
/// <item>The tree contains <see cref="DbApplyExpression"/></item>
/// <item>The tree contains <see cref="DbLimitExpression"/> with property Limit of type <see cref="DbParameterReferenceExpression"/></item>
/// <item>The tree contains <see cref="DbSkipExpression"/> with property Count of type <see cref="DbParameterReferenceExpression"/></item>
/// </list>
///
/// The visitor only checks for expressions for which the support differs between SQL Server 2000 and SQL Server 2005,
/// but does not check/throw for expressions that are not supported for both providers.
///
/// Implementation note: In the cases when the visitor encounters an expression that requires rewrite,
/// it still needs to walk its structure in case something below it is not supported and needs to throw.
///
/// </summary>
internal class Sql8ConformanceChecker : DbExpressionVisitor<bool>
{
#region 'Public' API
/// <summary>
/// The entry point
/// </summary>
/// <param name="expr"></param>
/// <returns>True if the tree needs to be rewriten, false otherwise</returns>
internal static bool NeedsRewrite(DbExpression expr)
{
Sql8ConformanceChecker checker = new Sql8ConformanceChecker();
return expr.Accept(checker);
}
#endregion
#region Constructor
/// <summary>
/// Default Constructor
/// </summary>
private Sql8ConformanceChecker()
{
}
#endregion
#region Visitor Helpers
/// <summary>
/// Default handling for DbUnaryExpression-derived classes. Simply visits its argument
/// </summary>
/// <param name="expr">The DbUnaryExpression to visit</param>
/// <returns></returns>
private bool VisitUnaryExpression(DbUnaryExpression expr)
{
return VisitExpression(expr.Argument);
}
/// <summary>
/// Default handling for DbBinaryExpression-derived classes. Visits both arguments.
/// </summary>
/// <param name="expr">The DbBinaryExpression to visit</param>
/// <returns></returns>
private bool VisitBinaryExpression(DbBinaryExpression expr)
{
bool leftNeedsRewrite = VisitExpression(expr.Left);
bool rightNeedsRewrite = VisitExpression(expr.Right);
return leftNeedsRewrite || rightNeedsRewrite;
}
/// <summary>
/// Used for <see cref="VisitList"/>
/// </summary>
/// <typeparam name="TElementType"></typeparam>
/// <param name="element"></param>
/// <returns></returns>
private delegate bool ListElementHandler<TElementType>(TElementType element);
/// <summary>
/// Walks the structure
/// </summary>
/// <param name="aggregate"></param>
/// <returns></returns>
private bool VisitAggregate(DbAggregate aggregate)
{
return VisitExpressionList(aggregate.Arguments);
}
/// <summary>
/// DbExpressionBinding handler
/// </summary>
/// <param name="expressionBinding"></param>
/// <returns></returns>
private bool VisitExpressionBinding(DbExpressionBinding expressionBinding)
{
return VisitExpression(expressionBinding.Expression);
}
/// <summary>
/// Used as handler for expressions
/// </summary>
/// <param name="expression"></param>
/// <returns></returns>
private bool VisitExpression(DbExpression expression)
{
if (expression == null)
{
return false;
}
return expression.Accept(this);
}
/// <summary>
/// Used as handler for SortClauses
/// </summary>
/// <param name="sortClause"></param>
/// <returns></returns>
private bool VisitSortClause(DbSortClause sortClause)
{
return VisitExpression(sortClause.Expression);
}
/// <summary>
/// Helper method for iterating a list
/// </summary>
/// <typeparam name="TElementType"></typeparam>
/// <param name="handler"></param>
/// <param name="list"></param>
/// <returns></returns>
private static bool VisitList<TElementType>(ListElementHandler<TElementType> handler, IList<TElementType> list)
{
bool result = false;
foreach (TElementType element in list)
{
bool localResult = handler(element);
result = result || localResult;
}
return result;
}
/// <summary>
/// Handing for list of <see cref="DbExpressionBinding"/>s.
/// </summary>
/// <param name="list"></param>
/// <returns></returns>
private bool VisitAggregateList(IList<DbAggregate> list)
{
return VisitList<DbAggregate>(VisitAggregate, list);
}
/// <summary>
/// Handing for list of <see cref="DbExpressionBinding"/>s.
/// </summary>
/// <param name="list"></param>
/// <returns></returns>
private bool VisitExpressionBindingList(IList<DbExpressionBinding> list)
{
return VisitList<DbExpressionBinding>(VisitExpressionBinding, list);
}
/// <summary>
/// Handing for list of <see cref="DbExpression"/>s.
/// </summary>
/// <param name="list"></param>
/// <returns></returns>
private bool VisitExpressionList(IList<DbExpression> list)
{
return VisitList<DbExpression>(VisitExpression, list);
}
/// <summary>
/// Handling for list of <see cref="DbSortClause"/>s.
/// </summary>
/// <param name="list"></param>
/// <returns></returns>
private bool VisitSortClauseList(IList<DbSortClause> list)
{
return VisitList<DbSortClause>(VisitSortClause, list);
}
#endregion
#region DbExpressionVisitor Members
/// <summary>
/// Called when an <see cref="DbExpression"/> of an otherwise unrecognized type is encountered.
/// </summary>
/// <param name="expression">The expression</param>
/// <returns></returns>
/// <exception cref="NotSupportedException">Always thrown if this method is called, since it indicates that <paramref name="expression"/> is of an unsupported type</exception>
public override bool Visit(DbExpression expression)
{
throw EntityUtil.NotSupported(System.Data.Entity.Strings.Cqt_General_UnsupportedExpression(expression.GetType().FullName));
}
/// <summary>
/// <see cref="VisitBinaryExpression"/>
/// </summary>
/// <param name="expression">The DbAndExpression that is being visited.</param>
/// <returns></returns>
public override bool Visit(DbAndExpression expression)
{
return VisitBinaryExpression(expression);
}
/// <summary>
/// Not supported on SQL Server 2000.
/// </summary>
/// <param name="expression">The DbApplyExpression that is being visited.</param>
/// <exception cref="NotSupportedException">Always</exception>
public override bool Visit(DbApplyExpression expression)
{
throw EntityUtil.NotSupported(System.Data.Entity.Strings.SqlGen_ApplyNotSupportedOnSql8);
}
/// <summary>
/// Default handling for DbArithmeticExpression. Visits all arguments.
/// </summary>
/// <param name="expression">The DbArithmeticExpression that is being visited.</param>
/// <returns></returns>
public override bool Visit(DbArithmeticExpression expression)
{
return VisitExpressionList(expression.Arguments);
}
/// <summary>
/// Walks the strucutre
/// </summary>
/// <param name="expression">The DbCaseExpression that is being visited.</param>
/// <returns></returns>
public override bool Visit(DbCaseExpression expression)
{
bool whenNeedsRewrite = VisitExpressionList(expression.When);
bool thenNeedsRewrite = VisitExpressionList(expression.Then);
bool elseNeedsRewrite = VisitExpression(expression.Else);
return whenNeedsRewrite || thenNeedsRewrite || elseNeedsRewrite;
}
/// <summary>
/// <see cref="VisitUnaryExpression"/>
/// </summary>
/// <param name="expression">The DbCastExpression that is being visited.</param>
/// <returns></returns>
public override bool Visit(DbCastExpression expression)
{
return VisitUnaryExpression(expression);
}
/// <summary>
/// <see cref="VisitBinaryExpression"/>
/// </summary>
/// <param name="expression">The DbComparisonExpression that is being visited.</param>
/// <returns></returns>
public override bool Visit(DbComparisonExpression expression)
{
return VisitBinaryExpression(expression);
}
/// <summary>
/// Returns false
/// </summary>
/// <param name="expression">The DbConstantExpression that is being visited.</param>
/// <returns></returns>
public override bool Visit(DbConstantExpression expression)
{
return false;
}
/// <summary>
/// Walks the structure
/// </summary>
/// <param name="expression">The DbCrossJoinExpression that is being visited.</param>
/// <returns></returns>
public override bool Visit(DbCrossJoinExpression expression)
{
return VisitExpressionBindingList(expression.Inputs);
}
/// <summary>
/// <see cref="VisitUnaryExpression"/>
/// </summary>
/// <param name="expression">The DeRefExpression that is being visited.</param>
/// <returns></returns>
public override bool Visit(DbDerefExpression expression)
{
return VisitUnaryExpression(expression);
}
/// <summary>
/// <see cref="VisitUnaryExpression"/>
/// </summary>
/// <param name="expression">The DbDistinctExpression that is being visited.</param>
/// <returns></returns>
public override bool Visit(DbDistinctExpression expression)
{
return VisitUnaryExpression(expression);
}
/// <summary>
/// <see cref="VisitUnaryExpression"/>
/// </summary>
/// <param name="expression">The DbElementExpression that is being visited.</param>
/// <returns></returns>
public override bool Visit(DbElementExpression expression)
{
return VisitUnaryExpression(expression);
}
/// <summary>
/// <see cref="VisitUnaryExpression"/>
/// </summary>
/// <param name="expression">The DbEntityRefExpression that is being visited.</param>
/// <returns></returns>
public override bool Visit(DbEntityRefExpression expression)
{
return VisitUnaryExpression(expression);
}
/// <summary>
/// Returns true, the tree needs to be rewritten.
/// </summary>
/// <param name="expression">The DbExceptExpression that is being visited.</param>
/// <returns></returns>
public override bool Visit(DbExceptExpression expression)
{
//Walk the structure in case a non-supported construct is encountered
VisitExpression(expression.Left);
VisitExpression(expression.Right);
return true;
}
/// <summary>
/// Walks the structure
/// </summary>
/// <param name="expression">The DbFilterExpression that is being visited.</param>
/// <returns></returns>
public override bool Visit(DbFilterExpression expression)
{
bool inputNeedsRewrite = VisitExpressionBinding(expression.Input);
bool predicateNeedsRewrite = VisitExpression(expression.Predicate);
return inputNeedsRewrite || predicateNeedsRewrite;
}
/// <summary>
/// Visits the arguments
/// </summary>
/// <param name="expression">The DbFunctionExpression that is being visited.</param>
/// <returns></returns>
public override bool Visit(DbFunctionExpression expression)
{
return VisitExpressionList(expression.Arguments);
}
/// <summary>
/// Visits the arguments and lambda body
/// </summary>
/// <param name="expression">The DbLambdaExpression that is being visited.</param>
/// <returns></returns>
public override bool Visit(DbLambdaExpression expression)
{
bool argumentsNeedRewrite = VisitExpressionList(expression.Arguments);
bool bodyNeedsRewrite = VisitExpression(expression.Lambda.Body);
return argumentsNeedRewrite || bodyNeedsRewrite;
}
/// <summary>
/// Walks the structure
/// </summary>
/// <param name="expression">The DbExpression that is being visited.</param>
/// <returns></returns>
public override bool Visit(DbGroupByExpression expression)
{
bool inputNeedsRewrite = VisitExpression(expression.Input.Expression);
bool keysNeedRewrite = VisitExpressionList(expression.Keys);
bool aggregatesNeedRewrite = VisitAggregateList(expression.Aggregates);
return inputNeedsRewrite || keysNeedRewrite || aggregatesNeedRewrite;
}
/// <summary>
/// Returns true.
/// </summary>
/// <param name="expression">The DbIntersectExpression that is being visited.</param>
/// <returns></returns>
public override bool Visit(DbIntersectExpression expression)
{
//Walk the structure in case a non-supported construct is encountered
VisitExpression(expression.Left);
VisitExpression(expression.Right);
return true;
}
/// <summary>
/// <see cref="VisitUnaryExpression"/>
/// </summary>
/// <param name="expression">The DbIsEmptyExpression that is being visited.</param>
/// <returns></returns>
public override bool Visit(DbIsEmptyExpression expression)
{
return VisitUnaryExpression(expression);
}
/// <summary>
/// <see cref="VisitUnaryExpression"/>
/// </summary>
/// <param name="expression">The DbIsNullExpression that is being visited.</param>
/// <returns></returns>
public override bool Visit(DbIsNullExpression expression)
{
return VisitUnaryExpression(expression);
}
/// <summary>
/// <see cref="VisitUnaryExpression"/>
/// </summary>
/// <param name="expression">The DbIsOfExpression that is being visited.</param>
/// <returns></returns>
public override bool Visit(DbIsOfExpression expression)
{
return VisitUnaryExpression(expression);
}
/// <summary>
/// Walks the structure
/// </summary>
/// <param name="expression">The DbJoinExpression that is being visited.</param>
/// <returns></returns>
public override bool Visit(DbJoinExpression expression)
{
bool leftNeedsRewrite = VisitExpressionBinding(expression.Left);
bool rightNeedsRewrite = VisitExpressionBinding(expression.Right);
bool conditionNeedsRewrite = VisitExpression(expression.JoinCondition);
return leftNeedsRewrite || rightNeedsRewrite || conditionNeedsRewrite;
}
/// <summary>
/// Walks the structure
/// </summary>
/// <param name="expression">The DbLikeExpression that is being visited.</param>
/// <returns></returns>
public override bool Visit(DbLikeExpression expression)
{
bool argumentNeedsRewrite = VisitExpression(expression.Argument);
bool patternNeedsRewrite = VisitExpression(expression.Pattern);
bool excapeNeedsRewrite = VisitExpression(expression.Escape);
return argumentNeedsRewrite || patternNeedsRewrite || excapeNeedsRewrite;
}
/// <summary>
/// Walks the structure
/// </summary>
/// <param name="expression"></param>
/// <returns></returns>
/// <exception cref="NotSupportedException">expression.Limit is DbParameterReferenceExpression</exception>
public override bool Visit(DbLimitExpression expression)
{
if (expression.Limit is DbParameterReferenceExpression)
{
throw EntityUtil.NotSupported(System.Data.Entity.Strings.SqlGen_ParameterForLimitNotSupportedOnSql8);
}
return VisitExpression(expression.Argument);
}
#if METHOD_EXPRESSION
/// <summary>
/// Visitor pattern method for <see cref="MethodExpression"/>.
/// </summary>
/// <param name="expression">The MethodExpression that is being visited.</param>
/// <exception cref="ArgumentNullException"><paramref name="expression"/> is null</exception>
public override bool Visit(MethodExpression expression)
{
bool result = VisitExpressionList(expression.Arguments);
if (expression.Instance != null)
{
bool instanceNeedsRewrite = VisitExpression(expression.Instance);
result = result || instanceNeedsRewrite;
}
return result;
}
#endif
/// <summary>
/// Walks the arguments
/// </summary>
/// <param name="expression">The DbNewInstanceExpression that is being visited.</param>
/// <returns></returns>
public override bool Visit(DbNewInstanceExpression expression)
{
return VisitExpressionList(expression.Arguments);
}
/// <summary>
/// <see cref="VisitUnaryExpression"/>
/// </summary>
/// <param name="expression">The DbNotExpression that is being visited.</param>
/// <returns></returns>
public override bool Visit(DbNotExpression expression)
{
return VisitUnaryExpression(expression);
}
/// <summary>
/// Returns false
/// </summary>
/// <param name="expression">The DbNullExpression that is being visited.</param>
/// <returns>false</returns>
public override bool Visit(DbNullExpression expression)
{
return false;
}
/// <summary>
/// <see cref="VisitUnaryExpression"/>
/// </summary>
/// <param name="expression">The DbOfTypeExpression that is being visited.</param>
/// <returns></returns>
public override bool Visit(DbOfTypeExpression expression)
{
return VisitUnaryExpression(expression);
}
/// <summary>
/// <see cref="VisitBinaryExpression"/>
/// </summary>
/// <param name="expression">The DbOrExpression that is being visited.</param>
/// <returns></returns>
public override bool Visit(DbOrExpression expression)
{
return VisitBinaryExpression(expression);
}
/// <summary>
/// Returns false
/// </summary>
/// <param name="expression">The DbParameterReferenceExpression that is being visited.</param>
/// <returns></returns>
public override bool Visit(DbParameterReferenceExpression expression)
{
return false;
}
/// <summary>
/// Walks the structure
/// </summary>
/// <param name="expression">The DbProjectExpression that is being visited.</param>
/// <returns></returns>
public override bool Visit(DbProjectExpression expression)
{
bool inputNeedsRewrite = VisitExpressionBinding(expression.Input);
bool projectionNeedsRewrite = VisitExpression(expression.Projection);
return inputNeedsRewrite || projectionNeedsRewrite;
}
/// <summary>
/// Returns false
/// </summary>
/// <param name="expression">The DbPropertyExpression that is being visited.</param>
/// <returns></returns>
public override bool Visit(DbPropertyExpression expression)
{
return VisitExpression(expression.Instance);
}
/// <summary>
/// Walks the structure
/// </summary>
/// <param name="expression">The DbQuantifierExpression that is being visited.</param>
/// <returns></returns>
public override bool Visit(DbQuantifierExpression expression)
{
bool inputNeedsRewrite = VisitExpressionBinding(expression.Input);
bool predicateNeedsRewrite = VisitExpression(expression.Predicate);
return inputNeedsRewrite || predicateNeedsRewrite;
}
/// <summary>
/// <see cref="VisitUnaryExpression"/>
/// </summary>
/// <param name="expression">The DbRefExpression that is being visited.</param>
/// <returns></returns>
public override bool Visit(DbRefExpression expression)
{
return VisitUnaryExpression(expression);
}
/// <summary>
/// <see cref="VisitUnaryExpression"/>
/// </summary>
/// <param name="expression">The DbRefKeyExpression that is being visited.</param>
/// <returns></returns>
public override bool Visit(DbRefKeyExpression expression)
{
return VisitUnaryExpression(expression);
}
/// <summary>
/// Walks the structure
/// </summary>
/// <param name="expression">The DbRelationshipNavigationExpression that is being visited.</param>
/// <returns></returns>
public override bool Visit(DbRelationshipNavigationExpression expression)
{
return VisitExpression(expression.NavigationSource);
}
/// <summary>
/// Returns false;
/// </summary>
/// <param name="expression">The DbScanExpression that is being visited.</param>
/// <returns></returns>
public override bool Visit(DbScanExpression expression)
{
return false;
}
/// <summary>
/// Resturns true
/// </summary>
/// <param name="expression"></param>
/// <returns></returns>
/// <exception cref="NotSupportedException">expression.Count is DbParameterReferenceExpression</exception>
public override bool Visit(DbSkipExpression expression)
{
if (expression.Count is DbParameterReferenceExpression)
{
throw EntityUtil.NotSupported(System.Data.Entity.Strings.SqlGen_ParameterForSkipNotSupportedOnSql8);
}
//Walk the structure in case a non-supported construct is encountered
VisitExpressionBinding(expression.Input);
VisitSortClauseList(expression.SortOrder);
VisitExpression(expression.Count);
return true;
}
/// <summary>
/// Walks the structure
/// </summary>
/// <param name="expression">The DbSortExpression that is being visited.</param>
/// <returns></returns>
public override bool Visit(DbSortExpression expression)
{
bool inputNeedsRewrite = VisitExpressionBinding(expression.Input);
bool sortClauseNeedsRewrite = VisitSortClauseList(expression.SortOrder);
return inputNeedsRewrite || sortClauseNeedsRewrite;
}
/// <summary>
/// <see cref="VisitUnaryExpression"/>
/// </summary>
/// <param name="expression">The DbTreatExpression that is being visited.</param>
/// <returns></returns>
public override bool Visit(DbTreatExpression expression)
{
return VisitUnaryExpression(expression);
}
/// <summary>
/// <see cref="VisitBinaryExpression"/>
/// </summary>
/// <param name="expression">The DbUnionAllExpression that is being visited.</param>
/// <returns></returns>
public override bool Visit(DbUnionAllExpression expression)
{
return VisitBinaryExpression(expression);
}
/// <summary>
/// Returns false
/// </summary>
/// <param name="expression">The DbVariableReferenceExpression that is being visited.</param>
/// <returns>false</returns>
public override bool Visit(DbVariableReferenceExpression expression)
{
return false;
}
#endregion
}
}
|