1//===----------------------------------------------------------------------===//
2// DuckDB
3//
4// duckdb/parser/sql_statement.hpp
5//
6//
7//===----------------------------------------------------------------------===//
8
9#pragma once
10
11#include "duckdb/common/common.hpp"
12#include "duckdb/common/enums/statement_type.hpp"
13#include "duckdb/common/exception.hpp"
14#include "duckdb/common/printer.hpp"
15#include "duckdb/common/named_parameter_map.hpp"
16
17namespace duckdb {
18
19//! SQLStatement is the base class of any type of SQL statement.
20class SQLStatement {
21public:
22 static constexpr const StatementType TYPE = StatementType::INVALID_STATEMENT;
23
24public:
25 explicit SQLStatement(StatementType type) : type(type) {
26 }
27 virtual ~SQLStatement() {
28 }
29
30 //! The statement type
31 StatementType type;
32 //! The statement location within the query string
33 idx_t stmt_location = 0;
34 //! The statement length within the query string
35 idx_t stmt_length = 0;
36 //! The number of prepared statement parameters (if any)
37 idx_t n_param = 0;
38 //! The map of named parameter to param index (if n_param and any named)
39 case_insensitive_map_t<idx_t> named_param_map;
40 //! The query text that corresponds to this SQL statement
41 string query;
42
43protected:
44 SQLStatement(const SQLStatement &other) = default;
45
46public:
47 virtual string ToString() const {
48 throw InternalException("ToString not supported for this type of SQLStatement: '%s'",
49 StatementTypeToString(type));
50 }
51 //! Create a copy of this SelectStatement
52 DUCKDB_API virtual unique_ptr<SQLStatement> Copy() const = 0;
53
54public:
55public:
56 template <class TARGET>
57 TARGET &Cast() {
58 if (type != TARGET::TYPE && TARGET::TYPE != StatementType::INVALID_STATEMENT) {
59 throw InternalException("Failed to cast statement to type - statement type mismatch");
60 }
61 return reinterpret_cast<TARGET &>(*this);
62 }
63
64 template <class TARGET>
65 const TARGET &Cast() const {
66 if (type != TARGET::TYPE && TARGET::TYPE != StatementType::INVALID_STATEMENT) {
67 throw InternalException("Failed to cast statement to type - statement type mismatch");
68 }
69 return reinterpret_cast<const TARGET &>(*this);
70 }
71};
72} // namespace duckdb
73