2019-01-23 19:28:13 +00:00
|
|
|
#include <Formats/FormatSchemaInfo.h>
|
|
|
|
#include <Poco/Path.h>
|
|
|
|
#include <Interpreters/Context.h>
|
|
|
|
#include <Common/Exception.h>
|
|
|
|
|
|
|
|
|
|
|
|
namespace DB
|
|
|
|
{
|
|
|
|
namespace ErrorCodes
|
|
|
|
{
|
|
|
|
extern const int BAD_ARGUMENTS;
|
|
|
|
}
|
|
|
|
|
|
|
|
FormatSchemaInfo::FormatSchemaInfo(const Context & context, const String & schema_file_extension, bool schema_required)
|
|
|
|
{
|
|
|
|
String format_schema = context.getSettingsRef().format_schema.toString();
|
|
|
|
if (format_schema.empty())
|
|
|
|
{
|
|
|
|
if (schema_required)
|
|
|
|
{
|
|
|
|
throw Exception(
|
|
|
|
"Format schema requires the 'format_schema' setting to have the 'schema_file:message_name' format"
|
2019-02-19 19:34:55 +00:00
|
|
|
+ (schema_file_extension.empty() ? "" : ", e.g. 'schema." + schema_file_extension + ":Message'"),
|
2019-01-23 19:28:13 +00:00
|
|
|
ErrorCodes::BAD_ARGUMENTS);
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
size_t colon_pos = format_schema.find(':');
|
2019-01-27 09:15:32 +00:00
|
|
|
Poco::Path path;
|
|
|
|
if ((colon_pos == String::npos) || (colon_pos == 0) || (colon_pos == format_schema.length() - 1)
|
2019-02-19 19:34:55 +00:00
|
|
|
|| path.assign(format_schema.substr(0, colon_pos)).makeFile().getFileName().empty())
|
2019-01-23 19:28:13 +00:00
|
|
|
{
|
|
|
|
throw Exception(
|
|
|
|
"Format schema requires the 'format_schema' setting to have the 'schema_file:message_name' format"
|
2019-02-19 19:34:55 +00:00
|
|
|
+ (schema_file_extension.empty() ? "" : ", e.g. 'schema." + schema_file_extension + ":Message'") + ". Got '" + format_schema
|
2019-01-23 19:28:13 +00:00
|
|
|
+ "'",
|
|
|
|
ErrorCodes::BAD_ARGUMENTS);
|
|
|
|
}
|
|
|
|
|
2019-01-27 09:15:32 +00:00
|
|
|
is_null = false;
|
|
|
|
message_name = format_schema.substr(colon_pos + 1);
|
|
|
|
|
|
|
|
auto default_schema_directory = [&context]()
|
2019-01-23 19:28:13 +00:00
|
|
|
{
|
2019-01-27 09:15:32 +00:00
|
|
|
static const String str = Poco::Path(context.getFormatSchemaPath()).makeAbsolute().makeDirectory().toString();
|
|
|
|
return str;
|
|
|
|
};
|
|
|
|
auto is_server = [&context]()
|
|
|
|
{
|
|
|
|
return context.hasGlobalContext() && (context.getGlobalContext().getApplicationType() == Context::ApplicationType::SERVER);
|
|
|
|
};
|
2019-01-23 19:28:13 +00:00
|
|
|
|
2019-01-27 09:15:32 +00:00
|
|
|
if (path.getExtension().empty() && !schema_file_extension.empty())
|
|
|
|
path.setExtension(schema_file_extension);
|
|
|
|
|
|
|
|
if (path.isAbsolute())
|
|
|
|
{
|
|
|
|
if (is_server())
|
|
|
|
throw Exception("Absolute path in the 'format_schema' setting is prohibited: " + path.toString(), ErrorCodes::BAD_ARGUMENTS);
|
|
|
|
schema_path = path.getFileName();
|
|
|
|
schema_directory = path.makeParent().toString();
|
|
|
|
}
|
|
|
|
else if (path.depth() >= 1 && path.directory(0) == "..")
|
|
|
|
{
|
|
|
|
if (is_server())
|
2019-01-23 19:28:13 +00:00
|
|
|
throw Exception(
|
|
|
|
"Path in the 'format_schema' setting shouldn't go outside the 'format_schema_path' directory: " + path.toString(),
|
|
|
|
ErrorCodes::BAD_ARGUMENTS);
|
2019-01-27 09:15:32 +00:00
|
|
|
path = Poco::Path(default_schema_directory()).resolve(path).toString();
|
|
|
|
schema_path = path.getFileName();
|
|
|
|
schema_directory = path.makeParent().toString();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
schema_path = path.toString();
|
|
|
|
schema_directory = default_schema_directory();
|
2019-01-23 19:28:13 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|