-
Notifications
You must be signed in to change notification settings - Fork 31
Expand file tree
/
Copy pathContext.java
More file actions
132 lines (101 loc) · 2.87 KB
/
Context.java
File metadata and controls
132 lines (101 loc) · 2.87 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
package com.github.jonathanhds.sqlbuilder;
import com.github.jonathanhds.sqlbuilder.select.RowMapper;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Arrays;
import java.util.LinkedList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
public class Context {
private static final String NEW_LINE = System.getProperty("line.separator");
private final Connection connection;
private final StringBuilder sql;
private final List<Object> parameters;
private final Database database;
private transient final Logger log;
{
this.log = Logger.getLogger(getClass().getName());
}
public Context(Context clone) {
this.connection = clone.connection;
this.parameters = new LinkedList<Object>(clone.parameters);
this.sql = new StringBuilder();
this.database = clone.database;
}
public Context(Database database, Connection connection) {
this.connection = connection;
this.database = database;
sql = new StringBuilder();
parameters = new LinkedList<>();
}
public Context(Database database) {
this.database = database;
this.connection = null;
this.sql = new StringBuilder();
this.parameters = new LinkedList<>();
}
@Override
public String toString() {
return sql.toString();
}
public <E> List<E> list(RowMapper<E> rowMapper) throws SQLException {
List<E> result = new LinkedList<>();
try (ResultSet resultSet = execute(toString())) {
int rowNum = 0;
while (resultSet.next()) {
E obj = rowMapper.convert(resultSet, rowNum++);
result.add(obj);
}
}
return result;
}
public <E> E single(RowMapper<E> rowMapper) throws SQLException {
E result = null;
try (ResultSet resultSet = execute(toString())) {
if (resultSet.next()) {
result = rowMapper.convert(resultSet, 1);
if (resultSet.next()) {
throw new SQLException("The query returned more than one result");
}
}
}
return result;
}
public Context append(String expression) {
sql.append(expression);
return this;
}
public Context appendLine(String expression) {
sql.append(expression);
sql.append(NEW_LINE);
return this;
}
public Context newLine() {
sql.append(NEW_LINE);
return this;
}
public void addParameters(Object... parameters) {
this.parameters.addAll(Arrays.asList(parameters));
}
public Database getDatabase() {
return database;
}
private ResultSet execute(String sql) throws SQLException {
log.log(Level.INFO, "\n" + sql);
try (PreparedStatement statement = prepareStatement(sql)) {
return statement.executeQuery();
}
}
private PreparedStatement prepareStatement(String sql) throws SQLException {
try (PreparedStatement statement = connection.prepareStatement(sql)) {
int i = 1;
for (Object parameter : parameters) {
statement.setObject(i++, parameter);
}
return statement;
}
}
}