使用PHP的postgresql sproc(返回引用游标)的最佳方法?

时间:2023-02-01 22:49:35

I'm using stored procs and call them from PHP (I use the same procedures in a Java app too).

我正在使用存储过程并从PHP调用它们(我在Java应用程序中也使用相同的过程)。

Right now I do it this way (which works):

现在我这样做(有效):

if(!($result = pg_query($connection, 'BEGIN; SELECT '.$query.'; FETCH ALL IN '.self::$cursor.';'))) return NULL;

where $query is something like "CALL create_account('foo', 'bar', 'etc')" and $cursor is just "ref_cursor" since that is the cursor name (and yes, I know it seems like a hack...).

其中$ query类似于“CALL create_account('foo','bar','etc')”和$ cursor只是“ref_cursor”,因为那是光标名称(是的,我知道它看起来像是一个黑客...... )。

I know about the benefits of procedures (and prepared statements), and I wonder if there's any point in executing the above. A procedure is pre-compiled as far as I know, but the query above is not. So am I just fooling my self in believing that I would get some performance gain in this? I'm kind of bound to my procedures because I have an auto generator in Java that writes them for me. Is it better to use PDO in this case? I've searched on the net for something on pgsql ref cursors + pdo, but I didn't find much. I know I could just use PDO prepared statements but that would not coop with my procedures.

我知道程序(和准备好的陈述)的好处,我想知道执行上述内容是否有任何意义。据我所知,程序是预编译的,但上面的查询不是。所以我只是在欺骗自己,相信我会在这方面获得一些性能提升?我对我的程序很有帮助,因为我有一个Java自动生成器为我编写它们。在这种情况下使用PDO更好吗?我在网上搜索了pgsql ref游标+ pdo上的内容,但我找不到多少。我知道我可以使用PDO准备好的语句,但这不符合我的程序。

-Yngve

1 个解决方案

#1


The 'better way' would be using pg_query_params, but you send only 1 query/statement at a time:

“更好的方法”是使用pg_query_params,但一次只发送一个查询/语句:

 pg_query_params('SELECT procedure_name($1, $2);'.array('foo','bar'));

#1


The 'better way' would be using pg_query_params, but you send only 1 query/statement at a time:

“更好的方法”是使用pg_query_params,但一次只发送一个查询/语句:

 pg_query_params('SELECT procedure_name($1, $2);'.array('foo','bar'));