~posulliv/drizzle/memcached_applier

« back to all changes in this revision

Viewing changes to drizzled/temporal_interval.cc

  • Committer: Jay Pipes
  • Date: 2009-08-03 14:23:22 UTC
  • mfrom: (1039.2.68 staging)
  • mto: This revision was merged to the branch mainline in revision 1078.
  • Revision ID: jpipes@serialcoder-20090803142322-1g67h7su9mocg9ig
Merge trunk

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/global.h>
 
21
#include <drizzled/error.h>
 
22
#include <drizzled/session.h>
 
23
#include <drizzled/server_includes.h>
 
24
#include <drizzled/function/time/date.h>
 
25
#include <drizzled/temporal_interval.h>
 
26
 
 
27
bool drizzled::TemporalInterval::initFromItem(Item *args, interval_type int_type, String *str_value)
 
28
{
 
29
  uint64_t array[MAX_STRING_ELEMENTS];
 
30
  int64_t value= 0;
 
31
  const char *str= NULL;
 
32
  size_t length= 0;
 
33
  const CHARSET_INFO * const cs= str_value->charset();
 
34
 
 
35
 
 
36
  // Types <= microsecond can be converted as an integer
 
37
  if (static_cast<int>(int_type) <= INTERVAL_MICROSECOND)
 
38
  {
 
39
    value= args->val_int();
 
40
    if (args->null_value)
 
41
      return true;
 
42
    if (value < 0)
 
43
    {
 
44
      neg= true;
 
45
      value= -value;
 
46
    }
 
47
  }
 
48
  else
 
49
  {
 
50
    // Otherwise we must convert to a string and extract the multiple parts
 
51
    String *res;
 
52
    if (!(res= args->val_str(str_value)))
 
53
      return true;
 
54
 
 
55
    // record negative intervalls in interval->neg 
 
56
    str= res->ptr();
 
57
    const char *end= str+res->length();
 
58
    // Skip the whitespace
 
59
    while (str != end && my_isspace(cs,*str))
 
60
      str++;
 
61
    if (str != end && *str == '-')
 
62
    {
 
63
      neg= true;
 
64
      // skip the -
 
65
      str++;
 
66
    }
 
67
    length= static_cast<size_t>(end-str);               // Set up pointers to new str
 
68
  }
 
69
 
 
70
  switch (int_type)
 
71
  {
 
72
  case INTERVAL_YEAR:
 
73
    year= static_cast<uint32_t>(value);
 
74
    break;
 
75
  case INTERVAL_QUARTER:
 
76
    month= static_cast<uint32_t>(value*3);
 
77
    break;
 
78
  case INTERVAL_MONTH:
 
79
    month= static_cast<uint32_t>(value);
 
80
    break;
 
81
  case INTERVAL_WEEK:
 
82
    day= static_cast<uint32_t>(value*7);
 
83
    break;
 
84
  case INTERVAL_DAY:
 
85
    day= static_cast<uint32_t>(value);
 
86
    break;
 
87
  case INTERVAL_HOUR:
 
88
    hour= static_cast<uint32_t>(value);
 
89
    break;
 
90
  case INTERVAL_MICROSECOND:
 
91
    second_part= value;
 
92
    break;
 
93
  case INTERVAL_MINUTE:
 
94
    minute= value;
 
95
    break;
 
96
  case INTERVAL_SECOND:
 
97
    second= value;
 
98
    break;
 
99
  case INTERVAL_YEAR_MONTH:                     // Allow YEAR-MONTH YYYYYMM
 
100
    if (getIntervalFromString(str,length,cs,NUM_YEAR_MONTH_STRING_ELEMENTS,array,false))
 
101
      return true;
 
102
    year=  static_cast<uint32_t>(array[0]);
 
103
    month= static_cast<uint32_t>(array[1]);
 
104
    break;
 
105
  case INTERVAL_DAY_HOUR:
 
106
    if (getIntervalFromString(str,length,cs,NUM_DAY_HOUR_STRING_ELEMENTS,array,false))
 
107
      return true;
 
108
    day=  static_cast<uint32_t>(array[0]);
 
109
    hour= static_cast<uint32_t>(array[1]);
 
110
    break;
 
111
  case INTERVAL_DAY_MICROSECOND:
 
112
    if (getIntervalFromString(str,length,cs,NUM_DAY_MICROSECOND_STRING_ELEMENTS,array,true))
 
113
      return true;
 
114
    day=    static_cast<uint32_t>(array[0]);
 
115
    hour=   static_cast<uint32_t>(array[1]);
 
116
    minute= array[2];
 
117
    second= array[3];
 
118
    second_part= array[4];
 
119
    break;
 
120
  case INTERVAL_DAY_MINUTE:
 
121
    if (getIntervalFromString(str,length,cs,NUM_DAY_MINUTE_STRING_ELEMENTS,array,false))
 
122
      return true;
 
123
    day=    static_cast<uint32_t>(array[0]);
 
124
    hour=   static_cast<uint32_t>(array[1]);
 
125
    minute= array[2];
 
126
    break;
 
127
  case INTERVAL_DAY_SECOND:
 
128
    if (getIntervalFromString(str,length,cs,NUM_DAY_SECOND_STRING_ELEMENTS,array,false))
 
129
      return true;
 
130
    day=    static_cast<uint32_t>(array[0]);
 
131
    hour=   static_cast<uint32_t>(array[1]);
 
132
    minute= array[2];
 
133
    second= array[3];
 
134
    break;
 
135
  case INTERVAL_HOUR_MICROSECOND:
 
136
    if (getIntervalFromString(str,length,cs,NUM_HOUR_MICROSECOND_STRING_ELEMENTS,array,true))
 
137
      return true;
 
138
    hour=   static_cast<uint32_t>(array[0]);
 
139
    minute= array[1];
 
140
    second= array[2];
 
141
    second_part= array[3];
 
142
    break;
 
143
  case INTERVAL_HOUR_MINUTE:
 
144
    if (getIntervalFromString(str,length,cs,NUM_HOUR_MINUTE_STRING_ELEMENTS,array,false))
 
145
      return true;
 
146
    hour=   static_cast<uint32_t>(array[0]);
 
147
    minute= array[1];
 
148
    break;
 
149
  case INTERVAL_HOUR_SECOND:
 
150
    if (getIntervalFromString(str,length,cs,NUM_HOUR_SECOND_STRING_ELEMENTS,array,false))
 
151
      return true;
 
152
    hour=   static_cast<uint32_t>(array[0]);
 
153
    minute= array[1];
 
154
    second= array[2];
 
155
    break;
 
156
  case INTERVAL_MINUTE_MICROSECOND:
 
157
    if (getIntervalFromString(str,length,cs,NUM_MINUTE_MICROSECOND_STRING_ELEMENTS,array,true))
 
158
      return true;
 
159
    minute= array[0];
 
160
    second= array[1];
 
161
    second_part= array[2];
 
162
    break;
 
163
  case INTERVAL_MINUTE_SECOND:
 
164
    if (getIntervalFromString(str,length,cs,NUM_MINUTE_SECOND_STRING_ELEMENTS,array,false))
 
165
      return true;
 
166
    minute= array[0];
 
167
    second= array[1];
 
168
    break;
 
169
  case INTERVAL_SECOND_MICROSECOND:
 
170
    if (getIntervalFromString(str,length,cs,NUM_SECOND_MICROSECOND_STRING_ELEMENTS,array,true))
 
171
      return true;
 
172
    second= array[0];
 
173
    second_part= array[1];
 
174
    break;
 
175
  case INTERVAL_LAST: // purecov: begin deadcode
 
176
    assert(0);
 
177
    break;            // purecov: end
 
178
  }
 
179
  return false;
 
180
}
 
