Skip to content

Query Using PL

Gal Koren edited this page Nov 12, 2020 · 1 revision

PL has a query DSL similar to JOOQ that looks like this:

var campaigns = plContext
    .select(
        Campaign.NAME,
        Campaign.BUDGET,
        CampaignUrl.URL,
        CampaignUrl.DESCRIPTION,
        Profile.NAME
     )
    .from(Campaign.INSTANCE)
    .where(Campaign.STATUS.eq(Status.ACTIVE))
    .fetchByKeys( /* campaign IDs, or names, or whatever */ );

But why using this DSL and not JOOQ DSL directly? Answer is relations. PL can do the JOINs for you and brings fields from other related entities. How far can the relations go? The answer is in the Relations page. In the example above, the query starts from the campaign table, joining with the profile table (and any table in the way to get there). Child entities can also be requested such as campaign URLs.

One-to-one fields can be accessed like this:

campaigns.forEach(campaign -> {
    var name = campaign.get(Campaign.NAME);
    var profileName = campaign.get(Profile.NAME);
});

Children fields (One-to-many) can be accessed like this:

campaigns.forEach(campaign -> {
    var urls = campaign.getMany(CampaignUrl.INSTANCE);
    urls.forEach(urlEntity -> {
        var url = urlEntity.get(CampaignUrl.URL);
        var urlDescription = urlEntity.get(CampaignUrl.DESCRIPTION);
    });
});