2016-02-27 10:57:29 +01:00
|
|
|
#ifndef _SQSQLITE_COLUMN_HPP_
|
|
|
|
#define _SQSQLITE_COLUMN_HPP_
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
2016-06-03 20:31:00 +02:00
|
|
|
#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.
|
2016-06-15 22:49:25 +02:00
|
|
|
StmtRef m_Handle; // The statement where the column exist.
|
|
|
|
|
|
|
|
protected:
|
2016-02-27 10:57:29 +01:00
|
|
|
|
|
|
|
/* --------------------------------------------------------------------------------------------
|
2016-06-15 22:49:25 +02:00
|
|
|
* Validate the managed statement handle and throw an error if invalid.
|
2016-02-27 10:57:29 +01:00
|
|
|
*/
|
2016-06-15 22:49:25 +02:00
|
|
|
#if defined(_DEBUG) || defined(SQMOD_EXCEPTLOC)
|
|
|
|
void Validate(CCStr file, Int32 line) const;
|
|
|
|
#else
|
2016-02-27 16:53:12 +01:00
|
|
|
void Validate() const;
|
2016-06-15 22:49:25 +02:00
|
|
|
#endif // _DEBUG
|
2016-02-27 10:57:29 +01:00
|
|
|
|
|
|
|
/* --------------------------------------------------------------------------------------------
|
2016-06-15 22:49:25 +02:00
|
|
|
* Validate the managed statement handle and throw an error if invalid.
|
2016-02-27 10:57:29 +01:00
|
|
|
*/
|
2016-06-15 22:49:25 +02: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
|
|
|
|
|
2016-07-10 02:00:33 +02:00
|
|
|
/* --------------------------------------------------------------------------------------------
|
|
|
|
* 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
|
|
|
|
|
2016-06-15 22:49:25 +02: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
|
2016-02-27 16:53:12 +01:00
|
|
|
void ValidateRow() const;
|
2016-06-15 22:49:25 +02:00
|
|
|
#endif // _DEBUG
|
2016-02-27 10:57:29 +01:00
|
|
|
|
|
|
|
/* --------------------------------------------------------------------------------------------
|
2016-07-10 02:00:33 +02:00
|
|
|
* Modify the index to the specified value.
|
2016-02-27 10:57:29 +01:00
|
|
|
*/
|
2016-07-10 02:00:33 +02:00
|
|
|
void SetIndex(Int32 idx)
|
2016-02-27 10:57:29 +01:00
|
|
|
{
|
2016-08-26 17:22:45 +02:00
|
|
|
// Assign the index with a failsafe to invalid on error
|
|
|
|
AutoAssign< Int32 > aa(m_Index, -1, idx);
|
|
|
|
// Validate the obtained column index
|
2016-07-10 02:00:33 +02:00
|
|
|
SQMOD_VALIDATE_COLUMN(*this, idx);
|
2016-08-26 17:22:45 +02:00
|
|
|
// Don't fall back to the invalid index anymore
|
|
|
|
aa.Set(idx);
|
2016-02-27 10:57:29 +01:00
|
|
|
}
|
|
|
|
|
2016-07-10 02:00:33 +02: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()
|
2016-06-15 22:49:25 +02:00
|
|
|
: m_Index(-1), m_Handle()
|
2016-02-27 10:57:29 +01:00
|
|
|
{
|
|
|
|
/* ... */
|
|
|
|
}
|
|
|
|
|
2016-07-10 02:00:33 +02: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.
|
|
|
|
*/
|
2016-06-15 22:49:25 +02:00
|
|
|
Column(const Column & o) = default;
|
2016-02-27 10:57:29 +01:00
|
|
|
|
|
|
|
/* --------------------------------------------------------------------------------------------
|
2016-06-15 22:49:25 +02:00
|
|
|
* Move constructor.
|
2016-02-27 10:57:29 +01:00
|
|
|
*/
|
2016-06-15 22:49:25 +02:00
|
|
|
Column(Column && o) = default;
|
2016-02-27 10:57:29 +01:00
|
|
|
|
|
|
|
/* --------------------------------------------------------------------------------------------
|
|
|
|
* Copy assignment operator.
|
|
|
|
*/
|
2016-06-15 22:49:25 +02:00
|
|
|
Column & operator = (const Column & o) = default;
|
|
|
|
|
|
|
|
/* --------------------------------------------------------------------------------------------
|
|
|
|
* Move assignment operator.
|
|
|
|
*/
|
|
|
|
Column & operator = (Column && o) = default;
|
2016-02-27 10:57:29 +01:00
|
|
|
|
|
|
|
/* --------------------------------------------------------------------------------------------
|
2016-07-10 02:00:33 +02: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);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* --------------------------------------------------------------------------------------------
|
2016-07-10 02:00:33 +02:00
|
|
|
* 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
|
|
|
|
{
|
2016-07-10 02:00:33 +02:00
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
/* --------------------------------------------------------------------------------------------
|
|
|
|
* Used by the script engine to retrieve the name from instances of this type.
|
|
|
|
*/
|
|
|
|
static SQInteger Typename(HSQUIRRELVM vm);
|
|
|
|
|
|
|
|
/* --------------------------------------------------------------------------------------------
|
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
|
|
|
|
{
|
2016-06-15 22:49:25 +02:00
|
|
|
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
|
|
|
|
{
|
2016-06-15 22:49:25 +02:00
|
|
|
return m_Handle.Count();
|
2016-02-27 10:57:29 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/* --------------------------------------------------------------------------------------------
|
2016-07-10 02:00:33 +02:00
|
|
|
* Retrieve the referenced column index.
|
2016-02-27 10:57:29 +01:00
|
|
|
*/
|
|
|
|
Int32 GetIndex() const
|
|
|
|
{
|
|
|
|
return m_Index;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* --------------------------------------------------------------------------------------------
|
2016-07-10 02:00:33 +02:00
|
|
|
* Retrieve the referenced database statement.
|
2016-02-27 10:57:29 +01:00
|
|
|
*/
|
2016-02-27 12:51:14 +01:00
|
|
|
Object GetStatement() const;
|
2016-02-27 10:57:29 +01:00
|
|
|
|
|
|
|
/* --------------------------------------------------------------------------------------------
|
2016-07-10 02:00:33 +02:00
|
|
|
* Retrieve the referenced database connection.
|
2016-02-27 10:57:29 +01:00
|
|
|
*/
|
2016-02-27 12:51:14 +01:00
|
|
|
Object GetConnection() const;
|
2016-02-27 10:57:29 +01:00
|
|
|
|
|
|
|
/* --------------------------------------------------------------------------------------------
|
2016-07-10 02:00:33 +02:00
|
|
|
* Release the reference to the referenced database statement and column index.
|
2016-02-27 10:57:29 +01:00
|
|
|
*/
|
|
|
|
void Release()
|
|
|
|
{
|
2016-06-15 22:49:25 +02:00
|
|
|
m_Handle.Reset();
|
2016-02-27 10:57:29 +01:00
|
|
|
m_Index = -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* --------------------------------------------------------------------------------------------
|
2016-07-10 02:00:33 +02:00
|
|
|
* Check whether the referenced column is null.
|
2016-02-27 10:57:29 +01:00
|
|
|
*/
|
2016-07-10 02:00:33 +02:00
|
|
|
bool IsNull() const;
|
2016-06-15 22:49:25 +02:00
|
|
|
|
|
|
|
/* --------------------------------------------------------------------------------------------
|
2016-07-10 02:00:33 +02:00
|
|
|
* Retrieve the name of the referenced column index.
|
2016-06-15 22:49:25 +02:00
|
|
|
*/
|
2016-07-10 02:00:33 +02:00
|
|
|
CSStr GetName() const;
|
2016-02-27 10:57:29 +01:00
|
|
|
|
|
|
|
/* --------------------------------------------------------------------------------------------
|
2016-07-10 02:00:33 +02:00
|
|
|
* Retrieve the column origin name if the library was compiled with such feature.
|
2016-02-27 10:57:29 +01:00
|
|
|
*/
|
2016-07-10 02:00:33 +02:00
|
|
|
CSStr GetOriginName() const;
|
2016-02-27 10:57:29 +01:00
|
|
|
|
|
|
|
/* --------------------------------------------------------------------------------------------
|
2016-07-10 02:00:33 +02:00
|
|
|
* Retrieve the type identifier of the referenced column index.
|
2016-02-27 10:57:29 +01:00
|
|
|
*/
|
2016-07-10 02:00:33 +02:00
|
|
|
Int32 GetType() const;
|
2016-02-27 10:57:29 +01:00
|
|
|
|
|
|
|
/* --------------------------------------------------------------------------------------------
|
2016-07-10 02:00:33 +02:00
|
|
|
* Retrieve the size in bytes of the referenced column index.
|
2016-02-27 10:57:29 +01:00
|
|
|
*/
|
2016-07-10 02:00:33 +02:00
|
|
|
Int32 GetBytes() const;
|
2016-02-27 10:57:29 +01:00
|
|
|
|
|
|
|
/* --------------------------------------------------------------------------------------------
|
2016-07-10 02:00:33 +02:00
|
|
|
* Retrieve the value inside the referenced column as a dynamic type.
|
2016-02-27 10:57:29 +01:00
|
|
|
*/
|
2016-07-10 02:00:33 +02:00
|
|
|
Object GetValue() const;
|
2016-02-27 10:57:29 +01:00
|
|
|
|
|
|
|
/* --------------------------------------------------------------------------------------------
|
2016-07-10 02:00:33 +02:00
|
|
|
* Retrieve the value inside the referenced column as a numeric type.
|
2016-02-27 10:57:29 +01:00
|
|
|
*/
|
2016-07-10 02:00:33 +02:00
|
|
|
Object GetNumber() const;
|
2016-02-27 10:57:29 +01:00
|
|
|
|
|
|
|
/* --------------------------------------------------------------------------------------------
|
2016-07-10 02:00:33 +02:00
|
|
|
* Retrieve the value inside the referenced column as a native script integer.
|
2016-02-27 10:57:29 +01:00
|
|
|
*/
|
2016-07-10 02:00:33 +02:00
|
|
|
SQInteger GetInteger() const;
|
2016-02-27 10:57:29 +01:00
|
|
|
|
|
|
|
/* --------------------------------------------------------------------------------------------
|
2016-07-10 02:00:33 +02:00
|
|
|
* Retrieve the value inside the referenced column as a native script floating point.
|
2016-02-27 10:57:29 +01:00
|
|
|
*/
|
2016-07-10 02:00:33 +02:00
|
|
|
SQFloat GetFloat() const;
|
2016-06-15 22:49:25 +02:00
|
|
|
|
|
|
|
/* --------------------------------------------------------------------------------------------
|
2016-07-10 02:00:33 +02:00
|
|
|
* Retrieve the value inside the referenced column as a long integer.
|
2016-06-15 22:49:25 +02:00
|
|
|
*/
|
2016-07-10 02:00:33 +02:00
|
|
|
Object GetLong() const;
|
2016-02-27 10:57:29 +01:00
|
|
|
|
|
|
|
/* --------------------------------------------------------------------------------------------
|
2016-07-10 02:00:33 +02:00
|
|
|
* Retrieve the value inside the referenced column as a string.
|
2016-02-27 10:57:29 +01:00
|
|
|
*/
|
2016-07-10 02:00:33 +02:00
|
|
|
Object GetString() const;
|
2016-02-27 10:57:29 +01:00
|
|
|
|
|
|
|
/* --------------------------------------------------------------------------------------------
|
2016-07-10 02:00:33 +02:00
|
|
|
* Retrieve the value inside the referenced column as a boolean.
|
2016-02-27 10:57:29 +01:00
|
|
|
*/
|
2016-07-10 02:00:33 +02:00
|
|
|
bool GetBoolean() const;
|
2016-02-27 10:57:29 +01:00
|
|
|
|
|
|
|
/* --------------------------------------------------------------------------------------------
|
2016-07-10 02:00:33 +02:00
|
|
|
* Retrieve the value inside the referenced column as a character.
|
2016-02-27 10:57:29 +01:00
|
|
|
*/
|
2016-07-10 02:00:33 +02:00
|
|
|
SQChar GetChar() const;
|
2016-02-27 10:57:29 +01:00
|
|
|
|
|
|
|
/* --------------------------------------------------------------------------------------------
|
2016-07-10 02:00:33 +02:00
|
|
|
* Retrieve the value inside the referenced column as a memory buffer.
|
2016-02-27 10:57:29 +01:00
|
|
|
*/
|
2016-07-10 02:00:33 +02:00
|
|
|
Object GetBuffer() const;
|
2016-02-27 10:57:29 +01:00
|
|
|
|
|
|
|
/* --------------------------------------------------------------------------------------------
|
2016-07-10 02:00:33 +02:00
|
|
|
* Retrieve the value inside the referenced column as a memory blob.
|
2016-02-27 10:57:29 +01:00
|
|
|
*/
|
2016-07-10 02:00:33 +02:00
|
|
|
Object GetBlob() const;
|
2016-02-27 10:57:29 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
} // Namespace:: SqMod
|
|
|
|
|
|
|
|
#endif // _SQSQLITE_COLUMN_HPP_
|