~drizzle-developers/drizzle/elliott-release

« back to all changes in this revision

Viewing changes to drizzled/function/cast/signed.cc

  • Committer: patrick crews
  • Date: 2011-01-04 12:54:59 UTC
  • mfrom: (1845.2.208 drizzle)
  • Revision ID: gleebix@gmail.com-20110104125459-0pba1ry64d9imj13
Merged Trunk

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) 2010 Brian Aker
 
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; either version 2 of the License, or
 
9
 *  (at your option) any later version.
 
10
 *
 
11
 *  This program is distributed in the hope that it will be useful,
 
12
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 
13
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
14
 *  GNU General Public License for more details.
 
15
 *
 
16
 *  You should have received a copy of the GNU General Public License
 
17
 *  along with this program; if not, write to the Free Software
 
18
 *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 
19
 */
 
20
 
 
21
#include "config.h"
 
22
 
 
23
#include "drizzled/function/cast/signed.h"
 
24
#include "drizzled/error.h"
 
25
 
 
26
namespace drizzled {
 
27
namespace function {
 
28
namespace cast {
 
29
 
 
30
void Signed::print(String *str, enum_query_type query_type)
 
31
{
 
32
  str->append(STRING_WITH_LEN("cast("));
 
33
  args[0]->print(str, query_type);
 
34
  str->append(STRING_WITH_LEN(" as signed)"));
 
35
 
 
36
}
 
37
 
 
38
 
 
39
int64_t Signed::val_int()
 
40
{
 
41
  int64_t value;
 
42
  int error= 0;
 
43
  char buff[MAX_FIELD_WIDTH];
 
44
  String res_buffer(buff,sizeof(buff), &my_charset_bin);
 
45
  String *res;
 
46
 
 
47
  if (not (res= args[0]->val_str(&res_buffer)))
 
48
  { 
 
49
    null_value= 1; 
 
50
    error= 0; 
 
51
 
 
52
    return 0; 
 
53
  } 
 
54
  null_value= 0; 
 
55
 
 
56
  char *end= res->ptr() + res->length(); 
 
57
  value= res->charset()->cset->strntoull10rnd(res->charset(),
 
58
                                              res->ptr(),
 
59
                                              res->length(),
 
60
                                              false, &end, &error);
 
61
 
 
62
  if (error or (end != res->ptr() + res->length())) 
 
63
  {
 
64
    my_error(ER_INVALID_CAST_TO_SIGNED, MYF(0), res->c_str());
 
65
  }
 
66
 
 
67
  return value;
 
68
}
 
69
 
 
70
 
 
71
} // namespace cast
 
72
} // namespace function
 
73
} // namespace drizzled