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

com.wizarius.orm.database.actions.WizDBUpdate Maven / Gradle / Ivy

There is a newer version: 0.0.27.6
Show newest version
package com.wizarius.orm.database.actions;

import com.wizarius.orm.database.DBException;
import com.wizarius.orm.database.connection.DBConnection;
import com.wizarius.orm.database.connection.DBConnectionPool;
import com.wizarius.orm.database.entityreader.DBParsedField;
import com.wizarius.orm.database.entityreader.DBParsedFieldsList;
import lombok.extern.slf4j.Slf4j;

import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.concurrent.atomic.AtomicInteger;

/**
 * @author Vladyslav Shyshkin on 21.01.17.
 */
@Slf4j
public class WizDBUpdate extends WizAbstractWhereAction> implements IDBUpdate {
    private final String preparedStatementQuery;

    public WizDBUpdate(DBConnectionPool pool, DBParsedFieldsList fields) {
        super(pool, fields);
        this.preparedStatementQuery = buildPrepareStatementQuery();
    }

    /**
     * Build prepare statement query
     *
     * @return prepare statement query
     */
    private String buildPrepareStatementQuery() {
        StringBuilder sb = new StringBuilder();
        sb.append("UPDATE ").append(fields.getTableName()).append(" SET ");
        //build delete into query
        for (DBParsedField entry : fields) {
            if (entry.isJoinField() || entry.isAutoincrement()) {
                continue;
            }
            sb.append(entry.getDbFieldName()).append(" = ").append("?").append(", ");
        }
        sb.setLength(sb.length() - 2);
        return sb.toString();
    }

    /**
     * Execute delete query
     *
     * @param entity entity to delete
     * @throws DBException on unable to delete
     */
    @Override
    public void execute(Entity entity) throws DBException {
        try (DBConnection connection = pool.getConnection()) {
            execute(entity, connection);
        }
    }

    /**
     * Execute delete query
     * If connection is presented, it is assumed that the user himself wants to manage the connection
     * The connection will not be automatically closed after the request
     *
     * @param entity     entity to delete
     * @param connection connection to database
     * @throws DBException on unable to delete
     */
    @Override
    public void execute(Entity entity, DBConnection connection) throws DBException {
        try {
            PreparedStatement statement = toPreparedSQLQuery(entity, connection);
            log.trace("Execute delete query: " + statement.toString());
            statement.executeUpdate();
        } catch (SQLException e) {
            throw new DBException("Unable to execute query " + e.getMessage(), e);
        }
    }

    /**
     * Get sql query with all parameters and where conditions
     *
     * @return query for execution
     */
    public String toSQLQuery(Entity entity) throws DBException {
        try (DBConnection connection = pool.getConnection()) {
            return toPreparedSQLQuery(entity, connection).toString();
        }
    }

    /**
     * Get sql query with all parameters and where conditions
     *
     * @param connection jdbc connection
     * @return prepared sql query
     * @throws DBException on unable to build query
     */
    private PreparedStatement toPreparedSQLQuery(Entity entity, DBConnection connection) throws DBException {
        try {
            PreparedStatement statement = connection.createPrepareStatement(preparedStatementQuery + whereQueryBuilder.buildWhereClause());
            AtomicInteger index = new AtomicInteger(1);
            fieldsPrepareStatementQueryBuilder.setupPreparedStatementValues(index, entity, statement);
            whereQueryBuilder.setupWhereValues(index, statement);
            return statement;
        } catch (SQLException e) {
            throw new DBException("Unable to build prepared statement query. " + e.getMessage(), e);
        }
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy