Drizzled Public API Documentation

last_day.cc
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, Inc.
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 <config.h>
21 
22 #include <drizzled/function/time/last_day.h>
23 #include <drizzled/error.h>
24 #include <drizzled/calendar.h>
25 #include <drizzled/temporal.h>
26 
27 #include <sstream>
28 #include <string>
29 
30 namespace drizzled
31 {
32 
39 {
40  assert(fixed);
41 
42  /* We return NULL from LAST_DAY() only when supplied a NULL argument */
43  if (args[0]->null_value)
44  {
45  null_value= true;
46  return false;
47  }
48 
49  /* We use a DateTime to match as many temporal formats as possible. */
50  DateTime temporal;
51  Item_result arg0_result_type= args[0]->result_type();
52 
53  switch (arg0_result_type)
54  {
55  case REAL_RESULT:
56  case DECIMAL_RESULT:
57  /*
58  * For doubles supplied, interpret the arg as a string,
59  * so intentionally fall-through here...
60  * This allows us to accept double parameters like
61  * 19971231235959.01 and interpret it the way MySQL does:
62  * as a TIMESTAMP-like thing with a microsecond component.
63  * Ugh, but need to keep backwards-compat.
64  */
65  case STRING_RESULT:
66  {
67  char buff[DRIZZLE_MAX_LENGTH_DATETIME_AS_STRING];
68  String tmp(buff,sizeof(buff), &my_charset_utf8_bin);
69  String *res= args[0]->val_str(&tmp);
70 
71  if (! res)
72  {
73  /*
74  * Likely a nested function issue where the nested
75  * function had bad input. We rely on the nested
76  * function my_error() and simply return false here.
77  */
78  return false;
79  }
80 
81  if (res != &tmp)
82  {
83  tmp.copy(*res);
84  }
85 
86  if (! temporal.from_string(tmp.c_ptr(), tmp.length()))
87  {
88  /*
89  * Could not interpret the function argument as a temporal value,
90  * so throw an error and return 0
91  */
92  my_error(ER_INVALID_DATETIME_VALUE, MYF(0), tmp.c_ptr());
93  return false;
94  }
95  }
96  break;
97  case INT_RESULT:
98  if (temporal.from_int64_t(args[0]->val_int()))
99  break;
100  /* Intentionally fall-through on invalid conversion from integer */
101  default:
102  {
103  /*
104  * Could not interpret the function argument as a temporal value,
105  * so throw an error and return 0
106  */
107  null_value= true;
108  char buff[DRIZZLE_MAX_LENGTH_DATETIME_AS_STRING];
109  String tmp(buff,sizeof(buff), &my_charset_utf8_bin);
110  String *res;
111 
112  res= args[0]->val_str(&tmp);
113 
114  if (! res)
115  {
116  /*
117  * Likely a nested function issue where the nested
118  * function had bad input. We rely on the nested
119  * function my_error() and simply return false here.
120  */
121  return false;
122  }
123 
124  if (res != &tmp)
125  {
126  tmp.copy(*res);
127  }
128 
129  my_error(ER_INVALID_DATETIME_VALUE, MYF(0), tmp.c_ptr());
130  return false;
131  }
132  }
133  null_value= false;
134 
135  /* Now strip to the last day of the month... */
136  temporal.set_days(days_in_gregorian_year_month(temporal.years(), temporal.months()));
137  to= temporal; /* Operator overload in effect for assign DateTime to Date. */
138 
139  return true;
140 }
141 
142 } /* namespace drizzled */
bool from_string(const char *from, size_t from_len)
Definition: temporal.cc:160
uint32_t days_in_gregorian_year_month(uint32_t year, uint32_t month)
Definition: calendar.cc:317
virtual int64_t val_int()=0
bool fixed
Definition: item.h:120
TODO: Rename this file - func.h is stupid.
bool get_temporal(Date &temporal)
Definition: last_day.cc:38
uint32_t months() const
Definition: temporal.h:146
bool null_value
Definition: item.h:122
void set_days(const uint32_t day)
Definition: temporal.h:140
bool from_int64_t(const int64_t from, bool convert)
Definition: temporal.cc:1144
virtual String * val_str(String *str)=0
uint32_t years() const
Definition: temporal.h:150