2011-08-09 19:19:25 +00:00
|
|
|
|
#pragma once
|
|
|
|
|
|
|
|
|
|
#include <map>
|
2011-08-18 20:33:20 +00:00
|
|
|
|
#include <set>
|
2011-08-09 19:19:25 +00:00
|
|
|
|
|
2011-08-19 18:31:14 +00:00
|
|
|
|
#include <Poco/SharedPtr.h>
|
|
|
|
|
#include <Poco/Mutex.h>
|
|
|
|
|
|
2011-08-09 19:19:25 +00:00
|
|
|
|
#include <DB/Core/NamesAndTypes.h>
|
2012-03-11 08:52:56 +00:00
|
|
|
|
#include <DB/DataStreams/FormatFactory.h>
|
2011-08-18 20:33:20 +00:00
|
|
|
|
#include <DB/Storages/IStorage.h>
|
2012-03-09 03:06:09 +00:00
|
|
|
|
#include <DB/Functions/FunctionsLibrary.h>
|
2011-09-19 01:42:16 +00:00
|
|
|
|
#include <DB/AggregateFunctions/AggregateFunctionFactory.h>
|
2011-08-18 20:33:20 +00:00
|
|
|
|
#include <DB/DataTypes/DataTypeFactory.h>
|
2011-10-31 17:30:44 +00:00
|
|
|
|
#include <DB/Storages/StorageFactory.h>
|
2012-03-05 00:09:41 +00:00
|
|
|
|
#include <DB/Interpreters/Settings.h>
|
2011-08-09 19:19:25 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
namespace DB
|
|
|
|
|
{
|
|
|
|
|
|
2011-08-19 18:31:14 +00:00
|
|
|
|
using Poco::SharedPtr;
|
|
|
|
|
|
2011-08-18 20:33:20 +00:00
|
|
|
|
/// имя таблицы -> таблица
|
|
|
|
|
typedef std::map<String, StoragePtr> Tables;
|
|
|
|
|
|
|
|
|
|
/// имя БД -> таблицы
|
|
|
|
|
typedef std::map<String, Tables> Databases;
|
|
|
|
|
|
|
|
|
|
|
2011-08-09 19:19:25 +00:00
|
|
|
|
/** Набор известных объектов, которые могут быть использованы в запросе.
|
|
|
|
|
*/
|
|
|
|
|
struct Context
|
|
|
|
|
{
|
2011-09-19 01:42:16 +00:00
|
|
|
|
String path; /// Путь к директории с данными, со слешем на конце.
|
|
|
|
|
SharedPtr<Databases> databases; /// Список БД и таблиц в них.
|
|
|
|
|
String current_database; /// Текущая БД.
|
|
|
|
|
SharedPtr<Functions> functions; /// Обычные функции.
|
|
|
|
|
AggregateFunctionFactoryPtr aggregate_function_factory; /// Агрегатные функции.
|
|
|
|
|
DataTypeFactoryPtr data_type_factory; /// Типы данных.
|
2011-10-31 17:30:44 +00:00
|
|
|
|
StorageFactoryPtr storage_factory; /// Движки таблиц.
|
2012-03-11 08:52:56 +00:00
|
|
|
|
FormatFactoryPtr format_factory; /// Форматы.
|
2011-11-03 20:30:12 +00:00
|
|
|
|
NamesAndTypesList columns; /// Столбцы текущей обрабатываемой таблицы.
|
2012-03-05 00:09:41 +00:00
|
|
|
|
Settings settings; /// Настройки выполнения запроса.
|
2012-03-09 03:56:12 +00:00
|
|
|
|
Logger * log; /// Логгер.
|
2011-08-19 18:31:14 +00:00
|
|
|
|
|
2012-05-30 05:53:09 +00:00
|
|
|
|
mutable SharedPtr<Poco::Mutex> mutex; /// Для доступа и модификации разделяемых объектов.
|
2011-08-19 18:31:14 +00:00
|
|
|
|
|
2012-05-30 05:53:09 +00:00
|
|
|
|
Context() : databases(new Databases), functions(new Functions), log(&Logger::get("Context")), mutex(new Poco::Mutex) {}
|
2012-03-09 03:06:09 +00:00
|
|
|
|
|
|
|
|
|
/** В сервере есть глобальный контекст.
|
|
|
|
|
* При соединении, он копируется в контекст сессии.
|
|
|
|
|
* Для каждого запроса, контекст сессии копируется в контекст запроса.
|
|
|
|
|
* Блокировка нужна, так как запрос может модифицировать глобальный контекст (SET GLOBAL ...).
|
|
|
|
|
*/
|
|
|
|
|
Context(const Context & rhs)
|
|
|
|
|
{
|
2012-05-30 05:53:09 +00:00
|
|
|
|
Poco::ScopedLock<Poco::Mutex> lock(*rhs.mutex);
|
2012-03-09 03:06:09 +00:00
|
|
|
|
|
|
|
|
|
path = rhs.path;
|
|
|
|
|
databases = rhs.databases;
|
|
|
|
|
current_database = rhs.current_database;
|
|
|
|
|
functions = rhs.functions;
|
|
|
|
|
aggregate_function_factory = rhs.aggregate_function_factory;
|
|
|
|
|
data_type_factory = rhs.data_type_factory;
|
|
|
|
|
storage_factory = rhs.storage_factory;
|
|
|
|
|
columns = rhs.columns;
|
|
|
|
|
settings = rhs.settings;
|
2012-03-09 03:56:12 +00:00
|
|
|
|
log = rhs.log;
|
2012-03-09 03:06:09 +00:00
|
|
|
|
mutex = rhs.mutex;
|
|
|
|
|
}
|
2012-06-18 06:19:13 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/// Проверка существования таблицы. database может быть пустой - в этом случае используется текущая БД.
|
2012-06-18 07:49:19 +00:00
|
|
|
|
bool isTableExist(const String & database_name, const String & table_name)
|
2012-06-18 06:19:13 +00:00
|
|
|
|
{
|
|
|
|
|
Poco::ScopedLock<Poco::Mutex> lock(*mutex);
|
|
|
|
|
|
|
|
|
|
String db = database_name.empty() ? current_database : database_name;
|
|
|
|
|
|
|
|
|
|
return databases->end() == databases->find(db)
|
|
|
|
|
|| (*databases)[db].end() == (*databases)[db].find(table_name);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2012-06-18 07:49:19 +00:00
|
|
|
|
void assertTableExists(const String & database_name, const String & table_name)
|
2012-06-18 06:19:13 +00:00
|
|
|
|
{
|
|
|
|
|
Poco::ScopedLock<Poco::Mutex> lock(*mutex);
|
|
|
|
|
|
|
|
|
|
String db = database_name.empty() ? current_database : database_name;
|
|
|
|
|
|
|
|
|
|
if (databases->end() == databases->find(db))
|
|
|
|
|
throw Exception("Database " + db + " doesn't exist", ErrorCodes::UNKNOWN_DATABASE);
|
|
|
|
|
|
|
|
|
|
if ((*databases)[db].end() == (*databases)[db].find(table_name))
|
|
|
|
|
throw Exception("Table " + db + "." + table_name + " doesn't exist.", ErrorCodes::UNKNOWN_TABLE);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2012-06-18 07:49:19 +00:00
|
|
|
|
void assertTableDoesntExist(const String & database_name, const String & table_name)
|
2012-06-18 06:19:13 +00:00
|
|
|
|
{
|
|
|
|
|
Poco::ScopedLock<Poco::Mutex> lock(*mutex);
|
|
|
|
|
|
|
|
|
|
String db = database_name.empty() ? current_database : database_name;
|
|
|
|
|
|
|
|
|
|
if (databases->end() != databases->find(db)
|
|
|
|
|
&& (*databases)[db].end() != (*databases)[db].find(table_name))
|
|
|
|
|
throw Exception("Table " + db + "." + table_name + " already exists.", ErrorCodes::TABLE_ALREADY_EXISTS);
|
|
|
|
|
}
|
2012-06-18 07:49:19 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void assertDatabaseExists(const String & database_name)
|
|
|
|
|
{
|
|
|
|
|
Poco::ScopedLock<Poco::Mutex> lock(*mutex);
|
|
|
|
|
|
|
|
|
|
String db = database_name.empty() ? current_database : database_name;
|
|
|
|
|
|
|
|
|
|
if (databases->end() == databases->find(db))
|
|
|
|
|
throw Exception("Database " + db + " doesn't exist", ErrorCodes::UNKNOWN_DATABASE);
|
|
|
|
|
}
|
2011-08-09 19:19:25 +00:00
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
}
|