com.github.schlak.universalQB.Definition.StatementBoxes.StatementBox Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of universalQB Show documentation
Show all versions of universalQB Show documentation
The java lib universalQB can be used to access different databases
using formalized objects. These objects got converted into a query matching to the
syntactical rules of the database management system
package com.github.schlak.universalQB.Definition.StatementBoxes;
import com.github.schlak.universalQB.Definition.Cleanable;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.Queue;
/**
* Created by joans on 24.01.17.
*/
public abstract class StatementBox implements Cleanable {
/**
* Creates the prepared statement and returns it.
*
* @param connection the connection
* @return the prepared statement
* @throws SQLException the sql exception
*/
public PreparedStatement getPreparedStatement(Connection connection) throws SQLException {
Queue parameterQueue = this.getParameterQueue();
int queueLength = parameterQueue.size();
PreparedStatement stmt = connection.prepareStatement(this.getPreparedStatementString());
for (int i = 0; i < queueLength; i++) {
stmt.setString(i + 1, parameterQueue.poll());
}
return stmt;
}
/**
* Generates the plane statement without replacing the variables is question marks and
* binding them after that.
*
* @param connection the connection
* @return the statement
* @throws SQLException the sql exception
*/
public abstract PreparedStatement getStatement(Connection connection) throws SQLException;
/**
* Used to validate the query box. If the query box is valid it will return true
* if not the return value will be false
*
* @return the boolean
*/
public abstract boolean validate();
/**
* The method is used to get the parameter queue that could be used to prepare the statement like
* the method {@link StatementBox#getPreparedStatement(Connection)}.
*
* @return the parameter queue
*/
public abstract Queue getParameterQueue();
/**
* The returning value is the query, that contains question marks instead of the values
* that could be bound on there later.
*
* @return the prepared statement string
*/
public abstract String getPreparedStatementString();
/**
* The type that is returned is used to identify the type of statement box like
* in the {@link com.github.schlak.universalQB.Manager.QueryManager} class.
* The method will return its on class.
*
*
* @return the type
*/
public abstract Class getType();
}