fr.quatrevieux.araknemu.data.world.repository.implementation.sql.SqlMonsterRewardItemRepository   A
last analyzed

Complexity

Total Complexity 9

Size/Duplication

Total Lines 78
Duplicated Lines 0 %

Test Coverage

Coverage 82.76%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 54
dl 0
loc 78
c 1
b 0
f 0
ccs 24
cts 29
cp 0.8276
rs 10
wmc 9

9 Methods

Rating   Name   Duplication   Size   Complexity  
A Loader.create(Record) 0 8 1
A all() 0 5 1
A get(MonsterRewardItem) 0 3 1
A destroy() 0 6 2
A byMonster(int) 0 5 1
A Loader.fillKeys(MonsterRewardItem,ResultSet) 0 3 1
A SqlMonsterRewardItemRepository(QueryExecutor) 0 4 1
A has(MonsterRewardItem) 0 3 1
A initialize() 0 15 2
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.world.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.world.entity.monster.MonsterRewardItem;
27
import fr.quatrevieux.araknemu.data.world.repository.monster.MonsterRewardItemRepository;
28
29
import java.sql.ResultSet;
30
import java.sql.SQLException;
31
import java.util.List;
32
import java.util.Map;
33
import java.util.stream.Collectors;
34
35
/**
36
 * SQL implementation for monster reward item repository
37
 */
38
final class SqlMonsterRewardItemRepository implements MonsterRewardItemRepository {
39
    private final QueryExecutor executor;
40
    private final RepositoryUtils<MonsterRewardItem> utils;
41
42 1
    public SqlMonsterRewardItemRepository(QueryExecutor executor) {
43 1
        this.executor = executor;
44
45 1
        utils = new RepositoryUtils<>(this.executor, new SqlMonsterRewardItemRepository.Loader());
46 1
    }
47
48
    @Override
49
    public void initialize() throws RepositoryException {
50
        try {
51 1
            executor.query(
52
                "CREATE TABLE `MONSTER_REWARD_ITEM` (" +
53
                    "`MONSTER_ID` INTEGER," +
54
                    "`ITEM_TEMPLATE_ID` INTEGER," +
55
                    "`QUANTITY` INTEGER," +
56
                    "`DISCERNMENT` INTEGER," +
57
                    "`RATE` INTEGER," +
58
                    "PRIMARY KEY (`MONSTER_ID`, `ITEM_TEMPLATE_ID`)" +
59
                ")"
60
            );
61
        } catch (SQLException e) {
62
            throw new RepositoryException(e);
63 1
        }
64 1
    }
65
66
    @Override
67
    public void destroy() throws RepositoryException {
68
        try {
69 1
            executor.query("DROP TABLE MONSTER_REWARD_ITEM");
70
        } catch (SQLException e) {
71
            throw new RepositoryException(e);
72 1
        }
73 1
    }
74
75
    @Override
76
    public MonsterRewardItem get(MonsterRewardItem entity) throws RepositoryException {
77 1
        throw new UnsupportedOperationException("No implemented");
78
    }
79
80
    @Override
81
    public boolean has(MonsterRewardItem entity) throws RepositoryException {
82 1
        throw new UnsupportedOperationException("No implemented");
83
    }
84
85
    @Override
86
    public List<MonsterRewardItem> byMonster(int monsterId) {
87 1
        return utils.findAll(
88
            "SELECT * FROM MONSTER_REWARD_ITEM WHERE MONSTER_ID = ?",
89 1
            stmt -> stmt.setInt(1, monsterId)
90
        );
91
    }
92
93
    @Override
94
    public Map<Integer, List<MonsterRewardItem>> all() {
95 1
        return utils.findAll("SELECT * FROM MONSTER_REWARD_ITEM")
96 1
            .stream()
97 1
            .collect(Collectors.groupingBy(MonsterRewardItem::monsterId))
98
        ;
99
    }
100
101 1
    private class Loader implements RepositoryUtils.Loader<MonsterRewardItem> {
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...
102
        @Override
103
        public MonsterRewardItem create(Record record) throws SQLException {
104 1
            return new MonsterRewardItem(
105 1
                record.getInt("MONSTER_ID"),
106 1
                record.getInt("ITEM_TEMPLATE_ID"),
107 1
                record.getPositiveInt("QUANTITY"),
108 1
                record.getNonNegativeInt("DISCERNMENT"),
109 1
                record.getDouble("RATE")
110
            );
111
        }
112
113
        @Override
114
        public MonsterRewardItem fillKeys(MonsterRewardItem entity, ResultSet keys) {
115
            throw new RepositoryException("Read-only entity");
116
        }
117
    }
118
}
119