com.wizarius.orm.database.actions.AbstractDBDelete Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of wizarius-orm Show documentation
Show all versions of wizarius-orm Show documentation
Java orm for Postgres or Mysql with migration system and connection pool
package com.wizarius.orm.database.actions;
import com.wizarius.orm.database.AbstractConnection;
import com.wizarius.orm.database.DBConnectionPool;
import com.wizarius.orm.database.DBParsedFieldsList;
import com.wizarius.orm.database.actions.common.AbstractAction;
import com.wizarius.orm.database.actions.common.AbstractWhereAction;
import com.wizarius.orm.database.exceptions.DBException;
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 abstract class AbstractDBDelete extends AbstractWhereAction {
/**
* DBDelete constructor
*/
public AbstractDBDelete(DBParsedFieldsList fieldsMap, DBConnectionPool connectionPool) {
super(connectionPool, fieldsMap);
}
/**
* Execute delete prepared statement
*
* @throws DBException on unable to build query or execute query
*/
public void execute() throws DBException {
try (AbstractConnection session = pool.getConnection()) {
PreparedStatement preparedStatement = toSQLQuery(session);
log.trace("Execute delete sql query: " + preparedStatement.toString());
preparedStatement.executeUpdate();
} catch (SQLException e) {
throw new DBException(" Unable to execute query ", e);
}
}
/**
* Get sql query with all parameters and where conditions
*/
public String toSQLQuery() throws DBException {
try (AbstractConnection session = pool.getConnection()) {
return toSQLQuery(session).toString();
}
}
/**
* Get sql query with all parameters and where conditions
*
* @param connection jdbc connection
* @return sql query
* @throws DBException on unable to build query
*/
private PreparedStatement toSQLQuery(AbstractConnection connection) throws DBException {
try {
PreparedStatement prepareStatement = connection.createPrepareStatement(toPreparedSQLQuery());
wherePrepareStatementQueryBuilder.setupWhereValues(new AtomicInteger(1), prepareStatement);
return prepareStatement;
} catch (SQLException e) {
throw new DBException("Unable to build prepared statement query. " + e.getMessage(), e);
}
}
/**
* Get prepared sql query
*
* @return prepared sql query
*/
private String toPreparedSQLQuery() {
return "DELETE FROM " + fieldsMap.getTableName() + wherePrepareStatementQueryBuilder.buildWhereClause();
}
}