Skip to content

Commit 00a89f3

Browse files
committed
支持下范围查询
1 parent 6104754 commit 00a89f3

File tree

17 files changed

+252
-266
lines changed

17 files changed

+252
-266
lines changed

tddl-common/src/main/java/com/taobao/tddl/common/model/BaseRowSet.java

+10
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
package com.taobao.tddl.common.model;
22

3+
import java.math.BigDecimal;
34
import java.sql.Date;
5+
import java.sql.Time;
46
import java.sql.Timestamp;
57
import java.util.List;
68

@@ -44,6 +46,14 @@ public interface BaseRowSet {
4446

4547
public void setBytes(int index, byte[] bytes);
4648

49+
public BigDecimal getBigDecimal(int index);
50+
51+
public void setBigDecimal(int index, BigDecimal bigDecimal);
52+
53+
public Time getTime(int index);
54+
55+
public void setTime(int index, Time time);
56+
4757
public Date getDate(int index);
4858

4959
public void setDate(int index, Date date);

tddl-common/src/main/java/com/taobao/tddl/common/utils/convertor/ConvertorHelper.java

+2
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ public class ConvertorHelper {
3636
public static final Convertor dateToSql = new SqlDateAndDateConvertor.DateToSqlDateConvertor();
3737
public static final Convertor blobToBytes = new BlobAndBytesConvertor.BlobToBytes();
3838
public static final Convertor stringToBytes = new StringAndObjectConvertor.StringToBytes();
39+
public static final Convertor bytesToString = new StringAndObjectConvertor.BytesToString();
3940

4041
private static volatile ConvertorHelper singleton = null;
4142

@@ -193,6 +194,7 @@ private void StringDateRegister() {
193194
repository.registerConvertor(java.sql.Time.class, java.sql.Date.class, dateToSql);
194195
repository.registerConvertor(Blob.class, byte[].class, blobToBytes);
195196
repository.registerConvertor(String.class, byte[].class, stringToBytes);
197+
repository.registerConvertor(byte[].class, String.class, bytesToString);
196198
}
197199

198200
private void initCommonTypes() {

tddl-common/src/main/java/com/taobao/tddl/common/utils/convertor/StringAndObjectConvertor.java

+20
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import java.io.IOException;
44
import java.io.InputStream;
5+
import java.io.UnsupportedEncodingException;
56
import java.sql.Clob;
67
import java.sql.SQLException;
78

@@ -56,4 +57,23 @@ public Object convert(Object src, Class destClass) {
5657
}
5758
}
5859

60+
/**
61+
* bytes -> String 转化
62+
*/
63+
public static class BytesToString extends AbastactConvertor {
64+
65+
@Override
66+
public Object convert(Object src, Class destClass) {
67+
if (byte[].class.isInstance(src) && destClass.equals(String.class)) {
68+
try {
69+
return new String((byte[]) src, "ISO-8859-1");
70+
} catch (UnsupportedEncodingException e) {
71+
throw new ConvertorException(e);
72+
}
73+
}
74+
75+
throw new ConvertorException("Unsupported convert: [" + src + "," + destClass.getName() + "]");
76+
}
77+
}
78+
5979
}
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,14 @@
11
package com.taobao.tddl.executor.rowset;
22

3+
import java.math.BigDecimal;
34
import java.sql.Date;
5+
import java.sql.Time;
46
import java.sql.Timestamp;
57
import java.util.List;
68

79
import com.taobao.tddl.executor.cursor.ICursorMeta;
810
import com.taobao.tddl.optimizer.config.table.ColumnMeta;
11+
import com.taobao.tddl.optimizer.core.datatype.DataType;
912

1013
/**
1114
* @author mengshi.sunmengshi 2013-12-3 上午11:06:04
@@ -27,22 +30,8 @@ public ICursorMeta getParentCursorMeta() {
2730

2831
@Override
2932
public Integer getInteger(int index) {
30-
3133
ColumnMeta cm = iCursorMeta.getColumns().get(index);
32-
return (Integer) cm.getDataType().getResultGetter().get(this, index);
33-
34-
// Object val = this.getObject(index);
35-
// if (val == null) return 0;
36-
//
37-
// if (val instanceof Integer) return (Integer) val;
38-
//
39-
// if (val instanceof Number) return ((Number) val).intValue();
40-
//
41-
// if (val instanceof BigDecimal) return ((BigDecimal) val).intValue();
42-
//
43-
// String strVal = this.getString(index);
44-
//
45-
// return Integer.valueOf(strVal);
34+
return DataType.IntegerType.convertFrom(cm.getDataType().getResultGetter().get(this, index));
4635
}
4736

4837
@Override
@@ -52,48 +41,19 @@ public void setInteger(int index, Integer value) {
5241

5342
@Override
5443
public Long getLong(int index) {
55-
5644
ColumnMeta cm = iCursorMeta.getColumns().get(index);
57-
return (Long) cm.getDataType().getResultGetter().get(this, index);
58-
59-
// Object val = this.getObject(index);
60-
// if (val == null) return 0L;
61-
//
62-
// if (val instanceof Long) return (Long) val;
63-
//
64-
// if (val instanceof Number) return ((Number) val).longValue();
65-
//
66-
// if (val instanceof BigDecimal) return ((BigDecimal) val).longValue();
67-
//
68-
// String strVal = this.getString(index);
69-
//
70-
// return Long.valueOf(strVal);
45+
return DataType.LongType.convertFrom(cm.getDataType().getResultGetter().get(this, index));
7146
}
7247

7348
@Override
7449
public void setLong(int index, Long value) {
7550
setObject(index, value);
7651
}
7752

78-
@Override
79-
public List<Object> getValues() {
80-
throw new UnsupportedOperationException();
81-
}
82-
83-
@Override
84-
public String toString() {
85-
StringBuilder sb = new StringBuilder();
86-
87-
for (ColumnMeta cm : this.getParentCursorMeta().getColumns()) {
88-
int index = this.getParentCursorMeta().getIndex(cm.getTableName(), cm.getName());
89-
sb.append(cm.getName() + ":" + this.getValues().get(index) + " ");
90-
}
91-
return sb.toString();
92-
}
93-
9453
@Override
9554
public String getString(int index) {
96-
return String.valueOf(getObject(index));
55+
ColumnMeta cm = iCursorMeta.getColumns().get(index);
56+
return DataType.StringType.convertFrom(cm.getDataType().getResultGetter().get(this, index));
9757
}
9858

9959
@Override
@@ -103,19 +63,8 @@ public void setString(int index, String str) {
10363

10464
@Override
10565
public Boolean getBoolean(int index) {
106-
10766
ColumnMeta cm = iCursorMeta.getColumns().get(index);
108-
return (Boolean) cm.getDataType().getResultGetter().get(this, index);
109-
110-
// Object val = this.getObject(index);
111-
// // -1 || >0 是true
112-
// if (val == null) return false;
113-
//
114-
// if (val instanceof Boolean) return (Boolean) val;
115-
//
116-
// String strVal = this.getString(index);
117-
//
118-
// return Boolean.valueOf(strVal);
67+
return DataType.BooleanType.convertFrom(cm.getDataType().getResultGetter().get(this, index));
11968
}
12069

12170
@Override
@@ -125,23 +74,8 @@ public void setBoolean(int index, Boolean bool) {
12574

12675
@Override
12776
public Short getShort(int index) {
128-
12977
ColumnMeta cm = iCursorMeta.getColumns().get(index);
130-
return (Short) cm.getDataType().getResultGetter().get(this, index);
131-
132-
// Object val = this.getObject(index);
133-
// if (val == null) return 0;
134-
//
135-
// if (val instanceof Short) return (Short) val;
136-
//
137-
// if (val instanceof Number) return ((Number) val).shortValue();
138-
//
139-
// if (val instanceof BigDecimal) return ((BigDecimal)
140-
// val).shortValue();
141-
//
142-
// String strVal = this.getString(index);
143-
//
144-
// return Short.valueOf(strVal);
78+
return DataType.ShortType.convertFrom(cm.getDataType().getResultGetter().get(this, index));
14579
}
14680

14781
@Override
@@ -151,23 +85,8 @@ public void setShort(int index, Short shortval) {
15185

15286
@Override
15387
public Float getFloat(int index) {
154-
15588
ColumnMeta cm = iCursorMeta.getColumns().get(index);
156-
return (Float) cm.getDataType().getResultGetter().get(this, index);
157-
158-
// Object val = this.getObject(index);
159-
// if (val == null) return (float) 0.0;
160-
//
161-
// if (val instanceof Float) return (Float) val;
162-
//
163-
// if (val instanceof Number) return ((Number) val).floatValue();
164-
//
165-
// if (val instanceof BigDecimal) return ((BigDecimal)
166-
// val).floatValue();
167-
//
168-
// String strVal = this.getString(index);
169-
//
170-
// return Float.valueOf(strVal);
89+
return DataType.FloatType.convertFrom(cm.getDataType().getResultGetter().get(this, index));
17190
}
17291

17392
@Override
@@ -178,21 +97,7 @@ public void setFloat(int index, Float fl) {
17897
@Override
17998
public Double getDouble(int index) {
18099
ColumnMeta cm = iCursorMeta.getColumns().get(index);
181-
return (Double) cm.getDataType().getResultGetter().get(this, index);
182-
183-
// Object val = this.getObject(index);
184-
// if (val == null) return 0.0;
185-
//
186-
// if (val instanceof Double) return (Double) val;
187-
//
188-
// if (val instanceof Number) return ((Number) val).doubleValue();
189-
//
190-
// if (val instanceof BigDecimal) return ((BigDecimal)
191-
// val).doubleValue();
192-
//
193-
// String strVal = this.getString(index);
194-
//
195-
// return Double.valueOf(strVal);
100+
return DataType.DoubleType.convertFrom(cm.getDataType().getResultGetter().get(this, index));
196101
}
197102

198103
@Override
@@ -202,15 +107,8 @@ public void setDouble(int index, Double doub) {
202107

203108
@Override
204109
public byte[] getBytes(int index) {
205-
206110
ColumnMeta cm = iCursorMeta.getColumns().get(index);
207-
return (byte[]) cm.getDataType().getResultGetter().get(this, index);
208-
209-
// Object obj = getObject(index);
210-
// if (obj == null) return null;
211-
// if (obj instanceof byte[]) return (byte[]) obj;
212-
// else throw new RuntimeException("暂不支持类型:" + obj.getClass() +
213-
// " 的getBytes操作");
111+
return DataType.BytesType.convertFrom(cm.getDataType().getResultGetter().get(this, index));
214112
}
215113

216114
@Override
@@ -220,17 +118,8 @@ public void setBytes(int index, byte[] bytes) {
220118

221119
@Override
222120
public Date getDate(int index) {
223-
224121
ColumnMeta cm = iCursorMeta.getColumns().get(index);
225-
return (Date) cm.getDataType().getResultGetter().get(this, index);
226-
227-
// Object obj = getObject(index);
228-
// if (obj == null) return null;
229-
// if (obj instanceof Date) return (Date) obj;
230-
// if (obj instanceof java.util.Date) {
231-
// return new Date(((java.util.Date) obj).getTime());
232-
// } else throw new RuntimeException("暂不支持类型:" + obj.getClass() +
233-
// " 的getDate操作");
122+
return DataType.DateType.convertFrom(cm.getDataType().getResultGetter().get(this, index));
234123
}
235124

236125
@Override
@@ -241,19 +130,49 @@ public void setDate(int index, Date date) {
241130
@Override
242131
public Timestamp getTimestamp(int index) {
243132
ColumnMeta cm = iCursorMeta.getColumns().get(index);
244-
return (Timestamp) cm.getDataType().getResultGetter().get(this, index);
245-
// Object obj = getObject(index);
246-
// if (obj == null) return null;
247-
// if (obj instanceof Timestamp) return (Timestamp) obj;
248-
// if (obj instanceof java.util.Date) {
249-
// return new Timestamp(((java.util.Date) obj).getTime());
250-
// } else throw new RuntimeException("暂不支持类型:" + obj.getClass() +
251-
// " 的getTimestamp操作");
133+
return DataType.TimestampType.convertFrom(cm.getDataType().getResultGetter().get(this, index));
252134
}
253135

254136
@Override
255137
public void setTimestamp(int index, Timestamp timestamp) {
256138
setObject(index, timestamp);
257139
}
258140

141+
@Override
142+
public Time getTime(int index) {
143+
ColumnMeta cm = iCursorMeta.getColumns().get(index);
144+
return DataType.TimeType.convertFrom(cm.getDataType().getResultGetter().get(this, index));
145+
}
146+
147+
@Override
148+
public void setTime(int index, Time time) {
149+
setObject(index, time);
150+
}
151+
152+
@Override
153+
public BigDecimal getBigDecimal(int index) {
154+
ColumnMeta cm = iCursorMeta.getColumns().get(index);
155+
return DataType.BigDecimalType.convertFrom(cm.getDataType().getResultGetter().get(this, index));
156+
}
157+
158+
@Override
159+
public void setBigDecimal(int index, BigDecimal bigDecimal) {
160+
setObject(index, bigDecimal);
161+
}
162+
163+
@Override
164+
public List<Object> getValues() {
165+
throw new UnsupportedOperationException();
166+
}
167+
168+
@Override
169+
public String toString() {
170+
StringBuilder sb = new StringBuilder();
171+
172+
for (ColumnMeta cm : this.getParentCursorMeta().getColumns()) {
173+
int index = this.getParentCursorMeta().getIndex(cm.getTableName(), cm.getName());
174+
sb.append(cm.getName() + ":" + this.getValues().get(index) + " ");
175+
}
176+
return sb.toString();
177+
}
259178
}

0 commit comments

Comments
 (0)