1 #ifndef HA_SEQUENCE_INCLUDED
2 #define HA_SEQUENCE_INCLUDED
3 /*
4    Copyright (c) 2017 Aliyun and/or its affiliates.
5    Copyright (c) 2017 MariaDB corporation
6 
7    This program is free software; you can redistribute it and/or modify
8    it under the terms of the GNU General Public License as published by
9    the Free Software Foundation; version 2 of the License.
10 
11    This program is distributed in the hope that it will be useful,
12    but WITHOUT ANY WARRANTY; without even the implied warranty of
13    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14    GNU General Public License for more details.
15 
16    You should have received a copy of the GNU General Public License
17    along with this program; if not, write to the Free Software
18    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301  USA
19 */
20 
21 #include "sql_sequence.h"
22 #include "table.h"
23 #include "handler.h"
24 
25 extern handlerton *sql_sequence_hton;
26 
27 /*
28   Sequence engine handler.
29 
30   The sequence engine is a logic engine. It doesn't store any data.
31   All the sequence data stored into the base table which must support
32   non rollback writes (HA_CAN_TABLES_WITHOUT_ROLLBACK)
33 
34   The sequence data (SEQUENCE class) is stored in TABLE_SHARE->sequence
35 
36   TABLE RULES:
37       1. When table is created, one row is automaticlly inserted into
38          the table. The table will always have one and only one row.
39       2. Any inserts or updates to the table will be validated.
40       3. Inserts will overwrite the original row.
41       4. DELETE and TRUNCATE will not affect the table.
42          Instead a warning will be given.
43       5. Cache will be reset for any updates.
44 
45   CACHE RULES:
46     SEQUENCE class is used to cache values that sequence defined.
47       1. If hit cache, we can query back the sequence nextval directly
48          instead of reading the underlying table.
49 
50       2. When run out of values, the sequence engine will reserve new values
51          in update the base table.
52 
53       3. The cache is invalidated if any update on based table.
54 */
55 
56 class ha_sequence :public handler
57 {
58 private:
59   handler *file;
60   SEQUENCE *sequence;                     /* From table_share->sequence */
61 
62 public:
63   /* Set when handler is write locked */
64   bool write_locked;
65 
66   ha_sequence(handlerton *hton, TABLE_SHARE *share);
67   ~ha_sequence();
68 
69   /* virtual function that are re-implemented for sequence */
70   int open(const char *name, int mode, uint test_if_locked);
71   int create(const char *name, TABLE *form,
72              HA_CREATE_INFO *create_info);
73   handler *clone(const char *name, MEM_ROOT *mem_root);
74   int write_row(const uchar *buf);
75   Table_flags table_flags() const;
76   /* One can't update or delete from sequence engine */
update_row(const uchar * old_data,const uchar * new_data)77   int update_row(const uchar *old_data, const uchar *new_data)
78   { return HA_ERR_WRONG_COMMAND; }
delete_row(const uchar * buf)79   int delete_row(const uchar *buf)
80   { return HA_ERR_WRONG_COMMAND; }
81   /* One can't delete from sequence engine */
truncate()82   int truncate()
83   { return HA_ERR_WRONG_COMMAND; }
84   /* Can't use query cache */
table_cache_type()85   uint8 table_cache_type()
86   { return HA_CACHE_TBL_NOCACHE; }
87   void print_error(int error, myf errflag);
88   int info(uint);
engine_name()89   LEX_CSTRING *engine_name() { return hton_name(file->ht); }
90   int external_lock(THD *thd, int lock_type);
91   int extra(enum ha_extra_function operation);
92   /* For ALTER ONLINE TABLE */
93   bool check_if_incompatible_data(HA_CREATE_INFO *create_info,
94                                   uint table_changes);
write_lock()95   void write_lock() { write_locked= 1;}
unlock()96   void unlock() { write_locked= 0; }
is_locked()97   bool is_locked() { return write_locked; }
98 
99   /* Functions that are directly mapped to the underlying handler */
rnd_init(bool scan)100   int rnd_init(bool scan)
101   { return file->rnd_init(scan); }
102   /*
103     We need to have a lock here to protect engines like MyISAM from
104     simultaneous read and write. For sequence's this is not critical
105     as this function is used extremely seldom.
106   */
rnd_next(uchar * buf)107   int rnd_next(uchar *buf)
108   {
109     int error;
110     table->s->sequence->read_lock(table);
111     error= file->rnd_next(buf);
112     table->s->sequence->read_unlock(table);
113     return error;
114   }
rnd_end()115   int rnd_end()
116   { return file->rnd_end(); }
rnd_pos(uchar * buf,uchar * pos)117   int rnd_pos(uchar *buf, uchar *pos)
118   {
119     int error;
120     table->s->sequence->read_lock(table);
121     error= file->rnd_pos(buf, pos);
122     table->s->sequence->read_unlock(table);
123     return error;
124   }
position(const uchar * record)125   void position(const uchar *record)
126   { return file->position(record); }
table_type()127   const char *table_type() const
128   { return file->table_type(); }
index_flags(uint inx,uint part,bool all_parts)129   ulong index_flags(uint inx, uint part, bool all_parts) const
130   { return file->index_flags(inx, part, all_parts); }
store_lock(THD * thd,THR_LOCK_DATA ** to,enum thr_lock_type lock_type)131   THR_LOCK_DATA **store_lock(THD *thd, THR_LOCK_DATA **to,
132                              enum thr_lock_type lock_type)
133   { return file->store_lock(thd, to, lock_type); }
close(void)134   int close(void)
135   { return file->close(); }
bas_ext()136   const char **bas_ext() const
137   { return file->bas_ext(); }
delete_table(const char * name)138   int delete_table(const char*name)
139   { return file->delete_table(name); }
rename_table(const char * from,const char * to)140   int rename_table(const char *from, const char *to)
141   { return file->rename_table(from, to); }
unbind_psi()142   void unbind_psi()
143   { file->unbind_psi(); }
rebind_psi()144   void rebind_psi()
145   { file->rebind_psi(); }
146 
auto_repair(int error)147   bool auto_repair(int error) const
148   { return file->auto_repair(error); }
repair(THD * thd,HA_CHECK_OPT * check_opt)149   int repair(THD* thd, HA_CHECK_OPT* check_opt)
150   { return file->repair(thd, check_opt); }
check_and_repair(THD * thd)151   bool check_and_repair(THD *thd)
152   { return file->check_and_repair(thd); }
is_crashed()153   bool is_crashed() const
154   { return file->is_crashed(); }
column_bitmaps_signal()155   void column_bitmaps_signal()
156   { return file->column_bitmaps_signal(); }
157 
158   /* New methods */
register_original_handler(handler * file_arg)159   void register_original_handler(handler *file_arg)
160   {
161     file= file_arg;
162     init();                                     /* Update cached_table_flags */
163   }
164 };
165 #endif
166