[#1709] Add Map<Record, Result<R>> ResultQuery.fetchGroups(Field<?>[])
- Changed result Map key from List<?> to Record - Fixed some debugger-related issues
This commit is contained in:
parent
0bf633fc26
commit
f9f351762c
@ -75,7 +75,6 @@ import org.jooq.test._.converters.Boolean_YES_NO_UC;
|
||||
import org.jooq.test._.converters.Boolean_YN_LC;
|
||||
import org.jooq.test._.converters.Boolean_YN_UC;
|
||||
import org.jooq.tools.JooqLogger;
|
||||
import org.jooq.tools.debug.old.impl.DebugListener;
|
||||
import org.jooq.tools.unsigned.UByte;
|
||||
import org.jooq.tools.unsigned.UInteger;
|
||||
import org.jooq.tools.unsigned.ULong;
|
||||
@ -682,7 +681,6 @@ public abstract class BaseTest<
|
||||
protected Factory create(Settings settings) {
|
||||
Factory create = delegate.create(settings);
|
||||
create.getSettings().getExecuteListeners().add(TestStatisticsListener.class.getName());
|
||||
create.getSettings().getExecuteListeners().add(DebugListener.class.getName());
|
||||
return create;
|
||||
}
|
||||
|
||||
|
||||
@ -106,7 +106,7 @@ extends BaseTest<A, AP, B, S, B2S, BS, L, X, DATE, BOOL, D, T, U, I, IPK, T658,
|
||||
Thread.sleep(1000);
|
||||
}
|
||||
|
||||
Debugger remote = remoteDebugger("localhost", 5555);
|
||||
Debugger remote = remoteDebugger("localhost", jOOQAbstractTest.DEBUGGER_PORT);
|
||||
Debugger local = localDebugger();
|
||||
|
||||
try {
|
||||
|
||||
@ -149,15 +149,15 @@ extends BaseTest<A, AP, B, S, B2S, BS, L, X, DATE, BOOL, D, T, U, I, IPK, T658,
|
||||
|
||||
// Keys -> Record
|
||||
// --------------
|
||||
Map<List<?>, B> map3 = create().selectFrom(TBook()).orderBy(TBook_ID())
|
||||
Map<Record, B> map3 = create().selectFrom(TBook()).orderBy(TBook_ID())
|
||||
.fetchMap(new Field<?>[] { TBook_ID(), TBook_LANGUAGE_ID(), TBook_TITLE() });
|
||||
assertEquals(4, map3.keySet().size());
|
||||
|
||||
for (List<?> keyList : map3.keySet()) {
|
||||
B record = map3.get(keyList);
|
||||
assertEquals(keyList.get(0), record.getValue(TBook_ID()));
|
||||
assertEquals(keyList.get(1), record.getValue(TBook_LANGUAGE_ID()));
|
||||
assertEquals(keyList.get(2), record.getValue(TBook_TITLE()));
|
||||
for (Record key : map3.keySet()) {
|
||||
B record = map3.get(key);
|
||||
assertEquals(key.getValue(0), record.getValue(TBook_ID()));
|
||||
assertEquals(key.getValue(1), record.getValue(TBook_LANGUAGE_ID()));
|
||||
assertEquals(key.getValue(2), record.getValue(TBook_TITLE()));
|
||||
}
|
||||
|
||||
// List of Map
|
||||
@ -258,40 +258,40 @@ extends BaseTest<A, AP, B, S, B2S, BS, L, X, DATE, BOOL, D, T, U, I, IPK, T658,
|
||||
// Keys -> Record
|
||||
// --------------
|
||||
// Grouping by BOOK.AUTHOR_ID, BOOK.LANGUAGE_ID
|
||||
Map<List<?>, Result<B>> map5 = create().selectFrom(TBook()).orderBy(TBook_ID())
|
||||
Map<Record, Result<B>> map5 = create().selectFrom(TBook()).orderBy(TBook_ID())
|
||||
.fetchGroups(new Field<?>[] { TBook_AUTHOR_ID(), TBook_LANGUAGE_ID() });
|
||||
|
||||
Iterator<Entry<List<?>, Result<B>>> iterator = map5.entrySet().iterator();
|
||||
Entry<List<?>, Result<B>> entry1_en = iterator.next();
|
||||
Iterator<Entry<Record, Result<B>>> iterator = map5.entrySet().iterator();
|
||||
Entry<Record, Result<B>> entry1_en = iterator.next();
|
||||
assertEquals(2, entry1_en.getValue().size());
|
||||
assertEquals(entry1_en.getKey().get(0), entry1_en.getValue().get(0).getValue(TBook_AUTHOR_ID()));
|
||||
assertEquals(entry1_en.getKey().get(0), entry1_en.getValue().get(1).getValue(TBook_AUTHOR_ID()));
|
||||
assertEquals(entry1_en.getKey().get(1), entry1_en.getValue().get(0).getValue(TBook_LANGUAGE_ID()));
|
||||
assertEquals(entry1_en.getKey().get(1), entry1_en.getValue().get(1).getValue(TBook_LANGUAGE_ID()));
|
||||
assertEquals(entry1_en.getKey().getValue(0), entry1_en.getValue().get(0).getValue(TBook_AUTHOR_ID()));
|
||||
assertEquals(entry1_en.getKey().getValue(0), entry1_en.getValue().get(1).getValue(TBook_AUTHOR_ID()));
|
||||
assertEquals(entry1_en.getKey().getValue(1), entry1_en.getValue().get(0).getValue(TBook_LANGUAGE_ID()));
|
||||
assertEquals(entry1_en.getKey().getValue(1), entry1_en.getValue().get(1).getValue(TBook_LANGUAGE_ID()));
|
||||
|
||||
Entry<List<?>, Result<B>> entry2_pt = iterator.next();
|
||||
Entry<Record, Result<B>> entry2_pt = iterator.next();
|
||||
assertEquals(1, entry2_pt.getValue().size());
|
||||
assertEquals(entry2_pt.getKey().get(0), entry2_pt.getValue().get(0).getValue(TBook_AUTHOR_ID()));
|
||||
assertEquals(entry2_pt.getKey().get(1), entry2_pt.getValue().get(0).getValue(TBook_LANGUAGE_ID()));
|
||||
assertEquals(entry2_pt.getKey().getValue(0), entry2_pt.getValue().get(0).getValue(TBook_AUTHOR_ID()));
|
||||
assertEquals(entry2_pt.getKey().getValue(1), entry2_pt.getValue().get(0).getValue(TBook_LANGUAGE_ID()));
|
||||
|
||||
Entry<List<?>, Result<B>> entry2_de = iterator.next();
|
||||
Entry<Record, Result<B>> entry2_de = iterator.next();
|
||||
assertEquals(1, entry2_de.getValue().size());
|
||||
assertEquals(entry2_de.getKey().get(0), entry2_de.getValue().get(0).getValue(TBook_AUTHOR_ID()));
|
||||
assertEquals(entry2_de.getKey().get(1), entry2_de.getValue().get(0).getValue(TBook_LANGUAGE_ID()));
|
||||
assertEquals(entry2_de.getKey().getValue(0), entry2_de.getValue().get(0).getValue(TBook_AUTHOR_ID()));
|
||||
assertEquals(entry2_de.getKey().getValue(1), entry2_de.getValue().get(0).getValue(TBook_LANGUAGE_ID()));
|
||||
|
||||
assertFalse(iterator.hasNext());
|
||||
|
||||
// Grouping by BOOK.AUTHOR_ID, BOOK.LANGUAGE_ID, BOOK.TITLE
|
||||
Map<List<?>, Result<B>> map6 = create().selectFrom(TBook()).orderBy(TBook_ID())
|
||||
Map<Record, Result<B>> map6 = create().selectFrom(TBook()).orderBy(TBook_ID())
|
||||
.fetchGroups(new Field<?>[] { TBook_ID(), TBook_LANGUAGE_ID(), TBook_TITLE() });
|
||||
assertEquals(4, map6.size());
|
||||
|
||||
for (List<?> keyList : map6.keySet()) {
|
||||
Result<B> result = map6.get(keyList);
|
||||
for (Record key : map6.keySet()) {
|
||||
Result<B> result = map6.get(key);
|
||||
assertEquals(1, result.size());
|
||||
assertEquals(keyList.get(0), result.get(0).getValue(TBook_ID()));
|
||||
assertEquals(keyList.get(1), result.get(0).getValue(TBook_LANGUAGE_ID()));
|
||||
assertEquals(keyList.get(2), result.get(0).getValue(TBook_TITLE()));
|
||||
assertEquals(key.getValue(0), result.get(0).getValue(TBook_ID()));
|
||||
assertEquals(key.getValue(1), result.get(0).getValue(TBook_LANGUAGE_ID()));
|
||||
assertEquals(key.getValue(2), result.get(0).getValue(TBook_TITLE()));
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@ -118,9 +118,8 @@ import org.jooq.test._.testcases.ThreadSafetyTests;
|
||||
import org.jooq.tools.JooqLogger;
|
||||
import org.jooq.tools.StopWatch;
|
||||
import org.jooq.tools.StringUtils;
|
||||
import org.jooq.tools.debug.old.Debugger;
|
||||
import org.jooq.tools.debug.old.impl.DebuggerFactory;
|
||||
import org.jooq.tools.debug.old.impl.Server;
|
||||
import org.jooq.tools.debug.Debugger;
|
||||
import org.jooq.tools.debug.impl.DebuggerFactory;
|
||||
import org.jooq.tools.reflect.ReflectException;
|
||||
import org.jooq.tools.unsigned.UByte;
|
||||
import org.jooq.tools.unsigned.UInteger;
|
||||
@ -132,7 +131,6 @@ import org.apache.commons.io.FileUtils;
|
||||
import org.junit.After;
|
||||
import org.junit.AfterClass;
|
||||
import org.junit.Before;
|
||||
import org.junit.BeforeClass;
|
||||
import org.junit.Test;
|
||||
import org.postgresql.util.PSQLException;
|
||||
|
||||
@ -213,7 +211,6 @@ public abstract class jOOQAbstractTest<
|
||||
private static final String JDBC_USER = "jdbc.User";
|
||||
private static final String JDBC_URL = "jdbc.URL";
|
||||
private static final String JDBC_DRIVER = "jdbc.Driver";
|
||||
private static final int DEBUGGER_PORT = 5533;
|
||||
|
||||
public static final JooqLogger log = JooqLogger.getLogger(jOOQAbstractTest.class);
|
||||
public static final StopWatch testSQLWatch = new StopWatch();
|
||||
@ -230,8 +227,8 @@ public abstract class jOOQAbstractTest<
|
||||
public static String jdbcSchema;
|
||||
public static Map<String, String> scripts = new HashMap<String, String>();
|
||||
|
||||
private static Server SERVER;
|
||||
private static boolean RUN_CONSOLE_IN_PROCESS = false;
|
||||
public static final int DEBUGGER_PORT = 5533;
|
||||
public static boolean RUN_CONSOLE_IN_PROCESS = false;
|
||||
|
||||
protected void execute(String script) throws Exception {
|
||||
Statement stmt = null;
|
||||
@ -377,11 +374,6 @@ public abstract class jOOQAbstractTest<
|
||||
}
|
||||
}
|
||||
|
||||
@BeforeClass
|
||||
public static void sqlConsole() throws Exception {
|
||||
SERVER = new Server(DEBUGGER_PORT);
|
||||
}
|
||||
|
||||
@Before
|
||||
public void setUp() throws Exception {
|
||||
connection = getConnection();
|
||||
@ -407,8 +399,6 @@ public abstract class jOOQAbstractTest<
|
||||
|
||||
@AfterClass
|
||||
public static void quit() throws Exception {
|
||||
SERVER.close();
|
||||
|
||||
log.info("QUITTING");
|
||||
|
||||
// Issue a log dump on adaptive server. Don't know why this is needed
|
||||
@ -442,6 +432,9 @@ public abstract class jOOQAbstractTest<
|
||||
connection = getConnection0(null, null);
|
||||
|
||||
if (RUN_CONSOLE_IN_PROCESS) {
|
||||
// This workaround will start the server
|
||||
create().selectOne().fetch();
|
||||
|
||||
try {
|
||||
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
|
||||
Debugger debugger = DebuggerFactory.remoteDebugger("127.0.0.1", DEBUGGER_PORT);
|
||||
@ -748,7 +741,7 @@ public abstract class jOOQAbstractTest<
|
||||
|
||||
Settings settings = SettingsTools.defaultSettings()
|
||||
.withExecuteDebugging(ExecuteDebugging.SERVER)
|
||||
.withExecuteDebuggingPort(5555)
|
||||
.withExecuteDebuggingPort(DEBUGGER_PORT)
|
||||
.withRenderSchema(renderSchema)
|
||||
.withRenderMapping(new RenderMapping()
|
||||
.withDefaultSchema(defaultSchema))
|
||||
|
||||
@ -1850,7 +1850,7 @@ public interface Result<R extends Record> extends FieldProvider, List<R>, Attach
|
||||
* @throws InvalidResultException if the keys are non-unique in the result
|
||||
* set.
|
||||
*/
|
||||
Map<List<?>, R> intoMap(Field<?>[] keys);
|
||||
Map<Record, R> intoMap(Field<?>[] keys);
|
||||
|
||||
/**
|
||||
* Return a {@link Map} with results grouped by the given key and mapped
|
||||
@ -1907,7 +1907,7 @@ public interface Result<R extends Record> extends FieldProvider, List<R>, Attach
|
||||
* resulting map will contain at most one entry.
|
||||
* @return A Map containing grouped results
|
||||
*/
|
||||
Map<List<?>, Result<R>> intoGroups(Field<?>[] keys);
|
||||
Map<Record, Result<R>> intoGroups(Field<?>[] keys);
|
||||
|
||||
/**
|
||||
* Return a {@link Map} with results grouped by the given key and mapped
|
||||
|
||||
@ -517,7 +517,7 @@ public interface ResultQuery<R extends Record> extends Query {
|
||||
* result set.
|
||||
* @see Result#intoMap(Field[])
|
||||
*/
|
||||
Map<List<?>, R> fetchMap(Field<?>[] keys) throws DataAccessException;
|
||||
Map<Record, R> fetchMap(Field<?>[] keys) throws DataAccessException;
|
||||
|
||||
/**
|
||||
* Execute the query and return a {@link Map} with results grouped by the
|
||||
@ -585,7 +585,7 @@ public interface ResultQuery<R extends Record> extends Query {
|
||||
* @throws DataAccessException if something went wrong executing the query
|
||||
* @see Result#intoGroups(Field[])
|
||||
*/
|
||||
Map<List<?>, Result<R>> fetchGroups(Field<?>[] keys) throws DataAccessException;
|
||||
Map<Record, Result<R>> fetchGroups(Field<?>[] keys) throws DataAccessException;
|
||||
|
||||
/**
|
||||
* Return a {@link Map} with results grouped by the given key and mapped
|
||||
|
||||
@ -234,7 +234,7 @@ abstract class AbstractDelegatingSelect<R extends Record>
|
||||
}
|
||||
|
||||
@Override
|
||||
public final Map<List<?>, R> fetchMap(Field<?>[] keys) {
|
||||
public final Map<Record, R> fetchMap(Field<?>[] keys) {
|
||||
return getDelegate().fetchMap(keys);
|
||||
}
|
||||
|
||||
@ -264,7 +264,7 @@ abstract class AbstractDelegatingSelect<R extends Record>
|
||||
}
|
||||
|
||||
@Override
|
||||
public final Map<List<?>, Result<R>> fetchGroups(Field<?>[] keys) {
|
||||
public final Map<Record, Result<R>> fetchGroups(Field<?>[] keys) {
|
||||
return getDelegate().fetchGroups(keys);
|
||||
}
|
||||
|
||||
|
||||
@ -422,7 +422,7 @@ abstract class AbstractResultQuery<R extends Record> extends AbstractQuery imple
|
||||
}
|
||||
|
||||
@Override
|
||||
public final Map<List<?>, R> fetchMap(Field<?>[] keys) {
|
||||
public final Map<Record, R> fetchMap(Field<?>[] keys) {
|
||||
return fetch().intoMap(keys);
|
||||
}
|
||||
|
||||
@ -452,7 +452,7 @@ abstract class AbstractResultQuery<R extends Record> extends AbstractQuery imple
|
||||
}
|
||||
|
||||
@Override
|
||||
public final Map<List<?>, Result<R>> fetchGroups(Field<?>[] keys) {
|
||||
public final Map<Record, Result<R>> fetchGroups(Field<?>[] keys) {
|
||||
return fetch().intoGroups(keys);
|
||||
}
|
||||
|
||||
|
||||
@ -1392,21 +1392,23 @@ class ResultImpl<R extends Record> implements Result<R>, AttachableInternal {
|
||||
}
|
||||
|
||||
@Override
|
||||
public final Map<List<?>, R> intoMap(Field<?>[] keys) {
|
||||
public final Map<Record, R> intoMap(Field<?>[] keys) {
|
||||
if (keys == null) {
|
||||
keys = new Field[0];
|
||||
}
|
||||
|
||||
Map<List<?>, R> map = new LinkedHashMap<List<?>, R>();
|
||||
Map<Record, R> map = new LinkedHashMap<Record, R>();
|
||||
FieldList keyList = new FieldList(keys);
|
||||
|
||||
for (R record : this) {
|
||||
List<Object> keyList = new ArrayList<Object>();
|
||||
for (Field<?> key : keys) {
|
||||
keyList.add(record.getValue(key));
|
||||
Record key = new RecordImpl(keyList);
|
||||
|
||||
for (Field<?> field : keys) {
|
||||
Util.setValue(key, field, record, field);
|
||||
}
|
||||
|
||||
if (map.put(keyList, record) != null) {
|
||||
throw new InvalidResultException("Key list " + keys + " is not unique in Result for " + this);
|
||||
if (map.put(key, record) != null) {
|
||||
throw new InvalidResultException("Key list " + keyList + " is not unique in Result for " + this);
|
||||
}
|
||||
}
|
||||
|
||||
@ -1466,23 +1468,25 @@ class ResultImpl<R extends Record> implements Result<R>, AttachableInternal {
|
||||
}
|
||||
|
||||
@Override
|
||||
public final Map<List<?>, Result<R>> intoGroups(Field<?>[] keys) {
|
||||
public final Map<Record, Result<R>> intoGroups(Field<?>[] keys) {
|
||||
if (keys == null) {
|
||||
keys = new Field[0];
|
||||
}
|
||||
|
||||
Map<List<?>, Result<R>> map = new LinkedHashMap<List<?>, Result<R>>();
|
||||
Map<Record, Result<R>> map = new LinkedHashMap<Record, Result<R>>();
|
||||
FieldList keyList = new FieldList(keys);
|
||||
|
||||
for (R record : this) {
|
||||
List<Object> keyList = new ArrayList<Object>();
|
||||
for (Field<?> key : keys) {
|
||||
keyList.add(record.getValue(key));
|
||||
Record key = new RecordImpl(keyList);
|
||||
|
||||
for (Field<?> field : keys) {
|
||||
Util.setValue(key, field, record, field);
|
||||
}
|
||||
|
||||
Result<R> result = map.get(keyList);
|
||||
Result<R> result = map.get(key);
|
||||
if (result == null) {
|
||||
result = new ResultImpl<R>(getConfiguration(), this.fields);
|
||||
map.put(keyList, result);
|
||||
map.put(key, result);
|
||||
}
|
||||
|
||||
result.add(record);
|
||||
|
||||
Loading…
Reference in New Issue
Block a user