Giter VIP home page Giter VIP logo

sqlbuilder-1's Introduction

SQL Builder Utilities

This package contains a number of utility classes to simplify working with SQL.

Maven

Add the following dependency to your POM:

<dependency>
  <groupId>ca.krasnay</groupId>
  <artifactId>sqlbuilder</artifactId>
  <version>1.2</version>
</dependency>

SQL Builder depends on slf4j and Spring JDBC, so be sure to add the desired versions of these to your <dependencyManagement> section.

Gradle

Add the following dependency to your build.gradle:

compile 'ca.krasnay:sqlbuilder:1.2'

Builders

Builders simplify the creation of SQL strings. They know a little bit about SQL syntax, and make the creation of dynamic SQL a little nicer in Java. Like Java's StringBuilder, they use chainable calls. Here's an example of using SelectBuilder.

new SelectBuilder()
    .column("name")
    .column("age")
    .from("Employee")
    .where("dept = 'engineering'")
    .where("salary > 100000")
    .toString();

This produces the SQL string select name, age from Employee where dept = 'engineering' and salary > 100000. Note how SelectBuilder knows to join the columns with a comma and to join the where clauses with and.

For more info, see http://john.krasnay.ca/2010/02/15/building-sql-in-java.html

ParameterizedPreparedStatementCreator

Spring has a powerful abstraction known as JdbcTemplate that makes working with JDBC bearable. JdbcTemplate takes care of the proper allocation and disposal of JDBC connections from a DataSource. It never returns a Connection; instead, connection objects are passed to callbacks provided by the caller. Once such callback, the PreparedStatementCreator, is used to create a prepared statement given a connection.

In a typical PreparedStatementCreator, one creates SQL with substitutable parameters indicated by question marks, then sets the parameter values by index. Keeping track of these indexes can be challenging when working with dynamic SQL. To simplify this, ParamerizedPreparedStatementCreator uses named parameters. Here's an example:

PreparedStatementCreator psc =
    new ParameterizedPreparedStatementCreator()
        .setSql("update Employee set name = :name where id = :id")
        .setParameter("name", "Bob")
        .setParameter("id", 42);

new JdbcTemplate(dataSource).update(psc);

Creators

Each builder class has a corresponding Creator class that combines a builder and a ParameterizedPreparedStatmentCreator.

PreparedStatementCreator psc =
    new UpdateCreator("Employee")
        .setValue("name", "Bob")
        .whereEquals("id", 42);

new JdbcTemplate(dataSource).update(psc);

Creators don't add much functionality themselves, but they make working with builders and ParameterizedPreparedStatementCreators a little easier (plus you don't have to keep typing that ridiculously large class name!).

sqlbuilder-1's People

Contributors

jkrasnay avatar monarezio avatar alcharkov avatar baz8080 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.