[#2944] Add support for GROUP_CONCAT in SQLite
This commit is contained in:
parent
df0f3c0840
commit
7bdc75bb64
@ -955,12 +955,39 @@ extends BaseTest<A, AP, B, S, B2S, BS, L, X, DATE, BOOL, D, T, U, UU, I, IPK, T7
|
||||
xxxx xxxxxxxxxx
|
||||
xx [/pro] */
|
||||
case DERBY:
|
||||
case SQLITE:
|
||||
log.info("SKIPPING", "LISTAGG tests");
|
||||
return;
|
||||
}
|
||||
|
||||
Result<?> result1 = create().select(
|
||||
TAuthor_FIRST_NAME(),
|
||||
TAuthor_LAST_NAME(),
|
||||
groupConcat(TBook_ID(), ", ")
|
||||
.as("books"))
|
||||
.from(TAuthor())
|
||||
.join(TBook()).on(TAuthor_ID().equal(TBook_AUTHOR_ID()))
|
||||
.groupBy(
|
||||
TAuthor_ID(),
|
||||
TAuthor_FIRST_NAME(),
|
||||
TAuthor_LAST_NAME())
|
||||
.orderBy(TAuthor_ID())
|
||||
.fetch();
|
||||
|
||||
assertEquals(2, result1.size());
|
||||
assertEquals(AUTHOR_FIRST_NAMES, result1.getValues(TAuthor_FIRST_NAME()));
|
||||
assertEquals(AUTHOR_LAST_NAMES, result1.getValues(TAuthor_LAST_NAME()));
|
||||
|
||||
// [#2944] SQLite cannot guarantee any order among aggregated values...
|
||||
assertTrue(asList("1, 2", "2, 1").contains(result1.getValue(0, "books")));
|
||||
assertTrue(asList("3, 4", "4, 3").contains(result1.getValue(1, "books")));
|
||||
|
||||
switch (dialect().family()) {
|
||||
case SQLITE:
|
||||
log.info("SKIPPING", "LISTAGG ordered tests");
|
||||
return;
|
||||
}
|
||||
|
||||
Result<?> result2 = create().select(
|
||||
TAuthor_FIRST_NAME(),
|
||||
TAuthor_LAST_NAME(),
|
||||
listAgg(TBook_ID(), ", ")
|
||||
@ -979,13 +1006,13 @@ extends BaseTest<A, AP, B, S, B2S, BS, L, X, DATE, BOOL, D, T, U, UU, I, IPK, T7
|
||||
.orderBy(TAuthor_ID())
|
||||
.fetch();
|
||||
|
||||
assertEquals(2, result1.size());
|
||||
assertEquals(AUTHOR_FIRST_NAMES, result1.getValues(TAuthor_FIRST_NAME()));
|
||||
assertEquals(AUTHOR_LAST_NAMES, result1.getValues(TAuthor_LAST_NAME()));
|
||||
assertEquals("2, 1", result1.getValue(0, "books1"));
|
||||
assertEquals("2, 1", result1.getValue(0, "books2"));
|
||||
assertEquals("4, 3", result1.getValue(1, "books1"));
|
||||
assertEquals("4, 3", result1.getValue(1, "books2"));
|
||||
assertEquals(2, result2.size());
|
||||
assertEquals(AUTHOR_FIRST_NAMES, result2.getValues(TAuthor_FIRST_NAME()));
|
||||
assertEquals(AUTHOR_LAST_NAMES, result2.getValues(TAuthor_LAST_NAME()));
|
||||
assertEquals("2, 1", result2.getValue(0, "books1"));
|
||||
assertEquals("2, 1", result2.getValue(0, "books2"));
|
||||
assertEquals("4, 3", result2.getValue(1, "books1"));
|
||||
assertEquals("4, 3", result2.getValue(1, "books2"));
|
||||
|
||||
switch (dialect()) {
|
||||
/* [pro] xx
|
||||
@ -1002,7 +1029,7 @@ extends BaseTest<A, AP, B, S, B2S, BS, L, X, DATE, BOOL, D, T, U, UU, I, IPK, T7
|
||||
return;
|
||||
}
|
||||
|
||||
Result<?> result2 = create().select(
|
||||
Result<?> result3 = create().select(
|
||||
TAuthor_FIRST_NAME(),
|
||||
TAuthor_LAST_NAME(),
|
||||
listAgg(TBook_TITLE())
|
||||
@ -1013,13 +1040,13 @@ extends BaseTest<A, AP, B, S, B2S, BS, L, X, DATE, BOOL, D, T, U, UU, I, IPK, T7
|
||||
.orderBy(TBook_ID())
|
||||
.fetch();
|
||||
|
||||
assertEquals(4, result2.size());
|
||||
assertEquals(BOOK_FIRST_NAMES, result2.getValues(TAuthor_FIRST_NAME()));
|
||||
assertEquals(BOOK_LAST_NAMES, result2.getValues(TAuthor_LAST_NAME()));
|
||||
assertEquals("1984Animal Farm", result2.getValue(0, 2));
|
||||
assertEquals("1984Animal Farm", result2.getValue(1, 2));
|
||||
assertEquals("O AlquimistaBrida", result2.getValue(2, 2));
|
||||
assertEquals("O AlquimistaBrida", result2.getValue(3, 2));
|
||||
assertEquals(4, result3.size());
|
||||
assertEquals(BOOK_FIRST_NAMES, result3.getValues(TAuthor_FIRST_NAME()));
|
||||
assertEquals(BOOK_LAST_NAMES, result3.getValues(TAuthor_LAST_NAME()));
|
||||
assertEquals("1984Animal Farm", result3.getValue(0, 2));
|
||||
assertEquals("1984Animal Farm", result3.getValue(1, 2));
|
||||
assertEquals("O AlquimistaBrida", result3.getValue(2, 2));
|
||||
assertEquals("O AlquimistaBrida", result3.getValue(3, 2));
|
||||
}
|
||||
|
||||
@Test
|
||||
|
||||
@ -49,6 +49,7 @@ import static org.jooq.SQLDialect.MYSQL;
|
||||
// ...
|
||||
// ...
|
||||
import static org.jooq.SQLDialect.POSTGRES;
|
||||
import static org.jooq.SQLDialect.SQLITE;
|
||||
// ...
|
||||
|
||||
import org.jooq.api.annotation.State;
|
||||
@ -67,7 +68,7 @@ public interface GroupConcatSeparatorStep extends AggregateFunction<String> {
|
||||
/**
|
||||
* Specify the separator on the <code>GROUP_CONCAT</code> function
|
||||
*/
|
||||
@Support({ CUBRID, H2, HSQLDB, MARIADB, MYSQL, POSTGRES })
|
||||
@Support({ CUBRID, H2, HSQLDB, MARIADB, MYSQL, POSTGRES, SQLITE })
|
||||
@Transition(
|
||||
name = "SEPARATOR",
|
||||
args = "String"
|
||||
|
||||
@ -10586,6 +10586,7 @@ public class DSL {
|
||||
* <li> {@link SQLDialect#H2}</li>
|
||||
* <li> {@link SQLDialect#HSQLDB}</li>
|
||||
* <li> {@link SQLDialect#MYSQL}</li>
|
||||
* <li> {@link SQLDialect#SQLITE} (but without <code>ORDER BY</code>)</li>
|
||||
* </ul>
|
||||
* <p>
|
||||
* It is simulated by the following dialects:
|
||||
@ -10598,7 +10599,7 @@ public class DSL {
|
||||
*
|
||||
* @see #listAgg(Field)
|
||||
*/
|
||||
@Support({ CUBRID, H2, HSQLDB, MARIADB, MYSQL, POSTGRES })
|
||||
@Support({ CUBRID, H2, HSQLDB, MARIADB, MYSQL, POSTGRES, SQLITE })
|
||||
@Transition(
|
||||
name = "GROUP_CONCAT",
|
||||
args = "Field"
|
||||
@ -10607,6 +10608,37 @@ public class DSL {
|
||||
return new GroupConcat(nullSafe(field));
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the aggregated concatenation for a field.
|
||||
* <p>
|
||||
* This is natively supported by
|
||||
* <ul>
|
||||
* <li> {@link SQLDialect#CUBRID}</li>
|
||||
* <li> {@link SQLDialect#H2}</li>
|
||||
* <li> {@link SQLDialect#HSQLDB}</li>
|
||||
* <li> {@link SQLDialect#MYSQL}</li>
|
||||
* <li> {@link SQLDialect#SQLITE}</li>
|
||||
* </ul>
|
||||
* <p>
|
||||
* It is simulated by the following dialects:
|
||||
* <ul>
|
||||
* <li> {@link SQLDialect#DB2}: Using <code>XMLAGG()</code></li>
|
||||
* <li> {@link SQLDialect#ORACLE}: Using <code>LISTAGG()</code></li>
|
||||
* <li> {@link SQLDialect#POSTGRES}: Using <code>STRING_AGG()</code></li>
|
||||
* <li> {@link SQLDialect#SYBASE}: Using <code>LIST()</code></li>
|
||||
* </ul>
|
||||
*
|
||||
* @see #listAgg(Field)
|
||||
*/
|
||||
@Support({ CUBRID, H2, HSQLDB, MARIADB, MYSQL, POSTGRES, SQLITE })
|
||||
@Transition(
|
||||
name = "GROUP_CONCAT",
|
||||
args = "Field+"
|
||||
)
|
||||
public static AggregateFunction<String> groupConcat(Field<?> field, String separator) {
|
||||
return new GroupConcat(nullSafe(field)).separator(separator);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the aggregated concatenation for a field.
|
||||
* <p>
|
||||
|
||||
@ -135,6 +135,7 @@ enum Term {
|
||||
case HSQLDB:
|
||||
case MARIADB:
|
||||
case MYSQL:
|
||||
case SQLITE:
|
||||
return "group_concat";
|
||||
|
||||
case POSTGRES:
|
||||
|
||||
Loading…
Reference in New Issue
Block a user