1
0
mirror of https://github.com/VCMP-SqMod/SqMod.git synced 2024-11-08 16:57:16 +01:00
SqMod/modules/sqlite/Statement.hpp

564 lines
21 KiB
C++
Raw Normal View History

2016-02-27 10:57:29 +01:00
#ifndef _SQSQLITE_STATEMENT_HPP
#define _SQSQLITE_STATEMENT_HPP
// ------------------------------------------------------------------------------------------------
#include "Handle/Statement.hpp"
2016-02-27 10:57:29 +01:00
// ------------------------------------------------------------------------------------------------
namespace SqMod {
/* ------------------------------------------------------------------------------------------------
* Used to manage and interact a database statement.
2016-02-27 10:57:29 +01:00
*/
class Statement
{
private:
// --------------------------------------------------------------------------------------------
StmtRef m_Handle; // Reference to the managed statement.
2016-02-27 10:57:29 +01:00
protected:
/* --------------------------------------------------------------------------------------------
* Validate the managed statement handle and throw an error if invalid.
2016-02-27 10:57:29 +01:00
*/
#if defined(_DEBUG) || defined(SQMOD_EXCEPTLOC)
void Validate(CCStr file, Int32 line) const;
#else
void Validate() const;
#endif // _DEBUG
/* --------------------------------------------------------------------------------------------
* Validate the managed statement handle and throw an error if invalid.
*/
#if defined(_DEBUG) || defined(SQMOD_EXCEPTLOC)
void ValidateCreated(CCStr file, Int32 line) const;
#else
void ValidateCreated() const;
#endif // _DEBUG
/* --------------------------------------------------------------------------------------------
* Validate the managed statement handle and throw an error if invalid.
*/
#if defined(_DEBUG) || defined(SQMOD_EXCEPTLOC)
const StmtRef & GetValid(CCStr file, Int32 line) const;
#else
const StmtRef & GetValid() const;
#endif // _DEBUG
/* --------------------------------------------------------------------------------------------
* Validate the managed statement handle and throw an error if invalid.
*/
#if defined(_DEBUG) || defined(SQMOD_EXCEPTLOC)
const StmtRef & GetCreated(CCStr file, Int32 line) const;
#else
const StmtRef & GetCreated() const;
#endif // _DEBUG
/* --------------------------------------------------------------------------------------------
* Validate the statement reference and column index, and throw an error if they're invalid.
*/
#if defined(_DEBUG) || defined(SQMOD_EXCEPTLOC)
void ValidateColumn(Int32 idx, CCStr file, Int32 line) const;
#else
void ValidateColumn(Int32 idx) const;
#endif // _DEBUG
/* --------------------------------------------------------------------------------------------
* Validate the statement reference and parameter index, and throw an error if they're invalid.
*/
#if defined(_DEBUG) || defined(SQMOD_EXCEPTLOC)
void ValidateParameter(Int32 idx, CCStr file, Int32 line) const;
#else
void ValidateParameter(Int32 idx) const;
#endif // _DEBUG
2016-02-27 10:57:29 +01:00
/* --------------------------------------------------------------------------------------------
* Validate the statement reference and row, and throw an error if they're invalid.
*/
#if defined(_DEBUG) || defined(SQMOD_EXCEPTLOC)
void ValidateRow(CCStr file, Int32 line) const;
#else
void ValidateRow() const;
#endif // _DEBUG
public:
2016-02-27 10:57:29 +01:00
public:
/* --------------------------------------------------------------------------------------------
* Default constructor.
*/
Statement()
: m_Handle()
{
/* ... */
}
2016-02-27 10:57:29 +01:00
/* --------------------------------------------------------------------------------------------
* Construct a statement under the specified connection using the specified string.
*/
Statement(const ConnRef & connection, CSStr query)
: m_Handle(new StmtHnd(connection))
{
GET_VALID_HND(*this)->Create(query);
}
2016-02-27 10:57:29 +01:00
/* --------------------------------------------------------------------------------------------
* Construct a statement under the specified connection using the specified string.
*/
Statement(const Connection & connection, CSStr query);
/* --------------------------------------------------------------------------------------------
* Direct handle constructor.
*/
Statement(const StmtRef & s)
: m_Handle(s)
2016-02-27 10:57:29 +01:00
{
/* ... */
}
/* --------------------------------------------------------------------------------------------
* Copy constructor.
*/
Statement(const Statement & o) = default;
2016-02-27 10:57:29 +01:00
/* --------------------------------------------------------------------------------------------
* Move constructor.
2016-02-27 10:57:29 +01:00
*/
Statement(Statement && o) = default;
2016-02-27 10:57:29 +01:00
/* --------------------------------------------------------------------------------------------
* Copy assignment operator.
*/
Statement & operator = (const Statement & o) = default;
/* --------------------------------------------------------------------------------------------
* Move assignment operator.
*/
Statement & operator = (Statement && o) = default;
2016-02-27 10:57:29 +01:00
/* --------------------------------------------------------------------------------------------
* Perform an equality comparison between two connections.
*/
bool operator == (const Statement & o) const
{
return (m_Handle.Get() == o.m_Handle.Get());
2016-02-27 10:57:29 +01:00
}
/* --------------------------------------------------------------------------------------------
* Perform an inequality comparison between two connections.
*/
bool operator != (const Statement & o) const
{
return (m_Handle.Get() != o.m_Handle.Get());
2016-02-27 10:57:29 +01:00
}
/* --------------------------------------------------------------------------------------------
* Implicit conversion to the raw connection handle.
*/
operator sqlite3_stmt * ()
{
return m_Handle ? m_Handle->mPtr : nullptr;
2016-02-27 10:57:29 +01:00
}
/* --------------------------------------------------------------------------------------------
* Implicit conversion to the raw connection handle.
*/
operator sqlite3_stmt * () 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 Statement & 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->mQuery : 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 statement handle.
2016-02-27 10:57:29 +01:00
*/
const StmtRef & GetHandle() const
2016-02-27 10:57:29 +01:00
{
return m_Handle;
2016-02-27 10:57:29 +01:00
}
/* --------------------------------------------------------------------------------------------
* See whether the managed 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 statement is valid.
*/
bool IsPrepared() const
{
return m_Handle && (m_Handle->mPtr != nullptr);
}
2016-02-27 10:57:29 +01:00
/* --------------------------------------------------------------------------------------------
* Return the number of active references to this statement handle.
*/
Uint32 GetReferences() const
2016-02-27 10:57:29 +01:00
{
return m_Handle.Count();
}
/* --------------------------------------------------------------------------------------------
* Retrieve the associated database connection.
*/
Object GetConnection() const;
2016-02-27 10:57:29 +01:00
/* --------------------------------------------------------------------------------------------
* Release the reference to the associated database statement.
*/
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
}
/* --------------------------------------------------------------------------------------------
* 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
}
/* --------------------------------------------------------------------------------------------
* Retrieve the amount of requested columns.
*/
Int32 GetColumns() const
{
return GET_VALID_HND(*this)->mColumns;
}
/* --------------------------------------------------------------------------------------------
* Retrieve the amount of specified parameters.
*/
Int32 GetParameters() const
{
return GET_VALID_HND(*this)->mParameters;
2016-02-27 10:57:29 +01:00
}
/* --------------------------------------------------------------------------------------------
* Retrieve the query string used to create this statement.
*/
const String & GetQuery() const
2016-02-27 10:57:29 +01:00
{
return GET_VALID_HND(*this)->mQuery;
2016-02-27 10:57:29 +01:00
}
/* --------------------------------------------------------------------------------------------
* See if the last step retrieved a valid row.
*/
bool GetGood() const
{
return GET_CREATED_HND(*this)->mGood;
2016-02-27 10:57:29 +01:00
}
/* --------------------------------------------------------------------------------------------
* See if there are any steps left.
*/
bool GetDone() const
{
return GET_CREATED_HND(*this)->mDone;
2016-02-27 10:57:29 +01:00
}
/* --------------------------------------------------------------------------------------------
* Reset the statement back to its initial position to be stepped again.
*/
void Reset();
/* --------------------------------------------------------------------------------------------
* Clear any values binded to this statement.
*/
void Clear();
/* --------------------------------------------------------------------------------------------
* Execute this statement and don't expect any rows to be returned.
*/
Int32 Exec();
/* --------------------------------------------------------------------------------------------
* Step the statement and expect a row to be returned.
*/
bool Step();
/* --------------------------------------------------------------------------------------------
* Attempt to bind the values from an array starting at the specified index.
*/
Statement & IndexBindArray(Int32 idx, const Array & arr);
2016-02-27 10:57:29 +01:00
/* --------------------------------------------------------------------------------------------
* Attempt to bind the a numeric value at the the specified parameter index.
2016-02-27 10:57:29 +01:00
*/
Statement & IndexBindValue(Int32 idx, Int32 value);
2016-02-27 10:57:29 +01:00
/* --------------------------------------------------------------------------------------------
* Attempt to bind the a long integer value at the the specified parameter index.
*/
Statement & IndexBindLong(Int32 idx, const Object & value);
2016-02-27 10:57:29 +01:00
/* --------------------------------------------------------------------------------------------
* Attempt to bind the a native integer value at the the specified parameter index.
*/
Statement & IndexBindInteger(Int32 idx, SQInteger value);
2016-02-27 10:57:29 +01:00
/* --------------------------------------------------------------------------------------------
* Attempt to bind the a floating point value at the the specified parameter index.
*/
Statement & IndexBindFloat(Int32 idx, SQFloat value);
2016-02-27 10:57:29 +01:00
/* --------------------------------------------------------------------------------------------
* Attempt to bind the a string value at the the specified parameter index.
*/
Statement & IndexBindString(Int32 idx, CSStr value);
2016-02-27 10:57:29 +01:00
/* --------------------------------------------------------------------------------------------
* Attempt to bind the a boolean value at the the specified parameter index.
*/
Statement & IndexBindBool(Int32 idx, bool value);
2016-02-27 10:57:29 +01:00
/* --------------------------------------------------------------------------------------------
* Attempt to bind the a null value at the the specified parameter index.
*/
Statement & IndexBindNull(Int32 idx);
2016-02-27 10:57:29 +01:00
/* --------------------------------------------------------------------------------------------
* Attempt to bind the values from an associative container.
*/
Statement & NameBindTable(const Table & tbl);
2016-02-27 10:57:29 +01:00
/* --------------------------------------------------------------------------------------------
* Attempt to bind the a integer value at the specified parameter name.
*/
Statement & NameBindValue(CSStr name, Int32 value);
2016-02-27 10:57:29 +01:00
/* --------------------------------------------------------------------------------------------
* Attempt to bind the a long integer value at the specified parameter name.
*/
Statement & NameBindLong(CSStr name, const Object & value);
2016-02-27 10:57:29 +01:00
/* --------------------------------------------------------------------------------------------
* Attempt to bind the a native integer value at the specified parameter name.
*/
Statement & NameBindInteger(CSStr name, SQInteger value);
2016-02-27 10:57:29 +01:00
/* --------------------------------------------------------------------------------------------
* Attempt to bind the a floating point value at the specified parameter name.
*/
Statement & NameBindFloat(CSStr name, SQFloat value);
2016-02-27 10:57:29 +01:00
/* --------------------------------------------------------------------------------------------
* Attempt to bind the a string value at the specified parameter name.
*/
Statement & NameBindString(CSStr name, CSStr value);
2016-02-27 10:57:29 +01:00
/* --------------------------------------------------------------------------------------------
* Attempt to bind the a boolean value at the specified parameter name.
*/
Statement & NameBindBool(CSStr name, bool value);
2016-02-27 10:57:29 +01:00
/* --------------------------------------------------------------------------------------------
* Attempt to bind the a null value at the specified parameter name.
*/
Statement & NameBindNull(CSStr name);
2016-02-27 10:57:29 +01:00
/* --------------------------------------------------------------------------------------------
* Attempt to bind the specified value at the specified parameter index.
*/
Statement & IndexBind(Int32 idx, const Object & value);
2016-02-27 10:57:29 +01:00
/* --------------------------------------------------------------------------------------------
* Attempt to bind the specified value at the specified parameter name.
*/
Statement & NameBind(CSStr name, const Object & value);
/* --------------------------------------------------------------------------------------------
* Attempt to bind the specified value at the specified parameter.
*/
Statement & Bind(const Object & param, const Object & value);
2016-02-27 10:57:29 +01:00
/* --------------------------------------------------------------------------------------------
* Fetch the value at the specific column index.
2016-02-27 10:57:29 +01:00
*/
Object FetchColumnIndex(Int32 idx) const;
/* --------------------------------------------------------------------------------------------
* Fetch the value at the specific column name.
2016-02-27 10:57:29 +01:00
*/
Object FetchColumnName(CSStr name) const;
/* --------------------------------------------------------------------------------------------
* Fetch the value at the specific column.
*/
Object FetchColumn(const Object & column) const;
2016-02-27 10:57:29 +01:00
/* --------------------------------------------------------------------------------------------
* Fetch the row as an array container.
*/
Array FetchArray() const;
/* --------------------------------------------------------------------------------------------
* Fetch the row as an array container.
*/
Array FetchArray(Int32 min) const;
/* --------------------------------------------------------------------------------------------
* Fetch the row as an array container.
*/
Array FetchArray(Int32 min, Int32 max) const;
/* --------------------------------------------------------------------------------------------
* Fetch the row as an associative container.
*/
Table FetchTable() const;
/* --------------------------------------------------------------------------------------------
* Fetch the row as an associative container.
*/
Table FetchTable(Int32 min) const;
/* --------------------------------------------------------------------------------------------
* Fetch the row as an associative container.
*/
Table FetchTable(Int32 min, Int32 max) const;
/* --------------------------------------------------------------------------------------------
* Check whether a specific column index is in range.
*/
bool CheckColumn(Int32 idx) const;
/* --------------------------------------------------------------------------------------------
* Check whether a specific parameter index is in range.
2016-02-27 10:57:29 +01:00
*/
bool CheckParameter(Int32 idx) const;
2016-02-27 10:57:29 +01:00
/* --------------------------------------------------------------------------------------------
* Check whether the specified column is null.
*/
bool IsColumnNull(Int32 idx) const;
/* --------------------------------------------------------------------------------------------
* Retrieve the column index associated with the specified name.
*/
Int32 GetColumnIndex(CSStr name) const;
/* --------------------------------------------------------------------------------------------
* Retrieve the column name associated with the specified index.
*/
CSStr GetColumnName(Int32 idx) const;
/* --------------------------------------------------------------------------------------------
* Retrieve the column origin name if the library was compiled with such feature.
*/
CSStr GetColumnOriginName(Int32 idx) const;
/* --------------------------------------------------------------------------------------------
* Retrieve the type identifier of the column associated with the specified index.
*/
Int32 GetColumnType(Int32 idx) const;
/* --------------------------------------------------------------------------------------------
* Retrieve the size in bytes of the column associated with the specified index.
*/
Int32 GetColumnBytes(Int32 idx) const;
/* --------------------------------------------------------------------------------------------
* Retrieve the column with the specified index.
*/
Object GetColumnByIndex(Int32 idx) const;
2016-02-27 10:57:29 +01:00
/* --------------------------------------------------------------------------------------------
* Retrieve the column with the specified name.
*/
Object GetColumnByName(CSStr name) const;
/* --------------------------------------------------------------------------------------------
* Retrieve the column with the specified name or index.
*/
Object GetColumn(const Object & column) const;
/* --------------------------------------------------------------------------------------------
* Retrieve the amount of columns available in the current row.
*/
Int32 GetDataCount() const;
/* --------------------------------------------------------------------------------------------
* Retrieve the parameter index associated with the specified name.
*/
Int32 GetParameterIndex(CSStr name) const;
/* --------------------------------------------------------------------------------------------
* Retrieve the parameter name associated with the specified index.
*/
CSStr GetParameterName(Int32 idx) const;
2016-02-27 10:57:29 +01:00
};
} // Namespace:: SqMod
#endif // _SQSQLITE_STATEMENT_HPP