1
0
mirror of https://github.com/VCMP-SqMod/SqMod.git synced 2024-11-08 08:47:17 +01:00

Fixed various issues with Sqrat thinking the type wasn't registered if an error is thrown in the constructor.

Fixed asserts in connection and statement handles to check the correct property.
Switched various methods to return objects instead of direct types.
Various other fixes and improvements on the SQLite module.
This commit is contained in:
Sandu Liviu Catalin 2016-02-27 13:51:14 +02:00
parent 6e7abfc354
commit a867bfd84d
9 changed files with 78 additions and 46 deletions

View File

@ -7,6 +7,7 @@
<Project filename="ModINI.cbp" />
<Project filename="ModIRC.cbp" />
<Project filename="ModXML.cbp" />
<Project filename="ModSQLite.cbp" />
<Project filename="ModSample.cbp" />
</Workspace>
</CodeBlocks_workspace_file>

View File

@ -54,23 +54,23 @@ bool Column::RowAvailable() const
}
// ------------------------------------------------------------------------------------------------
Statement Column::GetStatement() const
Object Column::GetStatement() const
{
// Validate the handle
if (Validate())
return Statement(m_Stmt);
return Object(new Statement(m_Stmt));
// Request failed
return Statement();
return Object(new Statement());
}
// ------------------------------------------------------------------------------------------------
Connection Column::GetConnection() const
Object Column::GetConnection() const
{
// Validate the handle
if (Validate())
return Connection(m_Stmt->mConn);
return Object(new Connection(m_Stmt->mConn));
// Request failed
return Connection();
return Object(new Connection());
}
// ------------------------------------------------------------------------------------------------
@ -129,7 +129,7 @@ Object Column::GetLong() const
Object Column::GetString() const
{
// Validate the handle and index
if (RowAvailable())
if (!RowAvailable())
return Object(); // Request failed
// Obtain the initial stack size
const Int32 top = sq_gettop(_SqVM);

View File

@ -158,12 +158,12 @@ public:
/* --------------------------------------------------------------------------------------------
* Retrieve the associated database statement.
*/
Statement GetStatement() const;
Object GetStatement() const;
/* --------------------------------------------------------------------------------------------
* Retrieve the associated database connection.
*/
Connection GetConnection() const;
Object GetConnection() const;
/* --------------------------------------------------------------------------------------------
* Release the reference to the associated database statement and index.

View File

@ -268,7 +268,7 @@ public:
*/
operator bool () const
{
return !m_Hnd || !(m_Hnd->mPtr);
return m_Hnd && m_Hnd->mPtr;
}
/* --------------------------------------------------------------------------------------------
@ -310,7 +310,7 @@ public:
*/
Handle * operator -> () const
{
assert(m_Ptr);
assert(m_Hnd);
return m_Hnd;
}
@ -319,7 +319,7 @@ public:
*/
Handle & operator * () const
{
assert(m_Ptr);
assert(m_Hnd);
return *m_Hnd;
}
@ -344,7 +344,7 @@ public:
*/
Counter Count() const
{
assert(m_Ptr);
assert(m_Hnd);
return m_Hnd ? m_Hnd->mRef : 0;
}
@ -353,7 +353,7 @@ public:
*/
CCStr ErrStr() const
{
assert(m_Ptr); // SQLite does it's null pointer validations internally
assert(m_Hnd); // SQLite does it's null pointer validations internally
return sqlite3_errstr(sqlite3_errcode(m_Hnd->mPtr));
}
@ -362,7 +362,7 @@ public:
*/
CCStr ErrMsg() const
{
assert(m_Ptr); // SQLite does it's null pointer validations internally
assert(m_Hnd); // SQLite does it's null pointer validations internally
return sqlite3_errmsg(m_Hnd->mPtr);
}
@ -371,7 +371,7 @@ public:
*/
Int32 ErrNo() const
{
assert(m_Ptr); // SQLite does it's null pointer validations internally
assert(m_Hnd); // SQLite does it's null pointer validations internally
return sqlite3_errcode(m_Hnd->mPtr);
}
@ -380,7 +380,7 @@ public:
*/
Int32 ExErrNo() const
{
assert(m_Ptr); // SQLite does it's null pointer validations internally
assert(m_Hnd); // SQLite does it's null pointer validations internally
return sqlite3_extended_errcode(m_Hnd->mPtr);
}
};
@ -608,7 +608,7 @@ public:
*/
operator bool () const
{
return !m_Hnd || !(m_Hnd->mPtr);
return m_Hnd && m_Hnd->mPtr;
}
/* --------------------------------------------------------------------------------------------
@ -650,7 +650,7 @@ public:
*/
Handle * operator -> () const
{
assert(m_Ptr);
assert(m_Hnd);
return m_Hnd;
}
@ -659,7 +659,7 @@ public:
*/
Handle & operator * () const
{
assert(m_Ptr);
assert(m_Hnd);
return *m_Hnd;
}
@ -684,7 +684,7 @@ public:
*/
Counter Count() const
{
assert(m_Ptr);
assert(m_Hnd);
return m_Hnd ? m_Hnd->mRef : 0;
}
@ -693,7 +693,7 @@ public:
*/
CCStr ErrStr() const
{
assert(m_Ptr); // SQLite does it's null pointer validations internally
assert(m_Hnd); // SQLite does it's null pointer validations internally
return m_Hnd->mConn.ErrStr();
}
@ -702,7 +702,7 @@ public:
*/
CCStr ErrMsg() const
{
assert(m_Ptr); // SQLite does it's null pointer validations internally
assert(m_Hnd); // SQLite does it's null pointer validations internally
return m_Hnd->mConn.ErrMsg();
}
@ -711,7 +711,7 @@ public:
*/
Int32 ErrNo() const
{
assert(m_Ptr); // SQLite does it's null pointer validations internally
assert(m_Hnd); // SQLite does it's null pointer validations internally
return m_Hnd->mConn.ErrNo();
}
@ -720,7 +720,7 @@ public:
*/
Int32 ExErrNo() const
{
assert(m_Ptr); // SQLite does it's null pointer validations internally
assert(m_Hnd); // SQLite does it's null pointer validations internally
return m_Hnd->mConn.ExErrNo();
}
};

