2016-02-27 10:57:29 +01:00
|
|
|
// ------------------------------------------------------------------------------------------------
|
|
|
|
#include "Statement.hpp"
|
|
|
|
#include "Connection.hpp"
|
|
|
|
#include "Column.hpp"
|
|
|
|
|
2016-04-02 11:11:14 +02:00
|
|
|
// ------------------------------------------------------------------------------------------------
|
|
|
|
#include <cstring>
|
|
|
|
|
2016-02-27 10:57:29 +01:00
|
|
|
// ------------------------------------------------------------------------------------------------
|
|
|
|
namespace SqMod {
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
|
|
|
SQInteger Statement::Typename(HSQUIRRELVM vm)
|
|
|
|
{
|
2016-05-22 21:34:27 +02:00
|
|
|
static const SQChar name[] = _SC("SqSQLiteStatement");
|
2016-02-27 10:57:29 +01:00
|
|
|
sq_pushstring(vm, name, sizeof(name));
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
2016-06-15 22:49:25 +02:00
|
|
|
#if defined(_DEBUG) || defined(SQMOD_EXCEPTLOC)
|
|
|
|
void Statement::Validate(CCStr file, Int32 line) const
|
2016-02-27 10:57:29 +01:00
|
|
|
{
|
|
|
|
if (!m_Handle)
|
2016-03-18 11:58:02 +01:00
|
|
|
{
|
2016-06-15 22:49:25 +02:00
|
|
|
SqThrowF("Invalid SQLite statement reference =>[%s:%d]", file, line);
|
2016-03-18 11:58:02 +01:00
|
|
|
}
|
2016-06-15 22:49:25 +02:00
|
|
|
}
|
|
|
|
#else
|
|
|
|
void Statement::Validate() const
|
|
|
|
{
|
|
|
|
if (!m_Handle)
|
2016-03-18 11:58:02 +01:00
|
|
|
{
|
2016-06-15 22:49:25 +02:00
|
|
|
SqThrowF("Invalid SQLite statement reference");
|
2016-03-18 11:58:02 +01:00
|
|
|
}
|
2016-02-27 10:57:29 +01:00
|
|
|
}
|
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
|
|
|
#if defined(_DEBUG) || defined(SQMOD_EXCEPTLOC)
|
|
|
|
void Statement::ValidateCreated(CCStr file, Int32 line) const
|
2016-02-27 10:57:29 +01:00
|
|
|
{
|
|
|
|
if (!m_Handle)
|
2016-03-18 11:58:02 +01:00
|
|
|
{
|
2016-06-15 22:49:25 +02:00
|
|
|
SqThrowF("Invalid SQLite statement reference =>[%s:%d]", file, line);
|
2016-03-18 11:58:02 +01:00
|
|
|
}
|
2016-06-15 22:49:25 +02:00
|
|
|
else if (m_Handle->mPtr == nullptr)
|
2016-03-18 11:58:02 +01:00
|
|
|
{
|
2016-06-15 22:49:25 +02:00
|
|
|
SqThrowF("Invalid SQLite statement =>[%s:%d]", file, line);
|
2016-03-18 11:58:02 +01:00
|
|
|
}
|
2016-02-27 10:57:29 +01:00
|
|
|
}
|
2016-06-15 22:49:25 +02:00
|
|
|
#else
|
|
|
|
void Statement::ValidateCreated() const
|
|
|
|
{
|
|
|
|
if (!m_Handle)
|
|
|
|
{
|
|
|
|
SqThrowF("Invalid SQLite statement reference");
|
|
|
|
}
|
|
|
|
else if (m_Handle->mPtr == nullptr)
|
|
|
|
{
|
|
|
|
SqThrowF("Invalid SQLite statement");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif // _DEBUG
|
2016-02-27 10:57:29 +01:00
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
2016-06-15 22:49:25 +02:00
|
|
|
#if defined(_DEBUG) || defined(SQMOD_EXCEPTLOC)
|
|
|
|
const StmtRef & Statement::GetValid(CCStr file, Int32 line) const
|
|
|
|
{
|
|
|
|
Validate(file, line);
|
|
|
|
return m_Handle;
|
|
|
|
}
|
|
|
|
#else
|
|
|
|
const StmtRef & Statement::GetValid() const
|
2016-02-27 10:57:29 +01:00
|
|
|
{
|
2016-06-15 22:49:25 +02:00
|
|
|
Validate();
|
|
|
|
return m_Handle;
|
2016-02-27 10:57:29 +01:00
|
|
|
}
|
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
|
|
|
#if defined(_DEBUG) || defined(SQMOD_EXCEPTLOC)
|
|
|
|
const StmtRef & Statement::GetCreated(CCStr file, Int32 line) const
|
|
|
|
{
|
|
|
|
ValidateCreated(file, line);
|
|
|
|
return m_Handle;
|
|
|
|
}
|
|
|
|
#else
|
|
|
|
const StmtRef & Statement::GetCreated() const
|
2016-02-27 10:57:29 +01:00
|
|
|
{
|
2016-06-15 22:49:25 +02:00
|
|
|
ValidateCreated();
|
|
|
|
return m_Handle;
|
|
|
|
}
|
|
|
|
#endif // _DEBUG
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
|
|
|
#if defined(_DEBUG) || defined(SQMOD_EXCEPTLOC)
|
|
|
|
void Statement::ValidateColumn(Int32 idx, CCStr file, Int32 line) const
|
|
|
|
{
|
|
|
|
ValidateCreated(file, line);
|
|
|
|
// Is the specified index in range?
|
|
|
|
if (!m_Handle->CheckColumn(idx))
|
2016-03-18 11:58:02 +01:00
|
|
|
{
|
2016-06-15 22:49:25 +02:00
|
|
|
SqThrowF("Column index is out of range: %d:%d =>[%s:%d]", idx, m_Handle->mColumns,
|
|
|
|
file, line);
|
2016-03-18 11:58:02 +01:00
|
|
|
}
|
2016-06-15 22:49:25 +02:00
|
|
|
}
|
|
|
|
#else
|
|
|
|
void Statement::ValidateColumn(Int32 idx) const
|
|
|
|
{
|
|
|
|
ValidateCreated();
|
|
|
|
// Is the specified index in range?
|
|
|
|
if (!m_Handle->CheckColumn(idx))
|
2016-03-18 11:58:02 +01:00
|
|
|
{
|
2016-06-15 22:49:25 +02:00
|
|
|
SqThrowF("Column index is out of range: %d:%d", idx, m_Handle->mColumns);
|
2016-03-18 11:58:02 +01:00
|
|
|
}
|
2016-02-27 10:57:29 +01:00
|
|
|
}
|
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
|
|
|
#if defined(_DEBUG) || defined(SQMOD_EXCEPTLOC)
|
2016-07-10 02:00:33 +02:00
|
|
|
void Statement::ValidateParam(Int32 idx, CCStr file, Int32 line) const
|
2016-06-15 22:49:25 +02:00
|
|
|
{
|
|
|
|
ValidateCreated(file, line);
|
|
|
|
// Is the specified index in range?
|
|
|
|
if (!m_Handle->CheckParameter(idx))
|
|
|
|
{
|
|
|
|
SqThrowF("Parameter index is out of range: %d:%d =>[%s:%d]", idx, m_Handle->mParameters,
|
|
|
|
file, line);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#else
|
2016-07-10 02:00:33 +02:00
|
|
|
void Statement::ValidateParam(Int32 idx) const
|
2016-06-15 22:49:25 +02:00
|
|
|
{
|
|
|
|
ValidateCreated();
|
|
|
|
// Is the specified index in range?
|
|
|
|
if (!m_Handle->CheckParameter(idx))
|
|
|
|
{
|
|
|
|
SqThrowF("Parameter index is out of range: %d:%d", idx, m_Handle->mParameters);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif // _DEBUG
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
|
|
|
#if defined(_DEBUG) || defined(SQMOD_EXCEPTLOC)
|
|
|
|
void Statement::ValidateRow(CCStr file, Int32 line) const
|
2016-02-27 10:57:29 +01:00
|
|
|
{
|
2016-06-15 22:49:25 +02:00
|
|
|
ValidateCreated(file, line);
|
|
|
|
// Do we have any rows available?
|
|
|
|
if (!m_Handle->mGood)
|
2016-03-18 11:58:02 +01:00
|
|
|
{
|
2016-06-15 22:49:25 +02:00
|
|
|
SqThrowF("No row available =>[%s:%d]", file, line);
|
2016-03-18 11:58:02 +01:00
|
|
|
}
|
2016-06-15 22:49:25 +02:00
|
|
|
}
|
|
|
|
#else
|
|
|
|
void Statement::ValidateRow() const
|
|
|
|
{
|
|
|
|
ValidateCreated();
|
|
|
|
// Do we have any rows available?
|
|
|
|
if (!m_Handle->mGood)
|
2016-03-18 11:58:02 +01:00
|
|
|
{
|
2016-06-15 22:49:25 +02:00
|
|
|
SqThrowF("No row available");
|
2016-03-18 11:58:02 +01:00
|
|
|
}
|
2016-02-27 10:57:29 +01:00
|
|
|
}
|
2016-06-15 22:49:25 +02:00
|
|
|
#endif // _DEBUG
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
|
|
|
Statement::Statement(const Connection & connection, CSStr query)
|
|
|
|
: m_Handle(new StmtHnd(connection.GetHandle()))
|
|
|
|
{
|
2016-07-10 02:00:33 +02:00
|
|
|
SQMOD_GET_VALID(*this)->Create(query);
|
2016-06-15 22:49:25 +02:00
|
|
|
}
|
2016-02-27 10:57:29 +01:00
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
2016-02-27 12:51:14 +01:00
|
|
|
Object Statement::GetConnection() const
|
2016-02-27 10:57:29 +01:00
|
|
|
{
|
2016-07-10 02:00:33 +02:00
|
|
|
return Object(new Connection(SQMOD_GET_VALID(*this)->mConn));
|
2016-02-27 10:57:29 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
2016-06-16 05:37:32 +02:00
|
|
|
Statement & Statement::Reset()
|
2016-02-27 10:57:29 +01:00
|
|
|
{
|
2016-07-10 02:00:33 +02:00
|
|
|
SQMOD_VALIDATE_CREATED(*this);
|
2016-02-27 10:57:29 +01:00
|
|
|
// Specify that we don't have a row available and we haven't finished stepping
|
|
|
|
m_Handle->mGood = false;
|
|
|
|
m_Handle->mDone = false;
|
|
|
|
// Attempt to reset the statement to it's initial state
|
2016-06-15 22:49:25 +02:00
|
|
|
m_Handle->mStatus = sqlite3_reset(m_Handle->mPtr);
|
2016-02-27 10:57:29 +01:00
|
|
|
// Validate the result
|
2016-06-15 22:49:25 +02:00
|
|
|
if (m_Handle->mStatus != SQLITE_OK)
|
2016-03-18 11:58:02 +01:00
|
|
|
{
|
2016-06-15 22:49:25 +02:00
|
|
|
STHROWF("Unable to reset statement [%s]", m_Handle->ErrStr());
|
2016-03-18 11:58:02 +01:00
|
|
|
}
|
2016-06-16 05:37:32 +02:00
|
|
|
// Allow further chaining of operations
|
|
|
|
return *this;
|
2016-02-27 10:57:29 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
2016-06-16 05:37:32 +02:00
|
|
|
Statement & Statement::Clear()
|
2016-02-27 10:57:29 +01:00
|
|
|
{
|
2016-07-10 02:00:33 +02:00
|
|
|
SQMOD_VALIDATE_CREATED(*this);
|
2016-02-27 10:57:29 +01:00
|
|
|
// Specify that we don't have a row available and we haven't finished stepping
|
|
|
|
m_Handle->mGood = false;
|
|
|
|
m_Handle->mDone = false;
|
|
|
|
// Attempt to clear the statement
|
2016-06-15 22:49:25 +02:00
|
|
|
m_Handle->mStatus = sqlite3_clear_bindings(m_Handle->mPtr);
|
2016-02-27 10:57:29 +01:00
|
|
|
// Validate the result
|
2016-06-15 22:49:25 +02:00
|
|
|
if (m_Handle->mStatus != SQLITE_OK)
|
2016-03-18 11:58:02 +01:00
|
|
|
{
|
2016-06-15 22:49:25 +02:00
|
|
|
STHROWF("Unable to clear statement [%s]", m_Handle->ErrStr());
|
2016-03-18 11:58:02 +01:00
|
|
|
}
|
2016-06-16 05:37:32 +02:00
|
|
|
// Allow further chaining of operations
|
|
|
|
return *this;
|
2016-02-27 10:57:29 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
|
|
|
Int32 Statement::Exec()
|
|
|
|
{
|
2016-07-10 02:00:33 +02:00
|
|
|
SQMOD_VALIDATE_CREATED(*this);
|
2016-02-27 10:57:29 +01:00
|
|
|
// Did we reset first?
|
2016-02-27 16:53:12 +01:00
|
|
|
if (m_Handle->mDone)
|
2016-03-18 11:58:02 +01:00
|
|
|
{
|
2016-03-22 23:25:32 +01:00
|
|
|
STHROWF("Executed without resetting first");
|
2016-03-18 11:58:02 +01:00
|
|
|
}
|
2016-02-27 10:57:29 +01:00
|
|
|
// Attempt to step the statement
|
2016-06-15 22:49:25 +02:00
|
|
|
m_Handle->mStatus = sqlite3_step(m_Handle->mPtr);
|
2016-02-27 10:57:29 +01:00
|
|
|
// Have we finished stepping?
|
2016-06-15 22:49:25 +02:00
|
|
|
if (m_Handle->mStatus == SQLITE_DONE)
|
2016-02-27 10:57:29 +01:00
|
|
|
{
|
|
|
|
// Specify that we don't have row available and we finished stepping
|
|
|
|
m_Handle->mGood = false;
|
|
|
|
m_Handle->mDone = true;
|
|
|
|
// Return the changes made by this statement
|
2016-06-15 22:49:25 +02:00
|
|
|
return sqlite3_changes(m_Handle->mConn->mPtr);
|
2016-02-27 10:57:29 +01:00
|
|
|
}
|
|
|
|
// Specify that we don't have any row and we haven't finished stepping
|
|
|
|
m_Handle->mGood = false;
|
|
|
|
m_Handle->mDone = false;
|
|
|
|
// Inspect the result
|
|
|
|
switch (m_Handle->mStatus)
|
|
|
|
{
|
|
|
|
// We don't expect any rows to be returned in this case!
|
2016-03-22 23:25:32 +01:00
|
|
|
case SQLITE_ROW: STHROWF("Results were found");
|
|
|
|
case SQLITE_BUSY: STHROWF("Database was busy");
|
|
|
|
case SQLITE_ERROR: STHROWF("Runtime error occurred");
|
|
|
|
case SQLITE_MISUSE: STHROWF("Statement misuse");
|
2016-07-10 15:19:56 +02:00
|
|
|
default: STHROWF("Unknown failure [%s]", m_Handle->ErrStr());
|
2016-02-27 10:57:29 +01:00
|
|
|
}
|
2016-02-27 16:53:12 +01:00
|
|
|
// Operation failed (shouldn't reach this point!)
|
2016-02-27 10:57:29 +01:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
|
|
|
bool Statement::Step()
|
|
|
|
{
|
2016-07-10 02:00:33 +02:00
|
|
|
SQMOD_VALIDATE_CREATED(*this);
|
2016-02-27 10:57:29 +01:00
|
|
|
// Did we reset first?
|
2016-02-27 16:53:12 +01:00
|
|
|
if (m_Handle->mDone)
|
2016-03-18 11:58:02 +01:00
|
|
|
{
|
2016-03-22 23:25:32 +01:00
|
|
|
STHROWF("Stepped without resetting first");
|
2016-03-18 11:58:02 +01:00
|
|
|
}
|
2016-02-27 10:57:29 +01:00
|
|
|
// Attempt to step the statement
|
2016-06-15 22:49:25 +02:00
|
|
|
m_Handle->mStatus = sqlite3_step(m_Handle->mPtr);
|
2016-02-27 10:57:29 +01:00
|
|
|
// Do we have a row available?
|
2016-06-15 22:49:25 +02:00
|
|
|
if (m_Handle->mStatus == SQLITE_ROW)
|
2016-03-18 11:58:02 +01:00
|
|
|
{
|
2016-02-27 10:57:29 +01:00
|
|
|
// Specify that we have a row available
|
|
|
|
return (m_Handle->mGood = true);
|
2016-03-18 11:58:02 +01:00
|
|
|
}
|
2016-02-27 10:57:29 +01:00
|
|
|
// Have we finished stepping?
|
2016-06-15 22:49:25 +02:00
|
|
|
else if (m_Handle->mStatus == SQLITE_DONE)
|
2016-02-27 10:57:29 +01:00
|
|
|
{
|
|
|
|
// Specify that we finished stepping
|
|
|
|
m_Handle->mDone = true;
|
|
|
|
// Specify that we don't have a row available
|
|
|
|
return (m_Handle->mGood = false);
|
|
|
|
}
|
|
|
|
// Specify that we don't have any row and we haven't finished stepping
|
|
|
|
m_Handle->mGood = false;
|
|
|
|
m_Handle->mDone = false;
|
|
|
|
// Inspect the result
|
|
|
|
switch (m_Handle->mStatus)
|
|
|
|
{
|
2016-03-22 23:25:32 +01:00
|
|
|
case SQLITE_BUSY: STHROWF("Database was busy");
|
|
|
|
case SQLITE_ERROR: STHROWF("Runtime error occurred");
|
|
|
|
case SQLITE_MISUSE: STHROWF("Statement misuse");
|
2016-07-10 15:19:56 +02:00
|
|
|
default: STHROWF("Unknown failure [%s]", m_Handle->ErrStr());
|
2016-02-27 10:57:29 +01:00
|
|
|
}
|
2016-02-27 16:53:12 +01:00
|
|
|
// Operation failed (shouldn't reach this point!)
|
2016-02-27 10:57:29 +01:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
2016-07-10 02:00:33 +02:00
|
|
|
Statement & Statement::SetArray(Int32 idx, const Array & arr)
|
2016-06-15 22:49:25 +02:00
|
|
|
{
|
|
|
|
// Obtain a script iterator
|
|
|
|
Array::iterator itr;
|
2016-07-10 02:00:33 +02:00
|
|
|
// Create a parameter instance to bind the values
|
|
|
|
Parameter param(m_Handle);
|
2016-06-15 22:49:25 +02:00
|
|
|
// Process each element until _next returns null
|
|
|
|
while (idx <= m_Handle->mParameters && arr.Next(itr))
|
2016-02-27 10:57:29 +01:00
|
|
|
{
|
2016-07-10 02:00:33 +02:00
|
|
|
// Update the parameter index
|
|
|
|
param.SetIndex(idx++);
|
|
|
|
// Bind it to the current index
|
|
|
|
param.SetValue(Object(itr.getValue()));
|
2016-03-18 11:58:02 +01:00
|
|
|
}
|
2016-06-15 22:49:25 +02:00
|
|
|
// Allow further chaining of operations
|
|
|
|
return *this;
|
2016-02-27 10:57:29 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
2016-07-10 02:00:33 +02:00
|
|
|
Statement & Statement::SetTable(const Table & tbl)
|
2016-02-27 10:57:29 +01:00
|
|
|
{
|
2016-06-15 22:49:25 +02:00
|
|
|
// Is there anything to bind?
|
2016-02-27 16:53:12 +01:00
|
|
|
if (tbl.GetSize() <= 0)
|
2016-03-18 11:58:02 +01:00
|
|
|
{
|
2016-06-15 22:49:25 +02:00
|
|
|
return *this; // Nothing to bind!
|
2016-03-18 11:58:02 +01:00
|
|
|
}
|
2016-06-15 22:49:25 +02:00
|
|
|
// Obtain a table iterator
|
|
|
|
Table::iterator itr;
|
2016-07-10 02:00:33 +02:00
|
|
|
// Create a parameter instance to bind the values
|
|
|
|
Parameter param(m_Handle);
|
2016-06-15 22:49:25 +02:00
|
|
|
// Process each element until _next returns null
|
|
|
|
while (tbl.Next(itr))
|
2016-02-27 10:57:29 +01:00
|
|
|
{
|
2016-07-10 02:00:33 +02:00
|
|
|
// Update the parameter index
|
|
|
|
param.SetIndex(itr.getName());
|
|
|
|
// Bind it to the current index
|
|
|
|
param.SetValue(Object(itr.getValue()));
|
2016-03-18 11:58:02 +01:00
|
|
|
}
|
2016-06-15 22:49:25 +02:00
|
|
|
// Allow further chaining of operations
|
|
|
|
return *this;
|
2016-02-27 10:57:29 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
2016-07-10 02:00:33 +02:00
|
|
|
Array Statement::GetArray(Int32 min, Int32 max) const
|
2016-02-27 10:57:29 +01:00
|
|
|
{
|
2016-07-10 02:00:33 +02:00
|
|
|
SQMOD_VALIDATE_ROW(*this);
|
2016-08-26 17:22:45 +02:00
|
|
|
// Is the specified minimum index valid?
|
|
|
|
if (min < 0)
|
2016-03-18 11:58:02 +01:00
|
|
|
{
|
2016-08-26 17:22:45 +02:00
|
|
|
STHROWF("Minimum is bellow zero: %d", min);
|
2016-03-18 11:58:02 +01:00
|
|
|
}
|
2016-02-27 10:57:29 +01:00
|
|
|
// Is the minimum actually the minimum?
|
|
|
|
else if (min > max)
|
2016-03-18 11:58:02 +01:00
|
|
|
{
|
2016-06-15 22:49:25 +02:00
|
|
|
STHROWF("Minimum is higher than maximum: %d > %d", min, max);
|
2016-03-18 11:58:02 +01:00
|
|
|
}
|
2016-08-26 17:22:45 +02:00
|
|
|
// Is the minimum in range?
|
2016-06-15 22:49:25 +02:00
|
|
|
else if (!m_Handle->CheckColumn(min))
|
2016-03-18 11:58:02 +01:00
|
|
|
{
|
2016-06-15 22:49:25 +02:00
|
|
|
STHROWF("Minimum is out of range: %d:%d", min, m_Handle->mColumns);
|
2016-03-18 11:58:02 +01:00
|
|
|
}
|
2016-02-27 10:57:29 +01:00
|
|
|
// Is the maximum in range?
|
2016-06-15 22:49:25 +02:00
|
|
|
else if (!m_Handle->CheckColumn(max))
|
2016-03-18 11:58:02 +01:00
|
|
|
{
|
2016-06-15 22:49:25 +02:00
|
|
|
STHROWF("Maximum is out of range: %d:%d", max, m_Handle->mColumns);
|
2016-03-18 11:58:02 +01:00
|
|
|
}
|
2016-02-27 10:57:29 +01:00
|
|
|
// Allocate an array large enough to hold the values from selected columns
|
2016-03-18 11:58:02 +01:00
|
|
|
Array arr(DefaultVM::Get(), max-min);
|
2016-07-10 02:00:33 +02:00
|
|
|
// Create a column instance to retrieve the values
|
|
|
|
Column column(m_Handle);
|
2016-08-26 17:22:45 +02:00
|
|
|
// Array element counter
|
|
|
|
Int32 elem = 0;
|
2016-02-27 10:57:29 +01:00
|
|
|
// Process the range of selected columns
|
2016-08-26 17:22:45 +02:00
|
|
|
while (min <= max)
|
2016-02-27 10:57:29 +01:00
|
|
|
{
|
2016-07-10 02:00:33 +02:00
|
|
|
// Update the column index
|
2016-08-26 17:22:45 +02:00
|
|
|
column.SetIndex(min++);
|
2016-07-10 02:00:33 +02:00
|
|
|
// Retrieve the column value and bind it to the array
|
2016-08-26 17:22:45 +02:00
|
|
|
arr.SetValue(elem++, column.GetValue());
|
2016-02-27 10:57:29 +01:00
|
|
|
}
|
|
|
|
// Return the resulted array
|
|
|
|
return arr;
|
|
|
|
}
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
2016-07-10 02:00:33 +02:00
|
|
|
Table Statement::GetTable(Int32 min, Int32 max) const
|
2016-02-27 10:57:29 +01:00
|
|
|
{
|
2016-07-10 02:00:33 +02:00
|
|
|
SQMOD_VALIDATE_ROW(*this);
|
2016-08-26 17:22:45 +02:00
|
|
|
// Is the specified minimum index valid?
|
|
|
|
if (min < 0)
|
2016-03-18 11:58:02 +01:00
|
|
|
{
|
2016-08-26 17:22:45 +02:00
|
|
|
STHROWF("Minimum is bellow zero: %d", min);
|
2016-03-18 11:58:02 +01:00
|
|
|
}
|
2016-02-27 10:57:29 +01:00
|
|
|
// Is the minimum actually the minimum?
|
|
|
|
else if (min > max)
|
2016-03-18 11:58:02 +01:00
|
|
|
{
|
2016-06-15 22:49:25 +02:00
|
|
|
STHROWF("Minimum is higher than maximum: %d > %d", min, max);
|
2016-03-18 11:58:02 +01:00
|
|
|
}
|
2016-02-27 10:57:29 +01:00
|
|
|
// Is the minimum in range>
|
2016-06-15 22:49:25 +02:00
|
|
|
else if (!m_Handle->CheckColumn(min))
|
2016-03-18 11:58:02 +01:00
|
|
|
{
|
2016-06-15 22:49:25 +02:00
|
|
|
STHROWF("Minimum is out of range: %d:%d", min, m_Handle->mColumns);
|
2016-03-18 11:58:02 +01:00
|
|
|
}
|
2016-02-27 10:57:29 +01:00
|
|
|
// Is the maximum in range?
|
2016-06-15 22:49:25 +02:00
|
|
|
else if (!m_Handle->CheckColumn(max))
|
2016-03-18 11:58:02 +01:00
|
|
|
{
|
2016-06-15 22:49:25 +02:00
|
|
|
STHROWF("Maximum is out of range: %d:%d", max, m_Handle->mColumns);
|
2016-03-18 11:58:02 +01:00
|
|
|
}
|
2016-02-27 16:53:12 +01:00
|
|
|
// Create a table to hold the selected column values
|
2016-03-18 11:58:02 +01:00
|
|
|
Table tbl(DefaultVM::Get());
|
2016-07-10 02:00:33 +02:00
|
|
|
// Create a column instance to retrieve the values
|
|
|
|
Column column(m_Handle);
|
2016-02-27 10:57:29 +01:00
|
|
|
// Process the range of selected columns
|
2016-08-26 17:22:45 +02:00
|
|
|
while (min <= max)
|
2016-02-27 10:57:29 +01:00
|
|
|
{
|
2016-07-10 02:00:33 +02:00
|
|
|
// Attempt to obtain the column name
|
2016-08-26 17:22:45 +02:00
|
|
|
CSStr name = sqlite3_column_name(m_Handle->mPtr, min);
|
2016-02-27 10:57:29 +01:00
|
|
|
// Validate the obtained name
|
|
|
|
if (!name)
|
2016-04-02 11:11:14 +02:00
|
|
|
{
|
2016-08-26 17:22:45 +02:00
|
|
|
STHROWF("Unable to retrieve name of column (%d)", min);
|
2016-04-02 11:11:14 +02:00
|
|
|
}
|
2016-07-10 02:00:33 +02:00
|
|
|
// Update the column index
|
2016-08-26 17:22:45 +02:00
|
|
|
column.SetIndex(min++);
|
2016-07-10 02:00:33 +02:00
|
|
|
// Retrieve the column value and bind it to the table
|
|
|
|
tbl.SetValue(name, column.GetValue());
|
2016-02-27 10:57:29 +01:00
|
|
|
}
|
|
|
|
// Return the resulted table
|
|
|
|
return tbl;
|
|
|
|
}
|
|
|
|
|
2016-06-15 22:49:25 +02:00
|
|
|
// ================================================================================================
|
|
|
|
void Register_Statement(Table & sqlns)
|
|
|
|
{
|
|
|
|
sqlns.Bind(_SC("Statement"),
|
|
|
|
Class< Statement >(sqlns.GetVM(), _SC("SqSQLiteStatement"))
|
|
|
|
// Constructors
|
|
|
|
.Ctor()
|
|
|
|
.Ctor< const Statement & >()
|
2016-07-10 02:00:33 +02:00
|
|
|
.Ctor< const Connection &, CCStr >()
|
2016-06-15 22:49:25 +02:00
|
|
|
// Meta-methods
|
|
|
|
.Func(_SC("_cmp"), &Statement::Cmp)
|
|
|
|
.SquirrelFunc(_SC("_typename"), &Statement::Typename)
|
|
|
|
.Func(_SC("_tostring"), &Statement::ToString)
|
|
|
|
// Properties
|
|
|
|
.Prop(_SC("IsValid"), &Statement::IsValid)
|
|
|
|
.Prop(_SC("Prepared"), &Statement::IsPrepared)
|
|
|
|
.Prop(_SC("References"), &Statement::GetReferences)
|
|
|
|
.Prop(_SC("Connection"), &Statement::GetConnection)
|
|
|
|
.Prop(_SC("Status"), &Statement::GetStatus)
|
|
|
|
.Prop(_SC("ErrCode"), &Statement::GetErrorCode)
|
|
|
|
.Prop(_SC("ExErrCode"), &Statement::GetExtendedErrorCode)
|
|
|
|
.Prop(_SC("ExtendedErrCode"), &Statement::GetExtendedErrorCode)
|
|
|
|
.Prop(_SC("ErrStr"), &Statement::GetErrStr)
|
|
|
|
.Prop(_SC("ErrMsg"), &Statement::GetErrMsg)
|
|
|
|
.Prop(_SC("Columns"), &Statement::GetColumns)
|
|
|
|
.Prop(_SC("Parameters"), &Statement::GetParameters)
|
|
|
|
.Prop(_SC("Query"), &Statement::GetQuery)
|
|
|
|
.Prop(_SC("Good"), &Statement::GetGood)
|
|
|
|
.Prop(_SC("Done"), &Statement::GetDone)
|
|
|
|
.Prop(_SC("DataCount"), &Statement::GetDataCount)
|
|
|
|
// Member Methods
|
|
|
|
.Func(_SC("Release"), &Statement::Release)
|
|
|
|
.Func(_SC("CheckParameter"), &Statement::CheckParameter)
|
2016-07-10 02:00:33 +02:00
|
|
|
.Func(_SC("GetParameterIndex"), &Statement::GetParameterIndex)
|
|
|
|
.Func(_SC("GetParameterName"), &Statement::GetParameterName)
|
|
|
|
.Func(_SC("CheckColumn"), &Statement::CheckColumn)
|
2016-06-15 22:49:25 +02:00
|
|
|
.Func(_SC("IsColumnNull"), &Statement::IsColumnNull)
|
|
|
|
.Func(_SC("ColumnIndex"), &Statement::GetColumnIndex)
|
|
|
|
.Func(_SC("ColumnName"), &Statement::GetColumnName)
|
|
|
|
.Func(_SC("ColumnOriginName"), &Statement::GetColumnOriginName)
|
|
|
|
.Func(_SC("ColumnType"), &Statement::GetColumnType)
|
|
|
|
.Func(_SC("ColumnBytes"), &Statement::GetColumnBytes)
|
2016-07-10 02:00:33 +02:00
|
|
|
.Func(_SC("Reset"), &Statement::Reset)
|
|
|
|
.Func(_SC("Clear"), &Statement::Clear)
|
|
|
|
.Func(_SC("Exec"), &Statement::Exec)
|
|
|
|
.Func(_SC("Step"), &Statement::Step)
|
|
|
|
.Func(_SC("Param"), &Statement::GetParameter)
|
|
|
|
.Func(_SC("Parameter"), &Statement::GetParameter)
|
|
|
|
.Func(_SC("SetValue"), &Statement::SetValue)
|
|
|
|
.Func(_SC("SetBool"), &Statement::SetBool)
|
|
|
|
.Func(_SC("SetChar"), &Statement::SetChar)
|
|
|
|
.Func(_SC("SetInteger"), &Statement::SetInteger)
|
|
|
|
.Func(_SC("SetInt8"), &Statement::SetInt8)
|
|
|
|
.Func(_SC("SetUint8"), &Statement::SetUint8)
|
|
|
|
.Func(_SC("SetInt16"), &Statement::SetInt16)
|
|
|
|
.Func(_SC("SetUint16"), &Statement::SetUint16)
|
|
|
|
.Func(_SC("SetInt32"), &Statement::SetInt32)
|
|
|
|
.Func(_SC("SetUint32"), &Statement::SetUint32)
|
|
|
|
.Func(_SC("SetInt64"), &Statement::SetInt64)
|
|
|
|
.Func(_SC("SetUint64"), &Statement::SetUint64)
|
|
|
|
.Func(_SC("SetFloat"), &Statement::SetFloat)
|
|
|
|
.Func(_SC("SetFloat32"), &Statement::SetFloat32)
|
|
|
|
.Func(_SC("SetFloat64"), &Statement::SetFloat64)
|
|
|
|
.Func(_SC("SetString"), &Statement::SetString)
|
|
|
|
.Func(_SC("SetZeroBlob"), &Statement::SetZeroBlob)
|
|
|
|
.Func(_SC("SetBlob"), &Statement::SetBlob)
|
|
|
|
.Func(_SC("SetData"), &Statement::SetData)
|
|
|
|
.Func(_SC("SetDate"), &Statement::SetDate)
|
|
|
|
.Func(_SC("SetDateEx"), &Statement::SetDateEx)
|
|
|
|
.Func(_SC("SetTime"), &Statement::SetTime)
|
|
|
|
.Func(_SC("SetTimeEx"), &Statement::SetTimeEx)
|
|
|
|
.Func(_SC("SetDatetime"), &Statement::SetDatetime)
|
|
|
|
.Func(_SC("SetDatetimeEx"), &Statement::SetDatetimeEx)
|
|
|
|
.Func(_SC("SetNow"), &Statement::SetNow)
|
|
|
|
.Func(_SC("SetNull"), &Statement::SetNull)
|
|
|
|
.Func(_SC("SetArray"), &Statement::SetArray)
|
|
|
|
.Func(_SC("SetTable"), &Statement::SetTable)
|
|
|
|
.Func(_SC("Field"), &Statement::GetColumn)
|
|
|
|
.Func(_SC("Column"), &Statement::GetColumn)
|
|
|
|
.Func(_SC("GetValue"), &Statement::GetValue)
|
|
|
|
.Func(_SC("GetNumber"), &Statement::GetNumber)
|
|
|
|
.Func(_SC("GetInteger"), &Statement::GetInteger)
|
|
|
|
.Func(_SC("GetFloat"), &Statement::GetFloat)
|
|
|
|
.Func(_SC("GetLong"), &Statement::GetLong)
|
|
|
|
.Func(_SC("GetString"), &Statement::GetString)
|
|
|
|
.Func(_SC("GetBoolean"), &Statement::GetBoolean)
|
|
|
|
.Func(_SC("GetChar"), &Statement::GetChar)
|
|
|
|
.Func(_SC("GetBuffer"), &Statement::GetBuffer)
|
|
|
|
.Func(_SC("GetBlob"), &Statement::GetBlob)
|
2016-06-15 22:49:25 +02:00
|
|
|
// Member overloads
|
2016-07-10 02:00:33 +02:00
|
|
|
.Overload< Array (Statement::*)(void) const >(_SC("GetArray"), &Statement::GetArray)
|
|
|
|
.Overload< Array (Statement::*)(Int32) const >(_SC("GetArray"), &Statement::GetArray)
|
|
|
|
.Overload< Array (Statement::*)(Int32, Int32) const >(_SC("GetArray"), &Statement::GetArray)
|
|
|
|
.Overload< Table (Statement::*)(void) const >(_SC("GetTable"), &Statement::GetTable)
|
|
|
|
.Overload< Table (Statement::*)(Int32) const >(_SC("GetTable"), &Statement::GetTable)
|
|
|
|
.Overload< Table (Statement::*)(Int32, Int32) const >(_SC("GetTable"), &Statement::GetTable)
|
2016-06-15 22:49:25 +02:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2016-02-27 10:57:29 +01:00
|
|
|
} // Namespace:: SqMod
|