2021-07-12 21:07:11 +00:00
|
|
|
#include <TableFunctions/TableFunctionSQLite.h>
|
|
|
|
|
|
|
|
#if USE_SQLITE
|
|
|
|
|
|
|
|
#include <Common/Exception.h>
|
|
|
|
#include <Common/quoteString.h>
|
|
|
|
|
2021-07-13 09:39:36 +00:00
|
|
|
#include <Databases/SQLite/fetchSQLiteTableStructure.h>
|
2021-07-14 13:13:37 +00:00
|
|
|
#include <Databases/SQLite/SQLiteUtils.h>
|
2021-07-12 21:07:11 +00:00
|
|
|
#include "registerTableFunctions.h"
|
|
|
|
|
|
|
|
#include <Interpreters/evaluateConstantExpression.h>
|
2021-07-13 12:38:37 +00:00
|
|
|
#include <Interpreters/Context.h>
|
|
|
|
|
2021-07-12 21:07:11 +00:00
|
|
|
#include <Parsers/ASTFunction.h>
|
|
|
|
#include <Parsers/ASTLiteral.h>
|
|
|
|
|
|
|
|
#include <TableFunctions/ITableFunction.h>
|
|
|
|
#include <TableFunctions/TableFunctionFactory.h>
|
2021-07-13 09:39:36 +00:00
|
|
|
|
2021-07-12 21:07:11 +00:00
|
|
|
|
|
|
|
namespace DB
|
|
|
|
{
|
|
|
|
|
|
|
|
namespace ErrorCodes
|
|
|
|
{
|
|
|
|
extern const int NUMBER_OF_ARGUMENTS_DOESNT_MATCH;
|
|
|
|
extern const int BAD_ARGUMENTS;
|
|
|
|
extern const int SQLITE_ENGINE_ERROR;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
StoragePtr TableFunctionSQLite::executeImpl(const ASTPtr & /*ast_function*/,
|
|
|
|
ContextPtr context, const String & table_name, ColumnsDescription /*cached_columns*/) const
|
|
|
|
{
|
|
|
|
auto columns = getActualTableStructure(context);
|
|
|
|
|
|
|
|
auto storage = StorageSQLite::create(StorageID(getDatabaseName(), table_name),
|
|
|
|
sqlite_db,
|
2021-07-26 07:05:28 +00:00
|
|
|
database_path,
|
2021-07-12 21:07:11 +00:00
|
|
|
remote_table_name,
|
|
|
|
columns, ConstraintsDescription{}, context);
|
|
|
|
|
|
|
|
storage->startup();
|
|
|
|
return storage;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
ColumnsDescription TableFunctionSQLite::getActualTableStructure(ContextPtr /* context */) const
|
|
|
|
{
|
|
|
|
auto columns = fetchSQLiteTableStructure(sqlite_db.get(), remote_table_name);
|
2021-07-12 21:09:34 +00:00
|
|
|
|
|
|
|
if (!columns)
|
2021-07-13 00:07:53 +00:00
|
|
|
throw Exception(ErrorCodes::SQLITE_ENGINE_ERROR, "Failed to fetch table structure for {}", remote_table_name);
|
2021-07-12 21:09:34 +00:00
|
|
|
|
2021-07-12 21:07:11 +00:00
|
|
|
return ColumnsDescription{*columns};
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void TableFunctionSQLite::parseArguments(const ASTPtr & ast_function, ContextPtr context)
|
|
|
|
{
|
|
|
|
const auto & func_args = ast_function->as<ASTFunction &>();
|
|
|
|
|
|
|
|
if (!func_args.arguments)
|
|
|
|
throw Exception("Table function 'sqlite' must have arguments.", ErrorCodes::BAD_ARGUMENTS);
|
|
|
|
|
|
|
|
ASTs & args = func_args.arguments->children;
|
|
|
|
|
|
|
|
if (args.size() != 2)
|
|
|
|
throw Exception("SQLite database requires 2 arguments: database path, table name",
|
|
|
|
ErrorCodes::NUMBER_OF_ARGUMENTS_DOESNT_MATCH);
|
|
|
|
|
|
|
|
for (auto & arg : args)
|
|
|
|
arg = evaluateConstantExpressionOrIdentifierAsLiteral(arg, context);
|
|
|
|
|
|
|
|
database_path = args[0]->as<ASTLiteral &>().value.safeGet<String>();
|
|
|
|
remote_table_name = args[1]->as<ASTLiteral &>().value.safeGet<String>();
|
|
|
|
|
2021-07-14 13:13:37 +00:00
|
|
|
sqlite_db = openSQLiteDB(database_path, context);
|
2021-07-12 21:07:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void registerTableFunctionSQLite(TableFunctionFactory & factory)
|
|
|
|
{
|
|
|
|
factory.registerFunction<TableFunctionSQLite>();
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|