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> { |
|
|
|
|
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
|
|
|
|