mirror of
https://github.com/Kingsrook/qqq.git
synced 2025-07-18 13:10:44 +00:00
CE-1546 - Moving audit ids to longs and adding general support for long ids
This commit is contained in:
@ -146,8 +146,8 @@ public class RDBMSInsertAction extends AbstractRDBMSAction implements InsertInte
|
||||
// todo sql customization - can edit sql and/or param list
|
||||
// todo - non-serial-id style tables
|
||||
// todo - other generated values, e.g., createDate... maybe need to re-select?
|
||||
List<Integer> idList = QueryManager.executeInsertForGeneratedIds(connection, sql.toString(), params);
|
||||
int index = 0;
|
||||
List<Serializable> idList = QueryManager.executeInsertForGeneratedIds(connection, sql.toString(), params, table.getFields());
|
||||
int index = 0;
|
||||
for(QRecord record : page)
|
||||
{
|
||||
QRecord outputRecord = new QRecord(record);
|
||||
@ -155,7 +155,7 @@ public class RDBMSInsertAction extends AbstractRDBMSAction implements InsertInte
|
||||
|
||||
if(CollectionUtils.nullSafeIsEmpty(record.getErrors()))
|
||||
{
|
||||
Integer id = idList.get(index++);
|
||||
Serializable id = idList.get(index++);
|
||||
outputRecord.setValue(table.getPrimaryKeyField(), id);
|
||||
}
|
||||
}
|
||||
|
@ -51,8 +51,11 @@ import java.util.Collections;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Optional;
|
||||
import com.kingsrook.qqq.backend.core.exceptions.QException;
|
||||
import com.kingsrook.qqq.backend.core.logging.QLogger;
|
||||
import com.kingsrook.qqq.backend.core.model.metadata.fields.QFieldMetaData;
|
||||
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.utils.CollectionUtils;
|
||||
import com.kingsrook.qqq.backend.core.utils.StringUtils;
|
||||
@ -526,18 +529,57 @@ public class QueryManager
|
||||
/*******************************************************************************
|
||||
** todo - needs (specific) unit test
|
||||
*******************************************************************************/
|
||||
public static List<Integer> executeInsertForGeneratedIds(Connection connection, String sql, List<Object> params) throws SQLException
|
||||
public static List<Serializable> executeInsertForGeneratedIds(Connection connection, String sql, List<Object> params, Map<String, QFieldMetaData> fields) throws SQLException
|
||||
{
|
||||
try(PreparedStatement statement = connection.prepareStatement(sql, Statement.RETURN_GENERATED_KEYS))
|
||||
{
|
||||
bindParams(params.toArray(), statement);
|
||||
incrementStatistic(STAT_QUERIES_RAN);
|
||||
statement.executeUpdate();
|
||||
ResultSet generatedKeys = statement.getGeneratedKeys();
|
||||
List<Integer> rs = new ArrayList<>();
|
||||
|
||||
/////////////////////////////////////////////////////////////
|
||||
// We default to idType of INTEGER but if we are passed in //
|
||||
// fields attempt to find the dataType of the id field //
|
||||
/////////////////////////////////////////////////////////////
|
||||
QFieldType idType = QFieldType.INTEGER;
|
||||
if(fields != null && !fields.isEmpty())
|
||||
{
|
||||
Optional<QFieldMetaData> field = fields.values().stream()
|
||||
.filter(f -> f.getName().equals("id"))
|
||||
.findFirst();
|
||||
|
||||
if(field.isPresent())
|
||||
{
|
||||
/////////////////////////////////////////////////////////
|
||||
// if we find "id" field get the type and use it below //
|
||||
/////////////////////////////////////////////////////////
|
||||
idType = field.get().getType();
|
||||
}
|
||||
}
|
||||
|
||||
ResultSet generatedKeys = statement.getGeneratedKeys();
|
||||
List<Serializable> rs = new ArrayList<>();
|
||||
while(generatedKeys.next())
|
||||
{
|
||||
rs.add(getInteger(generatedKeys, 1));
|
||||
switch(idType)
|
||||
{
|
||||
case INTEGER:
|
||||
{
|
||||
rs.add(getInteger(generatedKeys, 1));
|
||||
break;
|
||||
}
|
||||
case LONG:
|
||||
{
|
||||
rs.add(getLong(generatedKeys, 1));
|
||||
break;
|
||||
}
|
||||
default:
|
||||
{
|
||||
LOG.warn("Unknown id data type, attempting to getInteger.", logPair("sql", sql));
|
||||
rs.add(getInteger(generatedKeys, 1));
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
return (rs);
|
||||
}
|
||||
|
Reference in New Issue
Block a user