~drizzle-trunk/drizzle/development

574.3.14 by Lee
moving functions from item_timefunc to functions/time directory
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
813.1.15 by Jay Pipes
Fixed HOUR() function to use new Temporal system.
20
#include "drizzled/server_includes.h"
574.3.14 by Lee
moving functions from item_timefunc to functions/time directory
21
#include CSTDINT_H
813.1.15 by Jay Pipes
Fixed HOUR() function to use new Temporal system.
22
#include "drizzled/temporal.h"
23
#include "drizzled/error.h"
24
#include "drizzled/function/time/hour.h"
574.3.14 by Lee
moving functions from item_timefunc to functions/time directory
25
26
int64_t Item_func_hour::val_int()
27
{
813.1.15 by Jay Pipes
Fixed HOUR() function to use new Temporal system.
28
  assert(fixed);
29
30
  if (args[0]->is_null())
31
  {
32
    /* For NULL argument, we return a NULL result */
33
    null_value= true;
34
    return 0;
35
  }
36
37
  /* 
38
   * Because of the ridiculous way in which MySQL handles
39
   * TIME values (it does implicit integer -> string conversions
40
   * but only for DATETIME, not TIME values) we must first 
41
   * try a conversion into a TIME from a string.  If this
42
   * fails, we fall back on a DATETIME conversion.  This is
43
   * necessary because of the fact that DateTime::from_string()
44
   * looks first for DATETIME, then DATE regex matches.  6 consecutive
45
   * numbers, say 231130, will match the DATE regex YYMMDD
46
   * with no TIME part, but MySQL actually implicitly treats
47
   * parameters to SECOND(), HOUR(), and MINUTE() as TIME-only
48
   * values and matches 231130 as HHmmSS!
49
   *
50
   * Oh, and Brian Aker MADE me do this. :) --JRP
51
   */
52
  drizzled::Time temporal_time;
53
  
54
  char time_buff[DRIZZLE_MAX_LENGTH_DATETIME_AS_STRING];
55
  String tmp_time(time_buff,sizeof(time_buff), &my_charset_utf8_bin);
56
  String *time_res= args[0]->val_str(&tmp_time);
57
  if (! temporal_time.from_string(time_res->c_ptr(), time_res->length()))
58
  {
59
    /* 
60
     * OK, we failed to match the first argument as a string
61
     * representing a time value, so we grab the first argument 
62
     * as a DateTime object and try that for a match...
63
     */
64
    drizzled::DateTime temporal_datetime;
65
    Item_result arg0_result_type= args[0]->result_type();
66
    
67
    switch (arg0_result_type)
68
    {
813.1.19 by Jay Pipes
To remain in compatibility with MySQL, added ability to interpret
69
      case DECIMAL_RESULT: 
70
        /* 
71
         * For doubles supplied, interpret the arg as a string, 
72
         * so intentionally fall-through here...
73
         * This allows us to accept double parameters like 
74
         * 19971231235959.01 and interpret it the way MySQL does:
75
         * as a TIMESTAMP-like thing with a microsecond component.
76
         * Ugh, but need to keep backwards-compat.
77
         */
813.1.15 by Jay Pipes
Fixed HOUR() function to use new Temporal system.
78
      case STRING_RESULT:
79
        {
80
          char buff[DRIZZLE_MAX_LENGTH_DATETIME_AS_STRING];
81
          String tmp(buff,sizeof(buff), &my_charset_utf8_bin);
82
          String *res= args[0]->val_str(&tmp);
83
          if (! temporal_datetime.from_string(res->c_ptr(), res->length()))
84
          {
85
            /* 
86
            * Could not interpret the function argument as a temporal value, 
87
            * so throw an error and return 0
88
            */
89
            my_error(ER_INVALID_DATETIME_VALUE, MYF(0), res->c_ptr());
90
            return 0;
91
          }
92
        }
93
        break;
94
      case INT_RESULT:
95
        if (temporal_datetime.from_int64_t(args[0]->val_int()))
96
          break;
97
        /* Intentionally fall-through on invalid conversion from integer */
98
      default:
99
        {
100
          /* 
101
          * Could not interpret the function argument as a temporal value, 
102
          * so throw an error and return 0
103
          */
104
          null_value= true;
105
          char buff[DRIZZLE_MAX_LENGTH_DATETIME_AS_STRING];
106
          String tmp(buff,sizeof(buff), &my_charset_utf8_bin);
107
          String *res;
108
109
          res= args[0]->val_str(&tmp);
110
111
          my_error(ER_INVALID_DATETIME_VALUE, MYF(0), res->c_ptr());
112
          return 0;
113
        }
114
    }
115
    return (int64_t) temporal_datetime.hours();
116
  }
117
  return (int64_t) temporal_time.hours();
574.3.14 by Lee
moving functions from item_timefunc to functions/time directory
118
}