简体   繁体   English

DBD :: CSV:用户定义函数存在问题

[英]DBD::CSV: Problem with userdefined functions

From the SQL::Statement::Functions documentation: 从SQL :: Statement :: Functions文档:

Creating User-Defined Functions 创建用户定义的函数
... ...
More complex functions can make use of a number of arguments always passed to functions automatically. 更复杂的函数可以利用总是自动传递给函数的许多参数。 Functions always receive these values in @_: 函数始终在@_中接收这些值:
sub FOO { my( $self, $sth, $rowhash, @params ); sub FOO {my($ self,$ sth,$ rowhash,@params); } }

#!/usr/bin/env perl
use 5.012;
use warnings; use strict;
use DBI;

my $dbh = DBI->connect( "DBI:CSV:", undef, undef, { RaiseError => 1, } );
my $table = 'wages';
my $array_ref = [   [ 'id', 'number' ],  
            [ 0, 6900 ], 
            [ 1, 3200 ], 
            [ 2, 1800 ],    ];

$dbh->do( "CREATE TEMP TABLE $table AS import( ? )", {}, $array_ref );

sub routine {
    my $self = shift;
    my $sth = shift;
    my $rowhash = shift; #
    return $_[0] / 30; 
};

$dbh->do( "CREATE FUNCTION routine" );
my $sth = $dbh->prepare( "SELECT id, routine( number ) AS result FROM  $table" );
$sth->execute();
$sth->dump_results();

When I try this I get an error-message: 当我尝试这样做时,我收到一条错误消息:

DBD::CSV::st execute failed: Use of uninitialized value $_[0] in division (/) at ./so.pl line 27. DBD :: CSV :: st执行失败:在./so.pl第27行的除法(/)中使用未初始化的值$ _ [0]。
[for Statement "SELECT id, routine( number ) AS result FROM "wages""] at ./so.pl line 34. [对于语句“ SELECT id,例程(number)AS来自“工资”的结果””]在./so.pl第34行。

When I comment out the third argument I works as expected ( because it looks as if the third argument is missing ): 当我注释掉第三个参数时,我可以按预期工作(因为看起来好像第三个参数丢失了):

#!/usr/bin/env perl
...  
sub routine {
    my $self = shift;
    my $sth = shift;
    #my $rowhash = shift;
    return $_[0] / 30; 
};
...

0, 230 0、230
1, 106.667 1,106.667
2, 60 2、60
3 rows 3排

Is this a bug? 这是错误吗?

Also from the SQL::Statement::Functions documentation: 同样从SQL :: Statement :: Functions文档中:

When using SQL::Statement/SQL::Parser directly to parse SQL, functions (either built-in or user-defined) may occur anywhere in a SQL statement that values, column names, table names, or predicates may occur. 直接使用SQL :: Statement / SQL :: Parser解析SQL时,函数(内置的或用户定义的)可能会出现在SQL语句中可能出现值,列名,表名或谓词的任何地方。 When using the modules through a DBD or in any other context in which the SQL is both parsed and executed, functions can occur in the same places except that they can not occur in the column selection clause of a SELECT statement that contains a FROM clause. 在通过DBD或在其中同时解析和执行SQL的任何其他上下文中使用模块时,函数可以在相同的地方发生,除了它们不能出现在包含FROM子句的SELECT语句的列选择子句中。

ie

SELECT id, routine( number ) AS result FROM  wages

will not work. 不管用。

Rewrite your function to return a table, something like this: 重写您的函数以返回表,如下所示:

sub routine {
    my($self,$sth,$rowhash,@params) = @_;
    return [ [qw(id result)],
             map { [ $_->[0], $_->[1] / 30 ] } @$array_ref ];
};

$dbh->do( "CREATE FUNCTION routine" );
my $sth = $dbh->prepare( "SELECT * FROM routine()" );
$sth->execute();
$sth->dump_results();

With the expected results: 预期结果:

$ perl dl.pl 
0, 230
1, 106.667
2, 60
3 rows

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM