|
16 | 16 |
|
17 | 17 | package com.navercorp.pinpoint.common.pinot.datasource;
|
18 | 18 |
|
| 19 | +import org.apache.commons.codec.binary.Hex; |
19 | 20 | import org.apache.pinot.client.PinotConnection;
|
20 | 21 | import org.apache.pinot.client.PinotPreparedStatement;
|
| 22 | +import org.apache.pinot.client.utils.DateTimeUtils; |
21 | 23 |
|
| 24 | +import java.math.BigDecimal; |
22 | 25 | import java.sql.ResultSet;
|
23 | 26 | import java.sql.SQLException;
|
| 27 | +import java.sql.Time; |
| 28 | +import java.sql.Timestamp; |
| 29 | +import java.sql.Date; |
24 | 30 |
|
25 | 31 | /**
|
26 | 32 | * @author Hyunjoon Cho
|
27 | 33 | */
|
28 | 34 | 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; |
29 | 38 |
|
30 | 39 | public WrappedPinotPreparedStatement(PinotConnection connection, String query) {
|
31 | 40 | super(connection, query);
|
| 41 | + this.query = query; |
| 42 | + this.parameters = new String[this.getQuestionMarkCount(query)]; |
32 | 43 | }
|
33 | 44 |
|
| 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 | + |
34 | 57 | @Override
|
35 | 58 | public boolean execute() throws SQLException {
|
36 | 59 | // WARNING Do not invoke close().
|
37 | 60 | // Ignore spotbug warning
|
38 |
| - ResultSet resultSet = executeQuery(); |
| 61 | + ResultSet resultSet = executeQuery(fillStatementWithParameters()); |
39 | 62 | if (resultSet.isLast()) {
|
40 | 63 | return false;
|
41 | 64 | } else {
|
42 | 65 | return true;
|
43 | 66 | }
|
44 | 67 | }
|
| 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 | + } |
45 | 159 | }
|
0 commit comments