~drizzle-trunk/drizzle/development

492.3.13 by Lee
code clean move Item_func_floor, Item_func_length, Item_func_min_max, Item_func_rand, Item_func_round to 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
642.1.30 by Lee
move math functions to drizzled/function/math directory
22
#include <drizzled/function/math/rand.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 <libdrizzle/password.h>
24
#include <drizzled/session.h>
492.3.13 by Lee
code clean move Item_func_floor, Item_func_length, Item_func_min_max, Item_func_rand, Item_func_round to functions directory
25
26
void Item_func_rand::seed_random(Item *arg)
27
{
28
  /*
29
    TODO: do not do reinit 'rand' for every execute of PS/SP if
30
    args[0] is a constant.
31
  */
32
  uint32_t tmp= (uint32_t) arg->val_int();
33
  randominit(rand, (uint32_t) (tmp*0x10001L+55555555L),
34
             (uint32_t) (tmp*0x10000001L));
35
}
36
520.1.22 by Brian Aker
Second pass of thd cleanup
37
bool Item_func_rand::fix_fields(Session *session,Item **ref)
492.3.13 by Lee
code clean move Item_func_floor, Item_func_length, Item_func_min_max, Item_func_rand, Item_func_round to functions directory
38
{
520.1.22 by Brian Aker
Second pass of thd cleanup
39
  if (Item_real_func::fix_fields(session, ref))
492.3.13 by Lee
code clean move Item_func_floor, Item_func_length, Item_func_min_max, Item_func_rand, Item_func_round to functions directory
40
    return true;
41
  used_tables_cache|= RAND_TABLE_BIT;
42
  if (arg_count)
43
  {                                     // Only use argument once in query
44
    /*
45
      No need to send a Rand log event if seed was given eg: RAND(seed),
46
      as it will be replicated in the query as such.
47
    */
48
    if (!rand && !(rand= (struct rand_struct*)
520.1.22 by Brian Aker
Second pass of thd cleanup
49
                   session->alloc(sizeof(*rand))))
492.3.13 by Lee
code clean move Item_func_floor, Item_func_length, Item_func_min_max, Item_func_rand, Item_func_round to functions directory
50
      return true;
51
52
    if (args[0]->const_item())
53
      seed_random (args[0]);
54
  }
55
  else
56
  {
520.1.22 by Brian Aker
Second pass of thd cleanup
57
    rand= &session->rand;
492.3.13 by Lee
code clean move Item_func_floor, Item_func_length, Item_func_min_max, Item_func_rand, Item_func_round to functions directory
58
  }
59
  return false;
60
}
61
62
void Item_func_rand::update_used_tables()
63
{
64
  Item_real_func::update_used_tables();
65
  used_tables_cache|= RAND_TABLE_BIT;
66
}
67
68
69
double Item_func_rand::val_real()
70
{
71
  assert(fixed == 1);
72
  if (arg_count && !args[0]->const_item())
73
    seed_random (args[0]);
74
  return my_rnd(rand);
75
}
76