Repository navigation
Expand file tree
/
Copy pathTableFunctionSQLite.cpp
More file actions
103 lines (71 loc) · 3.04 KB
/
Copy pathTableFunctionSQLite.cpp
File metadata and controls
103 lines (71 loc) · 3.04 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
#include "config.h"
#if USE_SQLITE
#include <Common/Exception.h>
#include <TableFunctions/ITableFunction.h>
#include <Storages/StorageSQLite.h>
#include <Databases/SQLite/SQLiteUtils.h>
#include <TableFunctions/registerTableFunctions.h>
#include <Interpreters/evaluateConstantExpression.h>
#include <Parsers/ASTFunction.h>
#include <TableFunctions/TableFunctionFactory.h>
#include <Storages/checkAndGetLiteralArgument.h>
namespace DB
{
namespace ErrorCodes
{
extern const int NUMBER_OF_ARGUMENTS_DOESNT_MATCH;
extern const int BAD_ARGUMENTS;
}
namespace
{
class TableFunctionSQLite : public ITableFunction
{
public:
static constexpr auto name = "sqlite";
std::string getName() const override { return name; }
private:
StoragePtr executeImpl(
const ASTPtr & ast_function, ContextPtr context,
const std::string & table_name, ColumnsDescription cached_columns, bool is_insert_query) const override;
const char * getStorageTypeName() const override { return "SQLite"; }
ColumnsDescription getActualTableStructure(ContextPtr context, bool is_insert_query) const override;
void parseArguments(const ASTPtr & ast_function, ContextPtr context) override;
String database_path, remote_table_name;
std::shared_ptr<sqlite3> sqlite_db;
};
StoragePtr TableFunctionSQLite::executeImpl(const ASTPtr & /*ast_function*/,
ContextPtr context, const String & table_name, ColumnsDescription cached_columns, bool /*is_insert_query*/) const
{
auto storage = std::make_shared<StorageSQLite>(StorageID(getDatabaseName(), table_name),
sqlite_db,
database_path,
remote_table_name,
cached_columns, ConstraintsDescription{}, /* comment = */ "", context);
storage->startup();
return storage;
}
ColumnsDescription TableFunctionSQLite::getActualTableStructure(ContextPtr /* context */, bool /*is_insert_query*/) const
{
return StorageSQLite::getTableStructureFromData(sqlite_db, remote_table_name);
}
void TableFunctionSQLite::parseArguments(const ASTPtr & ast_function, ContextPtr context)
{
const auto & func_args = ast_function->as<ASTFunction &>();
if (!func_args.arguments)
throw Exception(ErrorCodes::BAD_ARGUMENTS, "Table function 'sqlite' must have arguments.");
ASTs & args = func_args.arguments->children;
if (args.size() != 2)
throw Exception(ErrorCodes::NUMBER_OF_ARGUMENTS_DOESNT_MATCH, "SQLite database requires 2 arguments: database path, table name");
for (auto & arg : args)
arg = evaluateConstantExpressionOrIdentifierAsLiteral(arg, context);
database_path = checkAndGetLiteralArgument<String>(args[0], "database_path");
remote_table_name = checkAndGetLiteralArgument<String>(args[1], "table_name");
sqlite_db = openSQLiteDB(database_path, context);
}
}
void registerTableFunctionSQLite(TableFunctionFactory & factory)
{
factory.registerFunction<TableFunctionSQLite>();
}
}
#endif