Raised This Month: $51 Target: $400
 12% 

[EXTENSION] MySQL


Post New Thread Reply   
 
Thread Tools Display Modes
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, 387 views)

Last edited by khap; 05-31-2007 at 01:28. Reason: new version
khap is offline
FlyingMongoose
Veteran Member
Join Date: Mar 2004
Old 05-20-2007 , 13:58   Re: [EXTENSION] MySQL
Reply With Quote #2

Very nice, however the next major step in sourcemod is their own SQL API/extension. I'm honestly going to be using that, but good work anyway.
FlyingMongoose is offline
sessus
Senior Member
Join Date: May 2006
Old 05-20-2007 , 18:55   Re: [EXTENSION] MySQL
Reply With Quote #3

nice work khap. And: can't wait for the SQL API to finally manage bans und admins without any .TXT's / .CFG's
sessus is offline
BAILOPAN
Join Date: Jan 2004
Old 05-21-2007 , 01:27   Re: [EXTENSION] MySQL
Reply With Quote #4

As noted, we will be providing a unified/abstracted SQL interface very similar to AMX Mod X's. If you need SQL support now this is great, but just be aware that an official API will be out within the next two weeks or so.

I like that your extension uses Handles and statement binding ;) I was having trouble finding a good way to express bound parameters but you seem to have done it nicely. It would probably be better to use MySQL's builtin statement API though.
__________________
egg
BAILOPAN is offline
khap
Junior Member
Join Date: May 2007
Location: Moscow, Russia
Old 05-31-2007 , 01:30   Re: [EXTENSION] MySQL
Reply With Quote #5

update to new version.

The basic changes is an opportunity of work with stored procedures which can return some results.
__________________
Russian Servers Day Of Defeat: Source - www.polygon4.net
khap is offline
Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT -4. The time now is 12:01.


Powered by vBulletin®
Copyright ©2000 - 2024, vBulletin Solutions, Inc.
Theme made by Freecode