Update ValueUtils.getValueAsInteger and ValueUtils.getValueAsString, as well as RDBMS's bindParamObject to all be tolerant of an input PossibleValueEnum (which people sometimes accidentally pass in), by extracting its id

This commit is contained in:
2023-12-04 16:01:50 -06:00
parent 6f5c2c16bb
commit 41009a5c84
4 changed files with 21 additions and 0 deletions

View File

@ -42,6 +42,7 @@ import java.util.TimeZone;
import com.kingsrook.qqq.backend.core.context.QContext;
import com.kingsrook.qqq.backend.core.exceptions.QValueException;
import com.kingsrook.qqq.backend.core.model.metadata.fields.QFieldType;
import com.kingsrook.qqq.backend.core.model.metadata.possiblevalues.PossibleValueEnum;
import com.kingsrook.qqq.backend.core.model.session.QSession;
@ -73,6 +74,10 @@ public class ValueUtils
{
return (new String(ba));
}
else if(value instanceof PossibleValueEnum<?> pve)
{
return getValueAsString(pve.getPossibleValueId());
}
else
{
return (String.valueOf(value));
@ -155,6 +160,10 @@ public class ValueUtils
{
return bd.intValueExact();
}
else if(value instanceof PossibleValueEnum<?> pve)
{
return getValueAsInteger(pve.getPossibleValueId());
}
else if(value instanceof String s)
{
if(!StringUtils.hasContent(s))

View File

@ -35,6 +35,7 @@ import java.time.ZoneId;
import java.util.Calendar;
import java.util.GregorianCalendar;
import com.kingsrook.qqq.backend.core.BaseTest;
import com.kingsrook.qqq.backend.core.actions.automation.AutomationStatus;
import com.kingsrook.qqq.backend.core.context.QContext;
import com.kingsrook.qqq.backend.core.exceptions.QValueException;
import com.kingsrook.qqq.backend.core.model.session.QSession;
@ -69,6 +70,8 @@ class ValueUtilsTest extends BaseTest
assertEquals("1", ValueUtils.getValueAsString(1));
assertEquals("1.10", ValueUtils.getValueAsString(new BigDecimal("1.10")));
assertEquals("ABC", ValueUtils.getValueAsString(new byte[] { 65, 66, 67 }));
assertEquals(String.valueOf(AutomationStatus.PENDING_INSERT_AUTOMATIONS.getId()), ValueUtils.getValueAsString(AutomationStatus.PENDING_INSERT_AUTOMATIONS));
}
@ -129,6 +132,8 @@ class ValueUtilsTest extends BaseTest
assertEquals(1, ValueUtils.getValueAsInteger(1.0F));
assertEquals(1, ValueUtils.getValueAsInteger(1.0D));
assertEquals(AutomationStatus.PENDING_INSERT_AUTOMATIONS.getId(), ValueUtils.getValueAsInteger(AutomationStatus.PENDING_INSERT_AUTOMATIONS));
assertThrows(QValueException.class, () -> ValueUtils.getValueAsInteger("a"));
assertThrows(QValueException.class, () -> ValueUtils.getValueAsInteger("a,b"));
assertThrows(QValueException.class, () -> ValueUtils.getValueAsInteger(new Object()));

View File

@ -52,6 +52,7 @@ import java.util.List;
import java.util.Map;
import com.kingsrook.qqq.backend.core.exceptions.QException;
import com.kingsrook.qqq.backend.core.logging.QLogger;
import com.kingsrook.qqq.backend.core.model.metadata.possiblevalues.PossibleValueEnum;
import com.kingsrook.qqq.backend.core.utils.CollectionUtils;
import com.kingsrook.qqq.backend.core.utils.StringUtils;
import org.apache.commons.lang.NotImplementedException;
@ -772,6 +773,10 @@ public class QueryManager
statement.setTimestamp(index, timestamp);
return (1);
}
else if(value instanceof PossibleValueEnum<?> pve)
{
return (bindParamObject(statement, index, pve.getPossibleValueId()));
}
else
{
throw (new SQLException("Unexpected value type [" + value.getClass().getSimpleName() + "] in bindParamObject."));

View File

@ -38,6 +38,7 @@ import java.time.OffsetDateTime;
import java.util.GregorianCalendar;
import java.util.List;
import java.util.Map;
import com.kingsrook.qqq.backend.core.actions.automation.AutomationStatus;
import com.kingsrook.qqq.backend.module.rdbms.BaseTest;
import com.kingsrook.qqq.backend.module.rdbms.TestUtils;
import org.junit.jupiter.api.AfterEach;
@ -126,6 +127,7 @@ class QueryManagerTest extends BaseTest
QueryManager.bindParamObject(ps, 1, LocalDate.now());
QueryManager.bindParamObject(ps, 1, OffsetDateTime.now());
QueryManager.bindParamObject(ps, 1, LocalDateTime.now());
QueryManager.bindParamObject(ps, 1, AutomationStatus.PENDING_INSERT_AUTOMATIONS);
assertThrows(SQLException.class, () ->
{