query.vala 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  1. using Invercargill.Expressions;
  2. using Invercargill.DataStructures;
  3. namespace InvercargillSql.Orm {
  4. /**
  5. * Represents an ORDER BY clause with expression and direction.
  6. */
  7. public class OrderByClause : Object {
  8. /**
  9. * The expression to order by.
  10. */
  11. public Expression expression { get; construct; }
  12. /**
  13. * Whether to order in descending order.
  14. */
  15. public bool descending { get; construct; }
  16. /**
  17. * Creates a new OrderByClause.
  18. *
  19. * @param expression The expression to order by
  20. * @param descending True for descending order, false for ascending
  21. */
  22. public OrderByClause(Expression expression, bool descending) {
  23. Object(expression: expression, descending: descending);
  24. }
  25. }
  26. /**
  27. * Abstract base class for all queries.
  28. *
  29. * Query<T> provides a fluent interface for building database queries.
  30. * Subclasses implement specific query execution strategies for entities
  31. * and projections.
  32. *
  33. * Important: Query<T> does NOT extend Enumerable<T>. Instead, call
  34. * materialise() or materialise_async() to execute the query and get results.
  35. *
  36. * Example usage:
  37. * {{{
  38. * var users = session.query<User>()
  39. * .where(expr("age > $0", new NativeElement<int?>(18)))
  40. * .order_by(expr("name"))
  41. * .limit(10)
  42. * .materialise();
  43. * }}}
  44. */
  45. public abstract class Query<T> : Object {
  46. // Shared state - protected for subclass access
  47. protected OrmSession _session;
  48. protected Vector<OrderByClause> _orderings;
  49. protected int64? _limit;
  50. protected int64? _offset;
  51. protected bool _use_or;
  52. protected Vector<Expression> _where_expressions;
  53. /**
  54. * Creates a new Query for the given session.
  55. *
  56. * This constructor is protected - use OrmSession.query<T>() to create queries.
  57. *
  58. * @param session The OrmSession that will execute this query
  59. */
  60. protected Query(OrmSession session) {
  61. _session = session;
  62. _orderings = new Vector<OrderByClause>();
  63. _where_expressions = new Vector<Expression>();
  64. _limit = null;
  65. _offset = null;
  66. _use_or = false;
  67. }
  68. // === Internal accessors for OrmSession compatibility ===
  69. /**
  70. * The filter expression for this query (for entity queries).
  71. * Returns null for projection queries which use string-based where clauses.
  72. */
  73. internal abstract owned Expression? filter { owned get; }
  74. /**
  75. * The ORDER BY clauses for this query.
  76. */
  77. internal abstract Vector<OrderByClause> orderings { get; }
  78. /**
  79. * The LIMIT value for this query, or null if not set.
  80. */
  81. internal abstract int64? limit_value { get; }
  82. /**
  83. * The OFFSET value for this query, or null if not set.
  84. */
  85. internal abstract int64? offset_value { get; }
  86. // === Fluent query builders ===
  87. /**
  88. * Adds a WHERE clause using an Expression.
  89. *
  90. * Use the expr() convenience function to create expressions:
  91. * {{{
  92. * .where(expr("age > $0", new NativeElement<int?>(18)))
  93. * }}}
  94. *
  95. * @param expression The filter expression
  96. * @return This query for method chaining
  97. */
  98. public virtual Query<T> where(Expression expression) {
  99. _where_expressions.add(expression);
  100. return this;
  101. }
  102. /**
  103. * Adds an OR WHERE clause using an Expression.
  104. *
  105. * Multiple where clauses will be combined with OR instead of AND.
  106. *
  107. * @param expression The filter expression
  108. * @return This query for method chaining
  109. */
  110. public virtual Query<T> or_where(Expression expression) {
  111. _use_or = true;
  112. _where_expressions.add(expression);
  113. return this;
  114. }
  115. /**
  116. * Adds an ascending ORDER BY clause.
  117. *
  118. * @param expression The expression to order by
  119. * @return This query for method chaining
  120. */
  121. public virtual Query<T> order_by(Expression expression) {
  122. _orderings.add(new OrderByClause(expression, false));
  123. return this;
  124. }
  125. /**
  126. * Adds a descending ORDER BY clause.
  127. *
  128. * @param expression The expression to order by
  129. * @return This query for method chaining
  130. */
  131. public virtual Query<T> order_by_desc(Expression expression) {
  132. _orderings.add(new OrderByClause(expression, true));
  133. return this;
  134. }
  135. /**
  136. * Sets the maximum number of results to return.
  137. *
  138. * @param count The maximum number of results
  139. * @return This query for method chaining
  140. */
  141. public virtual Query<T> limit(int64 count) {
  142. _limit = count;
  143. return this;
  144. }
  145. /**
  146. * Sets the number of results to skip.
  147. *
  148. * @param count The number of results to skip
  149. * @return This query for method chaining
  150. */
  151. public virtual Query<T> offset(int64 count) {
  152. _offset = count;
  153. return this;
  154. }
  155. // === Execution methods ===
  156. /**
  157. * Executes the query and returns the results as an ImmutableLot.
  158. *
  159. * @return An ImmutableLot<T> containing the query results
  160. * @throws SqlError if query execution fails
  161. */
  162. public abstract Invercargill.ImmutableLot<T> materialise() throws SqlError;
  163. /**
  164. * Executes the query asynchronously and returns the results.
  165. *
  166. * @return An ImmutableLot<T> containing the query results
  167. * @throws SqlError if query execution fails
  168. */
  169. public abstract async Invercargill.ImmutableLot<T> materialise_async() throws SqlError;
  170. /**
  171. * Executes the query and returns the first result.
  172. *
  173. * This method automatically applies a limit of 1 before executing.
  174. *
  175. * @return The first result, or null if no results
  176. * @throws SqlError if query execution fails
  177. */
  178. public virtual T? first() throws SqlError {
  179. _limit = 1;
  180. var results = materialise();
  181. if (results.length > 0) {
  182. return results.first();
  183. }
  184. return null;
  185. }
  186. /**
  187. * Executes the query asynchronously and returns the first result.
  188. *
  189. * This method automatically applies a limit of 1 before executing.
  190. *
  191. * @return The first result, or null if no results
  192. * @throws SqlError if query execution fails
  193. */
  194. public virtual async T? first_async() throws SqlError {
  195. _limit = 1;
  196. var results = yield materialise_async();
  197. if (results.length > 0) {
  198. return results.first();
  199. }
  200. return null;
  201. }
  202. /**
  203. * Returns the SQL for this query.
  204. *
  205. * @return The SQL string for this query
  206. */
  207. public abstract string to_sql();
  208. // === Protected helpers ===
  209. /**
  210. * Combines all where expressions into a single Expression.
  211. *
  212. * If there are no where expressions, returns null.
  213. * If there is one expression, returns it directly.
  214. * If there are multiple expressions, combines them with AND or OR
  215. * based on the _use_or flag.
  216. *
  217. * @return The combined where expression, or null if no expressions
  218. */
  219. protected Expression? get_combined_where() {
  220. if (_where_expressions.length == 0) {
  221. return null;
  222. }
  223. if (_where_expressions.length == 1) {
  224. return _where_expressions.get(0);
  225. }
  226. // Combine multiple expressions with AND or OR
  227. BinaryOperator op = _use_or ? BinaryOperator.OR : BinaryOperator.AND;
  228. Expression? result = _where_expressions.get(0);
  229. for (uint i = 1; i < _where_expressions.length; i++) {
  230. result = new BinaryExpression(
  231. result,
  232. _where_expressions.get(i),
  233. op
  234. );
  235. }
  236. return result;
  237. }
  238. }
  239. }