Skip to content

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.

Notifications You must be signed in to change notification settings

STR4NG3R/querybuilder4j

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

76 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

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 = "anemail@example.com";

        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/>.

About

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.

Topics

Resources

Stars

Watchers

Forks

Packages

No packages published

Languages

  • Java 100.0%