1 /* Copyright (c) 2017, MariaDB corporation
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 as published by
5    the Free Software Foundation; version 2 of the License.
6 
7    This program is distributed in the hope that it will be useful,
8    but WITHOUT ANY WARRANTY; without even the implied warranty of
9    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
10    GNU General Public License for more details.
11 
12    You should have received a copy of the GNU General Public License
13    along with this program; if not, write to the Free Software
14    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
15 */
16 
17 #ifndef SQL_SEQUENCE_INCLUDED
18 #define SQL_SEQUENCE_INCLUDED
19 
20 #define seq_field_used_min_value 1
21 #define seq_field_used_max_value 2
22 #define seq_field_used_start     4
23 #define seq_field_used_increment 8
24 #define seq_field_used_cache     16
25 #define seq_field_used_cycle     32
26 #define seq_field_used_restart   64
27 #define seq_field_used_restart_value 128
28 
29 /* Field position in sequence table for some fields we refer to directly */
30 #define NEXT_FIELD_NO 0
31 #define MIN_VALUE_FIELD_NO 1
32 #define ROUND_FIELD_NO 7
33 
34 /**
35    sequence_definition is used when defining a sequence as part of create
36 */
37 
38 class sequence_definition :public Sql_alloc
39 {
40 public:
sequence_definition()41   sequence_definition():
42     min_value(1), max_value(LONGLONG_MAX-1), start(1), increment(1),
43       cache(1000), round(0), restart(0), cycle(0), used_fields(0)
44   {}
45   longlong reserved_until;
46   longlong min_value;
47   longlong max_value;
48   longlong start;
49   longlong increment;
50   longlong cache;
51   ulonglong round;
52   longlong restart;              // alter sequence restart value
53   bool     cycle;
54   uint used_fields;              // Which fields where used in CREATE
55 
56   bool check_and_adjust(bool set_reserved_until);
57   void store_fields(TABLE *table);
58   void read_fields(TABLE *table);
59   int write_initial_sequence(TABLE *table);
60   int write(TABLE *table, bool all_fields);
61   /* This must be called after sequence data has been updated */
62   void adjust_values(longlong next_value);
print_dbug()63   inline void print_dbug()
64   {
65     DBUG_PRINT("sequence", ("reserved: %lld  start: %lld  increment: %lld  min_value: %lld  max_value: %lld  cache: %lld  round: %lld",
66                       reserved_until, start, increment, min_value,
67                         max_value, cache, round));
68   }
69 protected:
70   /*
71     The following values are the values from sequence_definition
72     merged with global auto_increment_offset and auto_increment_increment
73   */
74   longlong real_increment;
75   longlong next_free_value;
76 };
77 
78 /**
79   SEQUENCE is in charge of managing the sequence values.
80   It's also responsible to generate new values and updating the sequence
81   table (engine=SQL_SEQUENCE) trough it's specialized handler interface.
82 
83   If increment is 0 then the sequence will be be using
84   auto_increment_increment and auto_increment_offset variables, just like
85   AUTO_INCREMENT is using.
86 */
87 
88 class SEQUENCE :public sequence_definition
89 {
90 public:
91   enum seq_init { SEQ_UNINTIALIZED, SEQ_IN_PREPARE, SEQ_IN_ALTER,
92                   SEQ_READY_TO_USE };
93   SEQUENCE();
94   ~SEQUENCE();
95   int  read_initial_values(TABLE *table);
96   int  read_stored_values(TABLE *table);
97   void write_lock(TABLE *table);
98   void write_unlock(TABLE *table);
99   void read_lock(TABLE *table);
100   void read_unlock(TABLE *table);
copy(sequence_definition * seq)101   void copy(sequence_definition *seq)
102   {
103     sequence_definition::operator= (*seq);
104     adjust_values(reserved_until);
105     all_values_used= 0;
106   }
107   longlong next_value(TABLE *table, bool second_round, int *error);
108   int set_value(TABLE *table, longlong next_value, ulonglong round_arg,
109                 bool is_used);
increment_value(longlong value)110   longlong increment_value(longlong value)
111   {
112     if (real_increment > 0)
113     {
114       if (value > max_value - real_increment ||
115           value + real_increment > max_value)
116         value= max_value + 1;
117       else
118         value+= real_increment;
119     }
120     else
121     {
122       if (value + real_increment < min_value ||
123           value < min_value - real_increment)
124         value= min_value - 1;
125       else
126         value+= real_increment;
127     }
128     return value;
129   }
130 
131   bool all_values_used;
132   seq_init initialized;
133 
134 private:
135   mysql_rwlock_t mutex;
136 };
137 
138 
139 /**
140   Class to cache last value of NEXT VALUE from the sequence
141 */
142 
143 class SEQUENCE_LAST_VALUE
144 {
145 public:
SEQUENCE_LAST_VALUE(uchar * key_arg,uint length_arg)146   SEQUENCE_LAST_VALUE(uchar *key_arg, uint length_arg)
147     :key(key_arg), length(length_arg)
148   {}
~SEQUENCE_LAST_VALUE()149   ~SEQUENCE_LAST_VALUE()
150   { my_free((void*) key); }
151   /* Returns 1 if table hasn't been dropped or re-created */
152   bool check_version(TABLE *table);
153   void set_version(TABLE *table);
154 
155   const uchar *key;
156   uint length;
157   bool null_value;
158   longlong value;
159   uchar table_version[MY_UUID_SIZE];
160 };
161 
162 
163 class Create_field;
164 extern bool prepare_sequence_fields(THD *thd, List<Create_field> *fields);
165 extern bool check_sequence_fields(LEX *lex, List<Create_field> *fields);
166 extern bool sequence_insert(THD *thd, LEX *lex, TABLE_LIST *table_list);
167 #endif /* SQL_SEQUENCE_INCLUDED */
168