com.j256.ormlite.android.apptools.OrmLitePreparedQueryLoader Maven / Gradle / Ivy
package com.j256.ormlite.android.apptools;
import java.sql.SQLException;
import java.util.Collections;
import java.util.List;
import android.content.Context;
import android.content.Loader;
import com.j256.ormlite.dao.Dao;
import com.j256.ormlite.stmt.PreparedQuery;
/**
* A {@link Loader} implementation that queries specified {@link Dao} using a {@link PreparedQuery}.
*
* @author Egorand
*/
public class OrmLitePreparedQueryLoader extends BaseOrmLiteLoader {
private PreparedQuery preparedQuery;
public OrmLitePreparedQueryLoader(Context context) {
super(context);
}
public OrmLitePreparedQueryLoader(Context context, Dao dao, PreparedQuery preparedQuery) {
super(context, dao);
this.preparedQuery = preparedQuery;
}
@Override
public List loadInBackground() {
if (dao == null) {
throw new IllegalStateException("Dao is not initialized.");
}
if (preparedQuery == null) {
throw new IllegalStateException("PreparedQuery is not initialized.");
}
try {
return dao.query(preparedQuery);
} catch (SQLException e) {
// XXX: is this really the right thing to do? Maybe throw RuntimeException?
e.printStackTrace();
return Collections.emptyList();
}
}
public void setPreparedQuery(PreparedQuery preparedQuery) {
this.preparedQuery = preparedQuery;
}
public PreparedQuery getPreparedQuery() {
return preparedQuery;
}
}