Skip to content

Commit

Permalink
Support :"ident" and :'literal' parameters of Sql_string
Browse files Browse the repository at this point in the history
  • Loading branch information
dmitigr committed Feb 18, 2022
1 parent a97cb8a commit c8352b1
Show file tree
Hide file tree
Showing 8 changed files with 354 additions and 123 deletions.
16 changes: 16 additions & 0 deletions src/pgfe/connection.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
#include "connection.hpp"
#include "exceptions.hpp"
#include "large_object.hpp"
#include "sql_string.hpp"

namespace dmitigr::pgfe {

Expand Down Expand Up @@ -478,6 +479,21 @@ DMITIGR_PGFE_INLINE Completion Connection::completion() noexcept
}
}

DMITIGR_PGFE_INLINE void
Connection::prepare_nio(const Sql_string& statement, const std::string& name)
{
assert(!statement.has_missing_parameters());
prepare_nio__(statement.to_query_string(*this).c_str(),
name.c_str(), &statement); // can throw
}

DMITIGR_PGFE_INLINE Prepared_statement&
Connection::prepare(const Sql_string& statement, const std::string& name)
{
using M = void(Connection::*)(const Sql_string&, const std::string&);
return prepare__(static_cast<M>(&Connection::prepare_nio), statement, name);
}

DMITIGR_PGFE_INLINE void Connection::describe_nio(const std::string& name)
{
assert(is_ready_for_nio_request());
Expand Down
21 changes: 8 additions & 13 deletions src/pgfe/connection.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@
#include "notification.hpp"
#include "pq.hpp"
#include "prepared_statement.hpp"
#include "sql_string.hpp"
#include "types_fwd.hpp"

#include <cassert>
Expand Down Expand Up @@ -730,11 +729,8 @@ class Connection final {
*
* @see unprepare_nio().
*/
void prepare_nio(const Sql_string& statement, const std::string& name = {})
{
assert(!statement.has_missing_parameters());
prepare_nio__(statement.to_query_string().c_str(), name.c_str(), &statement); // can throw
}
DMITIGR_PGFE_API void prepare_nio(const Sql_string& statement,
const std::string& name = {});

/// Same as prepare_nio() except the statement will be send without preparsing.
void prepare_nio_as_is(const std::string& statement, const std::string& name = {})
Expand All @@ -755,14 +751,12 @@ class Connection final {
*
* @see unprepare().
*/
Prepared_statement& prepare(const Sql_string& statement, const std::string& name = {})
{
using M = void(Connection::*)(const Sql_string&, const std::string&);
return prepare__(static_cast<M>(&Connection::prepare_nio), statement, name);
}
DMITIGR_PGFE_API Prepared_statement& prepare(const Sql_string& statement,
const std::string& name = {});

/// Same as prepare() except the statement will be send without preparsing.
Prepared_statement& prepare_as_is(const std::string& statement, const std::string& name = {})
Prepared_statement& prepare_as_is(const std::string& statement,
const std::string& name = {})
{
return prepare__(&Connection::prepare_nio_as_is, statement, name);
}
Expand Down Expand Up @@ -1302,7 +1296,8 @@ class Connection final {
// Prepared statement helpers
// ---------------------------------------------------------------------------

void prepare_nio__(const char* const query, const char* const name, const Sql_string* const preparsed);
void prepare_nio__(const char* const query, const char* const name,
const Sql_string* const preparsed);

template<typename M, typename T>
Prepared_statement& prepare__(M&& prepare, T&& statement, const std::string& name)
Expand Down
12 changes: 8 additions & 4 deletions src/pgfe/prepared_statement.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -134,11 +134,15 @@ DMITIGR_PGFE_INLINE void Prepared_statement::execute_nio__(const Sql_string* con

const int send_ok = statement
?
::PQsendQueryParams(connection_->conn(), statement->to_query_string().c_str(),
param_count, nullptr, values.data(), lengths.data(), formats.data(), result_format)
::PQsendQueryParams(connection_->conn(),
statement->to_query_string(*connection_).c_str(),
param_count, nullptr, values.data(), lengths.data(),
formats.data(), result_format)
:
::PQsendQueryPrepared(connection_->conn(), name_.c_str(),
param_count, values.data(), lengths.data(), formats.data(), result_format);
::PQsendQueryPrepared(connection_->conn(),
name_.c_str(),
param_count, values.data(), lengths.data(),
formats.data(), result_format);

if (!send_ok)
throw std::runtime_error{connection_->error_message()};
Expand Down
Loading

0 comments on commit c8352b1

Please sign in to comment.