forked from pgjdbc/r2dbc-postgresql
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCommandComplete.java
148 lines (123 loc) · 4.48 KB
/
CommandComplete.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
/*
* 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.message.backend;
import io.netty.buffer.ByteBuf;
import io.r2dbc.postgresql.util.Assert;
import reactor.util.annotation.Nullable;
import java.util.Objects;
import static io.r2dbc.postgresql.message.backend.BackendMessageUtils.readCStringUTF8;
/**
* The CommandComplete message.
*/
public final class CommandComplete implements BackendMessage {
private final static String[] NO_ROW_ID_TAGS = {"SELECT", "UPDATE", "DELETE", "COPY", "FETCH", "MOVE",};
private final String command;
private final Integer rowId;
private final Long rows;
/**
* Create a new message.
*
* @param command the command that was completed
* @param rowId the object ID of the inserted row
* @param rows the number of rows affected by the command
* @throws IllegalArgumentException if {@code command} is {@code null}
*/
public CommandComplete(String command, @Nullable Integer rowId, @Nullable Long rows) {
this.command = Assert.requireNonNull(command, "command must not be null");
this.rowId = rowId;
this.rows = rows;
}
@Override
public boolean equals(Object o) {
if (this == o) {
return true;
}
if (o == null || getClass() != o.getClass()) {
return false;
}
CommandComplete that = (CommandComplete) o;
return Objects.equals(this.command, that.command) &&
Objects.equals(this.rowId, that.rowId) &&
Objects.equals(this.rows, that.rows);
}
/**
* Returns the command that was completed.
*
* @return the command that was completed
*/
public String getCommand() {
return this.command;
}
/**
* Returns the object ID of the inserted row if a single row is inserted the target table has OIDs. Otherwise is {@code 0}.
*
* @return the object ID of the inserted row
*/
@Nullable
public Integer getRowId() {
return this.rowId;
}
/**
* Returns the number of rows affected by the command.
*
* @return the number of rows affected by the command
*/
@Nullable
public Long getRows() {
return this.rows;
}
@Override
public int hashCode() {
return Objects.hash(this.command, this.rowId, this.rows);
}
@Override
public String toString() {
return "CommandComplete{" +
"command=" + this.command +
", rowId=" + this.rowId +
", rows=" + this.rows +
'}';
}
static CommandComplete decode(ByteBuf in) {
Assert.requireNonNull(in, "in must not be null");
String tag = readCStringUTF8(in);
if (tag.startsWith("INSERT")) {
int index1 = tag.indexOf(' ');
int index2 = tag.indexOf(' ', index1 + 1);
int index3 = tag.indexOf(' ', index2 + 1);
String command = tag.substring(0, index1);
String rowId = tag.substring(index1 + 1, index2);
String rows = tag.substring(index2 + 1, index3 != -1 ? index3 : tag.length());
return new CommandComplete(command, Integer.parseInt(rowId), Long.parseLong(rows));
} else if (isNoRowId(tag)) {
int index1 = tag.indexOf(' ');
int index2 = tag.indexOf(' ', index1 + 1);
String command = tag.substring(0, index1 != -1 ? index1 : tag.length());
String rows = index1 != -1 ? tag.substring(index1 + 1, index2 != -1 ? index2 : tag.length()) : null;
return new CommandComplete(command, null, rows != null ? Long.parseLong(rows) : null);
} else {
return new CommandComplete(tag, null, null);
}
}
private static boolean isNoRowId(String tag) {
for (String noRowIdTag : NO_ROW_ID_TAGS) {
if (tag.startsWith(noRowIdTag)) {
return true;
}
}
return false;
}
}