1
0
mirror of https://github.com/VCMP-SqMod/SqMod.git synced 2024-11-14 03:37:16 +01:00
SqMod/modules/sqlite/Connection.hpp

525 lines
18 KiB
C++
Raw Normal View History

2016-02-27 10:57:29 +01:00
#ifndef _SQSQLITE_CONNECTION_HPP_
#define _SQSQLITE_CONNECTION_HPP_
// ------------------------------------------------------------------------------------------------
#include "Handle/Connection.hpp"
2016-02-27 10:57:29 +01:00
// ------------------------------------------------------------------------------------------------
namespace SqMod {
/* ------------------------------------------------------------------------------------------------
* Used to manage and interact with a database connection.
2016-02-27 10:57:29 +01:00
*/
class Connection
{
protected:
/* --------------------------------------------------------------------------------------------
* Create the database connection resource.
*/
void Create(CSStr name, Int32 flags, CSStr vfs);
private:
// --------------------------------------------------------------------------------------------
ConnRef m_Handle; // Reference to the managed connection.
protected:
/* --------------------------------------------------------------------------------------------
* Callback function for ActivateTracing()
*/
static void TraceOutput(void * ptr, CCStr sql);
/* --------------------------------------------------------------------------------------------
* Callback function for ActivateProfiling()
*/
static void ProfileOutput(void * ptr, CCStr sql, sqlite3_uint64 time);
/* --------------------------------------------------------------------------------------------
* Validate the managed connection handle and throw an error if invalid.
*/
#if defined(_DEBUG) || defined(SQMOD_EXCEPTLOC)
void Validate(CCStr file, Int32 line) const;
#else
void Validate() const;
#endif // _DEBUG
/* --------------------------------------------------------------------------------------------
* Validate the managed connection handle and throw an error if invalid.
*/
#if defined(_DEBUG) || defined(SQMOD_EXCEPTLOC)
void ValidateOpened(CCStr file, Int32 line) const;
#else
void ValidateOpened() const;
#endif // _DEBUG
/* --------------------------------------------------------------------------------------------
* Validate the managed connection handle and throw an error if invalid.
*/
#if defined(_DEBUG) || defined(SQMOD_EXCEPTLOC)
const ConnRef & GetValid(CCStr file, Int32 line) const;
#else
const ConnRef & GetValid() const;
#endif // _DEBUG
/* --------------------------------------------------------------------------------------------
* Validate the managed connection handle and throw an error if invalid.
*/
#if defined(_DEBUG) || defined(SQMOD_EXCEPTLOC)
const ConnRef & GetOpened(CCStr file, Int32 line) const;
#else
const ConnRef & GetOpened() const;
#endif // _DEBUG
2016-02-27 10:57:29 +01:00
public:
/* --------------------------------------------------------------------------------------------
* Attempt to open the specified database.
*/
Connection()
: m_Handle()
{
/* ... */
}
2016-02-27 10:57:29 +01:00
/* --------------------------------------------------------------------------------------------
* Explicit constructor.
*/
Connection(CSStr name)
: m_Handle(new ConnHnd())
{
GET_VALID_HND(*this)->Create(name, SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE, nullptr);
}
2016-02-27 10:57:29 +01:00
/* --------------------------------------------------------------------------------------------
* Explicit constructor.
*/
Connection(CSStr name, Int32 flags)
: m_Handle(new ConnHnd())
{
GET_VALID_HND(*this)->Create(name, flags, nullptr);
}
2016-02-27 10:57:29 +01:00
/* --------------------------------------------------------------------------------------------
* Explicit constructor.
*/
Connection(CSStr name, Int32 flags, CSStr vfs)
: m_Handle(new ConnHnd())
{
GET_VALID_HND(*this)->Create(name, flags, vfs);
}
2016-02-27 10:57:29 +01:00
/* --------------------------------------------------------------------------------------------
* Direct handle constructor.
*/
Connection(const ConnRef & c)
: m_Handle(c)
2016-02-27 10:57:29 +01:00
{
/* ... */
}
/* --------------------------------------------------------------------------------------------
* Copy constructor.
*/
Connection(const Connection & o) = default;
2016-02-27 10:57:29 +01:00
/* --------------------------------------------------------------------------------------------
* Move constructor.
2016-02-27 10:57:29 +01:00
*/
Connection(Connection && o) = default;
2016-02-27 10:57:29 +01:00
/* --------------------------------------------------------------------------------------------
* Copy assignment operator.
*/
Connection & operator = (const Connection & o) = default;
/* --------------------------------------------------------------------------------------------
* Move assignment operator.
*/
Connection & operator = (Connection && o) = default;
2016-02-27 10:57:29 +01:00
/* --------------------------------------------------------------------------------------------
* Perform an equality comparison between two connections.
*/
bool operator == (const Connection & o) const
{
return (m_Handle == o.m_Handle);
}
/* --------------------------------------------------------------------------------------------
* Perform an inequality comparison between two connections.
*/
bool operator != (const Connection & o) const
{
return (m_Handle != o.m_Handle);
}
/* --------------------------------------------------------------------------------------------
* Implicit conversion to the raw connection handle.
*/
operator sqlite3 * ()
{
return m_Handle ? m_Handle->mPtr : nullptr;
2016-02-27 10:57:29 +01:00
}
/* --------------------------------------------------------------------------------------------
* Implicit conversion to the raw connection handle.
*/
operator sqlite3 * () const
{
return m_Handle ? m_Handle->mPtr : nullptr;
2016-02-27 10:57:29 +01:00
}
/* --------------------------------------------------------------------------------------------
* Used by the script engine to compare two instances of this type.
*/
Int32 Cmp(const Connection & o) const
{
if (m_Handle.Get() == o.m_Handle.Get())
{
2016-02-27 10:57:29 +01:00
return 0;
}
else if (m_Handle.Get() > o.m_Handle.Get())
{
2016-02-27 10:57:29 +01:00
return 1;
}
2016-02-27 10:57:29 +01:00
else
{
2016-02-27 10:57:29 +01:00
return -1;
}
2016-02-27 10:57:29 +01:00
}
/* --------------------------------------------------------------------------------------------
* Used by the script engine to convert an instance of this type to a string.
*/
const String & ToString() const
2016-02-27 10:57:29 +01:00
{
return m_Handle ? m_Handle->mName : NullString();
2016-02-27 10:57:29 +01:00
}
/* --------------------------------------------------------------------------------------------
* Used by the script engine to retrieve the name from instances of this type.
*/
static SQInteger Typename(HSQUIRRELVM vm);
/* --------------------------------------------------------------------------------------------
* Retrieve the associated connection handle.
2016-02-27 10:57:29 +01:00
*/
const ConnRef & GetHandle() const
2016-02-27 10:57:29 +01:00
{
return m_Handle;
}
/* --------------------------------------------------------------------------------------------
* See whether the managed connection handle is valid.
2016-02-27 10:57:29 +01:00
*/
bool IsValid() const
2016-02-27 10:57:29 +01:00
{
return m_Handle;
}
/* --------------------------------------------------------------------------------------------
* See whether the managed connection handle was connected.
*/
bool IsConnected() const
{
return m_Handle && (m_Handle->mPtr != nullptr);
}
2016-02-27 10:57:29 +01:00
/* --------------------------------------------------------------------------------------------
* Return the number of active references to this connection handle.
*/
Uint32 GetRefCount() const
{
return m_Handle.Count();
}
/* --------------------------------------------------------------------------------------------
* Release the reference to the associated database connection.
*/
void Release()
{
m_Handle.Reset();
2016-02-27 10:57:29 +01:00
}
/* --------------------------------------------------------------------------------------------
* Retrieve the last received status code.
*/
Int32 GetStatus() const
{
return GET_VALID_HND(*this)->mStatus;
2016-02-27 10:57:29 +01:00
}
/* --------------------------------------------------------------------------------------------
* Retrieve the flags used to create this database connection.
*/
Int32 GetFlags() const
{
return GET_VALID_HND(*this)->mFlags;
2016-02-27 10:57:29 +01:00
}
/* --------------------------------------------------------------------------------------------
* Retrieve the name used to create this database connection.
*/
const String & GetName() const
2016-02-27 10:57:29 +01:00
{
return GET_VALID_HND(*this)->mName;
2016-02-27 10:57:29 +01:00
}
/* --------------------------------------------------------------------------------------------
* Retrieve the virtual file system used to create this database connection.
*/
const String GetVFS() const
2016-02-27 10:57:29 +01:00
{
return GET_VALID_HND(*this)->mVFS;
2016-02-27 10:57:29 +01:00
}
/* --------------------------------------------------------------------------------------------
* Return the numeric result code for the most recent failed API call (if any).
*/
Int32 GetErrorCode() const
{
return GET_VALID_HND(*this)->ErrNo();
2016-02-27 10:57:29 +01:00
}
/* --------------------------------------------------------------------------------------------
* Return the extended numeric result code for the most recent failed API call (if any).
*/
Int32 GetExtendedErrorCode() const
{
return GET_VALID_HND(*this)->ExErrNo();
2016-02-27 10:57:29 +01:00
}
/* --------------------------------------------------------------------------------------------
* Retrieve the message of the last received error code.
*/
CSStr GetErrStr() const
{
return GET_VALID_HND(*this)->ErrStr();
2016-02-27 10:57:29 +01:00
}
/* --------------------------------------------------------------------------------------------
* Return the last error message associated with this database connection.
*/
CSStr GetErrMsg() const
{
return GET_VALID_HND(*this)->ErrMsg();
2016-02-27 10:57:29 +01:00
}
/* --------------------------------------------------------------------------------------------
* Attempt to open the specified database.
*/
void Open(CSStr name);
2016-02-27 10:57:29 +01:00
/* --------------------------------------------------------------------------------------------
* Attempt to open the specified database.
*/
void Open(CSStr name, Int32 flags);
2016-02-27 10:57:29 +01:00
/* --------------------------------------------------------------------------------------------
* Attempt to open the specified database.
*/
void Open(CSStr name, Int32 flags, CSStr vfs);
2016-02-27 10:57:29 +01:00
/* --------------------------------------------------------------------------------------------
* Attempt to execute the specified query.
*/
Int32 Exec(CSStr str);
/* --------------------------------------------------------------------------------------------
* Attempt to queue the specified query.
*/
void Queue(CSStr str);
/* --------------------------------------------------------------------------------------------
* Attempt to create a statement from the specified query.
*/
Object Query(CSStr str) const;
2016-02-27 10:57:29 +01:00
/* --------------------------------------------------------------------------------------------
* See if the database connection was opened in read-only mode.
*/
bool IsReadOnly() const;
/* --------------------------------------------------------------------------------------------
* Shortcut to test if a table exists.
*/
bool TableExists(CCStr name) const;
/* --------------------------------------------------------------------------------------------
* See if the database connection is or is not in auto-commit mode.
*/
bool GetAutoCommit() const
{
return sqlite3_get_autocommit(GET_OPENED_HND(*this)->mPtr);
2016-02-27 10:57:29 +01:00
}
/* --------------------------------------------------------------------------------------------
* Get the row-id of the most recent successful INSERT into the database from the current connection.
*/
Object GetLastInsertRowID() const
{
return MakeSLongObj(sqlite3_last_insert_rowid(GET_OPENED_HND(*this)->mPtr));
}
2016-02-27 10:57:29 +01:00
/* --------------------------------------------------------------------------------------------
* Returns the number of database rows that were changed, inserted or deleted
* by the most recently completed SQL statement.
*/
Int32 GetChanges() const
{
return sqlite3_changes(GET_OPENED_HND(*this)->mPtr);
2016-02-27 10:57:29 +01:00
}
/* --------------------------------------------------------------------------------------------
* Returns the total number of row changes caused by INSERT, UPDATE or DELETE statements
* since the database connection was opened.
*/
Int32 GetTotalChanges() const
{
return sqlite3_total_changes(GET_OPENED_HND(*this)->mPtr);
2016-02-27 10:57:29 +01:00
}
/* --------------------------------------------------------------------------------------------
* See if this database connection has tracing enabled.
*/
bool GetTracing() const
{
return GET_VALID_HND(*this)->mTrace;
2016-02-27 10:57:29 +01:00
}
/* --------------------------------------------------------------------------------------------
* Activate or deactivate tracing on this database connection.
*/
void SetTracing(bool toggle);
2016-02-27 10:57:29 +01:00
/* --------------------------------------------------------------------------------------------
* See if this database connection has profiling enabled.
*/
bool GetProfiling() const
{
return GET_VALID_HND(*this)->mProfile;
2016-02-27 10:57:29 +01:00
}
/* --------------------------------------------------------------------------------------------
* Activate or deactivate profiling on this database connection.
*/
void SetProfiling(bool toggle);
2016-02-27 10:57:29 +01:00
/* --------------------------------------------------------------------------------------------
* Set a busy handler that sleeps for a specified amount of time when a table is locked.
*/
void SetBusyTimeout(Int32 millis);
/* --------------------------------------------------------------------------------------------
* Causes any pending database operation to abort and return at its earliest opportunity.
*/
void InterruptOperation() const
{
sqlite3_interrupt(GET_OPENED_HND(*this)->mPtr);
2016-02-27 10:57:29 +01:00
}
/* --------------------------------------------------------------------------------------------
* Attempts to free as much heap memory as possible from the database connection.
*/
void ReleaseMemory() const
{
sqlite3_db_release_memory(GET_OPENED_HND(*this)->mPtr);
2016-02-27 10:57:29 +01:00
}
/* --------------------------------------------------------------------------------------------
* Returns internal runtime status information associated with the current database connection.
*/
Int32 GetInfo(Int32 operation)
{
return GetInfo(operation, false, false);
}
/* --------------------------------------------------------------------------------------------
* Returns internal runtime status information associated with the current database connection.
*/
Int32 GetInfo(Int32 operation, bool highwater)
2016-02-27 10:57:29 +01:00
{
return GetInfo(operation, highwater, false);
}
/* --------------------------------------------------------------------------------------------
* Returns internal runtime status information associated with the current database connection.
*/
Int32 GetInfo(Int32 operation, bool highwater, bool reset);
2016-02-27 10:57:29 +01:00
/* --------------------------------------------------------------------------------------------
* Retrieve the number of queries in the queue.
*/
Uint32 QueueSize() const
{
return ConvTo< Uint32 >::From(GET_VALID_HND(*this)->mQueue.size());
2016-02-27 10:57:29 +01:00
}
/* --------------------------------------------------------------------------------------------
* Reserve space upfront for the specified amount of queries in the query queue.
*/
void ReserveQueue(Uint32 num);
/* --------------------------------------------------------------------------------------------
* Release memory that is not occupied from the query queue.
*/
void CompactQueue()
{
GET_VALID_HND(*this)->mQueue.shrink_to_fit();
}
2016-02-27 10:57:29 +01:00
/* --------------------------------------------------------------------------------------------
* Remove all queries from the queue without executing them.
*/
void ClearQueue()
2016-02-27 10:57:29 +01:00
{
GET_VALID_HND(*this)->mQueue.clear();
2016-02-27 10:57:29 +01:00
}
/* --------------------------------------------------------------------------------------------
* Remove the last query from the queue.
*/
void PopQueue();
2016-02-27 10:57:29 +01:00
/* --------------------------------------------------------------------------------------------
* Flush all queries from the queue.
*/
Int32 Flush();
2016-02-27 10:57:29 +01:00
/* --------------------------------------------------------------------------------------------
* Flush a specific amount of queries from the queue.
*/
Int32 Flush(SQInteger num);
2016-02-27 10:57:29 +01:00
/* --------------------------------------------------------------------------------------------
* Flush all queries from the queue and handle errors manually.
*/
Int32 Flush(Object & env, Function & func);
2016-02-27 10:57:29 +01:00
/* --------------------------------------------------------------------------------------------
* Flush a specific amount of queries from the queue and handle errors manually.
2016-02-27 10:57:29 +01:00
*/
Int32 Flush(SQInteger num, Object & env, Function & func);
2016-02-27 10:57:29 +01:00
/* --------------------------------------------------------------------------------------------
* Attempt to execute the specified query.
*/
static SQInteger ExecF(HSQUIRRELVM vm);
/* --------------------------------------------------------------------------------------------
* Attempt to queue the specified query.
*/
static SQInteger QueueF(HSQUIRRELVM vm);
/* --------------------------------------------------------------------------------------------
* Attempt to create a statement from the specified query.
*/
static SQInteger QueryF(HSQUIRRELVM vm);
};
} // Namespace:: SqMod
#endif // _SQSQLITE_CONNECTION_HPP_