Commit 16cff7e7 authored by pem@mysql.com's avatar pem@mysql.com

A small step forward. Fixed a few bugs and made string type functions work,

but still strange interferences between multiple function invocations...
parent ca2e77ca
...@@ -377,9 +377,10 @@ end; ...@@ -377,9 +377,10 @@ end;
drop procedure create_select; drop procedure create_select;
create function e() returns double create function e() returns double
return 2.7182818284590452354; return 2.7182818284590452354;
select e(); set @e = e();
e() select e(), @e;
2.718281828459 e() @e
2.718281828459 2.718281828459
create function inc(i int) returns int create function inc(i int) returns int
return i+1; return i+1;
select inc(1), inc(99), inc(-71); select inc(1), inc(99), inc(-71);
...@@ -390,6 +391,11 @@ return x*y; ...@@ -390,6 +391,11 @@ return x*y;
select mul(1,1), mul(3,5), mul(4711, 666); select mul(1,1), mul(3,5), mul(4711, 666);
mul(1,1) mul(3,5) mul(4711, 666) mul(1,1) mul(3,5) mul(4711, 666)
1 15 3137526 1 15 3137526
create function append(s1 char(8), s2 char(8)) returns char(16)
return concat(s1, s2);
select append("foo", "bar");
append("foo", "bar")
foobar
create function fac(n int unsigned) returns bigint unsigned create function fac(n int unsigned) returns bigint unsigned
begin begin
declare f bigint unsigned; declare f bigint unsigned;
...@@ -400,11 +406,12 @@ set n = n - 1; ...@@ -400,11 +406,12 @@ set n = n - 1;
end while; end while;
return f; return f;
end; end;
select fac(1), fac(2), fac(10); select fac(1), fac(2), fac(5), fac(10);
fac(1) fac(2) fac(10) fac(1) fac(2) fac(5) fac(10)
1 2 3628800 1 2 120 3628800
drop function e; drop function e;
drop function inc; drop function inc;
drop function mul; drop function mul;
drop function append;
drop function fac; drop function fac;
drop table t1; drop table t1;
...@@ -445,7 +445,8 @@ drop procedure create_select| ...@@ -445,7 +445,8 @@ drop procedure create_select|
create function e() returns double create function e() returns double
return 2.7182818284590452354| return 2.7182818284590452354|
select e()| set @e = e()|
select e(), @e|
# A minimal function with one argument # A minimal function with one argument
create function inc(i int) returns int create function inc(i int) returns int
...@@ -459,6 +460,12 @@ create function mul(x int, y int) returns int ...@@ -459,6 +460,12 @@ create function mul(x int, y int) returns int
select mul(1,1), mul(3,5), mul(4711, 666)| select mul(1,1), mul(3,5), mul(4711, 666)|
# A minimal string function
create function append(s1 char(8), s2 char(8)) returns char(16)
return concat(s1, s2)|
select append("foo", "bar")|
# A function with flow control # A function with flow control
create function fac(n int unsigned) returns bigint unsigned create function fac(n int unsigned) returns bigint unsigned
begin begin
...@@ -472,11 +479,12 @@ begin ...@@ -472,11 +479,12 @@ begin
return f; return f;
end| end|
select fac(1), fac(2), fac(10)| select fac(1), fac(2), fac(5), fac(10)|
drop function e| drop function e|
drop function inc| drop function inc|
drop function mul| drop function mul|
drop function append|
drop function fac| drop function fac|
delimiter ;| delimiter ;|
......
...@@ -2781,44 +2781,42 @@ Item_func_sp::execute(Item **itp) ...@@ -2781,44 +2781,42 @@ Item_func_sp::execute(Item **itp)
DBUG_ENTER("Item_func_sp::execute"); DBUG_ENTER("Item_func_sp::execute");
THD *thd= current_thd; THD *thd= current_thd;
if (!m_sp && !(m_sp= sp_find_function(thd, &m_name))) if (! m_sp)
m_sp= sp_find_function(thd, &m_name);
if (! m_sp)
DBUG_RETURN(-1); DBUG_RETURN(-1);
DBUG_RETURN(m_sp->execute_function(thd, args, arg_count, itp)); DBUG_RETURN(m_sp->execute_function(thd, args, arg_count, itp));
} }
Item_result enum enum_field_types
Item_func_sp::result_type() const Item_func_sp::field_type() const
{ {
DBUG_ENTER("Item_func_sp::result_type"); DBUG_ENTER("Item_func_sp::field_type");
DBUG_PRINT("info", ("m_sp = %p", m_sp));
if (! m_sp)
m_sp= sp_find_function(current_thd, const_cast<LEX_STRING*>(&m_name));
if (m_sp) if (m_sp)
{ {
DBUG_RETURN(m_sp->result()); DBUG_PRINT("info", ("m_returns = %d", m_sp->m_returns));
} DBUG_RETURN(m_sp->m_returns);
else
{
sp_head *sp= sp_find_function(current_thd, (LEX_STRING *)(&m_name));
if (sp)
DBUG_RETURN(m_sp->result());
DBUG_RETURN(STRING_RESULT);
} }
DBUG_RETURN(MYSQL_TYPE_STRING);
} }
void Item_result
Item_func_sp::make_field(Send_field *field) Item_func_sp::result_type() const
{ {
DBUG_ENTER("Item_func_sp::make_field"); DBUG_ENTER("Item_func_sp::result_type");
Item *it; DBUG_PRINT("info", ("m_sp = %p", m_sp));
if (!execute(&it)) if (! m_sp)
m_sp= sp_find_function(current_thd, const_cast<LEX_STRING*>(&m_name));
if (m_sp)
{ {
it->set_name(name, 0); DBUG_RETURN(m_sp->result());
init_make_field(field, field_type());
it->make_field(field);
} }
DBUG_VOID_RETURN; DBUG_RETURN(STRING_RESULT);
} }
void void
...@@ -2826,7 +2824,9 @@ Item_func_sp::fix_length_and_dec() ...@@ -2826,7 +2824,9 @@ Item_func_sp::fix_length_and_dec()
{ {
DBUG_ENTER("Item_func_sp::fix_length_and_dec"); DBUG_ENTER("Item_func_sp::fix_length_and_dec");
if (m_sp || (m_sp= sp_find_function(current_thd, &m_name))) if (! m_sp)
m_sp= sp_find_function(current_thd, &m_name);
if (m_sp)
{ {
switch (m_sp->result()) { switch (m_sp->result()) {
case STRING_RESULT: case STRING_RESULT:
......
...@@ -1161,7 +1161,7 @@ class Item_func_sp :public Item_func ...@@ -1161,7 +1161,7 @@ class Item_func_sp :public Item_func
{ {
private: private:
LEX_STRING m_name; LEX_STRING m_name;
sp_head *m_sp; mutable sp_head *m_sp;
int execute(Item **itp); int execute(Item **itp);
...@@ -1183,6 +1183,8 @@ public: ...@@ -1183,6 +1183,8 @@ public:
return m_name.str; return m_name.str;
} }
enum enum_field_types field_type() const;
Item_result result_type() const; Item_result result_type() const;
longlong val_int() longlong val_int()
...@@ -1208,8 +1210,6 @@ public: ...@@ -1208,8 +1210,6 @@ public:
return it->val_str(str); return it->val_str(str);
} }
void make_field(Send_field *field);
void fix_length_and_dec(); void fix_length_and_dec();
}; };
...@@ -70,11 +70,11 @@ eval_func_item(THD *thd, Item *it, enum enum_field_types type) ...@@ -70,11 +70,11 @@ eval_func_item(THD *thd, Item *it, enum enum_field_types type)
default: default:
{ {
char buffer[MAX_FIELD_WIDTH]; char buffer[MAX_FIELD_WIDTH];
String tmp(buffer, sizeof(buffer), default_charset_info); String tmp(buffer, sizeof(buffer), it->charset());
String *s= it->val_str(&tmp); String *s= it->val_str(&tmp);
it= new Item_string(s->c_ptr_quick(), s->length(), it= new Item_string(sql_strmake(s->c_ptr_quick(), s->length()),
default_charset_info); s->length(), it->charset());
break; break;
} }
} }
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment