|
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.core.dbal.executor; |
|
21
|
|
|
|
|
22
|
|
|
import java.sql.PreparedStatement; |
|
23
|
|
|
import java.sql.SQLException; |
|
24
|
|
|
|
|
25
|
|
|
/** |
|
26
|
|
|
* Execute SQL queries |
|
27
|
|
|
*/ |
|
28
|
|
|
public interface QueryExecutor { |
|
29
|
|
|
/** |
|
30
|
|
|
* Prepare SQL query |
|
31
|
|
|
* |
|
32
|
|
|
* executor.prepare("SELECT * FROM ACCOUNT WHERE ACCOUNT_ID = ?", stmt -> { |
|
33
|
|
|
* stmt.setString(1, 123); |
|
34
|
|
|
* ResultSet rs = stmt.executeQuery(); |
|
35
|
|
|
* |
|
36
|
|
|
* return load(rs); |
|
37
|
|
|
* }, false); |
|
38
|
|
|
* |
|
39
|
|
|
* @param sql SQL query to prepare |
|
40
|
|
|
* @param task Task to execute |
|
41
|
|
|
* @param returnGeneratedKeys Set true to return generated keys (like auto increment) |
|
42
|
|
|
* @param <T> The result type |
|
43
|
|
|
* |
|
44
|
|
|
* @return The result of the task |
|
45
|
|
|
* |
|
46
|
|
|
* @throws SQLException When error occurs during execution |
|
47
|
|
|
*/ |
|
48
|
|
|
public <T> T prepare(String sql, PreparedTask<T> task, boolean returnGeneratedKeys) throws SQLException; |
|
49
|
|
|
|
|
50
|
|
|
/** |
|
51
|
|
|
* Prepare SQL query |
|
52
|
|
|
* |
|
53
|
|
|
* executor.prepare("SELECT * FROM ACCOUNT WHERE ACCOUNT_ID = ?", stmt -> { |
|
54
|
|
|
* stmt.setString(1, 123); |
|
55
|
|
|
* ResultSet rs = stmt.executeQuery(); |
|
56
|
|
|
* |
|
57
|
|
|
* return load(rs); |
|
58
|
|
|
* }, false); |
|
59
|
|
|
* |
|
60
|
|
|
* @param sql SQL query to prepare |
|
61
|
|
|
* @param task Task to execute |
|
62
|
|
|
* @param <T> The result type |
|
63
|
|
|
* |
|
64
|
|
|
* @return The result of the task |
|
65
|
|
|
* |
|
66
|
|
|
* @throws SQLException When error occurs during execution |
|
67
|
|
|
*/ |
|
68
|
|
|
public default <T> T prepare(String sql, PreparedTask<T> task) throws SQLException { |
|
69
|
1 |
|
return prepare(sql, task, false); |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
/** |
|
73
|
|
|
* Execute simple SQL query |
|
74
|
|
|
* |
|
75
|
|
|
* @param sql Query to execute |
|
76
|
|
|
* |
|
77
|
|
|
* @throws SQLException When error occurs during execution |
|
78
|
|
|
*/ |
|
79
|
|
|
public void query(String sql) throws SQLException; |
|
80
|
|
|
|
|
81
|
|
|
@FunctionalInterface |
|
82
|
|
|
public static interface PreparedTask<T> { |
|
83
|
|
|
public T execute(PreparedStatement statement) throws SQLException; |
|
84
|
|
|
} |
|
85
|
|
|
} |
|
86
|
|
|
|