Skip to content

A wrapper to quickly make database queries with asynchronous callbacks

License

Notifications You must be signed in to change notification settings

PascalNB/dbwrapper

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

48 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

dbwrapper

A wrapper to quickly make database queries with asynchronous callbacks.

  1. Setup
  2. Querying
  3. Executing
  4. Value Mapping
  5. Custom Executor
  6. Combining Database Actions

Setup

Add jitpack repository:

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

Add dbwrapper dependency to project:

<dependency>
    <groupId>com.github.PascalNB</groupId>
    <artifactId>dbwrapper</artifactId>
    <version>master-SNAPSHOT</version>
</dependency>

Add config.cfg containing the database credentials, connection URL and JDBC driver as resource or the in the folder the jar is contained in:

username=<username>
password=<password>
# e.g. jdbc:mysql://localhost:3306/database
url=jdbc:<driver>://<host>:<port>/<database>
# e.g. com.mysql.cj.jdbc.Driver
driver=<driver class>

Or set a custom way to authenticate the database:

DatabaseAuthenticator.setImplementation(/* custom authenticator */);

Querying

A database query can be made as follows:

String version = DatabaseAction.of("SELECT version();")
    .query(Mapper.stringValue())
    .await();

System.out.println(version);

This can also be done asynchronously:

DatabaseAction.of("SELECT version();")
    .query(Mapper.stringValue())
    .async(System.out::println);

Query arguments can be added as follows:

DatabaseAction.of("SELECT * FROM users WHERE id=?;", 154)
    .query(Mapper.firstRow())
    .async(row -> {
        if (row == null) {
            return;
        }
        String username = row.get("username");
        // do stuff
    });

Reusing existing queries with different arguments:

Query query = new Query("SELECT * FROM users WHERE id=?;");
DatabaseAction.of(query.withArgs(154))
    .query(Mapper.firstRow())
    .async(row -> {
        // do stuff
    });
DatabaseAction.of(query.withArgs(451))
    .query(Mapper.firstRow())
    .async(row -> {
        // do stuff
    });

Executing

Executing without response:

DatabaseAction.of("DELETE FROM users WHERE id=?;", 154)
    .execute();

Awaiting execution:

DatabaseAction.of("DELETE FROM users WHERE id=?;", 154)
    .execute()
    .await();

Value Mapping

Returned values can be mapped to primitives:

int count = DatabaseAction.of("SELECT count(*) FROM users")
    .query(Mapper.toPrimitive(Integer.class)) // or Integer.TYPE
    .await();

They can also be mapped to objects with the ParseField annotation:

User user = DatabaseAction.of("SELECT id, username FROM users WHERE id=?", 154)
    .query(Mapper.toObject(User.class))
    .await();

List<User> users = DatabaseAction.of("SELECT id, username FROM users WHERE username=?", "username")
    .query(Mapper.toObjects(User.class))
    .await();

Given the following class:

public class User {

    @ParseField // annotate with ParseField
    private int id;

    @ParseField("username") // specifiy different name
    private String name;

    public int getId() {
        return id;
    }

    public String getName() {
        return name;
    }

}

Custom executor

Select what executor to use the database actions on:

ExecutorService executorService = Executors.newFixedThreadPool(10);
DatabaseAction.of("DELETE FROM users WHERE id=?;", 154)
    .withExecutor(executorService)
    .execute();

// run on current thread
DatabaseAction.of("DELETE FROM users WHERE id=?;", 154)
    .withExecutor(Runnable::run)
    .execute();

Combining database actions

Multiple database actions can be combined as follows:

Query query = new Query("SELECT * FROM users WHERE id=?")
DatabaseAction.allOf(
        Mapper.toObject(User.class),
        DatabaseAction.of(query.withArgs(154)),
        DatabaseAction.of(query.withArgs(541)),
        DatabaseAction.of("SELECT * FROM users WHERE username=?;", "username")
    )
    .query()
    .await(list -> {
        for (User user : list) {
            // do stuff
        }
    });

About

A wrapper to quickly make database queries with asynchronous callbacks

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages