2016-02-27 10:57:29 +01:00
|
|
|
// ------------------------------------------------------------------------------------------------
|
|
|
|
#include "Column.hpp"
|
|
|
|
#include "Connection.hpp"
|
|
|
|
#include "Statement.hpp"
|
|
|
|
#include "Module.hpp"
|
|
|
|
|
2016-04-02 11:11:14 +02:00
|
|
|
// ------------------------------------------------------------------------------------------------
|
|
|
|
#include <cstring>
|
|
|
|
|
2016-02-27 10:57:29 +01:00
|
|
|
// ------------------------------------------------------------------------------------------------
|
|
|
|
#include <sqrat.h>
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
|
|
|
namespace SqMod {
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
|
|
|
SQInteger Column::Typename(HSQUIRRELVM vm)
|
|
|
|
{
|
|
|
|
static SQChar name[] = _SC("SqSQLiteColumn");
|
|
|
|
sq_pushstring(vm, name, sizeof(name));
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
2016-02-27 16:53:12 +01:00
|
|
|
void Column::Validate() const
|
2016-02-27 10:57:29 +01:00
|
|
|
{
|
|
|
|
// Are we pointing to a valid index?
|
|
|
|
if (m_Index < 0)
|
2016-04-02 11:11:14 +02:00
|
|
|
{
|
2016-03-22 23:25:32 +01:00
|
|
|
STHROWF("Invalid column index");
|
2016-04-02 11:11:14 +02:00
|
|
|
}
|
2016-02-27 10:57:29 +01:00
|
|
|
// Do we belong to a valid statement?
|
2016-04-02 11:11:14 +02:00
|
|
|
m_Stmt.Validate();
|
2016-02-27 10:57:29 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
2016-02-27 16:53:12 +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)
|
2016-04-02 11:11:14 +02:00
|
|
|
{
|
2016-03-22 23:25:32 +01:00
|
|
|
STHROWF("Invalid column index");
|
2016-04-02 11:11:14 +02:00
|
|
|
}
|
2016-02-27 10:57:29 +01:00
|
|
|
// Do we belong to a valid statement?
|
2016-04-02 11:11:14 +02:00
|
|
|
m_Stmt.Validate();
|
2016-02-27 10:57:29 +01:00
|
|
|
// Do we have any rows available?
|
2016-04-02 11:11:14 +02:00
|
|
|
if (!m_Stmt->mGood)
|
|
|
|
{
|
2016-03-22 23:25:32 +01:00
|
|
|
STHROWF("No row available");
|
2016-04-02 11:11:14 +02:00
|
|
|
}
|
2016-02-27 10:57:29 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
2016-02-27 12:51:14 +01:00
|
|
|
Object Column::GetStatement() const
|
2016-02-27 10:57:29 +01:00
|
|
|
{
|
2016-02-27 16:53:12 +01:00
|
|
|
// Validate the column
|
|
|
|
Validate();
|
|
|
|
// Return the requested information
|
|
|
|
return Object(new Statement(m_Stmt));
|
2016-02-27 10:57:29 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
2016-02-27 12:51:14 +01:00
|
|
|
Object Column::GetConnection() const
|
2016-02-27 10:57:29 +01:00
|
|
|
{
|
2016-02-27 16:53:12 +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
|
|
|
|
{
|
2016-02-27 16:53:12 +01:00
|
|
|
// 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
|
|
|
|
{
|
2016-02-27 16:53:12 +01:00
|
|
|
// Validate the column and statement row
|
|
|
|
ValidateRow();
|
|
|
|
// Return the requested information
|
2016-02-27 10:57:29 +01:00
|
|
|
#ifdef _SQ64
|
2016-02-27 16:53:12 +01:00
|
|
|
return sqlite3_column_int64(m_Stmt, m_Index);
|
2016-02-27 10:57:29 +01:00
|
|
|
#else
|
2016-02-27 16:53:12 +01:00
|
|
|
return sqlite3_column_int(m_Stmt, m_Index);
|
2016-02-27 10:57:29 +01:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
|
|
|
SQFloat Column::GetFloat() const
|
|
|
|
{
|
2016-02-27 16:53:12 +01:00
|
|
|
// Validate the column and statement row
|
|
|
|
ValidateRow();
|
|
|
|
// Return the requested information
|
2016-04-02 11:11:14 +02:00
|
|
|
return static_cast< SQFloat >(sqlite3_column_double(m_Stmt, m_Index));
|
2016-02-27 10:57:29 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
|
|
|
Object Column::GetLong() const
|
|
|
|
{
|
2016-02-27 16:53:12 +01:00
|
|
|
// Validate the column and statement row
|
|
|
|
ValidateRow();
|
2016-02-27 10:57:29 +01:00
|
|
|
// Obtain the initial stack size
|
2016-02-28 15:20:33 +01:00
|
|
|
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));
|
2016-02-28 15:20:33 +01:00
|
|
|
// 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
|
|
|
|
{
|
2016-02-27 16:53:12 +01:00
|
|
|
// Validate the column and statement row
|
|
|
|
ValidateRow();
|
2016-02-27 10:57:29 +01:00
|
|
|
// Obtain the initial stack size
|
2016-02-28 15:20:33 +01:00
|
|
|
const StackGuard sg(_SqVM);
|
2016-02-27 10:57:29 +01:00
|
|
|
// Push the column text on the stack
|
2016-04-02 11:11:14 +02:00
|
|
|
sq_pushstring(_SqVM, reinterpret_cast< CSStr >(sqlite3_column_text(m_Stmt, m_Index)),
|
|
|
|
sqlite3_column_bytes(m_Stmt, m_Index));
|
2016-02-28 15:20:33 +01:00
|
|
|
// 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
|
|
|
|
{
|
2016-02-27 16:53:12 +01:00
|
|
|
// 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
|
|
|
|
{
|
2016-02-27 16:53:12 +01:00
|
|
|
// Validate the column and statement row
|
|
|
|
ValidateRow();
|
2016-02-27 10:57:29 +01:00
|
|
|
// Obtain the initial stack size
|
2016-02-28 15:20:33 +01:00
|
|
|
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)
|
2016-04-02 11:11:14 +02:00
|
|
|
{
|
2016-03-22 23:25:32 +01:00
|
|
|
STHROWF("Unable to allocate space for column blob value");
|
2016-04-02 11:11:14 +02:00
|
|
|
}
|
2016-02-27 10:57:29 +01:00
|
|
|
// Is there any data to read?
|
|
|
|
else if (!b)
|
|
|
|
{
|
|
|
|
// Pop the memory blob from the stack
|
2016-02-28 15:20:33 +01:00
|
|
|
sq_pop(_SqVM, 1);
|
2016-02-27 16:53:12 +01:00
|
|
|
// Push a null value instead
|
|
|
|
sq_pushnull(_SqVM);
|
2016-02-27 10:57:29 +01:00
|
|
|
}
|
|
|
|
// Copy the data into the memory blob
|
|
|
|
else
|
2016-04-02 11:11:14 +02:00
|
|
|
{
|
|
|
|
std::memcpy(p, b, sz);
|
|
|
|
}
|
2016-02-28 15:20:33 +01:00
|
|
|
// 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
|
|
|
|
{
|
2016-02-27 16:53:12 +01:00
|
|
|
// 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
|
|
|
|
{
|
2016-02-27 16:53:12 +01:00
|
|
|
// 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
|
|
|
|
{
|
2016-02-27 16:53:12 +01:00
|
|
|
// 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
|
2016-02-27 16:53:12 +01:00
|
|
|
// 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
|
2016-03-22 23:25:32 +01:00
|
|
|
STHROWF("The module was compiled without this feature");
|
2016-02-27 10:57:29 +01:00
|
|
|
// Request failed
|
|
|
|
return _SC("");
|
2016-04-02 11:11:14 +02:00
|
|
|
#endif
|
2016-02-27 10:57:29 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
|
|
|
Int32 Column::GetType() const
|
|
|
|
{
|
2016-02-27 16:53:12 +01:00
|
|
|
// 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
|
|
|
|
{
|
2016-02-27 16:53:12 +01:00
|
|
|
// 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
|