DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Newsletter
Log In / Join
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Related

  • The Query Takes 20 ms. Why Does the API Take Two Seconds?
  • Relate: A SQLite-Style Graph Database
  • Idempotent Consumers: Preventing Stale Retries With Version-Fenced Writes
  • Building DynamoDB Streams Compatibility on Apache Phoenix

Trending

  • Designing Distributed Counters: A Redis Challenge for Frontend Engineers
  • How Has AI Impacted Code Obfuscation?
  • Staff Engineers in the Age of AI: Rethinking Technical Leadership
  • Testing Asynchronous Systems, Part 3: Message Semantics, Observability, and a Test Checklist

Draft This article hasn't been published, so readers can't see it.

In moderation This article is waiting for review, so readers can't see it yet.

Deleted This article has been deleted, so readers can't see it.

  1. DZone
  2. Data Engineering
  3. Databases
  4. Introducing QueryBuilder

Introducing QueryBuilder

The new QueryBuilder class introduced in HTTP-RPC 7.5 simplifies the task of writing SQL queries in Java, helping keep your queries readable.

By 
Greg Brown
·
Updated Jan. 04, 21 · Review
Post on X (opens in a new tab) Share on LinkedIn (opens in a new tab)
23.6K Views

Join the DZone community Save articles, join the discussion, and download Refcards and Trend Reports with a free account.

Join for free Already a member? Log in

Programmatically constructing SQL queries is a common but cumbersome task. For example, consider this query that retrieves information about a hypothetical IoT device:

select Device.*, Building.name as buildingName, Site.name as siteName
from Device
join Building on buildingID = Building.id
join Site on siteID = Site.id
where Device.id = :deviceID


Written in plain text, as above, the query is fairly easy to read and understand. However, in Java, the same query might be declared as follows:

String sql = "select Device.*, Building.name as buildingName,

Site.name as siteName "
+ "from Device "
+ "join Building on buildingID = Building.id "
+ "join Site on siteID = Site.id "
+ "where Device.id = :deviceID";


Even in this simple example, readability is negatively impacted by the addition of double quotes and string concatenation operators. Care must be taken to ensure that whitespace is properly managed. Longer, more complex queries become increasingly difficult to write and maintain.

The new QueryBuilder class introduced in HTTP-RPC 7.5 simplifies the task of writing SQL queries in Java. Using QueryBuilder, the preceding example could be rewritten as follows:\

String sql = 
  QueryBuilder.select("Device.*", "Building.name as buildingName",

"Site.name as siteName")
    .from("Device")
.join("Building").on("buildingID = Building.id")
.join("Site").on("siteID = Site.id")
.where("Device.id = :deviceID").toString();


Because SQL verbs and clauses are represented as Java methods and nouns (tables, columns, and predicates) by strings, this version is much more readable and easier to maintain. It also scales better as query complexity increases.

A complete example using QueryBuilder along with HTTP-RPC’s Parameters and ResultSetAdapter classes is shown below. Parameters is used to prepare SQL statements for execution using named parameter values rather than indexed arguments. ResultSetAdapter provides access to the contents of a result set via the Iterable and Map interfaces, facilitating direct serialization of the result data to JSON:

String sql = 
  QueryBuilder.select("Device.*", "Building.name as buildingName",

"Site.name as siteName")
.from("Device")
.join("Building").on("buildingID = Building.id")
.join("Site").on("siteID = Site.id")
.where("Device.id = :deviceID").toString();

Parameters parameters = Parameters.parse(sql);

try (PreparedStatement statement = connection.prepareStatement

(parameters.getSQL())) {
parameters.apply(statement, mapOf(
entry("deviceID", deviceID)
));

    try (ResultSetAdapter resultSetAdapter = new ResultSetAdapter

(statement.executeQuery())) {
JSONEncoder jsonEncoder = new JSONEncoder();

jsonEncoder.write(resultSetAdapter, System.out);
}
}


For more information, see the project README.

Database

Published at DZone with permission of Greg Brown. See the original article here.

Opinions expressed by DZone contributors are their own.

Related

  • The Query Takes 20 ms. Why Does the API Take Two Seconds?
  • Relate: A SQLite-Style Graph Database
  • Idempotent Consumers: Preventing Stale Retries With Version-Fenced Writes
  • Building DynamoDB Streams Compatibility on Apache Phoenix

Partner Resources

×

Comments

Loading comments

The comments didn't load

Please try again in a moment.

Likes

Loading

The next likes didn't load. Please try again.

The likes didn't load

Please try again in a moment.

See who liked this

Log in or join DZone for free to see who liked this article.

Log in Join for free
  • RSS
  • X
  • Facebook

ABOUT US

  • About DZone
  • Support and feedback
  • Community research

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 215
  • Nashville, TN 37211
  • [email protected]

Let's be friends:

  • RSS
  • X
  • Facebook