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;
|
|
|
|
}
|
|
|
|
|
2019-03-29 14:37:08 +00:00
|
|
|
|
|
|
|
namespace
|
2019-01-23 19:28:13 +00:00
|
|
|
{
|
2019-03-29 14:37:08 +00:00
|
|
|
String getFormatSchemaDefaultFileExtension(const String & format)
|
2019-01-23 19:28:13 +00:00
|
|
|
{
|
2019-03-29 14:37:08 +00:00
|
|
|
if (format == "Protobuf")
|
|
|
|
return "proto";
|
|
|
|
else if (format == "CapnProto")
|
|
|
|
return "capnp";
|
|
|
|
else
|
|
|
|
return "";
|
2019-01-23 19:28:13 +00:00
|
|
|
}
|
2019-03-29 14:37:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
FormatSchemaInfo::FormatSchemaInfo(const Context & context, const String & format)
|
|
|
|
{
|
|
|
|
String format_schema = context.getSettingsRef().format_schema.toString();
|
|
|
|
if (format_schema.empty())
|
|
|
|
throw Exception(
|
|
|
|
"The format " + format + " requires a schema. The 'format_schema' setting should be set", ErrorCodes::BAD_ARGUMENTS);
|
|
|
|
|
|
|
|
String default_file_extension = getFormatSchemaDefaultFileExtension(format);
|
2019-01-23 19:28:13 +00:00
|
|
|
|
|
|
|
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-03-29 14:37:08 +00:00
|
|
|
+ (default_file_extension.empty() ? "" : ", e.g. 'schema." + default_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
|
|
|
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-03-29 14:37:08 +00:00
|
|
|
if (path.getExtension().empty() && !default_file_extension.empty())
|
|
|
|
path.setExtension(default_file_extension);
|
2019-01-27 09:15:32 +00:00
|
|
|
|
|
|
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|