Coder Social home page Coder Social logo

str4ng3r / querybuilder4j Goto Github PK

View Code? Open in Web Editor NEW

This project forked from thederickff/jsqb

1.0 0.0 0.0 127 KB

Stop to lost time writing repeated SQL queries and let Java SQL Query Builder do the job for you. It's simple, fast and lightweight.

Java 100.00%
java mysql oracle pagination postgresql query-builder sql spring-boot

querybuilder4j's Introduction

SQL Query Builder

Stop to lost time writing repeated SQL queries and let Java SQL Query Builder do the job for you. It's simple, fast and lightweight. You don't need to make a connection with a database. This project could be implement in any kind of Java Project since there's any dependency.

1. Installation

For default installation, see Releases section to download the .jar file and add it to the path of your project.

1.1. Installation with Maven

To install with maven

Step 1. Add the JitPack repository to your build file

<repositories>
    <repository>
        <id>jitpack.io</id>
        <url>https://jitpack.io</url>
    </repository>
</repositories>

Step 2. Add the dependency

<dependency>
    <groupId>com.github.STR4NG3R</groupId>
    <artifactId>querybuilder4j</artifactId>
    <version>1.0.2</version>
</dependency>

2. SELECT Statement

2.1. Basic SELECT statement

Usage:

public class Usage {
    public static void main(String[] args)
    {
        Selector selector = new Selector();
        String sql = selector.select("users").write();
    
        System.out.println(sql);
    }
}

Output:

SELECT users.* FROM users

2.2. SELECT with Specific Fields

Usage:

public class Usage {
    public static void main(String[] args)
    {
        Selector selector = new Selector();
        String sql = selector.select("users", "id")
        addSelect("name", "email")
        .write();
    
        System.out.println(sql);
    }
}

Output:

SELECT users.id, users.name, users.email FROM users

2.2. SELECT with where statement

Add a filter criteria to select statement

Usage:

public class Usage {
    public static void main(String[] args)
    {
        String username = "a";
        String email = "[email protected]";

        Selector jsqb = new Selector();
        String sql = jsqb
        .select("users", "id as userId", "name as username", "email as receiver")
        .where("username LIKE \"%:username\"", selector.addParameter("username", username))
        .andWhere("email = :email", selector.addParameter("email", email, true))
        .write();
    
        System.out.println(sql);
    }
}

Output:

SELECT users.id as userId, users.name as username, users.email as receiver FROM users
WHERE usename LIKE "%?" AND email "?"

3. INNER JOIN statement

3.1. Simple Inner join

The join() method expects an enum of possible type of JOIN This method is described as: innerJoin(JOIN join,String table, String on).

Usage:

public class Usage {
    public static void main(String[] args)
    {
        Selector selector = new Selector();
        String sql = selector.select("users as u", "id", "name", "email")
                             .join(JOIN.INNER, "roles as r", "r.id = u.role_id")
                             .addSelect("r.name", "r.id", "r.level")
                             .join(JOIN.RIGHT, "address as a", "a.user_id = u.id")
                             .addSelect("a.street", "a.cp", "a.number")
                             .write();
    
        System.out.println(sql);
    }
}

Output:

SELECT u.id, u.name, u.email, r.name, r.level, a.street, a.cp, a.number FROM users 
INNER JOIN roles r ON r.id = u.role_id
RIGHT JOIN address ON a a.user_id = u.id

4. Authors

Derick Felix

Pablo Eduardo Martinez Solis

5. License

Java SQL Query Builder is licensed under the GPLv3 license.

The GPLv3 License (GPLv3)

Copyright (c) 2023 Pablo Eduardo Martinez Solis, Derick Felix

This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program.  If not, see <http://www.gnu.org/licenses/>.

querybuilder4j's People

Contributors

str4ng3r avatar thederickff avatar

Stargazers

 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.