View Single Post
Author Message
khap
Junior Member
Join Date: May 2007
Location: Moscow, Russia
Old 05-20-2007 , 12:57   [EXTENSION] MySQL
Reply With Quote #1

I apologize for the English, but I wish to present you extension for MySQL.

I have compiled the given version for Linux, but I think what to compile for Windows special work will not make.

Natives mysqlk.inc:
Code:
#if defined _mysqlk_included
  #endinput
#endif
#define _mysqlk_included

#include <core>

#define CLIENT_FOUND_ROWS                (2)
#define CLIENT_MULTI_STATEMENTS            (1<<16)
#define CLIENT_MULTI_RESULTS            (1<<17)


native Handle:MySQLk_Connect(const String:host[], dbPort, const String:login[], const String:password[], const String:database[], const String:config[], flags);

native MySQLk_ExecuteSQL(Handle:dbConnect, const String:querySQL[]);

native MySQLk_CreateQuery(Handle:hndl, const String:sql[]);

native MySQLk_ExecuteQuerySQL(Handle:hndl);

native MySQLk_QueryBindString(Handle:hndl, const String:tag[], const String:value[], bool:EscapeDB);

native MySQLk_QueryBindNumber(Handle:hndl, const String:tag[], value);

native MySQLk_QueryStartFetch(Handle:hndl);

native bool:MySQLk_QueryIsFetchAvailable(Handle:hndl);

native MySQLk_QueryNextFetch(Handle:hndl);

native bool:MySQLk_QueryIsNextRSAvailable(Handle:hndl);

native bool:MySQLk_QueryIsMoreResults(Handle:hndl);

native MySQLk_QueryCloseFetch(Handle:hndl);

native bool:MySQLk_RowIsNull(Handle:hndl, position);

native MySQLk_RowGetNumber(Handle:hndl, position);

native MySQLk_RowGetString(Handle:hndl, position, String:value[], maxValue);

native MySQLk_GetLastAutoIncrement(Handle:hndl);

native MySQLk_QueryGetAffectedRows(Handle:hndl);

/**
 * Do not edit below this line!
 */
public Extension:__ext_mysqlk = 
{
    name = "MySQLk",
    file = "mysqlk.ext",
#if defined AUTOLOAD_EXTENSIONS
    autoload = 1,
#else
    autoload = 0,
#endif
#if defined REQUIRE_EXTENSIONS
    required = 1,
#else
    required = 0,
#endif
};
Plugin example:

Code:
#pragma semicolon 1

#include <sourcemod>
#include <console>
#include <events>
#include <entity>
#include <string>
#include <float>
#include <clients>
#include <core>
#include <timers>
 #include <geoip>
 #include <mysqlk>

public Plugin:myinfo = 
{
    name = "Polygon4 support",
    author = "Alexander Khokhlov",
    description = "check MySQL access",
    version = "0.0.2",
    url = "http://www.polygon4.net/"
};

new DBConnect:mySQL;

new Handle:cvarHost;
new Handle:cvarPort;
new Handle:cvarLogin;
new Handle:cvarPassword;
new Handle:cvarDatabase;
new Handle:cvarConfig;

public bool:zCheckDBConnect(){
    if(mySQL != INVALID_HANDLE)
        return true;

    decl String:host[128];
    decl port;
    decl String:login[128];
    decl String:password[128];
    decl String:database[128];
    decl String:config[128];

    GetConVarString(cvarHost, host, sizeof(host) - 1);
    port = GetConVarInt(cvarPort);
    GetConVarString(cvarLogin, login, sizeof(login) - 1);
    GetConVarString(cvarPassword, password, sizeof(password) - 1);
    GetConVarString(cvarDatabase, database, sizeof(database) - 1);
    GetConVarString(cvarConfig, config, sizeof(config) - 1);

    mySQL = MySQLk_Connect(host,port,login,password,database,config,CLIENT_FOUND_ROWS|CLIENT_MULTI_STATEMENTS|CLIENT_MULTI_RESULTS);

    return mySQL != INVALID_HANDLE;
}