View File

@ -40,6 +40,12 @@ Connection::Connection(CSStr name)
{
if (m_Handle.m_Hnd)
m_Handle->Create(name, SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE, NULL);
// Because Sqrat is majorly stupid and clears the error message
// then does an assert on debug builds thinking the type wasn't registered
// or throws a generic "unknown error" message on release builds
// we have to use this approach
if (Sqrat::Error::Occurred(_SqVM))
_SqMod->LogErr("%s", Sqrat::Error::Message(_SqVM).c_str());
}
// ------------------------------------------------------------------------------------------------
@ -48,6 +54,12 @@ Connection::Connection(CSStr name, Int32 flags)
{
if (m_Handle.m_Hnd)
m_Handle->Create(name, flags, NULL);
// Because Sqrat is majorly stupid and clears the error message
// then does an assert on debug builds thinking the type wasn't registered
// or throws a generic "unknown error" message on release builds
// we have to use this approach
if (Sqrat::Error::Occurred(_SqVM))
_SqMod->LogErr("%s", Sqrat::Error::Message(_SqVM).c_str());
}
// ------------------------------------------------------------------------------------------------
@ -56,6 +68,12 @@ Connection::Connection(CSStr name, Int32 flags, CSStr vfs)
{
if (m_Handle.m_Hnd)
m_Handle->Create(name, flags, vfs);
// Because Sqrat is majorly stupid and clears the error message
// then does an assert on debug builds thinking the type wasn't registered
// or throws a generic "unknown error" message on release builds
// we have to use this approach
if (Sqrat::Error::Occurred(_SqVM))
_SqMod->LogErr("%s", Sqrat::Error::Message(_SqVM).c_str());
}
// ------------------------------------------------------------------------------------------------
@ -72,13 +90,13 @@ Int32 Connection::Exec(CSStr str)
}
// ------------------------------------------------------------------------------------------------
Statement Connection::Query(CSStr str) const
Object Connection::Query(CSStr str) const
{
// Validate the handle
if (Validate())
return Statement(m_Handle, str);
return Object(new Statement(m_Handle, str));
// Request failed
return Statement();
return Object(new Statement());
}
// ------------------------------------------------------------------------------------------------

View File

