byBank(AccountBank)   A
last analyzed

Complexity

Conditions 1

Size

Total Lines 7
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 7
dl 0
loc 7
ccs 3
cts 3
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-2019 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.EntityNotFoundException;
24
import fr.quatrevieux.araknemu.core.dbal.repository.Record;
25
import fr.quatrevieux.araknemu.core.dbal.repository.RepositoryException;
26
import fr.quatrevieux.araknemu.core.dbal.repository.RepositoryUtils;
27
import fr.quatrevieux.araknemu.data.living.entity.account.AccountBank;
28
import fr.quatrevieux.araknemu.data.living.entity.account.BankItem;
29
import fr.quatrevieux.araknemu.data.living.repository.account.BankItemRepository;
30
import fr.quatrevieux.araknemu.data.transformer.Transformer;
31
import fr.quatrevieux.araknemu.data.value.ItemTemplateEffectEntry;
32
import fr.quatrevieux.araknemu.util.Asserter;
33
import org.checkerframework.checker.index.qual.NonNegative;
34
35
import java.sql.ResultSet;
36
import java.sql.SQLException;
37
import java.util.Collection;
38
import java.util.List;
39
40
/**
41
 * SQL implementation for {@link BankItem} repository
42
 */
43
final class SqlBankItemRepository implements BankItemRepository {
44
    private final QueryExecutor executor;
45
    private final RepositoryUtils<BankItem> utils;
46
    private final Transformer<List<ItemTemplateEffectEntry>> effectsTransformer;
47
48 1
    public SqlBankItemRepository(QueryExecutor executor, Transformer<List<ItemTemplateEffectEntry>> effectsTransformer) {
49 1
        this.executor = executor;
50 1
        this.effectsTransformer = effectsTransformer;
51 1
        this.utils = new RepositoryUtils<>(this.executor, new Loader());
52 1
    }
53
54
    @Override
55
    public void initialize() throws RepositoryException {
56
        try {
57 1
            executor.query(
58
                "CREATE TABLE BANK_ITEM (" +
59
                    "ACCOUNT_ID INTEGER," +
60
                    "SERVER_ID INTEGER," +
61
                    "ITEM_ENTRY_ID INTEGER," +
62
                    "ITEM_TEMPLATE_ID INTEGER," +
63
                    "ITEM_EFFECTS TEXT," +
64
                    "QUANTITY SMALLINT," +
65
                    "PRIMARY KEY (ACCOUNT_ID, SERVER_ID, ITEM_ENTRY_ID)" +
66
                ")"
67
            );
68
        } catch (SQLException e) {
69
            throw new RepositoryException(e);
70 1
        }
71 1
    }
72
73
    @Override
74
    public void destroy() throws RepositoryException {
75
        try {
76 1
            executor.query("DROP TABLE BANK_ITEM");
77
        } catch (SQLException e) {
78
            throw new RepositoryException(e);
79 1
        }
80 1
    }
81
82
    @Override
83
    public void update(BankItem item) {
84 1
        final int count = utils.update(
85
            "UPDATE BANK_ITEM SET QUANTITY = ? WHERE ACCOUNT_ID = ? AND SERVER_ID = ? AND ITEM_ENTRY_ID = ?",
86
            stmt -> {
87 1
                stmt.setInt(1, item.quantity());
88 1
                stmt.setInt(2, item.accountId());
89 1
                stmt.setInt(3, item.serverId());
90 1
                stmt.setInt(4, item.entryId());
91 1
            }
92
        );
93
94 1
        if (count != 1) {
95 1
            throw new EntityNotFoundException();
96
        }
97 1
    }
98
99
    @Override
100
    public void delete(BankItem item) {
101 1
        final int count = utils.update(
102
            "DELETE FROM BANK_ITEM WHERE ACCOUNT_ID = ? AND SERVER_ID = ? AND ITEM_ENTRY_ID = ?",
103
            stmt -> {
104 1
                stmt.setInt(1, item.accountId());
105 1
                stmt.setInt(2, item.serverId());
106 1
                stmt.setInt(3, item.entryId());
107 1
            }
108
        );
109
110 1
        if (count != 1) {
111 1
            throw new EntityNotFoundException();
112
        }
113 1
    }
114
115
    @Override
116
    public BankItem add(BankItem entity) throws RepositoryException {
117 1
        utils.update(
118
            "INSERT INTO BANK_ITEM (ACCOUNT_ID, SERVER_ID, ITEM_ENTRY_ID, ITEM_TEMPLATE_ID, ITEM_EFFECTS, QUANTITY) VALUES (?, ?, ?, ?, ?, ?)",
119
            stmt -> {
120 1
                stmt.setInt(1,    entity.accountId());
121 1
                stmt.setInt(2,    entity.serverId());
122 1
                stmt.setInt(3,    entity.entryId());
123 1
                stmt.setInt(4,    entity.itemTemplateId());
124 1
                stmt.setString(5, effectsTransformer.serialize(entity.effects()));
125 1
                stmt.setInt(6,    entity.quantity());
126 1
            }
127
        );
128
129 1
        return entity;
130
    }
131
132
    @Override
133
    public BankItem get(BankItem entity) throws RepositoryException {
134 1
        return utils.findOne(
135
            "SELECT * FROM BANK_ITEM WHERE ACCOUNT_ID = ? AND SERVER_ID = ? AND ITEM_ENTRY_ID = ?",
136
            stmt -> {
137 1
                stmt.setInt(1, entity.accountId());
138 1
                stmt.setInt(2, entity.serverId());
139 1
                stmt.setInt(3, entity.entryId());
140 1
            }
141
        );
142
    }
143
144
    @Override
145
    public boolean has(BankItem entity) throws RepositoryException {
146 1
        return utils.aggregate(
147
            "SELECT COUNT(*) FROM BANK_ITEM WHERE ACCOUNT_ID = ? AND SERVER_ID = ? AND ITEM_ENTRY_ID = ?",
148
            stmt -> {
149 1
                stmt.setInt(1, entity.accountId());
150 1
                stmt.setInt(2, entity.serverId());
151 1
                stmt.setInt(3, entity.entryId());
152 1
            }
153
        ) > 0;
154
    }
155
156
    @Override
157
    public Collection<BankItem> byBank(AccountBank bank) {
158 1
        return utils.findAll(
159
            "SELECT * FROM BANK_ITEM WHERE ACCOUNT_ID = ? AND SERVER_ID = ?",
160
            stmt -> {
161 1
                stmt.setInt(1, bank.accountId());
162 1
                stmt.setInt(2, bank.serverId());
163 1
            }
164
        );
165
    }
166
167
    @Override
168
    public @NonNegative int count(AccountBank bank) throws RepositoryException {
169 1
        return Asserter.castNonNegative(utils.aggregate(
170
            "SELECT COUNT(*) FROM BANK_ITEM WHERE ACCOUNT_ID = ? AND SERVER_ID = ?",
171
            stmt -> {
172 1
                stmt.setInt(1, bank.accountId());
173 1
                stmt.setInt(2, bank.serverId());
174 1
            }
175
        ));
176
    }
177
178 1
    private class Loader implements RepositoryUtils.Loader<BankItem> {
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...
179
        @Override
180
        public BankItem create(Record record) throws SQLException {
181 1
            return new BankItem(
182 1
                record.getInt("ACCOUNT_ID"),
183 1
                record.getInt("SERVER_ID"),
184 1
                record.getInt("ITEM_ENTRY_ID"),
185 1
                record.getInt("ITEM_TEMPLATE_ID"),
186 1
                record.unserialize("ITEM_EFFECTS", effectsTransformer),
187 1
                record.getNonNegativeInt("QUANTITY")
188
            );
189
        }
190
191
        @Override
192
        public BankItem fillKeys(BankItem entity, ResultSet keys) {
193
            throw new UnsupportedOperationException();
194
        }
195
    }
196
}
197