aboutsummaryrefslogtreecommitdiffstats
path: root/contrib/clickhouse/src/Storages/MySQL/MySQLSettings.cpp
blob: fd53174f4f692d7c89646e63c5630c82308ae04f (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
#include <Storages/MySQL/MySQLSettings.h>
#include <Parsers/ASTCreateQuery.h>
#include <Parsers/ASTSetQuery.h>
#include <Parsers/ASTFunction.h>
#include <Common/Exception.h>
#include <Interpreters/Context.h>
#include <Parsers/formatAST.h>
#include <Core/Field.h>


namespace DB
{

namespace ErrorCodes
{
    extern const int UNKNOWN_SETTING;
}

IMPLEMENT_SETTINGS_TRAITS(MySQLSettingsTraits, LIST_OF_MYSQL_SETTINGS)

void MySQLSettings::loadFromQuery(const ASTSetQuery & settings_def)
{
    applyChanges(settings_def.changes);
}

void MySQLSettings::loadFromQuery(ASTStorage & storage_def)
{
    if (storage_def.settings)
    {
        try
        {
            loadFromQuery(*storage_def.settings);
        }
        catch (Exception & e)
        {
            if (e.code() == ErrorCodes::UNKNOWN_SETTING)
                e.addMessage("for storage " + storage_def.engine->name);
            throw;
        }
    }
    else
    {
        auto settings_ast = std::make_shared<ASTSetQuery>();
        settings_ast->is_standalone = false;
        storage_def.set(storage_def.settings, settings_ast);
    }
}

void MySQLSettings::loadFromQueryContext(ContextPtr context, ASTStorage & storage_def)
{
    if (!context->hasQueryContext())
        return;

    const Settings & settings = context->getQueryContext()->getSettingsRef();

    if (settings.mysql_datatypes_support_level.value != mysql_datatypes_support_level.value)
    {
        static constexpr auto setting_name = "mysql_datatypes_support_level";
        set(setting_name, settings.mysql_datatypes_support_level.toString());

        if (!storage_def.settings)
        {
            auto settings_ast = std::make_shared<ASTSetQuery>();
            settings_ast->is_standalone = false;
            storage_def.set(storage_def.settings, settings_ast);
        }

        auto & changes = storage_def.settings->changes;
        if (changes.end() == std::find_if(
                changes.begin(), changes.end(),
                [](const SettingChange & c) { return c.name == setting_name; }))
        {
            changes.push_back(SettingChange{setting_name, settings.mysql_datatypes_support_level.toString()});
        }
    }
}

}