/Users/deen/code/yugabyte-db/src/yb/yql/pggate/pg_ddl.h
Line | Count | Source (jump to first uncovered line) |
1 | | //-------------------------------------------------------------------------------------------------- |
2 | | // Copyright (c) YugaByte, Inc. |
3 | | // |
4 | | // Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except |
5 | | // in compliance with the License. You may obtain a copy of the License at |
6 | | // |
7 | | // http://www.apache.org/licenses/LICENSE-2.0 |
8 | | // |
9 | | // Unless required by applicable law or agreed to in writing, software distributed under the License |
10 | | // is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express |
11 | | // or implied. See the License for the specific language governing permissions and limitations |
12 | | // under the License. |
13 | | //-------------------------------------------------------------------------------------------------- |
14 | | |
15 | | #ifndef YB_YQL_PGGATE_PG_DDL_H_ |
16 | | #define YB_YQL_PGGATE_PG_DDL_H_ |
17 | | |
18 | | #include "yb/common/transaction.h" |
19 | | |
20 | | #include "yb/tserver/pg_client.pb.h" |
21 | | |
22 | | #include "yb/yql/pggate/pg_statement.h" |
23 | | |
24 | | namespace yb { |
25 | | namespace pggate { |
26 | | |
27 | | class PgDdl : public PgStatement { |
28 | | public: |
29 | 3.33k | explicit PgDdl(PgSession::ScopedRefPtr pg_session) : PgStatement(pg_session) { |
30 | 3.33k | } |
31 | | }; |
32 | | |
33 | | //-------------------------------------------------------------------------------------------------- |
34 | | // CREATE DATABASE |
35 | | //-------------------------------------------------------------------------------------------------- |
36 | | |
37 | | class PgCreateDatabase : public PgDdl { |
38 | | public: |
39 | | PgCreateDatabase(PgSession::ScopedRefPtr pg_session, |
40 | | const char *database_name, |
41 | | PgOid database_oid, |
42 | | PgOid source_database_oid, |
43 | | PgOid next_oid, |
44 | | const bool colocated); |
45 | | virtual ~PgCreateDatabase(); |
46 | | |
47 | 22 | void UseTransaction() { |
48 | 22 | req_.set_use_transaction(true); |
49 | 22 | } |
50 | | |
51 | 22 | StmtOp stmt_op() const override { return StmtOp::STMT_CREATE_DATABASE; } |
52 | | |
53 | | // Execute. |
54 | | CHECKED_STATUS Exec(); |
55 | | |
56 | | private: |
57 | | tserver::PgCreateDatabaseRequestPB req_; |
58 | | }; |
59 | | |
60 | | class PgDropDatabase : public PgDdl { |
61 | | public: |
62 | | PgDropDatabase(PgSession::ScopedRefPtr pg_session, const char *database_name, PgOid database_oid); |
63 | | virtual ~PgDropDatabase(); |
64 | | |
65 | 21 | StmtOp stmt_op() const override { return StmtOp::STMT_DROP_DATABASE; } |
66 | | |
67 | | // Execute. |
68 | | CHECKED_STATUS Exec(); |
69 | | |
70 | | private: |
71 | | const char *database_name_; |
72 | | const PgOid database_oid_; |
73 | | }; |
74 | | |
75 | | class PgAlterDatabase : public PgDdl { |
76 | | public: |
77 | | PgAlterDatabase(PgSession::ScopedRefPtr pg_session, |
78 | | const char *database_name, |
79 | | PgOid database_oid); |
80 | | virtual ~PgAlterDatabase(); |
81 | | |
82 | 0 | StmtOp stmt_op() const override { return StmtOp::STMT_ALTER_DATABASE; } |
83 | | |
84 | | void RenameDatabase(const char *newname); |
85 | | |
86 | | // Execute. |
87 | | CHECKED_STATUS Exec(); |
88 | | |
89 | | private: |
90 | | tserver::PgAlterDatabaseRequestPB req_; |
91 | | }; |
92 | | |
93 | | //-------------------------------------------------------------------------------------------------- |
94 | | // CREATE / DROP TABLEGROUP |
95 | | //-------------------------------------------------------------------------------------------------- |
96 | | |
97 | | class PgCreateTablegroup : public PgDdl { |
98 | | public: |
99 | | PgCreateTablegroup(PgSession::ScopedRefPtr pg_session, |
100 | | const char *database_name, |
101 | | const PgOid database_oid, |
102 | | const PgOid tablegroup_oid, |
103 | | const PgOid tablespace_oid); |
104 | | virtual ~PgCreateTablegroup(); |
105 | | |
106 | 1 | StmtOp stmt_op() const override { return StmtOp::STMT_CREATE_TABLEGROUP; } |
107 | | |
108 | | // Execute. |
109 | | CHECKED_STATUS Exec(); |
110 | | |
111 | | private: |
112 | | tserver::PgCreateTablegroupRequestPB req_; |
113 | | }; |
114 | | |
115 | | class PgDropTablegroup : public PgDdl { |
116 | | public: |
117 | | PgDropTablegroup(PgSession::ScopedRefPtr pg_session, |
118 | | PgOid database_oid, |
119 | | PgOid tablegroup_oid); |
120 | | virtual ~PgDropTablegroup(); |
121 | | |
122 | 1 | StmtOp stmt_op() const override { return StmtOp::STMT_DROP_TABLEGROUP; } |
123 | | |
124 | | // Execute. |
125 | | CHECKED_STATUS Exec(); |
126 | | |
127 | | private: |
128 | | tserver::PgDropTablegroupRequestPB req_; |
129 | | }; |
130 | | |
131 | | //-------------------------------------------------------------------------------------------------- |
132 | | // CREATE TABLE |
133 | | //-------------------------------------------------------------------------------------------------- |
134 | | |
135 | | class PgCreateTable : public PgDdl { |
136 | | public: |
137 | | PgCreateTable(PgSession::ScopedRefPtr pg_session, |
138 | | const char *database_name, |
139 | | const char *schema_name, |
140 | | const char *table_name, |
141 | | const PgObjectId& table_id, |
142 | | bool is_shared_table, |
143 | | bool if_not_exist, |
144 | | bool add_primary_key, |
145 | | const bool colocated, |
146 | | const PgObjectId& tablegroup_oid, |
147 | | const PgObjectId& tablespace_oid, |
148 | | const PgObjectId& matview_pg_table_oid); |
149 | | |
150 | | void SetupIndex( |
151 | | const PgObjectId& base_table_id, bool is_unique_index, bool skip_index_backfill); |
152 | | |
153 | | StmtOp stmt_op() const override; |
154 | | |
155 | | CHECKED_STATUS AddColumn(const char *attr_name, |
156 | | int attr_num, |
157 | | int attr_ybtype, |
158 | | bool is_hash, |
159 | | bool is_range, |
160 | 570 | SortingType sorting_type = SortingType::kNotSpecified) { |
161 | 570 | return AddColumnImpl(attr_name, attr_num, attr_ybtype, 20 /*INT8OID*/, |
162 | 570 | is_hash, is_range, sorting_type); |
163 | 570 | } |
164 | | |
165 | | CHECKED_STATUS AddColumn(const char *attr_name, |
166 | | int attr_num, |
167 | | const YBCPgTypeEntity *attr_type, |
168 | | bool is_hash, |
169 | | bool is_range, |
170 | 3.04k | SortingType sorting_type = SortingType::kNotSpecified) { |
171 | 3.04k | return AddColumnImpl(attr_name, attr_num, attr_type->yb_type, attr_type->type_oid, |
172 | 3.04k | is_hash, is_range, sorting_type); |
173 | 3.04k | } |
174 | | |
175 | | // Specify the number of tablets explicitly. |
176 | | CHECKED_STATUS SetNumTablets(int32_t num_tablets); |
177 | | |
178 | | CHECKED_STATUS AddSplitBoundary(PgExpr **exprs, int expr_count); |
179 | | |
180 | 1.42k | void UseTransaction() { |
181 | 1.42k | req_.set_use_transaction(true); |
182 | 1.42k | } |
183 | | |
184 | | // Execute. |
185 | | virtual CHECKED_STATUS Exec(); |
186 | | |
187 | | protected: |
188 | | virtual CHECKED_STATUS AddColumnImpl( |
189 | | const char *attr_name, int attr_num, int attr_ybtype, int pg_type_oid, bool is_hash, |
190 | | bool is_range, SortingType sorting_type = SortingType::kNotSpecified); |
191 | | |
192 | | private: |
193 | | tserver::PgCreateTableRequestPB req_; |
194 | | }; |
195 | | |
196 | | class PgDropTable : public PgDdl { |
197 | | public: |
198 | | PgDropTable(PgSession::ScopedRefPtr pg_session, const PgObjectId& table_id, bool if_exist); |
199 | | virtual ~PgDropTable(); |
200 | | |
201 | 1.03k | StmtOp stmt_op() const override { return StmtOp::STMT_DROP_TABLE; } |
202 | | |
203 | | // Execute. |
204 | | CHECKED_STATUS Exec(); |
205 | | |
206 | | protected: |
207 | | const PgObjectId table_id_; |
208 | | bool if_exist_; |
209 | | }; |
210 | | |
211 | | class PgTruncateTable : public PgDdl { |
212 | | public: |
213 | | PgTruncateTable(PgSession::ScopedRefPtr pg_session, const PgObjectId& table_id); |
214 | | virtual ~PgTruncateTable(); |
215 | | |
216 | 31 | StmtOp stmt_op() const override { return StmtOp::STMT_TRUNCATE_TABLE; } |
217 | | |
218 | | // Execute. |
219 | | CHECKED_STATUS Exec(); |
220 | | |
221 | | private: |
222 | | tserver::PgTruncateTableRequestPB req_; |
223 | | }; |
224 | | |
225 | | class PgDropIndex : public PgDropTable { |
226 | | public: |
227 | | PgDropIndex(PgSession::ScopedRefPtr pg_session, const PgObjectId& index_id, bool if_exist); |
228 | | virtual ~PgDropIndex(); |
229 | | |
230 | 141 | StmtOp stmt_op() const override { return StmtOp::STMT_DROP_INDEX; } |
231 | | |
232 | | // Execute. |
233 | | CHECKED_STATUS Exec(); |
234 | | }; |
235 | | |
236 | | //-------------------------------------------------------------------------------------------------- |
237 | | // ALTER TABLE |
238 | | //-------------------------------------------------------------------------------------------------- |
239 | | |
240 | | class PgAlterTable : public PgDdl { |
241 | | public: |
242 | | PgAlterTable(PgSession::ScopedRefPtr pg_session, |
243 | | const PgObjectId& table_id); |
244 | | |
245 | | CHECKED_STATUS AddColumn(const char *name, |
246 | | const YBCPgTypeEntity *attr_type, |
247 | | int order); |
248 | | |
249 | | CHECKED_STATUS RenameColumn(const char *oldname, const char *newname); |
250 | | |
251 | | CHECKED_STATUS DropColumn(const char *name); |
252 | | |
253 | | CHECKED_STATUS RenameTable(const char *db_name, const char *newname); |
254 | | |
255 | | CHECKED_STATUS Exec(); |
256 | | |
257 | | virtual ~PgAlterTable(); |
258 | | |
259 | 383 | StmtOp stmt_op() const override { return StmtOp::STMT_ALTER_TABLE; } |
260 | | |
261 | 661 | void UseTransaction() { |
262 | 661 | req_.set_use_transaction(true); |
263 | 661 | } |
264 | | |
265 | | private: |
266 | | tserver::PgAlterTableRequestPB req_; |
267 | | }; |
268 | | |
269 | | } // namespace pggate |
270 | | } // namespace yb |
271 | | |
272 | | #endif // YB_YQL_PGGATE_PG_DDL_H_ |