1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 package org.apache.shardingsphere.infra.binder.context.segment.select.projection.impl;
19
20 import lombok.EqualsAndHashCode;
21 import lombok.Getter;
22 import lombok.RequiredArgsConstructor;
23 import lombok.ToString;
24 import org.apache.shardingsphere.infra.binder.context.segment.select.projection.Projection;
25 import org.apache.shardingsphere.infra.binder.context.segment.select.projection.extractor.ProjectionIdentifierExtractEngine;
26 import org.apache.shardingsphere.infra.database.core.type.DatabaseType;
27 import org.apache.shardingsphere.sql.parser.statement.core.segment.dml.item.SubqueryProjectionSegment;
28 import org.apache.shardingsphere.sql.parser.statement.core.value.identifier.IdentifierValue;
29
30 import java.util.Optional;
31
32
33
34
35 @RequiredArgsConstructor
36 @Getter
37 @EqualsAndHashCode
38 @ToString
39 public final class SubqueryProjection implements Projection {
40
41 private final SubqueryProjectionSegment subquerySegment;
42
43 private final Projection projection;
44
45 private final IdentifierValue alias;
46
47 private final DatabaseType databaseType;
48
49 @Override
50 public String getColumnName() {
51 return getColumnLabel();
52 }
53
54 @Override
55 public String getColumnLabel() {
56 ProjectionIdentifierExtractEngine extractEngine = new ProjectionIdentifierExtractEngine(databaseType);
57 return getAlias().map(extractEngine::getIdentifierValue).orElseGet(() -> extractEngine.getColumnNameFromSubquery(subquerySegment));
58 }
59
60 @Override
61 public Optional<IdentifierValue> getAlias() {
62 return Optional.ofNullable(alias);
63 }
64
65 @Override
66 public String getExpression() {
67 return subquerySegment.getText();
68 }
69 }