Passed
Branch master (6b380a)
by Vincent
13:11
created

create(Record)   A

Complexity

Conditions 1

Size

Total Lines 13
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 12
dl 0
loc 13
ccs 8
cts 8
cp 1
crap 1
rs 9.8
c 0
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.world.repository.implementation.sql;
21
22
import fr.arakne.utils.value.Interval;
23
import fr.quatrevieux.araknemu.core.dbal.executor.QueryExecutor;
24
import fr.quatrevieux.araknemu.core.dbal.repository.EntityNotFoundException;
25
import fr.quatrevieux.araknemu.core.dbal.repository.Record;
26
import fr.quatrevieux.araknemu.core.dbal.repository.RepositoryException;
27
import fr.quatrevieux.araknemu.core.dbal.repository.RepositoryUtils;
28
import fr.quatrevieux.araknemu.data.world.entity.monster.MonsterRewardData;
29
import fr.quatrevieux.araknemu.data.world.repository.monster.MonsterRewardRepository;
30
import fr.quatrevieux.araknemu.util.Asserter;
31
32
import java.sql.ResultSet;
33
import java.sql.SQLException;
34
import java.util.Arrays;
35
import java.util.List;
36
import java.util.Optional;
37
38
/**
39
 * SQL implementation for monster reward repository
40
 */
41
final class SqlMonsterRewardRepository implements MonsterRewardRepository {
42
    private final QueryExecutor executor;
43
    private final RepositoryUtils<MonsterRewardData> utils;
44
45 1
    public SqlMonsterRewardRepository(QueryExecutor executor) {
46 1
        this.executor = executor;
47
48 1
        utils = new RepositoryUtils<>(this.executor, new SqlMonsterRewardRepository.Loader());
49 1
    }
50
51
    @Override
52
    public void initialize() throws RepositoryException {
53
        try {
54 1
            executor.query(
55
                "CREATE TABLE `MONSTER_REWARD` (" +
56
                    "`MONSTER_ID` INTEGER PRIMARY KEY," +
57
                    "`MIN_KAMAS` INTEGER," +
58
                    "`MAX_KAMAS` INTEGER," +
59
                    "`EXPERIENCES` VARCHAR(200)" +
60
                ")"
61
            );
62
        } catch (SQLException e) {
63
            throw new RepositoryException(e);
64 1
        }
65 1
    }
66
67
    @Override
68
    public void destroy() throws RepositoryException {
69
        try {
70 1
            executor.query("DROP TABLE MONSTER_REWARD");
71
        } catch (SQLException e) {
72
            throw new RepositoryException(e);
73 1
        }
74 1
    }
75
76
    @Override
77
    public MonsterRewardData get(MonsterRewardData entity) throws RepositoryException {
78 1
        return get(entity.id()).orElseThrow(EntityNotFoundException::new);
79
    }
80
81
    @Override
82
    public Optional<MonsterRewardData> get(int id) {
83
        try {
84 1
            return Optional.of(utils.findOne(
85
                "SELECT * FROM MONSTER_REWARD WHERE MONSTER_ID = ?",
86 1
                stmt -> stmt.setInt(1, id)
87
            ));
88 1
        } catch (EntityNotFoundException e) {
89 1
            return Optional.empty();
90
        }
91
    }
92
93
    @Override
94
    public boolean has(MonsterRewardData entity) throws RepositoryException {
95 1
        return utils.aggregate(
96
            "SELECT COUNT(*) FROM MONSTER_REWARD WHERE MONSTER_ID = ?",
97 1
            stmt -> stmt.setInt(1, entity.id())
98
        ) > 0;
99
    }
100
101
    @Override
102
    public List<MonsterRewardData> all() {
103 1
        return utils.findAll("SELECT * FROM MONSTER_REWARD");
104
    }
105
106 1
    private class Loader implements RepositoryUtils.Loader<MonsterRewardData> {
107
        @Override
108
        @SuppressWarnings("argument") // LongStream do not handle @NonNegative parameter
109
        public MonsterRewardData create(Record record) throws SQLException {
110 1
            return new MonsterRewardData(
111 1
                record.getInt("MONSTER_ID"),
112
                new Interval(
113 1
                    record.getNonNegativeInt("MIN_KAMAS"),
114 1
                    record.getNonNegativeInt("MAX_KAMAS")
115
                ),
116 1
                Arrays.stream(record.getCsvArray("EXPERIENCES", '|'))
117 1
                    .mapToLong(Long::parseLong)
118 1
                    .map(Asserter::assertNonNegative)
119 1
                    .toArray()
120
            );
121
        }
122
123
        @Override
124
        public MonsterRewardData fillKeys(MonsterRewardData entity, ResultSet keys) {
125
            throw new RepositoryException("Read-only entity");
126
        }
127
    }
128
}
129