1
0
mirror of https://github.com/VCMP-SqMod/SqMod.git synced 2025-01-18 19:47:15 +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="ModINI.cbp" />
<Project filename="ModIRC.cbp" /> <Project filename="ModIRC.cbp" />
<Project filename="ModXML.cbp" /> <Project filename="ModXML.cbp" />
<Project filename="ModSQLite.cbp" />
<Project filename="ModSample.cbp" /> <Project filename="ModSample.cbp" />
</Workspace> </Workspace>
</CodeBlocks_workspace_file> </CodeBlocks_workspace_file>

View File

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

View File

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

View File

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

View File

@ -40,6 +40,12 @@ Connection::Connection(CSStr name)
{ {
if (m_Handle.m_Hnd) if (m_Handle.m_Hnd)
m_Handle->Create(name, SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE, NULL); 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) if (m_Handle.m_Hnd)
m_Handle->Create(name, flags, NULL); 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) if (m_Handle.m_Hnd)
m_Handle->Create(name, flags, vfs); 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 // Validate the handle
if (Validate()) if (Validate())
return Statement(m_Handle, str); return Object(new Statement(m_Handle, str));
// Request failed // Request failed
return Statement(); return Object(new Statement());
} }
// ------------------------------------------------------------------------------------------------ // ------------------------------------------------------------------------------------------------

View File

@ -309,7 +309,7 @@ public:
/* -------------------------------------------------------------------------------------------- /* --------------------------------------------------------------------------------------------
* Attempt to create a statement from the specified query. * 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. * See if the database connection was opened in read-only mode.

View File

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

View File

@ -75,30 +75,43 @@ Statement::Statement()
Statement::Statement(const ConnHnd & connection, CSStr query) Statement::Statement(const ConnHnd & connection, CSStr query)
: m_Handle(connection) : m_Handle(connection)
{ {
if (m_Handle) if (m_Handle.m_Hnd)
m_Handle->Create(query); m_Handle->Create(query);
else else
_SqMod->SqThrow("Unable to create the statement reference"); _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) Statement::Statement(const Connection & connection, CSStr query)
: m_Handle(connection.GetHandle()) : m_Handle(connection.GetHandle())
{ {
if (m_Handle) // Validate the statement handle
if (m_Handle.m_Hnd)
m_Handle->Create(query); m_Handle->Create(query);
else else
_SqMod->SqThrow("Unable to create the statement reference"); _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 // Validate the handle
if (Validate()) if (Validate())
return Connection(m_Handle->mConn); return Object(new Connection(m_Handle->mConn));
// Request failed // 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? // Can we make the request?
if (ValidateIndex(idx)) if (ValidateIndex(idx))
return Column(m_Handle, idx); return Object(new Column(m_Handle, idx));
// Request failed // Request failed
return Column(); return Object(new Column());
} }
// ------------------------------------------------------------------------------------------------ // ------------------------------------------------------------------------------------------------
Column Statement::GetColumnByName(CSStr name) const Object Statement::GetColumnByName(CSStr name) const
{ {
// Validate the handle // Validate the handle
if (Validate()) if (Validate())
@ -1130,10 +1143,10 @@ Column Statement::GetColumnByName(CSStr name) const
_SqMod->SqThrow("Unknown column named (%s)", name); _SqMod->SqThrow("Unknown column named (%s)", name);
// Return the requested column // Return the requested column
else else
return Column(m_Handle, idx); return Object(new Column(m_Handle, idx));
} }
// Request failed // Request failed
return Column(); return Object(new Column());
} }
} // Namespace:: SqMod } // Namespace:: SqMod

View File

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