~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to drizzled/function/update_hash.cc

  • Committer: Brian Aker
  • Date: 2009-07-10 01:49:29 UTC
  • mto: (1090.1.1 staging)
  • mto: This revision was merged to the branch mainline in revision 1091.
  • Revision ID: brian@gaz-20090710014929-v8pmvf7woqkld9iv
Cleanup of user_var

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