~drizzle-trunk/drizzle/development

574.3.22 by Lee
moving functions from drizzled/item/timefunc to drizzled/functions/time
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
1241.9.36 by Monty Taylor
ZOMG. I deleted drizzled/server_includes.h.
20
#include "config.h"
1280.1.10 by Monty Taylor
Put everything in drizzled into drizzled namespace.
21
1502.3.1 by iwamatsu at nigauri
Add cstdio include to files needing it. Fixes the build on some debian
22
#include <cstdio>
23
1237.9.4 by Padraig O'Sullivan
Removed the inclusion of drizzled/field.h in the server_includes header file.
24
#include "drizzled/function/time/date_add_interval.h"
25
#include "drizzled/temporal_interval.h"
1237.9.8 by Monty Taylor
Fixed solaris build.
26
#include "drizzled/time_functions.h"
574.3.22 by Lee
moving functions from drizzled/item/timefunc to drizzled/functions/time
27
1280.1.10 by Monty Taylor
Put everything in drizzled into drizzled namespace.
28
namespace drizzled
29
{
30
574.3.22 by Lee
moving functions from drizzled/item/timefunc to drizzled/functions/time
31
/*
32
   'interval_names' reflects the order of the enumeration interval_type.
33
   See item/time.h
34
 */
35
const char *interval_names[]=
36
{
37
  "year", "quarter", "month", "week", "day",
38
  "hour", "minute", "second", "microsecond",
39
  "year_month", "day_hour", "day_minute",
40
  "day_second", "hour_minute", "hour_second",
41
  "minute_second", "day_microsecond",
42
  "hour_microsecond", "minute_microsecond",
43
  "second_microsecond"
44
};
45
46
47
void Item_date_add_interval::fix_length_and_dec()
48
{
49
  enum_field_types arg0_field_type;
50
51
  collation.set(&my_charset_bin);
52
  maybe_null=1;
1280.1.10 by Monty Taylor
Put everything in drizzled into drizzled namespace.
53
  max_length=DateTime::MAX_STRING_LENGTH*MY_CHARSET_BIN_MB_MAXLEN;
574.3.22 by Lee
moving functions from drizzled/item/timefunc to drizzled/functions/time
54
  value.alloc(max_length);
55
56
  /*
57
    The field type for the result of an Item_date function is defined as
58
    follows:
59
60
    - If first arg is a DRIZZLE_TYPE_DATETIME result is DRIZZLE_TYPE_DATETIME
61
    - If first arg is a DRIZZLE_TYPE_DATE and the interval type uses hours,
62
      minutes or seconds then type is DRIZZLE_TYPE_DATETIME.
63
    - Otherwise the result is DRIZZLE_TYPE_VARCHAR
64
      (This is because you can't know if the string contains a DATE, DRIZZLE_TIME or
65
      DATETIME argument)
66
  */
67
  cached_field_type= DRIZZLE_TYPE_VARCHAR;
68
  arg0_field_type= args[0]->field_type();
69
  if (arg0_field_type == DRIZZLE_TYPE_DATETIME ||
70
      arg0_field_type == DRIZZLE_TYPE_TIMESTAMP)
71
    cached_field_type= DRIZZLE_TYPE_DATETIME;
72
  else if (arg0_field_type == DRIZZLE_TYPE_DATE)
73
  {
74
    if (int_type <= INTERVAL_DAY || int_type == INTERVAL_YEAR_MONTH)
75
      cached_field_type= arg0_field_type;
76
    else
77
      cached_field_type= DRIZZLE_TYPE_DATETIME;
78
  }
79
}
80
81
82
/* Here arg[1] is a Item_interval object */
83
779.1.27 by Monty Taylor
Got rid of __attribute__((unused)) and the like from the .cc files.
84
bool Item_date_add_interval::get_date(DRIZZLE_TIME *ltime, uint32_t )
574.3.22 by Lee
moving functions from drizzled/item/timefunc to drizzled/functions/time
85
{
1280.1.10 by Monty Taylor
Put everything in drizzled into drizzled namespace.
86
  TemporalInterval interval;
1097.2.1 by clint at fewbar
refactoring INTERVAL into C++ class
87
88
  if (args[0]->get_date(ltime, TIME_NO_ZERO_DATE))
1097.2.4 by clint at fewbar
coding standards cleanup
89
    return (null_value= true);
1097.2.1 by clint at fewbar
refactoring INTERVAL into C++ class
90
1097.2.2 by clint at fewbar
style/documentation cleanups and replacing copyright notices
91
  if (interval.initFromItem(args[1], int_type, &value))
1097.2.4 by clint at fewbar
coding standards cleanup
92
    return (null_value= true);
574.3.22 by Lee
moving functions from drizzled/item/timefunc to drizzled/functions/time
93
94
  if (date_sub_interval)
1097.2.10 by clint at fewbar
implementing Jays suggestions for clearer code
95
    interval.toggleNegative();
1097.2.1 by clint at fewbar
refactoring INTERVAL into C++ class
96
1097.2.2 by clint at fewbar
style/documentation cleanups and replacing copyright notices
97
  if ((null_value= interval.addDate(ltime, int_type)))
1097.2.1 by clint at fewbar
refactoring INTERVAL into C++ class
98
    return true;
99
100
  return false;
574.3.22 by Lee
moving functions from drizzled/item/timefunc to drizzled/functions/time
101
}
102
103
String *Item_date_add_interval::val_str(String *str)
104
{
105
  assert(fixed == 1);
106
  DRIZZLE_TIME ltime;
107
108
  if (Item_date_add_interval::get_date(&ltime, TIME_NO_ZERO_DATE))
109
    return 0;
110
111
  if (ltime.time_type == DRIZZLE_TIMESTAMP_DATE)
907.1.2 by Jay Pipes
Merging in old r902 temporal changes
112
    make_date(&ltime, str);
574.3.22 by Lee
moving functions from drizzled/item/timefunc to drizzled/functions/time
113
  else if (ltime.second_part)
907.1.2 by Jay Pipes
Merging in old r902 temporal changes
114
  {
907.1.4 by Jay Pipes
Merging in old r904 temporal changes
115
    /* Ensure we've got enough room for our timestamp string. */
1280.1.10 by Monty Taylor
Put everything in drizzled into drizzled namespace.
116
    str->length(DateTime::MAX_STRING_LENGTH);
117
    size_t length= snprintf(str->c_ptr(), DateTime::MAX_STRING_LENGTH,
1079.3.2 by Stewart Smith
Replace MAX_(DATE|TIME).*_WIDTH defines in definitions.h with real (and correct) static const members to Temporal types.
118
                            "%04u-%02u-%02u %02u:%02u:%02u.%06u",
119
                            ltime.year,
120
                            ltime.month,
121
                            ltime.day,
122
                            ltime.hour,
123
                            ltime.minute,
124
                            ltime.second,
125
                            (uint32_t) ltime.second_part);
907.1.2 by Jay Pipes
Merging in old r902 temporal changes
126
    str->length(length);
127
    str->set_charset(&my_charset_bin);
128
  }
574.3.22 by Lee
moving functions from drizzled/item/timefunc to drizzled/functions/time
129
  else
907.1.2 by Jay Pipes
Merging in old r902 temporal changes
130
    make_datetime(&ltime, str);
131
132
  return str;
574.3.22 by Lee
moving functions from drizzled/item/timefunc to drizzled/functions/time
133
}
134
135
int64_t Item_date_add_interval::val_int()
136
{
137
  assert(fixed == 1);
138
  DRIZZLE_TIME ltime;
139
  int64_t date;
140
  if (Item_date_add_interval::get_date(&ltime, TIME_NO_ZERO_DATE))
141
    return (int64_t) 0;
142
  date = (ltime.year*100L + ltime.month)*100L + ltime.day;
143
  return ltime.time_type == DRIZZLE_TIMESTAMP_DATE ? date :
144
    ((date*100L + ltime.hour)*100L+ ltime.minute)*100L + ltime.second;
145
}
146
147
148
149
bool Item_date_add_interval::eq(const Item *item, bool binary_cmp) const
150
{
151
  Item_date_add_interval *other= (Item_date_add_interval*) item;
152
  if (!Item_func::eq(item, binary_cmp))
153
    return 0;
154
  return ((int_type == other->int_type) &&
155
          (date_sub_interval == other->date_sub_interval));
156
}
157
158
void Item_date_add_interval::print(String *str, enum_query_type query_type)
159
{
160
  str->append('(');
161
  args[0]->print(str, query_type);
162
  str->append(date_sub_interval?" - interval ":" + interval ");
163
  args[1]->print(str, query_type);
164
  str->append(' ');
165
  str->append(interval_names[int_type]);
166
  str->append(')');
167
}
1280.1.10 by Monty Taylor
Put everything in drizzled into drizzled namespace.
168
169
} /* namespace drizzled */