SqlState.cs 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. namespace Ant.Core
  6. {
  7. public class SqlState : ISqlState
  8. {
  9. List<object> _sqlStorage;
  10. int _recursiveDepth = 0;
  11. public SqlState()
  12. : this(0)
  13. {
  14. }
  15. public int RecursiveDepth { get { return this._recursiveDepth; } }
  16. public SqlState(int capacity)
  17. {
  18. this._sqlStorage = new List<object>(capacity);
  19. }
  20. public static SqlState Create(object obj)
  21. {
  22. SqlState state = new SqlState(1);
  23. state.Append(obj);
  24. return state;
  25. }
  26. public static SqlState Create(object obj1, object obj2)
  27. {
  28. SqlState state = new SqlState(2);
  29. state.Append(obj1).Append(obj2);
  30. return state;
  31. }
  32. public static SqlState Create(object obj1, object obj2, object obj3)
  33. {
  34. SqlState state = new SqlState(3);
  35. state.Append(obj1).Append(obj2).Append(obj3);
  36. return state;
  37. }
  38. public static SqlState Create(object obj1, object obj2, object obj3, object obj4)
  39. {
  40. SqlState state = new SqlState(4);
  41. state.Append(obj1).Append(obj2).Append(obj3).Append(obj4);
  42. return state;
  43. }
  44. public static SqlState Create(object obj1, object obj2, object obj3, object obj4, object obj5)
  45. {
  46. SqlState state = new SqlState(5);
  47. state.Append(obj1).Append(obj2).Append(obj3).Append(obj4).Append(obj5);
  48. return state;
  49. }
  50. public static SqlState Create(params object[] objs)
  51. {
  52. if (objs == null)
  53. return new SqlState();
  54. SqlState state = new SqlState(objs.Length);
  55. return state.Append(objs);
  56. }
  57. public SqlState Append(object obj)
  58. {
  59. this._sqlStorage.Add(obj);
  60. return this;
  61. }
  62. public SqlState Append(object obj1, object obj2)
  63. {
  64. return this.Append(obj1).Append(obj2);
  65. }
  66. public SqlState Append(object obj1, object obj2, object obj3)
  67. {
  68. return this.Append(obj1).Append(obj2).Append(obj3);
  69. }
  70. public SqlState Append(object obj1, object obj2, object obj3, object obj4)
  71. {
  72. return this.Append(obj1).Append(obj2).Append(obj3).Append(obj4);
  73. }
  74. public SqlState Append(object obj1, object obj2, object obj3, object obj4, object obj5)
  75. {
  76. return this.Append(obj1).Append(obj2).Append(obj3).Append(obj4).Append(obj5);
  77. }
  78. public SqlState Append(params object[] objs)
  79. {
  80. if (objs == null)
  81. return this;
  82. foreach (object obj in objs)
  83. {
  84. this.Append(obj);
  85. }
  86. return this;
  87. }
  88. public override string ToString()
  89. {
  90. return this.ToSql();
  91. }
  92. public string ToSql()
  93. {
  94. string s = null;
  95. StringBuilder sb = new StringBuilder();
  96. this._recursiveDepth = this.ToSql(sb);
  97. s = sb.ToString();
  98. return s;
  99. }
  100. public int ToSql(StringBuilder sb)
  101. {
  102. this._recursiveDepth = 0;
  103. for (int i = 0; i < this._sqlStorage.Count; i++)
  104. {
  105. var obj = this._sqlStorage[i];
  106. SqlState state = obj as SqlState;
  107. if (state != null)
  108. {
  109. int recursiveDepth = 1;
  110. recursiveDepth += state.ToSql(sb);
  111. if (this._recursiveDepth < recursiveDepth)
  112. this._recursiveDepth = recursiveDepth;
  113. continue;
  114. }
  115. sb.Append(obj);
  116. }
  117. return this._recursiveDepth;
  118. }
  119. StringBuilder InnerToSql()
  120. {
  121. StringBuilder sb = new StringBuilder();
  122. Stack<object> stack = new Stack<object>();
  123. stack.Push(this);
  124. do
  125. {
  126. object obj = stack.Pop();
  127. SqlState state = obj as SqlState;
  128. if (state != null)
  129. {
  130. for (int i = state._sqlStorage.Count - 1; i >= 0; i--)
  131. {
  132. stack.Push(state._sqlStorage[i]);
  133. }
  134. }
  135. else
  136. {
  137. sb.Append(obj);
  138. }
  139. }
  140. while (stack.Count > 0);
  141. return sb;
  142. }
  143. }
  144. }