Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

const_iterator and const_row_iterator errors in v7.7.2 #551

Closed
CleanHit opened this issue Mar 24, 2022 · 6 comments
Closed

const_iterator and const_row_iterator errors in v7.7.2 #551

CleanHit opened this issue Mar 24, 2022 · 6 comments

Comments

@CleanHit
Copy link

CleanHit commented Mar 24, 2022

I'm on pqxx v7.7.0 right now and I haven't used for some time. Did anything change regarding iterators in v7.7.2? For example, this works in v7.7.0

std::vector<std::string> output{};
pqxx::result query_result; // this gets populated with a result.
for (pqxx::result::const_iterator iter = query_result.begin(); iter != query_result.end(); iter++)
{
    for (pqxx::const_row_iterator field = iter.begin(); field != iter.end(); field++)
    {
        output.push_back(field.as<std::string>());
    }
}

But fails in v7.7.2 with:

C2079: 'iter' uses undefined class 'pqxx::const_result_iterator'
C2027: use of undefined type 'pqxx::const_result_iterator'
C2677: binary '!=': no global operator found which takes type 'pqxx::result::const_iterator' (or there is no acceptable conversion)

I use:

  • Windows 11
  • Visual Studio 2022 community edition v17.1.2
  • CMake v3.22.3
@jtv
Copy link
Owner

jtv commented Mar 24, 2022

The main thing that's changed is how libpqxx includes its own headers internally. It could be that your code accidentally fell victim to that. Perhaps your code does an #include <pqxx/foo> which used to in turn do an #include of another header but no longer does.

Could you show us the libpqxx includes at the top of your relevant source file?

@jtv
Copy link
Owner

jtv commented Mar 24, 2022

And some completely unrelated notes on how to make the code "nicer":

  1. Try replacing the for loops with this syntax: for (pqxx::result::const_iterator iter : query_result). Easier on the eyes!
  2. Also, try replacing the pqxx::result::const_iterator iter with just auto. It'll figure out the type on its own.
  3. If you're querying a lot of rows here, you can speed it up by using pqxx::transaction_base::stream().
  4. Converting a field to std::string is pretty inefficient. If performance is a concern here, consider using pqxx::field::view() (when using exec() and pqxx::result) or converting to std::string_view (when using pqxx::transaction_base::stream()).

@CleanHit
Copy link
Author

CleanHit commented Mar 25, 2022

The main thing that's changed is how libpqxx includes its own headers internally. It could be that your code accidentally fell victim to that. Perhaps your code does an #include <pqxx/foo> which used to in turn do an #include of another header but no longer does.

Could you show us the libpqxx includes at the top of your relevant source file?

The .cpp file and the header file have those include

#include <stdexcept>
#include <iostream>
#include <typeinfo>
#include <string>
#include <pqxx/pqxx>

@CleanHit
Copy link
Author

And some completely unrelated notes on how to make the code "nicer":

1. Try replacing the `for` loops with this syntax: `for (pqxx::result::const_iterator iter : query_result)`.  Easier on the eyes!

2. Also, try replacing the `pqxx::result::const_iterator iter` with just `auto`.  It'll figure out the type on its own.

3. If you're querying a lot of rows here, you can speed it up by using [pqxx::transaction_base::stream()](https://libpqxx.readthedocs.io/en/stable/a01200.html#aec4d0f102c2c0fab8fa1a48f452abc0f).

4. Converting a field to `std::string` is pretty inefficient.  If performance is a concern here, consider using [pqxx::field::view()](https://libpqxx.readthedocs.io/en/stable/a01044.html#aa05908e8ed320fac8c96b9eb4cf46813) (when using `exec()` and `pqxx::result`) or converting to `std::string_view` (when using `pqxx::transaction_base::stream()`).

Thanks for the tips 😃. This file is relative old and will be refactored at some point.

jtv added a commit that referenced this issue Mar 25, 2022
@jtv
Copy link
Owner

jtv commented Mar 25, 2022

Looks like <pqxx/pqxx> didn't include all of the headers it needs to include now. Could you try the latest commit on master? I hope it'll fix this.

@CleanHit
Copy link
Author

Thanks @jtv, using the master branch resolved the errors.

@jtv jtv closed this as completed Mar 26, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants