Jdbc update sql
parent
2bb9a16cbb
commit
839098ca4f
|
@ -17,6 +17,8 @@ import org.springframework.beans.factory.annotation.Autowired;
|
|||
import org.springframework.jdbc.core.JdbcTemplate;
|
||||
import org.springframework.stereotype.Repository;
|
||||
|
||||
import java.sql.Timestamp;
|
||||
import java.time.Instant;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
|
@ -28,8 +30,7 @@ import java.util.List;
|
|||
public class OauthRepositoryJdbc implements OauthRepository {
|
||||
|
||||
|
||||
|
||||
private static OauthClientDetailsRowMapper oauthClientDetailsRowMapper = new OauthClientDetailsRowMapper();
|
||||
private final OauthClientDetailsRowMapper oauthClientDetailsRowMapper = new OauthClientDetailsRowMapper();
|
||||
|
||||
|
||||
@Autowired
|
||||
|
@ -38,47 +39,49 @@ public class OauthRepositoryJdbc implements OauthRepository {
|
|||
|
||||
@Override
|
||||
public OauthClientDetails findOauthClientDetails(String clientId) {
|
||||
final String sql = " select * from oauth_client_details where client_id = ? ";
|
||||
final String sql = " select * from oauth2_registered_client where client_id = ? ";
|
||||
final List<OauthClientDetails> list = this.jdbcTemplate.query(sql, new Object[]{clientId}, oauthClientDetailsRowMapper);
|
||||
return list.isEmpty() ? null : list.get(0);
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<OauthClientDetails> findAllOauthClientDetails() {
|
||||
final String sql = " select * from oauth_client_details where archived = 0 order by create_time desc ";
|
||||
final String sql = " select * from oauth2_registered_client where archived = 0 order by create_time desc ";
|
||||
return this.jdbcTemplate.query(sql, oauthClientDetailsRowMapper);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void updateOauthClientDetailsArchive(String clientId, boolean archive) {
|
||||
final String sql = " update oauth_client_details set archived = ? where client_id = ? ";
|
||||
final String sql = " update oauth2_registered_client set archived = ? where client_id = ? ";
|
||||
this.jdbcTemplate.update(sql, archive, clientId);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void saveOauthClientDetails(final OauthClientDetails clientDetails) {
|
||||
final String sql = " insert into oauth_client_details(client_id,resource_ids,client_secret,scope,authorized_grant_types,web_server_redirect_uri," +
|
||||
" authorities,access_token_validity,refresh_token_validity,additional_information,trusted,autoapprove) values (?,?,?,?,?,?,?,?,?,?,?,?)";
|
||||
final String sql = " insert into oauth2_registered_client(id,client_id,client_id_issued_at,client_secret,client_secret_expires_at," +
|
||||
"client_name,client_authentication_methods,authorization_grant_types,redirect_uris," +
|
||||
" post_logout_redirect_uris,scopes,client_settings,token_settings) values (?,?,?,?,?,?,?,?,?,?,?,?,?)";
|
||||
|
||||
this.jdbcTemplate.update(sql, ps -> {
|
||||
ps.setString(1, clientDetails.clientId());
|
||||
// ps.setString(2, clientDetails.resourceIds());
|
||||
int index = 1;
|
||||
ps.setString(index++, clientDetails.id());
|
||||
ps.setString(index++, clientDetails.clientId());
|
||||
ps.setTimestamp(index++, Timestamp.from(clientDetails.clientIdIssuedAt()));
|
||||
|
||||
ps.setString(3, clientDetails.clientSecret());
|
||||
// ps.setString(4, clientDetails.scope());
|
||||
ps.setString(index++, clientDetails.clientSecret());
|
||||
Instant clientSecretExpiresAt = clientDetails.clientSecretExpiresAt();
|
||||
ps.setTimestamp(index++, clientSecretExpiresAt != null ? Timestamp.from(clientSecretExpiresAt) : null);
|
||||
ps.setString(index++, clientDetails.clientName());
|
||||
|
||||
// ps.setString(5, clientDetails.authorizedGrantTypes());
|
||||
// ps.setString(6, clientDetails.webServerRedirectUri());
|
||||
ps.setString(index++, clientDetails.clientAuthenticationMethods());
|
||||
ps.setString(index++, clientDetails.authorizationGrantTypes());
|
||||
ps.setString(index++, clientDetails.redirectUris());
|
||||
|
||||
// ps.setString(7, clientDetails.authorities());
|
||||
// ps.setObject(8, clientDetails.accessTokenValidity());
|
||||
|
||||
// ps.setObject(9, clientDetails.refreshTokenValidity());
|
||||
// ps.setString(10, clientDetails.additionalInformation());
|
||||
|
||||
// ps.setBoolean(11, clientDetails.trusted());
|
||||
// ps.setString(12, clientDetails.autoApprove());
|
||||
ps.setString(index++, clientDetails.postLogoutRedirectUris());
|
||||
ps.setString(index++, clientDetails.scopes());
|
||||
ps.setString(index++, clientDetails.clientSettings());
|
||||
|
||||
ps.setString(index++, clientDetails.tokenSettings());
|
||||
});
|
||||
}
|
||||
}
|
||||
|
|
|
@ -34,7 +34,7 @@ import java.util.stream.Collectors;
|
|||
public class UserRepositoryJdbc implements UserRepository {
|
||||
|
||||
|
||||
private static UserRowMapper userRowMapper = new UserRowMapper();
|
||||
private final UserRowMapper userRowMapper = new UserRowMapper();
|
||||
|
||||
@Autowired
|
||||
private JdbcTemplate jdbcTemplate;
|
||||
|
@ -64,8 +64,9 @@ public class UserRepositoryJdbc implements UserRepository {
|
|||
|
||||
@Override
|
||||
public void saveUser(final User user) {
|
||||
final String sql = " insert into user_(guid,archived,create_time,email,password,username,phone) " +
|
||||
" values (?,?,?,?,?,?,?) ";
|
||||
final String sql = " insert into user_(guid,archived,create_time,email,password,username,phone," +
|
||||
"address,nickname,updated_at,enabled) " +
|
||||
" values (?,?,?,?,?,?,?,?,?,?,?) ";
|
||||
this.jdbcTemplate.update(sql, ps -> {
|
||||
ps.setString(1, user.guid());
|
||||
ps.setBoolean(2, user.archived());
|
||||
|
@ -77,10 +78,15 @@ public class UserRepositoryJdbc implements UserRepository {
|
|||
ps.setString(6, user.username());
|
||||
|
||||
ps.setString(7, user.phone());
|
||||
// v3.0.0 added
|
||||
ps.setString(8, user.address());
|
||||
ps.setString(9, user.nickname());
|
||||
ps.setLong(10, user.updatedAt());
|
||||
ps.setBoolean(11, user.enabled());
|
||||
});
|
||||
|
||||
//get user id
|
||||
final Integer id = this.jdbcTemplate.queryForObject("select id from user_ where guid = ?", new Object[]{user.guid()}, Integer.class);
|
||||
final Integer id = this.jdbcTemplate.queryForObject("select id from user_ where guid = ?", Integer.class, user.guid());
|
||||
|
||||
//insert privileges
|
||||
for (final Privilege privilege : user.privileges()) {
|
||||
|
@ -94,8 +100,9 @@ public class UserRepositoryJdbc implements UserRepository {
|
|||
|
||||
@Override
|
||||
public void updateUser(final User user) {
|
||||
final String sql = " update user_ set username = ?, password = ?, phone = ?,email = ? where guid = ? ";
|
||||
this.jdbcTemplate.update(sql, ps -> {
|
||||
final String sql = " update user_ set username = ?, password = ?, phone = ?,email = ?," +
|
||||
"address = ?, nickname = ?, enabled = ? where guid = ? ";
|
||||
int row = this.jdbcTemplate.update(sql, ps -> {
|
||||
ps.setString(1, user.username());
|
||||
ps.setString(2, user.password());
|
||||
|
||||
|
@ -103,13 +110,17 @@ public class UserRepositoryJdbc implements UserRepository {
|
|||
ps.setString(4, user.email());
|
||||
|
||||
ps.setString(5, user.guid());
|
||||
// v3.0.0 added
|
||||
ps.setString(6, user.address());
|
||||
ps.setString(7, user.nickname());
|
||||
ps.setBoolean(8, user.enabled());
|
||||
});
|
||||
}
|
||||
|
||||
@Override
|
||||
public User findByUsername(String username) {
|
||||
final String sql = " select * from user_ where username = ? and archived = 0 ";
|
||||
final List<User> list = this.jdbcTemplate.query(sql, new Object[]{username}, userRowMapper);
|
||||
final List<User> list = this.jdbcTemplate.query(sql, userRowMapper, username);
|
||||
|
||||
User user = null;
|
||||
if (!list.isEmpty()) {
|
||||
|
@ -130,7 +141,7 @@ public class UserRepositoryJdbc implements UserRepository {
|
|||
}
|
||||
sql += " order by create_time desc ";
|
||||
|
||||
final List<User> list = this.jdbcTemplate.query(sql, params, userRowMapper);
|
||||
final List<User> list = this.jdbcTemplate.query(sql, userRowMapper, params);
|
||||
for (User user : list) {
|
||||
user.privileges().addAll(findPrivileges(user.id()));
|
||||
}
|
||||
|
|
|
@ -46,6 +46,11 @@ public class UserRowMapper implements RowMapper<User> {
|
|||
user.username(rs.getString("username"));
|
||||
|
||||
user.lastLoginTime(rs.getTimestamp("last_login_time"));
|
||||
//v3.0.0 added
|
||||
user.address(rs.getString("address"));
|
||||
user.address(rs.getString("nickname"));
|
||||
user.enabled(rs.getBoolean("enabled"));
|
||||
user.updatedAt(rs.getLong("updated_at"));
|
||||
|
||||
return user;
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue