~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to drizzled/field/str.cc

  • Committer: Monty Taylor
  • Date: 2008-10-13 09:29:43 UTC
  • mfrom: (509 drizzle)
  • mto: (509.1.4 codestyle)
  • mto: This revision was merged to the branch mainline in revision 511.
  • Revision ID: monty@inaugust.com-20081013092943-rwvx4a6d85b5l2dh
MergedĀ inĀ 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) 2008 MySQL
 
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
 
 
22
#include <drizzled/server_includes.h>
 
23
#include <drizzled/field/str.h>
 
24
 
 
25
Field_str::Field_str(unsigned char *ptr_arg,uint32_t len_arg, unsigned char *null_ptr_arg,
 
26
                     unsigned char null_bit_arg, utype unireg_check_arg,
 
27
                     const char *field_name_arg, const CHARSET_INFO * const charset_arg)
 
28
  :Field(ptr_arg, len_arg, null_ptr_arg, null_bit_arg,
 
29
         unireg_check_arg, field_name_arg)
 
30
{
 
31
  field_charset= charset_arg;
 
32
  if (charset_arg->state & MY_CS_BINSORT)
 
33
    flags|=BINARY_FLAG;
 
34
  field_derivation= DERIVATION_IMPLICIT;
 
35
}
 
36
 
 
37
/**
 
38
  Decimal representation of Field_str.
 
39
 
 
40
  @param d         value for storing
 
41
 
 
42
  @note
 
43
    Field_str is the base class for fields like Field_enum,
 
44
    Field_date and some similar. Some dates use fraction and also
 
45
    string value should be converted to floating point value according
 
46
    our rules, so we use double to store value of decimal in string.
 
47
 
 
48
  @todo
 
49
    use decimal2string?
 
50
 
 
51
  @retval
 
52
    0     OK
 
53
  @retval
 
54
    !=0  error
 
55
*/
 
56
 
 
57
int Field_str::store_decimal(const my_decimal *d)
 
58
{
 
59
  double val;
 
60
  /* TODO: use decimal2string? */
 
61
  int err= warn_if_overflow(my_decimal2double(E_DEC_FATAL_ERROR &
 
62
                                            ~E_DEC_OVERFLOW, d, &val));
 
63
  return err | store(val);
 
64
}
 
65
 
 
66
my_decimal *Field_str::val_decimal(my_decimal *decimal_value)
 
67
{
 
68
  int64_t nr= val_int();
 
69
  int2my_decimal(E_DEC_FATAL_ERROR, nr, 0, decimal_value);
 
70
  return decimal_value;
 
71
}
 
72
 
 
73
/**
 
74
  Store double value in Field_varstring.
 
75
 
 
76
  Pretty prints double number into field_length characters buffer.
 
77
 
 
78
  @param nr            number
 
79
*/
 
80
 
 
81
int Field_str::store(double nr)
 
82
{
 
83
  char buff[DOUBLE_TO_STRING_CONVERSION_BUFFER_SIZE];
 
84
  uint32_t local_char_length= field_length / charset()->mbmaxlen;
 
85
  size_t length;
 
86
  bool error;
 
87
 
 
88
  length= my_gcvt(nr, MY_GCVT_ARG_DOUBLE, local_char_length, buff, &error);
 
89
  if (error)
 
90
  {
 
91
    if (table->in_use->abort_on_warning)
 
92
      set_warning(DRIZZLE_ERROR::WARN_LEVEL_ERROR, ER_DATA_TOO_LONG, 1);
 
93
    else
 
94
      set_warning(DRIZZLE_ERROR::WARN_LEVEL_WARN, ER_WARN_DATA_TRUNCATED, 1);
 
95
  }
 
96
  return store(buff, length, charset());
 
97
}
 
98
 
 
99
/* If one of the fields is binary and the other one isn't return 1 else 0 */
 
100
 
 
101
bool Field_str::compare_str_field_flags(Create_field *new_field, uint32_t flag_arg)
 
102
{
 
103
  return (((new_field->flags & (BINCMP_FLAG | BINARY_FLAG)) &&
 
104
          !(flag_arg & (BINCMP_FLAG | BINARY_FLAG))) ||
 
105
         (!(new_field->flags & (BINCMP_FLAG | BINARY_FLAG)) &&
 
106
          (flag_arg & (BINCMP_FLAG | BINARY_FLAG))));
 
107
}
 
108
 
 
109
 
 
110
uint32_t Field_str::is_equal(Create_field *new_field)
 
111
{
 
112
  if (compare_str_field_flags(new_field, flags))
 
113
    return 0;
 
114
 
 
115
  return ((new_field->sql_type == real_type()) &&
 
116
          new_field->charset == field_charset &&
 
117
          new_field->length == max_display_length());
 
118
}