[#1552] Generate fetchBy[ColumnName] methods in generated DAO classes
This commit is contained in:
parent
abc5c2e96e
commit
c95aa4cb7b
@ -1289,6 +1289,57 @@ public class DefaultGenerator extends AbstractGenerator {
|
||||
out.println("();");
|
||||
out.println("\t}");
|
||||
|
||||
for (ColumnDefinition column : table.getColumns()) {
|
||||
|
||||
// fetchBy[Column]([T]...)
|
||||
// -----------------------
|
||||
out.println();
|
||||
printJavadoc(out, "Fetch records that have <code>" + column.getOutputName() + " IN (values)</code>");
|
||||
out.print("\tpublic ");
|
||||
out.print(List.class);
|
||||
out.print("<");
|
||||
out.print(strategy.getFullJavaClassName(table, Mode.POJO));
|
||||
out.print("> fetchBy");
|
||||
out.print(strategy.getJavaClassName(column, Mode.POJO));
|
||||
out.print("(");
|
||||
out.print(getJavaType(column.getType()));
|
||||
out.println("... values) {");
|
||||
|
||||
out.print("\t\treturn fetch(");
|
||||
out.print(strategy.getFullJavaIdentifier(column));
|
||||
out.println(", values);");
|
||||
|
||||
out.println("\t}");
|
||||
|
||||
// fetchOneBy[Column]([T])
|
||||
// -----------------------
|
||||
ukLoop:
|
||||
for (UniqueKeyDefinition uk : column.getUniqueKeys()) {
|
||||
|
||||
// If column is part of a single-column unique key...
|
||||
if (uk.getKeyColumns().size() == 1 && uk.getKeyColumns().get(0).equals(column)) {
|
||||
|
||||
out.println();
|
||||
printJavadoc(out, "Fetch a unique that has <code>" + column.getOutputName() + " = value</code>");
|
||||
out.print("\tpublic ");
|
||||
out.print(strategy.getFullJavaClassName(table, Mode.POJO));
|
||||
out.print(" fetchOneBy");
|
||||
out.print(strategy.getJavaClassName(column, Mode.POJO));
|
||||
out.print("(");
|
||||
out.print(getJavaType(column.getType()));
|
||||
out.println(" value) {");
|
||||
|
||||
out.print("\t\treturn fetchOne(");
|
||||
out.print(strategy.getFullJavaIdentifier(column));
|
||||
out.println(", value);");
|
||||
|
||||
out.println("\t}");
|
||||
|
||||
break ukLoop;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
out.println("}");
|
||||
out.close();
|
||||
}
|
||||
|
||||
@ -101,6 +101,21 @@ extends BaseTest<A, AP, B, S, B2S, BS, L, X, DATE, BOOL, D, T, U, I, IPK, T658,
|
||||
assertTrue(TAuthorDao().existsById(1));
|
||||
assertNull(TAuthorDao().findById(17));
|
||||
|
||||
List<AP> authors1 = on(TAuthorDao()).call("fetchByLastName", (Object) new String[] { "Orwell", "George"}).<List<AP>>get();
|
||||
assertEquals(1, authors1.size());
|
||||
assertEquals(1, on(authors1.get(0)).get("id"));
|
||||
assertEquals("George", on(authors1.get(0)).get("firstName"));
|
||||
assertEquals("Orwell", on(authors1.get(0)).get("lastName"));
|
||||
|
||||
List<AP> authors2 = on(TAuthorDao()).call("fetchByLastName", (Object) new String[] { "Orwell", "Coelho"}).<List<AP>>get();
|
||||
assertEquals(2, authors2.size());
|
||||
assertEquals(1, on(authors2.get(0)).get("id"));
|
||||
assertEquals("George", on(authors2.get(0)).get("firstName"));
|
||||
assertEquals("Orwell", on(authors2.get(0)).get("lastName"));
|
||||
assertEquals(2, on(authors2.get(1)).get("id"));
|
||||
assertEquals("Paulo", on(authors2.get(1)).get("firstName"));
|
||||
assertEquals("Coelho", on(authors2.get(1)).get("lastName"));
|
||||
|
||||
// Single insertion
|
||||
// ----------------
|
||||
AP author =
|
||||
|
||||
@ -185,6 +185,31 @@ public interface DAO<R extends TableRecord<R>, P, T> {
|
||||
*/
|
||||
P findById(T id) throws DataAccessException;
|
||||
|
||||
/**
|
||||
* Find records by a given field and a set of values.
|
||||
*
|
||||
* @param field The field to compare values against
|
||||
* @param values The accepted values
|
||||
* @return A list of records fulfilling <code>field IN (values)</code>
|
||||
* @throws DataAccessException if something went wrong executing the query
|
||||
*/
|
||||
<Z> List<P> fetch(Field<Z> field, Z... values) throws DataAccessException;
|
||||
|
||||
/**
|
||||
* Find a unique record by a given field and a value.
|
||||
*
|
||||
* @param field The field to compare value against
|
||||
* @param value The accepted value
|
||||
* @return A record fulfilling <code>field = value</code>, or
|
||||
* <code>null</code>
|
||||
* @throws DataAccessException This exception is thrown
|
||||
* <ul>
|
||||
* <li>if something went wrong executing the query</li>
|
||||
* <li>if the query returned more than one value</li>
|
||||
* </ul>
|
||||
*/
|
||||
<Z> P fetchOne(Field<Z> field, Z value) throws DataAccessException;
|
||||
|
||||
/**
|
||||
* Get the underlying table
|
||||
*/
|
||||
|
||||
@ -212,6 +212,22 @@ public abstract class DAOImpl<R extends UpdatableRecord<R>, P, T> implements DAO
|
||||
return record == null ? null : record.into(type);
|
||||
}
|
||||
|
||||
@Override
|
||||
public final <Z> List<P> fetch(Field<Z> field, Z... values) {
|
||||
return create.selectFrom(table)
|
||||
.where(field.in(values))
|
||||
.fetch()
|
||||
.into(type);
|
||||
}
|
||||
|
||||
@Override
|
||||
public final <Z> P fetchOne(Field<Z> field, Z value) {
|
||||
return create.selectFrom(table)
|
||||
.where(field.equal(value))
|
||||
.fetchOne()
|
||||
.into(type);
|
||||
}
|
||||
|
||||
@Override
|
||||
public final Table<R> getTable() {
|
||||
return table;
|
||||
|
||||
Loading…
Reference in New Issue
Block a user