~drizzle-trunk/drizzle/development

492.3.4 by Lee
code clean move Item_func_signed and Item_func_unsigned to 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
549 by Monty Taylor
Took gettext.h out of header files.
22
#include <drizzled/error.h>
670.2.1 by Monty Taylor
Moved pthread keys
23
#include <drizzled/current_session.h>
670.1.20 by Monty Taylor
Renamed functions to function... everything else is singular.
24
#include <drizzled/function/signed.h>
492.3.4 by Lee
code clean move Item_func_signed and Item_func_unsigned to functions directory
25
26
void Item_func_signed::print(String *str, enum_query_type query_type)
27
{
28
  str->append(STRING_WITH_LEN("cast("));
29
  args[0]->print(str, query_type);
30
  str->append(STRING_WITH_LEN(" as signed)"));
31
32
}
33
34
int64_t Item_func_signed::val_int_from_str(int *error)
35
{
36
  char buff[MAX_FIELD_WIDTH], *end, *start;
37
  uint32_t length;
38
  String tmp(buff,sizeof(buff), &my_charset_bin), *res;
39
  int64_t value;
40
41
  /*
42
    For a string result, we must first get the string and then convert it
43
    to a int64_t
44
  */
45
46
  if (!(res= args[0]->val_str(&tmp)))
47
  {
48
    null_value= 1;
49
    *error= 0;
50
    return 0;
51
  }
52
  null_value= 0;
53
  start= (char *)res->ptr();
54
  length= res->length();
55
56
  end= start + length;
57
  value= my_strtoll10(start, &end, error);
58
  if (*error > 0 || end != start+ length)
59
  {
60
    char err_buff[128];
61
    String err_tmp(err_buff,(uint32_t) sizeof(err_buff), system_charset_info);
62
    err_tmp.copy(start, length, system_charset_info);
520.1.22 by Brian Aker
Second pass of thd cleanup
63
    push_warning_printf(current_session, DRIZZLE_ERROR::WARN_LEVEL_WARN,
492.3.4 by Lee
code clean move Item_func_signed and Item_func_unsigned to functions directory
64
                        ER_TRUNCATED_WRONG_VALUE,
65
                        ER(ER_TRUNCATED_WRONG_VALUE), "INTEGER",
66
                        err_tmp.c_ptr());
67
  }
68
  return value;
69
}
70
71
72
int64_t Item_func_signed::val_int()
73
{
74
  int64_t value;
75
  int error;
76
77
  if (args[0]->cast_to_int_type() != STRING_RESULT ||
78
      args[0]->result_as_int64_t())
79
  {
80
    value= args[0]->val_int();
660.1.3 by Eric Herman
removed trailing whitespace with simple script:
81
    null_value= args[0]->null_value;
492.3.4 by Lee
code clean move Item_func_signed and Item_func_unsigned to functions directory
82
    return value;
83
  }
84
85
  value= val_int_from_str(&error);
86
  if (value < 0 && error == 0)
87
  {
520.1.22 by Brian Aker
Second pass of thd cleanup
88
    push_warning(current_session, DRIZZLE_ERROR::WARN_LEVEL_WARN, ER_UNKNOWN_ERROR,
492.3.4 by Lee
code clean move Item_func_signed and Item_func_unsigned to functions directory
89
                 "Cast to signed converted positive out-of-range integer to "
90
                 "it's negative complement");
91
  }
92
  return value;
93
}