Skip to content

Commit 5afa728

Browse files
author
Vitaly Baranov
committed
Replace syntax "DEFAULT DATABASE ''" with "DEFAULT DATABASE NONE".
Show default database in system.users.
1 parent 9c56cc8 commit 5afa728

8 files changed

Lines changed: 35 additions & 16 deletions

src/Access/User.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ bool User::equal(const IAccessEntity & other) const
1111
const auto & other_user = typeid_cast<const User &>(other);
1212
return (authentication == other_user.authentication) && (allowed_client_hosts == other_user.allowed_client_hosts)
1313
&& (access == other_user.access) && (granted_roles == other_user.granted_roles) && (default_roles == other_user.default_roles)
14-
&& (settings == other_user.settings) && (grantees == other_user.grantees);
14+
&& (settings == other_user.settings) && (grantees == other_user.grantees) && (default_database == other_user.default_database);
1515
}
1616

1717
}

src/Access/UsersConfigAccessStorage.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -196,8 +196,7 @@ namespace
196196
user->access.revokeGrantOption(AccessType::ALL);
197197
}
198198

199-
String default_database = config.getString(user_config + ".default_database", "");
200-
user->default_database = default_database;
199+
user->default_database = config.getString(user_config + ".default_database", "");
201200

202201
return user;
203202
}

src/Interpreters/InterpreterCreateUserQuery.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,8 +59,8 @@ namespace
5959
else if (query.default_roles)
6060
set_default_roles(*query.default_roles);
6161

62-
if (!query.default_database.empty())
63-
user.default_database = query.default_database;
62+
if (query.default_database)
63+
user.default_database = *query.default_database;
6464

6565
if (override_settings)
6666
user.settings = *override_settings;

src/Interpreters/InterpreterShowCreateAccessEntityQuery.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,8 @@ namespace
8282
query->grantees->use_keyword_any = true;
8383
}
8484

85-
query->default_database = user.default_database;
85+
if (!user.default_database.empty())
86+
query->default_database = user.default_database;
8687

8788
return query;
8889
}

src/Parsers/ASTCreateUserQuery.cpp

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -213,7 +213,11 @@ namespace
213213

214214
void formatDefaultDatabase(const String & default_database, const IAST::FormatSettings & settings)
215215
{
216-
settings.ostr << (settings.hilite ? IAST::hilite_keyword : "") << " DEFAULT DATABASE " << (settings.hilite ? IAST::hilite_none : "") << default_database;
216+
settings.ostr << (settings.hilite ? IAST::hilite_keyword : "") << " DEFAULT DATABASE " << (settings.hilite ? IAST::hilite_none : "");
217+
if (default_database.empty())
218+
settings.ostr << (settings.hilite ? IAST::hilite_keyword : "") << "NONE" << (settings.hilite ? IAST::hilite_none : "");
219+
else
220+
settings.ostr << backQuoteIfNeed(default_database);
217221
}
218222
}
219223

@@ -267,8 +271,8 @@ void ASTCreateUserQuery::formatImpl(const FormatSettings & format, FormatState &
267271
if (remove_hosts)
268272
formatHosts("DROP", *remove_hosts, format);
269273

270-
if (!default_database.empty())
271-
formatDefaultDatabase(default_database, format);
274+
if (default_database)
275+
formatDefaultDatabase(*default_database, format);
272276

273277
if (default_roles)
274278
formatDefaultRoles(*default_roles, format);

src/Parsers/ASTCreateUserQuery.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ class ASTCreateUserQuery : public IAST, public ASTQueryWithOnCluster
5353
std::shared_ptr<ASTSettingsProfileElements> settings;
5454
std::shared_ptr<ASTRolesOrUsersSet> grantees;
5555

56-
String default_database;
56+
std::optional<String> default_database;
5757

5858
String getID(char) const override;
5959
ASTPtr clone() const override;

src/Parsers/ParserCreateUserQuery.cpp

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -301,14 +301,24 @@ namespace
301301
});
302302
}
303303

