~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to drizzled/functions/update_hash.cc

  • Committer: Monty Taylor
  • Date: 2008-12-02 21:16:50 UTC
  • mto: This revision was merged to the branch mainline in revision 641.
  • Revision ID: mordred@solanthus.local-20081202211650-r0adnzw0v9hbke7b
Removed AM_CONDITIONAL from SEARCH_FOR_LIB. Aligned naming of AM_CONDITIONALS.

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
#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
        entry->value= (char*) my_realloc(entry->value, length,
 
84
                                         MYF(MY_ALLOW_ZERO_PTR | MY_WME |
 
85
                                             ME_FATALERROR));
 
86
        if (!entry->value)
 
87
          return 1;
 
88
      }
 
89
    }
 
90
    if (type == STRING_RESULT)
 
91
    {
 
92
      length--;                                 // Fix length change above
 
93
      entry->value[length]= 0;                  // Store end \0
 
94
    }
 
95
    memcpy(entry->value,ptr,length);
 
96
    if (type == DECIMAL_RESULT)
 
97
      ((my_decimal*)entry->value)->fix_buffer_pointer();
 
98
    entry->length= length;
 
99
    entry->collation.set(cs, dv);
 
100
    entry->unsigned_flag= unsigned_arg;
 
101
  }
 
102
  entry->type=type;
 
103
  return 0;
 
104
}