LimitQueryState.cs 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. using Ant.DbExpressions;
  2. using Ant.Query.QueryExpressions;
  3. using System;
  4. using System.Linq.Expressions;
  5. namespace Ant.Query.QueryState
  6. {
  7. internal sealed class LimitQueryState : SubQueryState
  8. {
  9. int _skipCount;
  10. int _takeCount;
  11. public LimitQueryState(ResultElement resultElement, int skipCount, int takeCount)
  12. : base(resultElement)
  13. {
  14. this.SkipCount = skipCount;
  15. this.TakeCount = takeCount;
  16. }
  17. public int SkipCount
  18. {
  19. get
  20. {
  21. return this._skipCount;
  22. }
  23. set
  24. {
  25. this.CheckInputCount(value, "skip");
  26. this._skipCount = value;
  27. }
  28. }
  29. public int TakeCount
  30. {
  31. get
  32. {
  33. return this._takeCount;
  34. }
  35. set
  36. {
  37. this.CheckInputCount(value, "take");
  38. this._takeCount = value;
  39. }
  40. }
  41. void CheckInputCount(int count, string name)
  42. {
  43. if (count < 0)
  44. {
  45. throw new ArgumentException(string.Format("The {0} count could not less than 0.", name));
  46. }
  47. }
  48. public override IQueryState Accept(SelectExpression exp)
  49. {
  50. ResultElement result = this.CreateNewResult(exp.Selector);
  51. return this.CreateQueryState(result);
  52. }
  53. public override IQueryState Accept(TakeExpression exp)
  54. {
  55. if (exp.Count < this.TakeCount)
  56. this.TakeCount = exp.Count;
  57. return this;
  58. }
  59. public override IQueryState CreateQueryState(ResultElement result)
  60. {
  61. return new LimitQueryState(result, this.SkipCount, this.TakeCount);
  62. }
  63. public override DbSqlQueryExpression CreateSqlQuery()
  64. {
  65. DbSqlQueryExpression sqlQuery = base.CreateSqlQuery();
  66. sqlQuery.TakeCount = this.TakeCount;
  67. sqlQuery.SkipCount = this.SkipCount;
  68. return sqlQuery;
  69. }
  70. }
  71. }