summaryrefslogtreecommitdiff
path: root/sql/item_vers.cc
blob: 32a6c67f2fe0f775137b7e6beb5a52aa198c750b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
/* Copyright (c) 2017, MariaDB Corporation.

   This program is free software; you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation; version 2 of the License.

   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.

   You should have received a copy of the GNU General Public License
   along with this program; if not, write to the Free Software
   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA */


/**
  @brief
    System Versioning items
*/

#include "mariadb.h"
#include "sql_priv.h"

#include "sql_class.h"
#include "tztime.h"
#include "item.h"

Item_func_vtq_ts::Item_func_vtq_ts(THD *thd, Item* a, TR_table::field_id_t _vtq_field) :
  Item_datetimefunc(thd, a),
  vtq_field(_vtq_field)
{
  decimals= 6;
  null_value= true;
  DBUG_ASSERT(arg_count == 1 && args[0]);
}


bool
Item_func_vtq_ts::get_date(MYSQL_TIME *res, ulonglong fuzzy_date)
{
  THD *thd= current_thd; // can it differ from constructor's?
  DBUG_ASSERT(thd);
  DBUG_ASSERT(args[0]);
  if (args[0]->result_type() != INT_RESULT)
  {
    my_error(ER_ILLEGAL_PARAMETER_DATA_TYPE_FOR_OPERATION, MYF(0),
      args[0]->type_handler()->name().ptr(),
      func_name());
    return true;
  }
  ulonglong trx_id= args[0]->val_uint();
  if (trx_id == ULONGLONG_MAX)
  {
    null_value= false;
    thd->variables.time_zone->gmt_sec_to_TIME(res, TIMESTAMP_MAX_VALUE);
    res->second_part= TIME_MAX_SECOND_PART;
    return false;
  }

  TR_table trt(thd);

  null_value= !trt.query(trx_id);
  if (null_value)
  {
    my_error(ER_VERS_NO_TRX_ID, MYF(0), (longlong) trx_id);
    return true;
  }

  return trt[vtq_field]->get_date(res, fuzzy_date);
}


Item_func_vtq_id::Item_func_vtq_id(THD *thd, Item* a, TR_table::field_id_t _vtq_field,
                                   bool _backwards) :
  Item_longlong_func(thd, a),
  vtq_field(_vtq_field),
  backwards(_backwards)
{
  decimals= 0;
  unsigned_flag= 1;
  null_value= true;
  DBUG_ASSERT(arg_count == 1 && args[0]);
}

Item_func_vtq_id::Item_func_vtq_id(THD *thd, Item* a, Item* b, TR_table::field_id_t _vtq_field) :
  Item_longlong_func(thd, a, b),
  vtq_field(_vtq_field),
  backwards(false)
{
  decimals= 0;
  unsigned_flag= 1;
  null_value= true;
  DBUG_ASSERT(arg_count == 2 && args[0] && args[1]);
}

longlong
Item_func_vtq_id::get_by_trx_id(ulonglong trx_id)
{
  THD *thd= current_thd;
  DBUG_ASSERT(thd);

  if (trx_id == ULONGLONG_MAX)
  {
    null_value= true;
    return 0;
  }

  TR_table trt(thd);
  null_value= !trt.query(trx_id);
  if (null_value)
    return 0;

  return trt[vtq_field]->val_int();
}

longlong
Item_func_vtq_id::get_by_commit_ts(MYSQL_TIME &commit_ts, bool backwards)
{
  THD *thd= current_thd;
  DBUG_ASSERT(thd);

  TR_table trt(thd);
  null_value= !trt.query(commit_ts, backwards);
  if (null_value)
    return 0;

  return trt[vtq_field]->val_int();
}

longlong
Item_func_vtq_id::val_int()
{
  if (args[0]->is_null())
  {
    if (arg_count < 2 || vtq_field == TR_table::FLD_TRX_ID)
    {
      null_value= true;
      return 0;
    }
    return get_by_trx_id(args[1]->val_uint());
  }
  else
  {
    MYSQL_TIME commit_ts;
    if (args[0]->get_date(&commit_ts, 0))
    {
      null_value= true;
      return 0;
    }
    if (arg_count > 1)
    {
      backwards= args[1]->val_bool();
      DBUG_ASSERT(arg_count == 2);
    }
    return get_by_commit_ts(commit_ts, backwards);
  }
}

Item_func_vtq_trx_sees::Item_func_vtq_trx_sees(THD *thd, Item* a, Item* b) :
  Item_bool_func(thd, a, b),
  accept_eq(false)
{
  null_value= true;
  DBUG_ASSERT(arg_count == 2 && args[0] && args[1]);
}

longlong
Item_func_vtq_trx_sees::val_int()
{
  THD *thd= current_thd;
  DBUG_ASSERT(thd);

  DBUG_ASSERT(arg_count > 1);
  ulonglong trx_id1= args[0]->val_uint();
  ulonglong trx_id0= args[1]->val_uint();
  bool result= accept_eq;

  TR_table trt(thd);
  null_value= trt.query_sees(result, trx_id1, trx_id0);
  return result;
}