2017-04-01 09:19:00 +00:00
|
|
|
#include <Parsers/ASTSetQuery.h>
|
2017-05-23 18:24:43 +00:00
|
|
|
#include <Interpreters/Context.h>
|
2017-04-01 09:19:00 +00:00
|
|
|
#include <Interpreters/InterpreterSetQuery.h>
|
2017-07-13 20:58:19 +00:00
|
|
|
#include <Common/typeid_cast.h>
|
2017-11-24 13:55:31 +00:00
|
|
|
#include <Common/FieldVisitors.h>
|
2016-12-12 07:24:56 +00:00
|
|
|
|
|
|
|
namespace DB
|
|
|
|
{
|
|
|
|
|
|
|
|
namespace ErrorCodes
|
|
|
|
{
|
2017-04-01 07:20:54 +00:00
|
|
|
extern const int READONLY;
|
2018-09-11 18:37:19 +00:00
|
|
|
extern const int QUERY_IS_PROHIBITED;
|
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
|
|
|
|
2017-06-07 12:54:35 +00:00
|
|
|
checkAccess(ast);
|
2016-12-12 07:24:56 +00:00
|
|
|
|
2017-06-07 12:54:35 +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
|
|
|
}
|
|
|
|
|
2017-06-07 12:54:35 +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.
|
|
|
|
*/
|
2017-04-01 07:20:54 +00:00
|
|
|
|
2017-07-10 14:03:34 +00:00
|
|
|
const Settings & settings = context.getSettingsRef();
|
2018-03-11 00:15:26 +00:00
|
|
|
auto readonly = settings.readonly;
|
2018-09-11 18:37:19 +00:00
|
|
|
auto allow_ddl = settings.allow_ddl;
|
2017-04-01 07:20:54 +00:00
|
|
|
|
2017-07-10 14:03:34 +00:00
|
|
|
for (const auto & change : ast.changes)
|
2017-06-07 12:54:35 +00:00
|
|
|
{
|
2017-07-10 14:03:34 +00:00
|
|
|
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)
|
2017-06-07 12:54:35 +00:00
|
|
|
{
|
2018-09-11 18:37:19 +00:00
|
|
|
|
|
|
|
if (!allow_ddl && change.name == "allow_ddl")
|
2018-09-11 18:41:53 +00:00
|
|
|
throw Exception("Cannot modify 'allow_ddl' setting when DDL queries are prohibited for the user", ErrorCodes::QUERY_IS_PROHIBITED);
|
2018-09-11 18:37:19 +00:00
|
|
|
|
2017-07-10 14:03:34 +00:00
|
|
|
if (readonly == 1)
|
|
|
|
throw Exception("Cannot execute SET query in readonly mode", ErrorCodes::READONLY);
|
|
|
|
|
|
|
|
if (readonly > 1 && change.name == "readonly")
|
2017-04-01 07:20:54 +00:00
|
|
|
throw Exception("Cannot modify 'readonly' setting in readonly mode", ErrorCodes::READONLY);
|
2017-06-07 12:54:35 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void InterpreterSetQuery::executeForCurrentContext()
|
|
|
|
{
|
|
|
|
const ASTSetQuery & ast = typeid_cast<const ASTSetQuery &>(*query_ptr);
|
|
|
|
|
|
|
|
checkAccess(ast);
|
2017-04-01 07:20:54 +00:00
|
|
|
|
2017-06-07 12:54:35 +00:00
|
|
|
for (const auto & change : ast.changes)
|
|
|
|
context.setSetting(change.name, change.value);
|
2016-12-12 07:24:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
}
|