refactor sql view validation to fix NPE

This commit is contained in:
Grahame Grieve 2024-10-08 05:44:02 +08:00
parent 6439a2a732
commit 6123909533
4 changed files with 32 additions and 24 deletions

View File

@ -3,16 +3,17 @@ package org.hl7.fhir.r5.utils.sql;
import java.util.List;
import org.hl7.fhir.r5.model.Base;
import org.hl7.fhir.r5.utils.sql.Validator.TrueFalseOrUnknown;
public interface Storage {
boolean supportsArrays();
boolean supportsComplexTypes();
TrueFalseOrUnknown supportsArrays();
TrueFalseOrUnknown supportsComplexTypes();
Store createStore(String name, List<Column> columns);
void addRow(Store store, List<Cell> cells);
void finish(Store store);
boolean needsName();
TrueFalseOrUnknown needsName();
String getKeyForSourceResource(Base res);
String getKeyForTargetResource(Base res);
}

View File

@ -4,6 +4,7 @@ import java.util.List;
import org.hl7.fhir.exceptions.FHIRException;
import org.hl7.fhir.r5.model.Base;
import org.hl7.fhir.r5.utils.sql.Validator.TrueFalseOrUnknown;
import org.hl7.fhir.utilities.json.model.JsonArray;
import org.hl7.fhir.utilities.json.model.JsonBoolean;
import org.hl7.fhir.utilities.json.model.JsonElement;
@ -18,8 +19,8 @@ public class StorageJson implements Storage {
private JsonArray rows;
@Override
public boolean supportsArrays() {
return true;
public TrueFalseOrUnknown supportsArrays() {
return TrueFalseOrUnknown.TRUE;
}
@Override
@ -77,13 +78,13 @@ public class StorageJson implements Storage {
}
@Override
public boolean supportsComplexTypes() {
return true;
public TrueFalseOrUnknown supportsComplexTypes() {
return TrueFalseOrUnknown.TRUE;
}
@Override
public boolean needsName() {
return false;
public TrueFalseOrUnknown needsName() {
return TrueFalseOrUnknown.FALSE;
}
@Override

View File

@ -7,6 +7,7 @@ import java.util.List;
import org.hl7.fhir.exceptions.FHIRException;
import org.hl7.fhir.r5.model.Base;
import org.hl7.fhir.r5.utils.sql.Validator.TrueFalseOrUnknown;
import org.hl7.fhir.utilities.CommaSeparatedStringBuilder;
public class StorageSqlite3 implements Storage {
@ -119,18 +120,18 @@ public class StorageSqlite3 implements Storage {
}
@Override
public boolean supportsArrays() {
return false;
public TrueFalseOrUnknown supportsArrays() {
return TrueFalseOrUnknown.FALSE;
}
@Override
public boolean supportsComplexTypes() {
return false;
public TrueFalseOrUnknown supportsComplexTypes() {
return TrueFalseOrUnknown.FALSE;
}
@Override
public boolean needsName() {
return true;
public TrueFalseOrUnknown needsName() {
return TrueFalseOrUnknown.TRUE;
}
@Override

View File

@ -32,6 +32,7 @@ import org.hl7.fhir.r5.model.UnsignedIntType;
import org.hl7.fhir.r5.model.UriType;
import org.hl7.fhir.r5.model.UrlType;
import org.hl7.fhir.r5.model.UuidType;
import org.hl7.fhir.r5.utils.sql.Validator.TrueFalseOrUnknown;
import org.hl7.fhir.r5.fhirpath.ExpressionNode.CollectionStatus;
import org.hl7.fhir.r5.fhirpath.FHIRPathEngine.IssueMessage;
import org.hl7.fhir.utilities.Utilities;
@ -49,18 +50,22 @@ import org.hl7.fhir.utilities.validation.ValidationMessage.Source;
public class Validator {
public enum TrueFalseOrUnknown {
TRUE, FALSE, UNKNOWN
}
private IWorkerContext context;
private FHIRPathEngine fpe;
private List<String> prohibitedNames = new ArrayList<String>();
private List<ValidationMessage> issues = new ArrayList<ValidationMessage>();
private Boolean arrays;
private Boolean complexTypes;
private Boolean needsName;
private TrueFalseOrUnknown arrays;
private TrueFalseOrUnknown complexTypes;
private TrueFalseOrUnknown needsName;
private String resourceName;
private String name;
public Validator(IWorkerContext context, FHIRPathEngine fpe, List<String> prohibitedNames, Boolean arrays, Boolean complexTypes, Boolean needsName) {
public Validator(IWorkerContext context, FHIRPathEngine fpe, List<String> prohibitedNames, TrueFalseOrUnknown arrays, TrueFalseOrUnknown complexTypes, TrueFalseOrUnknown needsName) {
super();
this.context = context;
this.fpe = fpe;
@ -82,7 +87,7 @@ public class Validator {
if (nameJ == null) {
if (needsName == null) {
hint(path, viewDefinition, "No name provided. A name is required in many contexts where a ViewDefinition is used");
} else if (needsName) {
} else if (needsName == TrueFalseOrUnknown.TRUE) {
error(path, viewDefinition, "No name provided", IssueType.REQUIRED);
}
} else if (!(nameJ instanceof JsonString)) {
@ -334,9 +339,9 @@ public class Validator {
hint(path, column, "collection is true, but the path statement(s) can only return single values for the column '"+columnName+"'");
}
} else {
if (arrays == null) {
if (arrays == TrueFalseOrUnknown.UNKNOWN) {
warning(path, expression, "The column '"+columnName+"' appears to be a collection based on it's path. Collections are not supported in all execution contexts");
} else if (!arrays) {
} else if (arrays == TrueFalseOrUnknown.FALSE) {
warning(path, expression, "The column '"+columnName+"' appears to be a collection based on it's path, but this is not allowed in the current execution context");
}
if (td.getCollectionStatus() != CollectionStatus.SINGLETON) {
@ -373,9 +378,9 @@ public class Validator {
String type = types.iterator().next();
boolean ok = false;
if (!isSimpleType(type) && !"null".equals(type)) {
if (complexTypes) {
if (complexTypes == TrueFalseOrUnknown.UNKNOWN) {
warning(path, expression, "Column is a complex type. This is not supported in some Runners");
} else if (!complexTypes) {
} else if (complexTypes == TrueFalseOrUnknown.FALSE) {
error(path, expression, "Column is a complex type but this is not allowed in this context", IssueType.BUSINESSRULE);
} else {
ok = true;