TakeQueryState.cs 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. using Ant.DbExpressions;
  2. using Ant.Query.QueryExpressions;
  3. using System;
  4. namespace Ant.Query.QueryState
  5. {
  6. internal sealed class TakeQueryState : SubQueryState
  7. {
  8. int _count;
  9. public TakeQueryState(ResultElement resultElement, int count)
  10. : base(resultElement)
  11. {
  12. this.Count = count;
  13. }
  14. public int Count
  15. {
  16. get
  17. {
  18. return this._count;
  19. }
  20. set
  21. {
  22. this.CheckInputCount(value);
  23. this._count = value;
  24. }
  25. }
  26. void CheckInputCount(int count)
  27. {
  28. if (count < 0)
  29. {
  30. throw new ArgumentException("The take count could not less than 0.");
  31. }
  32. }
  33. public override IQueryState Accept(SelectExpression exp)
  34. {
  35. ResultElement result = this.CreateNewResult(exp.Selector);
  36. return this.CreateQueryState(result);
  37. }
  38. public override IQueryState Accept(TakeExpression exp)
  39. {
  40. if (exp.Count < this.Count)
  41. this.Count = exp.Count;
  42. return this;
  43. }
  44. public override IQueryState CreateQueryState(ResultElement result)
  45. {
  46. return new TakeQueryState(result, this.Count);
  47. }
  48. public override DbSqlQueryExpression CreateSqlQuery()
  49. {
  50. DbSqlQueryExpression sqlQuery = base.CreateSqlQuery();
  51. sqlQuery.TakeCount = this.Count;
  52. sqlQuery.SkipCount = null;
  53. return sqlQuery;
  54. }
  55. }
  56. }