• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

SimplyVanilla / SimplyRank / #57

27 Mar 2024 09:09PM UTC coverage: 20.0% (+0.03%) from 19.974%
#57

Pull #102

github

web-flow
Merge 703b7cfe7 into 1e8d87bba
Pull Request #102: refactor: use beecp

0 of 11 new or added lines in 1 file covered. (0.0%)

2 existing lines in 1 file now uncovered.

152 of 760 relevant lines covered (20.0%)

0.2 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

0.0
/src/main/java/net/simplyvanilla/simplyrank/database/sql/MySqlClient.java
1
package net.simplyvanilla.simplyrank.database.sql;
2

3
import net.simplyvanilla.simplyrank.SimplyRankPlugin;
4
import org.stone.beecp.BeeDataSource;
5
import org.stone.beecp.BeeDataSourceConfig;
6

7
import java.sql.*;
8

9
public class MySqlClient {
10

11
    public static final String TABLE_PLAYERS_NAME = "player";
12
    public static final String TABLE_GROUPS_NAME = "group";
13

14
    private final String url;
15
    private final String username;
16
    private final String password;
17

18
    private BeeDataSource dataSource;
19

20
    public MySqlClient(String url, String user, String password) {
×
21
        this.url = url;
×
22
        this.username = user;
×
23
        this.password = password;
×
24

NEW
25
        this.connect();
×
NEW
26
        this.initTables();
×
UNCOV
27
    }
×
28

29
    public void connect() {
NEW
30
        var config = new BeeDataSourceConfig();
×
NEW
31
        config.setJdbcUrl(this.url);
×
NEW
32
        config.setUsername(this.username);
×
NEW
33
        config.setPassword(this.password);
×
NEW
34
        this.dataSource = new BeeDataSource(config);
×
UNCOV
35
    }
×
36

37
    public void close() {
NEW
38
        this.dataSource.close();
×
39
    }
×
40

41
    public void update(PreparedStatement statement) throws SQLException {
42
        statement.executeUpdate();
×
43
        statement.close();
×
44
    }
×
45

46
    public ResultSet query(PreparedStatement statement) throws SQLException {
47
        ResultSet rs;
48
        rs = statement.executeQuery();
×
49

50
        statement.closeOnCompletion();
×
51

52
        return rs;
×
53
    }
54

55
    private void executeRawStatement(String cmd) throws SQLException {
NEW
56
        try (Connection connection = this.dataSource.getConnection(); Statement st = connection.createStatement()) {
×
57
            st.execute(cmd);
×
58
        }
59
    }
×
60

61
    private void initTables() {
62

63
        String cmdPlayers =
×
64
            String.format(
×
65
                """
66
                      CREATE TABLE if not exists `%s` (
67
                         `id` BINARY(16) NOT NULL,
68
                         `data` text NOT NULL,
69
                         `created_at` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
70
                         `updated_at` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
71
                         PRIMARY KEY (`id`)
72
                       )
73
                    """,
74
                TABLE_PLAYERS_NAME);
75

76
        String cmdGroups =
×
77
            String.format(
×
78
                """
79
                       CREATE TABLE if not exists `%s` (
80
                         `id` int unsigned NOT NULL AUTO_INCREMENT,
81
                         `name` varchar(255) NOT NULL,
82
                         `data` TEXT NOT NULL,
83
                         `created_at` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
84
                         `updated_at` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
85
                         PRIMARY KEY (`id`),
86
                         UNIQUE KEY `name` (`name`)
87
                       )
88
                    """,
89
                TABLE_GROUPS_NAME);
90

91
        // table has id, address, type, proxy, fetched_at
92
        String proxyCacheTable = """
×
93
                CREATE TABLE IF NOT EXISTS `proxy_cache` (
94
                    `id` INT UNSIGNED NOT NULL AUTO_INCREMENT,
95
                    `address` VARBINARY(16) NOT NULL,
96
                    `type` VARCHAR(255) NOT NULL,
97
                    `proxy` TINYINT(1) NOT NULL,
98
                    `fetched_at` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
99
                    PRIMARY KEY (`id`),
100
                    UNIQUE KEY `address` (`address`),
101
                    INDEX `fetched_at` (`fetched_at`)
102
                )
103
            """;
104

105
        // table has id, address (unique), invoker_id, created_at
106
        String addressWhitelistTable = """
×
107
                CREATE TABLE IF NOT EXISTS `address_whitelist` (
108
                    `id` INT UNSIGNED NOT NULL AUTO_INCREMENT,
109
                    `address` VARBINARY(16) NOT NULL,
110
                    `invoker_id` BINARY(16) NOT NULL,
111
                    `created_at` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
112
                    PRIMARY KEY (`id`),
113
                    UNIQUE KEY `address` (`address`)
114
                )
115
            """;
116

117
        try {
118
            executeRawStatement(cmdPlayers);
×
119
            executeRawStatement(cmdGroups);
×
120
            executeRawStatement(proxyCacheTable);
×
121
            executeRawStatement(addressWhitelistTable);
×
122
        } catch (SQLException e) {
×
123
            SimplyRankPlugin.getInstance().getSLF4JLogger().error("Could not create tables", e);
×
124
        }
×
125
    }
×
126

127
    public PreparedStatement prepareStatement(String qry) throws SQLException {
NEW
128
        try(Connection connection = this.dataSource.getConnection()) {
×
NEW
129
            return connection.prepareStatement(qry);
×
130
        }
131
    }
132

133
    public PreparedStatement prepareStatement(String qry, String... parameters) throws SQLException {
134
        var statement = prepareStatement(qry);
×
135

136
        for (int i = 0; i < parameters.length; i++) {
×
137
            String s = parameters[i];
×
138
            statement.setString(i + 1, s);
×
139
        }
140

141
        return statement;
×
142
    }
143
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc