-
Notifications
You must be signed in to change notification settings - Fork 176
/
PostgresqlRow.java
263 lines (207 loc) · 8.47 KB
/
PostgresqlRow.java
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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
/*
* Copyright 2017 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package io.r2dbc.postgresql;
import io.netty.buffer.ByteBuf;
import io.r2dbc.postgresql.api.PostgresqlResult;
import io.r2dbc.postgresql.api.RefCursor;
import io.r2dbc.postgresql.codec.Codecs;
import io.r2dbc.postgresql.message.backend.DataRow;
import io.r2dbc.postgresql.message.backend.RowDescription;
import io.r2dbc.postgresql.util.Assert;
import io.r2dbc.spi.Row;
import reactor.core.publisher.Mono;
import reactor.util.annotation.Nullable;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.NoSuchElementException;
import java.util.Objects;
/**
* An implementation of {@link Row} for a PostgreSQL database.
*/
final class PostgresqlRow implements io.r2dbc.postgresql.api.PostgresqlRow {
private final ConnectionResources context;
private final io.r2dbc.postgresql.api.PostgresqlRowMetadata metadata;
private final List<RowDescription.Field> fields;
private final ByteBuf[] data;
private volatile boolean isReleased = false;
private Map<String, Integer> columnNameIndexCacheMap;
PostgresqlRow(ConnectionResources context, io.r2dbc.postgresql.api.PostgresqlRowMetadata metadata, List<RowDescription.Field> fields, ByteBuf[] data) {
this.context = Assert.requireNonNull(context, "context must not be null");
this.metadata = Assert.requireNonNull(metadata, "metadata must not be null");
this.fields = Assert.requireNonNull(fields, "fields must not be null");
this.data = Assert.requireNonNull(data, "data must not be null");
}
@Override
public boolean equals(Object o) {
if (this == o) {
return true;
}
if (o == null || getClass() != o.getClass()) {
return false;
}
PostgresqlRow that = (PostgresqlRow) o;
return Objects.equals(this.fields, that.fields);
}
@Nullable
@Override
public <T> T get(int index, Class<T> type) {
Assert.requireNonNull(type, "type must not be null");
requireNotReleased();
return decode(getColumn(index), type);
}
@Nullable
@Override
public <T> T get(String name, Class<T> type) {
Assert.requireNonNull(name, "name must not be null");
Assert.requireNonNull(type, "type must not be null");
requireNotReleased();
return decode(getColumn(name), type);
}
@Override
public io.r2dbc.postgresql.api.PostgresqlRowMetadata getMetadata() {
return this.metadata;
}
@Nullable
private <T> T decode(int index, Class<T> type) {
ByteBuf data = this.data[index];
if (data == null) {
return null;
}
int readerIndex = data.readerIndex();
try {
RowDescription.Field field = this.fields.get(index);
T decoded = this.context.getCodecs().decode(data, field.getDataType(), field.getFormat(), type);
return type.cast(postProcessResult(decoded));
} finally {
data.readerIndex(readerIndex);
}
}
@Nullable
private Object postProcessResult(@Nullable Object decoded) {
if (decoded instanceof RefCursor) {
return createCursor((RefCursor) decoded);
}
return decoded;
}
private AttachedRefCursor createCursor(RefCursor decoded) {
return new AttachedRefCursor(this.context, decoded.getCursorName());
}
@Override
public int hashCode() {
return Objects.hash(this.fields);
}
@Override
public String toString() {
return "PostgresqlRow{" +
"context=" + this.context +
", columns=" + this.fields +
", isReleased=" + this.isReleased +
'}';
}
static Map<String, Integer> createColumnNameIndexMap(List<RowDescription.Field> fields) {
Map<String, Integer> columnNameIndexMap = new HashMap<>(fields.size(), 1);
for (int i = fields.size() - 1; i >= 0; i--) {
columnNameIndexMap.put(fields.get(i).getName().toLowerCase(Locale.ROOT), i);
}
return columnNameIndexMap;
}
static PostgresqlRow toRow(ConnectionResources context, DataRow dataRow, Codecs codecs, RowDescription rowDescription) {
Assert.requireNonNull(dataRow, "dataRow must not be null");
Assert.requireNonNull(codecs, "rowDescription must not be null");
Assert.requireNonNull(rowDescription, "rowDescription must not be null");
return new PostgresqlRow(context, PostgresqlRowMetadata.toRowMetadata(codecs, rowDescription), rowDescription.getFields(), dataRow.getColumns());
}
static PostgresqlRow toRow(ConnectionResources context, DataRow dataRow, PostgresqlRowMetadata metadata, RowDescription rowDescription) {
Assert.requireNonNull(dataRow, "dataRow must not be null");
Assert.requireNonNull(metadata, "metadata must not be null");
Assert.requireNonNull(rowDescription, "rowDescription must not be null");
return new PostgresqlRow(context, metadata, rowDescription.getFields(), dataRow.getColumns());
}
void release() {
for (ByteBuf datum : this.data) {
if (datum != null) {
datum.release();
}
}
this.isReleased = true;
}
private int getColumn(String name) {
if (this.columnNameIndexCacheMap == null) {
this.columnNameIndexCacheMap = createColumnNameIndexMap(this.fields);
}
Integer index = this.columnNameIndexCacheMap.get(name);
if (index != null) {
return index;
}
index = this.columnNameIndexCacheMap.get(name.toLowerCase(Locale.ROOT));
if (index != null) {
this.columnNameIndexCacheMap.put(name, index);
return index;
}
throw new NoSuchElementException(String.format("Column name '%s' does not exist in column names %s", name, toColumnNames()));
}
private List<String> toColumnNames() {
List<String> names = new ArrayList<>(this.fields.size());
for (RowDescription.Field field : this.fields) {
names.add(field.getName());
}
return names;
}
private int getColumn(int index) {
if (index >= this.fields.size()) {
throw new IndexOutOfBoundsException(String.format("Column index %d is larger than the number of columns %d", index, this.fields.size()));
}
return index;
}
private void requireNotReleased() {
if (this.isReleased) {
throw new IllegalStateException("Value cannot be retrieved after row has been released");
}
}
/**
* Default {@link RefCursor} implementation that is attached to a {@link io.r2dbc.postgresql.api.PostgresqlConnection}.
*/
static class AttachedRefCursor implements RefCursor {
private final ConnectionResources context;
private final String portal;
AttachedRefCursor(ConnectionResources context, String portal) {
this.context = Assert.requireNonNull(context, "connection must not be null");
this.portal = Assert.requireNotEmpty(portal, "portal must not be empty");
}
@Override
public String getCursorName() {
return this.portal;
}
@Override
public Mono<io.r2dbc.postgresql.api.PostgresqlResult> fetch() {
return Mono.fromDirect(this.context.getConnection().createStatement("FETCH ALL IN \"" + getCursorName() + "\"").execute());
}
@Override
public Mono<Void> close() {
return this.context.getConnection().createStatement("CLOSE \"" + getCursorName() + "\"").execute().flatMap(PostgresqlResult::getRowsUpdated).then();
}
@Override
public String toString() {
return "AttachedRefCursor{" +
"portal='" + this.portal + '\'' +
", context=" + this.context +
'}';
}
}
}