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
29Item_func_trt_ts::Item_func_trt_ts(THD *thd, Item* a, TR_table::field_id_t _trt_field) :
30 Item_datetimefunc(thd, a),
31 trt_field(_trt_field)
32{
33 decimals= 6;
34 null_value= true;
35 DBUG_ASSERT(arg_count == 1 && args[0]);
36}
37
38
39bool
40Item_func_trt_ts::get_date(MYSQL_TIME *res, ulonglong fuzzy_date)
41{
42 THD *thd= current_thd; // can it differ from constructor's?
43 DBUG_ASSERT(thd);
44 DBUG_ASSERT(args[0]);
45 if (args[0]->result_type() != INT_RESULT)
46 {
47 my_error(ER_ILLEGAL_PARAMETER_DATA_TYPE_FOR_OPERATION, MYF(0),
48 args[0]->type_handler()->name().ptr(),
49 func_name());
50 return true;
51 }
52 ulonglong trx_id= args[0]->val_uint();
53 if (trx_id == ULONGLONG_MAX)
54 {
55 null_value= false;
56 thd->variables.time_zone->gmt_sec_to_TIME(res, TIMESTAMP_MAX_VALUE);
57 res->second_part= TIME_MAX_SECOND_PART;
58 return false;
59 }
60
61 TR_table trt(thd);
62
63 null_value= !trt.query(trx_id);
64 if (null_value)
65 {
66 my_error(ER_VERS_NO_TRX_ID, MYF(0), (longlong) trx_id);
67 return true;
68 }
69
70 return trt[trt_field]->get_date(res, fuzzy_date);
71}
72
73
74Item_func_trt_id::Item_func_trt_id(THD *thd, Item* a, TR_table::field_id_t _trt_field,
75 bool _backwards) :
76 Item_longlong_func(thd, a),
77 trt_field(_trt_field),
78 backwards(_backwards)
79{
80 decimals= 0;
81 unsigned_flag= 1;
82 null_value= true;
83 DBUG_ASSERT(arg_count == 1 && args[0]);
84}
85
86Item_func_trt_id::Item_func_trt_id(THD *thd, Item* a, Item* b, TR_table::field_id_t _trt_field) :
87 Item_longlong_func(thd, a, b),
88 trt_field(_trt_field),
89 backwards(false)
90{
91 decimals= 0;
92 unsigned_flag= 1;
93 null_value= true;
94 DBUG_ASSERT(arg_count == 2 && args[0] && args[1]);
95}
96
97longlong
98Item_func_trt_id::get_by_trx_id(ulonglong trx_id)
99{
100 THD *thd= current_thd;
101 DBUG_ASSERT(thd);
102
103 if (trx_id == ULONGLONG_MAX)
104 {
105 null_value= true;
106 return 0;
107 }
108
109 TR_table trt(thd);
110 null_value= !trt.query(trx_id);
111 if (null_value)
112 return 0;
113
114 return trt[trt_field]->val_int();
115}
116
117longlong
118Item_func_trt_id::get_by_commit_ts(MYSQL_TIME &commit_ts, bool backwards)
119{
120 THD *thd= current_thd;
121 DBUG_ASSERT(thd);
122
123 TR_table trt(thd);
124 null_value= !trt.query(commit_ts, backwards);
125 if (null_value)
126 return backwards ? ULONGLONG_MAX : 0;
127
128 return trt[trt_field]->val_int();
129}
130
131longlong
132Item_func_trt_id::val_int()
133{
134 if (args[0]->is_null())
135 {
136 if (arg_count < 2 || trt_field == TR_table::FLD_TRX_ID)
137 {
138 null_value= true;
139 return 0;
140 }
141 return get_by_trx_id(args[1]->val_uint());
142 }
143 else
144 {
145 MYSQL_TIME commit_ts;
146 if (args[0]->get_date(&commit_ts, 0))
147 {
148 null_value= true;
149 return 0;
150 }
151 if (arg_count > 1)
152 {
153 backwards= args[1]->val_bool();
154 DBUG_ASSERT(arg_count == 2);
155 }
156 return get_by_commit_ts(commit_ts, backwards);
157 }
158}
159
160Item_func_trt_trx_sees::Item_func_trt_trx_sees(THD *thd, Item* a, Item* b) :
161 Item_bool_func(thd, a, b),
162 accept_eq(false)
163{
164 null_value= true;
165 DBUG_ASSERT(arg_count == 2 && args[0] && args[1]);
166}
167
168longlong
169Item_func_trt_trx_sees::val_int()
170{
171 THD *thd= current_thd;
172 DBUG_ASSERT(thd);
173
174 DBUG_ASSERT(arg_count > 1);
175 ulonglong trx_id1= args[0]->val_uint();
176 ulonglong trx_id0= args[1]->val_uint();
177 bool result= accept_eq;
178
179 TR_table trt(thd);
180 null_value= trt.query_sees(result, trx_id1, trx_id0);
181 return result;
182}
183