mirror of
https://github.com/rbock/sqlpp11.git
synced 2024-11-15 20:31:16 +08:00
Remove obsolete pointer indirection and some warnings
This commit is contained in:
parent
d6aef0fa9b
commit
ccc75eafc7
@ -443,17 +443,17 @@ namespace sqlpp
|
||||
|
||||
inline size_t connection::insert_impl(const std::string& stmt)
|
||||
{
|
||||
return execute(stmt)->result.affected_rows();
|
||||
return static_cast<size_t>(execute(stmt)->result.affected_rows());
|
||||
}
|
||||
|
||||
inline size_t connection::update_impl(const std::string& stmt)
|
||||
{
|
||||
return execute(stmt)->result.affected_rows();
|
||||
return static_cast<size_t>(execute(stmt)->result.affected_rows());
|
||||
}
|
||||
|
||||
inline size_t connection::remove_impl(const std::string& stmt)
|
||||
{
|
||||
return execute(stmt)->result.affected_rows();
|
||||
return static_cast<size_t>(execute(stmt)->result.affected_rows());
|
||||
}
|
||||
|
||||
// prepared execution
|
||||
@ -474,28 +474,28 @@ namespace sqlpp
|
||||
{
|
||||
validate_connection_handle();
|
||||
execute_prepared_statement(*_handle, *prep._handle.get());
|
||||
return prep._handle->result.affected_rows();
|
||||
return static_cast<size_t>(prep._handle->result.affected_rows());
|
||||
}
|
||||
|
||||
inline size_t connection::run_prepared_insert_impl(prepared_statement_t& prep)
|
||||
{
|
||||
validate_connection_handle();
|
||||
execute_prepared_statement(*_handle, *prep._handle.get());
|
||||
return prep._handle->result.affected_rows();
|
||||
return static_cast<size_t>(prep._handle->result.affected_rows());
|
||||
}
|
||||
|
||||
inline size_t connection::run_prepared_update_impl(prepared_statement_t& prep)
|
||||
{
|
||||
validate_connection_handle();
|
||||
execute_prepared_statement(*_handle, *prep._handle.get());
|
||||
return prep._handle->result.affected_rows();
|
||||
return static_cast<size_t>(prep._handle->result.affected_rows());
|
||||
}
|
||||
|
||||
inline size_t connection::run_prepared_remove_impl(prepared_statement_t& prep)
|
||||
{
|
||||
validate_connection_handle();
|
||||
execute_prepared_statement(*_handle, *prep._handle.get());
|
||||
return prep._handle->result.affected_rows();
|
||||
return static_cast<size_t>(prep._handle->result.affected_rows());
|
||||
}
|
||||
|
||||
inline void connection::set_default_isolation_level(isolation_level level)
|
||||
@ -561,7 +561,7 @@ namespace sqlpp
|
||||
result.resize((s.size() * 2) + 1);
|
||||
|
||||
int err;
|
||||
size_t length = PQescapeStringConn(_handle->postgres, &result[0], s.c_str(), s.size(), &err);
|
||||
size_t length = PQescapeStringConn(_handle->native(), &result[0], s.c_str(), s.size(), &err);
|
||||
result.resize(length);
|
||||
return result;
|
||||
}
|
||||
@ -662,7 +662,7 @@ namespace sqlpp
|
||||
inline uint64_t connection::last_insert_id(const std::string& table, const std::string& fieldname)
|
||||
{
|
||||
std::string sql = "SELECT currval('" + table + "_" + fieldname + "_seq')";
|
||||
PGresult* res = PQexec(_handle->postgres, sql.c_str());
|
||||
PGresult* res = PQexec(_handle->native(), sql.c_str());
|
||||
if (PQresultStatus(res) != PGRES_TUPLES_OK)
|
||||
{
|
||||
std::string err{PQresultErrorMessage(res)};
|
||||
@ -673,12 +673,12 @@ namespace sqlpp
|
||||
// Parse the number and return.
|
||||
std::string in{PQgetvalue(res, 0, 0)};
|
||||
PQclear(res);
|
||||
return std::stoi(in);
|
||||
return std::stoul(in);
|
||||
}
|
||||
|
||||
inline ::PGconn* connection::native_handle()
|
||||
{
|
||||
return _handle->postgres;
|
||||
return _handle->native();
|
||||
}
|
||||
|
||||
inline std::string context_t::escape(const std::string& arg)
|
||||
|
@ -53,28 +53,34 @@ namespace sqlpp
|
||||
|
||||
namespace detail
|
||||
{
|
||||
inline void handle_cleanup(PGconn* postgres)
|
||||
{
|
||||
PQfinish(postgres);
|
||||
}
|
||||
|
||||
struct DLL_LOCAL connection_handle
|
||||
{
|
||||
const std::shared_ptr<connection_config> config;
|
||||
PGconn* postgres{nullptr};
|
||||
std::set<std::string> prepared_statement_names;
|
||||
std::shared_ptr<connection_config> config;
|
||||
std::unique_ptr<PGconn, void(*)(PGconn*)> postgres;
|
||||
std::set<std::string> prepared_statement_names;
|
||||
|
||||
connection_handle(const std::shared_ptr<connection_config>& config);
|
||||
~connection_handle();
|
||||
connection_handle(const connection_handle&) = delete;
|
||||
connection_handle(connection_handle&&) = delete;
|
||||
connection_handle(connection_handle&&) = default;
|
||||
connection_handle& operator=(const connection_handle&) = delete;
|
||||
connection_handle& operator=(connection_handle&&) = delete;
|
||||
connection_handle& operator=(connection_handle&&) = default;
|
||||
~connection_handle();
|
||||
|
||||
PGconn* native() const
|
||||
{
|
||||
return postgres;
|
||||
return postgres.get();
|
||||
}
|
||||
|
||||
void deallocate_prepared_statement(const std::string& name);
|
||||
};
|
||||
|
||||
inline connection_handle::connection_handle(const std::shared_ptr<connection_config>& conf) : config(conf)
|
||||
inline connection_handle::connection_handle(const std::shared_ptr<connection_config>& conf)
|
||||
: config(conf), postgres{nullptr, handle_cleanup}
|
||||
{
|
||||
#ifdef SQLPP_DYNAMIC_LOADING
|
||||
init_pg("");
|
||||
@ -194,18 +200,15 @@ namespace sqlpp
|
||||
{
|
||||
conninfo.append(" service=" + config->service);
|
||||
}
|
||||
if (this->postgres)
|
||||
return;
|
||||
|
||||
this->postgres = PQconnectdb(conninfo.c_str());
|
||||
postgres.reset(PQconnectdb(conninfo.c_str()));
|
||||
|
||||
if (!this->postgres)
|
||||
if (!postgres)
|
||||
throw std::bad_alloc();
|
||||
|
||||
if (PQstatus(this->postgres) != CONNECTION_OK)
|
||||
if (PQstatus(postgres.get()) != CONNECTION_OK)
|
||||
{
|
||||
std::string msg(PQerrorMessage(this->postgres));
|
||||
PQfinish(this->postgres);
|
||||
std::string msg(PQerrorMessage(postgres.get()));
|
||||
throw broken_connection(std::move(msg));
|
||||
}
|
||||
}
|
||||
@ -217,18 +220,12 @@ namespace sqlpp
|
||||
{
|
||||
std::cerr << "PostgreSQL debug: closing database connection." << std::endl;
|
||||
}
|
||||
|
||||
// Close connection
|
||||
if (this->postgres)
|
||||
{
|
||||
PQfinish(this->postgres);
|
||||
}
|
||||
}
|
||||
|
||||
inline void connection_handle::deallocate_prepared_statement(const std::string& name)
|
||||
{
|
||||
std::string cmd = "DEALLOCATE \"" + name + "\"";
|
||||
PGresult* result = PQexec(postgres, cmd.c_str());
|
||||
PGresult* result = PQexec(postgres.get(), cmd.c_str());
|
||||
PQclear(result);
|
||||
prepared_statement_names.erase(name);
|
||||
}
|
||||
|
@ -65,9 +65,9 @@ namespace sqlpp
|
||||
// ctor
|
||||
statement_handle_t(detail::connection_handle& _connection);
|
||||
statement_handle_t(const statement_handle_t&) = delete;
|
||||
statement_handle_t(statement_handle_t&&) = default;
|
||||
statement_handle_t(statement_handle_t&&) = delete;
|
||||
statement_handle_t& operator=(const statement_handle_t&) = delete;
|
||||
statement_handle_t& operator=(statement_handle_t&&) = default;
|
||||
statement_handle_t& operator=(statement_handle_t&&) = delete;
|
||||
|
||||
virtual ~statement_handle_t();
|
||||
bool operator!() const;
|
||||
@ -89,9 +89,9 @@ namespace sqlpp
|
||||
// ctor
|
||||
prepared_statement_handle_t(detail::connection_handle& _connection, std::string stmt, const size_t& paramCount);
|
||||
prepared_statement_handle_t(const prepared_statement_handle_t&) = delete;
|
||||
prepared_statement_handle_t(prepared_statement_handle_t&&) = default;
|
||||
prepared_statement_handle_t(prepared_statement_handle_t&&) = delete;
|
||||
prepared_statement_handle_t& operator=(const prepared_statement_handle_t&) = delete;
|
||||
prepared_statement_handle_t& operator=(prepared_statement_handle_t&&) = default;
|
||||
prepared_statement_handle_t& operator=(prepared_statement_handle_t&&) = delete;
|
||||
|
||||
virtual ~prepared_statement_handle_t();
|
||||
|
||||
@ -153,10 +153,10 @@ namespace sqlpp
|
||||
|
||||
inline void prepared_statement_handle_t::execute()
|
||||
{
|
||||
int size = static_cast<int>(paramValues.size());
|
||||
const size_t size = paramValues.size();
|
||||
|
||||
std::vector<const char*> values;
|
||||
for (int i = 0; i < size; i++)
|
||||
for (size_t i = 0u; i < size; i++)
|
||||
values.push_back(nullValues[i] ? nullptr : const_cast<char*>(paramValues[i].c_str()));
|
||||
|
||||
// Execute prepared statement with the parameters.
|
||||
@ -164,7 +164,7 @@ namespace sqlpp
|
||||
valid = false;
|
||||
count = 0;
|
||||
totalCount = 0;
|
||||
result = PQexecPrepared(connection.postgres, _name.data(), size, values.data(), nullptr, nullptr, 0);
|
||||
result = PQexecPrepared(connection.native(), _name.data(), static_cast<int>(size), values.data(), nullptr, nullptr, 0);
|
||||
/// @todo validate result? is it really valid
|
||||
valid = true;
|
||||
}
|
||||
@ -188,7 +188,7 @@ namespace sqlpp
|
||||
inline void prepared_statement_handle_t::prepare(std::string stmt)
|
||||
{
|
||||
// Create the prepared statement
|
||||
result = PQprepare(connection.postgres, _name.c_str(), stmt.c_str(), 0, nullptr);
|
||||
result = PQprepare(connection.native(), _name.c_str(), stmt.c_str(), 0, nullptr);
|
||||
valid = true;
|
||||
}
|
||||
}
|
||||
|
@ -76,10 +76,10 @@ namespace sqlpp
|
||||
T t(0);
|
||||
auto txt = std::string(getPqValue(m_result, record, field));
|
||||
if(txt != "")
|
||||
|
||||
{
|
||||
t = std::stold(txt);
|
||||
}
|
||||
|
||||
return t;
|
||||
}
|
||||
|
||||
@ -87,6 +87,7 @@ namespace sqlpp
|
||||
{
|
||||
return m_query;
|
||||
}
|
||||
|
||||
std::string& query()
|
||||
{
|
||||
return m_query;
|
||||
|
@ -40,7 +40,7 @@ namespace sqlpp
|
||||
{
|
||||
namespace postgresql
|
||||
{
|
||||
struct connection;
|
||||
class connection;
|
||||
}
|
||||
|
||||
namespace detail
|
||||
|
Loading…
Reference in New Issue
Block a user