2021-09-09 09:37:51 +00:00
|
|
|
#include <Databases/DatabaseOnDisk.h>
|
|
|
|
#include <Storages/System/attachInformationSchemaTables.h>
|
|
|
|
#include <Storages/System/attachSystemTablesImpl.h>
|
|
|
|
#include <Parsers/ParserCreateQuery.h>
|
|
|
|
#include <Parsers/parseQuery.h>
|
2023-07-23 02:56:47 +00:00
|
|
|
#include <incbin.h>
|
|
|
|
|
|
|
|
/// Embedded SQL definitions
|
|
|
|
INCBIN(resource_schemata_sql, "schemata.sql");
|
|
|
|
INCBIN(resource_tables_sql, "tables.sql");
|
|
|
|
INCBIN(resource_views_sql, "views.sql");
|
|
|
|
INCBIN(resource_columns_sql, "columns.sql");
|
|
|
|
|
2021-09-09 09:37:51 +00:00
|
|
|
|
|
|
|
namespace DB
|
|
|
|
{
|
|
|
|
|
|
|
|
/// View structures are taken from http://www.contrib.andrew.cmu.edu/~shadow/sql/sql1992.txt
|
|
|
|
|
2023-07-23 02:56:47 +00:00
|
|
|
static void createInformationSchemaView(ContextMutablePtr context, IDatabase & database, const String & view_name, std::string_view query)
|
2021-09-09 09:37:51 +00:00
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
|
|
|
assert(database.getDatabaseName() == DatabaseCatalog::INFORMATION_SCHEMA ||
|
|
|
|
database.getDatabaseName() == DatabaseCatalog::INFORMATION_SCHEMA_UPPERCASE);
|
|
|
|
if (database.getEngineName() != "Memory")
|
|
|
|
return;
|
|
|
|
bool is_uppercase = database.getDatabaseName() == DatabaseCatalog::INFORMATION_SCHEMA_UPPERCASE;
|
|
|
|
|
|
|
|
String metadata_resource_name = view_name + ".sql";
|
2023-07-23 02:56:47 +00:00
|
|
|
if (query.empty())
|
2021-09-09 09:37:51 +00:00
|
|
|
return;
|
|
|
|
|
|
|
|
ParserCreateQuery parser;
|
2023-07-23 02:56:47 +00:00
|
|
|
ASTPtr ast = parseQuery(parser, query.data(), query.data() + query.size(),
|
2021-09-09 09:37:51 +00:00
|
|
|
"Attach query from embedded resource " + metadata_resource_name,
|
|
|
|
DBMS_DEFAULT_MAX_QUERY_SIZE, DBMS_DEFAULT_MAX_PARSER_DEPTH);
|
|
|
|
|
|
|
|
auto & ast_create = ast->as<ASTCreateQuery &>();
|
2021-09-13 10:17:29 +00:00
|
|
|
assert(view_name == ast_create.getTable());
|
2022-03-21 16:01:39 +00:00
|
|
|
ast_create.attach = false;
|
|
|
|
ast_create.setDatabase(database.getDatabaseName());
|
2021-09-09 09:37:51 +00:00
|
|
|
if (is_uppercase)
|
2021-09-13 10:17:29 +00:00
|
|
|
ast_create.setTable(Poco::toUpper(view_name));
|
2021-09-09 09:37:51 +00:00
|
|
|
|
|
|
|
StoragePtr view = createTableFromAST(ast_create, database.getDatabaseName(),
|
|
|
|
database.getTableDataPath(ast_create), context, true).second;
|
|
|
|
|
2021-09-13 10:17:29 +00:00
|
|
|
database.createTable(context, ast_create.getTable(), view, ast);
|
2021-09-09 09:37:51 +00:00
|
|
|
}
|
|
|
|
catch (...)
|
|
|
|
{
|
|
|
|
tryLogCurrentException(__PRETTY_FUNCTION__);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void attachInformationSchema(ContextMutablePtr context, IDatabase & information_schema_database)
|
|
|
|
{
|
2023-07-23 02:56:47 +00:00
|
|
|
createInformationSchemaView(context, information_schema_database, "schemata", std::string_view(reinterpret_cast<const char *>(gresource_schemata_sqlData), gresource_schemata_sqlSize));
|
|
|
|
createInformationSchemaView(context, information_schema_database, "tables", std::string_view(reinterpret_cast<const char *>(gresource_tables_sqlData), gresource_tables_sqlSize));
|
|
|
|
createInformationSchemaView(context, information_schema_database, "views", std::string_view(reinterpret_cast<const char *>(gresource_views_sqlData), gresource_views_sqlSize));
|
|
|
|
createInformationSchemaView(context, information_schema_database, "columns", std::string_view(reinterpret_cast<const char *>(gresource_columns_sqlData), gresource_columns_sqlSize));
|
2021-09-09 09:37:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|