Skip to content
This repository has been archived by the owner on Nov 22, 2023. It is now read-only.

Commit

Permalink
Fixes ClientMapper to be type safe.
Browse files Browse the repository at this point in the history
This is possible thanks to Lukas' answer on how to use Mappers in a
type safe way. His response is worth a read.

https://groups.google.com/d/msg/jooq-user/dq9r1bZ10vA/-QcKX6EyiucJ
  • Loading branch information
alokmenghrajani committed Apr 23, 2015
1 parent 5377f6b commit 5d4f03e
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 31 deletions.
10 changes: 4 additions & 6 deletions server/src/main/java/keywhiz/service/daos/AclDAO.java
Expand Up @@ -189,13 +189,12 @@ public Set<Group> getGroupsFor(Client client) {

public Set<Client> getClientsFor(Group group) {
List<Client> r = dslContext
.select(CLIENTS.ID, CLIENTS.NAME, CLIENTS.DESCRIPTION, CLIENTS.CREATEDAT, CLIENTS.CREATEDBY,
CLIENTS.UPDATEDAT, CLIENTS.UPDATEDBY, CLIENTS.ENABLED, CLIENTS.AUTOMATIONALLOWED)
.select()
.from(CLIENTS)
.join(MEMBERSHIPS).on(CLIENTS.ID.eq(MEMBERSHIPS.CLIENTID))
.join(GROUPS).on(GROUPS.ID.eq(MEMBERSHIPS.GROUPID))
.where(GROUPS.NAME.eq(group.getName()))
.fetch()
.fetchInto(CLIENTS)
.map(new ClientMapper());
return new HashSet<>(r);
}
Expand All @@ -217,14 +216,13 @@ public ImmutableSet<SanitizedSecret> getSanitizedSecretsFor(Client client) {

public Set<Client> getClientsFor(Secret secret) {
List<Client> r = dslContext
.select(CLIENTS.ID, CLIENTS.NAME, CLIENTS.DESCRIPTION, CLIENTS.CREATEDAT, CLIENTS.CREATEDBY,
CLIENTS.UPDATEDAT, CLIENTS.UPDATEDBY, CLIENTS.ENABLED, CLIENTS.AUTOMATIONALLOWED)
.select()
.from(CLIENTS)
.join(MEMBERSHIPS).on(CLIENTS.ID.eq(MEMBERSHIPS.CLIENTID))
.join(ACCESSGRANTS).on(MEMBERSHIPS.GROUPID.eq(ACCESSGRANTS.GROUPID))
.join(SECRETS).on(SECRETS.ID.eq(ACCESSGRANTS.SECRETID))
.where(SECRETS.NAME.eq(secret.getName()))
.fetch()
.fetchInto(CLIENTS)
.map(new ClientMapper());
return new HashSet<>(r);
}
Expand Down
7 changes: 3 additions & 4 deletions server/src/main/java/keywhiz/service/daos/ClientDAO.java
Expand Up @@ -58,19 +58,18 @@ public void deleteClient(Client client) {
public Optional<Client> getClient(String name) {
ClientsRecord r = dslContext.fetchOne(CLIENTS, CLIENTS.NAME.eq(name));
return Optional.ofNullable(r).map(
(rec) -> rec.map(new ClientMapper()));
rec -> new ClientMapper().map(rec));
}

public Optional<Client> getClientById(long id) {
ClientsRecord r = dslContext.fetchOne(CLIENTS, CLIENTS.ID.eq(Math.toIntExact(id)));
return Optional.ofNullable(r).map(
(rec) -> rec.map(new ClientMapper()));
rec -> new ClientMapper().map(rec));
}

public ImmutableSet<Client> getClients() {
List<Client> r = dslContext
.select()
.from(CLIENTS)
.selectFrom(CLIENTS)
.fetch()
.map(new ClientMapper());
return ImmutableSet.copyOf(r);
Expand Down
33 changes: 12 additions & 21 deletions server/src/main/java/keywhiz/service/daos/ClientMapper.java
Expand Up @@ -17,35 +17,26 @@
package keywhiz.service.daos;

import keywhiz.api.model.Client;
import org.jooq.Record;
import keywhiz.jooq.tables.records.ClientsRecord;
import org.jooq.RecordMapper;

import static keywhiz.jooq.tables.Clients.CLIENTS;

/**
* Jooq has the ability to map records to classes using Reflection. We however need a mapper because
* the constructor's parameter and the columns in the database do not share the same order.
*
* In general, I feel having a mapper is cleaner, so it might not be a bad thing.
*
* The way jooq built their generic API is somewhat broken, so we need to implement
* RecordMapper<Record, Client> instead of RecordMapper<ClientsRecord, Client>. I'll file a task
* and follow up on this issue.
*
* Also, when doing JOINS, I don't know if there's a good way to preserve the right Record type.
*/
class ClientMapper implements RecordMapper<Record, Client> {
public Client map(Record r) {
// Lots of :(
class ClientMapper implements RecordMapper<ClientsRecord, Client> {
public Client map(ClientsRecord r) {
return new Client(
r.getValue(CLIENTS.ID),
r.getValue(CLIENTS.NAME),
r.getValue(CLIENTS.DESCRIPTION),
r.getValue(CLIENTS.CREATEDAT),
r.getValue(CLIENTS.CREATEDBY),
r.getValue(CLIENTS.UPDATEDAT),
r.getValue(CLIENTS.UPDATEDBY),
r.getValue(CLIENTS.ENABLED),
r.getValue(CLIENTS.AUTOMATIONALLOWED));
r.getId(),
r.getName(),
r.getDescription(),
r.getCreatedat(),
r.getCreatedby(),
r.getUpdatedat(),
r.getUpdatedby(),
r.getEnabled(),
r.getAutomationallowed());
}
}

0 comments on commit 5d4f03e

Please sign in to comment.