~drizzle-trunk/drizzle/development

« back to all changes in this revision

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

  • Committer: Jay Pipes
  • Date: 2009-01-28 18:31:33 UTC
  • mto: This revision was merged to the branch mainline in revision 817.
  • Revision ID: jpipes@serialcoder-20090128183133-3rlb2hpq5tdlgoyy
Fixes MONTH() function to use new Temporal system.  Errors
are now always reported on bad datetime values.

Adds new test case for MONTH() function.

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/month.h>
23
 
#include <drizzled/session.h>
 
22
#include "drizzled/temporal.h"
 
23
#include "drizzled/error.h"
 
24
#include "drizzled/session.h"
 
25
#include "drizzled/function/time/month.h"
24
26
 
25
27
int64_t Item_func_month::val_int()
26
28
{
27
 
  assert(fixed == 1);
28
 
  DRIZZLE_TIME ltime;
29
 
  (void) get_arg0_date(&ltime, TIME_FUZZY_DATE);
30
 
  return (int64_t) ltime.month;
 
29
  assert(fixed);
 
30
 
 
31
  if (args[0]->is_null())
 
32
  {
 
33
    /* For NULL argument, we return a NULL result */
 
34
    null_value= true;
 
35
    return 0;
 
36
  }
 
37
 
 
38
  /* Grab the first argument as a DateTime object */
 
39
  drizzled::DateTime temporal;
 
40
  Item_result arg0_result_type= args[0]->result_type();
 
41
  
 
42
  switch (arg0_result_type)
 
43
  {
 
44
    case STRING_RESULT:
 
45
      {
 
46
        char buff[40];
 
47
        String tmp(buff,sizeof(buff), &my_charset_utf8_bin);
 
48
        String *res= args[0]->val_str(&tmp);
 
49
        if (! temporal.from_string(res->c_ptr(), res->length()))
 
50
        {
 
51
          /* 
 
52
          * Could not interpret the function argument as a temporal value, 
 
53
          * so throw an error and return 0
 
54
          */
 
55
          my_error(ER_INVALID_DATETIME_VALUE, MYF(0), res->c_ptr());
 
56
          return 0;
 
57
        }
 
58
      }
 
59
      break;
 
60
    case INT_RESULT:
 
61
      if (! temporal.from_int64_t(args[0]->val_int()))
 
62
      {
 
63
        /* 
 
64
        * Could not interpret the function argument as a temporal value, 
 
65
        * so throw an error and return 0
 
66
        */
 
67
        null_value= true;
 
68
        char buff[40];
 
69
        String tmp(buff,sizeof(buff), &my_charset_utf8_bin);
 
70
        String *res;
 
71
 
 
72
        res= args[0]->val_str(&tmp);
 
73
 
 
74
        my_error(ER_INVALID_DATETIME_VALUE, MYF(0), res->c_ptr());
 
75
        return 0;
 
76
      }
 
77
      break;
 
78
    default:
 
79
      {
 
80
        /* 
 
81
        * Could not interpret the function argument as a temporal value, 
 
82
        * so throw an error and return 0
 
83
        */
 
84
        null_value= true;
 
85
        char buff[40];
 
86
        String tmp(buff,sizeof(buff), &my_charset_utf8_bin);
 
87
        String *res;
 
88
 
 
89
        res= args[0]->val_str(&tmp);
 
90
 
 
91
        my_error(ER_INVALID_DATETIME_VALUE, MYF(0), res->c_ptr());
 
92
        return 0;
 
93
      }
 
94
  }
 
95
  return (int64_t) temporal.months();
31
96
}
32
97
 
33
 
 
34
98
String* Item_func_monthname::val_str(String* str)
35
99
{
36
 
  assert(fixed == 1);
 
100
  assert(fixed);
 
101
 
 
102
  if (args[0]->is_null())
 
103
  {
 
104
    /* For NULL argument, we return a NULL result */
 
105
    null_value= true;
 
106
    return (String *) 0;
 
107
  }
37
108
  const char *month_name;
38
109
  uint32_t   month= (uint) val_int();
39
110
  Session *session= current_session;
48
119
  str->set(month_name, strlen(month_name), system_charset_info);
49
120
  return str;
50
121
}
51