1
0
mirror of https://github.com/VCMP-SqMod/SqMod.git synced 2024-11-08 08:47:17 +01:00

Allow session pool to be used with legacy database wrappers (experimental).

This commit is contained in:
Sandu Liviu Catalin 2022-07-04 19:39:41 +03:00
parent b8b5e89216
commit 8f11e08150
6 changed files with 706 additions and 596 deletions

File diff suppressed because it is too large Load Diff

File diff suppressed because it is too large Load Diff

View File

@ -362,6 +362,15 @@ static const EnumElement g_MainEnum[] = {
{_SC("WARNING_AUTOINDEX"), SQLITE_WARNING_AUTOINDEX} {_SC("WARNING_AUTOINDEX"), SQLITE_WARNING_AUTOINDEX}
}; };
// ------------------------------------------------------------------------------------------------
LightObj GteSQLiteFromSession(Poco::Data::SessionImpl * session)
{
// Create a reference counted connection handle instance
SQLiteConnRef ref(new SQLiteConnHnd(session));
// Transform it into a connection instance and yield it as a script object
return LightObj(SqTypeIdentity< SQLiteConnection >{}, SqVM(), ref);
}
// ------------------------------------------------------------------------------------------------ // ------------------------------------------------------------------------------------------------
static inline bool IsDigitsOnly(const SQChar * str) static inline bool IsDigitsOnly(const SQChar * str)
{ {
@ -374,13 +383,13 @@ static inline bool IsDigitsOnly(const SQChar * str)
} }
// ------------------------------------------------------------------------------------------------ // ------------------------------------------------------------------------------------------------
Object GetConnectionObj(const ConnRef & conn) Object GetConnectionObj(const SQLiteConnRef & conn)
{ {
return Object(new SQLiteConnection(conn)); return Object(new SQLiteConnection(conn));
} }
// ------------------------------------------------------------------------------------------------ // ------------------------------------------------------------------------------------------------
Object GetStatementObj(const StmtRef & stmt) Object GetStatementObj(const SQLiteStmtRef & stmt)
{ {
return Object(new SQLiteStatement(stmt)); return Object(new SQLiteStatement(stmt));
} }
@ -602,6 +611,7 @@ SQLiteConnHnd::SQLiteConnHnd()
, mFlags(0) , mFlags(0)
, mName() , mName()
, mVFS() , mVFS()
, mSession()
, mMemory(false) , mMemory(false)
, mTrace(false) , mTrace(false)
, mProfile(false) , mProfile(false)
@ -609,6 +619,15 @@ SQLiteConnHnd::SQLiteConnHnd()
/* ... */ /* ... */
} }
// ------------------------------------------------------------------------------------------------
SQLiteConnHnd::SQLiteConnHnd(Poco::Data::SessionImpl * session)
: SQLiteConnHnd()
{
mSession.assign(session);
// Retrieve the internal handle property
mPtr = Poco::AnyCast< sqlite3 * >(session->getProperty("handle"));
}
// ------------------------------------------------------------------------------------------------ // ------------------------------------------------------------------------------------------------
SQLiteConnHnd::~SQLiteConnHnd() SQLiteConnHnd::~SQLiteConnHnd()
{ {
@ -619,10 +638,17 @@ SQLiteConnHnd::~SQLiteConnHnd()
Flush(static_cast<uint32_t>(mQueue.size()), NullObject(), NullFunction()); Flush(static_cast<uint32_t>(mQueue.size()), NullObject(), NullFunction());
// NOTE: Should we call sqlite3_interrupt(...) before closing? // NOTE: Should we call sqlite3_interrupt(...) before closing?
// Attempt to close the database // Attempt to close the database
if ((sqlite3_close(mPtr)) != SQLITE_OK) // If this connection is a pooled session then let it clean itself up
if (mSession.isNull() && (sqlite3_close(mPtr)) != SQLITE_OK)
{ {
LogErr("Unable to close SQLite connection [%s]", sqlite3_errmsg(mPtr)); LogErr("Unable to close SQLite connection [%s]", sqlite3_errmsg(mPtr));
} }
else
{
mSession.reset();
}
// Prevent further use of this connection
mPtr = nullptr;
} }
} }
@ -751,7 +777,7 @@ int32_t SQLiteConnHnd::Flush(uint32_t num, Object & env, Function & func)
} }
// ------------------------------------------------------------------------------------------------ // ------------------------------------------------------------------------------------------------
SQLiteStmtHnd::SQLiteStmtHnd(ConnRef conn) SQLiteStmtHnd::SQLiteStmtHnd(SQLiteConnRef conn)
: mPtr(nullptr) : mPtr(nullptr)
, mStatus(SQLITE_OK) , mStatus(SQLITE_OK)
, mConn(std::move(conn)) , mConn(std::move(conn))
@ -942,13 +968,13 @@ void SQLiteConnection::ValidateCreated() const
// ------------------------------------------------------------------------------------------------ // ------------------------------------------------------------------------------------------------
#if defined(_DEBUG) || defined(SQMOD_EXCEPTLOC) #if defined(_DEBUG) || defined(SQMOD_EXCEPTLOC)
const ConnRef & SQLiteConnection::GetValid(const char * file, int32_t line) const const SQLiteConnRef & SQLiteConnection::GetValid(const char * file, int32_t line) const
{ {
Validate(file, line); Validate(file, line);
return m_Handle; return m_Handle;
} }
#else #else
const ConnRef & SQLiteConnection::GetValid() const const SQLiteConnRef & SQLiteConnection::GetValid() const
{ {
Validate(); Validate();
return m_Handle; return m_Handle;
@ -957,13 +983,13 @@ const ConnRef & SQLiteConnection::GetValid() const
// ------------------------------------------------------------------------------------------------ // ------------------------------------------------------------------------------------------------
#if defined(_DEBUG) || defined(SQMOD_EXCEPTLOC) #if defined(_DEBUG) || defined(SQMOD_EXCEPTLOC)
const ConnRef & SQLiteConnection::GetCreated(const char * file, int32_t line) const const SQLiteConnRef & SQLiteConnection::GetCreated(const char * file, int32_t line) const
{ {
ValidateCreated(file, line); ValidateCreated(file, line);
return m_Handle; return m_Handle;
} }
#else #else
const ConnRef & SQLiteConnection::GetCreated() const const SQLiteConnRef & SQLiteConnection::GetCreated() const
{ {
ValidateCreated(); ValidateCreated();
return m_Handle; return m_Handle;
@ -976,7 +1002,7 @@ void SQLiteConnection::Open(StackStrF & name)
// Should we create a connection handle? // Should we create a connection handle?
if (!m_Handle) if (!m_Handle)
{ {
m_Handle = ConnRef(new SQLiteConnHnd()); m_Handle = SQLiteConnRef(new SQLiteConnHnd());
} }
// Make sure another database isn't opened // Make sure another database isn't opened
if (SQMOD_GET_VALID(*this)->mPtr != nullptr) if (SQMOD_GET_VALID(*this)->mPtr != nullptr)
@ -996,7 +1022,7 @@ void SQLiteConnection::Open(StackStrF & name, int32_t flags)
// Should we create a connection handle? // Should we create a connection handle?
if (!m_Handle) if (!m_Handle)
{ {
m_Handle = ConnRef(new SQLiteConnHnd()); m_Handle = SQLiteConnRef(new SQLiteConnHnd());
} }
// Make sure another database isn't opened // Make sure another database isn't opened
if (SQMOD_GET_VALID(*this)->mPtr != nullptr) if (SQMOD_GET_VALID(*this)->mPtr != nullptr)
@ -1013,7 +1039,7 @@ void SQLiteConnection::Open(StackStrF & name, int32_t flags, StackStrF & vfs)
// Should we create a connection handle? // Should we create a connection handle?
if (!m_Handle) if (!m_Handle)
{ {
m_Handle = ConnRef(new SQLiteConnHnd()); m_Handle = SQLiteConnRef(new SQLiteConnHnd());
} }
// Make sure another database isn't opened // Make sure another database isn't opened
if (SQMOD_GET_VALID(*this)->mPtr != nullptr) if (SQMOD_GET_VALID(*this)->mPtr != nullptr)
@ -1295,13 +1321,13 @@ void SQLiteParameter::ValidateCreated() const
// ------------------------------------------------------------------------------------------------ // ------------------------------------------------------------------------------------------------
#if defined(_DEBUG) || defined(SQMOD_EXCEPTLOC) #if defined(_DEBUG) || defined(SQMOD_EXCEPTLOC)
const StmtRef & SQLiteParameter::GetValid(const char * file, int32_t line) const const SQLiteStmtRef & SQLiteParameter::GetValid(const char * file, int32_t line) const
{ {
Validate(file, line); Validate(file, line);
return m_Handle; return m_Handle;
} }
#else #else
const StmtRef & SQLiteParameter::GetValid() const const SQLiteStmtRef & SQLiteParameter::GetValid() const
{ {
Validate(); Validate();
return m_Handle; return m_Handle;
@ -1310,13 +1336,13 @@ const StmtRef & SQLiteParameter::GetValid() const
// ------------------------------------------------------------------------------------------------ // ------------------------------------------------------------------------------------------------
#if defined(_DEBUG) || defined(SQMOD_EXCEPTLOC) #if defined(_DEBUG) || defined(SQMOD_EXCEPTLOC)
const StmtRef & SQLiteParameter::GetCreated(const char * file, int32_t line) const const SQLiteStmtRef & SQLiteParameter::GetCreated(const char * file, int32_t line) const
{ {
ValidateCreated(file, line); ValidateCreated(file, line);
return m_Handle; return m_Handle;
} }
#else #else
const StmtRef & SQLiteParameter::GetCreated() const const SQLiteStmtRef & SQLiteParameter::GetCreated() const
{ {
ValidateCreated(); ValidateCreated();
return m_Handle; return m_Handle;
@ -2001,13 +2027,13 @@ void SQLiteColumn::ValidateCreated() const
// ------------------------------------------------------------------------------------------------ // ------------------------------------------------------------------------------------------------
#if defined(_DEBUG) || defined(SQMOD_EXCEPTLOC) #if defined(_DEBUG) || defined(SQMOD_EXCEPTLOC)
const StmtRef & SQLiteColumn::GetValid(const char * file, int32_t line) const const SQLiteStmtRef & SQLiteColumn::GetValid(const char * file, int32_t line) const
{ {
Validate(file, line); Validate(file, line);
return m_Handle; return m_Handle;
} }
#else #else
const StmtRef & SQLiteColumn::GetValid() const const SQLiteStmtRef & SQLiteColumn::GetValid() const
{ {
Validate(); Validate();
return m_Handle; return m_Handle;
@ -2016,13 +2042,13 @@ const StmtRef & SQLiteColumn::GetValid() const
// ------------------------------------------------------------------------------------------------ // ------------------------------------------------------------------------------------------------
#if defined(_DEBUG) || defined(SQMOD_EXCEPTLOC) #if defined(_DEBUG) || defined(SQMOD_EXCEPTLOC)
const StmtRef & SQLiteColumn::GetCreated(const char * file, int32_t line) const const SQLiteStmtRef & SQLiteColumn::GetCreated(const char * file, int32_t line) const
{ {
ValidateCreated(file, line); ValidateCreated(file, line);
return m_Handle; return m_Handle;
} }
#else #else
const StmtRef & SQLiteColumn::GetCreated() const const SQLiteStmtRef & SQLiteColumn::GetCreated() const
{ {
ValidateCreated(); ValidateCreated();
return m_Handle; return m_Handle;
@ -2454,13 +2480,13 @@ void SQLiteStatement::ValidateCreated() const
// ------------------------------------------------------------------------------------------------ // ------------------------------------------------------------------------------------------------
#if defined(_DEBUG) || defined(SQMOD_EXCEPTLOC) #if defined(_DEBUG) || defined(SQMOD_EXCEPTLOC)
const StmtRef & SQLiteStatement::GetValid(const char * file, int32_t line) const const SQLiteStmtRef & SQLiteStatement::GetValid(const char * file, int32_t line) const
{ {
Validate(file, line); Validate(file, line);
return m_Handle; return m_Handle;
} }
#else #else
const StmtRef & SQLiteStatement::GetValid() const const SQLiteStmtRef & SQLiteStatement::GetValid() const
{ {
Validate(); Validate();
return m_Handle; return m_Handle;
@ -2469,13 +2495,13 @@ const StmtRef & SQLiteStatement::GetValid() const
// ------------------------------------------------------------------------------------------------ // ------------------------------------------------------------------------------------------------
#if defined(_DEBUG) || defined(SQMOD_EXCEPTLOC) #if defined(_DEBUG) || defined(SQMOD_EXCEPTLOC)
const StmtRef & SQLiteStatement::GetCreated(const char * file, int32_t line) const const SQLiteStmtRef & SQLiteStatement::GetCreated(const char * file, int32_t line) const
{ {
ValidateCreated(file, line); ValidateCreated(file, line);
return m_Handle; return m_Handle;
} }
#else #else
const StmtRef & SQLiteStatement::GetCreated() const const SQLiteStmtRef & SQLiteStatement::GetCreated() const
{ {
ValidateCreated(); ValidateCreated();
return m_Handle; return m_Handle;
@ -2824,7 +2850,7 @@ SQLiteTransaction::SQLiteTransaction(const SQLiteConnection & db)
} }
// ------------------------------------------------------------------------------------------------ // ------------------------------------------------------------------------------------------------
SQLiteTransaction::SQLiteTransaction(ConnRef db) SQLiteTransaction::SQLiteTransaction(SQLiteConnRef db)
: m_Handle(std::move(db)), m_Committed(false) : m_Handle(std::move(db)), m_Committed(false)
{ {
// Was the specified database connection valid? // Was the specified database connection valid?

View File

@ -10,6 +10,10 @@
#include "Library/Chrono/Time.hpp" #include "Library/Chrono/Time.hpp"
#include "Library/Chrono/Timestamp.hpp" #include "Library/Chrono/Timestamp.hpp"
// ------------------------------------------------------------------------------------------------
#include "Poco/AutoPtr.h"
#include "Poco/Data/SessionImpl.h"
// ------------------------------------------------------------------------------------------------ // ------------------------------------------------------------------------------------------------
#include <utility> #include <utility>
#include <vector> #include <vector>
@ -78,18 +82,18 @@ struct SQLiteStmtHnd;
/* ------------------------------------------------------------------------------------------------ /* ------------------------------------------------------------------------------------------------
* Common typedefs. * Common typedefs.
*/ */
typedef SharedPtr< SQLiteConnHnd > ConnRef; typedef SharedPtr< SQLiteConnHnd > SQLiteConnRef;
typedef SharedPtr< SQLiteStmtHnd > StmtRef; typedef SharedPtr< SQLiteStmtHnd > SQLiteStmtRef;
/* ------------------------------------------------------------------------------------------------ /* ------------------------------------------------------------------------------------------------
* Obtain a script object from a connection handle. (meant to avoid having to include the header) * Obtain a script object from a connection handle. (meant to avoid having to include the header)
*/ */
Object GetConnectionObj(const ConnRef & conn); Object GetConnectionObj(const SQLiteConnRef & conn);
/* ------------------------------------------------------------------------------------------------ /* ------------------------------------------------------------------------------------------------
* Obtain a script object from a statement handle. (meant to avoid having to include the header) * Obtain a script object from a statement handle. (meant to avoid having to include the header)
*/ */
Object GetStatementObj(const StmtRef & stmt); Object GetStatementObj(const SQLiteStmtRef & stmt);
/* ------------------------------------------------------------------------------------------------ /* ------------------------------------------------------------------------------------------------
* Tests if a certain query string is empty. * Tests if a certain query string is empty.
@ -178,6 +182,9 @@ public:
String mName; // The specified name to be used as the database file. String mName; // The specified name to be used as the database file.
String mVFS; // The specified virtual file system. String mVFS; // The specified virtual file system.
// --------------------------------------------------------------------------------------------
Poco::AutoPtr< Poco::Data::SessionImpl > mSession; // POCO session when this connection comes from a pool.
// -------------------------------------------------------------------------------------------- // --------------------------------------------------------------------------------------------
bool mMemory; // Whether the database exists in memory and not disk. bool mMemory; // Whether the database exists in memory and not disk.
bool mTrace; // Whether tracing was activated on the database. bool mTrace; // Whether tracing was activated on the database.
@ -188,6 +195,11 @@ public:
*/ */
SQLiteConnHnd(); SQLiteConnHnd();
/* --------------------------------------------------------------------------------------------
* Explicit constructor.
*/
explicit SQLiteConnHnd(Poco::Data::SessionImpl * session);
/* -------------------------------------------------------------------------------------------- /* --------------------------------------------------------------------------------------------
* Copy constructor. (disabled) * Copy constructor. (disabled)
*/ */
@ -286,7 +298,7 @@ public:
int32_t mStatus; // The last status code of this connection handle. int32_t mStatus; // The last status code of this connection handle.
// -------------------------------------------------------------------------------------------- // --------------------------------------------------------------------------------------------
ConnRef mConn; // The handle to the associated database connection. SQLiteConnRef mConn; // The handle to the associated database connection.
// -------------------------------------------------------------------------------------------- // --------------------------------------------------------------------------------------------
String mQuery; // The query string used to create this statement. String mQuery; // The query string used to create this statement.
@ -303,7 +315,7 @@ public:
/* -------------------------------------------------------------------------------------------- /* --------------------------------------------------------------------------------------------
* Default constructor. * Default constructor.
*/ */
explicit SQLiteStmtHnd(ConnRef conn); explicit SQLiteStmtHnd(SQLiteConnRef conn);
/* -------------------------------------------------------------------------------------------- /* --------------------------------------------------------------------------------------------
* Copy constructor. (disabled) * Copy constructor. (disabled)
@ -385,7 +397,7 @@ class SQLiteConnection
private: private:
// -------------------------------------------------------------------------------------------- // --------------------------------------------------------------------------------------------
ConnRef m_Handle; // Reference to the managed connection. SQLiteConnRef m_Handle; // Reference to the managed connection.
protected: protected:
@ -421,18 +433,18 @@ protected:
* Validate the managed connection handle and throw an error if invalid. * Validate the managed connection handle and throw an error if invalid.
*/ */
#if defined(_DEBUG) || defined(SQMOD_EXCEPTLOC) #if defined(_DEBUG) || defined(SQMOD_EXCEPTLOC)
SQMOD_NODISCARD const ConnRef & GetValid(const char * file, int32_t line) const; SQMOD_NODISCARD const SQLiteConnRef & GetValid(const char * file, int32_t line) const;
#else #else
SQMOD_NODISCARD const ConnRef & GetValid() const; SQMOD_NODISCARD const SQLiteConnRef & GetValid() const;
#endif // _DEBUG #endif // _DEBUG
/* -------------------------------------------------------------------------------------------- /* --------------------------------------------------------------------------------------------
* Validate the managed connection handle and throw an error if invalid. * Validate the managed connection handle and throw an error if invalid.
*/ */
#if defined(_DEBUG) || defined(SQMOD_EXCEPTLOC) #if defined(_DEBUG) || defined(SQMOD_EXCEPTLOC)
SQMOD_NODISCARD const ConnRef & GetCreated(const char * file, int32_t line) const; SQMOD_NODISCARD const SQLiteConnRef & GetCreated(const char * file, int32_t line) const;
#else #else
SQMOD_NODISCARD const ConnRef & GetCreated() const; SQMOD_NODISCARD const SQLiteConnRef & GetCreated() const;
#endif // _DEBUG #endif // _DEBUG
public: public:
@ -479,7 +491,7 @@ public:
/* -------------------------------------------------------------------------------------------- /* --------------------------------------------------------------------------------------------
* Direct handle constructor. * Direct handle constructor.
*/ */
explicit SQLiteConnection(ConnRef c) explicit SQLiteConnection(SQLiteConnRef c)
: m_Handle(std::move(c)) : m_Handle(std::move(c))
{ {
/* ... */ /* ... */
@ -548,7 +560,7 @@ public:
/* -------------------------------------------------------------------------------------------- /* --------------------------------------------------------------------------------------------
* Retrieve the associated connection handle. * Retrieve the associated connection handle.
*/ */
SQMOD_NODISCARD const ConnRef & GetHandle() const SQMOD_NODISCARD const SQLiteConnRef & GetHandle() const
{ {
return m_Handle; return m_Handle;
} }
@ -858,7 +870,7 @@ private:
// -------------------------------------------------------------------------------------------- // --------------------------------------------------------------------------------------------
int32_t m_Index{0}; // The index of the managed parameter. int32_t m_Index{0}; // The index of the managed parameter.
StmtRef m_Handle{}; // Reference to the managed statement. SQLiteStmtRef m_Handle{}; // Reference to the managed statement.
protected: protected:
@ -884,18 +896,18 @@ protected:
* Validate the managed statement handle and throw an error if invalid. * Validate the managed statement handle and throw an error if invalid.
*/ */
#if defined(_DEBUG) || defined(SQMOD_EXCEPTLOC) #if defined(_DEBUG) || defined(SQMOD_EXCEPTLOC)
SQMOD_NODISCARD const StmtRef & GetValid(const char * file, int32_t line) const; SQMOD_NODISCARD const SQLiteStmtRef & GetValid(const char * file, int32_t line) const;
#else #else
SQMOD_NODISCARD const StmtRef & GetValid() const; SQMOD_NODISCARD const SQLiteStmtRef & GetValid() const;
#endif // _DEBUG #endif // _DEBUG
/* -------------------------------------------------------------------------------------------- /* --------------------------------------------------------------------------------------------
* Validate the managed statement handle and throw an error if invalid. * Validate the managed statement handle and throw an error if invalid.
*/ */
#if defined(_DEBUG) || defined(SQMOD_EXCEPTLOC) #if defined(_DEBUG) || defined(SQMOD_EXCEPTLOC)
SQMOD_NODISCARD const StmtRef & GetCreated(const char * file, int32_t line) const; SQMOD_NODISCARD const SQLiteStmtRef & GetCreated(const char * file, int32_t line) const;
#else #else
SQMOD_NODISCARD const StmtRef & GetCreated() const; SQMOD_NODISCARD const SQLiteStmtRef & GetCreated() const;
#endif // _DEBUG #endif // _DEBUG
/* -------------------------------------------------------------------------------------------- /* --------------------------------------------------------------------------------------------
@ -943,7 +955,7 @@ public:
/* -------------------------------------------------------------------------------------------- /* --------------------------------------------------------------------------------------------
* No parameter constructor. * No parameter constructor.
*/ */
explicit SQLiteParameter(StmtRef stmt) explicit SQLiteParameter(SQLiteStmtRef stmt)
: m_Index(0), m_Handle(std::move(stmt)) : m_Index(0), m_Handle(std::move(stmt))
{ {
/* ... */ /* ... */
@ -952,7 +964,7 @@ public:
/* -------------------------------------------------------------------------------------------- /* --------------------------------------------------------------------------------------------
* Index constructor. * Index constructor.
*/ */
SQLiteParameter(StmtRef stmt, int32_t idx) SQLiteParameter(SQLiteStmtRef stmt, int32_t idx)
: m_Index(idx), m_Handle(std::move(stmt)) : m_Index(idx), m_Handle(std::move(stmt))
{ {
SQMOD_VALIDATE_PARAM(*this, m_Index); SQMOD_VALIDATE_PARAM(*this, m_Index);
@ -961,7 +973,7 @@ public:
/* -------------------------------------------------------------------------------------------- /* --------------------------------------------------------------------------------------------
* Name constructor. * Name constructor.
*/ */
SQLiteParameter(const StmtRef & stmt, const SQChar * name) SQLiteParameter(const SQLiteStmtRef & stmt, const SQChar * name)
: m_Index(stmt ? sqlite3_bind_parameter_index(stmt->mPtr, name) : 0), m_Handle(stmt) : m_Index(stmt ? sqlite3_bind_parameter_index(stmt->mPtr, name) : 0), m_Handle(stmt)
{ {
SQMOD_VALIDATE_PARAM(*this, m_Index); SQMOD_VALIDATE_PARAM(*this, m_Index);
@ -970,7 +982,7 @@ public:
/* -------------------------------------------------------------------------------------------- /* --------------------------------------------------------------------------------------------
* Dynamic constructor. * Dynamic constructor.
*/ */
SQLiteParameter(StmtRef stmt, const Object & param) SQLiteParameter(SQLiteStmtRef stmt, const Object & param)
: m_Index(0), m_Handle(std::move(stmt)) : m_Index(0), m_Handle(std::move(stmt))
{ {
if (!m_Handle) if (!m_Handle)
@ -1252,7 +1264,7 @@ private:
// -------------------------------------------------------------------------------------------- // --------------------------------------------------------------------------------------------
int32_t m_Index{-1}; // The index of the managed column. int32_t m_Index{-1}; // The index of the managed column.
StmtRef m_Handle{}; // The statement where the column exist. SQLiteStmtRef m_Handle{}; // The statement where the column exist.
protected: protected:
@ -1278,18 +1290,18 @@ protected:
* Validate the managed statement handle and throw an error if invalid. * Validate the managed statement handle and throw an error if invalid.
*/ */
#if defined(_DEBUG) || defined(SQMOD_EXCEPTLOC) #if defined(_DEBUG) || defined(SQMOD_EXCEPTLOC)
SQMOD_NODISCARD const StmtRef & GetValid(const char * file, int32_t line) const; SQMOD_NODISCARD const SQLiteStmtRef & GetValid(const char * file, int32_t line) const;
#else #else
SQMOD_NODISCARD const StmtRef & GetValid() const; SQMOD_NODISCARD const SQLiteStmtRef & GetValid() const;
#endif // _DEBUG #endif // _DEBUG
/* -------------------------------------------------------------------------------------------- /* --------------------------------------------------------------------------------------------
* Validate the managed statement handle and throw an error if invalid. * Validate the managed statement handle and throw an error if invalid.
*/ */
#if defined(_DEBUG) || defined(SQMOD_EXCEPTLOC) #if defined(_DEBUG) || defined(SQMOD_EXCEPTLOC)
SQMOD_NODISCARD const StmtRef & GetCreated(const char * file, int32_t line) const; SQMOD_NODISCARD const SQLiteStmtRef & GetCreated(const char * file, int32_t line) const;
#else #else
SQMOD_NODISCARD const StmtRef & GetCreated() const; SQMOD_NODISCARD const SQLiteStmtRef & GetCreated() const;
#endif // _DEBUG #endif // _DEBUG
/* -------------------------------------------------------------------------------------------- /* --------------------------------------------------------------------------------------------
@ -1346,7 +1358,7 @@ public:
/* -------------------------------------------------------------------------------------------- /* --------------------------------------------------------------------------------------------
* No column constructor. * No column constructor.
*/ */
explicit SQLiteColumn(StmtRef stmt) explicit SQLiteColumn(SQLiteStmtRef stmt)
: m_Index(-1), m_Handle(std::move(stmt)) : m_Index(-1), m_Handle(std::move(stmt))
{ {
/* ... */ /* ... */
@ -1355,7 +1367,7 @@ public:
/* -------------------------------------------------------------------------------------------- /* --------------------------------------------------------------------------------------------
* Index constructor. * Index constructor.
*/ */
SQLiteColumn(StmtRef stmt, int32_t idx) SQLiteColumn(SQLiteStmtRef stmt, int32_t idx)
: m_Index(idx), m_Handle(std::move(stmt)) : m_Index(idx), m_Handle(std::move(stmt))
{ {
SQMOD_VALIDATE_COLUMN(*this, m_Index); SQMOD_VALIDATE_COLUMN(*this, m_Index);
@ -1364,7 +1376,7 @@ public:
/* -------------------------------------------------------------------------------------------- /* --------------------------------------------------------------------------------------------
* Name constructor. * Name constructor.
*/ */
SQLiteColumn(const StmtRef & stmt, const SQChar * name) SQLiteColumn(const SQLiteStmtRef & stmt, const SQChar * name)
: m_Index(stmt ? stmt->GetColumnIndex(name) : -1), m_Handle(stmt) : m_Index(stmt ? stmt->GetColumnIndex(name) : -1), m_Handle(stmt)
{ {
SQMOD_VALIDATE_COLUMN(*this, m_Index); SQMOD_VALIDATE_COLUMN(*this, m_Index);
@ -1373,7 +1385,7 @@ public:
/* -------------------------------------------------------------------------------------------- /* --------------------------------------------------------------------------------------------
* Dynamic constructor. * Dynamic constructor.
*/ */
SQLiteColumn(StmtRef stmt, const Object & column) SQLiteColumn(SQLiteStmtRef stmt, const Object & column)
: m_Index(-1), m_Handle(std::move(stmt)) : m_Index(-1), m_Handle(std::move(stmt))
{ {
if (!m_Handle) if (!m_Handle)
@ -1573,7 +1585,7 @@ class SQLiteStatement
private: private:
// -------------------------------------------------------------------------------------------- // --------------------------------------------------------------------------------------------
StmtRef m_Handle; // Reference to the managed statement. SQLiteStmtRef m_Handle; // Reference to the managed statement.
protected: protected:
@ -1599,18 +1611,18 @@ protected:
* Validate the managed statement handle and throw an error if invalid. * Validate the managed statement handle and throw an error if invalid.
*/ */
#if defined(_DEBUG) || defined(SQMOD_EXCEPTLOC) #if defined(_DEBUG) || defined(SQMOD_EXCEPTLOC)
SQMOD_NODISCARD const StmtRef & GetValid(const char * file, int32_t line) const; SQMOD_NODISCARD const SQLiteStmtRef & GetValid(const char * file, int32_t line) const;
#else #else
SQMOD_NODISCARD const StmtRef & GetValid() const; SQMOD_NODISCARD const SQLiteStmtRef & GetValid() const;
#endif // _DEBUG #endif // _DEBUG
/* -------------------------------------------------------------------------------------------- /* --------------------------------------------------------------------------------------------
* Validate the managed statement handle and throw an error if invalid. * Validate the managed statement handle and throw an error if invalid.
*/ */
#if defined(_DEBUG) || defined(SQMOD_EXCEPTLOC) #if defined(_DEBUG) || defined(SQMOD_EXCEPTLOC)
SQMOD_NODISCARD const StmtRef & GetCreated(const char * file, int32_t line) const; SQMOD_NODISCARD const SQLiteStmtRef & GetCreated(const char * file, int32_t line) const;
#else #else
SQMOD_NODISCARD const StmtRef & GetCreated() const; SQMOD_NODISCARD const SQLiteStmtRef & GetCreated() const;
#endif // _DEBUG #endif // _DEBUG
/* -------------------------------------------------------------------------------------------- /* --------------------------------------------------------------------------------------------
@ -1654,7 +1666,7 @@ public:
/* -------------------------------------------------------------------------------------------- /* --------------------------------------------------------------------------------------------
* Construct a statement under the specified connection using the specified string. * Construct a statement under the specified connection using the specified string.
*/ */
SQLiteStatement(const ConnRef & connection, StackStrF & query) SQLiteStatement(const SQLiteConnRef & connection, StackStrF & query)
: m_Handle(new SQLiteStmtHnd(connection)) : m_Handle(new SQLiteStmtHnd(connection))
{ {
SQMOD_GET_VALID(*this)->Create(query.mPtr, query.mLen); SQMOD_GET_VALID(*this)->Create(query.mPtr, query.mLen);
@ -1668,7 +1680,7 @@ public:
/* -------------------------------------------------------------------------------------------- /* --------------------------------------------------------------------------------------------
* Direct handle constructor. * Direct handle constructor.
*/ */
explicit SQLiteStatement(StmtRef s) explicit SQLiteStatement(SQLiteStmtRef s)
: m_Handle(std::move(s)) : m_Handle(std::move(s))
{ {
/* ... */ /* ... */
@ -1737,7 +1749,7 @@ public:
/* -------------------------------------------------------------------------------------------- /* --------------------------------------------------------------------------------------------
* Retrieve the associated statement handle. * Retrieve the associated statement handle.
*/ */
SQMOD_NODISCARD const StmtRef & GetHandle() const SQMOD_NODISCARD const SQLiteStmtRef & GetHandle() const
{ {
return m_Handle; return m_Handle;
} }
@ -2436,7 +2448,7 @@ public:
/* -------------------------------------------------------------------------------------------- /* --------------------------------------------------------------------------------------------
* Construct using the direct connection handle. * Construct using the direct connection handle.
*/ */
explicit SQLiteTransaction(ConnRef db); explicit SQLiteTransaction(SQLiteConnRef db);
/* -------------------------------------------------------------------------------------------- /* --------------------------------------------------------------------------------------------
* Copy constructor. (disabled) * Copy constructor. (disabled)
@ -2495,7 +2507,7 @@ public:
private: private:
// -------------------------------------------------------------------------------------------- // --------------------------------------------------------------------------------------------
ConnRef m_Handle{}; // The database connection handle where the transaction began. SQLiteConnRef m_Handle{}; // The database connection handle where the transaction began.
bool m_Committed{false}; // Whether changes were successfully committed to the database. bool m_Committed{false}; // Whether changes were successfully committed to the database.
}; };

View File

@ -1,5 +1,6 @@
// ------------------------------------------------------------------------------------------------ // ------------------------------------------------------------------------------------------------
#include "PocoLib/Data.hpp" #include "PocoLib/Data.hpp"
#include "Poco/Data/SessionImpl.h"
// ------------------------------------------------------------------------------------------------ // ------------------------------------------------------------------------------------------------
#include <sqratConst.h> #include <sqratConst.h>
@ -470,6 +471,32 @@ SqDataStatement & SqDataStatement::Into_(LightObj & obj, LightObj & def)
return *this; return *this;
} }
// ------------------------------------------------------------------------------------------------
extern LightObj GteSQLiteFromSession(Poco::Data::SessionImpl * session);
extern LightObj GteMySQLFromSession(Poco::Data::SessionImpl * session);
// ------------------------------------------------------------------------------------------------
LightObj SqDataSessionPool::GetSq()
{
auto session = get();
auto * session_impl = session.impl();
auto & connector = session_impl->connectorName();
// Is this a SQLite session?
if (connector == "sqlite")
{
return GteSQLiteFromSession(session_impl);
}
// Is this a MySQL session?
else if (connector == "mysql")
{
return GteMySQLFromSession(session_impl);
}
else
{
STHROWF("Unknown connector type {}", connector);
}
}
// ------------------------------------------------------------------------------------------------ // ------------------------------------------------------------------------------------------------
LightObj SqDataSessionPool::GetProperty(StackStrF & name) LightObj SqDataSessionPool::GetProperty(StackStrF & name)
{ {
@ -764,6 +791,7 @@ void Register_POCO_Data(HSQUIRRELVM vm, Table &)
.Prop(_SC("IsActive"), &SqDataSessionPool::IsActive) .Prop(_SC("IsActive"), &SqDataSessionPool::IsActive)
// Member Methods // Member Methods
.Func(_SC("Get"), &SqDataSessionPool::Get) .Func(_SC("Get"), &SqDataSessionPool::Get)
.Func(_SC("GetSq"), &SqDataSessionPool::GetSq)
.FmtFunc(_SC("GetWithProperty"), &SqDataSessionPool::GetWithProperty) .FmtFunc(_SC("GetWithProperty"), &SqDataSessionPool::GetWithProperty)
.FmtFunc(_SC("GetWithFeature"), &SqDataSessionPool::GetWithFeature) .FmtFunc(_SC("GetWithFeature"), &SqDataSessionPool::GetWithFeature)
.FmtFunc(_SC("SetFeature"), &SqDataSessionPool::SetFeature) .FmtFunc(_SC("SetFeature"), &SqDataSessionPool::SetFeature)

View File

@ -1707,7 +1707,7 @@ protected:
} }
SQ_UNREACHABLE SQ_UNREACHABLE
// Unreachable // Unreachable
return LightObj(); return {};
} }
}; };
@ -1746,7 +1746,7 @@ struct SqDataSessionPool : public SessionPool
/* -------------------------------------------------------------------------------------------- /* --------------------------------------------------------------------------------------------
* Destroys the SessionPool. * Destroys the SessionPool.
*/ */
~SqDataSessionPool() = default; ~SqDataSessionPool() override = default;
/* -------------------------------------------------------------------------------------------- /* --------------------------------------------------------------------------------------------
* Assignment operator (disabled). * Assignment operator (disabled).
@ -1766,6 +1766,11 @@ struct SqDataSessionPool : public SessionPool
return LightObj(SqTypeIdentity< SqDataSession >{}, SqVM(), get()); return LightObj(SqTypeIdentity< SqDataSession >{}, SqVM(), get());
} }
/* --------------------------------------------------------------------------------------------
* Retrieve a Session wrapped in a native/legacy implementation.
*/
LightObj GetSq();
/* -------------------------------------------------------------------------------------------- /* --------------------------------------------------------------------------------------------
* Retrieve a Session with requested property set. * Retrieve a Session with requested property set.
*/ */