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

230 lines
7.0 KiB
C++
Raw Normal View History

2016-02-27 10:57:29 +01:00
// ------------------------------------------------------------------------------------------------
#include "Column.hpp"
#include "Connection.hpp"
#include "Statement.hpp"
// ------------------------------------------------------------------------------------------------
#include <cstring>
2016-02-27 10:57:29 +01:00
// ------------------------------------------------------------------------------------------------
namespace SqMod {
// ------------------------------------------------------------------------------------------------
SQInteger Column::Typename(HSQUIRRELVM vm)
{
static const SQChar name[] = _SC("SqSQLiteColumn");
2016-02-27 10:57:29 +01:00
sq_pushstring(vm, name, sizeof(name));
return 1;
}
// ------------------------------------------------------------------------------------------------
void Column::Validate() const
2016-02-27 10:57:29 +01:00
{
// Are we pointing to a valid index?
if (m_Index < 0)
{
STHROWF("Invalid column index");
}
2016-02-27 10:57:29 +01:00
// Do we belong to a valid statement?
m_Stmt.Validate();
2016-02-27 10:57:29 +01:00
}
// ------------------------------------------------------------------------------------------------
void Column::ValidateRow() const
2016-02-27 10:57:29 +01:00
{
// Are we pointing to a valid index?
if (m_Index < 0)
{
STHROWF("Invalid column index");
}
2016-02-27 10:57:29 +01:00
// Do we belong to a valid statement?
m_Stmt.Validate();
2016-02-27 10:57:29 +01:00
// Do we have any rows available?
if (!m_Stmt->mGood)
{
STHROWF("No row available");
}
2016-02-27 10:57:29 +01:00
}
// ------------------------------------------------------------------------------------------------
Object Column::GetStatement() const
2016-02-27 10:57:29 +01:00
{
// Validate the column
Validate();
// Return the requested information
return Object(new Statement(m_Stmt));
2016-02-27 10:57:29 +01:00
}
// ------------------------------------------------------------------------------------------------
Object Column::GetConnection() const
2016-02-27 10:57:29 +01:00
{
// Validate the column
Validate();
// Return the requested information
return Object(new Connection(m_Stmt->mConn));
2016-02-27 10:57:29 +01:00
}
// ------------------------------------------------------------------------------------------------
Int32 Column::GetNumber() const
{
// Validate the column and statement row
ValidateRow();
// Return the requested information
return sqlite3_column_int(m_Stmt, m_Index);
2016-02-27 10:57:29 +01:00
}
// ------------------------------------------------------------------------------------------------
SQInteger Column::GetInteger() const
{
// Validate the column and statement row
ValidateRow();
// Return the requested information
2016-02-27 10:57:29 +01:00
#ifdef _SQ64
return sqlite3_column_int64(m_Stmt, m_Index);
2016-02-27 10:57:29 +01:00
#else
return sqlite3_column_int(m_Stmt, m_Index);
2016-02-27 10:57:29 +01:00
#endif
}
// ------------------------------------------------------------------------------------------------
SQFloat Column::GetFloat() const
{
// Validate the column and statement row
ValidateRow();
// Return the requested information
return static_cast< SQFloat >(sqlite3_column_double(m_Stmt, m_Index));
2016-02-27 10:57:29 +01:00
}
// ------------------------------------------------------------------------------------------------
Object Column::GetLong() const
{
// Validate the column and statement row
ValidateRow();
2016-02-27 10:57:29 +01:00
// Obtain the initial stack size
const StackGuard sg(_SqVM);
2016-02-27 10:57:29 +01:00
// Push a long integer instance with the requested value on the stack
_SqMod->PushSLongObject(_SqVM, sqlite3_column_int64(m_Stmt, m_Index));
// Get the object from the stack and return it
return Var< Object >(_SqVM, -1).value;
2016-02-27 10:57:29 +01:00
}
// ------------------------------------------------------------------------------------------------
Object Column::GetString() const
{
// Validate the column and statement row
ValidateRow();
2016-02-27 10:57:29 +01:00
// Obtain the initial stack size
const StackGuard sg(_SqVM);
2016-02-27 10:57:29 +01:00
// Push the column text on the stack
sq_pushstring(_SqVM, reinterpret_cast< CSStr >(sqlite3_column_text(m_Stmt, m_Index)),
sqlite3_column_bytes(m_Stmt, m_Index));
// Get the object from the stack and return it
return Var< Object >(_SqVM, -1).value;
2016-02-27 10:57:29 +01:00
}
// ------------------------------------------------------------------------------------------------
bool Column::GetBoolean() const
{
// Validate the column and statement row
ValidateRow();
// Return the requested information
return sqlite3_column_int(m_Stmt, m_Index) > 0;
2016-02-27 10:57:29 +01:00
}
// ------------------------------------------------------------------------------------------------
Object Column::GetBlob() const
{
// Validate the column and statement row
ValidateRow();
2016-02-27 10:57:29 +01:00
// Obtain the initial stack size
const StackGuard sg(_SqVM);
2016-02-27 10:57:29 +01:00
// Obtain the size of the data
const Int32 sz = sqlite3_column_bytes(m_Stmt, m_Index);
// Allocate a blob of the same size
SQUserPointer p = sqstd_createblob(_SqVM, sz);
// Obtain a pointer to the data
const void * b = sqlite3_column_blob(m_Stmt, m_Index);
// Could the memory blob be allocated?
if (!p)
{
STHROWF("Unable to allocate space for column blob value");
}
2016-02-27 10:57:29 +01:00
// Is there any data to read?
else if (!b)
{
// Pop the memory blob from the stack
sq_pop(_SqVM, 1);
// Push a null value instead
sq_pushnull(_SqVM);
2016-02-27 10:57:29 +01:00
}
// Copy the data into the memory blob
else
{
std::memcpy(p, b, sz);
}
// Get the object from the stack and return it
return Var< Object >(_SqVM, -1).value;
2016-02-27 10:57:29 +01:00
}
// ------------------------------------------------------------------------------------------------
SQChar Column::GetChar() const
{
// Validate the column and statement row
ValidateRow();
// Return the requested information
return (SQChar)sqlite3_column_int(m_Stmt, m_Index);
2016-02-27 10:57:29 +01:00
}
// ------------------------------------------------------------------------------------------------
bool Column::IsNull() const
{
// Validate the column
Validate();
// Return the requested information
return (sqlite3_column_type(m_Stmt, m_Index) == SQLITE_NULL);
2016-02-27 10:57:29 +01:00
}
// ------------------------------------------------------------------------------------------------
CSStr Column::GetName() const
{
// Validate the column
Validate();
// Return the requested information
return sqlite3_column_name(m_Stmt, m_Index);
2016-02-27 10:57:29 +01:00
}
// ------------------------------------------------------------------------------------------------
CSStr Column::GetOriginName() const
{
#ifdef SQLITE_ENABLE_COLUMN_METADATA
// Validate the column
Validate();
// Return the requested information
return sqlite3_column_origin_name(m_Stmt, m_Index);
2016-02-27 10:57:29 +01:00
#else
STHROWF("The module was compiled without this feature");
2016-02-27 10:57:29 +01:00
// Request failed
return _SC("");
#endif
2016-02-27 10:57:29 +01:00
}
// ------------------------------------------------------------------------------------------------
Int32 Column::GetType() const
{
// Validate the column
Validate();
// Return the requested information
return sqlite3_column_type(m_Stmt, m_Index);
2016-02-27 10:57:29 +01:00
}
// ------------------------------------------------------------------------------------------------
Int32 Column::GetBytes() const
{
// Validate the column
Validate();
// Return the requested information
return sqlite3_column_bytes(m_Stmt, m_Index);
2016-02-27 10:57:29 +01:00
}
} // Namespace:: SqMod