2019-10-23 13:46:38 +00:00
|
|
|
#include <Databases/DatabaseAtomic.h>
|
2019-09-26 13:43:08 +00:00
|
|
|
#include <Databases/DatabaseDictionary.h>
|
2017-04-01 09:19:00 +00:00
|
|
|
#include <Databases/DatabaseFactory.h>
|
2019-09-26 13:43:08 +00:00
|
|
|
#include <Databases/DatabaseLazy.h>
|
2017-04-01 09:19:00 +00:00
|
|
|
#include <Databases/DatabaseMemory.h>
|
2019-09-26 13:43:08 +00:00
|
|
|
#include <Databases/DatabaseOrdinary.h>
|
2019-06-10 09:40:33 +00:00
|
|
|
#include <Parsers/ASTLiteral.h>
|
2020-04-07 05:14:49 +00:00
|
|
|
#include <Parsers/ASTIdentifier.h>
|
2019-06-10 09:40:33 +00:00
|
|
|
#include <Parsers/formatAST.h>
|
2019-06-14 16:18:48 +00:00
|
|
|
#include <Parsers/ASTCreateQuery.h>
|
2019-08-27 20:43:08 +00:00
|
|
|
#include <Parsers/ASTFunction.h>
|
2019-06-10 09:40:33 +00:00
|
|
|
#include <Common/parseAddress.h>
|
2019-12-12 12:17:27 +00:00
|
|
|
#include "DatabaseFactory.h"
|
|
|
|
#include <Poco/File.h>
|
|
|
|
|
2020-04-16 12:31:57 +00:00
|
|
|
#if !defined(ARCADIA_BUILD)
|
|
|
|
# include "config_core.h"
|
|
|
|
#endif
|
2019-06-13 08:19:26 +00:00
|
|
|
|
2020-04-16 12:31:57 +00:00
|
|
|
#if USE_MYSQL
|
|
|
|
# include <Databases/DatabaseMySQL.h>
|
|
|
|
# include <Interpreters/evaluateConstantExpression.h>
|
2019-06-13 08:19:26 +00:00
|
|
|
#endif
|
2019-06-10 09:40:33 +00:00
|
|
|
|
2016-03-19 01:18:49 +00:00
|
|
|
|
|
|
|
namespace DB
|
|
|
|
{
|
|
|
|
|
|
|
|
namespace ErrorCodes
|
|
|
|
{
|
2020-02-25 18:02:41 +00:00
|
|
|
extern const int UNKNOWN_ELEMENT_IN_AST;
|
2019-12-12 12:17:27 +00:00
|
|
|
extern const int BAD_ARGUMENTS;
|
|
|
|
extern const int UNKNOWN_DATABASE_ENGINE;
|
|
|
|
extern const int CANNOT_CREATE_DATABASE;
|
2016-03-19 01:18:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
DatabasePtr DatabaseFactory::get(
|
2019-12-12 12:17:27 +00:00
|
|
|
const String & database_name, const String & metadata_path, const ASTStorage * engine_define, Context & context)
|
|
|
|
{
|
2020-04-22 19:37:30 +00:00
|
|
|
bool created = false;
|
2020-04-22 16:03:58 +00:00
|
|
|
|
2019-12-12 12:17:27 +00:00
|
|
|
try
|
|
|
|
{
|
2020-04-22 19:37:30 +00:00
|
|
|
created = Poco::File(metadata_path).createDirectory();
|
2019-12-12 12:17:27 +00:00
|
|
|
return getImpl(database_name, metadata_path, engine_define, context);
|
|
|
|
}
|
|
|
|
catch (...)
|
|
|
|
{
|
|
|
|
Poco::File metadata_dir(metadata_path);
|
|
|
|
|
2020-04-22 19:37:30 +00:00
|
|
|
if (created && metadata_dir.exists())
|
2019-12-12 12:17:27 +00:00
|
|
|
metadata_dir.remove(true);
|
|
|
|
|
|
|
|
throw;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-07 05:14:49 +00:00
|
|
|
template <typename ValueType>
|
2020-04-08 05:41:11 +00:00
|
|
|
static inline ValueType safeGetLiteralValue(const ASTPtr &ast, const String &engine_name)
|
2020-04-07 05:14:49 +00:00
|
|
|
{
|
|
|
|
if (!ast || !ast->as<ASTLiteral>())
|
|
|
|
throw Exception("Database engine " + engine_name + " requested literal argument.", ErrorCodes::BAD_ARGUMENTS);
|
|
|
|
|
|
|
|
return ast->as<ASTLiteral>()->value.safeGet<ValueType>();
|
|
|
|
}
|
|
|
|
|
2019-12-12 12:17:27 +00:00
|
|
|
DatabasePtr DatabaseFactory::getImpl(
|
|
|
|
const String & database_name, const String & metadata_path, const ASTStorage * engine_define, Context & context)
|
2016-03-19 01:18:49 +00:00
|
|
|
{
|
2019-06-10 09:40:33 +00:00
|
|
|
String engine_name = engine_define->engine->name;
|
|
|
|
|
2019-10-01 10:24:09 +00:00
|
|
|
if (engine_name != "MySQL" && engine_name != "Lazy" && engine_define->engine->arguments)
|
2019-06-14 16:18:48 +00:00
|
|
|
throw Exception("Database engine " + engine_name + " cannot have arguments", ErrorCodes::BAD_ARGUMENTS);
|
|
|
|
|
|
|
|
if (engine_define->engine->parameters || engine_define->partition_by || engine_define->primary_key || engine_define->order_by ||
|
|
|
|
engine_define->sample_by || engine_define->settings)
|
|
|
|
throw Exception("Database engine " + engine_name + " cannot have parameters, primary_key, order_by, sample_by, settings",
|
|
|
|
ErrorCodes::UNKNOWN_ELEMENT_IN_AST);
|
2019-06-10 09:40:33 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
if (engine_name == "Ordinary")
|
2017-11-03 19:53:10 +00:00
|
|
|
return std::make_shared<DatabaseOrdinary>(database_name, metadata_path, context);
|
2019-10-23 13:46:38 +00:00
|
|
|
else if (engine_name == "Atomic")
|
|
|
|
return std::make_shared<DatabaseAtomic>(database_name, metadata_path, context);
|
2017-04-01 07:20:54 +00:00
|
|
|
else if (engine_name == "Memory")
|
2020-05-28 20:10:45 +00:00
|
|
|
return std::make_shared<DatabaseMemory>(database_name, context);
|
2017-06-22 15:44:19 +00:00
|
|
|
else if (engine_name == "Dictionary")
|
2020-04-01 22:41:29 +00:00
|
|
|
return std::make_shared<DatabaseDictionary>(database_name, context);
|
2019-06-13 08:19:26 +00:00
|
|
|
|
|
|
|
#if USE_MYSQL
|
|
|
|
|
2019-06-10 09:40:33 +00:00
|
|
|
else if (engine_name == "MySQL")
|
|
|
|
{
|
2019-06-14 16:18:48 +00:00
|
|
|
const ASTFunction * engine = engine_define->engine;
|
2019-06-10 09:40:33 +00:00
|
|
|
|
2019-10-22 10:47:43 +00:00
|
|
|
if (!engine->arguments || engine->arguments->children.size() != 4)
|
2019-12-12 12:17:27 +00:00
|
|
|
throw Exception("MySQL Database require mysql_hostname, mysql_database_name, mysql_username, mysql_password arguments.",
|
|
|
|
ErrorCodes::BAD_ARGUMENTS);
|
2019-06-10 09:40:33 +00:00
|
|
|
|
2020-04-07 05:14:49 +00:00
|
|
|
|
2020-04-08 05:41:11 +00:00
|
|
|
ASTs & arguments = engine->arguments->children;
|
|
|
|
arguments[1] = evaluateConstantExpressionOrIdentifierAsLiteral(arguments[1], context);
|
|
|
|
|
|
|
|
const auto & host_name_and_port = safeGetLiteralValue<String>(arguments[0], "MySQL");
|
|
|
|
const auto & database_name_in_mysql = safeGetLiteralValue<String>(arguments[1], "MySQL");
|
|
|
|
const auto & mysql_user_name = safeGetLiteralValue<String>(arguments[2], "MySQL");
|
|
|
|
const auto & mysql_user_password = safeGetLiteralValue<String>(arguments[3], "MySQL");
|
2019-06-10 09:40:33 +00:00
|
|
|
|
2019-12-12 12:17:27 +00:00
|
|
|
try
|
|
|
|
{
|
|
|
|
const auto & [remote_host_name, remote_port] = parseAddress(host_name_and_port, 3306);
|
|
|
|
auto mysql_pool = mysqlxx::Pool(database_name_in_mysql, remote_host_name, mysql_user_name, mysql_user_password, remote_port);
|
|
|
|
|
|
|
|
auto mysql_database = std::make_shared<DatabaseMySQL>(
|
|
|
|
context, database_name, metadata_path, engine_define, database_name_in_mysql, std::move(mysql_pool));
|
|
|
|
|
2020-04-23 16:51:48 +00:00
|
|
|
mysql_database->empty(); /// test database is works fine.
|
2019-12-12 12:17:27 +00:00
|
|
|
return mysql_database;
|
|
|
|
}
|
|
|
|
catch (...)
|
|
|
|
{
|
|
|
|
const auto & exception_message = getCurrentExceptionMessage(true);
|
|
|
|
throw Exception("Cannot create MySQL database, because " + exception_message, ErrorCodes::CANNOT_CREATE_DATABASE);
|
|
|
|
}
|
2019-06-10 09:40:33 +00:00
|
|
|
}
|
|
|
|
|
2019-06-13 08:19:26 +00:00
|
|
|
#endif
|
2016-03-19 01:18:49 +00:00
|
|
|
|
2019-09-26 13:43:08 +00:00
|
|
|
else if (engine_name == "Lazy")
|
|
|
|
{
|
|
|
|
const ASTFunction * engine = engine_define->engine;
|
|
|
|
|
2019-10-22 10:47:43 +00:00
|
|
|
if (!engine->arguments || engine->arguments->children.size() != 1)
|
|
|
|
throw Exception("Lazy database require cache_expiration_time_seconds argument", ErrorCodes::BAD_ARGUMENTS);
|
2019-09-26 13:43:08 +00:00
|
|
|
|
2019-10-22 10:47:43 +00:00
|
|
|
const auto & arguments = engine->arguments->children;
|
2019-09-26 13:43:08 +00:00
|
|
|
|
2020-04-08 05:41:11 +00:00
|
|
|
const auto cache_expiration_time_seconds = safeGetLiteralValue<UInt64>(arguments[0], "Lazy");
|
2019-09-26 13:43:08 +00:00
|
|
|
return std::make_shared<DatabaseLazy>(database_name, metadata_path, cache_expiration_time_seconds, context);
|
|
|
|
}
|
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
throw Exception("Unknown database engine: " + engine_name, ErrorCodes::UNKNOWN_DATABASE_ENGINE);
|
2016-03-19 01:18:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|