Skip to content

Commit 898aae5

Browse files
Add SLEEP(seconds) function, which always returns 0 after the given
number of seconds (which can include microseconds). (Bug #6760)
1 parent 7d1c4bc commit 898aae5

7 files changed

Lines changed: 49 additions & 0 deletions

File tree

mysql-test/r/func_misc.result

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,3 +59,14 @@ t1 CREATE TABLE `t1` (
5959
`length(uuid())` int(10) NOT NULL default '0'
6060
) ENGINE=MyISAM DEFAULT CHARSET=latin1
6161
drop table t1;
62+
create table t1 (a timestamp default '2005-05-05 01:01:01',
63+
b timestamp default '2005-05-05 01:01:01');
64+
insert into t1 set a = now();
65+
select sleep(3);
66+
sleep(3)
67+
0
68+
update t1 set b = now();
69+
select timediff(b, a) >= '00:00:03' from t1;
70+
timediff(b, a) >= '00:00:03'
71+
1
72+
drop table t1;

mysql-test/t/func_misc.test

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,3 +46,12 @@ drop table t1;
4646
create table t1 as select uuid(), length(uuid());
4747
show create table t1;
4848
drop table t1;
49+
50+
# Bug #6760: Add SLEEP() function
51+
create table t1 (a timestamp default '2005-05-05 01:01:01',
52+
b timestamp default '2005-05-05 01:01:01');
53+
insert into t1 set a = now();
54+
select sleep(3);
55+
update t1 set b = now();
56+
select timediff(b, a) >= '00:00:03' from t1;
57+
drop table t1;

sql/item_create.cc

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -354,6 +354,11 @@ Item *create_func_sha(Item* a)
354354
return new Item_func_sha(a);
355355
}
356356

357+
Item *create_func_sleep(Item* a)
358+
{
359+
return new Item_func_sleep(a);
360+
}
361+
357362
Item *create_func_space(Item *a)
358363
{
359364
CHARSET_INFO *cs= current_thd->variables.collation_connection;

sql/item_create.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,7 @@ Item *create_func_sec_to_time(Item* a);
8383
Item *create_func_sign(Item* a);
8484
Item *create_func_sin(Item* a);
8585
Item *create_func_sha(Item* a);
86+
Item *create_func_sleep(Item* a);
8687
Item *create_func_soundex(Item* a);
8788
Item *create_func_space(Item *);
8889
Item *create_func_sqrt(Item* a);

sql/item_func.cc

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3259,6 +3259,17 @@ void Item_func_benchmark::print(String *str)
32593259
str->append(')');
32603260
}
32613261

3262+
/* This function is just used to create tests with time gaps */
3263+
3264+
longlong Item_func_sleep::val_int()
3265+
{
3266+
DBUG_ASSERT(fixed == 1);
3267+
double time= args[0]->val_real();
3268+
my_sleep((ulong)time*1000000L);
3269+
return 0;
3270+
}
3271+
3272+
32623273
#define extra_size sizeof(double)
32633274

32643275
static user_var_entry *get_variable(HASH *hash, LEX_STRING &name,

sql/item_func.h

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -874,6 +874,7 @@ class Item_func_last_insert_id :public Item_int_func
874874
}
875875
};
876876

877+
877878
class Item_func_benchmark :public Item_int_func
878879
{
879880
ulong loop_count;
@@ -888,6 +889,16 @@ class Item_func_benchmark :public Item_int_func
888889
};
889890

890891

892+
class Item_func_sleep :public Item_int_func
893+
{
894+
public:
895+
Item_func_sleep(Item *a) :Item_int_func(a) {}
896+
const char *func_name() const { return "sleep"; }
897+
longlong val_int();
898+
};
899+
900+
901+
891902
#ifdef HAVE_DLOPEN
892903

893904
class Item_udf_func :public Item_func

sql/lex.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -734,6 +734,7 @@ static SYMBOL sql_functions[] = {
734734
{ "SIN", F_SYM(FUNC_ARG1),0,CREATE_FUNC(create_func_sin)},
735735
{ "SHA", F_SYM(FUNC_ARG1),0,CREATE_FUNC(create_func_sha)},
736736
{ "SHA1", F_SYM(FUNC_ARG1),0,CREATE_FUNC(create_func_sha)},
737+
{ "SLEEP", F_SYM(FUNC_ARG1),0,CREATE_FUNC(create_func_sleep)},
737738
{ "SOUNDEX", F_SYM(FUNC_ARG1),0,CREATE_FUNC(create_func_soundex)},
738739
{ "SPACE", F_SYM(FUNC_ARG1),0,CREATE_FUNC(create_func_space)},
739740
{ "SQRT", F_SYM(FUNC_ARG1),0,CREATE_FUNC(create_func_sqrt)},

0 commit comments

Comments
 (0)