Improve JdbcPublicKeyCredentialUserEntityRepository save

Closes gh-16726

Signed-off-by: Max Batischev <mblancer@mail.ru>
This commit is contained in:
Max Batischev 2025-03-12 13:24:52 +03:00 committed by Josh Cummings
parent 6c24a1e717
commit a50a8b2f17
1 changed files with 6 additions and 15 deletions

View File

@ -1,5 +1,5 @@
/* /*
* Copyright 2002-2024 the original author or authors. * Copyright 2002-2025 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -23,7 +23,6 @@ import java.util.ArrayList;
import java.util.List; import java.util.List;
import java.util.function.Function; import java.util.function.Function;
import org.springframework.dao.DuplicateKeyException;
import org.springframework.jdbc.core.ArgumentPreparedStatementSetter; import org.springframework.jdbc.core.ArgumentPreparedStatementSetter;
import org.springframework.jdbc.core.JdbcOperations; import org.springframework.jdbc.core.JdbcOperations;
import org.springframework.jdbc.core.PreparedStatementSetter; import org.springframework.jdbc.core.PreparedStatementSetter;
@ -124,17 +123,9 @@ public final class JdbcPublicKeyCredentialUserEntityRepository implements Public
@Override @Override
public void save(PublicKeyCredentialUserEntity userEntity) { public void save(PublicKeyCredentialUserEntity userEntity) {
Assert.notNull(userEntity, "userEntity cannot be null"); Assert.notNull(userEntity, "userEntity cannot be null");
boolean existsUserEntity = null != this.findById(userEntity.getId()); int rows = updateUserEntity(userEntity);
if (existsUserEntity) { if (rows == 0) {
updateUserEntity(userEntity); insertUserEntity(userEntity);
}
else {
try {
insertUserEntity(userEntity);
}
catch (DuplicateKeyException ex) {
updateUserEntity(userEntity);
}
} }
} }
@ -144,12 +135,12 @@ public final class JdbcPublicKeyCredentialUserEntityRepository implements Public
this.jdbcOperations.update(SAVE_USER_SQL, pss); this.jdbcOperations.update(SAVE_USER_SQL, pss);
} }
private void updateUserEntity(PublicKeyCredentialUserEntity userEntity) { private int updateUserEntity(PublicKeyCredentialUserEntity userEntity) {
List<SqlParameterValue> parameters = this.userEntityParametersMapper.apply(userEntity); List<SqlParameterValue> parameters = this.userEntityParametersMapper.apply(userEntity);
SqlParameterValue userEntityId = parameters.remove(0); SqlParameterValue userEntityId = parameters.remove(0);
parameters.add(userEntityId); parameters.add(userEntityId);
PreparedStatementSetter pss = new ArgumentPreparedStatementSetter(parameters.toArray()); PreparedStatementSetter pss = new ArgumentPreparedStatementSetter(parameters.toArray());
this.jdbcOperations.update(UPDATE_USER_SQL, pss); return this.jdbcOperations.update(UPDATE_USER_SQL, pss);
} }
@Override @Override