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