@ -309,7 +309,7 @@ public:
/* --------------------------------------------------------------------------------------------
* Attempt to create a statement from the specified query.
*/
Statement Query(CSStr str) const;
Object Query(CSStr str) const;
/* --------------------------------------------------------------------------------------------
* See if the database connection was opened in read-only mode.

View File

@ -239,7 +239,7 @@ void RegisterAPI(HSQUIRRELVM vm)
/* Properties */
.Prop(_SC("Valid"), &Statement::IsValid)
.Prop(_SC("Refs"), &Statement::GetRefCount)
.Prop(_SC("Connection"), &Statement::GetConnection)
.Prop(_SC("Conn"), &Statement::GetConnection)
.Prop(_SC("Prepared"), &Statement::IsValid)
.Prop(_SC("Status"), &Statement::GetStatus)
.Prop(_SC("ErrCode"), &Statement::GetErrorCode)
@ -306,8 +306,8 @@ void RegisterAPI(HSQUIRRELVM vm)
.Prop(_SC("Valid"), &Column::IsValid)
.Prop(_SC("Refs"), &Column::GetRefCount)
.Prop(_SC("Index"), &Column::GetIndex)
.Prop(_SC("Statement"), &Column::GetNumber)
.Prop(_SC("Connection"), &Column::GetConnection)
.Prop(_SC("Stmt"), &Column::GetNumber)
.Prop(_SC("Conn"), &Column::GetConnection)
.Prop(_SC("Number"), &Column::GetNumber)
.Prop(_SC("Integer"), &Column::GetInteger)
.Prop(_SC("Float"), &Column::GetFloat)

View File

@ -75,30 +75,43 @@ Statement::Statement()
Statement::Statement(const ConnHnd & connection, CSStr query)
: m_Handle(connection)
{
if (m_Handle)
if (m_Handle.m_Hnd)
m_Handle->Create(query);
else
_SqMod->SqThrow("Unable to create the statement reference");
// Because Sqrat is majorly stupid and clears the error message
// then does an assert on debug builds thinking the type wasn't registered
// or throws a generic "unknown error" message on release builds
// we have to use this approach
if (Sqrat::Error::Occurred(_SqVM))
_SqMod->LogErr("%s", Sqrat::Error::Message(_SqVM).c_str());
}
// ------------------------------------------------------------------------------------------------
Statement::Statement(const Connection & connection, CSStr query)
: m_Handle(connection.GetHandle())
{
if (m_Handle)
// Validate the statement handle
if (m_Handle.m_Hnd)
m_Handle->Create(query);
else
_SqMod->SqThrow("Unable to create the statement reference");
// Because Sqrat is majorly stupid and clears the error message
// then does an assert on debug builds thinking the type wasn't registered
// or throws a generic "unknown error" message on release builds
// we have to use this approach
if (Sqrat::Error::Occurred(_SqVM))
_SqMod->LogErr("%s", Sqrat::Error::Message(_SqVM).c_str());
}
// ------------------------------------------------------------------------------------------------
Connection Statement::GetConnection() const
Object Statement::GetConnection() const
{
// Validate the handle
if (Validate())
return Connection(m_Handle->mConn);
return Object(new Connection(m_Handle->mConn));
// Request failed
return Connection();
return Object(new Connection());
}
// ------------------------------------------------------------------------------------------------
@ -1108,17 +1121,17 @@ Int32 Statement::GetColumnBytes(Int32 idx) const
}
// ------------------------------------------------------------------------------------------------
Column Statement::GetColumnByIndex(Int32 idx) const
Object Statement::GetColumnByIndex(Int32 idx) const
{
// Can we make the request?
if (ValidateIndex(idx))
return Column(m_Handle, idx);
return Object(new Column(m_Handle, idx));
// Request failed
return Column();
return Object(new Column());
}
// ------------------------------------------------------------------------------------------------
Column Statement::GetColumnByName(CSStr name) const
Object Statement::GetColumnByName(CSStr name) const
{
// Validate the handle
if (Validate())
@ -1130,10 +1143,10 @@ Column Statement::GetColumnByName(CSStr name) const
_SqMod->SqThrow("Unknown column named (%s)", name);
// Return the requested column
else
return Column(m_Handle, idx);
return Object(new Column(m_Handle, idx));
}
// Request failed
return Column();
return Object(new Column());
}
} // Namespace:: SqMod

View File

@ -180,7 +180,7 @@ public:
/* --------------------------------------------------------------------------------------------
* Retrieve the associated database connection.
*/
Connection GetConnection() const;
Object GetConnection() const;
/* --------------------------------------------------------------------------------------------
* Release the reference to the associated database statement.
@ -486,12 +486,12 @@ public:
/* --------------------------------------------------------------------------------------------
* Retrieve the column with the specified index.
*/
Column GetColumnByIndex(Int32 idx) const;
Object GetColumnByIndex(Int32 idx) const;
/* --------------------------------------------------------------------------------------------
* Retrieve the column with the specified name.
*/
Column GetColumnByName(CSStr name) const;
Object GetColumnByName(CSStr name) const;
};
} // Namespace:: SqMod