~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to drizzled/function/time/extract.cc

  • Committer: Monty Taylor
  • Date: 2008-08-02 01:03:15 UTC
  • mto: (236.1.42 codestyle)
  • mto: This revision was merged to the branch mainline in revision 261.
  • Revision ID: monty@inaugust.com-20080802010315-65h5938pymg9d99z
Moved m4 macros to top-level m4 dir, per GNU standards (and where gettext wanted it :)

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/function/time/week_mode.h>
23
 
#include <drizzled/function/time/extract.h>
24
 
#include <drizzled/session.h>
25
 
 
26
 
/*
27
 
   'interval_names' reflects the order of the enumeration interval_type.
28
 
   See item/time.h
29
 
 */
30
 
 
31
 
extern const char *interval_names[];
32
 
/*
33
 
static const char *interval_names[]=
34
 
{
35
 
  "year", "quarter", "month", "week", "day",
36
 
  "hour", "minute", "second", "microsecond",
37
 
  "year_month", "day_hour", "day_minute",
38
 
  "day_second", "hour_minute", "hour_second",
39
 
  "minute_second", "day_microsecond",
40
 
  "hour_microsecond", "minute_microsecond",
41
 
  "second_microsecond"
42
 
};
43
 
*/
44
 
 
45
 
void Item_extract::print(String *str, enum_query_type query_type)
46
 
{
47
 
  str->append(STRING_WITH_LEN("extract("));
48
 
  str->append(interval_names[int_type]);
49
 
  str->append(STRING_WITH_LEN(" from "));
50
 
  args[0]->print(str, query_type);
51
 
  str->append(')');
52
 
}
53
 
 
54
 
void Item_extract::fix_length_and_dec()
55
 
{
56
 
  value.alloc(32);                              // alloc buffer
57
 
 
58
 
  maybe_null=1;                                 // If wrong date
59
 
  switch (int_type) {
60
 
  case INTERVAL_YEAR:           max_length=4; date_value=1; break;
61
 
  case INTERVAL_YEAR_MONTH:     max_length=6; date_value=1; break;
62
 
  case INTERVAL_QUARTER:        max_length=2; date_value=1; break;
63
 
  case INTERVAL_MONTH:          max_length=2; date_value=1; break;
64
 
  case INTERVAL_WEEK:           max_length=2; date_value=1; break;
65
 
  case INTERVAL_DAY:            max_length=2; date_value=1; break;
66
 
  case INTERVAL_DAY_HOUR:       max_length=9; date_value=0; break;
67
 
  case INTERVAL_DAY_MINUTE:     max_length=11; date_value=0; break;
68
 
  case INTERVAL_DAY_SECOND:     max_length=13; date_value=0; break;
69
 
  case INTERVAL_HOUR:           max_length=2; date_value=0; break;
70
 
  case INTERVAL_HOUR_MINUTE:    max_length=4; date_value=0; break;
71
 
  case INTERVAL_HOUR_SECOND:    max_length=6; date_value=0; break;
72
 
  case INTERVAL_MINUTE:         max_length=2; date_value=0; break;
73
 
  case INTERVAL_MINUTE_SECOND:  max_length=4; date_value=0; break;
74
 
  case INTERVAL_SECOND:         max_length=2; date_value=0; break;
75
 
  case INTERVAL_MICROSECOND:    max_length=2; date_value=0; break;
76
 
  case INTERVAL_DAY_MICROSECOND: max_length=20; date_value=0; break;
77
 
  case INTERVAL_HOUR_MICROSECOND: max_length=13; date_value=0; break;
78
 
  case INTERVAL_MINUTE_MICROSECOND: max_length=11; date_value=0; break;
79
 
  case INTERVAL_SECOND_MICROSECOND: max_length=9; date_value=0; break;
80
 
  case INTERVAL_LAST: assert(0); break; /* purecov: deadcode */
81
 
  }
82
 
}
83
 
 
84
 
 
85
 
int64_t Item_extract::val_int()
86
 
