drizzle
C++ MySQL-protocol-compatible database server
Loading...
Searching...
No Matches
typecast.h
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#pragma once
21
22#include <drizzled/charset.h>
23#include <drizzled/field.h>
24#include <drizzled/function/str/strfunc.h>
25#include <drizzled/temporal.h>
26
27namespace drizzled {
28
29class Item_typecast : public Item_str_func
30{
31public:
32 using Item_func::tmp_table_field;
33
34 Item_typecast(Item *a) :Item_str_func(a) {}
36 {
37 assert(fixed == 1);
38 String *tmp=args[0]->val_str(a);
39 null_value=args[0]->null_value;
40 if (tmp)
41 tmp->set_charset(collation.collation);
42 return tmp;
43 }
44 void fix_length_and_dec()
45 {
46 collation.set(&my_charset_bin);
47 max_length=args[0]->max_length;
48 }
49 virtual const char* cast_type() const= 0;
50 virtual void print(String *str);
51};
52
53class Item_typecast_maybe_null :public Item_typecast
54{
55public:
56 Item_typecast_maybe_null(Item *a) :Item_typecast(a) {}
57 void fix_length_and_dec()
58 {
59 collation.set(&my_charset_bin);
60 max_length=args[0]->max_length;
61 maybe_null= 1;
62 }
63};
64
65class Item_char_typecast :public Item_typecast
66{
67 int cast_length;
68 const charset_info_st *cast_cs, *from_cs;
69 bool charset_conversion;
70 String tmp_value;
71public:
72 using Item_func::tmp_table_field;
73
74 Item_char_typecast(Item *a, int length_arg, const charset_info_st * const cs_arg)
75 :Item_typecast(a), cast_length(length_arg), cast_cs(cs_arg) {}
76 enum Functype functype() const { return CHAR_TYPECAST_FUNC; }
77 bool eq(const Item *item, bool binary_cmp) const;
78 const char *func_name() const { return "cast_as_char"; }
79 const char* cast_type() const { return "char"; }
81 void fix_length_and_dec();
82 virtual void print(String *str);
83};
84
85class Item_date_typecast :public Item_typecast_maybe_null
86{
87public:
88 using Item_func::tmp_table_field;
89
90 Item_date_typecast(Item *a) :
91 Item_typecast_maybe_null(a)
92 {}
93
94 const char *func_name() const { return "cast_as_date"; }
95 String *val_str(String *str);
96 bool get_date(type::Time &ltime, uint32_t fuzzy_date);
97 bool get_time(type::Time &ltime);
98 const char *cast_type() const { return "date"; }
99 enum_field_types field_type() const { return DRIZZLE_TYPE_DATE; }
100
101 Field *tmp_table_field(Table *table)
102 {
103 return tmp_table_field_from_field_type(table, 0);
104 }
105
106 void fix_length_and_dec()
107 {
108 collation.set(&my_charset_bin);
109 max_length= 10;
110 maybe_null= 1;
111 }
112
113 bool result_as_int64_t() { return true; }
114 int64_t val_int();
115 double val_real() { return (double) val_int(); }
116
118 {
119 assert(fixed == 1);
120 return val_decimal_from_date(decimal_value);
121 }
122
123 int save_in_field(Field *field, bool )
124 {
125 return save_date_in_field(field);
126 }
127};
128
129class Item_datetime_typecast :public Item_typecast_maybe_null
130{
131public:
132 using Item_func::tmp_table_field;
133
134 Item_datetime_typecast(Item *a) :Item_typecast_maybe_null(a) {}
135 const char *func_name() const { return "cast_as_datetime"; }
136 String *val_str(String *str);
137 const char *cast_type() const { return "datetime"; }
138 enum_field_types field_type() const { return DRIZZLE_TYPE_DATETIME; }
139
140 Field *tmp_table_field(Table *table)
141 {
142 return tmp_table_field_from_field_type(table, 0);
143 }
144
145 void fix_length_and_dec()
146 {
147 collation.set(&my_charset_bin);
148 maybe_null= 1;
149 max_length= DateTime::MAX_STRING_LENGTH * MY_CHARSET_BIN_MB_MAXLEN;
150 decimals= DATETIME_DEC;
151 }
152
154 {
155 return true;
156 }
157
158 int64_t val_int();
159 double val_real()
160 {
161 return val_real_from_decimal();
162 }
163
164 double val()
165 {
166 return (double) val_int();
167 }
168
170 {
171 assert(fixed == 1);
172 return val_decimal_from_date(decimal_value);
173 }
174 int save_in_field(Field *field, bool )
175 {
176 return save_date_in_field(field);
177 }
178};
179
180} /* namespace drizzled */
181
static const int MAX_STRING_LENGTH
Definition temporal.h:608
String * val_str(String *a)
Definition typecast.cc:84
virtual void print(String *str)
Definition typecast.cc:60
bool eq(const Item *item, bool binary_cmp) const
Definition typecast.cc:32
bool get_time(type::Time &ltime)
Definition typecast.cc:246
type::Decimal * val_decimal(type::Decimal *decimal_value)
Definition typecast.h:117
bool get_date(type::Time &ltime, uint32_t fuzzy_date)
Definition typecast.cc:235
String * val_str(String *str)
Definition typecast.cc:254
type::Decimal * val_decimal(type::Decimal *decimal_value)
Definition typecast.h:169
String * val_str(String *str)
Definition typecast.cc:194
virtual void print(String *str)
Definition typecast.cc:50
String * val_str(String *a)
Definition typecast.h:35
bool fixed
Definition item.h:122
Field * tmp_table_field_from_field_type(Table *table, bool fixed_length)
Definition item.cc:1124
bool null_value
Definition item.h:124
bool maybe_null
Definition item.h:123
TODO: Rename this file - func.h is stupid.