Skip to content

Commit 2512765

Browse files
committed
[#9614] Apply temporary fix to Pinot #9614 issue
1 parent 0a049cc commit 2512765

File tree

1 file changed

+115
-1
lines changed

1 file changed

+115
-1
lines changed

commons-pinot/src/main/java/com/navercorp/pinpoint/common/pinot/datasource/WrappedPinotPreparedStatement.java

Lines changed: 115 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,30 +16,144 @@
1616

1717
package com.navercorp.pinpoint.common.pinot.datasource;
1818

19+
import org.apache.commons.codec.binary.Hex;
1920
import org.apache.pinot.client.PinotConnection;
2021
import org.apache.pinot.client.PinotPreparedStatement;
22+
import org.apache.pinot.client.utils.DateTimeUtils;
2123

24+
import java.math.BigDecimal;
2225
import java.sql.ResultSet;
2326
import java.sql.SQLException;
27+
import java.sql.Time;
28+
import java.sql.Timestamp;
29+
import java.sql.Date;
2430

2531
/**
2632
* @author Hyunjoon Cho
2733
*/
2834
public class WrappedPinotPreparedStatement extends PinotPreparedStatement {
35+
// Temporary variables for fillStatementWithParameters(). TODO: remove these when Pinot driver is fixed.
36+
private final String query;
37+
private final String[] parameters;
2938

3039
public WrappedPinotPreparedStatement(PinotConnection connection, String query) {
3140
super(connection, query);
41+
this.query = query;
42+
this.parameters = new String[this.getQuestionMarkCount(query)];
3243
}
3344

45+
/* copied from org.apache.pinot.client.PreparedStatement. TODO: remove when Pinot driver is fixed. */
46+
private int getQuestionMarkCount(String query) {
47+
int questionMarkCount = 0;
48+
49+
for(int index = query.indexOf(63); index != -1; index = query.indexOf(63, index + 1)) {
50+
++questionMarkCount;
51+
}
52+
53+
return questionMarkCount;
54+
}
55+
56+
3457
@Override
3558
public boolean execute() throws SQLException {
3659
// WARNING Do not invoke close().
3760
// Ignore spotbug warning
38-
ResultSet resultSet = executeQuery();
61+
ResultSet resultSet = executeQuery(fillStatementWithParameters());
3962
if (resultSet.isLast()) {
4063
return false;
4164
} else {
4265
return true;
4366
}
4467
}
68+
69+
/* temporary function. TODO: remove these when Pinot driver is fixed */
70+
private String fillStatementWithParameters() {
71+
String[] queryParts = query.split("\\?");
72+
StringBuilder sb = new StringBuilder();
73+
int i = 0;
74+
while (i < parameters.length) {
75+
sb.append(queryParts[i]);
76+
sb.append(parameters[i] != null ? parameters[i] : "");
77+
i++;
78+
}
79+
80+
while (i < queryParts.length) {
81+
sb.append(queryParts[i]);
82+
i++;
83+
}
84+
85+
return sb.toString();
86+
}
87+
88+
/* temporary setter functions to redirect mybatis parameter setting to temporary variables this.query and this.parameters TODO: remove these when Pinot driver is fixed */
89+
public void setNull(int parameterIndex, int sqlType) throws SQLException {
90+
this.validateState();
91+
this.parameters[parameterIndex - 1] = "'NULL'";
92+
}
93+
94+
public void setBoolean(int parameterIndex, boolean x) throws SQLException {
95+
this.validateState();
96+
this.parameters[parameterIndex - 1] = String.valueOf(x);
97+
}
98+
99+
public void setShort(int parameterIndex, short x) throws SQLException {
100+
this.validateState();
101+
this.parameters[parameterIndex - 1] = String.valueOf(x);
102+
}
103+
104+
public void setInt(int parameterIndex, int x) throws SQLException {
105+
this.validateState();
106+
this.parameters[parameterIndex - 1] = String.valueOf(x);
107+
}
108+
109+
public void setLong(int parameterIndex, long x) throws SQLException {
110+
this.validateState();
111+
this.parameters[parameterIndex - 1] = String.valueOf(x);
112+
}
113+
114+
public void setFloat(int parameterIndex, float x) throws SQLException {
115+
this.validateState();
116+
this.parameters[parameterIndex - 1] = String.valueOf(x);
117+
}
118+
119+
public void setDouble(int parameterIndex, double x) throws SQLException {
120+
this.validateState();
121+
this.parameters[parameterIndex - 1] = String.valueOf(x);
122+
}
123+
124+
public void setString(int parameterIndex, String x) throws SQLException {
125+
this.validateState();
126+
this.parameters[parameterIndex - 1] = "'" + x.replace("'", "''") + "'";;
127+
}
128+
129+
public void setBytes(int parameterIndex, byte[] x) throws SQLException {
130+
this.validateState();
131+
this.parameters[parameterIndex - 1] = Hex.encodeHexString(x);
132+
}
133+
134+
public void setDate(int parameterIndex, Date x) throws SQLException {
135+
this.parameters[parameterIndex - 1] = DateTimeUtils.dateToString(x);
136+
}
137+
138+
public void setTime(int parameterIndex, Time x) throws SQLException {
139+
this.validateState();
140+
this.parameters[parameterIndex - 1] = DateTimeUtils.timeToString(x);
141+
}
142+
143+
public void setTimestamp(int parameterIndex, Timestamp x) throws SQLException {
144+
this.validateState();
145+
this.parameters[parameterIndex - 1] = DateTimeUtils.timeStampToString(x);
146+
}
147+
148+
public void setBigDecimal(int parameterIndex, BigDecimal x) throws SQLException {
149+
this.validateState();
150+
this.parameters[parameterIndex - 1] = x.toString();
151+
}
152+
153+
public void clearParameters() throws SQLException {
154+
this.validateState();
155+
for (int i = 0; i < this.parameters.length; i++) {
156+
this.parameters[i] = null;
157+
}
158+
}
45159
}

0 commit comments

Comments
 (0)