Skip to content

Java library that helps you to create SQL queries using Java objects instead of using raw text

Notifications You must be signed in to change notification settings

JaimePolidura/MySQL-Query-Builder

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

34 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Java library that helps you to create SQL queries using Java objects instead of using raw text.

Set up

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

<dependency>
    <groupId>com.github.JaimeTruman</groupId>
    <artifactId>MySQL-Query-Builder</artifactId>
    <version>1.0.1</version>
</dependency>

SELECT

SELECT * FROM players WHERE money >= 1000 ORDER BY money ASC LIMIT 3
String query = Select.from("players")
          .where("money").biggerOrEqual(1000)
          .orderBy("money", Order.ASC)
          .limit(3)
          .build();

INSERT

INSERT INTO players (name, money) VALUES ('jaimetruman', 100)
String query = Insert.table("players")
          .fields("name", "money")
          .values("jaimetruman", 100)
          .build();

UPDATE

UPDATE players SET money = 100, test = 1 WHERE name = 'jaimetruman'
String query = Update.table("players")
          .set("money", 100)
          .andSet("test", 1)
          .where("name").equal("jaimetruman")
          .build();

DELETE

DELETE FROM players WHERE name = 'jaimetruman' OR money >= 100
String query = Delete.from("players")
          .where("name")
          .equal("jaimetruman")
          .or("money").biggerOrEqual(100)
          .build();

CustomSerializer

INSERT INTO users_json (json) VALUES ("{'name': 'Jaime', 'creationTime': 10}")
MySQLQueryBuilder.addCustomSerializer(User.class, new UserSerializer());

Insert.table("users_json")         
        .fields("json")
        .values(new User("Jaime", 10));

public class UserSerializer implements DatabaseTypeSerializer<User> {
    @Override
    public String serialize(User user) {
        return String.format("\"{'name': '%s', 'creationTime': %s}\"", user.username, user.creationTime);
    } 
}

@AllArgsConstructor
public class User {
    String username;
    long creationTime;
}

About

Java library that helps you to create SQL queries using Java objects instead of using raw text

Resources

Stars

Watchers

Forks

Packages

No packages published

Languages