get(Banishment)   A
last analyzed

Complexity

Conditions 1

Size

Total Lines 5
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 5
dl 0
loc 5
ccs 2
cts 2
cp 1
crap 1
rs 10
c 1
b 0
f 0
1
/*
2
 * This file is part of Araknemu.
3
 *
4
 * Araknemu is free software: you can redistribute it and/or modify
5
 * it under the terms of the GNU Lesser General Public License as published by
6
 * the Free Software Foundation, either version 3 of the License, or
7
 * (at your option) any later version.
8
 *
9
 * Araknemu is distributed in the hope that it will be useful,
10
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12
 * GNU Lesser General Public License for more details.
13
 *
14
 * You should have received a copy of the GNU Lesser General Public License
15
 * along with Araknemu.  If not, see <https://www.gnu.org/licenses/>.
16
 *
17
 * Copyright (c) 2017-2020 Vincent Quatrevieux
18
 */
19
20
package fr.quatrevieux.araknemu.data.living.repository.implementation.sql;
21
22
import fr.quatrevieux.araknemu.core.dbal.executor.QueryExecutor;
23
import fr.quatrevieux.araknemu.core.dbal.repository.Record;
24
import fr.quatrevieux.araknemu.core.dbal.repository.RepositoryException;
25
import fr.quatrevieux.araknemu.core.dbal.repository.RepositoryUtils;
26
import fr.quatrevieux.araknemu.data.living.entity.account.Banishment;
27
import fr.quatrevieux.araknemu.data.living.repository.account.BanishmentRepository;
28
import fr.quatrevieux.araknemu.data.transformer.Transformer;
29
30
import java.sql.ResultSet;
31
import java.sql.SQLException;
32
import java.time.Instant;
33
import java.util.List;
34
35
/**
36
 * SQL Implementation for {@link BanishmentRepository}
37
 */
38
final class SqlBanishmentRepository implements BanishmentRepository {
39
    private final QueryExecutor executor;
40
    private final RepositoryUtils<Banishment> utils;
41
    private final Transformer<Instant> instantTransformer;
42
43 1
    public SqlBanishmentRepository(QueryExecutor executor, Transformer<Instant> instantTransformer) {
44 1
        this.executor = executor;
45 1
        this.instantTransformer = instantTransformer;
46 1
        this.utils = new RepositoryUtils<>(this.executor, new Loader());
47 1
    }
48
49
    @Override
50
    public void initialize() throws RepositoryException {
51
        try {
52 1
            executor.query(
53
                "CREATE TABLE BANISHMENT (" +
54
                    "BANISHMENT_ID INTEGER PRIMARY KEY AUTOINCREMENT," +
55
                    "ACCOUNT_ID INTEGER," +
56
                    "START_DATE DATETIME," +
57
                    "END_DATE DATETIME," +
58
                    "CAUSE VARCHAR(255)," +
59
                    "BANISHER_ID INTEGER" +
60
                ")"
61
            );
62 1
            executor.query("CREATE INDEX IDX_IS_BANISHMENT ON BANISHMENT (ACCOUNT_ID, START_DATE, END_DATE)");
63
        } catch (SQLException e) {
64
            throw new RepositoryException(e);
65 1
        }
66 1
    }
67
68
    @Override
69
    public void destroy() throws RepositoryException {
70
        try {
71 1
            executor.query("DROP TABLE BANISHMENT");
72
        } catch (SQLException e) {
73
            throw new RepositoryException(e);
74 1
        }
75 1
    }
76
77
    @Override
78
    public Banishment add(Banishment entity) {
79 1
        return utils.update(
80
            "INSERT INTO BANISHMENT (`ACCOUNT_ID`, `START_DATE`, `END_DATE`, `CAUSE`, `BANISHER_ID`) VALUES (?, ?, ?, ?, ?)",
81
            rs -> {
82 1
                rs.setInt(1, entity.accountId());
83 1
                rs.setString(2, instantTransformer.serialize(entity.startDate()));
84 1
                rs.setString(3, instantTransformer.serialize(entity.endDate()));
85 1
                rs.setString(4, entity.cause());
86 1
                rs.setInt(5, entity.banisherId());
87 1
            },
88
            entity
89
        );
90
    }
91
92
    @Override
93
    public void delete(Banishment entity) {
94 1
        utils.update("DELETE FROM BANISHMENT WHERE BANISHMENT_ID = ?", stmt -> stmt.setInt(1, entity.id()));
95 1
    }
96
97
    @Override
98
    public Banishment get(Banishment entity) throws RepositoryException {
99 1
        return utils.findOne(
100
            "SELECT * FROM BANISHMENT WHERE BANISHMENT_ID = ?",
101 1
            stmt -> stmt.setInt(1, entity.id())
102
        );
103
    }
104
105
    @Override
106
    public boolean has(Banishment entity) {
107 1
        return utils.aggregate(
108
            "SELECT COUNT(*) FROM BANISHMENT WHERE BANISHMENT_ID = ?",
109 1
            stmt -> stmt.setInt(1, entity.id())
110
        ) > 0;
111
    }
112
113
    @Override
114
    public boolean isBanned(int accountId) {
115 1
        final String date = instantTransformer.serialize(Instant.now());
116
117 1
        return utils.aggregate(
118
            "SELECT COUNT(*) FROM BANISHMENT WHERE ACCOUNT_ID = ? AND START_DATE <= ? AND END_DATE >= ?",
119
            stmt -> {
120 1
                stmt.setInt(1, accountId);
121 1
                stmt.setString(2, date);
122 1
                stmt.setString(3, date);
123 1
            }
124
        ) > 0;
125
    }
126
127
    @Override
128
    public List<Banishment> forAccount(int accountId) {
129 1
        return utils.findAll(
130
            "SELECT * FROM BANISHMENT WHERE ACCOUNT_ID = ? ORDER BY START_DATE DESC",
131 1
            stmt -> stmt.setInt(1, accountId)
132
        );
133
    }
134
135
    @Override
136
    public void removeActive(int accountId) {
137 1
        final String date = instantTransformer.serialize(Instant.now());
138
139 1
        utils.update(
140
            "DELETE FROM BANISHMENT WHERE ACCOUNT_ID = ? AND START_DATE <= ? AND END_DATE >= ?",
141
            stmt -> {
142 1
                stmt.setInt(1, accountId);
143 1
                stmt.setString(2, date);
144 1
                stmt.setString(3, date);
145 1
            }
146
        );
147 1
    }
148
149 1
    private class Loader implements RepositoryUtils.Loader<Banishment> {
0 ignored issues
show
Comprehensibility introduced by
Class or interface names should not shadow other classes or interfaces. In general, shadowing is a bad practice as it makes code harder to understand. Consider renaming this class.
Loading history...
150
        @Override
151
        public Banishment create(Record record) throws SQLException {
152 1
            return new Banishment(
153 1
                record.getInt("BANISHMENT_ID"),
154 1
                record.getInt("ACCOUNT_ID"),
155 1
                record.unserialize("START_DATE", instantTransformer),
156 1
                record.unserialize("END_DATE", instantTransformer),
157 1
                record.getString("CAUSE"),
158 1
                record.getInt("BANISHER_ID")
159
            );
160
        }
161
162
        @Override
163
        public Banishment fillKeys(Banishment entity, ResultSet keys) throws SQLException {
164 1
            return entity.withId(keys.getInt(1));
165
        }
166
    }
167
}
168