/querydsl-sql/src/main/java/com/mysema/query/sql/SQLExpressions.java

http://github.com/mysema/querydsl · Java · 503 lines · 178 code · 56 blank · 269 comment · 0 complexity · 16e33e9e3caa922e948217fda37353cb MD5 · raw file

  1. /*
  2. * Copyright 2011, Mysema Ltd
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. * http://www.apache.org/licenses/LICENSE-2.0
  8. * Unless required by applicable law or agreed to in writing, software
  9. * distributed under the License is distributed on an "AS IS" BASIS,
  10. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  11. * See the License for the specific language governing permissions and
  12. * limitations under the License.
  13. */
  14. package com.mysema.query.sql;
  15. import java.util.HashMap;
  16. import java.util.Map;
  17. import com.mysema.query.types.ConstantImpl;
  18. import com.mysema.query.types.Expression;
  19. import com.mysema.query.types.Operator;
  20. import com.mysema.query.types.Ops;
  21. import com.mysema.query.types.expr.BooleanExpression;
  22. import com.mysema.query.types.expr.BooleanOperation;
  23. import com.mysema.query.types.expr.DateExpression;
  24. import com.mysema.query.types.expr.DateOperation;
  25. import com.mysema.query.types.expr.DateTimeExpression;
  26. import com.mysema.query.types.expr.DateTimeOperation;
  27. import com.mysema.query.types.expr.NumberExpression;
  28. import com.mysema.query.types.expr.NumberOperation;
  29. import com.mysema.query.types.expr.SimpleExpression;
  30. import com.mysema.query.types.expr.SimpleOperation;
  31. import com.mysema.query.types.expr.Wildcard;
  32. /**
  33. * Common SQL expressions
  34. *
  35. * @author tiwe
  36. *
  37. */
  38. @SuppressWarnings("rawtypes")
  39. public final class SQLExpressions {
  40. private static final Map<DatePart, Operator> DATE_ADD_OPS = new HashMap<DatePart, Operator>();
  41. private static final Map<DatePart, Operator> DATE_DIFF_OPS = new HashMap<DatePart, Operator>();
  42. private static final Map<DatePart, Operator> DATE_TRUNC_OPS = new HashMap<DatePart, Operator>();
  43. static {
  44. DATE_ADD_OPS.put(DatePart.year, Ops.DateTimeOps.ADD_YEARS);
  45. DATE_ADD_OPS.put(DatePart.month, Ops.DateTimeOps.ADD_MONTHS);
  46. DATE_ADD_OPS.put(DatePart.week, Ops.DateTimeOps.ADD_WEEKS);
  47. DATE_ADD_OPS.put(DatePart.day, Ops.DateTimeOps.ADD_DAYS);
  48. DATE_ADD_OPS.put(DatePart.hour, Ops.DateTimeOps.ADD_HOURS);
  49. DATE_ADD_OPS.put(DatePart.minute, Ops.DateTimeOps.ADD_MINUTES);
  50. DATE_ADD_OPS.put(DatePart.second, Ops.DateTimeOps.ADD_SECONDS);
  51. DATE_ADD_OPS.put(DatePart.millisecond, null); // TODO
  52. DATE_DIFF_OPS.put(DatePart.year, Ops.DateTimeOps.DIFF_YEARS);
  53. DATE_DIFF_OPS.put(DatePart.month, Ops.DateTimeOps.DIFF_MONTHS);
  54. DATE_DIFF_OPS.put(DatePart.week, Ops.DateTimeOps.DIFF_WEEKS);
  55. DATE_DIFF_OPS.put(DatePart.day, Ops.DateTimeOps.DIFF_DAYS);
  56. DATE_DIFF_OPS.put(DatePart.hour, Ops.DateTimeOps.DIFF_HOURS);
  57. DATE_DIFF_OPS.put(DatePart.minute, Ops.DateTimeOps.DIFF_MINUTES);
  58. DATE_DIFF_OPS.put(DatePart.second, Ops.DateTimeOps.DIFF_SECONDS);
  59. DATE_DIFF_OPS.put(DatePart.millisecond, null); // TODO
  60. DATE_TRUNC_OPS.put(DatePart.year, Ops.DateTimeOps.TRUNC_YEAR);
  61. DATE_TRUNC_OPS.put(DatePart.month, Ops.DateTimeOps.TRUNC_MONTH);
  62. DATE_TRUNC_OPS.put(DatePart.week, Ops.DateTimeOps.TRUNC_WEEK);
  63. DATE_TRUNC_OPS.put(DatePart.day, Ops.DateTimeOps.TRUNC_DAY);
  64. DATE_TRUNC_OPS.put(DatePart.hour, Ops.DateTimeOps.TRUNC_HOUR);
  65. DATE_TRUNC_OPS.put(DatePart.minute, Ops.DateTimeOps.TRUNC_MINUTE);
  66. DATE_TRUNC_OPS.put(DatePart.second, Ops.DateTimeOps.TRUNC_SECOND);
  67. }
  68. private static final WindowOver<Long> rank = new WindowOver<Long>(Long.class, SQLTemplates.RANK);
  69. private static final WindowOver<Long> denseRank = new WindowOver<Long>(Long.class, SQLTemplates.DENSERANK);
  70. private static final WindowOver<Long> rowNumber = new WindowOver<Long>(Long.class, SQLTemplates.ROWNUMBER);
  71. /**
  72. * Wildcard expression
  73. */
  74. public static final Expression<Object[]> all = Wildcard.all;
  75. /**
  76. * Wilcard count expression
  77. */
  78. public static final Expression<Long> countAll = Wildcard.count;
  79. /**
  80. * Get an aggregate any expression for the given boolean expression
  81. */
  82. public static BooleanExpression any(BooleanExpression expr) {
  83. return BooleanOperation.create(Ops.AggOps.BOOLEAN_ANY, expr);
  84. }
  85. /**
  86. * Get an aggregate all expression for the given boolean expression
  87. */
  88. public static BooleanExpression all(BooleanExpression expr) {
  89. return BooleanOperation.create(Ops.AggOps.BOOLEAN_ALL, expr);
  90. }
  91. /**
  92. * Get a nextval(sequence) expression
  93. *
  94. * @param sequence
  95. * @return
  96. */
  97. public static final SimpleExpression<Long> nextval(String sequence) {
  98. return nextval(Long.class, sequence);
  99. }
  100. /**
  101. * Get a nextval(sequence) expression of the given type
  102. *
  103. * @param type
  104. * @param sequence
  105. * @return
  106. */
  107. public static final <T extends Number> SimpleExpression<T> nextval(Class<T> type, String sequence) {
  108. return SimpleOperation.create(type, SQLTemplates.NEXTVAL, ConstantImpl.create(sequence));
  109. }
  110. /**
  111. * Convert timestamp to date
  112. *
  113. * @param dateTime
  114. * @return
  115. */
  116. public static <D extends Comparable> DateExpression<D> date(DateTimeExpression<D> dateTime) {
  117. return DateOperation.create((Class)dateTime.getType(), Ops.DateTimeOps.DATE, dateTime);
  118. }
  119. /**
  120. * Convert timestamp to date
  121. *
  122. * @param type
  123. * @param dateTime
  124. * @return
  125. */
  126. public static <D extends Comparable> DateExpression<D> date(Class<D> type, DateTimeExpression<?> dateTime) {
  127. return DateOperation.create(type, Ops.DateTimeOps.DATE, dateTime);
  128. }
  129. /**
  130. * Get a dateadd(unit, date, amount) expression
  131. *
  132. * @param unit
  133. * @param date
  134. * @param amount
  135. * @return
  136. */
  137. public static <D extends Comparable> DateTimeExpression<D> dateadd(DatePart unit, DateTimeExpression<D> date, int amount) {
  138. return DateTimeOperation.create((Class)date.getType(), DATE_ADD_OPS.get(unit), date, ConstantImpl.create(amount));
  139. }
  140. /**
  141. * Get a dateadd(unit, date, amount) expression
  142. *
  143. * @param unit
  144. * @param date
  145. * @param amount
  146. * @return
  147. */
  148. public static <D extends Comparable> DateExpression<D> dateadd(DatePart unit, DateExpression<D> date, int amount) {
  149. return DateOperation.create((Class)date.getType(), DATE_ADD_OPS.get(unit), date, ConstantImpl.create(amount));
  150. }
  151. /**
  152. * Get a datediff(unit, start, end) expression
  153. *
  154. * @param unit
  155. * @param start
  156. * @param end
  157. * @return
  158. */
  159. public static <D extends Comparable> NumberExpression<Integer> datediff(DatePart unit,
  160. DateExpression<D> start, DateExpression<D> end) {
  161. return NumberOperation.create(Integer.class, DATE_DIFF_OPS.get(unit), start, end);
  162. }
  163. /**
  164. * Get a datediff(unit, start, end) expression
  165. *
  166. * @param unit
  167. * @param start
  168. * @param end
  169. * @return
  170. */
  171. public static <D extends Comparable> NumberExpression<Integer> datediff(DatePart unit,
  172. D start, DateExpression<D> end) {
  173. return NumberOperation.create(Integer.class, DATE_DIFF_OPS.get(unit), new ConstantImpl<D>(start), end);
  174. }
  175. /**
  176. * Get a datediff(unit, start, end) expression
  177. *
  178. * @param unit
  179. * @param start
  180. * @param end
  181. * @return
  182. */
  183. public static <D extends Comparable> NumberExpression<Integer> datediff(DatePart unit,
  184. DateExpression<D> start, D end) {
  185. return NumberOperation.create(Integer.class, DATE_DIFF_OPS.get(unit), start, new ConstantImpl<D>(end));
  186. }
  187. /**
  188. * Get a datediff(unit, start, end) expression
  189. *
  190. * @param unit
  191. * @param start
  192. * @param end
  193. * @return
  194. */
  195. public static <D extends Comparable> NumberExpression<Integer> datediff(DatePart unit,
  196. DateTimeExpression<D> start, DateTimeExpression<D> end) {
  197. return NumberOperation.create(Integer.class, DATE_DIFF_OPS.get(unit), start, end);
  198. }
  199. /**
  200. * Get a datediff(unit, start, end) expression
  201. *
  202. * @param unit
  203. * @param start
  204. * @param end
  205. * @return
  206. */
  207. public static <D extends Comparable> NumberExpression<Integer> datediff(DatePart unit,
  208. D start, DateTimeExpression<D> end) {
  209. return NumberOperation.create(Integer.class, DATE_DIFF_OPS.get(unit), new ConstantImpl<D>(start), end);
  210. }
  211. /**
  212. * Get a datediff(unit, start, end) expression
  213. *
  214. * @param unit
  215. * @param start
  216. * @param end
  217. * @return
  218. */
  219. public static <D extends Comparable> NumberExpression<Integer> datediff(DatePart unit,
  220. DateTimeExpression<D> start, D end) {
  221. return NumberOperation.create(Integer.class, DATE_DIFF_OPS.get(unit), start, new ConstantImpl<D>(end));
  222. }
  223. /**
  224. * Truncate the given date expression
  225. *
  226. * @param unit
  227. * @param expr
  228. */
  229. public static <D extends Comparable> DateExpression<D> datetrunc(DatePart unit, DateExpression<D> expr) {
  230. return DateOperation.create(expr.getType(), DATE_TRUNC_OPS.get(unit), expr);
  231. }
  232. /**
  233. * Truncate the given datetime expression
  234. *
  235. * @param unit
  236. * @param expr
  237. */
  238. public static <D extends Comparable> DateTimeExpression<D> datetrunc(DatePart unit, DateTimeExpression<D> expr) {
  239. return DateTimeOperation.create(expr.getType(), DATE_TRUNC_OPS.get(unit), expr);
  240. }
  241. /**
  242. * Add the given amount of years to the date
  243. *
  244. * @param date
  245. * @param years
  246. * @return
  247. */
  248. public static <D extends Comparable> DateTimeExpression<D> addYears(DateTimeExpression<D> date, int years) {
  249. return DateTimeOperation.create((Class)date.getType(), Ops.DateTimeOps.ADD_YEARS, date, ConstantImpl.create(years));
  250. }
  251. /**
  252. * Add the given amount of months to the date
  253. *
  254. * @param date
  255. * @param months
  256. * @return
  257. */
  258. public static <D extends Comparable> DateTimeExpression<D> addMonths(DateTimeExpression<D> date, int months) {
  259. return DateTimeOperation.create((Class)date.getType(), Ops.DateTimeOps.ADD_MONTHS, date, ConstantImpl.create(months));
  260. }
  261. /**
  262. * Add the given amount of weeks to the date
  263. *
  264. * @param date
  265. * @param weeks
  266. * @return
  267. */
  268. public static <D extends Comparable> DateTimeExpression<D> addWeeks(DateTimeExpression<D> date, int weeks) {
  269. return DateTimeOperation.create((Class)date.getType(), Ops.DateTimeOps.ADD_WEEKS, date, ConstantImpl.create(weeks));
  270. }
  271. /**
  272. * Add the given amount of days to the date
  273. *
  274. * @param date
  275. * @param days
  276. * @return
  277. */
  278. public static <D extends Comparable> DateTimeExpression<D> addDays(DateTimeExpression<D> date, int days) {
  279. return DateTimeOperation.create((Class)date.getType(), Ops.DateTimeOps.ADD_DAYS, date, ConstantImpl.create(days));
  280. }
  281. /**
  282. * Add the given amount of hours to the date
  283. *
  284. * @param date
  285. * @param hours
  286. * @return
  287. */
  288. public static <D extends Comparable> DateTimeExpression<D> addHours(DateTimeExpression<D> date, int hours) {
  289. return DateTimeOperation.create((Class)date.getType(), Ops.DateTimeOps.ADD_HOURS, date, ConstantImpl.create(hours));
  290. }
  291. /**
  292. * Add the given amount of minutes to the date
  293. *
  294. * @param date
  295. * @param minutes
  296. * @return
  297. */
  298. public static <D extends Comparable> DateTimeExpression<D> addMinutes(DateTimeExpression<D> date, int minutes) {
  299. return DateTimeOperation.create((Class)date.getType(), Ops.DateTimeOps.ADD_MINUTES, date, ConstantImpl.create(minutes));
  300. }
  301. /**
  302. * Add the given amount of seconds to the date
  303. *
  304. * @param date
  305. * @param seconds
  306. * @return
  307. */
  308. public static <D extends Comparable> DateTimeExpression<D> addSeconds(DateTimeExpression<D> date, int seconds) {
  309. return DateTimeOperation.create((Class)date.getType(), Ops.DateTimeOps.ADD_SECONDS, date, ConstantImpl.create(seconds));
  310. }
  311. /**
  312. * Add the given amount of years to the date
  313. *
  314. * @param date
  315. * @param years
  316. * @return
  317. */
  318. public static <D extends Comparable> DateExpression<D> addYears(DateExpression<D> date, int years) {
  319. return DateOperation.create((Class)date.getType(), Ops.DateTimeOps.ADD_YEARS, date, ConstantImpl.create(years));
  320. }
  321. /**
  322. * Add the given amount of months to the date
  323. *
  324. * @param date
  325. * @param months
  326. * @return
  327. */
  328. public static <D extends Comparable> DateExpression<D> addMonths(DateExpression<D> date, int months) {
  329. return DateOperation.create((Class)date.getType(), Ops.DateTimeOps.ADD_MONTHS, date, ConstantImpl.create(months));
  330. }
  331. /**
  332. * Add the given amount of weeks to the date
  333. *
  334. * @param date
  335. * @param weeks
  336. * @return
  337. */
  338. public static <D extends Comparable> DateExpression<D> addWeeks(DateExpression<D> date, int weeks) {
  339. return DateOperation.create((Class)date.getType(), Ops.DateTimeOps.ADD_WEEKS, date, ConstantImpl.create(weeks));
  340. }
  341. /**
  342. * Add the given amount of days to the date
  343. *
  344. * @param date
  345. * @param days
  346. * @return
  347. */
  348. public static <D extends Comparable> DateExpression<D> addDays(DateExpression<D> date, int days) {
  349. return DateOperation.create((Class)date.getType(), Ops.DateTimeOps.ADD_DAYS, date, ConstantImpl.create(days));
  350. }
  351. /**
  352. * @param expr
  353. * @return
  354. */
  355. public static <T extends Number> WindowOver<T> sum(Expression<T> expr) {
  356. return new WindowOver<T>((Class<T>)expr.getType(), Ops.AggOps.SUM_AGG, expr);
  357. }
  358. /**
  359. * @param expr
  360. * @return
  361. */
  362. public static WindowOver<Long> count(Expression<?> expr) {
  363. return new WindowOver<Long>(Long.class, Ops.AggOps.COUNT_AGG, expr);
  364. }
  365. /**
  366. * @param expr
  367. * @return
  368. */
  369. public static <T extends Number> WindowOver<T> avg(Expression<T> expr) {
  370. return new WindowOver<T>((Class<T>)expr.getType(), Ops.AggOps.AVG_AGG, expr);
  371. }
  372. /**
  373. * @param expr
  374. * @return
  375. */
  376. public static <T extends Comparable> WindowOver<T> min(Expression<T> expr) {
  377. return new WindowOver<T>((Class<T>)expr.getType(), Ops.AggOps.MIN_AGG, expr);
  378. }
  379. /**
  380. * @param expr
  381. * @return
  382. */
  383. public static <T extends Comparable> WindowOver<T> max(Expression<T> expr) {
  384. return new WindowOver<T>((Class<T>)expr.getType(), Ops.AggOps.MAX_AGG, expr);
  385. }
  386. /**
  387. * expr evaluated at the row that is one row after the current row within the partition;
  388. *
  389. * @param expr
  390. * @return
  391. */
  392. public static <T> WindowOver<T> lead(Expression<T> expr) {
  393. return new WindowOver<T>((Class<T>)expr.getType(), SQLTemplates.LEAD, expr);
  394. }
  395. /**
  396. * expr evaluated at the row that is one row before the current row within the partition
  397. *
  398. * @param expr
  399. * @return
  400. */
  401. public static <T> WindowOver<T> lag(Expression<T> expr) {
  402. return new WindowOver<T>((Class<T>)expr.getType(), SQLTemplates.LAG, expr);
  403. }
  404. /**
  405. * rank of the current row with gaps; same as row_number of its first peer
  406. *
  407. * @return
  408. */
  409. public static WindowOver<Long> rank() {
  410. return rank;
  411. }
  412. /**
  413. * rank of the current row without gaps; this function counts peer groups
  414. *
  415. * @return
  416. */
  417. public static WindowOver<Long> denseRank() {
  418. return denseRank;
  419. }
  420. /**
  421. * number of the current row within its partition, counting from 1
  422. *
  423. * @return
  424. */
  425. public static WindowOver<Long> rowNumber() {
  426. return rowNumber;
  427. }
  428. /**
  429. * returns value evaluated at the row that is the first row of the window frame
  430. *
  431. * @param expr
  432. * @return
  433. */
  434. public static <T> WindowOver<T> firstValue(Expression<T> expr) {
  435. return new WindowOver<T>((Class<T>)expr.getType(), SQLTemplates.FIRSTVALUE, expr);
  436. }
  437. /**
  438. * returns value evaluated at the row that is the last row of the window frame
  439. *
  440. * @param expr
  441. * @return
  442. */
  443. public static <T> WindowOver<T> lastValue(Expression<T> expr) {
  444. return new WindowOver<T>((Class<T>)expr.getType(), SQLTemplates.LASTVALUE, expr);
  445. }
  446. private SQLExpressions() {}
  447. }