~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
/* - mode: c; c-basic-offset: 2; indent-tabs-mode: nil; -*-
 *  vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
 *
 *  Copyright (C) 2008 Sun Microsystems, Inc.
 *
 *  Authors:
 *
 *  Jay Pipes <jay.pipes@sun.com>
 *
 *  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; either version 2 of the License, or
 *  (at your option) any later version.
 *
 *  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
 */

/**
 * @file 
 *
 * Implementation of the server's date and time string matching utility.
 */

#include <config.h>

#include <drizzled/temporal_format.h>
#include <drizzled/temporal.h>

#include <string.h>
#include PCRE_HEADER

#include <string>
#include <vector>

using namespace std;

namespace drizzled
{

TemporalFormat::TemporalFormat(const char *pattern) :
  _pattern(pattern)
, _error_offset(0)
, _error(NULL)
, _year_part_index(0)
, _month_part_index(0)
, _day_part_index(0)
, _hour_part_index(0)
, _minute_part_index(0)
, _second_part_index(0)
, _usecond_part_index(0)
, _nsecond_part_index(0)
{
  /* Compile our regular expression */
  _re= pcre_compile(pattern
                    , 0 /* Default options */
                    , &_error
                    , &_error_offset
                    , NULL /* Use default character table */
                    );
}

bool TemporalFormat::matches(const char *data, size_t data_len, Temporal *to)
{
  if (! is_valid()) 
    return false;

  int32_t match_vector[OUT_VECTOR_SIZE]; /**< Stores match substring indexes */
  
  /* Make sure we've got no junk in the match_vector. */
  memset(match_vector, 0, sizeof(match_vector));

  /* Simply check the subject against the compiled regular expression */
  int32_t result= pcre_exec(_re
                            , NULL /* No extra data */
                            , data
                            , data_len
                            , 0 /* Start at offset 0 of subject...*/
                            , 0 /* Default options */
                            , match_vector
                            , OUT_VECTOR_SIZE
                            );
  if (result < 0)
  {
    switch (result)
    {
      case PCRE_ERROR_NOMATCH:
        return false; /* No match, just return false */
      default:
        return false;
    }
    return false;
  }

  int32_t expected_match_count= (_year_part_index > 1 ? 1 : 0)
                              + (_month_part_index > 1 ? 1 : 0)
                              + (_day_part_index > 1 ? 1 : 0)
                              + (_hour_part_index > 1 ? 1 : 0)
                              + (_minute_part_index > 1 ? 1 : 0)
                              + (_second_part_index > 1 ? 1 : 0)
                              + (_usecond_part_index > 1 ? 1 : 0)
                              + (_nsecond_part_index > 1 ? 1 : 0)
                              + 1; /* Add one for the entire match... */
  if (result != expected_match_count)
    return false;

  /* C++ string class easy to use substr() method is very useful here */
  string copy_data(data, data_len);
  /* 
   * OK, we have the expected substring matches, so grab
   * the various temporal parts from the subject string
   *
   * @note 
   *
   * TemporalFormatMatch is a friend class to Temporal, so
   * we can access the temporal instance's protected data.
   */
  if (_year_part_index > 1)
  {
    size_t year_start= match_vector[_year_part_index];
    size_t year_len= match_vector[_year_part_index + 1] - match_vector[_year_part_index];
    to->_years= atoi(copy_data.substr(year_start, year_len).c_str());
    if (year_len == 2)
      to->_years+= (to->_years >= DRIZZLE_YY_PART_YEAR ? 1900 : 2000);
  }
  if (_month_part_index > 1)
  {
    size_t month_start= match_vector[_month_part_index];
    size_t month_len= match_vector[_month_part_index + 1] - match_vector[_month_part_index];
    to->_months= atoi(copy_data.substr(month_start, month_len).c_str());
  }
  if (_day_part_index > 1)
  {
    size_t day_start= match_vector[_day_part_index];
    size_t day_len= match_vector[_day_part_index + 1] - match_vector[_day_part_index];
    to->_days= atoi(copy_data.substr(day_start, day_len).c_str());
  }
  if (_hour_part_index > 1)
  {
    size_t hour_start= match_vector[_hour_part_index];
    size_t hour_len= match_vector[_hour_part_index + 1] - match_vector[_hour_part_index];
    to->_hours= atoi(copy_data.substr(hour_start, hour_len).c_str());
  }
  if (_minute_part_index > 1)
  {
    size_t minute_start= match_vector[_minute_part_index];
    size_t minute_len= match_vector[_minute_part_index + 1] - match_vector[_minute_part_index];
    to->_minutes= atoi(copy_data.substr(minute_start, minute_len).c_str());
  }
  if (_second_part_index > 1)
  {
    size_t second_start= match_vector[_second_part_index];
    size_t second_len= match_vector[_second_part_index + 1] - match_vector[_second_part_index];
    to->_seconds= atoi(copy_data.substr(second_start, second_len).c_str());
  }
  if (_usecond_part_index > 1)
  {
    size_t usecond_start= match_vector[_usecond_part_index];
    size_t usecond_len= match_vector[_usecond_part_index + 1] - match_vector[_usecond_part_index];
    /* 
     * For microseconds, which are millionth of 1 second, 
     * we must ensure that we produce a correct result, 
     * even if < 6 places were specified.  For instance, if we get .1, 
     * we must produce 100000. .11 should produce 110000, etc.
     */
    uint32_t multiplier= 1;
    int32_t x= usecond_len;
    while (x < 6)
    {
      multiplier*= 10;
      ++x;
    }
    to->_useconds= atoi(copy_data.substr(usecond_start, usecond_len).c_str()) * multiplier;
  }
  if (_nsecond_part_index > 1)
  {
    size_t nsecond_start= match_vector[_nsecond_part_index];
    size_t nsecond_len= match_vector[_nsecond_part_index + 1] - match_vector[_nsecond_part_index];
    /* 
     * For nanoseconds, which are 1 billionth of a second, 
     * we must ensure that we produce a correct result, 
     * even if < 9 places were specified.  For instance, if we get .1, 
     * we must produce 100000000. .11 should produce 110000000, etc.
     */
    uint32_t multiplier= 1;
    int32_t x= nsecond_len;
    while (x < 9)
    {
      multiplier*= 10;
      ++x;
    }
    to->_nseconds= atoi(copy_data.substr(nsecond_start, nsecond_len).c_str()) * multiplier;
  }
  return true;
}


#define COUNT_KNOWN_FORMATS 19

struct temporal_format_args
{
  const char *pattern;
  int32_t year_part_index;
  int32_t month_part_index;
  int32_t day_part_index;
  int32_t hour_part_index;
  int32_t minute_part_index;
  int32_t second_part_index;
  int32_t usecond_part_index;
  int32_t nsecond_part_index;
};

/**
 * A collection of all known format strings.
 *
 * @note
 *
 * IMPORTANT: Make sure TIMESTAMP and DATETIME formats precede DATE formats and TIME formats, 
 * as the matching functionality matches on the first hit.
 *
 * @note 
 *
 * Remember to increment COUNT_KNOWN_FORMATS when you add a known format!
 */
static struct temporal_format_args __format_args[COUNT_KNOWN_FORMATS]= 
{
  {"^(\\d{4})(\\d{2})(\\d{2})(\\d{2})(\\d{2})(\\d{2})\\.(\\d{1,6})$", 1, 2, 3, 4, 5, 6, 7, 0} /* YYYYMMDDHHmmSS.uuuuuu */
, {"^(\\d{4})(\\d{2})(\\d{2})(\\d{2})(\\d{2})(\\d{2})$", 1, 2, 3, 4, 5, 6, 0, 0} /* YYYYMMDDHHmmSS */
, {"^(\\d{4})[-/.](\\d{1,2})[-/.](\\d{1,2})[T|\\s+](\\d{2}):(\\d{2}):(\\d{2})\\.(\\d{1,6})$", 1, 2, 3, 4, 5, 6, 7, 0} /* YYYY[/-.]MM[/-.]DD[T]HH:mm:SS.uuuuuu */
, {"^(\\d{4})[-/.](\\d{1,2})[-/.](\\d{1,2})[T|\\s+](\\d{2}):(\\d{2}):(\\d{2})$", 1, 2, 3, 4, 5, 6, 0, 0} /* YYYY[/-.][M]M[/-.][D]D[T]HH:mm:SS */
, {"^(\\d{2})[-/.](\\d{1,2})[-/.](\\d{1,2})[\\s+](\\d{2}):(\\d{2}):(\\d{2})$", 1, 2, 3, 4, 5, 6, 0, 0} /* YY[/-.][M]M[/-.][D]D HH:mm:SS */
, {"^(\\d{2})[-/.](\\d{1,2})[-/.](\\d{1,2})[\\s+](\\d{2}):(\\d{2})$", 1, 2, 3, 4, 5, 0, 0, 0} /* YY[/-.][M]M[/-.][D]D HH:mm */
, {"^(\\d{4})[-/.](\\d{1,2})[-/.](\\d{1,2})[\\s+](\\d{2}):(\\d{2})$", 1, 2, 3, 4, 5, 0, 0, 0} /* YYYY[/-.][M]M[/-.][D]D HH:mm */
, {"^(\\d{4})[-/.](\\d{1,2})[-/.](\\d{1,2})$", 1, 2, 3, 0, 0, 0, 0, 0} /* YYYY-[M]M-[D]D, YYYY.[M]M.[D]D, YYYY/[M]M/[D]D */ 
, {"^(\\d{4})(\\d{2})(\\d{2})$", 1, 2, 3, 0, 0, 0, 0, 0} /* YYYYMMDD */
, {"^(\\d{2})[-/.]*(\\d{2})[-/.]*(\\d{4})$", 3, 1, 2, 0, 0, 0, 0, 0} /* MM[-/.]DD[-/.]YYYY (US common format)*/
, {"^(\\d{2})[-/.]*(\\d{2})[-/.]*(\\d{2})$", 1, 2, 3, 0, 0, 0, 0, 0} /* YY[-/.]MM[-/.]DD */
, {"^(\\d{2})[-/.]*(\\d{1,2})[-/.]*(\\d{1,2})$", 1, 2, 3, 0, 0, 0, 0, 0} /* YY[-/.][M]M[-/.][D]D */
, {"^(\\d{4})[-/.]*(\\d{1,2})[-/.]*(\\d{1,2})$", 1, 2, 3, 0, 0, 0, 0, 0} /* YYYY[-/.][M]M[-/.][D]D */
, {"^(\\d{2}):*(\\d{2}):*(\\d{2})\\.(\\d{1,6})$", 0, 0, 0, 1, 2, 3, 4, 0} /* HHmmSS.uuuuuu, HH:mm:SS.uuuuuu */
, {"^(\\d{1,2}):*(\\d{2}):*(\\d{2})$", 0, 0, 0, 1, 2, 3, 0, 0} /* [H]HmmSS, [H]H:mm:SS */
, {"^(\\d{1,2}):(\\d{1,2}):(\\d{1,2})$", 0, 0, 0, 1, 2, 3, 0, 0} /* [H]H:[m]m:[S]S */
, {"^(\\d{1,2}):*(\\d{2})$", 0, 0, 0, 0, 1, 2, 0, 0} /* [m]mSS, [m]m:SS */
, {"^(\\d{1,2})$", 0, 0, 0, 0, 0, 1, 0, 0} /* SS, S */
, {"^(\\d{1,2})\\.(\\d{1,6})$", 0, 0, 0, 0, 0, 1, 2, 0} /* [S]S.uuuuuu */
};

vector<TemporalFormat *> known_datetime_formats;
vector<TemporalFormat *> known_date_formats;
vector<TemporalFormat *> known_time_formats;
vector<TemporalFormat *> all_temporal_formats;

/**
 * We allocate and initialize all known date/time formats.
 *
 * @TODO Cut down calls to new. Allocate as a block...
 */
bool init_temporal_formats()
{
  /* Compile all the regular expressions for the datetime formats */
  TemporalFormat *tmp;
  struct temporal_format_args current_format_args;
  
  for (int32_t x= 0; x < COUNT_KNOWN_FORMATS; ++x)
  {
    current_format_args= __format_args[x];
    tmp= new TemporalFormat(current_format_args.pattern);
    tmp->set_year_part_index(current_format_args.year_part_index);
    tmp->set_month_part_index(current_format_args.month_part_index);
    tmp->set_day_part_index(current_format_args.day_part_index);
    tmp->set_hour_part_index(current_format_args.hour_part_index);
    tmp->set_minute_part_index(current_format_args.minute_part_index);
    tmp->set_second_part_index(current_format_args.second_part_index);
    tmp->set_usecond_part_index(current_format_args.usecond_part_index);
    tmp->set_nsecond_part_index(current_format_args.nsecond_part_index);
    
    /* 
     * We store the pointer in all_temporal_formats because we 
     * delete pointers from that vector and only that vector
     */
    all_temporal_formats.push_back(tmp); 

    if (current_format_args.year_part_index > 0) /* A date must have a year */
    {
      known_datetime_formats.push_back(tmp);
      if (current_format_args.second_part_index == 0) /* A time must have seconds. */
        known_date_formats.push_back(tmp);
    }

    if (current_format_args.second_part_index > 0) /* A time must have seconds, but may not have minutes or hours */
      known_time_formats.push_back(tmp);
  }
  return true;
}

/** Free all allocated temporal formats */
void deinit_temporal_formats()
{
  vector<TemporalFormat *>::iterator p= all_temporal_formats.begin();
  while (p != all_temporal_formats.end())
  {
    delete *p;
    ++p;
  }
  known_date_formats.clear();
  known_datetime_formats.clear();
  known_time_formats.clear();
  all_temporal_formats.clear();
}

} /* end namespace drizzled */