~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to drizzled/functions/str/export_set.cc

  • Committer: Brian Aker
  • Date: 2008-11-13 02:56:15 UTC
  • mfrom: (575.4.10 devel)
  • Revision ID: brian@tangent.org-20081113025615-snhsi52yb2ivmx6f
Merging Monty's code.

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
#include <drizzled/server_includes.h>
 
21
#include CSTDINT_H
 
22
#include <drizzled/functions/str/export_set.h>
 
23
 
 
24
String* Item_func_export_set::val_str(String* str)
 
25
{
 
26
  assert(fixed == 1);
 
27
  uint64_t the_set = (uint64_t) args[0]->val_int();
 
28
  String yes_buf, *yes;
 
29
  yes = args[1]->val_str(&yes_buf);
 
30
  String no_buf, *no;
 
31
  no = args[2]->val_str(&no_buf);
 
32
  String *sep = NULL, sep_buf ;
 
33
 
 
34
  uint32_t num_set_values = 64;
 
35
  uint64_t mask = 0x1;
 
36
  str->length(0);
 
37
  str->set_charset(collation.collation);
 
38
 
 
39
  /* Check if some argument is a NULL value */
 
40
  if (args[0]->null_value || args[1]->null_value || args[2]->null_value)
 
41
  {
 
42
    null_value=1;
 
43
    return 0;
 
44
  }
 
45
  /*
 
46
    Arg count can only be 3, 4 or 5 here. This is guaranteed from the
 
47
    grammar for EXPORT_SET()
 
48
  */
 
49
  switch(arg_count) {
 
50
  case 5:
 
51
    num_set_values = (uint) args[4]->val_int();
 
52
    if (num_set_values > 64)
 
53
      num_set_values=64;
 
54
    if (args[4]->null_value)
 
55
    {
 
56
      null_value=1;
 
57
      return 0;
 
58
    }
 
59
    /* Fall through */
 
60
  case 4:
 
61
    if (!(sep = args[3]->val_str(&sep_buf)))    // Only true if NULL
 
62
    {
 
63
      null_value=1;
 
64
      return 0;
 
65
    }
 
66
    break;
 
67
  case 3:
 
68
    {
 
69
      /* errors is not checked - assume "," can always be converted */
 
70
      uint32_t errors;
 
71
      sep_buf.copy(STRING_WITH_LEN(","), &my_charset_bin, collation.collation, &errors);
 
72
      sep = &sep_buf;
 
73
    }
 
74
    break;
 
75
  default:
 
76
    assert(0); // cannot happen
 
77
  }
 
78
  null_value=0;
 
79
 
 
80
  for (uint32_t i = 0; i < num_set_values; i++, mask = (mask << 1))
 
81
  {
 
82
    if (the_set & mask)
 
83
      str->append(*yes);
 
84
    else
 
85
      str->append(*no);
 
86
    if (i != num_set_values - 1)
 
87
      str->append(*sep);
 
88
  }
 
89
  return str;
 
90
}
 
91
 
 
92
void Item_func_export_set::fix_length_and_dec()
 
93
{
 
94
  uint32_t length=cmax(args[1]->max_length,args[2]->max_length);
 
95
  uint32_t sep_length=(arg_count > 3 ? args[3]->max_length : 1);
 
96
  max_length=length*64+sep_length*63;
 
97
 
 
98
  if (agg_arg_charsets(collation, args+1, cmin((uint)4,arg_count)-1,
 
99
                       MY_COLL_ALLOW_CONV, 1))
 
100
    return;
 
101
}
 
102