jQuery Builder 2 JOOQ

Bridges the gap between querybuilder.js.org and jooq.org with very little code.

License

License

MIT License
Categories

Categories

jOOQ Data Databases
GroupId

GroupId

io.kowalski
ArtifactId

ArtifactId

jqb2jooq
Last Version

Last Version

1.0.3
Release Date

Release Date

Type

Type

jar
Description

Description

jQuery Builder 2 JOOQ
Bridges the gap between querybuilder.js.org and jooq.org with very little code.
Project URL

Project URL

https://github.com/kowalski-io/jqb2jooq
Source Code Management

Source Code Management

https://github.com/Kowalski-IO/jqb2jooq/tree/master

Download jqb2jooq

How to add to project

<!-- https://jarcasting.com/artifacts/io.kowalski/jqb2jooq/ -->
<dependency>
    <groupId>io.kowalski</groupId>
    <artifactId>jqb2jooq</artifactId>
    <version>1.0.3</version>
</dependency>
// https://jarcasting.com/artifacts/io.kowalski/jqb2jooq/
implementation 'io.kowalski:jqb2jooq:1.0.3'
// https://jarcasting.com/artifacts/io.kowalski/jqb2jooq/
implementation ("io.kowalski:jqb2jooq:1.0.3")
'io.kowalski:jqb2jooq:jar:1.0.3'
<dependency org="io.kowalski" name="jqb2jooq" rev="1.0.3">
  <artifact name="jqb2jooq" type="jar" />
</dependency>
@Grapes(
@Grab(group='io.kowalski', module='jqb2jooq', version='1.0.3')
)
libraryDependencies += "io.kowalski" % "jqb2jooq" % "1.0.3"
[io.kowalski/jqb2jooq "1.0.3"]

Dependencies

compile (3)

Group / Artifact Type Version
org.jooq : joor-java-8 jar 0.9.7
org.projectlombok : lombok jar 1.16.20
org.jooq : jooq-meta jar 3.10.3

provided (1)

Group / Artifact Type Version
org.jooq : jooq jar 3.10.3

test (6)

Group / Artifact Type Version
org.jooq : jooq-codegen jar 3.10.3
com.zaxxer : HikariCP jar 2.7.6
com.h2database : h2 jar 1.4.196
com.google.code.gson : gson jar 2.8.2
ch.qos.logback : logback-classic jar 1.2.3
junit : junit jar 4.12

Project Modules

There are no modules declared in this project.

jqb2jooq

jQuery QueryBuilder meets JOOQ without the work.

Maven Central Build Status Coverage Status

What is jQuery QueryBuilder?

jqb is a super handy library for your website that allow for the creation of queries and filters.

What is JOOQ?

To shamelessly steal their tagline "JOOQ: The easiest way to write SQL in Java". This sentiment I agree with completely. The JOOQ library provides a typesafe way to execute queries and is an absolute joy to use.

This project aims to be the glue between these two awesome products. jqb2jooq provides the means of defining a mapping from jQuery QueryBuilder filter fields to JOOQ auto-generated fields. Once you define this mapping jqb2jooq will handle the conversion of QueryBuilder json to a JOOQ condition, ready to be included in your JOOQ query.

Usage

Getting started is just as easy as the famed 5 Minute Wordpress Install (only you don't have to use Wordpress 😏 ).

First, add the following dependency to your pom.xml.

<dependency>
  <groupId>io.kowalski</groupId>
  <artifactId>jqb2jooq</artifactId>
  <version>1.0.3</version>
</dependency>

Second, you will need to define a mapping between jqb filter fields and JOOQ fields.

This is done by creating an enum that implements RuleTarget.java.

package io.kowalski.jqb2jooq.test;

import io.kowalski.jqb2jooq.RuleTarget;
import org.jooq.Condition;
import org.jooq.Field;

import static io.kowalski.jqb2jooq.test.jooq.Tables.*;

public enum TestFilterTargets implements RuleTarget {

  FULLNAME(EMPLOYEES.FULLNAME),
  DOB(EMPLOYEES.DOB),
  SALARY(PAYROLL.AMOUNT, PAYROLL.TYPE.eq("SALARY")),
  HOURLY(PAYROLL.AMOUNT, PAYROLL.TYPE.eq("HOURLY")),
  FOOD(EMPLOYEES.FAVORITE_FOOD);

  private final Field field;
  private final Condition[] implicitConditions;

  TestFilterTargets(Field field, Condition... implicitConditions) {
      this.field = field;
      this.implicitConditions = implicitConditions;
  }

  @Override
  public TestFilterTargets parse(String value) {
      return TestFilterTargets.valueOf(value);
  }

  @Override
  public Field getField() {
      return field;
  }

  @Override
  public Condition[] getImplicitConditions() {
      return implicitConditions;
  }

}

Last, but not least, you need to convert the json filter from jqb to a JOOQ condition.

jqb2jooq assumes that the json filter has been deserialized into:

Map<String, Object>

Here is a quick snippet of how to perform said task with gson.

java.lang.reflect.Type mapType = new com.google.gson.reflect.TypeToken<Map<String, Object>>() 
{}.getType();

Map<String, Object> filterMap = new Gson().fromJson(rawJson, mapType);

Once you have the filter deserialized all you have to do is...

org.jooq.Condition condition = JQB2JOOQ.parse(TestFilterTargets.class, filterMap);

// ...and immediately put it to work

try (DSLContext dsl = DSL.using(dataSource, SQLDialect.H2)) {
  List<Employees> employees = dsl.select().from(EMPLOYEES)
          .where(condition).fetchInto(Employees.class);
}

That's all folks!

I hope you enjoy using jqb2jooq! If you notice anything funky please file an issue. ❤️

io.kowalski

Kowalski.io

Just a fancy namespace. All the broken stuff hides in @Kowalski-RND.

Versions

Version
1.0.3
1.0.2
1.0.1
1.0.0