1#include <Storages/ConstraintsDescription.h>
2
3#include <Parsers/formatAST.h>
4#include <Parsers/ParserCreateQuery.h>
5#include <Parsers/parseQuery.h>
6#include <Parsers/ASTExpressionList.h>
7
8
9namespace DB
10{
11
12String ConstraintsDescription::toString() const
13{
14 if (constraints.empty())
15 return {};
16
17 ASTExpressionList list;
18 for (const auto & constraint : constraints)
19 list.children.push_back(constraint);
20
21 return serializeAST(list, true);
22}
23
24ConstraintsDescription ConstraintsDescription::parse(const String & str)
25{
26 if (str.empty())
27 return {};
28
29 ConstraintsDescription res;
30 ParserConstraintDeclarationList parser;
31 ASTPtr list = parseQuery(parser, str, 0);
32
33 for (const auto & constraint : list->children)
34 res.constraints.push_back(std::dynamic_pointer_cast<ASTConstraintDeclaration>(constraint));
35
36 return res;
37}
38
39ConstraintsExpressions ConstraintsDescription::getExpressions(const DB::Context & context,
40 const DB::NamesAndTypesList & source_columns_) const
41{
42 ConstraintsExpressions res;
43 res.reserve(constraints.size());
44 for (const auto & constraint : constraints)
45 {
46 // SyntaxAnalyzer::analyze has query as non-const argument so to avoid accidental query changes we clone it
47 ASTPtr expr = constraint->expr->clone();
48 auto syntax_result = SyntaxAnalyzer(context).analyze(expr, source_columns_);
49 res.push_back(ExpressionAnalyzer(constraint->expr->clone(), syntax_result, context).getActions(false));
50 }
51 return res;
52}
53
54}
55