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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
//! Synchronous schema, view, and flow operations.
use std::collections::HashMap;
use greptimedb_proto::v1::{
AlterDatabaseExpr, AlterTableExpr, CommentOnExpr, CreateDatabaseExpr, CreateFlowExpr,
CreateTableExpr, CreateViewExpr, DdlRequest, DropFlowExpr, DropTableExpr, DropViewExpr,
TruncateTableExpr, ddl_request, greptime_request,
};
use crate::{Client, Result};
impl Client {
/// Creates a schema in the client's catalog with the supplied database options.
///
/// # Errors
///
/// Returns an error if the RPC fails or GreptimeDB rejects the expression.
pub fn create_database(
&mut self,
schema_name: impl Into<String>,
create_if_not_exists: bool,
options: HashMap<String, String>,
) -> Result<u32> {
self.execute_ddl(ddl_request::Expr::CreateDatabase(CreateDatabaseExpr {
catalog_name: self.options().catalog.clone(),
schema_name: schema_name.into(),
create_if_not_exists,
options,
}))
}
/// Alters a database using the expression's catalog, schema, and operation.
///
/// # Errors
///
/// Returns an error if the RPC fails or GreptimeDB rejects the expression.
pub fn alter_database(&mut self, expression: AlterDatabaseExpr) -> Result<u32> {
self.execute_ddl(ddl_request::Expr::AlterDatabase(expression))
}
/// Creates a table using the expression's complete schema and storage options.
///
/// # Errors
///
/// Returns an error if the RPC fails or GreptimeDB rejects the expression.
pub fn create_table(&mut self, expression: CreateTableExpr) -> Result<u32> {
self.execute_ddl(ddl_request::Expr::CreateTable(expression))
}
/// Alters a table using the expression's catalog, schema, and operation.
///
/// # Errors
///
/// Returns an error if the RPC fails or GreptimeDB rejects the expression.
pub fn alter_table(&mut self, expression: AlterTableExpr) -> Result<u32> {
self.execute_ddl(ddl_request::Expr::AlterTable(expression))
}
/// Drops a table in the client's catalog and schema.
///
/// # Errors
///
/// Returns an error if the RPC fails or GreptimeDB rejects the expression.
pub fn drop_table(
&mut self,
table_name: impl Into<String>,
drop_if_exists: bool,
) -> Result<u32> {
self.execute_ddl(ddl_request::Expr::DropTable(DropTableExpr {
catalog_name: self.options().catalog.clone(),
schema_name: self.options().schema.clone(),
table_name: table_name.into(),
table_id: None,
drop_if_exists,
}))
}
/// Truncates the entire table in the client's catalog and schema.
///
/// Use [`Self::execute_ddl`] with a `TruncateTableExpr` to select time ranges.
///
/// # Errors
///
/// Returns an error if the RPC fails or GreptimeDB rejects the expression.
pub fn truncate_table(&mut self, table_name: impl Into<String>) -> Result<u32> {
self.execute_ddl(ddl_request::Expr::TruncateTable(TruncateTableExpr {
catalog_name: self.options().catalog.clone(),
schema_name: self.options().schema.clone(),
table_name: table_name.into(),
table_id: None,
time_ranges: None,
}))
}
/// Creates a view using the expression's plan, dependencies, and definition.
///
/// # Errors
///
/// Returns an error if the RPC fails or GreptimeDB rejects the expression.
pub fn create_view(&mut self, expression: CreateViewExpr) -> Result<u32> {
self.execute_ddl(ddl_request::Expr::CreateView(expression))
}
/// Drops a view in the client's catalog and schema.
///
/// # Errors
///
/// Returns an error if the RPC fails or GreptimeDB rejects the expression.
pub fn drop_view(&mut self, view_name: impl Into<String>, drop_if_exists: bool) -> Result<u32> {
self.execute_ddl(ddl_request::Expr::DropView(DropViewExpr {
catalog_name: self.options().catalog.clone(),
schema_name: self.options().schema.clone(),
view_name: view_name.into(),
view_id: None,
drop_if_exists,
}))
}
/// Creates a flow using the expression's sources, sink, and SQL.
///
/// # Errors
///
/// Returns an error if the RPC fails or GreptimeDB rejects the expression.
pub fn create_flow(&mut self, expression: CreateFlowExpr) -> Result<u32> {
self.execute_ddl(ddl_request::Expr::CreateFlow(expression))
}
/// Drops a flow in the client's catalog.
///
/// # Errors
///
/// Returns an error if the RPC fails or GreptimeDB rejects the expression.
pub fn drop_flow(&mut self, flow_name: impl Into<String>, drop_if_exists: bool) -> Result<u32> {
self.execute_ddl(ddl_request::Expr::DropFlow(DropFlowExpr {
catalog_name: self.options().catalog.clone(),
flow_name: flow_name.into(),
flow_id: None,
drop_if_exists,
}))
}
/// Sets a table, column, or flow comment using the supplied expression.
///
/// # Errors
///
/// Returns an error if the RPC fails or GreptimeDB rejects the expression.
pub fn comment_on(&mut self, expression: CommentOnExpr) -> Result<u32> {
self.execute_ddl(ddl_request::Expr::CommentOn(expression))
}
/// Executes a DDL expression and returns GreptimeDB's affected row count.
///
/// Expression fields are sent unchanged. The client supplies the request
/// header, authentication, and metadata according to its configuration.
///
/// # Errors
///
/// Returns an error for transport failures, gRPC errors, GreptimeDB status
/// errors, or a response without an affected row count.
pub fn execute_ddl(&mut self, expression: ddl_request::Expr) -> Result<u32> {
self.handle_request(
greptime_request::Request::Ddl(DdlRequest {
expr: Some(expression),
}),
None,
)
}
}