1 | #pragma once |
2 | |
3 | #include <optional> |
4 | |
5 | #include <Parsers/ASTWithAlias.h> |
6 | |
7 | |
8 | namespace DB |
9 | { |
10 | |
11 | struct IdentifierSemantic; |
12 | struct IdentifierSemanticImpl; |
13 | struct DatabaseAndTableWithAlias; |
14 | |
15 | |
16 | /// Identifier (column, table or alias) |
17 | class ASTIdentifier : public ASTWithAlias |
18 | { |
19 | public: |
20 | /// The composite identifier will have a concatenated name (of the form a.b.c), |
21 | /// and individual components will be available inside the name_parts. |
22 | String name; |
23 | |
24 | ASTIdentifier(const String & name_, std::vector<String> && name_parts_ = {}); |
25 | ASTIdentifier(std::vector<String> && name_parts_); |
26 | |
27 | /** Get the text that identifies this element. */ |
28 | String getID(char delim) const override { return "Identifier" + (delim + name); } |
29 | |
30 | ASTPtr clone() const override; |
31 | |
32 | void collectIdentifierNames(IdentifierNameSet & set) const override |
33 | { |
34 | set.insert(name); |
35 | } |
36 | |
37 | bool compound() const { return !name_parts.empty(); } |
38 | bool isShort() const { return name_parts.empty() || name == name_parts.back(); } |
39 | |
40 | void setShortName(const String & new_name); |
41 | void restoreCompoundName(); |
42 | |
43 | const String & shortName() const |
44 | { |
45 | if (!name_parts.empty()) |
46 | return name_parts.back(); |
47 | return name; |
48 | } |
49 | |
50 | protected: |
51 | void formatImplWithoutAlias(const FormatSettings & settings, FormatState & state, FormatStateStacked frame) const override; |
52 | void appendColumnNameImpl(WriteBuffer & ostr) const override; |
53 | |
54 | private: |
55 | using ASTWithAlias::children; /// ASTIdentifier is child free |
56 | |
57 | std::vector<String> name_parts; |
58 | std::shared_ptr<IdentifierSemanticImpl> semantic; /// pimpl |
59 | |
60 | static std::shared_ptr<ASTIdentifier> createSpecial(const String & name, std::vector<String> && name_parts = {}); |
61 | |
62 | friend struct IdentifierSemantic; |
63 | friend ASTPtr createTableIdentifier(const String & database_name, const String & table_name); |
64 | friend void setIdentifierSpecial(ASTPtr & ast); |
65 | }; |
66 | |
67 | |
68 | /// ASTIdentifier Helpers: hide casts and semantic. |
69 | |
70 | ASTPtr createTableIdentifier(const String & database_name, const String & table_name); |
71 | void setIdentifierSpecial(ASTPtr & ast); |
72 | |
73 | String getIdentifierName(const IAST * ast); |
74 | std::optional<String> tryGetIdentifierName(const IAST * ast); |
75 | bool tryGetIdentifierNameInto(const IAST * ast, String & name); |
76 | |
77 | inline String getIdentifierName(const ASTPtr & ast) { return getIdentifierName(ast.get()); } |
78 | inline std::optional<String> tryGetIdentifierName(const ASTPtr & ast) { return tryGetIdentifierName(ast.get()); } |
79 | inline bool tryGetIdentifierNameInto(const ASTPtr & ast, String & name) { return tryGetIdentifierNameInto(ast.get(), name); } |
80 | |
81 | } |
82 | |