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
This commit is contained in:
David Anderson 2007-06-01 04:22:53 +00:00
parent e334e5f7e7
commit eb54cbb4f5
5 changed files with 132 additions and 5 deletions

View File

@ -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)

View File

@ -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()

View File

@ -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.
*

View File

@ -0,0 +1,108 @@
#include <sourcemod>
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<fields; i++)
{
SQL_FieldNumToName(query, i, fieldNames[i], 32)
PrintToServer("-> 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<fields; i++)
{
SQL_FetchString(query, i, result, sizeof(result))
PrintToServer(" [%s] %s", fieldNames[i], result)
}
}
}
public Action:Command_TestSql1(args)
{
new String:error[255]
new Handle:db = SQL_DefConnect(error, sizeof(error))
if (db == INVALID_HANDLE)
{
PrintToServer("Failed to connect: %s", error)
return Plugin_Handled
}
new Handle:query = SQL_Query(db, "SELECT * FROM gaben")
if (query == INVALID_HANDLE)
{
SQL_GetError(db, error, sizeof(error))
PrintToServer("Failed to query: %s", error)
} else {
PrintQueryData(query)
CloseHandle(query)
}
CloseHandle(db)
return Plugin_Handled;
}
public Action:Command_TestSql2(args)
{
new String:error[255]
new Handle:db = SQL_DefConnect(error, sizeof(error))
if (db == INVALID_HANDLE)
{
PrintToServer("Failed to connect: %s", error)
return Plugin_Handled
}
new Handle:stmt = SQL_PrepareQuery(db, "SELECT * FROM gaben WHERE gaben > ?", 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;
}

View File

@ -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.');