AlliedModders

AlliedModders (https://forums.alliedmods.net/index.php)
-   Snippets and Tutorials (https://forums.alliedmods.net/forumdisplay.php?f=112)
-   -   [DB] Database Manager (https://forums.alliedmods.net/showthread.php?t=60036)

Nican 08-25-2007 17:18

[DB] Database Manager
 
19 Attachment(s)
Database manager

Description:
Different plugins may require different tables to get user informations, like XP, level, money, kills, deaths and bla bla bla... This plugin/stock automatically select info from table, create columns where necessary and makes it easy for people that want to use the database

How it works
Database manager detects all columns from the sm_users. Plugins can enable,disable the selecting of each column, or create a new column if needed.

Cvars
  • sm_db_config (deafult: "default") - The connection DB manager will use
  • sm_db_persit (deafult: "1") - Set to true to use persistant connection
  • sm_db_autolite (deafult: "1") - If the DB does not exist, SQLite will be used

Installation:
  • MySQL
    First, go to the file addons\sourcemod\configs\databases.cfg , and edit the "default" connection to your needs, it shouldn't be complicated, It should end up looking something like this:
    PHP Code:

    "default"
        
    {
            
    "driver"        "default"
            "host"            "localhost"
            "database"        "sm"
            "user"            "root"
            "pass"            ""
            
    //"timeout"            "0"
            //"port"            "0"
        


    And the leave the plugin to do the rest
    -----------------------------------
  • SQLite
    Change the cvar sm_db_config to "storage-local"
    and make sure that in addons\sourcemod\configs\databases.cfg this exists:
    PHP Code:

        "storage-local"
        
    {
            
    "driver"            "sqlite"
            "database"            "sourcemod-local"
        


  • Put a_mysql.smx file into your /addons/sourcemod/plugins folder, you can easily get the a_mysql.smx file by clicking at "Get Plugin" at the end of this message


FAQ:
  • I am getting an error "Can't connect to local MySQL server through socket '/tmp/mysql.sock'"
    • If you have acess to root in your linux computer, execute this in shell "ln -s /var/run/mysqld/mysqld.sock /tmp/mysql.sock"



Example
See next post

ChangeLog:
  • August 25, 2007
    • Initial Release
    • Fixed Float Adding
    • Started Lock tables
    • Removed all FastQueries
  • August 26, 2007
    • Removed all the connecting/disconecting
    • Added cvars
    • Made it a lot user to use
  • August 27, 2007
    • Added SQLite support
    • Started using atd_array, there is no more limits now
  • August 29, 2007
    • MAJOR UPDATE!
  • August 30, 2007
    • Fixed adt_size array bug
  • September 06, 2007
    • Added new cvar
  • September 15, 2007 (3.1)
    • Changed some "new" to "decl"
    • Removed some fail states
  • January 10, 2007 (4.0)
    • Now the plugin will story all data in 2 Adt arrays.
    • Fixed possible error of things
    • Changed the way the plugin saves data on the database.
    • Added some extra checks to prevent errors.
  • January 21, 2008
    • Data is now saved on every round start.

Notes:
Put the globaldb.inc in addons\sourcemod\scripting\include

Nican 08-26-2007 23:20

Re: [MySQL] Database Manager
 
Exemples:

Simple bank plugin:

PHP Code:

new db_money;

public 
OnPluginStart()
{
}

public 
DB_OnRowsUpdated(){
    
db_money DB_GetColumnId("my_money"truetrueDB_INT);
    if(
db_money == -1)
        
SetFailState("[SMbank] Could not create money column!");
}

stock GetUserMoney(client){
    return 
DB_GetColumnclientdb_money );
}

stock SetUserMoney(clientmoney){
    return 
DB_SetInfo(clientdb_money money);



This function is called right after the server receives information from the database.
DO NOT try to use DB_GetColumn on player OnClientConnect or anything of the type.
PHP Code:

public DB_OnClientUpdate(client){
     
//Do something...


Note: Information is saved on OnClientDisconnect_Post, so it is OK to do this:
PHP Code:

public OnClientDisconnect(client){
    
DB_SetInfoclientdb_money5000 );



Functions
PHP Code:

#define DB_INT 0
#define DB_STRING 1
#define DB_FLOAT 2

/**
 * Gets the ID of a row
 * @param RowName Name of the mysql Column you want to get
 * @param autoenable To enable this row on select query
 * @param autocreate If the row does not exist, create it
 * @param rowtype row type if it is being created
 * @param rowsize row size if it is being created, 0 = default, float has no size
*/

native DB_GetColumnId(const String:RowName[], bool:autoenable truebool:autocreate falserowtype DB_INTrowsize 0);

/**
 * Gets the ID of a row
 * @param Enable Row id from fetching when client connect
*/
native DB_EnableIdColumnId );

/**
 * Gets the ID of a row
 * @param Disable Row Id from fetching when client connect
*/
native DB_DisableIdColumnId );

/**
 * Gets the ID of a row
 * @param client Client ID for set info
 * @param ColumnId The column ID for set info
 * @param value The information to set it to
*  @param add Set to true if you want to add the value to the old amount
*/
native DB_SetInfoclientColumnIdany:value booladd false);
native DB_SetInfoStringclientColumnId, const String:name[] );


/**
 * Gets the ID of a row
 * @param client Client ID for get info
 * @param ColumnId The column ID for get info
*/
native any:DB_GetColumnclient ,  ColumnId );
native DB_GetColumnStringclientColumnIdString:value[], maxsize);


