fixed various bugs in the sql natives and manager
authorDavid Anderson <dvander@alliedmods.net>
Fri Jun 01 04:22:53 2007 +0000 (2007-06-01)
changeset 869caa5587e708a
parent 868 bdbf922e246f
child 870 63d48d198340
fixed various bugs in the sql natives and manager
added a basic test plugin
core/Database.cpp
core/smn_database.cpp
plugins/include/dbi.inc
plugins/testsuite/sqltest.sp
plugins/testsuite/sqltest.sql
     1.1 --- a/core/Database.cpp	Fri Jun 01 02:30:29 2007 +0000
     1.2 +++ b/core/Database.cpp	Fri Jun 01 04:22:53 2007 +0000
     1.3 @@ -123,7 +123,7 @@
     1.4  		}
     1.5  	} else if (m_ParseState == DBPARSE_LEVEL_MAIN) {
     1.6  		s_CurInfo = ConfDbInfo();
     1.7 -		s_CurInfo.database = m_StrTab.AddString(name);
     1.8 +		s_CurInfo.name = m_StrTab.AddString(name);
     1.9  		m_ParseState = DBPARSE_LEVEL_DATABASE;
    1.10  	} else if (m_ParseState == DBPARSE_LEVEL_DATABASE) {
    1.11  		m_ParseLevel++;
    1.12 @@ -229,6 +229,7 @@
    1.13  		return false;
    1.14  	}
    1.15  
    1.16 +	const char *dname = pInfo->info.driver;
    1.17  	if (!pInfo->realDriver)
    1.18  	{
    1.19  		/* Try to assign a real driver pointer */
    1.20 @@ -238,6 +239,7 @@
    1.21  			{
    1.22  				m_pDefault = FindOrLoadDriver(m_DefDriver.c_str());
    1.23  			}
    1.24 +			dname = m_DefDriver.size() ? m_DefDriver.c_str() : "default";
    1.25  			pInfo->realDriver = m_pDefault;
    1.26  		} else {
    1.27  			pInfo->realDriver = FindOrLoadDriver(pInfo->info.driver);
    1.28 @@ -260,7 +262,7 @@
    1.29  	}
    1.30  	*pdb = NULL;
    1.31  
    1.32 -	UTIL_Format(error, maxlength, "Driver \"%s\" not found", pInfo->driver);
    1.33 +	UTIL_Format(error, maxlength, "Driver \"%s\" not found", dname);
    1.34  
    1.35  	return false;
    1.36  }
    1.37 @@ -334,7 +336,7 @@
    1.38  
    1.39  	HandleSecurity sec(NULL, g_pCoreIdent);
    1.40  
    1.41 -	return g_HandleSys.ReadHandle(hndl, dtype, &sec, ptr);
    1.42 +	return g_HandleSys.ReadHandle(hndl, type, &sec, ptr);
    1.43  }
    1.44  
    1.45  HandleError DBManager::ReleaseHandle(Handle_t hndl, DBHandleType type, IdentityToken_t *token)
    1.46 @@ -397,7 +399,7 @@
    1.47  	}
    1.48  
    1.49  	char filename[PLATFORM_MAX_PATH];
    1.50 -	UTIL_Format(filename, sizeof(filename), "dbi.%s", name);
    1.51 +	UTIL_Format(filename, sizeof(filename), "dbi.%s.ext", name);
    1.52  
    1.53  	IExtension *pExt = g_Extensions.LoadAutoExtension(filename);
    1.54  	if (!pExt || !pExt->IsLoaded() || m_drivers.size() <= last_size)
     2.1 --- a/core/smn_database.cpp	Fri Jun 01 02:30:29 2007 +0000
     2.2 +++ b/core/smn_database.cpp	Fri Jun 01 04:22:53 2007 +0000
     2.3 @@ -26,7 +26,7 @@
     2.4  		tacc.ident = g_pCoreIdent;
     2.5  
     2.6  		hQueryType = g_HandleSys.CreateType("IQuery", this, 0, &tacc, &acc, g_pCoreIdent, NULL);
     2.7 -		hStmtType = g_HandleSys.CreateType("IPreparedQuery", this, hQueryType, &tacc, &acc, NULL, NULL);
     2.8 +		hStmtType = g_HandleSys.CreateType("IPreparedQuery", this, hQueryType, &tacc, &acc, g_pCoreIdent, NULL);
     2.9  	}
    2.10  
    2.11  	virtual void OnSourceModShutdown()
     3.1 --- a/plugins/include/dbi.inc	Fri Jun 01 02:30:29 2007 +0000
     3.2 +++ b/plugins/include/dbi.inc	Fri Jun 01 04:22:53 2007 +0000
     3.3 @@ -260,11 +260,21 @@
     3.4   * Retrieves the number of rows in the last result set.
     3.5   * 
     3.6   * @param query			A query (or statement) Handle.
     3.7 + * @return				Number of rows in the current result set.
     3.8   * @error				Invalid query Handle.
     3.9   */
    3.10  native SQL_GetRowCount(Handle:query);
    3.11  
    3.12  /**
    3.13 + * Retrieves the number of fields in the last result set.
    3.14 + * 
    3.15 + * @param query			A query (or statement) Handle.
    3.16 + * @return				Number of fields in the current result set.
    3.17 + * @error				Invalid query Handle.
    3.18 + */
    3.19 +native SQL_GetFieldCount(Handle:query);
    3.20 +
    3.21 +/**
    3.22   * Retrieves the name of a field by index.
    3.23   * 
    3.24   * @param query			A query (or statement) Handle.
     4.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     4.2 +++ b/plugins/testsuite/sqltest.sp	Fri Jun 01 04:22:53 2007 +0000
     4.3 @@ -0,0 +1,108 @@
     4.4 +#include <sourcemod>
     4.5 +
     4.6 +public Plugin:myinfo = 
     4.7 +{
     4.8 +	name = "SQL Testing Lab",
     4.9 +	author = "AlliedModders LLC",
    4.10 +	description = "Tests basic function calls",
    4.11 +	version = "1.0.0.0",
    4.12 +	url = "http://www.sourcemod.net/"
    4.13 +};
    4.14 +
    4.15 +public OnPluginStart()
    4.16 +{
    4.17 +	RegServerCmd("sql_test_normal", Command_TestSql1)
    4.18 +	RegServerCmd("sql_test_stmt", Command_TestSql2)
    4.19 +}
    4.20 +
    4.21 +PrintQueryData(Handle:query)
    4.22 +{
    4.23 +	if (!SQL_HasResultSet(query))
    4.24 +	{
    4.25 +		PrintToServer("Query Handle %x has no results", query)
    4.26 +		return
    4.27 +	}
    4.28 +	
    4.29 +	new rows = SQL_GetRowCount(query)
    4.30 +	new fields = SQL_GetFieldCount(query)
    4.31 +	
    4.32 +	decl String:fieldNames[fields][32]
    4.33 +	PrintToServer("Fields: %d", fields)
    4.34 +	for (new i=0; i<fields; i++)
    4.35 +	{
    4.36 +		SQL_FieldNumToName(query, i, fieldNames[i], 32)
    4.37 +		PrintToServer("-> Field %d: \"%s\"", i, fieldNames[i])
    4.38 +	}
    4.39 +	
    4.40 +	PrintToServer("Rows: %d", rows)
    4.41 +	decl String:result[255]
    4.42 +	new row
    4.43 +	while (SQL_FetchRow(query))
    4.44 +	{
    4.45 +		row++
    4.46 +		PrintToServer("Row %d:", row)
    4.47 +		for (new i=0; i<fields; i++)
    4.48 +		{
    4.49 +			SQL_FetchString(query, i, result, sizeof(result))
    4.50 +			PrintToServer(" [%s] %s", fieldNames[i], result)
    4.51 +		}
    4.52 +	}
    4.53 +}
    4.54 +
    4.55 +public Action:Command_TestSql1(args)
    4.56 +{
    4.57 +	new String:error[255]
    4.58 +	new Handle:db = SQL_DefConnect(error, sizeof(error))
    4.59 +	if (db == INVALID_HANDLE)
    4.60 +	{
    4.61 +		PrintToServer("Failed to connect: %s", error)
    4.62 +		return Plugin_Handled
    4.63 +	}
    4.64 +	
    4.65 +	new Handle:query = SQL_Query(db, "SELECT * FROM gaben")
    4.66 +	if (query == INVALID_HANDLE)
    4.67 +	{
    4.68 +		SQL_GetError(db, error, sizeof(error))
    4.69 +		PrintToServer("Failed to query: %s", error)
    4.70 +	} else {
    4.71 +		PrintQueryData(query)
    4.72 +		CloseHandle(query)
    4.73 +	}
    4.74 +	
    4.75 +	CloseHandle(db)
    4.76 +	
    4.77 +	return Plugin_Handled;
    4.78 +}
    4.79 +
    4.80 +public Action:Command_TestSql2(args)
    4.81 +{
    4.82 +	new String:error[255]
    4.83 +	new Handle:db = SQL_DefConnect(error, sizeof(error))
    4.84 +	if (db == INVALID_HANDLE)
    4.85 +	{
    4.86 +		PrintToServer("Failed to connect: %s", error)
    4.87 +		return Plugin_Handled
    4.88 +	}
    4.89 +	
    4.90 +	new Handle:stmt = SQL_PrepareQuery(db, "SELECT * FROM gaben WHERE gaben > ?", error, sizeof(error))
    4.91 +	if (stmt == INVALID_HANDLE)
    4.92 +	{
    4.93 +		PrintToServer("Failed to prepare query: %s", error)
    4.94 +	} else {
    4.95 +		SQL_BindParamInt(stmt, 0, 1)
    4.96 +		if (!SQL_Execute(stmt))
    4.97 +		{
    4.98 +			SQL_GetError(stmt, error, sizeof(error))
    4.99 +			PrintToServer("Failed to execute query: %s", error)
   4.100 +		} else {
   4.101 +			PrintQueryData(stmt)
   4.102 +		}
   4.103 +		CloseHandle(stmt)
   4.104 +	}
   4.105 +	
   4.106 +	CloseHandle(db)
   4.107 +	
   4.108 +	return Plugin_Handled;
   4.109 +}
   4.110 +
   4.111 +
     5.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     5.2 +++ b/plugins/testsuite/sqltest.sql	Fri Jun 01 04:22:53 2007 +0000
     5.3 @@ -0,0 +1,7 @@
     5.4 +
     5.5 +CREATE TABLE gaben (gaben int primary key, fat varchar(32));
     5.6 +
     5.7 +INSERT INTO gaben VALUES(1, 'what the');
     5.8 +INSERT INTO gaben VALUES(2, 'Bee''s Knees!');
     5.9 +INSERT INTO gaben VALUES(3, 'newell');
    5.10 +INSERT INTO gaben VALUES(4, 'CRAB CAKE.');