All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.fluentjdbc.DatabaseInsertWithPkBuilder Maven / Gradle / Ivy

There is a newer version: 0.5.3
Show newest version
package org.fluentjdbc;

import javax.annotation.Nonnull;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.List;

public class DatabaseInsertWithPkBuilder extends DatabaseStatement {

    private DatabaseInsertBuilder insertBuilder;
    private T idValue;

    public DatabaseInsertWithPkBuilder(DatabaseInsertBuilder insertBuilder, T idValue) {
        this.insertBuilder = insertBuilder;
        this.idValue = idValue;
    }

    public DatabaseInsertWithPkBuilder setField(String fieldName, Object parameter) {
        insertBuilder.setField(fieldName, parameter);
        return this;
    }

    public DatabaseInsertWithPkBuilder setFields(List fields, List values) {
        insertBuilder.setFields(fields, values);
        return this;
    }

    @Nonnull
    public T execute(Connection connection) throws SQLException {
        T idValue = this.idValue;
        if (idValue == null) {
            return insertWithAutogeneratedKey(connection);
        } else {
            insertWithPregeneratedKey(connection);
            return idValue;
        }
    }

    private void insertWithPregeneratedKey(Connection connection) throws SQLException {
        assert idValue != null;
        long startTime = System.currentTimeMillis();
        String query = createInsertStatement();
        logger.trace(query);
        try (PreparedStatement stmt = connection.prepareStatement(query)) {
            bindParameters(stmt, insertBuilder.getParameters());
            stmt.executeUpdate();
        } finally {
            logger.debug("time={}s query=\"{}\"",
                    (System.currentTimeMillis()-startTime)/1000.0, query);
        }
    }

    @Nonnull
    private T insertWithAutogeneratedKey(Connection connection) throws SQLException {
        long startTime = System.currentTimeMillis();
        String query = createInsertStatement();
        logger.trace(query);
        try (PreparedStatement stmt = connection.prepareStatement(query, Statement.RETURN_GENERATED_KEYS)) {
            bindParameters(stmt, insertBuilder.getParameters());
            stmt.executeUpdate();
            return getGeneratedKey(stmt);
        } finally {
            logger.debug("time={}s query=\"{}\"", (System.currentTimeMillis()-startTime)/1000.0, query);
        }
    }

    // TODO: This doesn't work for Android - we need to do select last_insert_rowid() explicitly (or update SQLDroid)
    @SuppressWarnings("unchecked")
    private T getGeneratedKey(PreparedStatement stmt) throws SQLException {
        try (ResultSet generatedKeys = stmt.getGeneratedKeys()) {
            generatedKeys.next();
            return (T) Long.valueOf(generatedKeys.getLong(1));
        }
    }

    private String createInsertStatement() {
        return insertBuilder.createInsertStatement();
    }

}