Browse Source

jdbc template migrate

master
gsd 1 year ago
parent
commit
c37683a1ff
  1. 9
      src/main/java/app/configurations/DbRoConfiguration.java
  2. 9
      src/main/java/app/configurations/DbRwConfigurarion.java
  3. 23
      src/main/java/app/entities/db/AdminInfo.java
  4. 40
      src/main/java/app/entities/db/Ban.java
  5. 13
      src/main/java/app/entities/db/Permition.java
  6. 107
      src/main/java/app/services/db/BanService.java
  7. 37
      src/main/java/app/services/db/DBService.java
  8. 77
      src/main/java/app/services/db/PermitionService.java

9
src/main/java/app/configurations/DbRoConfiguration.java

@ -8,10 +8,10 @@ import org.springframework.boot.autoconfigure.jdbc.DataSourceProperties;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.context.annotation.Scope;
import org.springframework.core.env.Environment;
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.orm.jpa.JpaTransactionManager;
import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean;
import org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter;
@ -69,4 +69,11 @@ public class DbRoConfiguration implements DbConfiguration {
transactionManager.setEntityManagerFactory(EntityManager().getObject());
return transactionManager;
}
@Bean(name = "jt_ro")
@Scope(value = "prototype")
public JdbcTemplate getJT() {
return new JdbcTemplate(DataSource());
}
}

9
src/main/java/app/configurations/DbRwConfigurarion.java

@ -8,11 +8,10 @@ import org.springframework.boot.autoconfigure.jdbc.DataSourceProperties;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.context.annotation.Scope;
import org.springframework.core.env.Environment;
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
import org.springframework.orm.hibernate5.LocalSessionFactoryBean;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.orm.jpa.JpaTransactionManager;
import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean;
import org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter;
@ -66,4 +65,10 @@ public class DbRwConfigurarion implements DbConfiguration{
transactionManager.setEntityManagerFactory(EntityManager().getObject());
return transactionManager;
}
@Bean(name = "jt_rw")
@Scope(value = "prototype")
public JdbcTemplate getJT() {
return new JdbcTemplate(DataSource());
}
}

23
src/main/java/app/entities/db/AdminInfo.java

@ -4,6 +4,19 @@ import app.utils.SteamIDConverter;
import com.fasterxml.jackson.annotation.JsonIgnore;
import lombok.Data;
import java.sql.ResultSet;
import java.sql.SQLException;
/*
CREATE TABLE `bot_admins` (
`id` int(11) NOT NULL,
`vk_id` int(11) NOT NULL,
`discord_id` bigint(20) NOT NULL,
`discord_name` text NOT NULL,
`steam3` text NOT NULL,
`permition` int(11) NOT NULL COMMENT 'ADMIN = 10 MODDER = 5'
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
*/
@Data
public class AdminInfo {
@JsonIgnore
@ -26,4 +39,14 @@ public class AdminInfo {
permition = (int) obj[5];
steam_url = SteamIDConverter.getSteamID(steam3).community_url;
}
public AdminInfo(ResultSet resultSet) throws SQLException {
id = resultSet.getInt("id");
vk_id = resultSet.getInt("vk_id");
discord_id = resultSet.getLong("discord_id");
discord_name = resultSet.getString("discord_name");
steam3 = resultSet.getString("steam3");
permition = resultSet.getInt("permition");
steam_url = SteamIDConverter.getSteamID(steam3).community_url;
}
}

40
src/main/java/app/entities/db/Ban.java

