1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 package org.tikv.common.expression;
19
20 import com.google.common.collect.ImmutableList;
21 import java.util.List;
22 import java.util.Objects;
23 import org.tikv.common.exception.TiExpressionException;
24 import org.tikv.common.meta.TiColumnInfo;
25 import org.tikv.common.meta.TiTableInfo;
26 import org.tikv.common.types.DataType;
27
28 public class ColumnRef extends Expression {
29 private final String name;
30
31 @Deprecated
32 public ColumnRef(String name) {
33 this.name = name;
34 }
35
36 public ColumnRef(String name, DataType dataType) {
37 super(dataType);
38 resolved = true;
39 this.name = name;
40 }
41
42 public static ColumnRef create(String name, TiTableInfo table) {
43 name = name.replaceAll("`", "");
44 TiColumnInfo col = table.getColumn(name);
45 if (col != null) {
46 return new ColumnRef(name, col.getType());
47 }
48
49 throw new TiExpressionException(
50 String.format("Column name %s not found in table %s", name, table));
51 }
52
53 @Deprecated
54 public static ColumnRef create(String name) {
55 return new ColumnRef(name);
56 }
57
58 public static ColumnRef create(String name, DataType dataType) {
59 return new ColumnRef(name, dataType);
60 }
61
62 public static ColumnRef create(String name, TiColumnInfo columnInfo) {
63 return new ColumnRef(name, columnInfo.getType());
64 }
65
66 public String getName() {
67 return name.toLowerCase();
68 }
69
70 public void resolve(TiTableInfo table) {
71 TiColumnInfo columnInfo = null;
72 for (TiColumnInfo col : table.getColumns()) {
73 if (col.matchName(name)) {
74 this.dataType = col.getType();
75 columnInfo = col;
76 break;
77 }
78 }
79 if (columnInfo == null) {
80 throw new TiExpressionException(
81 String.format("No Matching column %s from table %s", name, table.getName()));
82 }
83
84 if (columnInfo.getId() == 0) {
85 throw new TiExpressionException("Zero Id is not a referable column id");
86 }
87 }
88
89 public boolean matchName(String name) {
90 return this.name.equalsIgnoreCase(name);
91 }
92
93 @Override
94 public DataType getDataType() {
95 return dataType;
96 }
97
98 @Override
99 public boolean isResolved() {
100 return resolved;
101 }
102
103 @Override
104 public boolean equals(Object another) {
105 if (this == another) {
106 return true;
107 }
108
109 if (another instanceof ColumnRef) {
110 ColumnRef that = (ColumnRef) another;
111 if (isResolved() && that.isResolved()) {
112 return name.equalsIgnoreCase(that.name)
113 && this.dataType.equals(((ColumnRef) another).dataType);
114 } else {
115 return name.equalsIgnoreCase(that.name);
116 }
117
118 } else {
119 return false;
120 }
121 }
122
123 @Override
124 public int hashCode() {
125 if (isResolved()) {
126 return Objects.hash(this.name.toLowerCase(), this.dataType);
127 } else {
128 return Objects.hashCode(name.toLowerCase());
129 }
130 }
131
132 @Override
133 public String toString() {
134 if (dataType != null) {
135 return String.format("%s@%s", getName(), dataType.getName());
136 } else {
137 return String.format("[%s]", getName());
138 }
139 }
140
141 @Override
142 public List<Expression> getChildren() {
143 return ImmutableList.of();
144 }
145
146 @Override
147 public <R, C> R accept(Visitor<R, C> visitor, C context) {
148 return visitor.visit(this, context);
149 }
150 }