~drizzle-trunk/drizzle/development

574.3.9 by Lee
moving functions from item_strfunc to functions/str directory
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
1241.9.36 by Monty Taylor
ZOMG. I deleted drizzled/server_includes.h.
20
#include "config.h"
1280.1.10 by Monty Taylor
Put everything in drizzled into drizzled namespace.
21
670.1.20 by Monty Taylor
Renamed functions to function... everything else is singular.
22
#include <drizzled/function/str/export_set.h>
574.3.9 by Lee
moving functions from item_strfunc to functions/str directory
23
1067.4.2 by Nathan Williams
All files in drizzled/function/ now use std::min instead of cmin.
24
#include <algorithm>
25
26
using namespace std;
27
1280.1.10 by Monty Taylor
Put everything in drizzled into drizzled namespace.
28
namespace drizzled
29
{
30
574.3.9 by Lee
moving functions from item_strfunc to functions/str directory
31
String* Item_func_export_set::val_str(String* str)
32
{
33
  assert(fixed == 1);
34
  uint64_t the_set = (uint64_t) args[0]->val_int();
35
  String yes_buf, *yes;
36
  yes = args[1]->val_str(&yes_buf);
37
  String no_buf, *no;
38
  no = args[2]->val_str(&no_buf);
39
  String *sep = NULL, sep_buf ;
40
41
  uint32_t num_set_values = 64;
42
  uint64_t mask = 0x1;
43
  str->length(0);
44
  str->set_charset(collation.collation);
45
46
  /* Check if some argument is a NULL value */
47
  if (args[0]->null_value || args[1]->null_value || args[2]->null_value)
48
  {
49
    null_value=1;
50
    return 0;
51
  }
52
  /*
53
    Arg count can only be 3, 4 or 5 here. This is guaranteed from the
54
    grammar for EXPORT_SET()
55
  */
56
  switch(arg_count) {
57
  case 5:
58
    num_set_values = (uint) args[4]->val_int();
59
    if (num_set_values > 64)
60
      num_set_values=64;
61
    if (args[4]->null_value)
62
    {
63
      null_value=1;
64
      return 0;
65
    }
66
    /* Fall through */
67
  case 4:
68
    if (!(sep = args[3]->val_str(&sep_buf)))	// Only true if NULL
69
    {
70
      null_value=1;
71
      return 0;
72
    }
73
    break;
74
  case 3:
75
    {
76
      /* errors is not checked - assume "," can always be converted */
1816.3.1 by Brian Aker
Convert sql_string to use size_t (this should clean up ICC warnings).
77
      size_t errors;
574.3.9 by Lee
moving functions from item_strfunc to functions/str directory
78
      sep_buf.copy(STRING_WITH_LEN(","), &my_charset_bin, collation.collation, &errors);
79
      sep = &sep_buf;
80
    }
81
    break;
82
  default:
83
    assert(0); // cannot happen
84
  }
85
  null_value=0;
86
87
  for (uint32_t i = 0; i < num_set_values; i++, mask = (mask << 1))
88
  {
89
    if (the_set & mask)
90
      str->append(*yes);
91
    else
92
      str->append(*no);
93
    if (i != num_set_values - 1)
94
      str->append(*sep);
95
  }
96
  return str;
97
}
98
99
void Item_func_export_set::fix_length_and_dec()
100
{
1067.4.6 by Nathan Williams
Converted all usages of cmax in drizzled/field/ and drizzled/function/ to std::max.
101
  uint32_t length= max(args[1]->max_length,args[2]->max_length);
1067.4.2 by Nathan Williams
All files in drizzled/function/ now use std::min instead of cmin.
102
  uint32_t sep_length= (arg_count > 3 ? args[3]->max_length : 1);
103
  max_length= length*64+sep_length*63;
574.3.9 by Lee
moving functions from item_strfunc to functions/str directory
104
1067.4.2 by Nathan Williams
All files in drizzled/function/ now use std::min instead of cmin.
105
  if (agg_arg_charsets(collation, args+1, min(4U,arg_count)-1,
574.3.9 by Lee
moving functions from item_strfunc to functions/str directory
106
                       MY_COLL_ALLOW_CONV, 1))
107
    return;
108
}
109
1280.1.10 by Monty Taylor
Put everything in drizzled into drizzled namespace.
110
} /* namespace drizzled */