~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to drizzled/functions/rand.cc

Moved sql_common.h and my_time.h to libdrizzle.

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/rand.h>
23
 
 
24
 
void Item_func_rand::seed_random(Item *arg)
25
 
{
26
 
  /*
27
 
    TODO: do not do reinit 'rand' for every execute of PS/SP if
28
 
    args[0] is a constant.
29
 
  */
30
 
  uint32_t tmp= (uint32_t) arg->val_int();
31
 
  randominit(rand, (uint32_t) (tmp*0x10001L+55555555L),
32
 
             (uint32_t) (tmp*0x10000001L));
33
 
}
34
 
 
35
 
bool Item_func_rand::fix_fields(Session *session,Item **ref)
36
 
{
37
 
  if (Item_real_func::fix_fields(session, ref))
38
 
    return true;
39
 
  used_tables_cache|= RAND_TABLE_BIT;
40
 
  if (arg_count)
41
 
  {                                     // Only use argument once in query
42
 
    /*
43
 
      No need to send a Rand log event if seed was given eg: RAND(seed),
44
 
      as it will be replicated in the query as such.
45
 
    */
46
 
    if (!rand && !(rand= (struct rand_struct*)
47
 
                   session->alloc(sizeof(*rand))))
48
 
      return true;
49
 
 
50
 
    if (args[0]->const_item())
51
 
      seed_random (args[0]);
52
 
  }
53
 
  else
54
 
  {
55
 
    /*
56
 
      Save the seed only the first time RAND() is used in the query
57
 
      Once events are forwarded rather than recreated,
58
 
      the following can be skipped if inside the slave thread
59
 
    */
60
 
    if (!session->rand_used)
61
 
    {
62
 
      session->rand_used= 1;
63
 
      session->rand_saved_seed1= session->rand.seed1;
64
 
      session->rand_saved_seed2= session->rand.seed2;
65
 
    }
66
 
    rand= &session->rand;
67
 
  }
68
 
  return false;
69
 
}
70
 
 
71
 
void Item_func_rand::update_used_tables()
72
 
{
73
 
  Item_real_func::update_used_tables();
74
 
  used_tables_cache|= RAND_TABLE_BIT;
75
 
}
76
 
 
77
 
 
78
 
double Item_func_rand::val_real()
79
 
{
80
 
  assert(fixed == 1);
81
 
  if (arg_count && !args[0]->const_item())
82
 
    seed_random (args[0]);
83
 
  return my_rnd(rand);
84
 
}
85