@ -6,8 +6,29 @@ import app.utils.CryptedCookie;
import app.utils.SteamIDConverter;
import com.fasterxml.jackson.annotation.JsonIgnore;
import lombok.Data;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Timestamp;
/*
CREATE TABLE `light_bans` (
`id` int(11) NOT NULL,
`steam_id` varchar(32) NOT NULL,
`account_id` bigint(20) NOT NULL,
`player_name` varchar(65) NOT NULL,
`ban_length` int(1) NOT NULL DEFAULT 0,
`ban_reason` varchar(100) NOT NULL,
`banned_by` varchar(100) NOT NULL,
`banned_by_id` varchar(32) NOT NULL,
`ip` varchar(15) NOT NULL,
`timestamp` timestamp NOT NULL DEFAULT current_timestamp(),
`active` tinyint(1) NOT NULL DEFAULT 1,
`unbanned_by_id` varchar(32) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci DEFAULT NULL,
`unbanned_timestamp` timestamp NULL DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci;
*/
@Data
public class Ban {
int id;
@ -46,6 +67,25 @@ public class Ban {
ban_utime = timestamp.getTime() / 1000;
}
public Ban(ResultSet resultSet) throws SQLException {
id = resultSet.getInt("id");
steam_id = resultSet.getString("steam_id");
account_id = resultSet.getLong("account_id");
player_name = resultSet.getString("player_name");
ban_length = resultSet.getInt("ban_length");
ban_reason = resultSet.getString("ban_reason");
banned_by = resultSet.getString("banned_by");
banned_by_id = resultSet.getString("banned_by_id");
ip = resultSet.getString("ip");
timestamp = resultSet.getTimestamp("timestamp");
active = resultSet.getBoolean("active");
unbanned_by_id = resultSet.getString("unbanned_by_id");
unbanned_timestamp = resultSet.getTimestamp("unbanned_timestamp");
ban_length_seconds = ban_length * 60L;
ban_utime = timestamp.getTime() / 1000;
}
public void cryptIP(CryptedCookie cryptedCookie) {
if (ip != null && !ip.isEmpty()) {
ip = cryptedCookie.Hashed(ip);

13
src/main/java/app/entities/db/Permition.java

@ -3,6 +3,10 @@ package app.entities.db;
import com.fasterxml.jackson.annotation.JsonIgnore;
import lombok.Data;
import java.sql.ResultSet;
import java.sql.SQLException;
//id, flags, immunity, status, amount, UNIX_TIMESTAMP(`reg_date`)
@Data
public class Permition {
@JsonIgnore
@ -21,4 +25,13 @@ public class Permition {
amount = (int) objects[4];
u_timestamp = (long) objects[5];
}
public Permition(ResultSet resultSet) throws SQLException {
id = resultSet.getInt("id");
flags = resultSet.getString("flags");
immunity = resultSet.getInt("immunity");
status = resultSet.getString("status");
amount = resultSet.getInt("amount");
u_timestamp = resultSet.getLong("utime");
}
}

107
src/main/java/app/services/db/BanService.java

@ -8,32 +8,41 @@ import app.utils.CryptedCookie;
import jakarta.persistence.EntityManager;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;
@Service
@Transactional(value = "RwTransactionManager")
public class BanService {
EntityManager entityManager;
@Autowired
@Qualifier("jt_rw")
JdbcTemplate jdbcTemplate;
ServerService serverService;
CryptedCookie cryptedCookie;
PermitionService permitionService;
@Autowired
public BanService(ServerService serverService, @Qualifier(value = "RwEntityManager") EntityManager entityManager, CryptedCookie cryptedCookie, PermitionService permitionService) {
this.entityManager = entityManager;
public BanService(ServerService serverService, CryptedCookie cryptedCookie, PermitionService permitionService) {
this.serverService = serverService;
this.cryptedCookie = cryptedCookie;
this.permitionService = permitionService;
}
public Ban getBan(SteamID steamID) {
List<Object[]> result = entityManager.createNativeQuery("SELECT * FROM light_bans WHERE account_id = ?1 AND active = 1")
.setParameter(1, steamID.account_id)
.getResultList();
return result.stream().map(Ban::new)
return jdbcTemplate.query("SELECT * FROM light_bans WHERE account_id = ? AND active = 1",
new Object[]{ steamID.account_id },
new RowMapper<Ban>() {
@Override
public Ban mapRow(ResultSet resultSet, int num) throws SQLException {
return new Ban(resultSet);
}
}).stream()
.peek(ban -> ban.cryptIP(cryptedCookie))
.peek(ban -> ban.fillAdmins(permitionService))
.findFirst()
@ -41,10 +50,14 @@ public class BanService {
}
public Ban getBan(int ban_id) {
List<Object[]> result = entityManager.createNativeQuery("SELECT * FROM light_bans WHERE id = ?1")
.setParameter(1, ban_id)
.getResultList();
return result.stream().map(Ban::new)
return jdbcTemplate.query("SELECT * FROM light_bans WHERE id = ?",
new Object[]{ ban_id },
new RowMapper<Ban>() {
@Override
public Ban mapRow(ResultSet resultSet, int num) throws SQLException {
return new Ban(resultSet);
}
}).stream()
.peek(ban -> ban.cryptIP(cryptedCookie))
.peek(ban -> ban.fillAdmins(permitionService))
.findFirst()
@ -52,65 +65,71 @@ public class BanService {
}
public List<Ban> getBans(SteamID steamID) {
List<Object[]> result = entityManager.createNativeQuery("SELECT * FROM light_bans WHERE account_id = ?1 AND active = 0")
.setParameter(1, steamID.account_id)
.getResultList();
return result.stream().map(Ban::new)
return jdbcTemplate.query("SELECT * FROM light_bans WHERE account_id = ? AND active = 0",
new Object[]{steamID.account_id},
new RowMapper<Ban>() {
@Override
public Ban mapRow(ResultSet rs, int rowNum) throws SQLException {
return new Ban(rs);
}
}).stream()
.peek(ban -> ban.cryptIP(cryptedCookie))
.peek(ban -> ban.fillAdmins(permitionService))
.toList();
}
public List<Ban> getLastBans(Integer limit, Integer offset) {
return ((List<Ban>) entityManager.createNativeQuery("SELECT * FROM `light_bans` WHERE `active` = ?1 ORDER BY `light_bans`.`id` DESC LIMIT ?2 OFFSET ?3")
.setParameter(1, 1)
.setParameter(2, limit)
.setParameter(3, offset)
.getResultStream().map(obj -> new Ban((Object[]) obj)).toList()).stream()
return jdbcTemplate.query("SELECT * FROM `light_bans` WHERE `active` = ? ORDER BY `light_bans`.`id` DESC LIMIT ? OFFSET ?",
new Object[]{1, limit, offset},
new RowMapper<Ban>() {
@Override
public Ban mapRow(ResultSet rs, int rowNum) throws SQLException {
return new Ban(rs);
}
}).stream()
.peek(ban -> ban.cryptIP(cryptedCookie))
.peek(ban -> ban.fillAdmins(permitionService))
.toList();
//этот каст конечно пиздец, но он работает
}
public Long getBansCount() {
return (Long) entityManager.createNativeQuery("SELECT COUNT(*) as count FROM `light_bans` WHERE active = 1").getSingleResult();
return jdbcTemplate.query("SELECT COUNT(*) as c FROM `light_bans` WHERE active = 1",
new RowMapper<Long>() {
@Override
public Long mapRow(ResultSet rs, int rowNum) throws SQLException {
return rs.getLong("c");
}
}).stream().findFirst().orElse(0L);
}
@Transactional(value = "RwTransactionManager")
public boolean removeBan(PlayerProfile user, PlayerProfile admin) {
Ban ban = getBan(user.getSteamids());
if (ban == null) return false;
return entityManager.createNativeQuery("UPDATE light_bans SET active=?1, unbanned_by_id = ?2, unbanned_timestamp = CURRENT_TIMESTAMP WHERE id = ?3")
.setParameter(1, 0)
.setParameter(2, admin.getSteamids().steam2)
.setParameter(3, ban.getId())
.executeUpdate() > 0;
return jdbcTemplate.update("UPDATE light_bans SET active = ?, unbanned_by_id = ?, unbanned_timestamp = CURRENT_TIMESTAMP WHERE id = ?",
0, admin.getSteamids().steam2, ban.getId()) > 0;
}
@Transactional(value = "RwTransactionManager")
public int addBan(PlayerProfile user, PlayerProfile admin, int ban_length, String ban_reason) {
Ban ban = getBan(user.getSteamids());
if (ban != null) return -1 * ban.getId();
int result = entityManager.createNativeQuery("INSERT INTO light_bans (steam_id, account_id, ban_length, ban_reason, banned_by_id, active, ip, player_name, banned_by) VALUES (?1,?2,?3,?4,?5,?6,?7,?8,?9)")
.setParameter(1, user.getSteamids().steam2)
.setParameter(2, user.getSteamids().account_id)
.setParameter(3, ban_length)
.setParameter(4, ban_reason)
.setParameter(5, admin.getSteamids().steam2)
.setParameter(6, 1)
.setParameter(7, user.getPlay_on() != null ? user.getPlay_on().getIp(cryptedCookie) : "")
.setParameter(8, user.getPlay_on() != null ? user.getPlay_on().getName() : user.getSteam_data().getNickname())
.setParameter(9, admin.getSteam_data().getNickname())
.executeUpdate();
jdbcTemplate.update("INSERT INTO light_bans (steam_id, account_id, ban_length, ban_reason, banned_by_id, active, ip, player_name, banned_by) VALUES (?,?,?,?,?,?,?,?,?)",
user.getSteamids().steam2, user.getSteamids().account_id, ban_length, ban_reason, admin.getSteamids().steam2, 1,
user.getPlay_on() != null ? user.getPlay_on().getIp(cryptedCookie) : "",
user.getPlay_on() != null ? user.getPlay_on().getName() : user.getSteam_data().getNickname(),
admin.getSteam_data().getNickname());
serverService.kickPlayer(user, "banned");
return getBan(user.getSteamids()).getId();
}
public List<Long> getUsersDiscordWithBanOnServers() {
return entityManager.createNativeQuery("SELECT `discord_id` FROM `light_bans` INNER JOIN `steam2discord` ON `light_bans`.`active` = ?1 AND `light_bans`.`steam_id` COLLATE utf8mb4_unicode_ci LIKE `steam2discord`.`steam_id`")
.setParameter(1,1)
.getResultList();
return jdbcTemplate.query("SELECT `discord_id` FROM `light_bans` INNER JOIN `steam2discord` ON `light_bans`.`active` = ?1 AND `light_bans`.`steam_id` COLLATE utf8mb4_unicode_ci LIKE `steam2discord`.`steam_id`",
new Object[]{1},
new RowMapper<Long>() {
@Override
public Long mapRow(ResultSet rs, int rowNum) throws SQLException {
return rs.getLong("discord_id");
}
});
}
public String deCrypt(String crypted) {

37
src/main/java/app/services/db/DBService.java

@ -4,22 +4,23 @@ import jakarta.persistence.EntityManager;
import jakarta.persistence.PersistenceContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Service;
import java.sql.ResultSet;
import java.sql.SQLException;
@Service
public class DBService {
EntityManager entityManager_rw;
EntityManager entityManager_ro;
@Autowired
@Qualifier("jt_rw")
JdbcTemplate jdbcTemplate_rw;
@Autowired
public DBService(
@Qualifier(value = "RwEntityManager") EntityManager entityManager_rw,
@Qualifier(value = "RoEntityManager") EntityManager entityManager_ro
) {
this.entityManager_rw = entityManager_rw;
this.entityManager_ro = entityManager_ro;
}
@Qualifier("jt_ro")
JdbcTemplate jdbcTemplate_ro;
public Long getDBServerTime() {
Long rw_time = getMainServerTime();
@ -28,12 +29,22 @@ public class DBService {
}
public Long getMainServerTime() {
return (Long) entityManager_rw.createNativeQuery("SELECT UNIX_TIMESTAMP()")
.getSingleResult();
return jdbcTemplate_rw.query("SELECT UNIX_TIMESTAMP() as u",
new RowMapper<Long>() {
@Override
public Long mapRow(ResultSet rs, int rowNum) throws SQLException {
return rs.getLong("u");
}
}).stream().findFirst().orElse(0L);
}
public Long getReplicaServerTime() {
return (Long) entityManager_ro.createNativeQuery("SELECT UNIX_TIMESTAMP()")
.getSingleResult();
return jdbcTemplate_rw.query("SELECT UNIX_TIMESTAMP() as u",
new RowMapper<Long>() {
@Override
public Long mapRow(ResultSet rs, int rowNum) throws SQLException {
return rs.getLong("u");
}
}).stream().findFirst().orElse(0L);
}
}

77
src/main/java/app/services/db/PermitionService.java

@ -8,27 +8,32 @@ import jakarta.persistence.EntityManager;
import jakarta.persistence.PersistenceContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.ApplicationContext;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;
@Service
@Transactional("RwTransactionManager")
public class PermitionService {
EntityManager entityManager;
@Autowired
public PermitionService(@Qualifier(value = "RwEntityManager") EntityManager entityManager) {
this.entityManager = entityManager;
}
@Qualifier("jt_rw")
JdbcTemplate jdbcTemplate;
public Permition getPermition(SteamID steamID){
List<Object[]> result = entityManager.createNativeQuery("SELECT id, flags, immunity, status, amount, UNIX_TIMESTAMP(`reg_date`) as u_timestamp FROM sm_admins WHERE `identity` LIKE ?1")
.setParameter(1, steamID.steam2)
.getResultList();
return result.stream().map(Permition::new).findFirst().orElse(null);
return jdbcTemplate.query("SELECT id, flags, immunity, status, amount, UNIX_TIMESTAMP(`reg_date`) as utime as u_timestamp FROM sm_admins WHERE `identity` LIKE ?",
new Object[]{steamID.steam2},
new RowMapper<Permition>() {
@Override
public Permition mapRow(ResultSet resultSet, int num) throws SQLException {
return new Permition(resultSet);
}
}).stream().findFirst().orElse(null);
}
public boolean CheckMorePowerfull(SteamID user_1, SteamID user_2) {
@ -40,36 +45,21 @@ public class PermitionService {
return permition_1.getImmunity() > permition_2.getImmunity();
}
@Transactional
public int addPermition(SteamID steamID, Integer amount, String flags, Integer immunity_level, String status, String comment) {
if (getPermition(steamID) != null) return 0;
return entityManager.createNativeQuery("INSERT INTO `sm_admins` (`authtype`, `identity`, `password`, `flags`, `name`, `immunity`, `comment`, `status`, `reg_date`, `amount`)" +
" VALUES ('steam', ?1, NULL, ?2, '', ?3, ?4, ?5, CURRENT_TIMESTAMP, ?6)")
.setParameter(1, steamID.steam2)
.setParameter(2, flags)
.setParameter(3, immunity_level)
.setParameter(4, comment)
.setParameter(5, status)
.setParameter(6, amount)
.executeUpdate();
return jdbcTemplate.update("INSERT INTO `sm_admins` (`authtype`, `identity`, `password`, `flags`, `name`, `immunity`, `comment`, `status`, `reg_date`, `amount`) VALUES ('steam', ?, NULL, ?, '', ?, ?, ?, CURRENT_TIMESTAMP, ?)",
steamID.steam2, flags, immunity_level, comment, status, amount);
}
@Transactional("RwTransactionManager")
public boolean removePermition(SteamID steamID, String status) {
if (getPermition(steamID) == null) return true;
return entityManager.createNativeQuery("DELETE FROM sm_admins WHERE identity = ?1 AND status LIKE ?2")
.setParameter(1, steamID.steam2)
.setParameter(2, status)
.executeUpdate() > 0;
return jdbcTemplate.update("DELETE FROM sm_admins WHERE identity = ? AND status LIKE ?",
steamID.steam2, status) > 0;
}
@Transactional("RwTransactionManager")
public int extendPermition(SteamID steamID, Integer amount, String status) {
return entityManager.createNativeQuery("UPDATE `sm_admins` SET `amount`=`amount`+?1, `reg_date`=`reg_date` WHERE `identity` LIKE ?2 AND `status` LIKE ?3")
.setParameter(1, amount)
.setParameter(2, steamID.steam2)
.setParameter(3, status)
.executeUpdate();
return jdbcTemplate.update("UPDATE `sm_admins` SET `amount`=`amount`+?1, `reg_date`=`reg_date` WHERE `identity` LIKE ?2 AND `status` LIKE ?3",
amount, steamID.steam2, status);
}
public int addFreeVIP(SteamID steamID, Integer amount) {
@ -91,14 +81,25 @@ public class PermitionService {
public AdminInfo getAdminInfo(SteamID steamID) {
//Так слушай меня ваня, ты ебанько ибо перепутал тут steam2 и steam3, d в бд steam3 хотя по факту там 2
List<Object[]> result = entityManager.createNativeQuery("SELECT id, vk_id, discord_id, discord_name, steam3, permition FROM bot_admins WHERE steam3 LIKE ?1")
.setParameter(1, steamID.steam2)
.getResultList();
List<AdminInfo> result = jdbcTemplate.query("SELECT id, vk_id, discord_id, discord_name, steam3, permition FROM bot_admins WHERE steam3 LIKE ?",
new Object[]{steamID.steam2},
new RowMapper<AdminInfo>() {
@Override
public AdminInfo mapRow(ResultSet resultSet, int num) throws SQLException {
return new AdminInfo(resultSet);
}
});
//fix если модератор снят делаем бан пельменю
if(result.isEmpty()) result = entityManager.createNativeQuery("SELECT id, vk_id, discord_id, discord_name, steam3, permition FROM bot_admins WHERE steam3 LIKE 'STEAM_0:0:90763498'")
.getResultList();
return result.stream().map(AdminInfo::new).findFirst().orElse(null);
if(result.isEmpty()) result = jdbcTemplate.query("SELECT id, vk_id, discord_id, discord_name, steam3, permition FROM bot_admins WHERE steam3 LIKE ?",
new Object[]{"STEAM_0:0:90763498"},
new RowMapper<AdminInfo>() {
@Override
public AdminInfo mapRow(ResultSet resultSet, int num) throws SQLException {
return new AdminInfo(resultSet);
}
});
return result.stream().findFirst().orElse(null);
}
}

Loading…
Cancel
Save