Skip to content
Permalink
Browse files

fix for null ids

  • Loading branch information...
arcuri82 committed Aug 2, 2019
1 parent 8105134 commit d487edcf0acdc7c65756dad8bc8a801f515656c9
@@ -161,7 +161,9 @@ public static void runCommands(Connection connection, List<String> commands) {
* of auto-generated ids
*
* @return a map from InsertionDto id to id of auto-generated primary
* keys in the database (if any was generated)
* keys in the database (if any was generated).
* If an InsertionDto has no id, we will not keep track of any auto-generated
* value for it.
*/
public static Map<Long, Long> execInsert(Connection conn, List<InsertionDto> insertions) throws SQLException {

@@ -180,7 +182,6 @@ public static void runCommands(Connection connection, List<String> commands) {

String sql = prepareSqlInsertionCommand(insertSql, map, i, insDto);


Long autoGeneratedId;

try {
@@ -191,11 +192,26 @@ public static void runCommands(Connection connection, List<String> commands) {
throw new SQLException(msg, e);
}

if (autoGeneratedId == null) {
if(insDto.id == null){
//throw new IllegalArgumentException("Insertion for an autoincrement value in table " + insDto.targetTable + " does not have an id");
continue;
}

if (autoGeneratedId != null) {
map.put(insDto.id, autoGeneratedId);

} else {

/*
check if we need to keep the auto generated value.
there can at most one entry that can be a FK to an auto-increment value
check if in this insertion there is no auto-generated PK,
but there is a foreign key to an auto-increment.
There can at most one entry that can be a FK to an auto-increment value.
FIXME: this is not really true, eg consider tables to handle relationships,
where PK is composed of 2 FK columns, ie the PKs of the 2 related tables.
NEED to support multi-column PKs/FKs
*/

InsertionEntryDto entry = insDto.data.stream()
.filter(e -> e.foreignKeyToPreviouslyGeneratedRow != null)
.findFirst().orElse(null);
@@ -204,9 +220,6 @@ public static void runCommands(Connection connection, List<String> commands) {
long previouslyGeneratedValue = map.get(entry.foreignKeyToPreviouslyGeneratedRow);
map.put(insDto.id, previouslyGeneratedValue);
}

} else {
map.put(insDto.id, autoGeneratedId);
}
}

@@ -271,7 +284,8 @@ private static String replaceQuotes(String value) {
}

/**
* @return a single id for the new row, if any was automatically generated, {@code null} otherwise
* @return a single id for the new row, if any was automatically generated, {@code null} otherwise.
* In other words, return the value of auto-generated primary key, if any was created.
* @throws SQLException
*/
public static Long execInsert(Connection conn, String command) throws SQLException {
@@ -41,6 +41,9 @@ object DbActionTransformer {
will be overridden.
But, as we need it only for automatically generated ones,
this shouldn't matter, as in that case there should be just 1.
FIXME: not really true, as can have multi-column PKs that are
FKs to auto-increment values in other tables.
*/
insertion.id = g.uniqueId
}

0 comments on commit d487edc

Please sign in to comment.
You can’t perform that action at this time.