1 | //===----------------------------------------------------------------------===// |
---|---|
2 | // DuckDB |
3 | // |
4 | // duckdb/parser/qualified_name.hpp |
5 | // |
6 | // |
7 | //===----------------------------------------------------------------------===// |
8 | |
9 | #pragma once |
10 | |
11 | #include "duckdb/common/string.hpp" |
12 | #include "duckdb/common/exception.hpp" |
13 | #include "duckdb/parser/keyword_helper.hpp" |
14 | #include "duckdb/common/string_util.hpp" |
15 | |
16 | namespace duckdb { |
17 | |
18 | struct QualifiedName { |
19 | string catalog; |
20 | string schema; |
21 | string name; |
22 | |
23 | //! Parse the (optional) schema and a name from a string in the format of e.g. "schema"."table"; if there is no dot |
24 | //! the schema will be set to INVALID_SCHEMA |
25 | static QualifiedName Parse(const string &input) { |
26 | string catalog; |
27 | string schema; |
28 | string name; |
29 | idx_t idx = 0; |
30 | vector<string> entries; |
31 | string entry; |
32 | normal: |
33 | //! quote |
34 | for (; idx < input.size(); idx++) { |
35 | if (input[idx] == '"') { |
36 | idx++; |
37 | goto quoted; |
38 | } else if (input[idx] == '.') { |
39 | goto separator; |
40 | } |
41 | entry += input[idx]; |
42 | } |
43 | goto end; |
44 | separator: |
45 | entries.push_back(x: entry); |
46 | entry = ""; |
47 | idx++; |
48 | goto normal; |
49 | quoted: |
50 | //! look for another quote |
51 | for (; idx < input.size(); idx++) { |
52 | if (input[idx] == '"') { |
53 | //! unquote |
54 | idx++; |
55 | goto normal; |
56 | } |
57 | entry += input[idx]; |
58 | } |
59 | throw ParserException("Unterminated quote in qualified name!"); |
60 | end: |
61 | if (entries.empty()) { |
62 | catalog = INVALID_CATALOG; |
63 | schema = INVALID_SCHEMA; |
64 | name = entry; |
65 | } else if (entries.size() == 1) { |
66 | catalog = INVALID_CATALOG; |
67 | schema = entries[0]; |
68 | name = entry; |
69 | } else if (entries.size() == 2) { |
70 | catalog = entries[0]; |
71 | schema = entries[1]; |
72 | name = entry; |
73 | } else { |
74 | throw ParserException("Expected catalog.entry, schema.entry or entry: too many entries found"); |
75 | } |
76 | return QualifiedName {.catalog: catalog, .schema: schema, .name: name}; |
77 | } |
78 | }; |
79 | |
80 | struct QualifiedColumnName { |
81 | QualifiedColumnName() { |
82 | } |
83 | QualifiedColumnName(string table_p, string column_p) : table(std::move(table_p)), column(std::move(column_p)) { |
84 | } |
85 | |
86 | string schema; |
87 | string table; |
88 | string column; |
89 | }; |
90 | |
91 | } // namespace duckdb |
92 |