181
 
 
182
bool drizzled::TemporalInterval::addDate(DRIZZLE_TIME *ltime, interval_type int_type)
 
183
{
 
184
  long period, sign;
 
185
 
 
186
  ltime->neg= 0;
 
187
 
 
188
  sign= (neg ? -1 : 1);
 
189
 
 
190
  switch (int_type)
 
191
  {
 
192
  case INTERVAL_SECOND:
 
193
  case INTERVAL_SECOND_MICROSECOND:
 
194
  case INTERVAL_MICROSECOND:
 
195
  case INTERVAL_MINUTE:
 
196
  case INTERVAL_HOUR:
 
197
  case INTERVAL_MINUTE_MICROSECOND:
 
198
  case INTERVAL_MINUTE_SECOND:
 
199
  case INTERVAL_HOUR_MICROSECOND:
 
200
  case INTERVAL_HOUR_SECOND:
 
201
  case INTERVAL_HOUR_MINUTE:
 
202
  case INTERVAL_DAY_MICROSECOND:
 
203
  case INTERVAL_DAY_SECOND:
 
204
  case INTERVAL_DAY_MINUTE:
 
205
  case INTERVAL_DAY_HOUR:
 
206
    int64_t sec, days, daynr, microseconds, extra_sec;
 
207
    ltime->time_type= DRIZZLE_TIMESTAMP_DATETIME; // Return full date
 
208
    microseconds= ltime->second_part + sign*second_part;
 
209
    extra_sec= microseconds/1000000L;
 
210
    microseconds= microseconds%1000000L;
 
211
 
 
212
    sec= ((ltime->day-1)*3600*24L+ltime->hour*3600+ltime->minute*60+
 
213
        ltime->second +
 
214
        sign* (int64_t) (day*3600*24L +
 
215
          hour*3600L+minute*60L+
 
216
          second))+ extra_sec;
 
217
    if (microseconds < 0)
 
218
    {
 
219
      microseconds+= 1000000L;
 
220
      sec--;
 
221
    }
 
222
    days= sec/(3600*24L);
 
223
    sec-= days*3600*24L;
 
224
    if (sec < 0)
 
225
    {
 
226
      days--;
 
227
      sec+= 3600*24L;
 
228
    }
 
229
    ltime->second_part= (uint32_t) microseconds;
 
230
    ltime->second= (uint32_t) (sec % 60);
 
231
    ltime->minute= (uint32_t) (sec/60 % 60);
 
232
    ltime->hour=   (uint32_t) (sec/3600);
 
233
    daynr= calc_daynr(ltime->year,ltime->month,1) + days;
 
234
    /* Day number from year 0 to 9999-12-31 */
 
235
    if ((uint64_t) daynr > MAX_DAY_NUMBER)
 
236
      goto invalid_date;
 
237
    get_date_from_daynr((long) daynr, &ltime->year, &ltime->month,
 
238
        &ltime->day);
 
239
    break;
 
240
  case INTERVAL_DAY:
 
241
  case INTERVAL_WEEK:
 
242
    period= (calc_daynr(ltime->year,ltime->month,ltime->day) +
 
243
        sign * (long) day);
 
244
    /* Daynumber from year 0 to 9999-12-31 */
 
245
    if (period > MAX_DAY_NUMBER)
 
246
      goto invalid_date;
 
247
    get_date_from_daynr((long) period,&ltime->year,&ltime->month,&ltime->day);
 
248
    break;
 
249
  case INTERVAL_YEAR:
 
250
    ltime->year+= sign * (long) year;
 
251
    if (ltime->year >= 10000L)
 
252
      goto invalid_date;
 
253
    if (ltime->month == 2 && ltime->day == 29 &&
 
254
        calc_days_in_year(ltime->year) != 366)
 
255
      ltime->day= 28;                           // Was leap-year
 
256
    break;
 
257
  case INTERVAL_YEAR_MONTH:
 
258
  case INTERVAL_QUARTER:
 
259
  case INTERVAL_MONTH:
 
260
    period= (ltime->year*12 + sign * (long) year*12 +
 
261
        ltime->month-1 + sign * (long) month);
 
262
    if (period >= 120000L)
 
263
      goto invalid_date;
 
264
    ltime->year= (uint32_t) (period / 12);
 
265
    ltime->month= (uint32_t) (period % 12L)+1;
 
266
    /* Adjust day if the new month doesn't have enough days */
 
267
    if (ltime->day > days_in_month[ltime->month-1])
 
268
    {
 
269
      ltime->day= days_in_month[ltime->month-1];
 
270
      if (ltime->month == 2 && calc_days_in_year(ltime->year) == 366)
 
271
        ltime->day++;                           // Leap-year
 
272
    }
 
273
    break;
 
274
  default:
 
275
    goto null_date;
 
276
  }
 
277
 
 
278
  return 0;                                     // Ok
 
279
 
 
280
invalid_date:
 
281
  push_warning_printf(current_session, DRIZZLE_ERROR::WARN_LEVEL_WARN,
 
282
      ER_DATETIME_FUNCTION_OVERFLOW,
 
283
      ER(ER_DATETIME_FUNCTION_OVERFLOW),
 
284
      "datetime");
 
285
null_date:
 
286
  return 1;
 
287
}
 
