~drizzle-trunk/drizzle/development

1097.2.2 by clint at fewbar
style/documentation cleanups and replacing copyright notices
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
1097.2.1 by clint at fewbar
refactoring INTERVAL into C++ class
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
1097.2.2 by clint at fewbar
style/documentation cleanups and replacing copyright notices
27
bool drizzled::TemporalInterval::initFromItem(Item *args, interval_type int_type, String *str_value)
1097.2.1 by clint at fewbar
refactoring INTERVAL into C++ class
28
{
1097.2.7 by clint at fewbar
using constants for magic numbers and true/false instead of 1/0. renamed getIntervalInfo -> getIntervalFromString for clarity
29
  uint64_t array[MAX_STRING_ELEMENTS];
1097.2.1 by clint at fewbar
refactoring INTERVAL into C++ class
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
1097.2.5 by clint at fewbar
use C++ style cast for int_type
37
  if (static_cast<int>(int_type) <= INTERVAL_MICROSECOND)
1097.2.1 by clint at fewbar
refactoring INTERVAL into C++ class
38
  {
39
    value= args->val_int();
40
    if (args->null_value)
41
      return true;
42
    if (value < 0)
43
    {
1097.2.7 by clint at fewbar
using constants for magic numbers and true/false instead of 1/0. renamed getIntervalInfo -> getIntervalFromString for clarity
44
      neg= true;
1097.2.1 by clint at fewbar
refactoring INTERVAL into C++ class
45
      value= -value;
46
    }
47
  }
48
  else
49
  {
50
    // Otherwise we must convert to a string and extract the multiple parts
51
    String *res;
1097.2.4 by clint at fewbar
coding standards cleanup
52
    if (!(res= args->val_str(str_value)))
1097.2.1 by clint at fewbar
refactoring INTERVAL into C++ class
53
      return true;
54
55
    // record negative intervalls in interval->neg 
1097.2.4 by clint at fewbar
coding standards cleanup
56
    str= res->ptr();
57
    const char *end= str+res->length();
1097.2.1 by clint at fewbar
refactoring INTERVAL into C++ class
58
    // Skip the whitespace
59
    while (str != end && my_isspace(cs,*str))
60
      str++;
61
    if (str != end && *str == '-')
62
    {
1097.2.7 by clint at fewbar
using constants for magic numbers and true/false instead of 1/0. renamed getIntervalInfo -> getIntervalFromString for clarity
63
      neg= true;
1097.2.1 by clint at fewbar
refactoring INTERVAL into C++ class
64
      // skip the -
65
      str++;
66
    }
1097.2.8 by clint at fewbar
using static_cast and uint32_t per coding standards
67
    length= static_cast<size_t>(end-str);		// Set up pointers to new str
1097.2.1 by clint at fewbar
refactoring INTERVAL into C++ class
68
  }
69
1097.2.4 by clint at fewbar
coding standards cleanup
70
  switch (int_type)
71
  {
72
  case INTERVAL_YEAR:
1097.2.8 by clint at fewbar
using static_cast and uint32_t per coding standards
73
    year= static_cast<uint32_t>(value);
1097.2.4 by clint at fewbar
coding standards cleanup
74
    break;
75
  case INTERVAL_QUARTER:
1097.2.8 by clint at fewbar
using static_cast and uint32_t per coding standards
76
    month= static_cast<uint32_t>(value*3);
1097.2.4 by clint at fewbar
coding standards cleanup
77
    break;
78
  case INTERVAL_MONTH:
1097.2.8 by clint at fewbar
using static_cast and uint32_t per coding standards
79
    month= static_cast<uint32_t>(value);
1097.2.4 by clint at fewbar
coding standards cleanup
80
    break;
81
  case INTERVAL_WEEK:
1097.2.8 by clint at fewbar
using static_cast and uint32_t per coding standards
82
    day= static_cast<uint32_t>(value*7);
1097.2.4 by clint at fewbar
coding standards cleanup
83
    break;
84
  case INTERVAL_DAY:
1097.2.8 by clint at fewbar
using static_cast and uint32_t per coding standards
85
    day= static_cast<uint32_t>(value);
1097.2.4 by clint at fewbar
coding standards cleanup
86
    break;
87
  case INTERVAL_HOUR:
1097.2.8 by clint at fewbar
using static_cast and uint32_t per coding standards
88
    hour= static_cast<uint32_t>(value);
1097.2.4 by clint at fewbar
coding standards cleanup
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
1097.2.7 by clint at fewbar
using constants for magic numbers and true/false instead of 1/0. renamed getIntervalInfo -> getIntervalFromString for clarity
100
    if (getIntervalFromString(str,length,cs,NUM_YEAR_MONTH_STRING_ELEMENTS,array,false))
1097.2.4 by clint at fewbar
coding standards cleanup
101
      return true;
1097.2.8 by clint at fewbar
using static_cast and uint32_t per coding standards
102
    year=  static_cast<uint32_t>(array[0]);
103
    month= static_cast<uint32_t>(array[1]);
1097.2.4 by clint at fewbar
coding standards cleanup
104
    break;
105
  case INTERVAL_DAY_HOUR:
1097.2.7 by clint at fewbar
using constants for magic numbers and true/false instead of 1/0. renamed getIntervalInfo -> getIntervalFromString for clarity
106
    if (getIntervalFromString(str,length,cs,NUM_DAY_HOUR_STRING_ELEMENTS,array,false))
1097.2.4 by clint at fewbar
coding standards cleanup
107
      return true;
1097.2.8 by clint at fewbar
using static_cast and uint32_t per coding standards
108
    day=  static_cast<uint32_t>(array[0]);
109
    hour= static_cast<uint32_t>(array[1]);
1097.2.4 by clint at fewbar
coding standards cleanup
110
    break;
111
  case INTERVAL_DAY_MICROSECOND:
1097.2.7 by clint at fewbar
using constants for magic numbers and true/false instead of 1/0. renamed getIntervalInfo -> getIntervalFromString for clarity
112
    if (getIntervalFromString(str,length,cs,NUM_DAY_MICROSECOND_STRING_ELEMENTS,array,true))
1097.2.4 by clint at fewbar
coding standards cleanup
113
      return true;
1097.2.8 by clint at fewbar
using static_cast and uint32_t per coding standards
114
    day=    static_cast<uint32_t>(array[0]);
115
    hour=   static_cast<uint32_t>(array[1]);
1097.2.4 by clint at fewbar
coding standards cleanup
116
    minute= array[2];
117
    second= array[3];
118
    second_part= array[4];
119
    break;
120
  case INTERVAL_DAY_MINUTE:
1097.2.7 by clint at fewbar
using constants for magic numbers and true/false instead of 1/0. renamed getIntervalInfo -> getIntervalFromString for clarity
121
    if (getIntervalFromString(str,length,cs,NUM_DAY_MINUTE_STRING_ELEMENTS,array,false))
1097.2.4 by clint at fewbar
coding standards cleanup
122
      return true;
1097.2.8 by clint at fewbar
using static_cast and uint32_t per coding standards
123
    day=    static_cast<uint32_t>(array[0]);
124
    hour=   static_cast<uint32_t>(array[1]);
1097.2.4 by clint at fewbar
coding standards cleanup
125
    minute= array[2];
126
    break;
127
  case INTERVAL_DAY_SECOND:
1097.2.7 by clint at fewbar
using constants for magic numbers and true/false instead of 1/0. renamed getIntervalInfo -> getIntervalFromString for clarity
128
    if (getIntervalFromString(str,length,cs,NUM_DAY_SECOND_STRING_ELEMENTS,array,false))
1097.2.4 by clint at fewbar
coding standards cleanup
129
      return true;
1097.2.8 by clint at fewbar
using static_cast and uint32_t per coding standards
130
    day=    static_cast<uint32_t>(array[0]);
131
    hour=   static_cast<uint32_t>(array[1]);
1097.2.4 by clint at fewbar
coding standards cleanup
132
    minute= array[2];
133
    second= array[3];
134
    break;
135
  case INTERVAL_HOUR_MICROSECOND:
1097.2.7 by clint at fewbar
using constants for magic numbers and true/false instead of 1/0. renamed getIntervalInfo -> getIntervalFromString for clarity
136
    if (getIntervalFromString(str,length,cs,NUM_HOUR_MICROSECOND_STRING_ELEMENTS,array,true))
1097.2.4 by clint at fewbar
coding standards cleanup
137
      return true;
1097.2.8 by clint at fewbar
using static_cast and uint32_t per coding standards
138
    hour=   static_cast<uint32_t>(array[0]);
1097.2.4 by clint at fewbar
coding standards cleanup
139
    minute= array[1];
140
    second= array[2];
141
    second_part= array[3];
142
    break;
143
  case INTERVAL_HOUR_MINUTE:
1097.2.7 by clint at fewbar
using constants for magic numbers and true/false instead of 1/0. renamed getIntervalInfo -> getIntervalFromString for clarity
144
    if (getIntervalFromString(str,length,cs,NUM_HOUR_MINUTE_STRING_ELEMENTS,array,false))
1097.2.4 by clint at fewbar
coding standards cleanup
145
      return true;
1097.2.8 by clint at fewbar
using static_cast and uint32_t per coding standards
146
    hour=   static_cast<uint32_t>(array[0]);
1097.2.4 by clint at fewbar
coding standards cleanup
147
    minute= array[1];
148
    break;
149
  case INTERVAL_HOUR_SECOND:
1097.2.7 by clint at fewbar
using constants for magic numbers and true/false instead of 1/0. renamed getIntervalInfo -> getIntervalFromString for clarity
150
    if (getIntervalFromString(str,length,cs,NUM_HOUR_SECOND_STRING_ELEMENTS,array,false))
1097.2.4 by clint at fewbar
coding standards cleanup
151
      return true;
1097.2.8 by clint at fewbar
using static_cast and uint32_t per coding standards
152
    hour=   static_cast<uint32_t>(array[0]);
1097.2.4 by clint at fewbar
coding standards cleanup
153
    minute= array[1];
154
    second= array[2];
155
    break;
156
  case INTERVAL_MINUTE_MICROSECOND:
1097.2.7 by clint at fewbar
using constants for magic numbers and true/false instead of 1/0. renamed getIntervalInfo -> getIntervalFromString for clarity
157
    if (getIntervalFromString(str,length,cs,NUM_MINUTE_MICROSECOND_STRING_ELEMENTS,array,true))
1097.2.4 by clint at fewbar
coding standards cleanup
158
      return true;
159
    minute= array[0];
160
    second= array[1];
161
    second_part= array[2];
162
    break;
163
  case INTERVAL_MINUTE_SECOND:
1097.2.7 by clint at fewbar
using constants for magic numbers and true/false instead of 1/0. renamed getIntervalInfo -> getIntervalFromString for clarity
164
    if (getIntervalFromString(str,length,cs,NUM_MINUTE_SECOND_STRING_ELEMENTS,array,false))
1097.2.4 by clint at fewbar
coding standards cleanup
165
      return true;
166
    minute= array[0];
167
    second= array[1];
168
    break;
169
  case INTERVAL_SECOND_MICROSECOND:
1097.2.7 by clint at fewbar
using constants for magic numbers and true/false instead of 1/0. renamed getIntervalInfo -> getIntervalFromString for clarity
170
    if (getIntervalFromString(str,length,cs,NUM_SECOND_MICROSECOND_STRING_ELEMENTS,array,true))
1097.2.4 by clint at fewbar
coding standards cleanup
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
1097.2.1 by clint at fewbar
refactoring INTERVAL into C++ class
178
  }
179
  return false;
180
}
181
1097.2.2 by clint at fewbar
style/documentation cleanups and replacing copyright notices
182
bool drizzled::TemporalInterval::addDate(DRIZZLE_TIME *ltime, interval_type int_type)
1097.2.1 by clint at fewbar
refactoring INTERVAL into C++ class
183
{
184
  long period, sign;
185
186
  ltime->neg= 0;
187
188
  sign= (neg ? -1 : 1);
189
1097.2.4 by clint at fewbar
coding standards cleanup
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;
1097.2.1 by clint at fewbar
refactoring INTERVAL into C++ class
211
1097.2.4 by clint at fewbar
coding standards cleanup
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;
1097.2.1 by clint at fewbar
refactoring INTERVAL into C++ class
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
1097.2.7 by clint at fewbar
using constants for magic numbers and true/false instead of 1/0. renamed getIntervalInfo -> getIntervalFromString for clarity
289
bool drizzled::TemporalInterval::getIntervalFromString(const char *str,uint32_t length, const CHARSET_INFO * const cs,
1097.2.1 by clint at fewbar
refactoring INTERVAL into C++ class
290
    uint32_t count, uint64_t *values,
291
    bool transform_msec)
292
{
1097.2.4 by clint at fewbar
coding standards cleanup
293
  const char *end= str+length;
1097.2.2 by clint at fewbar
style/documentation cleanups and replacing copyright notices
294
  uint32_t x;
295
1097.2.1 by clint at fewbar
refactoring INTERVAL into C++ class
296
  while (str != end && !my_isdigit(cs,*str))
297
    str++;
298
1097.2.2 by clint at fewbar
style/documentation cleanups and replacing copyright notices
299
  for (x= 0 ; x < count ; x++)
1097.2.1 by clint at fewbar
refactoring INTERVAL into C++ class
300
  {
301
    int64_t value;
302
    const char *start= str;
1097.2.2 by clint at fewbar
style/documentation cleanups and replacing copyright notices
303
    for (value= 0 ; str != end && my_isdigit(cs,*str) ; str++)
1097.2.1 by clint at fewbar
refactoring INTERVAL into C++ class
304
      value= value * 10L + (int64_t) (*str - '0');
1097.2.2 by clint at fewbar
style/documentation cleanups and replacing copyright notices
305
    if (transform_msec && x == count - 1) // microseconds always last
1097.2.1 by clint at fewbar
refactoring INTERVAL into C++ class
306
    {
307
      long msec_length= 6 - (str - start);
308
      if (msec_length > 0)
309
        value*= (long) log_10_int[msec_length];
310
    }
1097.2.2 by clint at fewbar
style/documentation cleanups and replacing copyright notices
311
    values[x]= value;
1097.2.1 by clint at fewbar
refactoring INTERVAL into C++ class
312
    while (str != end && !my_isdigit(cs,*str))
313
      str++;
1097.2.2 by clint at fewbar
style/documentation cleanups and replacing copyright notices
314
    if (str == end && x != count-1)
1097.2.1 by clint at fewbar
refactoring INTERVAL into C++ class
315
    {
1097.2.2 by clint at fewbar
style/documentation cleanups and replacing copyright notices
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));
1097.2.1 by clint at fewbar
refactoring INTERVAL into C++ class
321
      break;
322
    }
323
  }
324
  return (str != end);
325
}