plugins/testsuite/sqltest.sp
author Ruben Gonzalez <drifter01620@gmail.com>
Thu May 22 09:49:27 2014 -0400 (2014-05-22)
changeset 4378 568f4497ef4a
parent 2281 fcb07f41ad91
permissions -rw-r--r--
Update CS:GO team score gamedata.
     1 #include <sourcemod>
     2 
     3 public Plugin:myinfo = 
     4 {
     5 	name = "SQL Testing Lab",
     6 	author = "AlliedModders LLC",
     7 	description = "Tests basic function calls",
     8 	version = "1.0.0.0",
     9 	url = "http://www.sourcemod.net/"
    10 };
    11 
    12 public OnPluginStart()
    13 {
    14 	RegServerCmd("sql_test_normal", Command_TestSql1)
    15 	RegServerCmd("sql_test_stmt", Command_TestSql2)
    16 	RegServerCmd("sql_test_thread1", Command_TestSql3)
    17 	RegServerCmd("sql_test_thread2", Command_TestSql4)
    18 	RegServerCmd("sql_test_thread3", Command_TestSql5)
    19 }
    20 
    21 PrintQueryData(Handle:query)
    22 {
    23 	if (!SQL_HasResultSet(query))
    24 	{
    25 		PrintToServer("Query Handle %x has no results", query)
    26 		return
    27 	}
    28 	
    29 	new rows = SQL_GetRowCount(query)
    30 	new fields = SQL_GetFieldCount(query)
    31 	
    32 	decl String:fieldNames[fields][32]
    33 	PrintToServer("Fields: %d", fields)
    34 	for (new i=0; i<fields; i++)
    35 	{
    36 		SQL_FieldNumToName(query, i, fieldNames[i], 32)
    37 		PrintToServer("-> Field %d: \"%s\"", i, fieldNames[i])
    38 	}
    39 	
    40 	PrintToServer("Rows: %d", rows)
    41 	decl String:result[255]
    42 	new row
    43 	while (SQL_FetchRow(query))
    44 	{
    45 		row++
    46 		PrintToServer("Row %d:", row)
    47 		for (new i=0; i<fields; i++)
    48 		{
    49 			SQL_FetchString(query, i, result, sizeof(result))
    50 			PrintToServer(" [%s] %s", fieldNames[i], result)
    51 		}
    52 	}
    53 }
    54 
    55 public Action:Command_TestSql1(args)
    56 {
    57 	new String:error[255]
    58 	new Handle:db = SQL_DefConnect(error, sizeof(error))
    59 	if (db == INVALID_HANDLE)
    60 	{
    61 		PrintToServer("Failed to connect: %s", error)
    62 		return Plugin_Handled
    63 	}
    64 	
    65 	new Handle:query = SQL_Query(db, "SELECT * FROM gab")
    66 	if (query == INVALID_HANDLE)
    67 	{
    68 		SQL_GetError(db, error, sizeof(error))
    69 		PrintToServer("Failed to query: %s", error)
    70 	} else {
    71 		PrintQueryData(query)
    72 		CloseHandle(query)
    73 	}
    74 	
    75 	CloseHandle(db)
    76 	
    77 	return Plugin_Handled;
    78 }
    79 
    80 public Action:Command_TestSql2(args)
    81 {
    82 	new String:error[255]
    83 	new Handle:db = SQL_DefConnect(error, sizeof(error))
    84 	if (db == INVALID_HANDLE)
    85 	{
    86 		PrintToServer("Failed to connect: %s", error)
    87 		return Plugin_Handled
    88 	}
    89 	
    90 	new Handle:stmt = SQL_PrepareQuery(db, "SELECT * FROM gab WHERE gaben > ?", error, sizeof(error))
    91 	if (stmt == INVALID_HANDLE)
    92 	{
    93 		PrintToServer("Failed to prepare query: %s", error)
    94 	} else {
    95 		SQL_BindParamInt(stmt, 0, 1)
    96 		if (!SQL_Execute(stmt))
    97 		{
    98 			SQL_GetError(stmt, error, sizeof(error))
    99 			PrintToServer("Failed to execute query: %s", error)
   100 		} else {
   101 			PrintQueryData(stmt)
   102 		}
   103 		CloseHandle(stmt)
   104 	}
   105 	
   106 	CloseHandle(db)
   107 	
   108 	return Plugin_Handled;
   109 }
   110 
   111 new Handle:g_ThreadedHandle = INVALID_HANDLE;
   112 
   113 public CallbackTest3(Handle:owner, Handle:hndl, const String:error[], any:data)
   114 {
   115 	PrintToServer("CallbackTest1() (owner %x) (hndl %x) (error \"%s\") (data %d)", owner, hndl, error, data);
   116 	if (g_ThreadedHandle != INVALID_HANDLE && hndl != INVALID_HANDLE)
   117 	{
   118 		CloseHandle(hndl);
   119 	} else {
   120 		g_ThreadedHandle = hndl;
   121 	}
   122 }
   123 
   124 public Action:Command_TestSql3(args)
   125 {
   126 	if (g_ThreadedHandle != INVALID_HANDLE)
   127 	{
   128 		PrintToServer("A threaded connection already exists, run the next test");
   129 		return Plugin_Handled;
   130 	}
   131 	
   132 	new String:name[32];
   133 	GetCmdArg(1, name, sizeof(name));
   134 	
   135 	SQL_TConnect(CallbackTest3, name);
   136 	
   137 	return Plugin_Handled;
   138 }
   139 
   140 
   141 public Action:Command_TestSql4(args)
   142 {
   143 	SQL_LockDatabase(g_ThreadedHandle);
   144 	new Handle:query = SQL_Query(g_ThreadedHandle, "SELECT * FROM gab")
   145 	if (query == INVALID_HANDLE)
   146 	{
   147 		new String:error[255];
   148 		SQL_GetError(g_ThreadedHandle, error, sizeof(error))
   149 		PrintToServer("Failed to query: %s", error)
   150 	} else {
   151 		PrintQueryData(query)
   152 		CloseHandle(query)
   153 	}
   154 	SQL_UnlockDatabase(g_ThreadedHandle);
   155 	
   156 	return Plugin_Handled;
   157 }
   158 
   159 public CallbackTest5(Handle:owner, Handle:hndl, const String:error[], any:data)
   160 {
   161 	if (hndl == INVALID_HANDLE)
   162 	{
   163 		PrintToServer("Failed to query: %s", error)
   164 	} else {
   165 		PrintQueryData(hndl)
   166 		SQL_TQuery(g_ThreadedHandle, CallbackTest6, "UPDATE gab SET `gaben` = `gaben` + 1 WHERE `gaben` >= 4", 52)
   167 	}
   168 }
   169 
   170 public CallbackTest6(Handle:owner, Handle:hndl, const String:error[], any:data)
   171 {
   172 	if (hndl == INVALID_HANDLE)
   173 	{
   174 		PrintToServer("Failed to query: %s", error)
   175 	} else {
   176 		PrintToServer("Queried!");
   177 		SQL_TQuery(g_ThreadedHandle, CallbackTest7, "UPDATE gab SET `gaben` = `gaben` + 1 WHERE `gaben` >= 4", 52)
   178 	}
   179 }
   180 
   181 public CallbackTest7(Handle:owner, Handle:hndl, const String:error[], any:data)
   182 {
   183 	if (hndl == INVALID_HANDLE)
   184 	{
   185 		PrintToServer("Failed to query: %s", error)
   186 	} else {
   187 		PrintToServer("Queried!");
   188 	}
   189 }
   190 
   191 public Action:Command_TestSql5(args)
   192 {
   193 	SQL_TQuery(g_ThreadedHandle, CallbackTest5, "SELECT * FROM gab", 52)
   194 	SQL_TQuery(g_ThreadedHandle, CallbackTest5, "SELECT * FROM gab", 52)
   195 	SQL_TQuery(g_ThreadedHandle, CallbackTest5, "SELECT * FROM gab", 52)
   196 	SQL_TQuery(g_ThreadedHandle, CallbackTest5, "SELECT * FROM gab", 52)
   197 	
   198 	return Plugin_Handled;
   199 }
   200