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 Street, Fifth Floor, Boston, MA  02110-1301, USA */
15 
16 
17 /**
18   @brief
19     System Versioning items
20 */
21 
22 #include "mariadb.h"
23 #include "sql_priv.h"
24 
25 #include "sql_class.h"
26 #include "tztime.h"
27 #include "item.h"
28 
val_bool()29 bool Item_func_history::val_bool()
30 {
31   Item_field *f= static_cast<Item_field *>(args[0]);
32   DBUG_ASSERT(f->fixed);
33   DBUG_ASSERT(f->field->flags & VERS_ROW_END);
34   return !f->field->is_max();
35 }
36 
print(String * str,enum_query_type query_type)37 void Item_func_history::print(String *str, enum_query_type query_type)
38 {
39   str->append(func_name());
40   str->append('(');
41   args[0]->print(str, query_type);
42   str->append(')');
43 }
44 
Item_func_trt_ts(THD * thd,Item * a,TR_table::field_id_t _trt_field)45 Item_func_trt_ts::Item_func_trt_ts(THD *thd, Item* a, TR_table::field_id_t _trt_field) :
46   Item_datetimefunc(thd, a),
47   trt_field(_trt_field)
48 {
49   decimals= 6;
50   null_value= true;
51   DBUG_ASSERT(arg_count == 1 && args[0]);
52 }
53 
54 
55 bool
get_date(THD * thd,MYSQL_TIME * res,date_mode_t fuzzydate)56 Item_func_trt_ts::get_date(THD *thd, MYSQL_TIME *res, date_mode_t fuzzydate)
57 {
58   DBUG_ASSERT(thd);
59   DBUG_ASSERT(args[0]);
60   if (args[0]->result_type() != INT_RESULT)
61   {
62     my_error(ER_ILLEGAL_PARAMETER_DATA_TYPE_FOR_OPERATION, MYF(0),
63       args[0]->type_handler()->name().ptr(),
64       func_name());
65     return true;
66   }
67   ulonglong trx_id= args[0]->val_uint();
68   if (trx_id == ULONGLONG_MAX)
69   {
70     null_value= false;
71     thd->variables.time_zone->gmt_sec_to_TIME(res, TIMESTAMP_MAX_VALUE);
72     res->second_part= TIME_MAX_SECOND_PART;
73     return false;
74   }
75 
76   TR_table trt(thd);
77 
78   null_value= !trt.query(trx_id);
79   if (null_value)
80     return true;
81 
82   return trt[trt_field]->get_date(res, fuzzydate);
83 }
84 
85 
Item_func_trt_id(THD * thd,Item * a,TR_table::field_id_t _trt_field,bool _backwards)86 Item_func_trt_id::Item_func_trt_id(THD *thd, Item* a, TR_table::field_id_t _trt_field,
87                                    bool _backwards) :
88   Item_longlong_func(thd, a),
89   trt_field(_trt_field),
90   backwards(_backwards)
91 {
92   decimals= 0;
93   unsigned_flag= 1;
94   null_value= true;
95   DBUG_ASSERT(arg_count == 1 && args[0]);
96 }
97 
Item_func_trt_id(THD * thd,Item * a,Item * b,TR_table::field_id_t _trt_field)98 Item_func_trt_id::Item_func_trt_id(THD *thd, Item* a, Item* b, TR_table::field_id_t _trt_field) :
99   Item_longlong_func(thd, a, b),
100   trt_field(_trt_field),
101   backwards(false)
102 {
103   decimals= 0;
104   unsigned_flag= 1;
105   null_value= true;
106   DBUG_ASSERT(arg_count == 2 && args[0] && args[1]);
107 }
108 
109 longlong
get_by_trx_id(ulonglong trx_id)110 Item_func_trt_id::get_by_trx_id(ulonglong trx_id)
111 {
112   THD *thd= current_thd;
113   DBUG_ASSERT(thd);
114 
115   if (trx_id == ULONGLONG_MAX)
116   {
117     null_value= true;
118     return 0;
119   }
120 
121   TR_table trt(thd);
122   null_value= !trt.query(trx_id);
123   if (null_value)
124     return 0;
125 
126   return trt[trt_field]->val_int();
127 }
128 
129 longlong
get_by_commit_ts(MYSQL_TIME & commit_ts,bool backwards)130 Item_func_trt_id::get_by_commit_ts(MYSQL_TIME &commit_ts, bool backwards)
131 {
132   THD *thd= current_thd;
133   DBUG_ASSERT(thd);
134 
135   TR_table trt(thd);
136   null_value= !trt.query(commit_ts, backwards);
137   if (null_value)
138     return backwards ? ULONGLONG_MAX : 0;
139 
140   return trt[trt_field]->val_int();
141 }
142 
143 longlong
val_int()144 Item_func_trt_id::val_int()
145 {
146   if (args[0]->is_null())
147   {
148     if (arg_count < 2 || trt_field == TR_table::FLD_TRX_ID)
149     {
150       null_value= true;
151       return 0;
152     }
153     return get_by_trx_id(args[1]->val_uint());
154   }
155   else
156   {
157     MYSQL_TIME commit_ts;
158     THD *thd= current_thd;
159     Datetime::Options opt(TIME_CONV_NONE, thd);
160     if (args[0]->get_date(thd, &commit_ts, opt))
161     {
162       null_value= true;
163       return 0;
164     }
165     if (arg_count > 1)
166     {
167       backwards= args[1]->val_bool();
168       DBUG_ASSERT(arg_count == 2);
169     }
170     return get_by_commit_ts(commit_ts, backwards);
171   }
172 }
173 
Item_func_trt_trx_sees(THD * thd,Item * a,Item * b)174 Item_func_trt_trx_sees::Item_func_trt_trx_sees(THD *thd, Item* a, Item* b) :
175   Item_bool_func(thd, a, b),
176   accept_eq(false)
177 {
178   null_value= true;
179   DBUG_ASSERT(arg_count == 2 && args[0] && args[1]);
180 }
181 
182 longlong
val_int()183 Item_func_trt_trx_sees::val_int()
184 {
185   THD *thd= current_thd;
186   DBUG_ASSERT(thd);
187 
188   DBUG_ASSERT(arg_count > 1);
189   ulonglong trx_id1= args[0]->val_uint();
190   ulonglong trx_id0= args[1]->val_uint();
191   bool result= accept_eq;
192 
193   TR_table trt(thd);
194   null_value= trt.query_sees(result, trx_id1, trx_id0);
195   return result;
196 }
197