Skip to content

Commit 528d923

Browse files
authored
Merge pull request #1521 from kazuki43zoo/gh-1518
Support LIMIT and OFFSET on SQL class
2 parents bb1bf8c + 8e9eae4 commit 528d923

File tree

7 files changed

+312
-5
lines changed

7 files changed

+312
-5
lines changed

src/main/java/org/apache/ibatis/jdbc/AbstractSQL.java

Lines changed: 63 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/**
2-
* Copyright 2009-2018 the original author or authors.
2+
* Copyright 2009-2019 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -262,6 +262,54 @@ public T ORDER_BY(String... columns) {
262262
return getSelf();
263263
}
264264

265+
/**
266+
* Set the limit variable string(e.g. {@code "#{limit}"}).
267+
*
268+
* @param variable a limit variable string
269+
* @return a self instance
270+
* @since 3.5.2
271+
*/
272+
public T LIMIT(String variable) {
273+
sql().limit = variable;
274+
return getSelf();
275+
}
276+
277+
/**
278+
* Set the limit value.
279+
*
280+
* @param value an offset value
281+
* @return a self instance
282+
* @since 3.5.2
283+
*/
284+
public T LIMIT(int value) {
285+
sql().limit = String.valueOf(value);
286+
return getSelf();
287+
}
288+
289+
/**
290+
* Set the offset variable string(e.g. {@code "#{offset}"}).
291+
*
292+
* @param variable a offset variable string
293+
* @return a self instance
294+
* @since 3.5.2
295+
*/
296+
public T OFFSET(String variable) {
297+
sql().offset = variable;
298+
return getSelf();
299+
}
300+
301+
/**
302+
* Set the offset value.
303+
*
304+
* @param value an offset value
305+
* @return a self instance
306+
* @since 3.5.2
307+
*/
308+
public T OFFSET(long value) {
309+
sql().offset = String.valueOf(value);
310+
return getSelf();
311+
}
312+
265313
private SQLStatement sql() {
266314
return sql;
267315
}
@@ -328,6 +376,8 @@ public enum StatementType {
328376
List<String> columns = new ArrayList<>();
329377
List<String> values = new ArrayList<>();
330378
boolean distinct;
379+
String offset;
380+
String limit;
331381

332382
public SQLStatement() {
333383
// Prevent Synthetic Access
@@ -368,6 +418,12 @@ private String selectSQL(SafeAppendable builder) {
368418
sqlClause(builder, "GROUP BY", groupBy, "", "", ", ");
369419
sqlClause(builder, "HAVING", having, "(", ")", " AND ");
370420
sqlClause(builder, "ORDER BY", orderBy, "", "", ", ");
421+
if (limit != null) {
422+
builder.append(" LIMIT ").append(limit);
423+
}
424+
if (offset != null) {
425+
builder.append(" OFFSET ").append(offset);
426+
}
371427
return builder.toString();
372428
}
373429

@@ -389,6 +445,9 @@ private String insertSQL(SafeAppendable builder) {
389445
private String deleteSQL(SafeAppendable builder) {
390446
sqlClause(builder, "DELETE FROM", tables, "", "", "");
391447
sqlClause(builder, "WHERE", where, "(", ")", " AND ");
448+
if (limit != null) {
449+
builder.append(" LIMIT ").append(limit);
450+
}
392451
return builder.toString();
393452
}
394453

@@ -397,6 +456,9 @@ private String updateSQL(SafeAppendable builder) {
397456
joins(builder);
398457
sqlClause(builder, "SET", sets, "", "", ", ");
399458
sqlClause(builder, "WHERE", where, "(", ")", " AND ");
459+
if (limit != null) {
460+
builder.append(" LIMIT ").append(limit);
461+
}
400462
return builder.toString();
401463
}
402464

src/site/es/xdoc/statement-builders.xml

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -284,6 +284,38 @@ public String updatePersonSql() {
284284
</td>
285285
<td>Añade un Nuevo elemento a la clausula ORDER BY concatenado por coma. Se le puede llamar más de una vez, lo cual hará que se concatenen nuevas condiciones separadas también por coma.</td>
286286
</tr>
287+
<tr>
288+
<td>
289+
<ul>
290+
<li>
291+
<code>LIMIT(String)</code>
292+
</li>
293+
<li>
294+
<code>LIMIT(int)</code>
295+
</li>
296+
</ul>
297+
</td>
298+
<td>
299+
Appends a <code>LIMIT</code> clause.
300+
This method valid when use together with SELECT(), UPDATE() and DELETE(). (Available since 3.5.2)
301+
</td>
302+
</tr>
303+
<tr>
304+
<td>
305+
<ul>
306+
<li>
307+
<code>OFFSET(String)</code>
308+
</li>
309+
<li>
310+
<code>OFFSET(long)</code>
311+
</li>
312+
</ul>
313+
</td>
314+
<td>
315+
Appends a <code>OFFSET</code> clause.
316+
This method valid when use together with SELECT(). (Available since 3.5.2)
317+
</td>
318+
</tr>
287319
<tr>
288320
<td>
289321
<code>DELETE_FROM(String)</code>
@@ -344,6 +376,14 @@ public String updatePersonSql() {
344376
</tbody>
345377
</table>
346378

379+
<p>
380+
<span class="label important">NOTE</span>
381+
It is important to note that SQL class writes <code>LIMIT</code> and <code>OFFSET</code> clauses into the generated statement as is.
382+
In other words, the library does not attempt to normalize those values for databases that don’t support <code>LIMIT</code> and <code>OFFSET</code> directly.
383+
Therefore, it is very important for users to understand whether or not the target database supports <code>LIMIT</code> and <code>OFFSET</code>.
384+
If the target database does not support <code>LIMIT</code> and <code>OFFSET</code>, then it is likely that using this support will create SQL that has runtime errors.
385+
</p>
386+
347387
<p>Since version 3.4.2, you can use variable-length arguments as follows:</p>
348388

349389
<source><![CDATA[

src/site/ja/xdoc/statement-builders.xml

Lines changed: 42 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<?xml version="1.0" encoding="UTF-8"?>
22
<!--
33
4-
Copyright 2009-2016 the original author or authors.
4+
Copyright 2009-2019 the original author or authors.
55
66
Licensed under the Apache License, Version 2.0 (the "License");
77
you may not use this file except in compliance with the License.
@@ -301,6 +301,38 @@ public String updatePersonSql() {
301301
<td>カンマを挟んで <code>ORDER BY</code> 句を追加します。複数回呼ぶことができ、その度にカンマを挟んで新しい条件が追加されます。
302302
</td>
303303
</tr>
304+
<tr>
305+
<td>
306+
<ul>
307+
<li>
308+
<code>LIMIT(String)</code>
309+
</li>
310+
<li>
311+
<code>LIMIT(int)</code>
312+
</li>
313+
</ul>
314+
</td>
315+
<td>
316+
<code>LIMIT</code> 句を追加します。
317+
このメソッドは SELECT(), UPDATE(), DELETE() と一緒に使うと有効になります。 (3.5.2以降で利用可能)
318+
</td>
319+
</tr>
320+
<tr>
321+
<td>
322+
<ul>
323+
<li>
324+
<code>OFFSET(String)</code>
325+
</li>
326+
<li>
327+
<code>OFFSET(long)</code>
328+
</li>
329+
</ul>
330+
</td>
331+
<td>
332+
<code>OFFSET</code> 句を追加します。
333+
このメソッドは SELECT() と一緒に使うと有効になります。(3.5.2以降で利用可能)
334+
</td>
335+
</tr>
304336
<tr>
305337
<td>
306338
<code>DELETE_FROM(String)</code>
@@ -363,6 +395,15 @@ public String updatePersonSql() {
363395
</tbody>
364396
</table>
365397

398+
<p>
399+
<span class="label important">NOTE</span>
400+
<code>LIMIT</code> と <code>OFFSET</code> 句は生成されたステートメントにそのまま書き込むという点に注意することが重要です。
401+
言い換えると、<code>LIMIT</code> と <code>OFFSET</code> 句をサポートしていないデータベースに対して、
402+
そのデータベースで解釈可能な表現へ変換することはしません。
403+
そのため、利用するデータベースが<code>LIMIT</code> と <code>OFFSET</code> 句をサポートしているか否かを把握しておくことが重要になります。
404+
もし、利用するデータベースが<code>LIMIT</code> と <code>OFFSET</code> 句をサポートしていない場合は、
405+
SQL実行時にエラーになります。
406+
</p>
366407

367408
<p>バージョン3.4.2以降では、次のように可変長引数を使うことができます。</p>
368409

src/site/ko/xdoc/statement-builders.xml

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -325,6 +325,38 @@ public String updatePersonSql() {
325325
여러번 호출할 수 있고 개별조건을 콤마를 붙여서 합칠수 있다.
326326
</td>
327327
</tr>
328+
<tr>
329+
<td>
330+
<ul>
331+
<li>
332+
<code>LIMIT(String)</code>
333+
</li>
334+
<li>
335+
<code>LIMIT(int)</code>
336+
</li>
337+
</ul>
338+
</td>
339+
<td>
340+
Appends a <code>LIMIT</code> clause.
341+
This method valid when use together with SELECT(), UPDATE() and DELETE(). (Available since 3.5.2)
342+
</td>
343+
</tr>
344+
<tr>
345+
<td>
346+
<ul>
347+
<li>
348+
<code>OFFSET(String)</code>
349+
</li>
350+
<li>
351+
<code>OFFSET(long)</code>
352+
</li>
353+
</ul>
354+
</td>
355+
<td>
356+
Appends a <code>OFFSET</code> clause.
357+
This method valid when use together with SELECT(). (Available since 3.5.2)
358+
</td>
359+
</tr>
328360
<tr>
329361
<td>
330362
<code>DELETE_FROM(String)</code>
@@ -391,6 +423,14 @@ public String updatePersonSql() {
391423
</tbody>
392424
</table>
393425

426+
<p>
427+
<span class="label important">NOTE</span>
428+
It is important to note that SQL class writes <code>LIMIT</code> and <code>OFFSET</code> clauses into the generated statement as is.
429+
In other words, the library does not attempt to normalize those values for databases that don’t support <code>LIMIT</code> and <code>OFFSET</code> directly.
430+
Therefore, it is very important for users to understand whether or not the target database supports <code>LIMIT</code> and <code>OFFSET</code>.
431+
If the target database does not support <code>LIMIT</code> and <code>OFFSET</code>, then it is likely that using this support will create SQL that has runtime errors.
432+
</p>
433+
394434
<p>3.4.2 버전부터, variable-length 매개변수를 아래와 같이 사용할 수 있습니다.</p>
395435

396436
<source><![CDATA[

src/site/xdoc/statement-builders.xml

Lines changed: 41 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<?xml version="1.0" encoding="UTF-8"?>
22
<!--
33
4-
Copyright 2009-2016 the original author or authors.
4+
Copyright 2009-2019 the original author or authors.
55
66
Licensed under the Apache License, Version 2.0 (the "License");
77
you may not use this file except in compliance with the License.
@@ -352,6 +352,38 @@ public String updatePersonSql() {
352352
causes it to concatenate the new conditions each time with a comma.
353353
</td>
354354
</tr>
355+
<tr>
356+
<td>
357+
<ul>
358+
<li>
359+
<code>LIMIT(String)</code>
360+
</li>
361+
<li>
362+
<code>LIMIT(int)</code>
363+
</li>
364+
</ul>
365+
</td>
366+
<td>
367+
Appends a <code>LIMIT</code> clause.
368+
This method valid when use together with SELECT(), UPDATE() and DELETE(). (Available since 3.5.2)
369+
</td>
370+
</tr>
371+
<tr>
372+
<td>
373+
<ul>
374+
<li>
375+
<code>OFFSET(String)</code>
376+
</li>
377+
<li>
378+
<code>OFFSET(long)</code>
379+
</li>
380+
</ul>
381+
</td>
382+
<td>
383+
Appends a <code>OFFSET</code> clause.
384+
This method valid when use together with SELECT(). (Available since 3.5.2)
385+
</td>
386+
</tr>
355387
<tr>
356388
<td>
357389
<code>DELETE_FROM(String)</code>
@@ -420,6 +452,14 @@ public String updatePersonSql() {
420452
</tbody>
421453
</table>
422454

455+
<p>
456+
<span class="label important">NOTE</span>
457+
It is important to note that SQL class writes <code>LIMIT</code> and <code>OFFSET</code> clauses into the generated statement as is.
458+
In other words, the library does not attempt to normalize those values for databases that don’t support <code>LIMIT</code> and <code>OFFSET</code> directly.
459+
Therefore, it is very important for users to understand whether or not the target database supports <code>LIMIT</code> and <code>OFFSET</code>.
460+
If the target database does not support <code>LIMIT</code> and <code>OFFSET</code>, then it is likely that using this support will create SQL that has runtime errors.
461+
</p>
462+
423463
<p>Since version 3.4.2, you can use variable-length arguments as follows:</p>
424464

425465
<source><![CDATA[

src/site/zh/xdoc/statement-builders.xml

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -331,6 +331,38 @@ public String updatePersonSql() {
331331
由逗号连接。可以多次被调用,每次由逗号连接新的条件。
332332
</td>
333333
</tr>
334+
<tr>
335+
<td>
336+
<ul>
337+
<li>
338+
<code>LIMIT(String)</code>
339+
</li>
340+
<li>
341+
<code>LIMIT(int)</code>
342+
</li>
343+
</ul>
344+
</td>
345+
<td>
346+
Appends a <code>LIMIT</code> clause.
347+
This method valid when use together with SELECT(), UPDATE() and DELETE(). (Available since 3.5.2)
348+
</td>
349+
</tr>
350+
<tr>
351+
<td>
352+
<ul>
353+
<li>
354+
<code>OFFSET(String)</code>
355+
</li>
356+
<li>
357+
<code>OFFSET(long)</code>
358+
</li>
359+
</ul>
360+
</td>
361+
<td>
362+
Appends a <code>OFFSET</code> clause.
363+
This method valid when use together with SELECT(). (Available since 3.5.2)
364+
</td>
365+
</tr>
334366
<tr>
335367
<td>
336368
<code>DELETE_FROM(String)</code>
@@ -393,6 +425,14 @@ public String updatePersonSql() {
393425
</tbody>
394426
</table>
395427

428+
<p>
429+
<span class="label important">NOTE</span>
430+
It is important to note that SQL class writes <code>LIMIT</code> and <code>OFFSET</code> clauses into the generated statement as is.
431+
In other words, the library does not attempt to normalize those values for databases that don’t support <code>LIMIT</code> and <code>OFFSET</code> directly.
432+
Therefore, it is very important for users to understand whether or not the target database supports <code>LIMIT</code> and <code>OFFSET</code>.
433+
If the target database does not support <code>LIMIT</code> and <code>OFFSET</code>, then it is likely that using this support will create SQL that has runtime errors.
434+
</p>
435+
396436
<p>Since version 3.4.2, you can use variable-length arguments as follows:</p>
397437

398438
<source><![CDATA[

0 commit comments

Comments
 (0)