Coder Social home page Coder Social logo

sql-wrapper-codegen's Introduction

sql-wrapper-codegen Gradle Status CircleCI Build Status

Available on Gradle public repository : https://plugins.gradle.org/plugin/org.jet.sql.codegen

Languageย 

Gradle plugin to reduce boiler-plate code for running JDBC queries in java and improve type safety while running PreparedStatements in java.

This plugin requires simple YAML files and a gradle plugin to generate wrapper java classes that abstract away the boiler-plate code.

Code generation

  • Step 1

Gradle plugin configuration

// Add the gradle plugin.
plugins {
  id 'org.jet.sql.codegen' version '1.1.2'
}

sqlWrapperConfig {
    sources = fileTree(dir: 'src/main/resources/sql') // source directory for YAML sql configuration
    generatedFileDirectory = file('src/gen/java') // output directory for generated classes.
    userName = 'testUser' // user name required to connect to the database
    password = 'testPassword' // password required to connect to the database
    type = 'postgresql' or 'mysql' // type of the database to connect. Currently only supports postgres and mysql
    host = 'testHost' // hostname where the database server is deployed.
}
  • Step 2 : Add the YAML files to the source directory.

Sample Yaml file

packageName: org.jet.test
className: EmployeeQueries
queries:
  - name: 'get_employee_by_id'
    sql: 'SELECT ID, NAME FROM EMPLOYEE WHERE ID = arg_id'
  • Step 3 : Generate required java classes by running the gradle plugin task
    ./gradlew generateSqlWrapper
  • Step 4 : Use the generated classes

Once the plugin runs , it will auto generate all the required wrapper classes. Which in this example will be a class named EmployeeQueries

under package

    org.jet.test

you can then use it in your code to replace boiler plate code

package org.jet.example;

import org.jet.test.EmployeeQueries;

import java.sql.Connection;
import java.sql.SQLException;

public class EmployeeDao
{
    public void getEmployeeById(int id, Connection connection) throws SQLException
    {
        EmployeeQueries.Runners.GetEmployeeByIdQueryRunner()
                .connection(connection)
                .ID(id) // set the id before executing.
                .executeAsStream() // can also use .execute() or .executeAndReturnIterator()
                .forEach(e -> {
                    System.out.println("Employee Id " + e.id() + ", Employee Name" + e.name());
                });
    }
}

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.