~drizzle-trunk/drizzle/development

492.3.31 by Lee
more changes to move functions from item_func.cc/h to the functions 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
20
#include <drizzled/server_includes.h>
21
#include CSTDINT_H
22
#include <drizzled/functions/field.h>
584.4.7 by Monty Taylor
Removed a big bank of includes from item.h.
23
#include <drizzled/item/cmpfunc.h>
492.3.31 by Lee
more changes to move functions from item_func.cc/h to the functions directory
24
25
// Conversion functions
26
27
int64_t Item_func_field::val_int()
28
{
29
  assert(fixed == 1);
30
31
  if (cmp_type == STRING_RESULT)
32
  {
33
    String *field;
34
    if (!(field= args[0]->val_str(&value)))
35
      return 0;
36
    for (uint32_t i=1 ; i < arg_count ; i++)
37
    {
38
      String *tmp_value=args[i]->val_str(&tmp);
39
      if (tmp_value && !sortcmp(field,tmp_value,cmp_collation.collation))
40
        return (int64_t) (i);
41
    }
42
  }
43
  else if (cmp_type == INT_RESULT)
44
  {
45
    int64_t val= args[0]->val_int();
46
    if (args[0]->null_value)
47
      return 0;
48
    for (uint32_t i=1; i < arg_count ; i++)
49
    {
50
      if (val == args[i]->val_int() && !args[i]->null_value)
51
        return (int64_t) (i);
52
    }
53
  }
54
  else if (cmp_type == DECIMAL_RESULT)
55
  {
56
    my_decimal dec_arg_buf, *dec_arg,
57
               dec_buf, *dec= args[0]->val_decimal(&dec_buf);
58
    if (args[0]->null_value)
59
      return 0;
60
    for (uint32_t i=1; i < arg_count; i++)
61
    {
62
      dec_arg= args[i]->val_decimal(&dec_arg_buf);
63
      if (!args[i]->null_value && !my_decimal_cmp(dec_arg, dec))
64
        return (int64_t) (i);
65
    }
66
  }
67
  else
68
  {
69
    double val= args[0]->val_real();
70
    if (args[0]->null_value)
71
      return 0;
72
    for (uint32_t i=1; i < arg_count ; i++)
73
    {
74
      if (val == args[i]->val_real() && !args[i]->null_value)
75
        return (int64_t) (i);
76
    }
77
  }
78
  return 0;
79
}
80
81
void Item_func_field::fix_length_and_dec()
82
{
83
  maybe_null=0; max_length=3;
84
  cmp_type= args[0]->result_type();
85
  for (uint32_t i=1; i < arg_count ; i++)
86
    cmp_type= item_cmp_type(cmp_type, args[i]->result_type());
87
  if (cmp_type == STRING_RESULT)
88
    agg_arg_charsets(cmp_collation, args, arg_count, MY_COLL_CMP_CONV, 1);
89
}