use of org.odk.collect.android.external.ExternalSQLiteOpenHelper in project collect by opendatakit.
the class ExternalDataHandlerSearch method eval.
@Override
public Object eval(Object[] args, EvaluationContext ec) {
if (args == null || (args.length != 1 && args.length != 4 && args.length != 6)) {
// .getSearchXPathExpression(String appearance)
throw new ExternalDataException(Collect.getInstance().getString(R.string.ext_search_wrong_arguments_error));
}
String searchType = null;
String queriedColumnsParam = null;
List<String> queriedColumns = null;
String queriedValue = null;
if (args.length >= 4) {
searchType = XPathFuncExpr.toString(args[1]);
queriedColumnsParam = XPathFuncExpr.toString(args[2]);
queriedValue = XPathFuncExpr.toString(args[3]);
}
ExternalDataSearchType externalDataSearchType = ExternalDataSearchType.getByKeyword(searchType, ExternalDataSearchType.CONTAINS);
boolean searchRows = false;
boolean useFilter = false;
if (queriedColumnsParam != null && queriedColumnsParam.trim().length() > 0) {
searchRows = true;
queriedColumns = ExternalDataUtil.createListOfColumns(queriedColumnsParam);
}
String filterColumn = null;
String filterValue = null;
if (args.length == 6) {
filterColumn = XPathFuncExpr.toString(args[4]);
filterValue = XPathFuncExpr.toString(args[5]);
useFilter = true;
}
// SCTO-545
String dataSetName = normalize(XPathFuncExpr.toString(args[0]));
Cursor c = null;
try {
ExternalSQLiteOpenHelper sqLiteOpenHelper = getExternalDataManager().getDatabase(dataSetName, true);
SQLiteDatabase db = sqLiteOpenHelper.getReadableDatabase();
LinkedHashMap<String, String> selectColumnMap = ExternalDataUtil.createMapWithDisplayingColumns(getValueColumn(), getDisplayColumns());
List<String> columnsToFetch = new ArrayList<String>(selectColumnMap.keySet());
String safeImageColumn = null;
if (getImageColumn() != null && getImageColumn().trim().length() > 0) {
safeImageColumn = ExternalDataUtil.toSafeColumnName(getImageColumn());
columnsToFetch.add(safeImageColumn);
}
String[] sqlColumns = columnsToFetch.toArray(new String[columnsToFetch.size()]);
String selection;
String[] selectionArgs;
if (searchRows && useFilter) {
selection = "( " + createLikeExpression(queriedColumns) + " ) AND " + ExternalDataUtil.toSafeColumnName(filterColumn) + "=? ";
String[] likeArgs = externalDataSearchType.constructLikeArguments(queriedValue, queriedColumns.size());
selectionArgs = new String[likeArgs.length + 1];
System.arraycopy(likeArgs, 0, selectionArgs, 0, likeArgs.length);
selectionArgs[selectionArgs.length - 1] = filterValue;
} else if (searchRows) {
selection = createLikeExpression(queriedColumns);
selectionArgs = externalDataSearchType.constructLikeArguments(queriedValue, queriedColumns.size());
} else if (useFilter) {
selection = ExternalDataUtil.toSafeColumnName(filterColumn) + "=? ";
selectionArgs = new String[] { filterValue };
} else {
selection = null;
selectionArgs = null;
}
try {
c = db.query(ExternalDataUtil.EXTERNAL_DATA_TABLE_NAME, sqlColumns, selection, selectionArgs, null, null, ExternalDataUtil.SORT_COLUMN_NAME);
} catch (Exception e) {
if (c != null) {
c.close();
}
c = db.query(ExternalDataUtil.EXTERNAL_DATA_TABLE_NAME, sqlColumns, selection, selectionArgs, null, null, null);
}
return createDynamicSelectChoices(c, selectColumnMap, safeImageColumn);
} finally {
if (c != null) {
c.close();
}
}
}
use of org.odk.collect.android.external.ExternalSQLiteOpenHelper in project collect by opendatakit.
the class ExternalDataHandlerPull method eval.
@Override
public Object eval(Object[] args, EvaluationContext ec) {
if (args.length != 4) {
Timber.e("4 arguments are needed to evaluate the %s function", HANDLER_NAME);
return "";
}
String dataSetName = XPathFuncExpr.toString(args[0]);
String queriedColumn = XPathFuncExpr.toString(args[1]);
String referenceColumn = XPathFuncExpr.toString(args[2]);
String referenceValue = XPathFuncExpr.toString(args[3]);
// SCTO-545
dataSetName = normalize(dataSetName);
Cursor c = null;
try {
ExternalSQLiteOpenHelper sqLiteOpenHelper = getExternalDataManager().getDatabase(dataSetName, false);
if (sqLiteOpenHelper == null) {
return "";
}
SQLiteDatabase db = sqLiteOpenHelper.getReadableDatabase();
String[] columns = { ExternalDataUtil.toSafeColumnName(queriedColumn) };
String selection = ExternalDataUtil.toSafeColumnName(referenceColumn) + "=?";
String[] selectionArgs = { referenceValue };
c = db.query(ExternalDataUtil.EXTERNAL_DATA_TABLE_NAME, columns, selection, selectionArgs, null, null, null);
if (c.getCount() > 0) {
c.moveToFirst();
return ExternalDataUtil.nullSafe(c.getString(0));
} else {
Timber.i("Could not find a value in %s where the column %s has the value %s", queriedColumn, referenceColumn, referenceValue);
return "";
}
} catch (SQLiteException e) {
Timber.i(e);
return "";
} finally {
if (c != null) {
c.close();
}
}
}
Aggregations