59 lines
1.7 KiB
Java
59 lines
1.7 KiB
Java
package org.gcube.application.perform.service.engine;
|
|
|
|
import java.sql.Connection;
|
|
import java.sql.PreparedStatement;
|
|
import java.sql.SQLException;
|
|
import java.util.ArrayList;
|
|
import java.util.Arrays;
|
|
|
|
import org.gcube.application.perform.service.engine.model.DBField;
|
|
import org.gcube.application.perform.service.engine.model.DBQueryDescriptor;
|
|
import org.gcube.application.perform.service.engine.model.InvalidRequestException;
|
|
|
|
|
|
public class Query {
|
|
|
|
private final String query;
|
|
private final ArrayList<DBField> psFields;
|
|
|
|
public Query(String query,DBField[] fields) {
|
|
this.query=query;
|
|
this.psFields=new ArrayList<>(Arrays.asList(fields));
|
|
}
|
|
|
|
|
|
public PreparedStatement prepare(Connection conn, int statementOption) throws SQLException {
|
|
return conn.prepareStatement(getQuery(),statementOption);
|
|
}
|
|
public PreparedStatement prepare(Connection conn) throws SQLException {
|
|
return conn.prepareStatement(getQuery());
|
|
}
|
|
|
|
|
|
public PreparedStatement get(Connection conn, DBQueryDescriptor desc) throws SQLException, InvalidRequestException {
|
|
PreparedStatement ps=prepare(conn);
|
|
return fill(ps,desc);
|
|
}
|
|
|
|
public PreparedStatement fill(PreparedStatement ps,DBQueryDescriptor desc) throws SQLException, InvalidRequestException{
|
|
ArrayList<DBField> fields=getPSFields();
|
|
for(int i=0;i<fields.size();i++) {
|
|
DBField field=fields.get(i);
|
|
if(!desc.getCondition().containsKey(field))
|
|
throw new InvalidRequestException("Missing field "+field);
|
|
else ps.setObject(i+1, desc.getCondition().get(field), field.getType());
|
|
}
|
|
return ps;
|
|
}
|
|
|
|
|
|
public String getQuery() {
|
|
return query;
|
|
}
|
|
public ArrayList<DBField> getPSFields(){
|
|
return psFields;
|
|
}
|
|
|
|
|
|
}
|