public OnPluginStart()
{
    cvarHost        = CreateConVar("mysqlk_host","localhost","MySQL host");
    cvarPort        = CreateConVar("mysqlk_port","3306","MySQL port");
    cvarLogin        = CreateConVar("mysqlk_login","","MySQL login");
    cvarPassword    = CreateConVar("mysqlk_password","","MySQL password");
    cvarDatabase    = CreateConVar("mysqlk_database","","MySQL database");
    cvarConfig        = CreateConVar("mysqlk_config","","MySQL config");

    CreateTimer(3.0, OnPluginStart_Delayed);
}

public Action:OnPluginStart_Delayed(Handle:timer)
{
    decl DBQuery:dbQuery;
    decl String:val[55];
    decl String:so[128];

    if(zCheckDBConnect()){
      MySQLk_ExecuteSQL(mySQL, "UPDATE g_server SET type = 'dod' WHERE serverID = 1");

      dbQuery = MySQLk_CreateQuery(mySQL, "SELECT serverId, host, port FROM g_server WHERE type = ^1 ");
      if(dbQuery != INVALID_HANDLE){
        MySQLk_QueryBindString(dbQuery, "^1", "dods");

        MySQLk_ExecuteQuerySQL(dbQuery);

        for(MySQLk_QueryStartFetch(dbQuery); MySQLk_QueryIsFetchAvailable(dbQuery); MySQLk_QueryNextFetch(dbQuery))
        {
            MySQLk_RowGetString(dbQuery, 1, val, sizeof(val) - 1);
            FormatEx(so,128,"| %d | %s | %d |+",MySQLk_RowGetNumber(dbQuery, 0), val, MySQLk_RowGetNumber(dbQuery, 2));
            PrintToServer(so);
        }
      }
    }
    PrintToServer("[Pg4] - Loaded");
}

public OnClientAuthorized(client, const String:auth[]){
    if(zCheckDBConnect()){
        decl userid;

        userid = GetClientUserId(client);

        decl String:ip[24];
        decl String:nick[128];
        decl String:country[3];

        GetClientIP(client, ip, sizeof(ip) - 1);
        GetClientName(client, nick, sizeof(nick) - 1);

        GeoipCode2(ip, country);

        decl DBQuery:dbQuery;
        dbQuery = MySQLk_CreateQuery(mySQL, "CALL zg_EnterGameAccount(^1, ^2, ^3, ^4, ^5, ^6) ");
        if(dbQuery != INVALID_HANDLE){
            MySQLk_QueryBindNumber(dbQuery, "^1", GetConVarInt(cvarServerID));
            MySQLk_QueryBindNumber(dbQuery, "^2", userid);
            MySQLk_QueryBindString(dbQuery, "^3", auth,        false);
            MySQLk_QueryBindString(dbQuery, "^4", ip,        false);
            MySQLk_QueryBindString(dbQuery, "^5", nick,        true);
            MySQLk_QueryBindString(dbQuery, "^6", country,    false);

            MySQLk_ExecuteQuerySQL(dbQuery);
            CloseHandle(dbQuery);
        }
    }
}
Changelog
Quote:
1.0.3.10:
* Small cosmetic fixed
+ the Opportunity to receive some results after a call stored procedure (use flags CLIENT_MULTI_STATEMENTS and CLIENT_MULTI_RESULTS on connect to MySQL)
+ Replacement of symbols (', ", \) at binded string (It is necessary at storage of names of players and it is not necessary for IP and STEAMID)
+ Reception of quantity affected rows (use flag CLIENT_FOUND_ROWS on connect to MySQL)
+ it is possible to make extension by MS VS 2003
1.0.1.1 first release
Attached Files
File Type: zip mysqlk.1.0.3.10.zip (17.9 KB, 398 views)

Last edited by khap; 05-31-2007 at 01:28. Reason: new version
khap is offline