/Users/deen/code/yugabyte-db/src/yb/yql/pggate/pg_ddl.h
Line | Count | Source |
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/constants.h" |
19 | | #include "yb/common/transaction.h" |
20 | | |
21 | | #include "yb/tserver/pg_client.pb.h" |
22 | | |
23 | | #include "yb/yql/pggate/pg_statement.h" |
24 | | |
25 | | namespace yb { |
26 | | namespace pggate { |
27 | | |
28 | | class PgDdl : public PgStatement { |
29 | | public: |
30 | 11.9k | explicit PgDdl(PgSession::ScopedRefPtr pg_session) : PgStatement(pg_session) { |
31 | 11.9k | } |
32 | | }; |
33 | | |
34 | | //-------------------------------------------------------------------------------------------------- |
35 | | // CREATE DATABASE |
36 | | //-------------------------------------------------------------------------------------------------- |
37 | | |
38 | | class PgCreateDatabase : public PgDdl { |
39 | | public: |
40 | | PgCreateDatabase(PgSession::ScopedRefPtr pg_session, |
41 | | const char *database_name, |
42 | | PgOid database_oid, |
43 | | PgOid source_database_oid, |
44 | | PgOid next_oid, |
45 | | const bool colocated); |
46 | | virtual ~PgCreateDatabase(); |
47 | | |
48 | 114 | void UseTransaction() { |
49 | 114 | req_.set_use_transaction(true); |
50 | 114 | } |
51 | | |
52 | 134 | StmtOp stmt_op() const override { return StmtOp::STMT_CREATE_DATABASE; } |
53 | | |
54 | | // Execute. |
55 | | CHECKED_STATUS Exec(); |
56 | | |
57 | | private: |
58 | | tserver::PgCreateDatabaseRequestPB req_; |
59 | | }; |
60 | | |
61 | | class PgDropDatabase : public PgDdl { |
62 | | public: |
63 | | PgDropDatabase(PgSession::ScopedRefPtr pg_session, const char *database_name, PgOid database_oid); |
64 | | virtual ~PgDropDatabase(); |
65 | | |
66 | 72 | StmtOp stmt_op() const override { return StmtOp::STMT_DROP_DATABASE; } |
67 | | |
68 | | // Execute. |
69 | | CHECKED_STATUS Exec(); |
70 | | |
71 | | private: |
72 | | const char *database_name_; |
73 | | const PgOid database_oid_; |
74 | | }; |
75 | | |
76 | | class PgAlterDatabase : public PgDdl { |
77 | | public: |
78 | | PgAlterDatabase(PgSession::ScopedRefPtr pg_session, |
79 | | const char *database_name, |
80 | | PgOid database_oid); |
81 | | virtual ~PgAlterDatabase(); |
82 | | |
83 | 6 | StmtOp stmt_op() const override { return StmtOp::STMT_ALTER_DATABASE; } |
84 | | |
85 | | void RenameDatabase(const char *newname); |
86 | | |
87 | | // Execute. |
88 | | CHECKED_STATUS Exec(); |
89 | | |
90 | | private: |
91 | | tserver::PgAlterDatabaseRequestPB req_; |
92 | | }; |
93 | | |
94 | | //-------------------------------------------------------------------------------------------------- |
95 | | // CREATE / DROP TABLEGROUP |
96 | | //-------------------------------------------------------------------------------------------------- |
97 | | |
98 | | class PgCreateTablegroup : public PgDdl { |
99 | | public: |
100 | | PgCreateTablegroup(PgSession::ScopedRefPtr pg_session, |
101 | | const char *database_name, |
102 | | const PgOid database_oid, |
103 | | const PgOid tablegroup_oid, |
104 | | const PgOid tablespace_oid); |
105 | | virtual ~PgCreateTablegroup(); |
106 | | |
107 | 54 | StmtOp stmt_op() const override { return StmtOp::STMT_CREATE_TABLEGROUP; } |
108 | | |
109 | | // Execute. |
110 | | CHECKED_STATUS Exec(); |
111 | | |
112 | | private: |
113 | | tserver::PgCreateTablegroupRequestPB req_; |
114 | | }; |
115 | | |
116 | | class PgDropTablegroup : public PgDdl { |
117 | | public: |
118 | | PgDropTablegroup(PgSession::ScopedRefPtr pg_session, |
119 | | PgOid database_oid, |
120 | | PgOid tablegroup_oid); |
121 | | virtual ~PgDropTablegroup(); |
122 | | |
123 | 39 | StmtOp stmt_op() const override { return StmtOp::STMT_DROP_TABLEGROUP; } |
124 | | |
125 | | // Execute. |
126 | | CHECKED_STATUS Exec(); |
127 | | |
128 | | private: |
129 | | tserver::PgDropTablegroupRequestPB req_; |
130 | | }; |
131 | | |
132 | | //-------------------------------------------------------------------------------------------------- |
133 | | // CREATE TABLE |
134 | | //-------------------------------------------------------------------------------------------------- |
135 | | |
136 | | class PgCreateTable : public PgDdl { |
137 | | public: |
138 | | PgCreateTable(PgSession::ScopedRefPtr pg_session, |
139 | | const char *database_name, |
140 | | const char *schema_name, |
141 | | const char *table_name, |
142 | | const PgObjectId& table_id, |
143 | | bool is_shared_table, |
144 | | bool if_not_exist, |
145 | | bool add_primary_key, |
146 | | const bool colocated, |
147 | | const PgObjectId& tablegroup_oid, |
148 | | const ColocationId colocation_id, |
149 | | const PgObjectId& tablespace_oid, |
150 | | const PgObjectId& matview_pg_table_oid); |
151 | | |
152 | | void SetupIndex( |
153 | | const PgObjectId& base_table_id, bool is_unique_index, bool skip_index_backfill); |
154 | | |
155 | | StmtOp stmt_op() const override; |
156 | | |
157 | | CHECKED_STATUS AddColumn(const char *attr_name, |
158 | | int attr_num, |
159 | | int attr_ybtype, |
160 | | bool is_hash, |
161 | | bool is_range, |
162 | 2.25k | SortingType sorting_type = SortingType::kNotSpecified) { |
163 | 2.25k | return AddColumnImpl(attr_name, attr_num, attr_ybtype, 20 /*INT8OID*/, |
164 | 2.25k | is_hash, is_range, sorting_type); |
165 | 2.25k | } |
166 | | |
167 | | CHECKED_STATUS AddColumn(const char *attr_name, |
168 | | int attr_num, |
169 | | const YBCPgTypeEntity *attr_type, |
170 | | bool is_hash, |
171 | | bool is_range, |
172 | 11.7k | SortingType sorting_type = SortingType::kNotSpecified) { |
173 | 11.7k | return AddColumnImpl(attr_name, attr_num, attr_type->yb_type, attr_type->type_oid, |
174 | 11.7k | is_hash, is_range, sorting_type); |
175 | 11.7k | } |
176 | | |
177 | | // Specify the number of tablets explicitly. |
178 | | CHECKED_STATUS SetNumTablets(int32_t num_tablets); |
179 | | |
180 | | CHECKED_STATUS AddSplitBoundary(PgExpr **exprs, int expr_count); |
181 | | |
182 | 4.88k | void UseTransaction() { |
183 | 4.88k | req_.set_use_transaction(true); |
184 | 4.88k | } |
185 | | |
186 | | // Execute. |
187 | | virtual CHECKED_STATUS Exec(); |
188 | | |
189 | | protected: |
190 | | virtual CHECKED_STATUS AddColumnImpl( |
191 | | const char *attr_name, int attr_num, int attr_ybtype, int pg_type_oid, bool is_hash, |
192 | | bool is_range, SortingType sorting_type = SortingType::kNotSpecified); |
193 | | |
194 | | private: |
195 | | tserver::PgCreateTableRequestPB req_; |
196 | | }; |
197 | | |
198 | | class PgDropTable : public PgDdl { |
199 | | public: |
200 | | PgDropTable(PgSession::ScopedRefPtr pg_session, const PgObjectId& table_id, bool if_exist); |
201 | | virtual ~PgDropTable(); |
202 | | |
203 | 3.48k | StmtOp stmt_op() const override { return StmtOp::STMT_DROP_TABLE; } |
204 | | |
205 | | // Execute. |
206 | | CHECKED_STATUS Exec(); |
207 | | |
208 | | protected: |
209 | | const PgObjectId table_id_; |
210 | | bool if_exist_; |
211 | | }; |
212 | | |
213 | | class PgTruncateTable : public PgDdl { |
214 | | public: |
215 | | PgTruncateTable(PgSession::ScopedRefPtr pg_session, const PgObjectId& table_id); |
216 | | virtual ~PgTruncateTable(); |
217 | | |
218 | 624 | StmtOp stmt_op() const override { return StmtOp::STMT_TRUNCATE_TABLE; } |
219 | | |
220 | | // Execute. |
221 | | CHECKED_STATUS Exec(); |
222 | | |
223 | | private: |
224 | | tserver::PgTruncateTableRequestPB req_; |
225 | | }; |
226 | | |
227 | | class PgDropIndex : public PgDropTable { |
228 | | public: |
229 | | PgDropIndex(PgSession::ScopedRefPtr pg_session, const PgObjectId& index_id, bool if_exist); |
230 | | virtual ~PgDropIndex(); |
231 | | |
232 | 669 | StmtOp stmt_op() const override { return StmtOp::STMT_DROP_INDEX; } |
233 | | |
234 | | // Execute. |
235 | | CHECKED_STATUS Exec(); |
236 | | }; |
237 | | |
238 | | //-------------------------------------------------------------------------------------------------- |
239 | | // ALTER TABLE |
240 | | //-------------------------------------------------------------------------------------------------- |
241 | | |
242 | | class PgAlterTable : public PgDdl { |
243 | | public: |
244 | | PgAlterTable(PgSession::ScopedRefPtr pg_session, |
245 | | const PgObjectId& table_id); |
246 | | |
247 | | CHECKED_STATUS AddColumn(const char *name, |
248 | | const YBCPgTypeEntity *attr_type, |
249 | | int order); |
250 | | |
251 | | CHECKED_STATUS RenameColumn(const char *oldname, const char *newname); |
252 | | |
253 | | CHECKED_STATUS DropColumn(const char *name); |
254 | | |
255 | | CHECKED_STATUS RenameTable(const char *db_name, const char *newname); |
256 | | |
257 | | CHECKED_STATUS Exec(); |
258 | | |
259 | | virtual ~PgAlterTable(); |
260 | | |
261 | 1.30k | StmtOp stmt_op() const override { return StmtOp::STMT_ALTER_TABLE; } |
262 | | |
263 | 1.71k | void UseTransaction() { |
264 | 1.71k | req_.set_use_transaction(true); |
265 | 1.71k | } |
266 | | |
267 | | private: |
268 | | tserver::PgAlterTableRequestPB req_; |
269 | | }; |
270 | | |
271 | | } // namespace pggate |
272 | | } // namespace yb |
273 | | |
274 | | #endif // YB_YQL_PGGATE_PG_DDL_H_ |