1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 package org.apache.shardingsphere.sql.parser.sql.common.segment.dml.expr.simple;
19
20 import lombok.EqualsAndHashCode;
21 import lombok.Getter;
22 import lombok.RequiredArgsConstructor;
23 import lombok.Setter;
24 import org.apache.shardingsphere.sql.parser.sql.common.enums.ParameterMarkerType;
25 import org.apache.shardingsphere.sql.parser.sql.common.segment.dml.item.ProjectionSegment;
26 import org.apache.shardingsphere.sql.parser.sql.common.segment.generic.AliasAvailable;
27 import org.apache.shardingsphere.sql.parser.sql.common.segment.generic.AliasSegment;
28 import org.apache.shardingsphere.sql.parser.sql.common.segment.generic.ParameterMarkerSegment;
29 import org.apache.shardingsphere.sql.parser.sql.common.segment.generic.bounded.ColumnSegmentBoundedInfo;
30 import org.apache.shardingsphere.sql.parser.sql.common.value.identifier.IdentifierValue;
31
32 import java.util.Optional;
33
34
35
36
37 @RequiredArgsConstructor
38 @Getter
39 @EqualsAndHashCode(exclude = "boundedInfo")
40 public class ParameterMarkerExpressionSegment implements SimpleExpressionSegment, ProjectionSegment, AliasAvailable, ParameterMarkerSegment {
41
42 private final int startIndex;
43
44 private final int stopIndex;
45
46 private final int parameterMarkerIndex;
47
48 private final ParameterMarkerType parameterMarkerType;
49
50 @Setter
51 private AliasSegment alias;
52
53 @Setter
54 private ColumnSegmentBoundedInfo boundedInfo;
55
56 public ParameterMarkerExpressionSegment(final int startIndex, final int stopIndex, final int parameterMarkerIndex) {
57 this.startIndex = startIndex;
58 this.stopIndex = stopIndex;
59 this.parameterMarkerIndex = parameterMarkerIndex;
60 parameterMarkerType = ParameterMarkerType.QUESTION;
61 }
62
63 @Override
64 public String getColumnLabel() {
65 return getAliasName().orElse(String.valueOf(parameterMarkerIndex));
66 }
67
68 @Override
69 public Optional<String> getAliasName() {
70 return null == alias ? Optional.empty() : Optional.ofNullable(alias.getIdentifier().getValue());
71 }
72
73 @Override
74 public Optional<IdentifierValue> getAlias() {
75 return Optional.ofNullable(alias).map(AliasSegment::getIdentifier);
76 }
77
78 @Override
79 public int getParameterIndex() {
80 return parameterMarkerIndex;
81 }
82
83 @Override
84 public ColumnSegmentBoundedInfo getBoundedInfo() {
85 return Optional.ofNullable(boundedInfo).orElseGet(() -> new ColumnSegmentBoundedInfo(new IdentifierValue("")));
86 }
87
88 @Override
89 public int getStopIndex() {
90 return null == alias ? stopIndex : alias.getStopIndex();
91 }
92
93 @Override
94 public String getText() {
95 return parameterMarkerType.getMarker();
96 }
97
98
99
100
101
102
103 public Optional<AliasSegment> getAliasSegment() {
104 return Optional.ofNullable(alias);
105 }
106 }