~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to drizzled/function/time/second.cc

Fix merge issues with 1.0 CC fix.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
/* -*- mode: c++; c-basic-offset: 2; indent-tabs-mode: nil; -*-
2
2
 *  vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
3
3
 *
4
 
 *  Copyright (C) 2008 Sun Microsystems
 
4
 *  Copyright (C) 2008 Sun Microsystems, Inc.
5
5
 *
6
6
 *  This program is free software; you can redistribute it and/or modify
7
7
 *  it under the terms of the GNU General Public License as published by
17
17
 *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
18
18
 */
19
19
 
20
 
#include <drizzled/server_includes.h>
21
 
#include CSTDINT_H
 
20
#include <config.h>
 
21
#include <drizzled/temporal.h>
 
22
#include <drizzled/error.h>
22
23
#include <drizzled/function/time/second.h>
23
24
 
24
 
/**
25
 
  Returns the second in time_exp in the range of 0 - 59.
26
 
*/
 
25
namespace drizzled
 
26
{
 
27
 
27
28
int64_t Item_func_second::val_int()
28
29
{
29
 
  assert(fixed == 1);
30
 
  DRIZZLE_TIME ltime;
31
 
  (void) get_arg0_time(&ltime);
32
 
  return ltime.second;
 
30
  assert(fixed);
 
31
 
 
32
  if (args[0]->is_null())
 
33
  {
 
34
    /* For NULL argument, we return a NULL result */
 
35
    null_value= true;
 
36
    return 0;
 
37
  }
 
38
 
 
39
  /* 
 
40
   * Because of the ridiculous way in which MySQL handles
 
41
   * TIME values (it does implicit integer -> string conversions
 
42
   * but only for DATETIME, not TIME values) we must first 
 
43
   * try a conversion into a TIME from a string.  If this
 
44
   * fails, we fall back on a DATETIME conversion.  This is
 
45
   * necessary because of the fact that DateTime::from_string()
 
46
   * looks first for DATETIME, then DATE regex matches.  6 consecutive
 
47
   * numbers, say 231130, will match the DATE regex YYMMDD
 
48
   * with no TIME part, but MySQL actually implicitly treats
 
49
   * parameters to SECOND(), HOUR(), and MINUTE() as TIME-only
 
50
   * values and matches 231130 as HHmmSS!
 
51
   *
 
52
   * Oh, and Brian Aker MADE me do this. :) --JRP
 
53
   */
 
54
  Time temporal_time;
 
55
  
 
56
  char time_buff[DRIZZLE_MAX_LENGTH_DATETIME_AS_STRING];
 
57
  String tmp_time(time_buff,sizeof(time_buff), &my_charset_utf8_bin);
 
58
  String *time_res= args[0]->val_str(&tmp_time);
 
59
 
 
60
  if (time_res && (time_res != &tmp_time))
 
61
  {
 
62
    tmp_time.copy(*time_res);
 
63
  }
 
64
 
 
65
  if (! temporal_time.from_string(tmp_time.c_ptr(), tmp_time.length()))
 
66
  {
 
67
    /* 
 
68
     * OK, we failed to match the first argument as a string
 
69
     * representing a time value, so we grab the first argument 
 
70
     * as a DateTime object and try that for a match...
 
71
     */
 
72
    DateTime temporal_datetime;
 
73
    Item_result arg0_result_type= args[0]->result_type();
 
74
    
 
75
    switch (arg0_result_type)
 
76
    {
 
77
      case DECIMAL_RESULT: 
 
78
        /* 
 
79
         * For doubles supplied, interpret the arg as a string, 
 
80
         * so intentionally fall-through here...
 
81
         * This allows us to accept double parameters like 
 
82
         * 19971231235959.01 and interpret it the way MySQL does:
 
83
         * as a TIMESTAMP-like thing with a microsecond component.
 
84
         * Ugh, but need to keep backwards-compat.
 
85
         */
 
86
      case STRING_RESULT:
 
87
        {
 
88
          char buff[DRIZZLE_MAX_LENGTH_DATETIME_AS_STRING];
 
89
          String tmp(buff,sizeof(buff), &my_charset_utf8_bin);
 
90
          String *res= args[0]->val_str(&tmp);
 
91
 
 
92
          if (res && (res != &tmp))
 
93
          {
 
94
            tmp.copy(*res);
 
95
          }
 
96
 
 
97
          if (! temporal_datetime.from_string(tmp.c_ptr(), tmp.length()))
 
98
          {
 
99
            /* 
 
100
            * Could not interpret the function argument as a temporal value, 
 
101
            * so throw an error and return 0
 
102
            */
 
103
            my_error(ER_INVALID_DATETIME_VALUE, MYF(0), tmp.c_ptr());
 
104
            return 0;
 
105
          }
 
106
        }
 
107
        break;
 
108
      case INT_RESULT:
 
109
        if (temporal_datetime.from_int64_t(args[0]->val_int()))
 
110
          break;
 
111
        /* Intentionally fall-through on invalid conversion from integer */
 
112
      default:
 
113
        {
 
114
          /* 
 
115
          * Could not interpret the function argument as a temporal value, 
 
116
          * so throw an error and return 0
 
117
          */
 
118
          null_value= true;
 
119
          char buff[DRIZZLE_MAX_LENGTH_DATETIME_AS_STRING];
 
120
          String tmp(buff,sizeof(buff), &my_charset_utf8_bin);
 
121
          String *res;
 
122
 
 
123
          res= args[0]->val_str(&tmp);
 
124
 
 
125
          if (res && (res != &tmp))
 
126
          {
 
127
            tmp.copy(*res);
 
128
          }
 
129
 
 
130
          my_error(ER_INVALID_DATETIME_VALUE, MYF(0), tmp.c_ptr());
 
131
          return 0;
 
132
        }
 
133
    }
 
134
    return (int64_t) temporal_datetime.seconds();
 
135
  }
 
136
  return (int64_t) temporal_time.seconds();
33
137
}
34
138
 
 
139
} /* namespace drizzled */