~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to drizzled/function/str/conv_charset.h

Cleanup around SAFEMALLOC

Show diffs side-by-side

added added

removed removed

Lines of Context:
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
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
 
#ifndef DRIZZLED_FUNCTION_STR_CONV_CHARSET_H
21
 
#define DRIZZLED_FUNCTION_STR_CONV_CHARSET_H
22
 
 
23
 
#include <drizzled/function/str/strfunc.h>
24
 
 
25
 
namespace drizzled
26
 
{
27
 
 
28
 
class Item_func_conv_charset :public Item_str_func
29
 
{
30
 
  bool use_cached_value;
31
 
public:
32
 
  bool safe;
33
 
  const CHARSET_INFO *conv_charset; // keep it public
34
 
  Item_func_conv_charset(Item *a, const CHARSET_INFO * const cs) :Item_str_func(a)
35
 
  { conv_charset= cs; use_cached_value= 0; safe= 0; }
36
 
  Item_func_conv_charset(Item *a, const CHARSET_INFO * const cs, bool cache_if_const)
37
 
    :Item_str_func(a)
38
 
  {
39
 
    assert(args[0]->fixed);
40
 
    conv_charset= cs;
41
 
    if (cache_if_const && args[0]->const_item())
42
 
    {
43
 
      uint32_t errors= 0;
44
 
      String tmp, *str= args[0]->val_str(&tmp);
45
 
      if (!str || str_value.copy(str->ptr(), str->length(),
46
 
                                 str->charset(), conv_charset, &errors))
47
 
        null_value= 1;
48
 
      use_cached_value= 1;
49
 
      str_value.mark_as_const();
50
 
      safe= (errors == 0);
51
 
    }
52
 
    else
53
 
    {
54
 
      use_cached_value= 0;
55
 
      /*
56
 
        Conversion from and to "binary" is safe.
57
 
        Conversion to Unicode is safe.
58
 
        Other kind of conversions are potentially lossy.
59
 
      */
60
 
      safe= (args[0]->collation.collation == &my_charset_bin ||
61
 
             cs == &my_charset_bin ||
62
 
             (cs->state & MY_CS_UNICODE));
63
 
    }
64
 
  }
65
 
  String *val_str(String *);
66
 
  void fix_length_and_dec();
67
 
  const char *func_name() const { return "convert"; }
68
 
  virtual void print(String *str, enum_query_type query_type);
69
 
};
70
 
 
71
 
} /* namespace drizzled */
72
 
 
73
 
#endif /* DRIZZLED_FUNCTION_STR_CONV_CHARSET_H */