简体   繁体   English

如何使用 libpqxx 中的 pqxx::stateless_cursor 类?

[英]How to use pqxx::stateless_cursor class from libpqxx?

I'm learning libpqxx, the C++ API to PostgreSQL.我正在学习 libpqxx,PostgreSQL 的 C++ API。 I'd like to use the pqxx::stateless_cursor class, but 1) I find the Doxygen output unhelpful in this case, and 2) the pqxx.org website has been down for some time now.我想使用 pqxx::stateless_cursor 类,但是 1) 我发现 Doxygen 输出在这种情况下没有帮助,2) pqxx.org 网站现在已经关闭了一段时间。

Anyone know how to use it?有人知道怎么用吗?

I believe this is how I construct one:我相信这就是我构建一个的方式:

pqxx::stateless_cursor <pqxx::cursor_base::read_only, pqxx::cursor_base::owned>
    cursor( work, "SELECT * FROM mytable", ?, ? );

The last two parms are called cname and hold , but are not documented.最后两个参数称为cnamehold ,但没有记录。

And once the cursor is created, how would I go about using it in a for() loop to get each row, one at a time?一旦创建了游标,我将如何在 for() 循环中使用它来获取每一行,一次一个?

Thanks @Eelke for the comments on cname and hold .感谢@Eelke 对cnamehold的评论。

I figured out how to make pqxx::stateless_cursor work.我想出了如何使 pqxx::stateless_cursor 工作。 I have no idea if there is a cleaner or more obvious way but here is an example:我不知道是否有更清洁或更明显的方法,但这里有一个例子:

pqxx::work work( conn );
pqxx::stateless_cursor<pqxx::cursor_base::read_only, pqxx::cursor_base::owned>
    cursor( work, "SELECT * FROM mytable", "mycursor", false );

for ( size_t idx = 0; true; idx ++ )
{
    pqxx::result result = cursor.retrieve( idx, idx + 1 );
    if ( result.empty() )
    {
        // nothing left to read
        break;
    }

    // Do something with "result" which contains a single
    // row in this example since we told the cursor to
    // retrieve row #idx (inclusive) to idx+1 (exclusive).
    std::cout << result[ 0 ][ "name" ].as<std::string>() << std::endl;
}

I do not know the pqxx library but based on the underlying DECLARE command of postgresql I would guess我不知道 pqxx 库,但基于 postgresql 的底层 DECLARE 命令,我猜

That cname is the name of the cursor, so it can be anything postgresql normally accepts as a cursor name. cname 是游标的名称,因此它可以是 postgresql 通常接受的任何游标名称。

That hold refers to the WITH HOLD option of a cursor, from the docs:该持有指的是游标的 WITH HOLD 选项,来自文档:

WITH HOLD specifies that the cursor can continue to be used after the transaction that created it successfully commits. WITH HOLD 指定游标在创建它的事务成功提交后可以继续使用。 WITHOUT HOLD specifies that the cursor cannot be used outside of the transaction that created it. WITHOUT HOLD 指定游标不能在创建它的事务之外使用。 If neither WITHOUT HOLD nor WITH HOLD is specified, WITHOUT HOLD is the default.如果没有指定 WITHOUT HOLD 和 WITH HOLD,则 WITHOUT HOLD 是默认值。

Here's another cursor example, using a do-while() loop:这是另一个使用 do-while() 循环的游标示例:

     const std::conStr("user=" + opt::dbUser + " password=" + opt::dbPasswd + " host=" + opt::dbHost + " dbname=" + opt::dbName);                                            

      pqxx::connection conn(connStr);
      pqxx::work txn(conn);
      std::string selectString = "SELECT id, name FROM table_name WHERE condition";

      pqxx::stateless_cursor<pqxx::cursor_base::read_only, pqxx::cursor_base::owned> 
      cursor(txn, selectString, "myCursor", false);

      //cursor variables
      size_t idx = 0;       //starting location
      size_t step = 10000;  //number of rows for each chunk
      pqxx::result result;
      do{
        //get next cursor chunk and update the index
        result = cursor.retrieve( idx, idx + step );
        idx += step;

        size_t records = result.size();
        cout << idx << ": records pulled = " << records << endl;

        for( pqxx::result::const_iterator row : result ){
          //iterate over cursor rows
        }
      }
      while( result.size() == step ); //if the result.size() != step, we're on our last loop
      cout << "Done!" << endl;

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

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