304-
bool parseDefaultDatabase(IParserBase::Pos & pos, Expected & expected, String & default_database)
304+
bool parseDefaultDatabase(IParserBase::Pos & pos, Expected & expected, std::optional<String> & default_database)
305305
{
306306
return IParserBase::wrapParseImpl(pos, [&]
307307
{
308308
if (!ParserKeyword{"DEFAULT DATABASE"}.ignore(pos, expected))
309309
return false;
310310

311-
return parseIdentifierOrStringLiteral(pos, expected, default_database);
311+
if (ParserKeyword{"NONE"}.ignore(pos, expected))
312+
{
313+
default_database = "";
314+
return true;
315+
}
316+
317+
String db_name;
318+
if (!parseIdentifierOrStringLiteral(pos, expected, db_name))
319+
return false;
320+
default_database = db_name;
321+
return true;
312322
});
313323
}
314324
}
@@ -360,8 +370,8 @@ bool ParserCreateUserQuery::parseImpl(Pos & pos, ASTPtr & node, Expected & expec
360370
std::shared_ptr<ASTRolesOrUsersSet> default_roles;
361371
std::shared_ptr<ASTSettingsProfileElements> settings;
362372
std::shared_ptr<ASTRolesOrUsersSet> grantees;
373+
std::optional<String> default_database;
363374
String cluster;
364-
String default_database;
365375

366376
while (true)
367377
{
@@ -402,7 +412,7 @@ bool ParserCreateUserQuery::parseImpl(Pos & pos, ASTPtr & node, Expected & expec
402412
if (!grantees && parseGrantees(pos, expected, attach_mode, grantees))
403413
continue;
404414

405-
if (default_database.empty() && parseDefaultDatabase(pos, expected, default_database))
415+
if (!default_database && parseDefaultDatabase(pos, expected, default_database))
406416
continue;
407417

408418
if (alter)

src/Storages/System/StorageSystemUsers.cpp

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ NamesAndTypesList StorageSystemUsers::getNamesAndTypes()
5050
{"grantees_any", std::make_shared<DataTypeUInt8>()},
5151
{"grantees_list", std::make_shared<DataTypeArray>(std::make_shared<DataTypeString>())},
5252
{"grantees_except", std::make_shared<DataTypeArray>(std::make_shared<DataTypeString>())},
53+
{"default_database", std::make_shared<DataTypeString>()},
5354
};
5455
return names_and_types;
5556
}
@@ -85,14 +86,16 @@ void StorageSystemUsers::fillData(MutableColumns & res_columns, ContextPtr conte
8586
auto & column_grantees_list_offsets = assert_cast<ColumnArray &>(*res_columns[column_index++]).getOffsets();
8687
auto & column_grantees_except = assert_cast<ColumnString &>(assert_cast<ColumnArray &>(*res_columns[column_index]).getData());
8788
auto & column_grantees_except_offsets = assert_cast<ColumnArray &>(*res_columns[column_index++]).getOffsets();
89+
auto & column_default_database = assert_cast<ColumnString &>(*res_columns[column_index++]);
8890

8991
auto add_row = [&](const String & name,
9092
const UUID & id,
9193
const String & storage_name,
9294
const Authentication & authentication,
9395
const AllowedClientHosts & allowed_hosts,
9496
const RolesOrUsersSet & default_roles,
95-
const RolesOrUsersSet & grantees)
97+
const RolesOrUsersSet & grantees,
98+
const String & default_database)
9699
{
97100
column_name.insertData(name.data(), name.length());
98101
column_id.push_back(id.toUnderType());
@@ -180,6 +183,8 @@ void StorageSystemUsers::fillData(MutableColumns & res_columns, ContextPtr conte
180183
for (const auto & except_name : grantees_ast->except_names)
181184
column_grantees_except.insertData(except_name.data(), except_name.length());
182185
column_grantees_except_offsets.push_back(column_grantees_except.size());
186+
187+
column_default_database.insertData(default_database.data(), default_database.length());
183188
};
184189

185190
for (const auto & id : ids)
@@ -192,7 +197,7 @@ void StorageSystemUsers::fillData(MutableColumns & res_columns, ContextPtr conte
192197
if (!storage)
193198
continue;
194199

195-
add_row(user->getName(), id, storage->getStorageName(), user->authentication, user->allowed_client_hosts, user->default_roles, user->grantees);
200+
add_row(user->getName(), id, storage->getStorageName(), user->authentication, user->allowed_client_hosts, user->default_roles, user->grantees, user->default_database);
196201
}
197202
}
198203

0 commit comments

Comments
 (0)