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

How do I define a custom citext type provided by postgres as an extension in code? #299

Open
AntonMamedov opened this issue Jun 9, 2021 · 2 comments
Labels

Comments

@AntonMamedov
Copy link

AntonMamedov commented Jun 9, 2021

How do I define a custom citext type provided by postgres as an extension in code? The example provided in this video does not work. This code does not compile
https://www.youtube.com/watch?v=-1zbaxuUsMA

namespace test {
    OZO_STRONG_TYPEDEF(std::string, citext)
}

OZO_PG_DEFINE_CUSTOM_TYPE(test::citext, "citext", dynamic_size)

namespace asio = boost::asio;

using namespace ozo::literals;
using namespace std::chrono_literals;

int main() {
    using namespace std::chrono_literals;
    const auto oid_map = ozo::register_types<test::citext>();
    boost::asio::io_context io;
    std::string dbOpts = "host=localhost port=5432 dbname=postgres user=postgres password=postgres";

    const ozo::connection_info<decltype(oid_map)> connection_info(  dbOpts);

    ozo::connection_pool_config connection_pool_config;
    connection_pool_config.capacity = 4;
    connection_pool_config.queue_capacity = 10;
    connection_pool_config.idle_timeout = std::chrono::seconds(60);
    connection_pool_config.lifespan = std::chrono::hours(24);
    ozo::connection_pool connection_pool(connection_info, connection_pool_config);
    std::vector<test::citext> rows;
    ozo::request(connection_pool[io], "SELECT 'TeSt'::CITEXT"_SQL, 1s, ozo::into(rows), [&](auto ec, auto conn){
        if (ec) {
            std::cerr << ec.message();
            std::cerr << " | " << ozo::error_message(conn);
            if (!ozo::is_null_recursive(conn)) {
                std::cerr << " | " << ozo::get_error_context(conn);
            }
        }
    });
    for (auto row : rows){
        std::cout << row.get() << std::endl;
    }
    io.run();
}
@elsid
Copy link
Collaborator

elsid commented Jun 9, 2021

Hi, the video is quite outdated now. There have been many API changes since then. Examples provided there will compile only with the library version of that time. So I wouldn't rely on specific code examples there, only on some principles.

Your code example does not compile because connection_info type has const OidMap template parameter. It should have a non-const OidMap parameter because it modifies the map when a custom type oid is received from the server. I suggest to declared it as a non-const:

using oid_map_t = decltype(ozo::register_types<test::citext>());
const ozo::connection_info<oid_map_t> connection_info(dbOpts);

@AntonMamedov
Copy link
Author

AntonMamedov commented Jun 13, 2021

Thanks for the quick response.
Several more questions arose.
1) If during a transaction from the request or execute function I receive an error and I want to throw an exception without calling commit. Is it safe to do this, will not the connection allocated for the transaction hang?
2) As I understand it, make_query can be used instead of _SQL literals to query the build at compile time. But how can you collect a query at runtime?
Unfortunately, I have not found an answer to this question either in the documentation or in the examples.
3) While the connection object is alive, the postgres connection descriptor is held by it and is not used elsewhere, or while the connection object is alive, the descriptor used in it can be used by other connection objects?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants