~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to drizzled/functions/update_hash.cc

  • Committer: Lee
  • Date: 2008-10-30 21:40:56 UTC
  • mto: (572.1.2 devel)
  • mto: This revision was merged to the branch mainline in revision 573.
  • Revision ID: lbieber@lbieber-desktop-20081030214056-hdbeobj2dmaezdmz
more changes to move functions from item_func.cc/h to the functions directory

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 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/update_hash.h>
 
23
 
 
24
/**
 
25
  Set value to user variable.
 
26
 
 
27
  @param entry          pointer to structure representing variable
 
28
  @param set_null       should we set NULL value ?
 
29
  @param ptr            pointer to buffer with new value
 
30
  @param length         length of new value
 
31
  @param type           type of new value
 
32
  @param cs             charset info for new value
 
33
  @param dv             derivation for new value
 
34
  @param unsigned_arg   indiates if a value of type INT_RESULT is unsigned
 
35
 
 
36
  @note Sets error and fatal error if allocation fails.
 
37
 
 
38
  @retval
 
39
    false   success
 
40
  @retval
 
41
    true    failure
 
42
*/
 
43
 
 
44
#define extra_size sizeof(double)
 
45
 
 
46
bool
 
47
update_hash(user_var_entry *entry, bool set_null, void *ptr, uint32_t length,
 
48
            Item_result type, const CHARSET_INFO * const cs, Derivation dv,
 
49
            bool unsigned_arg)
 
50
{
 
51
  if (set_null)
 
52
  {
 
53
    char *pos= (char*) entry+ ALIGN_SIZE(sizeof(user_var_entry));
 
54
    if (entry->value && entry->value != pos)
 
55
      free(entry->value);
 
56
    entry->value= 0;
 
57
    entry->length= 0;
 
58
  }
 
59
  else
 
60
  {
 
61
    if (type == STRING_RESULT)
 
62
      length++;                                 // Store strings with end \0
 
63
    if (length <= extra_size)
 
64
    {
 
65
      /* Save value in value struct */
 
66
      char *pos= (char*) entry+ ALIGN_SIZE(sizeof(user_var_entry));
 
67
      if (entry->value != pos)
 
68
      {
 
69
        if (entry->value)
 
70
          free(entry->value);
 
71
        entry->value=pos;
 
72
      }
 
73
    }
 
74
    else
 
75
    {
 
76
      /* Allocate variable */
 
77
      if (entry->length != length)
 
78
      {
 
79
        char *pos= (char*) entry+ ALIGN_SIZE(sizeof(user_var_entry));
 
80
        if (entry->value == pos)
 
81
          entry->value=0;
 
82
        entry->value= (char*) my_realloc(entry->value, length,
 
83
                                         MYF(MY_ALLOW_ZERO_PTR | MY_WME |
 
84
                                             ME_FATALERROR));
 
85
        if (!entry->value)
 
86
          return 1;
 
87
      }
 
88
    }
 
89
    if (type == STRING_RESULT)
 
90
    {
 
91
      length--;                                 // Fix length change above
 
92
      entry->value[length]= 0;                  // Store end \0
 
93
    }
 
94
    memcpy(entry->value,ptr,length);
 
95
    if (type == DECIMAL_RESULT)
 
96
      ((my_decimal*)entry->value)->fix_buffer_pointer();
 
97
    entry->length= length;
 
98
    entry->collation.set(cs, dv);
 
99
    entry->unsigned_flag= unsigned_arg;
 
100
  }
 
101
  entry->type=type;
 
102
  return 0;
 
103
}