Project

General

Profile

Support #13147 ยป sqldeleter.patch

Matthias Van Ceulebroeck, 10/29/2024 02:42 PM

View differences:

src/Wt/Dbo/FixedSqlConnectionPool.C
#endif // WT_THREADED
std::chrono::steady_clock::duration timeout{ std::chrono::steady_clock::duration::zero() };
std::vector<std::unique_ptr<SqlConnection>> freeList;
std::vector<std::unique_ptr<SqlConnection, SqlConnectionDeleter>> freeList;
};
FixedSqlConnectionPool::FixedSqlConnectionPool(std::unique_ptr<SqlConnection> connection,
FixedSqlConnectionPool::FixedSqlConnectionPool(std::unique_ptr<SqlConnection, SqlConnectionDeleter> connection,
int size)
: impl_(new Impl)
{
......
return impl_->timeout;
}
std::unique_ptr<SqlConnection> FixedSqlConnectionPool::getConnection()
std::unique_ptr<SqlConnection, SqlConnectionDeleter> FixedSqlConnectionPool::getConnection()
{
#ifdef WT_THREADED
std::unique_lock<std::mutex> lock(impl_->mutex);
......
"no connection available but single-threaded build?");
#endif // WT_THREADED
std::unique_ptr<SqlConnection> result = std::move(impl_->freeList.back());
std::unique_ptr<SqlConnection, SqlConnectionDeleter> result = std::move(impl_->freeList.back());
impl_->freeList.pop_back();
return result;
......
throw Exception("FixedSqlConnectionPool::getConnection(): timeout");
}
void FixedSqlConnectionPool::returnConnection(std::unique_ptr<SqlConnection> connection)
void FixedSqlConnectionPool::returnConnection(std::unique_ptr<SqlConnection, SqlConnectionDeleter> connection)
{
#ifdef WT_THREADED
std::unique_lock<std::mutex> lock(impl_->mutex);
src/Wt/Dbo/FixedSqlConnectionPool.h
* The pool is initialized with the provided \p connection, which is
* cloned (\p size - 1) times.
*/
FixedSqlConnectionPool(std::unique_ptr<SqlConnection> connection, int size);
FixedSqlConnectionPool(std::unique_ptr<SqlConnection, SqlConnectionDeleter> connection, int size);
/*! \brief Set a timeout to get a connection.
*
......
std::chrono::steady_clock::duration timeout() const;
virtual ~FixedSqlConnectionPool();
virtual std::unique_ptr<SqlConnection> getConnection() override;
virtual void returnConnection(std::unique_ptr<SqlConnection>) override;
virtual std::unique_ptr<SqlConnection, SqlConnectionDeleter> getConnection() override;
virtual void returnConnection(std::unique_ptr<SqlConnection, SqlConnectionDeleter>) override;
virtual void prepareForDropTables() const override;
protected:
src/Wt/Dbo/Session.C
delete i->second;
}
void Session::setConnection(std::unique_ptr<SqlConnection> connection)
void Session::setConnection(std::unique_ptr<SqlConnection, SqlConnectionDeleter> connection)
{
connection_ = std::move(connection);
}
......
return transaction_->connection_.get();
}
std::unique_ptr<SqlConnection> Session::useConnection()
std::unique_ptr<SqlConnection, SqlConnectionDeleter> Session::useConnection()
{
if (connectionPool_)
return connectionPool_->getConnection();
......
return std::move(connection_);
}
void Session::returnConnection(std::unique_ptr<SqlConnection> connection)
void Session::returnConnection(std::unique_ptr<SqlConnection, SqlConnectionDeleter> connection)
{
if (connectionPool_)
connectionPool_->returnConnection(std::move(connection));
......
sql << ")";
std::unique_ptr<SqlConnection> connPtr;
std::unique_ptr<SqlConnection, SqlConnectionDeleter> connPtr;
SqlConnection *conn;
if (transaction_)
conn = transaction_->connection_.get();
src/Wt/Dbo/Session.h
#include <Wt/Dbo/Transaction.h>
#include <Wt/Dbo/SqlConnection.h>
#include <Wt/Dbo/SqlConnectionDeleter.h>
namespace Wt {
namespace Dbo {
namespace Impl {
......
*
* \sa setConnectionPool()
*/
void setConnection(std::unique_ptr<SqlConnection> connection);
void setConnection(std::unique_ptr<SqlConnection, SqlConnectionDeleter> connection);
/*! \brief Sets a connection pool.
*
......
Impl::MetaDboBaseSet *dirtyObjects_;
std::vector<MetaDboBase*> objectsToAdd_;
std::unique_ptr<SqlConnection> connection_;
std::unique_ptr<SqlConnection, SqlConnectionDeleter> connection_;
SqlConnectionPool *connectionPool_;
Transaction::Impl *transaction_;
FlushMode flushMode_;
......
template <class C> std::string manyToManyJoinId(const std::string& joinName,
const std::string& notId);
std::unique_ptr<SqlConnection> useConnection();
void returnConnection(std::unique_ptr<SqlConnection> connection);
std::unique_ptr<SqlConnection, SqlConnectionDeleter> useConnection();
void returnConnection(std::unique_ptr<SqlConnection, SqlConnectionDeleter> connection);
SqlConnection *connection(bool openTransaction);
MetaDboBase *createDbo(Impl::MappingInfo *mapping);
src/Wt/Dbo/SqlConnection.h
#include <vector>
#include <Wt/Dbo/WDboDllDefs.h>
#include <Wt/Dbo/SqlConnectionDeleter.h>
namespace Wt {
namespace Dbo {
......
* object. This is used by connection pool implementations to create
* its connections.
*/
virtual std::unique_ptr<SqlConnection> clone() const = 0;
virtual std::unique_ptr<SqlConnection, SqlConnectionDeleter> clone() const = 0;
/*! \brief Executes an SQL statement.
*
src/Wt/Dbo/SqlConnectionPool.h
#define WT_DBO_SQL_CONNECTION_POOL_H_
#include <Wt/Dbo/WDboDllDefs.h>
#include <Wt/Dbo/SqlConnectionDeleter.h>
#include <memory>
#include <vector>
......
* This method is called by a Session when a new transaction is
* started.
*/
virtual std::unique_ptr<SqlConnection> getConnection() = 0;
virtual std::unique_ptr<SqlConnection, SqlConnectionDeleter> getConnection() = 0;
/*! \brief Returns a connection to the pool.
*
* This returns a connection to the pool. This method is called by a
* Session after a transaction has been finished.
*/
virtual void returnConnection(std::unique_ptr<SqlConnection>) = 0;
virtual void returnConnection(std::unique_ptr<SqlConnection, SqlConnectionDeleter>) = 0;
/*! \brief Prepares all connections in the pool for dropping the tables.
*/
src/Wt/Dbo/Transaction.h
#include <vector>
#include <Wt/Dbo/WDboDllDefs.h>
#include <Wt/Dbo/SqlConnectionDeleter.h>
namespace Wt {
namespace Dbo {
......
int transactionCount_;
std::vector<ptr_base *> objects_;
std::unique_ptr<SqlConnection> connection_;
std::unique_ptr<SqlConnection, SqlConnectionDeleter> connection_;
void open();
void commit();
src/Wt/Dbo/backend/Firebird.C
delete impl_;
}
std::unique_ptr<SqlConnection> Firebird::clone() const
std::unique_ptr<SqlConnection, SqlConnectionDeleter> Firebird::clone() const
{
return std::unique_ptr<SqlConnection>(new Firebird(*this));
return std::unique_ptr<SqlConnection, SqlConnectionDeleter>(new Firebird(*this));
}
std::unique_ptr<SqlStatement> Firebird::prepareStatement(const std::string& sql)
src/Wt/Dbo/backend/Firebird.h
const std::string& CharSet = std::string(),
const std::string& CreateParams = std::string());
virtual std::unique_ptr<SqlConnection> clone() const override;
virtual std::unique_ptr<SqlConnection, SqlConnectionDeleter> clone() const override;
/*! \brief Returns the underlying connection handle.
*/
src/Wt/Dbo/backend/MSSQLServer.C
delete impl_;
}
std::unique_ptr<SqlConnection> MSSQLServer::clone() const
std::unique_ptr<SqlConnection, SqlConnectionDeleter> MSSQLServer::clone() const
{
return std::unique_ptr<SqlConnection>(new MSSQLServer(*this));
return std::unique_ptr<SqlConnection, SqlConnectionDeleter>(new MSSQLServer(*this));
}
bool MSSQLServer::connect(const std::string &connectionString)
src/Wt/Dbo/backend/MSSQLServer.h
*/
virtual ~MSSQLServer();
virtual std::unique_ptr<Wt::Dbo::SqlConnection> clone() const override;
virtual std::unique_ptr<Wt::Dbo::SqlConnection, SqlConnectionDeleter> clone() const override;
/*! \brief Tries to connect.
*
src/Wt/Dbo/backend/MySQL.C
}
}
std::unique_ptr<SqlConnection> MySQL::clone() const
std::unique_ptr<SqlConnection, SqlConnectionDeleter> MySQL::clone() const
{
return std::unique_ptr<SqlConnection>(new MySQL(*this));
return std::unique_ptr<SqlConnection, SqlConnectionDeleter>(new MySQL(*this));
}
bool MySQL::connect(const std::string &db, const std::string &dbuser,
src/Wt/Dbo/backend/MySQL.h
/*! \brief Returns a copy of the connection.
*/
virtual std::unique_ptr<SqlConnection> clone() const override;
virtual std::unique_ptr<SqlConnection, SqlConnectionDeleter> clone() const override;
/*! \brief Tries to connect.
*
src/Wt/Dbo/backend/Postgres.C
timeout_ = timeout;
}
std::unique_ptr<SqlConnection> Postgres::clone() const
std::unique_ptr<SqlConnection, SqlConnectionDeleter> Postgres::clone() const
{
return std::unique_ptr<SqlConnection>(new Postgres(*this));
return std::unique_ptr<SqlConnection, SqlConnectionDeleter>(new Postgres(*this));
}
bool Postgres::connect(const std::string& db)
src/Wt/Dbo/backend/Postgres.h
*/
~Postgres();
virtual std::unique_ptr<SqlConnection> clone() const override;
virtual std::unique_ptr<SqlConnection, SqlConnectionDeleter> clone() const override;
/*! \brief Tries to connect.
*
src/Wt/Dbo/backend/Sqlite3.C
sqlite3_close(db_);
}
std::unique_ptr<SqlConnection> Sqlite3::clone() const
std::unique_ptr<SqlConnection, SqlConnectionDeleter> Sqlite3::clone() const
{
return std::unique_ptr<SqlConnection>(new Sqlite3(*this));
return std::unique_ptr<SqlConnection, SqlConnectionDeleter>(new Sqlite3(*this));
}
std::unique_ptr<SqlStatement> Sqlite3::prepareStatement(const std::string& sql)
src/Wt/Dbo/backend/Sqlite3.h
*/
~Sqlite3();
virtual std::unique_ptr<SqlConnection> clone() const override;
virtual std::unique_ptr<SqlConnection, SqlConnectionDeleter> clone() const override;
/*! \brief Returns the underlying connection.
*/
test/dbo/DboFixture.h
#include <Wt/Dbo/ptr_tuple.h>
#include <Wt/Dbo/QueryModel.h>
#include <Wt/Dbo/SqlConnectionDeleter.h>
namespace dbo = Wt::Dbo;
#if defined(SQLITE3)
......
DboFixtureBase(bool showQueries = true)
{
static bool logged = false;
std::unique_ptr<dbo::SqlConnection> connection;
std::unique_ptr<dbo::SqlConnection, SqlConnectionDeleter> connection;
#ifdef SQLITE3
if (!logged) {
......
sqlite3->setDateTimeStorage
(dbo::SqlDateTimeType::Date,
dbo::backend::DateTimeStorage::JulianDaysAsReal);
connection = std::unique_ptr<dbo::SqlConnection>(sqlite3);
connection = std::unique_ptr<dbo::SqlConnection, SqlConnectionDeleter>(sqlite3);
#endif // SQLITE3
#ifdef POSTGRES
......
logged = true;
}
connection = std::unique_ptr<dbo::SqlConnection>(new dbo::backend::Postgres
connection = std::unique_ptr<dbo::SqlConnection, SqlConnectionDeleter>(new dbo::backend::Postgres
("host=db user=postgres_test password=postgres_test port=5432 dbname=wt_test"));
#endif // POSTGRES");
......
logged = true;
}
std::unique_ptr<dbo::backend::MySQL> mysql(
std::unique_ptr<dbo::backend::MySQL, SqlConnectionDeleter> mysql(
new dbo::backend::MySQL("wt_test_db", "test_user", "test_pw", "db", 3306));
mysql->setFractionalSecondsPart(3);
connection = std::move(mysql);
......
logged = true;
}
connection = std::unique_ptr<dbo::SqlConnection>(
connection = std::unique_ptr<dbo::SqlConnection, SqlConnectionDeleter>(
new dbo::backend::Firebird ("db",
file,
"test_user", "test_pwd",
......
logged = true;
}
connection = std::unique_ptr<dbo::SqlConnection>(
connection = std::unique_ptr<dbo::SqlConnection, SqlConnectionDeleter>(
new dbo::backend::MSSQLServer(
"Driver={ODBC Driver 17 for SQL Server};"
"Server=db;"
test/dbo/DboTest4.C
}
// None of the following should throw!
std::unique_ptr<Wt::Dbo::SqlConnection> connection = f.connectionPool_->getConnection();
std::unique_ptr<Wt::Dbo::SqlConnection, SqlConnectionDeleter> connection = f.connectionPool_->getConnection();
connection->startTransaction();
connection->executeSql("SELECT \"side1_name\",\"side2_name\" FROM \"friends1\"");
connection->executeSql("SELECT \"side1\",\"side2\" FROM \"friends2\"");
test/dbo/JsonTest.C
logged = true;
}
std::unique_ptr<dbo::SqlConnection> sqlite3(new dbo::backend::Sqlite3(":memory:"));
std::unique_ptr<dbo::SqlConnection, SqlConnectionDeleter> sqlite3(new dbo::backend::Sqlite3(":memory:"));
session_ = std::unique_ptr<dbo::Session>(new dbo::Session());
session_->setConnection(std::move(sqlite3));
test/dbo/SqliteTest.C
BOOST_AUTO_TEST_CASE( sqlite3_test_iso_timestamp )
{
auto sqlite3 = std::make_unique<dbo::backend::Sqlite3>(":memory:");
auto sqlite3 = std::unique_ptr<dbo::backend::Sqlite3, SqlConnectionDeleter>(new dbo::backend::Sqlite3(":memory:"));
sqlite3->setProperty("show-queries", "true");
sqlite3->setDateTimeStorage(dbo::SqlDateTimeType::Date, dbo::backend::DateTimeStorage::ISO8601AsText);
sqlite3->setDateTimeStorage(dbo::SqlDateTimeType::DateTime, dbo::backend::DateTimeStorage::ISO8601AsText);
......
BOOST_AUTO_TEST_CASE( sqlite3_test_julian_day )
{
auto sqlite3 = std::make_unique<dbo::backend::Sqlite3>(":memory:");
auto sqlite3 = std::unique_ptr<dbo::backend::Sqlite3, SqlConnectionDeleter>(new dbo::backend::Sqlite3(":memory:"));
sqlite3->setProperty("show-queries", "true");
sqlite3->setDateTimeStorage(dbo::SqlDateTimeType::Date, dbo::backend::DateTimeStorage::JulianDaysAsReal);
sqlite3->setDateTimeStorage(dbo::SqlDateTimeType::DateTime, dbo::backend::DateTimeStorage::JulianDaysAsReal);
......
BOOST_AUTO_TEST_CASE( sqlite3_test_unix_timestamp )
{
auto sqlite3 = std::make_unique<dbo::backend::Sqlite3>(":memory:");
auto sqlite3 = std::unique_ptr<dbo::backend::Sqlite3, SqlConnectionDeleter>(new dbo::backend::Sqlite3(":memory:"));
sqlite3->setProperty("show-queries", "true");
sqlite3->setDateTimeStorage(dbo::SqlDateTimeType::Date, dbo::backend::DateTimeStorage::UnixTimeAsInteger);
sqlite3->setDateTimeStorage(dbo::SqlDateTimeType::DateTime, dbo::backend::DateTimeStorage::UnixTimeAsInteger);
    (1-1/1)