Drizzled Public API Documentation

from_unixtime.cc
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, Inc.
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 
20 #include <config.h>
21 #include <boost/lexical_cast.hpp>
22 #include <drizzled/function/time/from_unixtime.h>
23 #include <drizzled/current_session.h>
24 #include <drizzled/session.h>
25 #include <drizzled/temporal.h>
26 #include <drizzled/time_functions.h>
27 #include <drizzled/field.h>
28 
29 #include <sstream>
30 #include <string>
31 
32 namespace drizzled
33 {
34 
35 void Item_func_from_unixtime::fix_length_and_dec()
36 {
37  session= current_session;
38  collation.set(&my_charset_bin);
39  decimals= DATETIME_DEC;
40  max_length=type::Time::MAX_STRING_LENGTH*MY_CHARSET_BIN_MB_MAXLEN;
41  maybe_null= 1;
42 }
43 
45 {
46  type::Time time_tmp;
47 
48  assert(fixed == 1);
49 
50  if (get_date(time_tmp, 0))
51  return 0;
52 
53  str->alloc(type::Time::MAX_STRING_LENGTH);
54 
55  time_tmp.convert(*str);
56 
57  return str;
58 }
59 
61 {
62  type::Time time_tmp;
63 
64  assert(fixed == 1);
65 
66  if (get_date(time_tmp, 0))
67  return 0;
68 
69  int64_t ret;
70  time_tmp.convert(ret);
71 
72  return (int64_t) ret;
73 }
74 
76 {
77  uint64_t tmp= 0;
78  type::usec_t fractional_tmp= 0;
79 
80  switch (args[0]->result_type()) {
81  case REAL_RESULT:
82  case ROW_RESULT:
83  case DECIMAL_RESULT:
84  case STRING_RESULT:
85  {
86  double double_tmp= args[0]->val_real();
87 
88  tmp= (uint64_t)(double_tmp);
89  fractional_tmp= (type::usec_t)((uint64_t)((double_tmp - tmp) * type::Time::FRACTIONAL_DIGITS) % type::Time::FRACTIONAL_DIGITS);
90 
91  break;
92  }
93 
94  case INT_RESULT:
95  tmp= (uint64_t)(args[0]->val_int());
96  break;
97  }
98 
99  /*
100  "tmp > TIMESTAMP_MAX_VALUE" check also covers case of negative
101  from_unixtime() argument since tmp is unsigned.
102  */
103  if ((null_value= (args[0]->null_value || tmp > TIMESTAMP_MAX_VALUE)))
104  return 1;
105 
106  ltime.reset();
107  ltime.store(tmp, fractional_tmp);
108 
109  return 0;
110 }
111 
112 } /* namespace drizzled */
String * val_str(String *str)
bool fixed
Definition: item.h:120
bool get_date(type::Time &res, uint32_t fuzzy_date)
bool null_value
Definition: item.h:122
bool maybe_null
Definition: item.h:121
virtual double val_real()=0