/**
 * Gets the ID of a row
 * @param RowName Name of the mysql Column you want to get
*/
native DB_ColumnTypeColumnId );

/**
 * Returns the DB handle, for your own personal use
*/
native Handle:DB_GetHandle( );


/**
 * Gets the ID of a row
 * @param RowName The name of the new row
 * @param RowType Use DB_* and define the new row type
 * @param Size The size of this now row, leave blank for default
 * @return RowId, -1 on failure
*/
stock DB_CreateRow( const String:Rowname[], RowtypeDB_INTsize ){
    return 
DB_GetColumnId(RowNametruetrueRowtype size);
}

/**
 * Called when info of a client has been updated
 * @param client client
 */
forward DB_OnClientUpdate(client); 


kipplitz 08-27-2007 00:52

Re: [MySQL] Database Manager
 
Ok, completely new here at this...so walk with me...do I need a mysql database to use this as well as the bank mod you have? Give me some insight here oh great one! :)

KippLitz

Rainbow 6 08-29-2007 18:34

Re: [DB] Database Manager
 
Which one do we need or do we need both?

Nican 08-29-2007 21:13

Re: [DB] Database Manager
 
Just the a_mysql.sp

tkaway69 08-29-2007 21:33

Re: [DB] Database Manager
 
I know this will be Noobish, but it would be nice if you had some kind of instalation instructions. We would like to use the Bank mod you fixed up. We are just not real Mysql savy. Thanks.

Rainbow 6 08-29-2007 22:18

Re: [DB] Database Manager
 
I will try to come up with one as soon as i get the bank mod + the mysql Manager plugin to work.

tkaway69 08-29-2007 23:06

Re: [DB] Database Manager
 
Thanks. I assume we can use the mysql database for our website? Waiting for some info.

Nican 08-29-2007 23:40

Re: [DB] Database Manager
 
Ok... I just majorly update it... there was a major error...


And what do you mean by "use the mysql database for our website"

tkaway69 08-30-2007 11:01

Re: [DB] Database Manager
 
Well I am not sure how this mysql stuff works, but we have a msql that holds all the info to our forums and such. I was assuming we could use that to create a database for the server as well.


All times are GMT -4. The time now is 10:05.

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