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