1 /* Copyright (c) 2018, 2019, Oracle and/or its affiliates. All rights reserved.
2 
3 This program is free software; you can redistribute it and/or modify
4 it under the terms of the GNU General Public License, version 2.0,
5 as published by the Free Software Foundation.
6 
7 This program is also distributed with certain software (including
8 but not limited to OpenSSL) that is licensed under separate terms,
9 as designated in a particular file or component or in included license
10 documentation.  The authors of MySQL hereby grant you an additional
11 permission to link the program and your derivative works with the
12 separately licensed software that they have included with MySQL.
13 
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17 GNU General Public License, version 2.0, for more details.
18 
19 You should have received a copy of the GNU General Public License
20 along with this program; if not, write to the Free Software
21 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301  USA */
22 
23 #ifndef ACL_TABLE_BASE_INCLUDED
24 #define ACL_TABLE_BASE_INCLUDED
25 
26 #include "my_base.h"
27 #include "my_dbug.h"
28 
29 class THD;
30 struct TABLE;
31 
32 namespace acl_table {
33 
34 typedef int Table_op_error_code;
35 
36 enum class Acl_table_op_status { OP_ERROR_CRITICAL = 0, OP_OK, OP_ERROR_ROW };
37 enum class Acl_table_operation { OP_INSERT = 0, OP_UPDATE, OP_DELETE, OP_READ };
38 
39 /**
40   Base class to handle ACL table manipulation
41 */
42 
43 class Acl_table {
44  public:
45   /* Constructor & Destructor */
Acl_table(THD * thd,TABLE * table,Acl_table_operation operation)46   Acl_table(THD *thd, TABLE *table, Acl_table_operation operation)
47       : m_thd(thd), m_table(table), m_operation(operation), m_error(0) {
48     DBUG_ASSERT(m_table);
49   }
~Acl_table()50   virtual ~Acl_table() {}
51 
52   /* Don't allow copy */
53   Acl_table(const Acl_table &) = delete;
54   const Acl_table &operator=(const Acl_table &) = delete;
55   Acl_table(const Acl_table &&) = delete;
56   const Acl_table &operator=(const Acl_table &&) = delete;
57 
58   /* Finish a reade/write operation on given table */
59   virtual Acl_table_op_status finish_operation(Table_op_error_code &error) = 0;
get_operation_mode()60   Acl_table_operation get_operation_mode() { return m_operation; }
61 
62  protected:
convert_table_op_error_code()63   Acl_table_op_status convert_table_op_error_code() {
64     if (!m_error) return Acl_table_op_status::OP_OK;
65 
66     if (m_error == HA_ERR_KEY_NOT_FOUND || m_error == HA_ERR_END_OF_FILE)
67       return Acl_table_op_status::OP_ERROR_ROW;
68 
69     return Acl_table_op_status::OP_ERROR_CRITICAL;
70   }
71 
72   /* Thread handle */
73   THD *m_thd;
74   /* Table handle */
75   TABLE *m_table;
76   /* Mode - INSERT/UPDATE/DELETE/READ */
77   Acl_table_operation m_operation;
78   /* Table operation status */
79   Table_op_error_code m_error;
80 };
81 
82 }  // namespace acl_table
83 
84 #endif /* ACL_TABLE_BASE_INCLUDED */
85