Fix passing parameters LIMIT/OFFSET to sql query in the findAll()

This commit is contained in:
Ivo Prajer
2015-04-01 18:22:13 +02:00
parent bc28df4093
commit dd46044361

View File

@@ -28,6 +28,7 @@ import org.dspace.workflow.WorkflowItem;
import org.dspace.xmlworkflow.storedcomponents.CollectionRole;
import org.dspace.xmlworkflow.storedcomponents.XmlWorkflowItem;
import java.io.Serializable;
import java.io.IOException;
import java.io.InputStream;
import java.sql.PreparedStatement;
@@ -294,31 +295,48 @@ public class Collection extends DSpaceObject
* @return the collections in the system
* @throws SQLException
*/
public static Collection[] findAll(Context context) throws SQLException {
public static Collection[] findAll(Context context) throws SQLException
{
TableRowIterator tri = null;
try {
String query = "SELECT c.* FROM collection c " +
"LEFT JOIN metadatavalue m on (m.resource_id = c.collection_id and m.resource_type_id = ? and m.metadata_field_id = ?) ";
if(DatabaseManager.isOracle()){
query += " ORDER BY cast(m.text_value as varchar2(128))";
}else{
query += " ORDER BY m.text_value";
}
List<Collection> collections = null;
List<Serializable> params = new ArrayList<Serializable>();
StringBuffer query = new StringBuffer(
"SELECT c.*" +
"FROM collection c " +
"LEFT JOIN metadatavalue m ON (" +
"m.resource_id = c.collection_id AND " +
"m.resource_type_id = ? AND " +
"m.metadata_field_id = ?" +
")"
);
tri = DatabaseManager.query(context,
query,
Constants.COLLECTION,
MetadataField.findByElement(context, MetadataSchema.find(context, MetadataSchema.DC_SCHEMA).getSchemaID(), "title", null).getFieldID()
);
} catch (SQLException e) {
log.error("Find all Collections - ",e);
throw e;
if (DatabaseManager.isOracle())
{
query.append(" ORDER BY cast(m.text_value as varchar2(128))");
}
else
{
query.append(" ORDER BY m.text_value");
}
List<Collection> collections = new ArrayList<Collection>();
params.add(Constants.COLLECTION);
params.add(
MetadataField.findByElement(
context,
MetadataSchema.find(context, MetadataSchema.DC_SCHEMA).getSchemaID(),
"title",
null
).getFieldID()
);
try
{
tri = DatabaseManager.query(
context, query.toString(), params.toArray()
);
collections = new ArrayList<Collection>();
while (tri.hasNext())
{
TableRow row = tri.next();
@@ -337,6 +355,11 @@ public class Collection extends DSpaceObject
}
}
}
catch (SQLException e)
{
log.error("Find all Collections - ", e);
throw e;
}
finally
{
// close the TableRowIterator to free up resources
@@ -363,31 +386,47 @@ public class Collection extends DSpaceObject
public static Collection[] findAll(Context context, Integer limit, Integer offset) throws SQLException
{
TableRowIterator tri = null;
try{
String query = "SELECT c.* FROM collection c " +
"LEFT JOIN metadatavalue m on (m.resource_id = c.collection_id and m.resource_type_id = ? and m.metadata_field_id = ?) ";
List<Collection> collections = null;
List<Serializable> params = new ArrayList<Serializable>();
StringBuffer query = new StringBuffer(
"SELECT c.*" +
"FROM collection c " +
"LEFT JOIN metadatavalue m ON (" +
"m.resource_id = c.collection_id AND " +
"m.resource_type_id = ? AND " +
"m.metadata_field_id = ?" +
")"
);
if(DatabaseManager.isOracle()){
query += " ORDER BY cast(m.text_value as varchar2(128))";
}else{
query += " ORDER BY m.text_value";
}
query += " limit ? offset ?";
tri = DatabaseManager.query(context,
query,
Constants.COLLECTION,
MetadataField.findByElement(context, MetadataSchema.find(context, MetadataSchema.DC_SCHEMA).getSchemaID(), "title", null).getFieldID(),
limit,
offset
);
} catch (SQLException e) {
log.error("Find all Collections offset/limit - ",e);
throw e;
if (DatabaseManager.isOracle())
{
query.append(" ORDER BY cast(m.text_value as varchar2(128))");
}
List<Collection> collections = new ArrayList<Collection>();
else
{
query.append(" ORDER BY m.text_value");
}
params.add(Constants.COLLECTION);
params.add(
MetadataField.findByElement(
context,
MetadataSchema.find(context, MetadataSchema.DC_SCHEMA).getSchemaID(),
"title",
null
).getFieldID()
);
DatabaseManager.applyOffsetAndLimit(query, params, offset, limit);
try
{
tri = DatabaseManager.query(
context, query.toString(), params.toArray()
);
collections = new ArrayList<Collection>();
while (tri.hasNext())
{
TableRow row = tri.next();
@@ -406,6 +445,11 @@ public class Collection extends DSpaceObject
}
}
}
catch (SQLException e)
{
log.error("Find all Collections offset/limit - ", e);
throw e;
}
finally
{
// close the TableRowIterator to free up resources