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
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Related

  • Connect Existing Data to AI Retrieval: How to Build Production-Ready Search Without Rebuilding Core Systems
  • Building a High-Throughput Distributed Sequence Generator Using the Hi-Lo Algorithm
  • When Snowflake Lies to You: Understanding False Failures in dbt Pipelines
  • Master-Class: Understanding Database Replication (Single, Multi, and Leaderless)

Trending

  • Building an Agentic Incident Resolution System for Developers
  • Your AI Coding Agent Can't Steal What It Never Had: The Docker Sandbox Isolation Story
  • AI Is Finding Bugs Faster Than Enterprises Can Patch — Here's What Data Security Teams Should Do
  • Why Infrastructure Efficiency Is Becoming the New Cloud Profitability Metric
  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 user avatar
Greg Brown
·
Updated Jan. 04, 21 · Review
Likes (4)
Comment
Save
Tweet
Share
23.4K Views

Join the DZone community and get the full member experience.

Join For Free

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

  • Connect Existing Data to AI Retrieval: How to Build Production-Ready Search Without Rebuilding Core Systems
  • Building a High-Throughput Distributed Sequence Generator Using the Hi-Lo Algorithm
  • When Snowflake Lies to You: Understanding False Failures in dbt Pipelines
  • Master-Class: Understanding Database Replication (Single, Multi, and Leaderless)

Partner Resources

×

Comments

The likes didn't load as expected. Please refresh the page and try again.

  • 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