~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
 *
1999.6.1 by kalebral at gmail
update Copyright strings to a more common format to help with creating the master debian copyright file
4
 *  Copyright (C) 2008 Sun Microsystems, Inc.
574.3.14 by Lee
moving functions from item_timefunc to functions/time directory
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
2173.2.1 by Monty Taylor
Fixes incorrect usage of include
20
#include <config.h>
1280.1.10 by Monty Taylor
Put everything in drizzled into drizzled namespace.
21
2173.2.1 by Monty Taylor
Fixes incorrect usage of include
22
#include <drizzled/temporal.h>
23
#include <drizzled/error.h>
24
#include <drizzled/function/time/weekday.h>
25
#include <drizzled/util/test.h>
574.3.14 by Lee
moving functions from item_timefunc to functions/time directory
26
1280.1.10 by Monty Taylor
Put everything in drizzled into drizzled namespace.
27
namespace drizzled
28
{
29
574.3.14 by Lee
moving functions from item_timefunc to functions/time directory
30
int64_t Item_func_weekday::val_int()
31
{
813.1.21 by Jay Pipes
Fixes the WEEKDAY() function use the new temporal system and enable proper error throwing on bad datetimes. Adds test case for the WEEKDAY() function.
32
  assert(fixed);
574.3.14 by Lee
moving functions from item_timefunc to functions/time directory
33
813.1.21 by Jay Pipes
Fixes the WEEKDAY() function use the new temporal system and enable proper error throwing on bad datetimes. Adds test case for the WEEKDAY() function.
34
  if (args[0]->is_null())
35
  {
36
    /* For NULL argument, we return a NULL result */
37
    null_value= true;
574.3.14 by Lee
moving functions from item_timefunc to functions/time directory
38
    return 0;
813.1.21 by Jay Pipes
Fixes the WEEKDAY() function use the new temporal system and enable proper error throwing on bad datetimes. Adds test case for the WEEKDAY() function.
39
  }
40
41
  /* Grab the first argument as a DateTime object */
1280.1.10 by Monty Taylor
Put everything in drizzled into drizzled namespace.
42
  DateTime temporal;
813.1.21 by Jay Pipes
Fixes the WEEKDAY() function use the new temporal system and enable proper error throwing on bad datetimes. Adds test case for the WEEKDAY() function.
43
  Item_result arg0_result_type= args[0]->result_type();
44
  
45
  switch (arg0_result_type)
46
  {
47
    case DECIMAL_RESULT: 
48
      /* 
49
       * For doubles supplied, interpret the arg as a string, 
50
       * so intentionally fall-through here...
51
       * This allows us to accept double parameters like 
52
       * 19971231235959.01 and interpret it the way MySQL does:
53
       * as a TIMESTAMP-like thing with a microsecond component.
54
       * Ugh, but need to keep backwards-compat.
55
       */
56
    case STRING_RESULT:
57
      {
58
        char buff[DRIZZLE_MAX_LENGTH_DATETIME_AS_STRING];
59
        String tmp(buff,sizeof(buff), &my_charset_utf8_bin);
60
        String *res= args[0]->val_str(&tmp);
1750.3.1 by David Shrewsbury
Use of String::c_ptr() in temporal functions can corrupt PBXT row cache, so make sure temporaries are used.
61
62
        if (res && (res != &tmp))
63
        {
64
          tmp.copy(*res);
65
        }
66
67
        if (! temporal.from_string(tmp.c_ptr(), tmp.length()))
813.1.21 by Jay Pipes
Fixes the WEEKDAY() function use the new temporal system and enable proper error throwing on bad datetimes. Adds test case for the WEEKDAY() function.
68
        {
69
          /* 
70
          * Could not interpret the function argument as a temporal value, 
71
          * so throw an error and return 0
72
          */
1750.3.1 by David Shrewsbury
Use of String::c_ptr() in temporal functions can corrupt PBXT row cache, so make sure temporaries are used.
73
          my_error(ER_INVALID_DATETIME_VALUE, MYF(0), tmp.c_ptr());
813.1.21 by Jay Pipes
Fixes the WEEKDAY() function use the new temporal system and enable proper error throwing on bad datetimes. Adds test case for the WEEKDAY() function.
74
          return 0;
75
        }
76
      }
77
      break;
78
    case INT_RESULT:
79
      if (temporal.from_int64_t(args[0]->val_int()))
80
        break;
81
      /* Intentionally fall-through on invalid conversion from integer */
82
    default:
83
      {
84
        /* 
85
        * Could not interpret the function argument as a temporal value, 
86
        * so throw an error and return 0
87
        */
88
        null_value= true;
89
        char buff[DRIZZLE_MAX_LENGTH_DATETIME_AS_STRING];
90
        String tmp(buff,sizeof(buff), &my_charset_utf8_bin);
91
        String *res;
92
93
        res= args[0]->val_str(&tmp);
94
1750.3.1 by David Shrewsbury
Use of String::c_ptr() in temporal functions can corrupt PBXT row cache, so make sure temporaries are used.
95
        if (res && (res != &tmp))
96
        {
97
          tmp.copy(*res);
98
        }
99
100
        my_error(ER_INVALID_DATETIME_VALUE, MYF(0), tmp.c_ptr());
813.1.21 by Jay Pipes
Fixes the WEEKDAY() function use the new temporal system and enable proper error throwing on bad datetimes. Adds test case for the WEEKDAY() function.
101
        return 0;
102
      }
103
  }
104
  /* @TODO This ODBC thing is a complete hack. Fix it. */
105
  bool sunday_first= odbc_type;
106
  int64_t julian_day= julian_day_number_from_gregorian_date(temporal.years(), temporal.months(), temporal.days());
107
  return (int64_t) day_of_week(julian_day, sunday_first) + test(odbc_type);
574.3.14 by Lee
moving functions from item_timefunc to functions/time directory
108
}
1280.1.10 by Monty Taylor
Put everything in drizzled into drizzled namespace.
109
110
} /* namespace drizzled */