|
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.environment.MapTrigger; |
|
27
|
|
|
import fr.quatrevieux.araknemu.data.world.repository.environment.MapTriggerRepository; |
|
28
|
|
|
|
|
29
|
|
|
import java.sql.ResultSet; |
|
30
|
|
|
import java.sql.SQLException; |
|
31
|
|
|
import java.util.Collection; |
|
32
|
|
|
|
|
33
|
|
|
/** |
|
34
|
|
|
* SQL implementation for map triggers |
|
35
|
|
|
*/ |
|
36
|
|
View Code Duplication |
final class SqlMapTriggerRepository implements MapTriggerRepository { |
|
|
|
|
|
|
37
|
|
|
private final QueryExecutor executor; |
|
38
|
|
|
private final RepositoryUtils<MapTrigger> utils; |
|
39
|
|
|
|
|
40
|
1 |
|
public SqlMapTriggerRepository(QueryExecutor executor) { |
|
41
|
1 |
|
this.executor = executor; |
|
42
|
1 |
|
this.utils = new RepositoryUtils<>(this.executor, new Loader()); |
|
43
|
1 |
|
} |
|
44
|
|
|
|
|
45
|
|
|
@Override |
|
46
|
|
|
public void initialize() throws RepositoryException { |
|
47
|
|
|
try { |
|
48
|
1 |
|
executor.query( |
|
49
|
|
|
"CREATE TABLE MAP_TRIGGER (" + |
|
50
|
|
|
"MAP_ID INTEGER," + |
|
51
|
|
|
"CELL_ID INTEGER," + |
|
52
|
|
|
"ACTION INTEGER," + |
|
53
|
|
|
"ARGUMENTS TEXT," + |
|
54
|
|
|
"CONDITIONS TEST," + |
|
55
|
|
|
"PRIMARY KEY (MAP_ID, CELL_ID)" + |
|
56
|
|
|
")" |
|
57
|
|
|
); |
|
58
|
|
|
|
|
59
|
1 |
|
executor.query("CREATE INDEX IDX_MAP ON MAP_TRIGGER (MAP_ID)"); |
|
60
|
|
|
} catch (SQLException e) { |
|
61
|
|
|
throw new RepositoryException(e); |
|
62
|
1 |
|
} |
|
63
|
1 |
|
} |
|
64
|
|
|
|
|
65
|
|
|
@Override |
|
66
|
|
|
public void destroy() throws RepositoryException { |
|
67
|
|
|
try { |
|
68
|
1 |
|
executor.query("DROP TABLE MAP_TRIGGER"); |
|
69
|
|
|
} catch (SQLException e) { |
|
70
|
|
|
throw new RepositoryException(e); |
|
71
|
1 |
|
} |
|
72
|
1 |
|
} |
|
73
|
|
|
|
|
74
|
|
|
@Override |
|
75
|
|
|
public MapTrigger get(MapTrigger entity) throws RepositoryException { |
|
76
|
1 |
|
return utils.findOne( |
|
77
|
|
|
"SELECT * FROM MAP_TRIGGER WHERE MAP_ID = ? AND CELL_ID = ?", |
|
78
|
|
|
stmt -> { |
|
79
|
1 |
|
stmt.setInt(1, entity.map()); |
|
80
|
1 |
|
stmt.setInt(2, entity.cell()); |
|
81
|
1 |
|
} |
|
82
|
|
|
); |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
|
|
@Override |
|
86
|
|
|
public boolean has(MapTrigger entity) throws RepositoryException { |
|
87
|
1 |
|
return utils.aggregate( |
|
88
|
|
|
"SELECT COUNT(*) FROM MAP_TRIGGER WHERE MAP_ID = ? AND CELL_ID = ?", |
|
89
|
|
|
stmt -> { |
|
90
|
1 |
|
stmt.setInt(1, entity.map()); |
|
91
|
1 |
|
stmt.setInt(2, entity.cell()); |
|
92
|
1 |
|
} |
|
93
|
|
|
) > 0; |
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
|
|
@Override |
|
97
|
|
|
public Collection<MapTrigger> findByMap(int map) { |
|
98
|
1 |
|
return utils.findAll( |
|
99
|
|
|
"SELECT * FROM MAP_TRIGGER WHERE MAP_ID = ?", |
|
100
|
1 |
|
stmt -> stmt.setInt(1, map) |
|
101
|
|
|
); |
|
102
|
|
|
} |
|
103
|
|
|
|
|
104
|
|
|
@Override |
|
105
|
|
|
public Collection<MapTrigger> all() { |
|
106
|
1 |
|
return utils.findAll("SELECT * FROM MAP_TRIGGER"); |
|
107
|
|
|
} |
|
108
|
|
|
|
|
109
|
|
|
private static class Loader implements RepositoryUtils.Loader<MapTrigger> { |
|
|
|
|
|
|
110
|
|
|
@Override |
|
111
|
|
|
public MapTrigger create(Record record) throws SQLException { |
|
112
|
1 |
|
return new MapTrigger( |
|
113
|
1 |
|
record.getInt("MAP_ID"), |
|
114
|
1 |
|
record.getNonNegativeInt("CELL_ID"), |
|
115
|
1 |
|
record.getInt("ACTION"), |
|
116
|
1 |
|
record.getString("ARGUMENTS"), |
|
117
|
1 |
|
record.getString("CONDITIONS") |
|
118
|
|
|
); |
|
119
|
|
|
} |
|
120
|
|
|
|
|
121
|
|
|
@Override |
|
122
|
|
|
public MapTrigger fillKeys(MapTrigger entity, ResultSet keys) { |
|
123
|
|
|
throw new RepositoryException("Read-only entity"); |
|
124
|
|
|
} |
|
125
|
|
|
} |
|
126
|
|
|
} |
|
127
|
|
|
|