~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to drizzled/functions/time/add_time.cc

move functions from item.cc/item.h to item directory

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* -*- mode: c++; c-basic-offset: 2; indent-tabs-mode: nil; -*-
 
2
 *  vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
 
3
 *
 
4
 *  Copyright (C) 2008 Sun Microsystems
 
5
 *
 
6
 *  This program is free software; you can redistribute it and/or modify
 
7
 *  it under the terms of the GNU General Public License as published by
 
8
 *  the Free Software Foundation; version 2 of the License.
 
9
 *
 
10
 *  This program is distributed in the hope that it will be useful,
 
11
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 
12
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
13
 *  GNU General Public License for more details.
 
14
 *
 
15
 *  You should have received a copy of the GNU General Public License
 
16
 *  along with this program; if not, write to the Free Software
 
17
 *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 
18
 */
 
19
 
 
20
#include <drizzled/server_includes.h>
 
21
#include CSTDINT_H
 
22
#include <drizzled/functions/time/add_time.h>
 
23
#include <drizzled/item/timefunc.h>
 
24
 
 
25
void Item_func_add_time::fix_length_and_dec()
 
26
{
 
27
  enum_field_types arg0_field_type;
 
28
  decimals=0;
 
29
  max_length=MAX_DATETIME_FULL_WIDTH*MY_CHARSET_BIN_MB_MAXLEN;
 
30
  maybe_null= 1;
 
31
 
 
32
  /*
 
33
    The field type for the result of an Item_func_add_time function is defined
 
34
    as follows:
 
35
 
 
36
    - If first arg is a DRIZZLE_TYPE_DATETIME or DRIZZLE_TYPE_TIMESTAMP
 
37
      result is DRIZZLE_TYPE_DATETIME
 
38
    - If first arg is a DRIZZLE_TYPE_TIME result is DRIZZLE_TYPE_TIME
 
39
    - Otherwise the result is DRIZZLE_TYPE_VARCHAR
 
40
  */
 
41
 
 
42
  cached_field_type= DRIZZLE_TYPE_VARCHAR;
 
43
  arg0_field_type= args[0]->field_type();
 
44
  if (arg0_field_type == DRIZZLE_TYPE_DATE ||
 
45
      arg0_field_type == DRIZZLE_TYPE_DATETIME ||
 
46
      arg0_field_type == DRIZZLE_TYPE_TIMESTAMP)
 
47
    cached_field_type= DRIZZLE_TYPE_DATETIME;
 
48
  else if (arg0_field_type == DRIZZLE_TYPE_TIME)
 
49
    cached_field_type= DRIZZLE_TYPE_TIME;
 
50
}
 
51
 
 
52
/**
 
53
  ADDTIME(t,a) and SUBTIME(t,a) are time functions that calculate a
 
54
  time/datetime value 
 
55
 
 
56
  t: time_or_datetime_expression
 
57
  a: time_expression
 
58
  
 
59
  Result: Time value or datetime value
 
60
*/
 
61
 
 
62
String *Item_func_add_time::val_str(String *str)
 
63
{
 
64
  assert(fixed == 1);
 
65
  DRIZZLE_TIME l_time1, l_time2, l_time3;
 
66
  bool is_time= 0;
 
67
  long days, microseconds;
 
68
  int64_t seconds;
 
69
  int l_sign= sign;
 
70
 
 
71
  null_value=0;
 
72
  if (is_date)                        // TIMESTAMP function
 
73
  {
 
74
    if (get_arg0_date(&l_time1, TIME_FUZZY_DATE) || 
 
75
        args[1]->get_time(&l_time2) ||
 
76
        l_time1.time_type == DRIZZLE_TIMESTAMP_TIME || 
 
77
        l_time2.time_type != DRIZZLE_TIMESTAMP_TIME)
 
78
      goto null_date;
 
79
  }
 
80
  else                                // ADDTIME function
 
81
  {
 
82
    if (args[0]->get_time(&l_time1) || 
 
83
        args[1]->get_time(&l_time2) ||
 
84
        l_time2.time_type == DRIZZLE_TIMESTAMP_DATETIME)
 
85
      goto null_date;
 
86
    is_time= (l_time1.time_type == DRIZZLE_TIMESTAMP_TIME);
 
87
  }
 
88
  if (l_time1.neg != l_time2.neg)
 
89
    l_sign= -l_sign;
 
90
  
 
91
  memset(&l_time3, 0, sizeof(l_time3));
 
92
  
 
93
  l_time3.neg= calc_time_diff(&l_time1, &l_time2, -l_sign,
 
94
                              &seconds, &microseconds);
 
95
 
 
96
  /*
 
97
    If first argument was negative and diff between arguments
 
98
    is non-zero we need to swap sign to get proper result.
 
99
  */
 
100
  if (l_time1.neg && (seconds || microseconds))
 
101
    l_time3.neg= 1-l_time3.neg;         // Swap sign of result
 
102
 
 
103
  if (!is_time && l_time3.neg)
 
104
    goto null_date;
 
105
 
 
106
  days= (long)(seconds/86400L);
 
107
 
 
108
  calc_time_from_sec(&l_time3, (long)(seconds%86400L), microseconds);
 
109
 
 
110
  if (!is_time)
 
111
  {
 
112
    get_date_from_daynr(days,&l_time3.year,&l_time3.month,&l_time3.day);
 
113
    if (l_time3.day &&
 
114
        !make_datetime(l_time1.second_part || l_time2.second_part ?
 
115
                       DATE_TIME_MICROSECOND : DATE_TIME,
 
116
                       &l_time3, str))
 
117
      return str;
 
118
    goto null_date;
 
119
  }
 
120
  
 
121
  l_time3.hour+= days*24;
 
122
  if (!make_datetime_with_warn(l_time1.second_part || l_time2.second_part ?
 
123
                               TIME_MICROSECOND : TIME_ONLY,
 
124
                               &l_time3, str))
 
125
    return str;
 
126
 
 
127
null_date:
 
128
  null_value=1;
 
129
  return 0;
 
130
}
 
131
 
 
132
 
 
133
void Item_func_add_time::print(String *str, enum_query_type query_type)
 
134
{
 
135
  if (is_date)
 
136
  {
 
137
    assert(sign > 0);
 
138
    str->append(STRING_WITH_LEN("timestamp("));
 
139
  }
 
140
  else
 
141
  {
 
142
    if (sign > 0)
 
143
      str->append(STRING_WITH_LEN("addtime("));
 
144
    else
 
145
      str->append(STRING_WITH_LEN("subtime("));
 
146
  }
 
147
  args[0]->print(str, query_type);
 
148
  str->append(',');
 
149
  args[1]->print(str, query_type);
 
150
  str->append(')');
 
151
}
 
152