From eb54cbb4f5cf42a4c54656812c5a90cb61c2ce2d Mon Sep 17 00:00:00 2001 From: David Anderson Date: Fri, 1 Jun 2007 04:22:53 +0000 Subject: [PATCH] fixed various bugs in the sql natives and manager added a basic test plugin --HG-- extra : convert_revision : svn%3A39bc706e-5318-0410-9160-8a85361fbb7c/trunk%40875 --- core/Database.cpp | 10 ++-- core/smn_database.cpp | 2 +- plugins/include/dbi.inc | 10 ++++ plugins/testsuite/sqltest.sp | 108 ++++++++++++++++++++++++++++++++++ plugins/testsuite/sqltest.sql | 7 +++ 5 files changed, 132 insertions(+), 5 deletions(-) create mode 100644 plugins/testsuite/sqltest.sp create mode 100644 plugins/testsuite/sqltest.sql diff --git a/core/Database.cpp b/core/Database.cpp index d0ffbac7..667c9d7b 100644 --- a/core/Database.cpp +++ b/core/Database.cpp @@ -123,7 +123,7 @@ SMCParseResult DBManager::ReadSMC_NewSection(const char *name, bool opt_quotes) } } else if (m_ParseState == DBPARSE_LEVEL_MAIN) { s_CurInfo = ConfDbInfo(); - s_CurInfo.database = m_StrTab.AddString(name); + s_CurInfo.name = m_StrTab.AddString(name); m_ParseState = DBPARSE_LEVEL_DATABASE; } else if (m_ParseState == DBPARSE_LEVEL_DATABASE) { m_ParseLevel++; @@ -229,6 +229,7 @@ bool DBManager::Connect(const char *name, IDBDriver **pdr, IDatabase **pdb, bool return false; } + const char *dname = pInfo->info.driver; if (!pInfo->realDriver) { /* Try to assign a real driver pointer */ @@ -238,6 +239,7 @@ bool DBManager::Connect(const char *name, IDBDriver **pdr, IDatabase **pdb, bool { m_pDefault = FindOrLoadDriver(m_DefDriver.c_str()); } + dname = m_DefDriver.size() ? m_DefDriver.c_str() : "default"; pInfo->realDriver = m_pDefault; } else { pInfo->realDriver = FindOrLoadDriver(pInfo->info.driver); @@ -260,7 +262,7 @@ bool DBManager::Connect(const char *name, IDBDriver **pdr, IDatabase **pdb, bool } *pdb = NULL; - UTIL_Format(error, maxlength, "Driver \"%s\" not found", pInfo->driver); + UTIL_Format(error, maxlength, "Driver \"%s\" not found", dname); return false; } @@ -334,7 +336,7 @@ HandleError DBManager::ReadHandle(Handle_t hndl, DBHandleType dtype, void **ptr) HandleSecurity sec(NULL, g_pCoreIdent); - return g_HandleSys.ReadHandle(hndl, dtype, &sec, ptr); + return g_HandleSys.ReadHandle(hndl, type, &sec, ptr); } HandleError DBManager::ReleaseHandle(Handle_t hndl, DBHandleType type, IdentityToken_t *token) @@ -397,7 +399,7 @@ IDBDriver *DBManager::FindOrLoadDriver(const char *name) } char filename[PLATFORM_MAX_PATH]; - UTIL_Format(filename, sizeof(filename), "dbi.%s", name); + UTIL_Format(filename, sizeof(filename), "dbi.%s.ext", name); IExtension *pExt = g_Extensions.LoadAutoExtension(filename); if (!pExt || !pExt->IsLoaded() || m_drivers.size() <= last_size) diff --git a/core/smn_database.cpp b/core/smn_database.cpp index d1b8c84b..95cc79a3 100644 --- a/core/smn_database.cpp +++ b/core/smn_database.cpp @@ -26,7 +26,7 @@ public: tacc.ident = g_pCoreIdent; hQueryType = g_HandleSys.CreateType("IQuery", this, 0, &tacc, &acc, g_pCoreIdent, NULL); - hStmtType = g_HandleSys.CreateType("IPreparedQuery", this, hQueryType, &tacc, &acc, NULL, NULL); + hStmtType = g_HandleSys.CreateType("IPreparedQuery", this, hQueryType, &tacc, &acc, g_pCoreIdent, NULL); } virtual void OnSourceModShutdown() diff --git a/plugins/include/dbi.inc b/plugins/include/dbi.inc index 3393e5e0..52ea0fd2 100644 --- a/plugins/include/dbi.inc +++ b/plugins/include/dbi.inc @@ -260,10 +260,20 @@ native bool:SQL_HasResultSet(Handle:query); * Retrieves the number of rows in the last result set. * * @param query A query (or statement) Handle. + * @return Number of rows in the current result set. * @error Invalid query Handle. */ native SQL_GetRowCount(Handle:query); +/** + * Retrieves the number of fields in the last result set. + * + * @param query A query (or statement) Handle. + * @return Number of fields in the current result set. + * @error Invalid query Handle. + */ +native SQL_GetFieldCount(Handle:query); + /** * Retrieves the name of a field by index. * diff --git a/plugins/testsuite/sqltest.sp b/plugins/testsuite/sqltest.sp new file mode 100644 index 00000000..22dd092b --- /dev/null +++ b/plugins/testsuite/sqltest.sp @@ -0,0 +1,108 @@ +#include + +public Plugin:myinfo = +{ + name = "SQL Testing Lab", + author = "AlliedModders LLC", + description = "Tests basic function calls", + version = "1.0.0.0", + url = "http://www.sourcemod.net/" +}; + +public OnPluginStart() +{ + RegServerCmd("sql_test_normal", Command_TestSql1) + RegServerCmd("sql_test_stmt", Command_TestSql2) +} + +PrintQueryData(Handle:query) +{ + if (!SQL_HasResultSet(query)) + { + PrintToServer("Query Handle %x has no results", query) + return + } + + new rows = SQL_GetRowCount(query) + new fields = SQL_GetFieldCount(query) + + decl String:fieldNames[fields][32] + PrintToServer("Fields: %d", fields) + for (new i=0; i Field %d: \"%s\"", i, fieldNames[i]) + } + + PrintToServer("Rows: %d", rows) + decl String:result[255] + new row + while (SQL_FetchRow(query)) + { + row++ + PrintToServer("Row %d:", row) + for (new i=0; i ?", error, sizeof(error)) + if (stmt == INVALID_HANDLE) + { + PrintToServer("Failed to prepare query: %s", error) + } else { + SQL_BindParamInt(stmt, 0, 1) + if (!SQL_Execute(stmt)) + { + SQL_GetError(stmt, error, sizeof(error)) + PrintToServer("Failed to execute query: %s", error) + } else { + PrintQueryData(stmt) + } + CloseHandle(stmt) + } + + CloseHandle(db) + + return Plugin_Handled; +} + + diff --git a/plugins/testsuite/sqltest.sql b/plugins/testsuite/sqltest.sql new file mode 100644 index 00000000..a58de9e8 --- /dev/null +++ b/plugins/testsuite/sqltest.sql @@ -0,0 +1,7 @@ + +CREATE TABLE gaben (gaben int primary key, fat varchar(32)); + +INSERT INTO gaben VALUES(1, 'what the'); +INSERT INTO gaben VALUES(2, 'Bee''s Knees!'); +INSERT INTO gaben VALUES(3, 'newell'); +INSERT INTO gaben VALUES(4, 'CRAB CAKE.');