1
0
mirror of https://github.com/VCMP-SqMod/SqMod.git synced 2024-11-14 11:47:15 +01:00
SqMod/modules/sqlite/Column.hpp

343 lines
12 KiB
C++
Raw Normal View History

2016-02-27 10:57:29 +01:00
#ifndef _SQSQLITE_COLUMN_HPP_
#define _SQSQLITE_COLUMN_HPP_
// ------------------------------------------------------------------------------------------------
#include "Handle/Statement.hpp"
2016-02-27 10:57:29 +01:00
// ------------------------------------------------------------------------------------------------
namespace SqMod {
/* ------------------------------------------------------------------------------------------------
2016-07-17 12:36:14 +02:00
* Used to manage and interact with statement columns.
2016-02-27 10:57:29 +01:00
*/
class Column
{
// --------------------------------------------------------------------------------------------
friend class Statement;
private:
// --------------------------------------------------------------------------------------------
Int32 m_Index; // The index of the managed column.
StmtRef m_Handle; // The statement where the column exist.
protected:
2016-02-27 10:57:29 +01:00
/* --------------------------------------------------------------------------------------------
* 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
2016-02-27 10:57:29 +01:00
/* --------------------------------------------------------------------------------------------
* 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 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 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
2016-02-27 10:57:29 +01:00
/* --------------------------------------------------------------------------------------------
* Modify the index to the specified value.
2016-02-27 10:57:29 +01:00
*/
void SetIndex(Int32 idx)
2016-02-27 10:57:29 +01:00
{
// Assign the index with a failsafe to invalid on error
AutoAssign< Int32 > aa(m_Index, -1, idx);
// Validate the obtained column index
SQMOD_VALIDATE_COLUMN(*this, idx);
// Don't fall back to the invalid index anymore
aa.Set(idx);
2016-02-27 10:57:29 +01:00
}
/* --------------------------------------------------------------------------------------------
* Modify the index to the specified value.
*/
void SetIndex(CSStr name)
{
SetIndex(SQMOD_GET_CREATED(*this)->GetColumnIndex(name));
}
/* --------------------------------------------------------------------------------------------
* Modify the index to the specified value.
*/
void SetIndex(const Object & column);
2016-02-27 10:57:29 +01:00
public:
/* --------------------------------------------------------------------------------------------
* Default constructor (null).
*/
Column()
: m_Index(-1), m_Handle()
2016-02-27 10:57:29 +01:00
{
/* ... */
}
/* --------------------------------------------------------------------------------------------
* No column constructor.
*/
Column(const StmtRef & stmt)
: m_Index(-1), m_Handle(stmt)
{
/* ... */
}
/* --------------------------------------------------------------------------------------------
* Index constructor.
*/
Column(const StmtRef & stmt, Int32 idx)
: m_Index(idx), m_Handle(stmt)
{
SQMOD_VALIDATE_COLUMN(*this, m_Index);
}
/* --------------------------------------------------------------------------------------------
* Name constructor.
*/
Column(const StmtRef & stmt, CSStr name)
: m_Index(stmt ? stmt->GetColumnIndex(name) : -1), m_Handle(stmt)
{
SQMOD_VALIDATE_COLUMN(*this, m_Index);
}
/* --------------------------------------------------------------------------------------------
* Dynamic constructor.
*/
Column(const StmtRef & stmt, const Object & column)
: m_Index(-1), m_Handle(stmt)
{
if (!m_Handle)
{
STHROWF("Invalid SQLite statement reference");
}
// Extract the index
SetIndex(column);
}
2016-02-27 10:57:29 +01:00
/* --------------------------------------------------------------------------------------------
* Copy constructor.
*/
Column(const Column & o) = default;
2016-02-27 10:57:29 +01:00
/* --------------------------------------------------------------------------------------------
* Move constructor.
2016-02-27 10:57:29 +01:00
*/
Column(Column && o) = default;
2016-02-27 10:57:29 +01:00
/* --------------------------------------------------------------------------------------------
* Copy assignment operator.
*/
Column & operator = (const Column & o) = default;
/* --------------------------------------------------------------------------------------------
* Move assignment operator.
*/
Column & operator = (Column && o) = default;
2016-02-27 10:57:29 +01:00
/* --------------------------------------------------------------------------------------------
* Perform an equality comparison between two table column indexes.
2016-02-27 10:57:29 +01:00
*/
bool operator == (const Column & o) const
{
return (m_Index == o.m_Index);
}
/* --------------------------------------------------------------------------------------------
* Perform an inequality comparison between two table column indexes.
2016-02-27 10:57:29 +01:00
*/
bool operator != (const Column & o) const
{
return (m_Index != o.m_Index);
}
/* --------------------------------------------------------------------------------------------
* Implicit conversion to boolean for use in boolean operations.
*/
operator bool () const
{
return m_Index >= 0;
}
/* --------------------------------------------------------------------------------------------
* Used by the script engine to convert an instance of this type to a string.
*/
CSStr ToString() const
{
CSStr val = nullptr;
// Can we attempt to return the parameter name?
if (m_Handle && m_Index)
{
val = sqlite3_column_name(m_Handle->mPtr, m_Index);
}
else
{
val = ToStrF(_SC("%d"), m_Index);
}
// Return the value if valid
return val ? val : _SC("");
2016-02-27 10:57:29 +01:00
}
/* --------------------------------------------------------------------------------------------
2016-07-17 12:36:14 +02:00
* See whether the column is valid.
2016-02-27 10:57:29 +01:00
*/
bool IsValid() const
{
return m_Handle; // An invalid statement means an invalid column
2016-02-27 10:57:29 +01:00
}
/* --------------------------------------------------------------------------------------------
2016-07-17 12:36:14 +02:00
* Return the number of active references to the associated statement handle.
2016-02-27 10:57:29 +01:00
*/
Uint32 GetRefCount() const
{
return m_Handle.Count();
2016-02-27 10:57:29 +01:00
}
/* --------------------------------------------------------------------------------------------
* Retrieve the referenced column index.
2016-02-27 10:57:29 +01:00
*/
Int32 GetIndex() const
{
return m_Index;
}
/* --------------------------------------------------------------------------------------------
* Retrieve the referenced database statement.
2016-02-27 10:57:29 +01:00
*/
Object GetStatement() const;
2016-02-27 10:57:29 +01:00
/* --------------------------------------------------------------------------------------------
* Retrieve the referenced database connection.
2016-02-27 10:57:29 +01:00
*/
Object GetConnection() const;
2016-02-27 10:57:29 +01:00
/* --------------------------------------------------------------------------------------------
* Release the reference to the referenced database statement and column index.
2016-02-27 10:57:29 +01:00
*/
void Release()
{
m_Handle.Reset();
2016-02-27 10:57:29 +01:00
m_Index = -1;
}
/* --------------------------------------------------------------------------------------------
* Check whether the referenced column is null.
2016-02-27 10:57:29 +01:00
*/
bool IsNull() const;
/* --------------------------------------------------------------------------------------------
* Retrieve the name of the referenced column index.
*/
CSStr GetName() const;
2016-02-27 10:57:29 +01:00
/* --------------------------------------------------------------------------------------------
* Retrieve the column origin name if the library was compiled with such feature.
2016-02-27 10:57:29 +01:00
*/
CSStr GetOriginName() const;
2016-02-27 10:57:29 +01:00
/* --------------------------------------------------------------------------------------------
* Retrieve the type identifier of the referenced column index.
2016-02-27 10:57:29 +01:00
*/
Int32 GetType() const;
2016-02-27 10:57:29 +01:00
/* --------------------------------------------------------------------------------------------
* Retrieve the size in bytes of the referenced column index.
2016-02-27 10:57:29 +01:00
*/
Int32 GetBytes() const;
2016-02-27 10:57:29 +01:00
/* --------------------------------------------------------------------------------------------
* Retrieve the value inside the referenced column as a dynamic type.
2016-02-27 10:57:29 +01:00
*/
Object GetValue() const;
2016-02-27 10:57:29 +01:00
/* --------------------------------------------------------------------------------------------
* Retrieve the value inside the referenced column as a numeric type.
2016-02-27 10:57:29 +01:00
*/
Object GetNumber() const;
2016-02-27 10:57:29 +01:00
/* --------------------------------------------------------------------------------------------
* Retrieve the value inside the referenced column as a native script integer.
2016-02-27 10:57:29 +01:00
*/
SQInteger GetInteger() const;
2016-02-27 10:57:29 +01:00
/* --------------------------------------------------------------------------------------------
* Retrieve the value inside the referenced column as a native script floating point.
2016-02-27 10:57:29 +01:00
*/
SQFloat GetFloat() const;
/* --------------------------------------------------------------------------------------------
* Retrieve the value inside the referenced column as a long integer.
*/
Object GetLong() const;
2016-02-27 10:57:29 +01:00
/* --------------------------------------------------------------------------------------------
* Retrieve the value inside the referenced column as a string.
2016-02-27 10:57:29 +01:00
*/
Object GetString() const;
2016-02-27 10:57:29 +01:00
/* --------------------------------------------------------------------------------------------
* Retrieve the value inside the referenced column as a boolean.
2016-02-27 10:57:29 +01:00
*/
bool GetBoolean() const;
2016-02-27 10:57:29 +01:00
/* --------------------------------------------------------------------------------------------
* Retrieve the value inside the referenced column as a character.
2016-02-27 10:57:29 +01:00
*/
SQChar GetChar() const;
2016-02-27 10:57:29 +01:00
/* --------------------------------------------------------------------------------------------
* Retrieve the value inside the referenced column as a memory buffer.
2016-02-27 10:57:29 +01:00
*/
Object GetBuffer() const;
2016-02-27 10:57:29 +01:00
/* --------------------------------------------------------------------------------------------
* Retrieve the value inside the referenced column as a memory blob.
2016-02-27 10:57:29 +01:00
*/
Object GetBlob() const;
2016-02-27 10:57:29 +01:00
};
} // Namespace:: SqMod
#endif // _SQSQLITE_COLUMN_HPP_