Commit 3a175449 authored by Nico Mack's avatar Nico Mack

Re-worked getting of variables after query execution

parent f39a0f78
......@@ -384,7 +384,25 @@ public class SqlExecutor extends Executor {
// ---------------------------------------------------------------------------
private void retrieveMultipleResults(Declaration declaration) {
private Variable<?> clearVariable(Declaration declaration) {
Variable<?> variable = declaration.getVariable();
if (variable instanceof VectorVariable) {
VectorVariable<?> vector = (VectorVariable<?>) variable;
vector.clear();
vector.notifyInputChangeListeners();
} else if (variable instanceof MapVariable) {
MapVariable<?, ?> map = (MapVariable<?, ?>) variable;
map.clear();
map.notifyInputChangeListeners();
} else {
variable.setValue(null);
}
return variable;
}
// ---------------------------------------------------------------------------
private Variable<?> retrieveMapVariable(Declaration declaration) {
Variable<?> variable = declaration.getVariable();
if (Strings.isNullOrEmpty(declaration.getEquate())) {
// No Equate specified! Check whether we have a key-value pair declaration
......@@ -395,23 +413,64 @@ public class SqlExecutor extends Executor {
indices[1] = columnNames.indexOf(declaration.getValue());
this.setMap(map, null, indices);
} else {
VectorVariable<?> vector = (VectorVariable<?>) variable;
int index = columnNames.indexOf(variable.getName());
this.setVector(vector, index);
LOGGER.error("Failed to retrieve Map Variable {}! You must either specify KEY/VALUE or FIELD/EQUATE attributes in declaration", variable.getName()); //$NON-NLS-1$
}
} else {
// Equate specified! Check whether we have a field declaration
if (!Strings.isNullOrEmpty(declaration.getField())) {
MapVariable<?, ?> map = (MapVariable<?, ?>) variable;
int[] indices = new int[1];
indices[0] = columnNames.indexOf(declaration.getEquate());
this.setMap(map, null, indices);
} else {
VectorVariable<?> vector = (VectorVariable<?>) variable;
int index = columnNames.indexOf(declaration.getEquate());
this.setVector(vector, index);
MapVariable<?, ?> map = (MapVariable<?, ?>) variable;
int[] indices = new int[1];
indices[0] = columnNames.indexOf(declaration.getEquate());
this.setMap(map, null, indices);
}
return variable;
}
// ---------------------------------------------------------------------------
private Variable<?> retrieveVectorVariable(Declaration declaration) {
Variable<?> variable = declaration.getVariable();
if (Strings.isNullOrEmpty(declaration.getEquate())) {
// No Equate specified! Check whether we have a key-value pair declaration
VectorVariable<?> vector = (VectorVariable<?>) variable;
int index = columnNames.indexOf(variable.getName());
this.setVector(vector, index);
} else {
VectorVariable<?> vector = (VectorVariable<?>) variable;
int index = columnNames.indexOf(declaration.getEquate());
this.setVector(vector, index);
}
return variable;
}
// ---------------------------------------------------------------------------
private Variable<?> retrieveScalarVariable(Declaration declaration) {
Variable<?> variable = declaration.getVariable();
String column = (Strings.isNullOrEmpty(declaration.getEquate())) ? variable.getName() : declaration.getEquate();
int index = columnNames.indexOf(column);
if (index >= 0) {
Object[] values = columnValues.get(0);
Object value = ((index >= 0) && (index < values.length)) ? values[index] : null;
if (LOGGER.isInfoEnabled()) {
String rendered = (value != null) ? StringUtils.abbreviate(value.toString(), ABBREVIATE_LENGTH) : "NULL";
LOGGER.info(Executor.RESULT_TEMPLATE_PLAIN, variable.getName(), rendered); // $NON-NLS-1$
}
}
return variable;
}
// ---------------------------------------------------------------------------
private Variable<?> retrieveResults(Declaration declaration) {
Variable<?> variable = declaration.getVariable();
if (variable instanceof VectorVariable) {
variable = this.retrieveVectorVariable(declaration);
} else if (variable instanceof MapVariable) {
variable = this.retrieveMapVariable(declaration);
} else if (columnValues.size() == 1) {
variable = this.retrieveScalarVariable(declaration);
}
return variable;
}
// ---------------------------------------------------------------------------
......@@ -573,47 +632,55 @@ public class SqlExecutor extends Executor {
*/
@Override
public Variable<?> get(Declaration declaration) {
Variable<?> variable;
Variable<?> variable = declaration.getVariable();
if (columnValues.size() > 1) {
this.retrieveMultipleResults(declaration);
if (columnValues.isEmpty()) {
LOGGER.info("Query returned no results!"); //$NON-NLS-1$
variable = this.clearVariable(declaration);
} else {
String column = (Strings.isNullOrEmpty(declaration.getEquate())) ? variable.getName() : declaration.getEquate();
int index = columnNames.indexOf(column);
if (index >= 0) {
if (!columnValues.isEmpty()) {
Object[] values = columnValues.get(0);
Object value = ((index >= 0) && (index < values.length)) ? values[index] : null;
if (LOGGER.isInfoEnabled()) {
String rendered = (value != null) ? StringUtils.abbreviate(value.toString(), 256) : "NULL";
LOGGER.info(Executor.RESULT_TEMPLATE_PLAIN, variable.getName(), rendered); // $NON-NLS-1$
}
if (variable instanceof VectorVariable) {
VectorVariable<?> vector = (VectorVariable<?>) variable;
this.setVector(vector, index);
} else {
variable.setValueFromObject(value);
}
} else {
LOGGER.info("Query returned no results!"); //$NON-NLS-1$
if (variable instanceof VectorVariable) {
VectorVariable<?> vector = (VectorVariable<?>) variable;
vector.clear();
vector.notifyInputChangeListeners();
} else if (variable instanceof MapVariable) {
MapVariable<?, ?> map = (MapVariable<?, ?>) variable;
map.clear();
map.notifyInputChangeListeners();
} else {
variable.setValue(null);
}
}
}
variable = this.retrieveResults(declaration);
}
return variable;
// if (columnValues.size() > 1) {
// this.retrieveMultipleResults(declaration);
// } else {
// String column = (Strings.isNullOrEmpty(declaration.getEquate())) ? variable.getName() :
// declaration.getEquate();
// int index = columnNames.indexOf(column);
// if (index >= 0) {
// if (!columnValues.isEmpty()) {
// Object[] values = columnValues.get(0);
// Object value = ((index >= 0) && (index < values.length)) ? values[index] : null;
//
// if (LOGGER.isInfoEnabled()) {
// String rendered = (value != null) ? StringUtils.abbreviate(value.toString(), 256) : "NULL";
// LOGGER.info(Executor.RESULT_TEMPLATE_PLAIN, variable.getName(), rendered); // $NON-NLS-1$
// }
// if (variable instanceof VectorVariable) {
// VectorVariable<?> vector = (VectorVariable<?>) variable;
// this.setVector(vector, index);
// } else {
// variable.setValueFromObject(value);
// }
// } else {
// LOGGER.info("Query returned no results!"); //$NON-NLS-1$
// if (variable instanceof VectorVariable) {
// VectorVariable<?> vector = (VectorVariable<?>) variable;
// vector.clear();
// vector.notifyInputChangeListeners();
// } else if (variable instanceof MapVariable) {
// MapVariable<?, ?> map = (MapVariable<?, ?>) variable;
// map.clear();
// map.notifyInputChangeListeners();
// } else {
// variable.setValue(null);
// }
// }
// }
// }
//
// return variable;
}
/** {@inheritDoc} */
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment