~drizzle-trunk/drizzle/development

492.3.32 by Lee
more changes to move functions from item_func.cc/h to the functions directory
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
670.1.20 by Monty Taylor
Renamed functions to function... everything else is singular.
22
#include <drizzled/function/get_variable.h>
584.1.15 by Monty Taylor
The mega-patch from hell. Renamed sql_class to session (since that's what it is) and removed it and field and table from common_includes.
23
#include <drizzled/session.h>
492.3.32 by Lee
more changes to move functions from item_func.cc/h to the functions directory
24
25
#define extra_size sizeof(double)
26
27
user_var_entry *get_variable(HASH *hash, LEX_STRING &name,
584.1.15 by Monty Taylor
The mega-patch from hell. Renamed sql_class to session (since that's what it is) and removed it and field and table from common_includes.
28
                             bool create_if_not_exists)
492.3.32 by Lee
more changes to move functions from item_func.cc/h to the functions directory
29
{
30
  user_var_entry *entry;
31
32
  if (!(entry = (user_var_entry*) hash_search(hash, (unsigned char*) name.str,
33
					      name.length)) &&
34
      create_if_not_exists)
35
  {
36
    uint32_t size=ALIGN_SIZE(sizeof(user_var_entry))+name.length+1+extra_size;
37
    if (!hash_inited(hash))
38
      return 0;
641.3.8 by Monty Taylor
Removed my_malloc from drizzled.
39
    if (!(entry = (user_var_entry*) malloc(size)))
492.3.32 by Lee
more changes to move functions from item_func.cc/h to the functions directory
40
      return 0;
41
    entry->name.str=(char*) entry+ ALIGN_SIZE(sizeof(user_var_entry))+
42
      extra_size;
43
    entry->name.length=name.length;
44
    entry->value=0;
45
    entry->length=0;
46
    entry->update_query_id=0;
47
    entry->collation.set(NULL, DERIVATION_IMPLICIT, 0);
48
    entry->unsigned_flag= 0;
49
    /*
50
      If we are here, we were called from a SET or a query which sets a
51
      variable. Imagine it is this:
52
      INSERT INTO t SELECT @a:=10, @a:=@a+1.
53
      Then when we have a Item_func_get_user_var (because of the @a+1) so we
54
      think we have to write the value of @a to the binlog. But before that,
55
      we have a Item_func_set_user_var to create @a (@a:=10), in this we mark
56
      the variable as "already logged" (line below) so that it won't be logged
57
      by Item_func_get_user_var (because that's not necessary).
58
    */
59
    entry->used_query_id=current_session->query_id;
60
    entry->type=STRING_RESULT;
61
    memcpy(entry->name.str, name.str, name.length+1);
62
    if (my_hash_insert(hash,(unsigned char*) entry))
63
    {
64
      free((char*) entry);
65
      return 0;
66
    }
67
  }
68
  return entry;
69
}