简体   繁体   English

用于Perl的SQL查询生成器,支持存储过程

[英]SQL query generator for Perl with stored procedures support

Current code base I'm working on is full of ad-hoc conditional string concatenations producing less than clear SQL queries. 我正在处理的当前代码库充满了临时条件字符串连接,产生的查询不是很清晰。 I want to make them maintainable, but since using DBIx::Class is too complex to move to for now (giant legacy base), I'm looking to at least make them more robust by using some sort of SQL generator, which would only create the SQL by object-orientation or any other clean technique (no DB handling necessary). 我想让它们可维护,但由于使用DBIx :: Class过于复杂而无法继续使用(巨型遗留基础),我希望至少通过使用某种SQL生成器使它们更加健壮,这只会通过面向对象或任何其他干净技术创建SQL(无需DB处理)。

One general constraint on that generator is the ability to use stored procedures in a sane way, since my application is mostly based on those. 对该生成器的一个一般约束是能够以理智的方式使用存储过程,因为我的应用程序主要基于那些。 For example, I need to SELECT * FROM StoredProcedure(Parameter) WHERE ... . 例如,我需要SELECT * FROM StoredProcedure(Parameter) WHERE ... I've looked into Fey::SQL , SQL::Abstract and some others, but haven't seen any support apart from "inline SQL" for this kind of statement. 我已经研究过Fey :: SQLSQL :: Abstract和其他一些,但是对于这种语句,除了“内联SQL”之外还没有看到任何支持。 Neither have I seen any support for EXECUTE ... , not even in DBIx::Class , which I frankly can't really believe, probably I've been looking in wrong places. 我也没有看到对EXECUTE ...任何支持EXECUTE ... ,甚至在DBIx :: Class中也是如此 ,我坦白地说我不能相信,可能我一直在寻找错误的地方。

I actually liked Fey::SQL 's approach, until I found out it required some sort of scheme: 我实际上喜欢Fey :: SQL的方法,直到我发现它需要某种方案:

 $select->select( $user->columns( 'user_id', 'username' ) )
     ->from( $user, $group )
     ->where( $group->group_id, 'IN', 1, 2, 3 )
     ->and  ( $func, 'LIKE', 'smith%' );

What would you recommend? 你会推荐什么?

You can try SQL::Abstract . 您可以尝试SQL :: Abstract It's nice for simple SQL statements 它对于简单的SQL语句很好

I think the main problem is I do not understand what function(?) does. 我认为主要的问题是我不明白什么function(?) Is the ? ? there a placeholder to be passed to a stored procedure called function ? 有一个占位符要传递给一个名为function的存储过程? I also do not have access to a database where I can test my understanding of this. 我也无法访问数据库,我可以测试我对此的理解。 However, does the following really not work? 但是,以下情况真的不起作用吗?

my $sql = SQL::Abstract->new;

my ($st, @values) = $sql->select(
    \'function(?)',
    '*',
    { group_id => { 'IN' => [ 1 .. 3] } },
);

my $sth = $dbh->prepare($st);
$sth->execute('arg1', @values);

In my case, $st contains: 在我的例子中, $st包含:

SELECT * FROM function(?) WHERE ( group_id IN ( ?, ?, ? ) )

If that does not work, then how about: 如果这不起作用,那么如何:

my ($st, @values) = build_select_for_function(
    function => [ qw(arg1) ],
    '*',
    { group_id => { 'IN' => [ 1 .. 3] } },
);

print $st, "\n";


sub build_select_for_function {
    my ($func, $args, @sql_abstract_args) = @_;
    my $func_str = sprintf '%s(%s)', $func, join(',', @$args);

    my $sql = SQL::Abstract->new;
    $sql->select(
        \$func_str,
        @sql_abstract_args,
    );
}

Output: 输出:

SELECT * FROM function(arg1) WHERE ( group_id IN ( ?, ?, ? ) )

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

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