1 | //===----------------------------------------------------------------------===// |
2 | // DuckDB |
3 | // |
4 | // duckdb/main/connection.hpp |
5 | // |
6 | // |
7 | //===----------------------------------------------------------------------===// |
8 | |
9 | #pragma once |
10 | |
11 | #include "duckdb/main/materialized_query_result.hpp" |
12 | #include "duckdb/main/query_result.hpp" |
13 | #include "duckdb/main/stream_query_result.hpp" |
14 | #include "duckdb/main/prepared_statement.hpp" |
15 | #include "duckdb/main/table_description.hpp" |
16 | #include "duckdb/main/relation.hpp" |
17 | #include "duckdb/common/enums/profiler_format.hpp" |
18 | #include "duckdb/parser/sql_statement.hpp" |
19 | |
20 | namespace duckdb { |
21 | |
22 | class ClientContext; |
23 | class DuckDB; |
24 | |
25 | typedef void (*warning_callback)(std::string); |
26 | |
27 | //! A connection to a database. This represents a (client) connection that can |
28 | //! be used to query the database. |
29 | class Connection { |
30 | public: |
31 | Connection(DuckDB &database); |
32 | ~Connection(); |
33 | |
34 | DuckDB &db; |
35 | unique_ptr<ClientContext> context; |
36 | warning_callback warning_cb; |
37 | |
38 | public: |
39 | //! Returns query profiling information for the current query |
40 | string GetProfilingInformation(ProfilerPrintFormat format = ProfilerPrintFormat::QUERY_TREE); |
41 | |
42 | //! Interrupt execution of the current query |
43 | void Interrupt(); |
44 | |
45 | //! Enable query profiling |
46 | void EnableProfiling(); |
47 | //! Disable query profiling |
48 | void DisableProfiling(); |
49 | |
50 | void SetWarningCallback(warning_callback); |
51 | |
52 | //! Enable aggressive verification/testing of queries, should only be used in testing |
53 | void EnableQueryVerification(); |
54 | void DisableQueryVerification(); |
55 | |
56 | //! Issues a query to the database and returns a QueryResult. This result can be either a StreamQueryResult or a |
57 | //! MaterializedQueryResult. The result can be stepped through with calls to Fetch(). Note that there can only be |
58 | //! one active StreamQueryResult per Connection object. Calling SendQuery() will invalidate any previously existing |
59 | //! StreamQueryResult. |
60 | unique_ptr<QueryResult> SendQuery(string query); |
61 | //! Issues a query to the database and materializes the result (if necessary). Always returns a |
62 | //! MaterializedQueryResult. |
63 | unique_ptr<MaterializedQueryResult> Query(string query); |
64 | // prepared statements |
65 | template <typename... Args> unique_ptr<QueryResult> Query(string query, Args... args) { |
66 | vector<Value> values; |
67 | return QueryParamsRecursive(query, values, args...); |
68 | } |
69 | |
70 | //! Prepare the specified query, returning a prepared statement object |
71 | unique_ptr<PreparedStatement> Prepare(string query); |
72 | |
73 | //! Get the table info of a specific table (in the default schema), or nullptr if it cannot be found |
74 | unique_ptr<TableDescription> TableInfo(string table_name); |
75 | //! Get the table info of a specific table, or nullptr if it cannot be found |
76 | unique_ptr<TableDescription> TableInfo(string schema_name, string table_name); |
77 | |
78 | //! Extract a set of SQL statements from a specific query |
79 | vector<unique_ptr<SQLStatement>> (string query); |
80 | |
81 | //! Appends a DataChunk to the specified table |
82 | void Append(TableDescription &description, DataChunk &chunk); |
83 | |
84 | //! Returns a relation that produces a table from this connection |
85 | shared_ptr<Relation> Table(string tname); |
86 | shared_ptr<Relation> Table(string schema_name, string table_name); |
87 | //! Returns a relation that produces a view from this connection |
88 | shared_ptr<Relation> View(string tname); |
89 | shared_ptr<Relation> View(string schema_name, string table_name); |
90 | //! Returns a relation that calls a specified table function |
91 | shared_ptr<Relation> TableFunction(string tname); |
92 | shared_ptr<Relation> TableFunction(string tname, vector<Value> values); |
93 | //! Returns a relation that produces values |
94 | shared_ptr<Relation> Values(vector<vector<Value>> values); |
95 | shared_ptr<Relation> Values(vector<vector<Value>> values, vector<string> column_names, string alias = "values" ); |
96 | shared_ptr<Relation> Values(string values); |
97 | shared_ptr<Relation> Values(string values, vector<string> column_names, string alias = "values" ); |
98 | //! Reads CSV file |
99 | shared_ptr<Relation> ReadCSV(string csv_file, vector<string> columns); |
100 | |
101 | void BeginTransaction(); |
102 | void Commit(); |
103 | void Rollback(); |
104 | |
105 | private: |
106 | unique_ptr<QueryResult> QueryParamsRecursive(string query, vector<Value> &values); |
107 | |
108 | template <typename T, typename... Args> |
109 | unique_ptr<QueryResult> QueryParamsRecursive(string query, vector<Value> &values, T value, Args... args) { |
110 | values.push_back(Value::CreateValue<T>(value)); |
111 | return QueryParamsRecursive(query, values, args...); |
112 | } |
113 | }; |
114 | |
115 | } // namespace duckdb |
116 | |