ClickHouse/dbms/src/Interpreters/InterpreterSetQuery.cpp

68 lines
1.8 KiB
C++
Raw Normal View History

#include <Parsers/ASTSetQuery.h>
2017-05-23 18:24:43 +00:00
#include <Interpreters/Context.h>
#include <Interpreters/InterpreterSetQuery.h>
2017-07-13 20:58:19 +00:00
#include <Common/typeid_cast.h>
#include <Common/FieldVisitors.h>
2016-12-12 07:24:56 +00:00
namespace DB
{
namespace ErrorCodes
{
extern const int READONLY;
2016-12-12 07:24:56 +00:00
}
BlockIO InterpreterSetQuery::execute()
{
2017-05-23 18:24:43 +00:00
const ASTSetQuery & ast = typeid_cast<const ASTSetQuery &>(*query_ptr);
2016-12-12 07:24:56 +00:00
checkAccess(ast);
2016-12-12 07:24:56 +00:00
Context & target = context.getSessionContext();
for (const auto & change : ast.changes)
target.setSetting(change.name, change.value);
return {};
2016-12-12 07:24:56 +00:00
}
void InterpreterSetQuery::checkAccess(const ASTSetQuery & ast)
2016-12-12 07:24:56 +00:00
{
2017-04-02 17:37:49 +00:00
/** The `readonly` value is understood as follows:
* 0 - everything allowed.
* 1 - only read queries can be made; you can not change the settings.
* 2 - You can only do read queries and you can change the settings, except for the `readonly` setting.
*/
const Settings & settings = context.getSettingsRef();
auto readonly = settings.limits.readonly;
for (const auto & change : ast.changes)
{
String value;
/// Setting isn't checked if value wasn't changed.
2017-07-10 16:59:08 +00:00
if (!settings.tryGet(change.name, value) || applyVisitor(FieldVisitorToString(), change.value) != value)
{
if (readonly == 1)
throw Exception("Cannot execute SET query in readonly mode", ErrorCodes::READONLY);
if (readonly > 1 && change.name == "readonly")
throw Exception("Cannot modify 'readonly' setting in readonly mode", ErrorCodes::READONLY);
}
}
}
void InterpreterSetQuery::executeForCurrentContext()
{
const ASTSetQuery & ast = typeid_cast<const ASTSetQuery &>(*query_ptr);
checkAccess(ast);
for (const auto & change : ast.changes)
context.setSetting(change.name, change.value);
2016-12-12 07:24:56 +00:00
}
}