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.maps.constant.Direction; |
23
|
|
|
import fr.quatrevieux.araknemu.core.dbal.executor.QueryExecutor; |
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.value.Position; |
28
|
|
|
import fr.quatrevieux.araknemu.data.world.entity.environment.npc.Npc; |
29
|
|
|
import fr.quatrevieux.araknemu.data.world.repository.environment.npc.NpcRepository; |
30
|
|
|
|
31
|
|
|
import java.sql.ResultSet; |
32
|
|
|
import java.sql.SQLException; |
33
|
|
|
import java.util.Collection; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* Npc repository implementation for SQL database |
37
|
|
|
* |
38
|
|
|
* @see Npc |
39
|
|
|
*/ |
40
|
|
|
final class SqlNpcRepository implements NpcRepository { |
41
|
|
|
private final QueryExecutor executor; |
42
|
|
|
private final RepositoryUtils<Npc> utils; |
43
|
|
|
|
44
|
1 |
|
public SqlNpcRepository(QueryExecutor executor) { |
45
|
1 |
|
this.executor = executor; |
46
|
|
|
|
47
|
1 |
|
utils = new RepositoryUtils<>(this.executor, new Loader()); |
48
|
1 |
|
} |
49
|
|
|
|
50
|
|
|
@Override |
51
|
|
|
public void initialize() throws RepositoryException { |
52
|
|
|
try { |
53
|
1 |
|
executor.query( |
54
|
|
|
"CREATE TABLE NPC(" + |
55
|
|
|
"NPC_ID INTEGER PRIMARY KEY AUTOINCREMENT," + |
56
|
|
|
"NPC_TEMPLATE_ID INTEGER," + |
57
|
|
|
"MAP_ID INTEGER," + |
58
|
|
|
"CELL_ID INTEGER," + |
59
|
|
|
"ORIENTATION TINYINT(1)," + |
60
|
|
|
"QUESTIONS VARCHAR(32)" + |
61
|
|
|
")" |
62
|
|
|
); |
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 NPC"); |
72
|
|
|
} catch (SQLException e) { |
73
|
|
|
throw new RepositoryException(e); |
74
|
1 |
|
} |
75
|
1 |
|
} |
76
|
|
|
|
77
|
|
|
@Override |
78
|
|
|
public Npc get(int id) { |
79
|
1 |
|
return utils.findOne( |
80
|
|
|
"SELECT * FROM NPC WHERE NPC_ID = ?", |
81
|
1 |
|
stmt -> stmt.setInt(1, id) |
82
|
|
|
); |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
@Override |
86
|
|
|
public Npc get(Npc entity) throws RepositoryException { |
87
|
1 |
|
return get(entity.id()); |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
@Override |
91
|
|
|
public boolean has(Npc entity) throws RepositoryException { |
92
|
1 |
|
return utils.aggregate( |
93
|
|
|
"SELECT COUNT(*) FROM NPC WHERE NPC_ID = ?", |
94
|
1 |
|
stmt -> stmt.setInt(1, entity.id()) |
95
|
|
|
) > 0; |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
@Override |
99
|
|
|
public Collection<Npc> all() throws RepositoryException { |
100
|
1 |
|
return utils.findAll("SELECT * FROM NPC"); |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
@Override |
104
|
|
|
public Collection<Npc> byMapId(int mapId) { |
105
|
1 |
|
return utils.findAll( |
106
|
|
|
"SELECT * FROM NPC WHERE MAP_ID = ?", |
107
|
1 |
|
rs -> rs.setInt(1, mapId) |
108
|
|
|
); |
109
|
|
|
} |
110
|
|
|
|
111
|
1 |
|
private static class Loader implements RepositoryUtils.Loader<Npc> { |
|
|
|
|
112
|
1 |
|
private final Direction[] directions = Direction.values(); |
113
|
|
|
|
114
|
|
|
@Override |
115
|
|
|
public Npc create(Record record) throws SQLException { |
116
|
1 |
|
return new Npc( |
117
|
1 |
|
record.getInt("NPC_ID"), |
118
|
1 |
|
record.getInt("NPC_TEMPLATE_ID"), |
119
|
|
|
new Position( |
120
|
1 |
|
record.getNonNegativeInt("MAP_ID"), |
121
|
1 |
|
record.getNonNegativeInt("CELL_ID") |
122
|
|
|
), |
123
|
1 |
|
record.getArrayValue("ORIENTATION", directions), |
124
|
1 |
|
record.getIntArray("QUESTIONS", ';') |
125
|
|
|
); |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
@Override |
129
|
|
|
public Npc fillKeys(Npc entity, ResultSet keys) { |
130
|
|
|
throw new RepositoryException("Read-only entity"); |
131
|
|
|
} |
132
|
|
|
} |
133
|
|
|
} |
134
|
|
|
|