288
 
 
289
bool drizzled::TemporalInterval::getIntervalFromString(const char *str,uint32_t length, const CHARSET_INFO * const cs,
 
290
    uint32_t count, uint64_t *values,
 
291
    bool transform_msec)
 
292
{
 
293
  const char *end= str+length;
 
294
  uint32_t x;
 
295
 
 
296
  while (str != end && !my_isdigit(cs,*str))
 
297
    str++;
 
298
 
 
299
  for (x= 0 ; x < count ; x++)
 
300
  {
 
301
    int64_t value;
 
302
    const char *start= str;
 
303
    for (value= 0 ; str != end && my_isdigit(cs,*str) ; str++)
 
304
      value= value * 10L + (int64_t) (*str - '0');
 
305
    if (transform_msec && x == count - 1) // microseconds always last
 
306
    {
 
307
      long msec_length= 6 - (str - start);
 
308
      if (msec_length > 0)
 
309
        value*= (long) log_10_int[msec_length];
 
310
    }
 
311
    values[x]= value;
 
312
    while (str != end && !my_isdigit(cs,*str))
 
313
      str++;
 
314
    if (str == end && x != count-1)
 
315
    {
 
316
      x++;
 
317
      /* Change values[0...x-1] -> values[0...count-1] */
 
318
      bmove_upp((unsigned char*) (values+count), (unsigned char*) (values+x),
 
319
          sizeof(*values)*x);
 
320
      memset(values, 0, sizeof(*values)*(count-x));
 
321
      break;
 
322
    }
 
323
  }
 
324
  return (str != end);
 
325
}