YugabyteDB (2.13.1.0-b60, 21121d69985fbf76aa6958d8f04a9bfa936293b5)

Coverage Report

Created: 2022-03-22 16:43

/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
                                                  CoarseTimePoint deadline);
90
91
  //------------------------------------------------------------------------------------------------
92
  // Permission related methods.
93
94
  // Grant/Revoke a permission with the given arguments.
95
  virtual CHECKED_STATUS GrantRevokePermission(client::GrantRevokeStatementType statement_type,
96
                                               const PermissionType& permission,
97
                                               const ResourceType& resource_type,
98
                                               const std::string& canonical_resource,
99
                                               const char* resource_name,
100
                                               const char* namespace_name,
101
                                               const std::string& role_name);
102
103
  //------------------------------------------------------------------------------------------------
104
  // Keyspace related methods.
105
106
  // Create a new keyspace with the given name.
107
  virtual CHECKED_STATUS CreateKeyspace(const std::string& keyspace_name);
108
109
  // Delete keyspace with the given name.
110
  virtual CHECKED_STATUS DeleteKeyspace(const std::string& keyspace_name);
111
112
  // Use keyspace with the given name.
113
  virtual CHECKED_STATUS UseKeyspace(const std::string& keyspace_name);
114
115
  // Alter keyspace with the given name.
116
  virtual CHECKED_STATUS AlterKeyspace(const std::string& keyspace_name);
117
118
72.5k
  virtual std::string CurrentKeyspace() const {
119
72.5k
    return ql_session()->current_keyspace();
120
72.5k
  }
121
122
  //------------------------------------------------------------------------------------------------
123
  // Role related methods.
124
125
  // Create role with the given arguments.
126
  virtual CHECKED_STATUS CreateRole(const std::string& role_name,
127
                                    const std::string& salted_hash,
128
                                    const bool login, const bool superuser);
129
130
  // Alter an existing role with the given arguments.
131
  virtual CHECKED_STATUS AlterRole(const std::string& role_name,
132
                                   const boost::optional<std::string>& salted_hash,
133
                                   const boost::optional<bool> login,
134
                                   const boost::optional<bool> superuser);
135
136
  // Delete role by name.
137
  virtual CHECKED_STATUS DeleteRole(const std::string& role_name);
138
139
  virtual CHECKED_STATUS GrantRevokeRole(client::GrantRevokeStatementType statement_type,
140
                                         const std::string& granted_role_name,
141
                                         const std::string& recipient_role_name);
142
143
32.0k
  virtual std::string CurrentRoleName() const {
144
32.0k
    return ql_session()->current_role_name();
145
32.0k
  }
146
147
  // Check the cache to determine whether the current role has been given permissions on a specific
148
  // canonical resource.
149
  // keyspace and table are only used to generate the error message.
150
  // If the permission is not found, the client will refresh the cache from the master once.
151
  virtual CHECKED_STATUS HasResourcePermission(const string& canonical_name,
152
                                               const ql::ObjectType& object_type,
153
                                               const PermissionType permission,
154
                                               const NamespaceName& keyspace = "",
155
                                               const TableName& table = "");
156
157
  // Convenience methods to check whether the current role has the specified permission on the
158
  // given table.
159
  // These method call YBMetaDataCache::HasTablePermissionWithRetry which first checks if the
160
  // keyspace has the permission. Otherwise, it checks whether the table has the permission.
161
  virtual CHECKED_STATUS HasTablePermission(const NamespaceName& keyspace_name,
162
                                            const TableName& table_name,
163
                                            const PermissionType permission);
164
165
  virtual CHECKED_STATUS HasTablePermission(const client::YBTableName table_name,
166
                                            const PermissionType permission);
167
168
  // Convenience method to check whether the current role has the specified permission on the given
169
  // role.
170
  virtual CHECKED_STATUS HasRolePermission(const RoleName& role_name,
171
                                           const PermissionType permission);
172
173
  Result<std::string> RoleSaltedHash(const RoleName& role_name);
174
175
  Result<bool> RoleCanLogin(const RoleName& role_name);
176
177
  //------------------------------------------------------------------------------------------------
178
  // (User-defined) Type related methods.
179
180
  // Create (user-defined) type with the given arguments.
181
  CHECKED_STATUS CreateUDType(const std::string& keyspace_name,
182
                              const std::string& type_name,
183
                              const std::vector<std::string>& field_names,
184
                              const std::vector<std::shared_ptr<QLType>>& field_types);
185
186
  // Delete (user-defined) type by name.
187
  virtual CHECKED_STATUS DeleteUDType(const std::string& keyspace_name,
188
                                      const std::string& type_name);
189
190
  // Retrieve (user-defined) type by name.
191
  std::shared_ptr<QLType> GetUDType(const std::string& keyspace_name,
192
                                    const std::string& type_name,
193
                                    bool* cache_used);
194
195
  virtual void RemoveCachedUDType(const std::string& keyspace_name, const std::string& type_name);
196
197
  //------------------------------------------------------------------------------------------------
198
  // QLSession related methods.
199
200
18.0M
  void set_ql_session(const QLSession::SharedPtr& ql_session) {
201
18.0M
    ql_session_ = ql_session;
202
18.0M
  }
203
111k
  const QLSession::SharedPtr& ql_session() const {
204
111k
    if (!ql_session_) {
205
0
      ql_session_.reset(new QLSession());
206
0
    }
207
111k
    return ql_session_;
208
111k
  }
209
210
 private:
211
  //------------------------------------------------------------------------------------------------
212
  // Persistent attributes.
213
214
  // YBClient, an API that SQL engine uses to communicate with all servers.
215
  client::YBClient* const client_;
216
217
  // YBMetaDataCache, a cache to avoid creating a new table or type for each call.
218
  // Also used to hold the permissions cache when authentication is enabled.
219
  std::shared_ptr<client::YBMetaDataCache> metadata_cache_;
220
221
  // Server clock.
222
  const server::ClockPtr clock_;
223
224
  // Transaction manager to create distributed transactions.
225
  TransactionPoolProvider transaction_pool_provider_;
226
  client::TransactionPool* transaction_pool_ = nullptr;
227
228
  //------------------------------------------------------------------------------------------------
229
  // Transient attributes.
230
  // The following attributes are reset implicitly for every execution.
231
232
  // The QL session processing the statement.
233
  mutable QLSession::SharedPtr ql_session_;
234
};
235
236
}  // namespace ql
237
}  // namespace yb
238
239
#endif  // YB_YQL_CQL_QL_UTIL_QL_ENV_H_