I am trying to work with UDFs, defined through SQL::Statement::Functions in Perl with a mySQL database in the backend. I am playing it by the book, as stated in http://metacpan.org/pod/SQL::Statement::Functions#User-Defined-Functions, however, before I even leave square one, an error in the CREATE FUNCTION statement is rearing its ugly head.
Here's the code first ($dbh is a given and works fine, it is just part of a bigger construct of the framework I am working with, so I don't want to spam my question with all of it; if I replace foo()
in the SELECT by 22, or by "_PK FROM locations", all is good and I get a 22 or a value corresponding to the query in Data::Dumper, so, the connection is ok):
use Data::Dumper;
use SQL::Statement::Functions;
sub foo {
return 999;
}
#$dbh is defined elsewhere
$dbh->do("CREATE FUNCTION foo") or die "error creating function foo(); " . $dbh->errstr;
my $sth = $dbh->prepare("SELECT foo(22)") or die "error preparing query: " . $dbh->errstr;
$sth->execute or die "error executing query: " . $sth->errstr;
my $row = $sth->fetchrow_arrayref();
print "ROW " . Dumper($row);
The error message reads as follows: error creating function foo(); You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '' at line 1
, refering to the $dbh->do("CREATE...")
line.
I have tried to dumb the CREATE FUNCTION part down as much as possible, but I also tried it with ...foo EXTERNAL
and ...foo EXTERNAL NAME foo
as suggested by the manual. Heck, I even created a package for the function, to no avail.
Any ideas what I am doing wrong here?
I checked, and the user I am connected to the database with does have all privileges, including alter_routine_priv and create_routine_priv, if that is what SQL::Statement is using.