YugabyteDB (2.13.0.0-b42, bfc6a6643e7399ac8a0e81d06a3ee6d6571b33ab)

Coverage Report

Created: 2022-03-09 17:30

/Users/deen/code/yugabyte-db/src/yb/yql/cql/ql/util/ql_env.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
// QLEnv defines the interface for the environment where SQL engine is running.
16
//
17
// If we support different types of servers underneath SQL engine (which we don't), this class
18
// should be an abstract interface and let the server (such as proxy server) defines the content.
19
//--------------------------------------------------------------------------------------------------
20
21
#ifndef YB_YQL_CQL_QL_UTIL_QL_ENV_H_
22
#define YB_YQL_CQL_QL_UTIL_QL_ENV_H_
23
24
#include "yb/client/client_fwd.h"
25
26
#include "yb/common/common_types.pb.h"
27
#include "yb/common/transaction.pb.h"
28
29
#include "yb/server/hybrid_clock.h"
30
31
#include "yb/util/enums.h"
32
33
#include "yb/yql/cql/ql/ptree/pt_option.h"
34
#include "yb/yql/cql/ql/ql_session.h"
35
#include "yb/yql/cql/ql/util/util_fwd.h"
36
37
namespace yb {
38
namespace ql {
39
40
class QLEnv {
41
 public:
42
  //------------------------------------------------------------------------------------------------
43
  // Public types.
44
  typedef std::unique_ptr<QLEnv> UniPtr;
45
  typedef std::unique_ptr<const QLEnv> UniPtrConst;
46
47
  //------------------------------------------------------------------------------------------------
48
  // Constructor & destructor.
49
  QLEnv(client::YBClient* client,
50
        std::shared_ptr<client::YBMetaDataCache> cache,
51
        const server::ClockPtr& clock,
52
        TransactionPoolProvider transaction_pool_provider);
53
  virtual ~QLEnv();
54
55
  //------------------------------------------------------------------------------------------------
56
  // Table related methods.
57
58
  virtual std::unique_ptr<client::YBTableCreator> NewTableCreator();
59
60
  virtual std::unique_ptr<client::YBTableAlterer> NewTableAlterer(
61
      const client::YBTableName& table_name);
62
63
  virtual CHECKED_STATUS TruncateTable(const std::string& table_id);
64
65
  virtual CHECKED_STATUS DeleteTable(const client::YBTableName& name);
66
67
  virtual CHECKED_STATUS DeleteIndexTable(const client::YBTableName& name,
68
                                          client::YBTableName* indexed_table_name);
69
70
  virtual CHECKED_STATUS GetUpToDateTableSchemaVersion(const client::YBTableName& table_name,
71
                                                       uint32_t* ver);
72
73
  virtual std::shared_ptr<client::YBTable> GetTableDesc(const client::YBTableName& table_name,
74
                                                        bool* cache_used);
75
  virtual std::shared_ptr<client::YBTable> GetTableDesc(const TableId& table_id, bool* cache_used);
76
77
  virtual void RemoveCachedTableDesc(const client::YBTableName& table_name);
78
  virtual void RemoveCachedTableDesc(const TableId& table_id);
79
80
  //------------------------------------------------------------------------------------------------
81
  // Read/write related methods.
82
83
  // Create a read/write session.
84
  client::YBSessionPtr NewSession();
85
86
  // Create a new transaction.
87
  Result<client::YBTransactionPtr> NewTransaction(const client::YBTransactionPtr& transaction,
88
                                                  IsolationLevel isolation_level);
89
90
  //------------------------------------------------------------------------------------------------
91
  // Permission related methods.
92
93
  // Grant/Revoke a permission with the given arguments.
94
  virtual CHECKED_STATUS GrantRevokePermission(client::GrantRevokeStatementType statement_type,
95
                                               const PermissionType& permission,
96
                                               const ResourceType& resource_type,
97
                                               const std::string& canonical_resource,
98
                                               const char* resource_name,
99
                                               const char* namespace_name,
100
                                               const std::string& role_name);
101
102
  //------------------------------------------------------------------------------------------------
103
  // Keyspace related methods.
104
105
  // Create a new keyspace with the given name.
106
  virtual CHECKED_STATUS CreateKeyspace(const std::string& keyspace_name);
107
108
  // Delete keyspace with the given name.
109
  virtual CHECKED_STATUS DeleteKeyspace(const std::string& keyspace_name);
110
111
  // Use keyspace with the given name.
112
  virtual CHECKED_STATUS UseKeyspace(const std::string& keyspace_name);
113
114
  // Alter keyspace with the given name.
115
  virtual CHECKED_STATUS AlterKeyspace(const std::string& keyspace_name);
116
117
69.2k
  virtual std::string CurrentKeyspace() const {
118
69.2k
    return ql_session()->current_keyspace();
119
69.2k
  }
120
121
  //------------------------------------------------------------------------------------------------
122
  // Role related methods.
123
124
  // Create role with the given arguments.
125
  virtual CHECKED_STATUS CreateRole(const std::string& role_name,
126
                                    const std::string& salted_hash,
127
                                    const bool login, const bool superuser);
128
129
  // Alter an existing role with the given arguments.
130
  virtual CHECKED_STATUS AlterRole(const std::string& role_name,
131
                                   const boost::optional<std::string>& salted_hash,
132
                                   const boost::optional<bool> login,
133
                                   const boost::optional<bool> superuser);
134
135
  // Delete role by name.
136
  virtual CHECKED_STATUS DeleteRole(const std::string& role_name);
137
138
  virtual CHECKED_STATUS GrantRevokeRole(client::GrantRevokeStatementType statement_type,
139
                                         const std::string& granted_role_name,
140
                                         const std::string& recipient_role_name);
141
142
31.9k
  virtual std::string CurrentRoleName() const {
143
31.9k
    return ql_session()->current_role_name();
144
31.9k
  }
145
146
  // Check the cache to determine whether the current role has been given permissions on a specific
147
  // canonical resource.
148
  // keyspace and table are only used to generate the error message.
149
  // If the permission is not found, the client will refresh the cache from the master once.
150
  virtual CHECKED_STATUS HasResourcePermission(const string& canonical_name,
151
                                               const ql::ObjectType& object_type,
152
                                               const PermissionType permission,
153
                                               const NamespaceName& keyspace = "",
154
                                               const TableName& table = "");
155
156
  // Convenience methods to check whether the current role has the specified permission on the
157
  // given table.
158
  // These method call YBMetaDataCache::HasTablePermissionWithRetry which first checks if the
159
  // keyspace has the permission. Otherwise, it checks whether the table has the permission.
160
  virtual CHECKED_STATUS HasTablePermission(const NamespaceName& keyspace_name,
161
                                            const TableName& table_name,
162
                                            const PermissionType permission);
163
164
  virtual CHECKED_STATUS HasTablePermission(const client::YBTableName table_name,
165
                                            const PermissionType permission);
166
167
  // Convenience method to check whether the current role has the specified permission on the given
168
  // role.
169
  virtual CHECKED_STATUS HasRolePermission(const RoleName& role_name,
170
                                           const PermissionType permission);
171
172
  Result<std::string> RoleSaltedHash(const RoleName& role_name);
173
174
  Result<bool> RoleCanLogin(const RoleName& role_name);
175
176
  //------------------------------------------------------------------------------------------------
177
  // (User-defined) Type related methods.
178
179
  // Create (user-defined) type with the given arguments.
180
  CHECKED_STATUS CreateUDType(const std::string& keyspace_name,
181
                              const std::string& type_name,
182
                              const std::vector<std::string>& field_names,
183
                              const std::vector<std::shared_ptr<QLType>>& field_types);
184
185
  // Delete (user-defined) type by name.
186
  virtual CHECKED_STATUS DeleteUDType(const std::string& keyspace_name,
187
                                      const std::string& type_name);
188
189
  // Retrieve (user-defined) type by name.
190
  std::shared_ptr<QLType> GetUDType(const std::string& keyspace_name,
191
                                    const std::string& type_name,
192
                                    bool* cache_used);
193
194
  virtual void RemoveCachedUDType(const std::string& keyspace_name, const std::string& type_name);
195
196
  //------------------------------------------------------------------------------------------------
197
  // QLSession related methods.
198
199
9.30M
  void set_ql_session(const QLSession::SharedPtr& ql_session) {
200
9.30M
    ql_session_ = ql_session;
201
9.30M
  }
202
107k
  const QLSession::SharedPtr& ql_session() const {
203
107k
    if (!ql_session_) {
204
0
      ql_session_.reset(new QLSession());
205
0
    }
206
107k
    return ql_session_;
207
107k
  }
208
209
 private:
210
  //------------------------------------------------------------------------------------------------
211
  // Persistent attributes.
212
213
  // YBClient, an API that SQL engine uses to communicate with all servers.
214
  client::YBClient* const client_;
215
216
  // YBMetaDataCache, a cache to avoid creating a new table or type for each call.
217
  // Also used to hold the permissions cache when authentication is enabled.
218
  std::shared_ptr<client::YBMetaDataCache> metadata_cache_;
219
220
  // Server clock.
221
  const server::ClockPtr clock_;
222
223
  // Transaction manager to create distributed transactions.
224
  TransactionPoolProvider transaction_pool_provider_;
225
  client::TransactionPool* transaction_pool_ = nullptr;
226
227
  //------------------------------------------------------------------------------------------------
228
  // Transient attributes.
229
  // The following attributes are reset implicitly for every execution.
230
231
  // The QL session processing the statement.
232
  mutable QLSession::SharedPtr ql_session_;
233
};
234
235
}  // namespace ql
236
}  // namespace yb
237
238
#endif  // YB_YQL_CQL_QL_UTIL_QL_ENV_H_