~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to drizzled/function/get_variable.cc

  • Committer: Brian Aker
  • Date: 2009-04-17 01:45:33 UTC
  • Revision ID: brian@gaz-20090417014533-exdrtriab9zecqs2
Refactor get_variable to session

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/get_variable.h>
23
 
#include <drizzled/session.h>
24
 
 
25
 
#define extra_size sizeof(double)
26
 
 
27
 
user_var_entry *get_variable(HASH *hash, LEX_STRING &name,
28
 
                             bool create_if_not_exists)
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;
39
 
    if (!(entry = (user_var_entry*) malloc(size)))
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
 
}