SQL API

A simple SQL api

License

License

GroupId

GroupId

com.github.fabienbarbero
ArtifactId

ArtifactId

sql-api
Last Version

Last Version

1.1
Release Date

Release Date

Type

Type

jar
Description

Description

SQL API
A simple SQL api
Project URL

Project URL

https://github.com/fabienbarbero/sql-api
Source Code Management

Source Code Management

https://github.com/fabienbarbero/sql-api

Download sql-api

How to add to project

<!-- https://jarcasting.com/artifacts/com.github.fabienbarbero/sql-api/ -->
<dependency>
    <groupId>com.github.fabienbarbero</groupId>
    <artifactId>sql-api</artifactId>
    <version>1.1</version>
</dependency>
// https://jarcasting.com/artifacts/com.github.fabienbarbero/sql-api/
implementation 'com.github.fabienbarbero:sql-api:1.1'
// https://jarcasting.com/artifacts/com.github.fabienbarbero/sql-api/
implementation ("com.github.fabienbarbero:sql-api:1.1")
'com.github.fabienbarbero:sql-api:jar:1.1'
<dependency org="com.github.fabienbarbero" name="sql-api" rev="1.1">
  <artifact name="sql-api" type="jar" />
</dependency>
@Grapes(
@Grab(group='com.github.fabienbarbero', module='sql-api', version='1.1')
)
libraryDependencies += "com.github.fabienbarbero" % "sql-api" % "1.1"
[com.github.fabienbarbero/sql-api "1.1"]

Dependencies

compile (1)

Group / Artifact Type Version
org.slf4j : slf4j-api jar 1.7.25

test (3)

Group / Artifact Type Version
junit : junit jar 4.12
org.xerial : sqlite-jdbc jar 3.23.1
org.slf4j : slf4j-simple jar 1.7.25

Project Modules

There are no modules declared in this project.

SQL API

SQL API is a lightweight library to handle SQL operations. It is only compatible with Java8+.

The use is simple as you will see. By default in Java SQLException must be caught ; here we use a RuntimeException (SQLFaultException).

You can use the following dependency in your Maven projects :

<dependency>
    <groupId>com.github.fabienbarbero</groupId>
    <artifactId>sql-api</artifactId>
    <version>1.1</version>
</dependency>

Use of DAO

Data access objects are useful when using SQL queries. For instance if we have to store users in a table we can create a class names UserDAO and some implementations for various SQL engine (MySQL, Oracle ...)

Example using SQLite

public class User
        implements Entity
{

    public static User newInstance( String name, String email ) {
        User user = new User();
        user.uuid = UUID.randomUUID().toString();
        user.name = name;
        user.email = email;
        return user;
    }

    private String uuid;
    private String name;
    private String email;

    // Use getters and setters

}
public class UserDAOImpl
        implements UserDAO, SQLRecordMapper<User>
{

    private final SQLRunner runner;

    public UserDAOImpl( SQLTransaction tx ) {
        runner = new SQLRunner( tx );
    }

    @Override
    public User buildEntity( SQLRecord record ) {
        // Map the SQL record to a new Java entity
        User user = new User();
        user.setUuid( record.getString( "UUID" ).get() );
        user.setEmail( record.getString( "EMAIL" ).get() );
        user.setName( record.getString( "NAME" ).get() );
        return user;
    }

    @Override
    public void addEntity( User entity )
            throws SQLFaultException {
        runner.execute( new SQLQueryBuilder( "insert into USERS (UUID, EMAIL, NAME) values (?,?,?)",
                                             entity.getUuid(), entity.getEmail(), entity.getName() ) );
    }

    @Override
    public List<User> findAll()
            throws SQLFaultException {
        return runner.query( this, new SQLQueryBuilder( "select * from USERS" ) );
    }

    @Override
    public Optional<User> find( String key )
            throws SQLFaultException {
        return runner.querySingle( this, new SQLQueryBuilder( "select * from USERS where UUID=?", key ) );
    }

    // Other methods

}
SQLiteDataSource ds  = new SQLiteDataSource();
ds.setEncoding( "UTF-8" );
ds.setUrl( "jdbc:sqlite:test.db" );

try (SQLTransaction tx = SQLTransaction.begin( ds )) {
    SQLRunner exec = new SQLRunner( tx );
    UserDAO userDAO = new UserDAOImpl( tx );

    // Create new table
    exec.execute( new SQLQueryBuilder( "create table USERS (UUID char(36) primary key, NAME varchar(128) not null, EMAIL varchar(128) not null)" ) );

    // Insert new entity
    User user = User.newInstance( "john doe", "[email protected]" );
    userDAO.addEntity( user );
}

Database migrations

You can also migrate your database. To do this, you must use the MigrationManager class. The migration can be executed using specific modes:

  • LIVE_BEFORE: when the server is running, but before stopping it for a normal migration. This can be useful for migrating tables without locking them or "pre-heating" the database
  • NORMAL: when the server is stopped. The operation which locks the database should be done here
  • LIVE_AFTER: when the server is running, but after the normal migration is done

Migrators classes must be registered in the manager. There are easy to implements. It contains three methods to implements if needed:

  • migrateLiveBefore: when executing the LIVE_BEFORE migration
  • migrateNormal : when executing the NORMAL migration
  • migrateLiveAfter : when executing the LIVE_AFTER migration

Each of these method has a "context" parameter. It only centralize the useful objects to process the migration

Here is a simple usage:

private static class CreateTableMigrator extends Migrator {

    public CreateTableMigrator() {
        super( "create-table" );
    }

    @Override
    protected void migrateNormal( MigrationContext context ) throws Exception {
        SQLRunner runner = context.getRunner();
        runner.execute( new SQLQueryBuilder(
                "create table USERS (UUID varchar(36) primary key, NAME varchar(128) not null)") );
    }
}
MigrationManager manager = new MigrationManager( dataSource );

// Register the migrators
manager.register(new CreateTableMigrator());
manager.register(new FillTableMigrator());

// Execute the migration in "normal" mode
manager.execute( MigrationManager.Mode.NORMAL );

Versions

Version
1.1
1.0.1