Expand access to SqlQuery

This commit is contained in:
jamesagnew 2021-07-08 16:16:08 -04:00
parent ee1f1fb5b8
commit 6d4c2ef8f0

View File

@ -21,6 +21,7 @@ package ca.uhn.fhir.jpa.util;
*/ */
import ca.uhn.fhir.util.UrlUtil; import ca.uhn.fhir.util.UrlUtil;
import org.apache.commons.lang3.Validate;
import org.hibernate.engine.jdbc.internal.BasicFormatterImpl; import org.hibernate.engine.jdbc.internal.BasicFormatterImpl;
import java.util.ArrayList; import java.util.ArrayList;
@ -37,14 +38,22 @@ public class SqlQuery {
private final long myElapsedTime; private final long myElapsedTime;
private final StackTraceElement[] myStackTrace; private final StackTraceElement[] myStackTrace;
private final int mySize; private final int mySize;
private final LanguageEnum myLanguage;
public SqlQuery(String theSql, List<String> theParams, long theQueryTimestamp, long theElapsedTime, StackTraceElement[] theStackTraceElements, int theSize) {
this(theSql, theParams, theQueryTimestamp, theElapsedTime, theStackTraceElements, theSize, LanguageEnum.SQL);
}
public SqlQuery(String theSql, List<String> theParams, long theQueryTimestamp, long theElapsedTime, StackTraceElement[] theStackTraceElements, int theSize, LanguageEnum theLanguage) {
Validate.notNull(theLanguage, "theLanguage must not be null");
SqlQuery(String theSql, List<String> theParams, long theQueryTimestamp, long theElapsedTime, StackTraceElement[] theStackTraceElements, int theSize) {
mySql = theSql; mySql = theSql;
myParams = Collections.unmodifiableList(theParams); myParams = Collections.unmodifiableList(theParams);
myQueryTimestamp = theQueryTimestamp; myQueryTimestamp = theQueryTimestamp;
myElapsedTime = theElapsedTime; myElapsedTime = theElapsedTime;
myStackTrace = theStackTraceElements; myStackTrace = theStackTraceElements;
mySize = theSize; mySize = theSize;
myLanguage = theLanguage;
} }
public long getQueryTimestamp() { public long getQueryTimestamp() {
@ -63,14 +72,20 @@ public class SqlQuery {
return getSql(theInlineParams, theFormat, false); return getSql(theInlineParams, theFormat, false);
} }
public LanguageEnum getLanguage() {
return myLanguage;
}
public String getSql(boolean theInlineParams, boolean theFormat, boolean theSanitizeParams) { public String getSql(boolean theInlineParams, boolean theFormat, boolean theSanitizeParams) {
String retVal = mySql; String retVal = mySql;
if (theFormat) { if (theFormat) {
retVal = new BasicFormatterImpl().format(retVal); if (getLanguage() == LanguageEnum.SQL) {
retVal = new BasicFormatterImpl().format(retVal);
// BasicFormatterImpl annoyingly adds a newline at the very start of its output // BasicFormatterImpl annoyingly adds a newline at the very start of its output
while (retVal.startsWith("\n")) { while (retVal.startsWith("\n")) {
retVal = retVal.substring(1); retVal = retVal.substring(1);
}
} }
} }
@ -102,4 +117,14 @@ public class SqlQuery {
public int getSize() { public int getSize() {
return mySize; return mySize;
} }
public enum LanguageEnum {
SQL,
JSON
}
} }