~drizzle-trunk/drizzle/development

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
/* -*- mode: c++; c-basic-offset: 2; indent-tabs-mode: nil; -*-
 *  vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
 *
 *  Copyright (C) 2008 Sun Microsystems
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; version 2 of the License.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 */

#include "config.h"
#include "drizzled/internal/m_string.h"
#include "drizzled/error.h"
#include "drizzled/session.h"
#include "config.h"
#include "drizzled/function/time/date.h"
#include "drizzled/temporal_interval.h"
#include "drizzled/time_functions.h"

namespace drizzled
{

bool TemporalInterval::initFromItem(Item *args,
                                    interval_type int_type,
                                    String *str_value)
{
  uint64_t array[MAX_STRING_ELEMENTS];
  int64_t value= 0;
  const char *str= NULL;
  size_t length= 0;
  const CHARSET_INFO * const cs= str_value->charset();


  // Types <= microsecond can be converted as an integer
  if (static_cast<int>(int_type) <= INTERVAL_MICROSECOND)
  {
    value= args->val_int();
    if (args->null_value)
      return true;
    if (value < 0)
    {
      neg= true;
      value= -value;
    }
  }
  else
  {
    // Otherwise we must convert to a string and extract the multiple parts
    String *res;
    if (!(res= args->val_str(str_value)))
      return true;

    // record negative intervalls in interval->neg 
    str= res->ptr();
    const char *end= str+res->length();
    // Skip the whitespace
    while (str != end && my_isspace(cs,*str))
      str++;
    if (str != end && *str == '-')
    {
      neg= true;
      // skip the -
      str++;
    }
    length= static_cast<size_t>(end-str);		// Set up pointers to new str
  }

  switch (int_type)
  {
  case INTERVAL_YEAR:
    year= static_cast<uint32_t>(value);
    break;
  case INTERVAL_QUARTER:
    month= static_cast<uint32_t>(value*3);
    break;
  case INTERVAL_MONTH:
    month= static_cast<uint32_t>(value);
    break;
  case INTERVAL_WEEK:
    day= static_cast<uint32_t>(value*7);
    break;
  case INTERVAL_DAY:
    day= static_cast<uint32_t>(value);
    break;
  case INTERVAL_HOUR:
    hour= static_cast<uint32_t>(value);
    break;
  case INTERVAL_MICROSECOND:
    second_part= value;
    break;
  case INTERVAL_MINUTE:
    minute= value;
    break;
  case INTERVAL_SECOND:
    second= value;
    break;
  case INTERVAL_YEAR_MONTH:			// Allow YEAR-MONTH YYYYYMM
    if (getIntervalFromString(str,length,cs,NUM_YEAR_MONTH_STRING_ELEMENTS,array,false))
      return true;
    year=  static_cast<uint32_t>(array[0]);
    month= static_cast<uint32_t>(array[1]);
    break;
  case INTERVAL_DAY_HOUR:
    if (getIntervalFromString(str,length,cs,NUM_DAY_HOUR_STRING_ELEMENTS,array,false))
      return true;
    day=  static_cast<uint32_t>(array[0]);
    hour= static_cast<uint32_t>(array[1]);
    break;
  case INTERVAL_DAY_MICROSECOND:
    if (getIntervalFromString(str,length,cs,NUM_DAY_MICROSECOND_STRING_ELEMENTS,array,true))
      return true;
    day=    static_cast<uint32_t>(array[0]);
    hour=   static_cast<uint32_t>(array[1]);
    minute= array[2];
    second= array[3];
    second_part= array[4];
    break;
  case INTERVAL_DAY_MINUTE:
    if (getIntervalFromString(str,length,cs,NUM_DAY_MINUTE_STRING_ELEMENTS,array,false))
      return true;
    day=    static_cast<uint32_t>(array[0]);
    hour=   static_cast<uint32_t>(array[1]);
    minute= array[2];
    break;
  case INTERVAL_DAY_SECOND:
    if (getIntervalFromString(str,length,cs,NUM_DAY_SECOND_STRING_ELEMENTS,array,false))
      return true;
    day=    static_cast<uint32_t>(array[0]);
    hour=   static_cast<uint32_t>(array[1]);
    minute= array[2];
    second= array[3];
    break;
  case INTERVAL_HOUR_MICROSECOND:
    if (getIntervalFromString(str,length,cs,NUM_HOUR_MICROSECOND_STRING_ELEMENTS,array,true))
      return true;
    hour=   static_cast<uint32_t>(array[0]);
    minute= array[1];
    second= array[2];
    second_part= array[3];
    break;
  case INTERVAL_HOUR_MINUTE:
    if (getIntervalFromString(str,length,cs,NUM_HOUR_MINUTE_STRING_ELEMENTS,array,false))
      return true;
    hour=   static_cast<uint32_t>(array[0]);
    minute= array[1];
    break;
  case INTERVAL_HOUR_SECOND:
    if (getIntervalFromString(str,length,cs,NUM_HOUR_SECOND_STRING_ELEMENTS,array,false))
      return true;
    hour=   static_cast<uint32_t>(array[0]);
    minute= array[1];
    second= array[2];
    break;
  case INTERVAL_MINUTE_MICROSECOND:
    if (getIntervalFromString(str,length,cs,NUM_MINUTE_MICROSECOND_STRING_ELEMENTS,array,true))
      return true;
    minute= array[0];
    second= array[1];
    second_part= array[2];
    break;
  case INTERVAL_MINUTE_SECOND:
    if (getIntervalFromString(str,length,cs,NUM_MINUTE_SECOND_STRING_ELEMENTS,array,false))
      return true;
    minute= array[0];
    second= array[1];
    break;
  case INTERVAL_SECOND_MICROSECOND:
    if (getIntervalFromString(str,length,cs,NUM_SECOND_MICROSECOND_STRING_ELEMENTS,array,true))
      return true;
    second= array[0];
    second_part= array[1];
    break;
  case INTERVAL_LAST:
    assert(0);
    break;
  }
  return false;
}

bool TemporalInterval::addDate(DRIZZLE_TIME *ltime, interval_type int_type)
{
  long period, sign;

  ltime->neg= 0;

  sign= (neg ? -1 : 1);

  switch (int_type)
  {
  case INTERVAL_SECOND:
  case INTERVAL_SECOND_MICROSECOND:
  case INTERVAL_MICROSECOND:
  case INTERVAL_MINUTE:
  case INTERVAL_HOUR:
  case INTERVAL_MINUTE_MICROSECOND:
  case INTERVAL_MINUTE_SECOND:
  case INTERVAL_HOUR_MICROSECOND:
  case INTERVAL_HOUR_SECOND:
  case INTERVAL_HOUR_MINUTE:
  case INTERVAL_DAY_MICROSECOND:
  case INTERVAL_DAY_SECOND:
  case INTERVAL_DAY_MINUTE:
  case INTERVAL_DAY_HOUR:
    int64_t sec, days, daynr, microseconds, extra_sec;
    ltime->time_type= DRIZZLE_TIMESTAMP_DATETIME; // Return full date
    microseconds= ltime->second_part + sign*second_part;
    extra_sec= microseconds/1000000L;
    microseconds= microseconds%1000000L;

    sec= ((ltime->day-1)*3600*24L+ltime->hour*3600+ltime->minute*60+
        ltime->second +
        sign* (int64_t) (day*3600*24L +
          hour*3600L+minute*60L+
          second))+ extra_sec;
    if (microseconds < 0)
    {
      microseconds+= 1000000L;
      sec--;
    }
    days= sec/(3600*24L);
    sec-= days*3600*24L;
    if (sec < 0)
    {
      days--;
      sec+= 3600*24L;
    }
    ltime->second_part= (uint32_t) microseconds;
    ltime->second= (uint32_t) (sec % 60);
    ltime->minute= (uint32_t) (sec/60 % 60);
    ltime->hour=   (uint32_t) (sec/3600);
    daynr= calc_daynr(ltime->year,ltime->month,1) + days;
    /* Day number from year 0 to 9999-12-31 */
    if ((uint64_t) daynr > MAX_DAY_NUMBER)
      goto invalid_date;
    get_date_from_daynr((long) daynr, &ltime->year, &ltime->month,
        &ltime->day);
    break;
  case INTERVAL_DAY:
  case INTERVAL_WEEK:
    period= (calc_daynr(ltime->year,ltime->month,ltime->day) +
        sign * (long) day);
    /* Daynumber from year 0 to 9999-12-31 */
    if (period > MAX_DAY_NUMBER)
      goto invalid_date;
    get_date_from_daynr((long) period,&ltime->year,&ltime->month,&ltime->day);
    break;
  case INTERVAL_YEAR:
    ltime->year+= sign * (long) year;
    if (ltime->year >= 10000L)
      goto invalid_date;
    if (ltime->month == 2 && ltime->day == 29 &&
        calc_days_in_year(ltime->year) != 366)
      ltime->day= 28;				// Was leap-year
    break;
  case INTERVAL_YEAR_MONTH:
  case INTERVAL_QUARTER:
  case INTERVAL_MONTH:
    period= (ltime->year*12 + sign * (long) year*12 +
        ltime->month-1 + sign * (long) month);
    if (period >= 120000L)
      goto invalid_date;
    ltime->year= (uint32_t) (period / 12);
    ltime->month= (uint32_t) (period % 12L)+1;
    /* Adjust day if the new month doesn't have enough days */
    if (ltime->day > days_in_month[ltime->month-1])
    {
      ltime->day= days_in_month[ltime->month-1];
      if (ltime->month == 2 && calc_days_in_year(ltime->year) == 366)
        ltime->day++;				// Leap-year
    }
    break;
  default:
    goto null_date;
  }

  return 0;					// Ok

invalid_date:
  push_warning_printf(current_session, DRIZZLE_ERROR::WARN_LEVEL_WARN,
      ER_DATETIME_FUNCTION_OVERFLOW,
      ER(ER_DATETIME_FUNCTION_OVERFLOW),
      "datetime");
null_date:
  return 1;
}

bool TemporalInterval::getIntervalFromString(const char *str,
                                             uint32_t length,
                                             const CHARSET_INFO * const cs,
                                             uint32_t count, uint64_t *values,
                                             bool transform_msec)
{
  const char *end= str+length;
  uint32_t x;

  while (str != end && !my_isdigit(cs,*str))
    str++;

  for (x= 0 ; x < count ; x++)
  {
    int64_t value;
    const char *start= str;
    for (value= 0 ; str != end && my_isdigit(cs,*str) ; str++)
      value= value * 10L + (int64_t) (*str - '0');
    if (transform_msec && x == count - 1) // microseconds always last
    {
      long msec_length= 6 - (str - start);
      if (msec_length > 0)
        value*= (long) log_10_int[msec_length];
    }
    values[x]= value;
    while (str != end && !my_isdigit(cs,*str))
      str++;
    if (str == end && x != count-1)
    {
      x++;
      /* Change values[0...x-1] -> values[0...count-1] */
      internal::bmove_upp((unsigned char*) (values+count),
                          (unsigned char*) (values+x),
                          sizeof(*values)*x);
      memset(values, 0, sizeof(*values)*(count-x));
      break;
    }
  }
  return (str != end);
}

} /* namespace drizzled */