{
87
 
  assert(fixed == 1);
88
 
  DRIZZLE_TIME ltime;
89
 
  uint32_t year;
90
 
  ulong week_format;
91
 
  long neg;
92
 
  if (date_value)
93
 
  {
94
 
    if (get_arg0_date(&ltime, TIME_FUZZY_DATE))
95
 
      return 0;
96
 
    neg=1;
97
 
  }
98
 
  else
99
 
  {
100
 
    String *res= args[0]->val_str(&value);
101
 
    if (!res || str_to_time_with_warn(res->ptr(), res->length(), &ltime))
102
 
    {
103
 
      null_value=1;
104
 
      return 0;
105
 
    }
106
 
    neg= ltime.neg ? -1 : 1;
107
 
    null_value=0;
108
 
  }
109
 
  switch (int_type) {
110
 
  case INTERVAL_YEAR:           return ltime.year;
111
 
  case INTERVAL_YEAR_MONTH:     return ltime.year*100L+ltime.month;
112
 
  case INTERVAL_QUARTER:        return (ltime.month+2)/3;
113
 
  case INTERVAL_MONTH:          return ltime.month;
114
 
  case INTERVAL_WEEK:
115
 
  {
116
 
    week_format= current_session->variables.default_week_format;
117
 
    return calc_week(&ltime, week_mode(week_format), &year);
118
 
  }
119
 
  case INTERVAL_DAY:            return ltime.day;
120
 
  case INTERVAL_DAY_HOUR:       return (long) (ltime.day*100L+ltime.hour)*neg;
121
 
  case INTERVAL_DAY_MINUTE:     return (long) (ltime.day*10000L+
122
 
                                               ltime.hour*100L+
123
 
                                               ltime.minute)*neg;
124
 
  case INTERVAL_DAY_SECOND:      return ((int64_t) ltime.day*1000000L+
125
 
                                         (int64_t) (ltime.hour*10000L+
126
 
                                                     ltime.minute*100+
127
 
                                                     ltime.second))*neg;
128
 
  case INTERVAL_HOUR:           return (long) ltime.hour*neg;
129
 
  case INTERVAL_HOUR_MINUTE:    return (long) (ltime.hour*100+ltime.minute)*neg;
130
 
  case INTERVAL_HOUR_SECOND:    return (long) (ltime.hour*10000+ltime.minute*100+
131
 
                                               ltime.second)*neg;
132
 
  case INTERVAL_MINUTE:         return (long) ltime.minute*neg;
133
 
  case INTERVAL_MINUTE_SECOND:  return (long) (ltime.minute*100+ltime.second)*neg;
134
 
  case INTERVAL_SECOND:         return (long) ltime.second*neg;
135
 
  case INTERVAL_MICROSECOND:    return (long) ltime.second_part*neg;
136
 
  case INTERVAL_DAY_MICROSECOND: return (((int64_t)ltime.day*1000000L +
137
 
                                          (int64_t)ltime.hour*10000L +
138
 
                                          ltime.minute*100 +
139
 
                                          ltime.second)*1000000L +
140
 
                                         ltime.second_part)*neg;
141
 
  case INTERVAL_HOUR_MICROSECOND: return (((int64_t)ltime.hour*10000L +
142
 
                                           ltime.minute*100 +
143
 
                                           ltime.second)*1000000L +
144
 
                                          ltime.second_part)*neg;
145
 
  case INTERVAL_MINUTE_MICROSECOND: return (((int64_t)(ltime.minute*100+
146
 
                                                        ltime.second))*1000000L+
147
 
                                            ltime.second_part)*neg;
148
 
  case INTERVAL_SECOND_MICROSECOND: return ((int64_t)ltime.second*1000000L+
149
 
                                            ltime.second_part)*neg;
150
 
  case INTERVAL_LAST: assert(0); break;  /* purecov: deadcode */
151
 
  }
152
 
  return 0;                                     // Impossible
153
 
}
154
 
 
155
 
bool Item_extract::eq(const Item *item, bool binary_cmp) const
156
 
{
157
 
  if (this == item)
158
 
    return 1;
159
 
  if (item->type() != FUNC_ITEM ||
160
 
      functype() != ((Item_func*)item)->functype())
161
 
    return 0;
162
 
 
163
 
  Item_extract* ie= (Item_extract*)item;
164
 
  if (ie->int_type != int_type)
165
 
    return 0;
166
 
 
167
 
  if (!args[0]->eq(ie->args[0], binary_cmp))
168
 
      return 0;
169
 
  return 1;
170
 
}
171