drizzle
C++ MySQL-protocol-compatible database server
Loading...
Searching...
No Matches
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
30namespace 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 /* fallthrough */
102 default:
103 {
104 /*
105 * Could not interpret the function argument as a temporal value,
106 * so throw an error and return 0
107 */
108 null_value= true;
109 char buff[DRIZZLE_MAX_LENGTH_DATETIME_AS_STRING];
110 String tmp(buff,sizeof(buff), &my_charset_utf8_bin);
111 String *res;
112
113 res= args[0]->val_str(&tmp);
114
115 if (! res)
116 {
117 /*
118 * Likely a nested function issue where the nested
119 * function had bad input. We rely on the nested
120 * function my_error() and simply return false here.
121 */
122 return false;
123 }
124
125 if (res != &tmp)
126 {
127 tmp.copy(*res);
128 }
129
130 my_error(ER_INVALID_DATETIME_VALUE, MYF(0), tmp.c_ptr());
131 return false;
132 }
133 }
134 null_value= false;
135
136 /* Now strip to the last day of the month... */
137 temporal.set_days(days_in_gregorian_year_month(temporal.years(), temporal.months()));
138 to= temporal; /* Operator overload in effect for assign DateTime to Date. */
139
140 return true;
141}
142
143} /* namespace drizzled */
bool from_string(const char *from, size_t from_len)
Definition temporal.cc:122
bool from_int64_t(const int64_t from, bool convert)
Definition temporal.cc:1106
bool get_temporal(Date &temporal)
Definition last_day.cc:38
bool fixed
Definition item.h:122
bool null_value
Definition item.h:124
void set_days(const uint32_t day)
Definition temporal.h:140
uint32_t months() const
Definition temporal.h:146
uint32_t years() const
Definition temporal.h:150
TODO: Rename this file - func.h is stupid.
uint32_t days_in_gregorian_year_month(uint32_t year, uint32_t month)
Definition calendar.cc:309