~fallenpegasus/drizzle/returning

« back to all changes in this revision

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

  • Committer: Brian Aker
  • Date: 2009-01-29 20:05:14 UTC
  • mfrom: (813.1.14 new-temporal)
  • mto: This revision was merged to the branch mainline in revision 821.
  • Revision ID: brian@tangent.org-20090129200514-84zoh7uc7bln6x0b
Merge Jay

Show diffs side-by-side

added added

removed removed

Lines of Context:
17
17
 *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
18
18
 */
19
19
 
20
 
#include <drizzled/server_includes.h>
 
20
#include "drizzled/server_includes.h"
21
21
#include CSTDINT_H
22
 
#include <drizzled/function/time/second.h>
 
22
#include "drizzled/temporal.h"
 
23
#include "drizzled/error.h"
 
24
#include "drizzled/function/time/second.h"
23
25
 
24
 
/**
25
 
  Returns the second in time_exp in the range of 0 - 59.
26
 
*/
27
26
int64_t Item_func_second::val_int()
28
27
{
29
 
  assert(fixed == 1);
30
 
  DRIZZLE_TIME ltime;
31
 
  (void) get_arg0_time(&ltime);
32
 
  return ltime.second;
 
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
    {
 
69
      case STRING_RESULT:
 
70
        {
 
71
          char buff[DRIZZLE_MAX_LENGTH_DATETIME_AS_STRING];
 
72
          String tmp(buff,sizeof(buff), &my_charset_utf8_bin);
 
73
          String *res= args[0]->val_str(&tmp);
 
74
          if (! temporal_datetime.from_string(res->c_ptr(), res->length()))
 
75
          {
 
76
            /* 
 
77
            * Could not interpret the function argument as a temporal value, 
 
78
            * so throw an error and return 0
 
79
            */
 
80
            my_error(ER_INVALID_DATETIME_VALUE, MYF(0), res->c_ptr());
 
81
            return 0;
 
82
          }
 
83
        }
 
84
        break;
 
85
      case INT_RESULT:
 
86
        if (temporal_datetime.from_int64_t(args[0]->val_int()))
 
87
          break;
 
88
        /* Intentionally fall-through on invalid conversion from integer */
 
89
      default:
 
90
        {
 
91
          /* 
 
92
          * Could not interpret the function argument as a temporal value, 
 
93
          * so throw an error and return 0
 
94
          */
 
95
          null_value= true;
 
96
          char buff[DRIZZLE_MAX_LENGTH_DATETIME_AS_STRING];
 
97
          String tmp(buff,sizeof(buff), &my_charset_utf8_bin);
 
98
          String *res;
 
99
 
 
100
          res= args[0]->val_str(&tmp);
 
101
 
 
102
          my_error(ER_INVALID_DATETIME_VALUE, MYF(0), res->c_ptr());
 
103
          return 0;
 
104
        }
 
105
    }
 
106
    return (int64_t) temporal_datetime.seconds();
 
107
  }
 
108
  return (int64_t) temporal_time.seconds();
33
109
}
34
110