AlliedModders

AlliedModders (https://forums.alliedmods.net/index.php)
-   Snippets and Tutorials (https://forums.alliedmods.net/forumdisplay.php?f=112)
-   -   SQLite Helper (https://forums.alliedmods.net/showthread.php?t=309861)

eyal282 08-09-2018 19:29

SQLite Helper
 
1 Attachment(s)
I haven't tested this hard enough so I'll be glad to know if there are bugs.

I hated SQLite when I started so I created this snippet to make SQLite working as easy as client preferences.

This plugin uses sourcemod-local SQLite database for its function.

This plugin obviously doesn't match the potential of SQL querying as a whole because this allows only saving strings into the database and not multiple variables, but it's still quite useful.

If you believe a native is missing and can be added, please tell me.

List of forward and natives:

Code:


public SQLiteHelper_OnDatabaseConnected()
{
        // Called when the database is connected.
       
        // Note: All of the plugin's natives are useless before this is called.
}


native bool:SQLiteHelper_isDatabaseConnected(const String:Name[100], String:Error[50]);

// return: true if the database is connected ( forward SQLiteHelper_OnDatabaseConnected was called ), false otherwise.

// Note: if this returns false ( rare occasions ) then no other native will work.

native SQLiteHelper_RegisterValue(const String:Name[100], String:Error[50]);

// Registers a value, must be done in the SQLiteHelper_OnDatabaseConnected forward ONLY!

// String:Name - Name of value, must be extra-unique to avoid collisions. If your plugin is named "CSGO Stats" and you want to save kills, use the name as "CSGO-Stats_Kills" or something similar.

// String:Error - Buffer in case of error. Go to sqliteHelper.sp to see the possible errors.

// Note: Collision between two names may occur even if another plugin is not using this stock, so don't assume if you're the only plugin to use this stock collisions won't occur.

// Note: this function must be called only once in the lifetime of your plugin but it's always a good idea to keep this function because better safe than sorry.

native SQLiteHelper_SetClientValue(client, const String:Name[100], String:Error[50], const String:value[], timestamp=-1);

// Sets a client's value in the database based on value's name.

// client - Client to set the value for.

// String:Name - Name of value to set for the client.

// String:Error - Buffer for an error. See the sqliteHelper plugin for list of possible errors.

// String:value - Value to set for the client.

// timestamp - Optional timestamp in the value's "last updated". This timestamp is unix and is retrieved through GetClientValue.

native SQLiteHelper_AddClientValue(client, const String:Name[100], String:Error[50], const String:value[], timestamp=-1);

// Note: The exact same as SetClientValue in terms of arguments so peek there for info here, EXCEPT it won't set the value but instead add the existing value with the value you put in String:value[].

// Note: You can set String:value[] to be a negative int / float for subtracting.

// Note: If you add a float to an int, the database will store the entire value as a float.

native SQLiteHelper_GetClientValue(client, const String:Name[100], String:Error[50], String:buffer[], length, &timestamp=0);

// Gets the value of a client.

// client - Client to get value from.

// Name - Name of value to get from.

// Error - Buffer in case of error. Go to sqliteHelper.sp to see the possible errors.

// buffer - Buffer to store the value into.

// length - Length of buffer.

// timestamp - Copyback for the timestamp when last updated.

// Note: Buffer will be set to be empty if client was not found. This means that if you create the variable buffer to be non-empty and it becomes empty you can tell for sure that the client doesn't exist.

native SQLiteHelper_SetAuthIdValue(const String:AuthId[100], const String:Name[100], String:Error[50], String:value[], timestamp=-1);

native SQLiteHelper_AddAuthIdValue(const String:AuthId[100], const String:Name[100], String:Error[50], String:value[], timestamp=-1);

native SQLiteHelper_GetAuthIdValue(const String:AuthId[100], const String:Name[100], String:Error[50], String:buffer[], length, &timestamp=0);

// All these three natives about AuthId all act like their Client equivalents, except you provide the AuthId of an offline ( or online ) player.

// Note: For reference, if you were to use it on an online client, GetClientAuthId must use AuthId_Engine. This means the AuthId inside the database is the engine's and not any other AuthId types.

native SQLiteHelper_SortValue(const String:Name[100], bool:Ascending = false, String:Error[50], Handle:AuthIdArray, Handle:ValueArray, Handle:TimestampArray);

// Sorts values based on ascending or descending order.

// String:Name - Name of value to sort.

// bool:Ascending - true if to sort from lowest value to highest value. false to sort from highest value to lowest value. If false, the highest value will be 0 in the arrays.

// String:Error - Buffer in case of error. Go to sqliteHelper.sp to see the possible errors.

// Handle:AuthIdArray - An array which YOU created to contain the Auth IDs that were sorted. CreateArray(100);

// Handle:ValueArray - An array which YOU created to contain the values that were sorted. CreateArray(100);

// Handle:TimestampArray - An array which YOU created to contain the timestamps of the values that were sorted. CreateArray(1).

// Note: Always create the arrays or you will get errors from this snippet.

// Note: Arrays are filled equally so in a single index within all arays, the data will match a single client.

// Note: If necessary you can use this native to irate through the entire database.


Psyk0tik 08-10-2018 10:54

Re: SQLite Helper
 
Your commenting style amuses and confuses me at the same time. :(

eyal282 08-10-2018 11:06

Re: SQLite Helper
 
Quote:

Originally Posted by Crasher_3637 (Post 2609474)
Your commenting style amuses and confuses me at the same time. :(

What is unclear?

Walgrim 08-10-2018 18:19

Re: SQLite Helper
 
He's talking of all the double slashes you are using instead of
PHP Code:

/* */ 


eyal282 08-10-2018 18:34

Re: SQLite Helper
 
Quote:

Originally Posted by Walgrim (Post 2609545)
He's talking of all the double slashes you are using instead of
PHP Code:

/* */ 


I'll get to it later.

Ilusion9 08-11-2018 06:01

Re: SQLite Helper
 
Use the new syntax and the database class which is threaded.

PHP Code:


public void OnPluginStart()
{
    
Database.Connect(SQL_ConnectToDatabase"storage-local");
}

public 
void SQL_ConnectToDatabase(Database db, const char[] errorany data)
{
    
db.Query(SQL_OnFastQuery"CREATE TABLE IF NOT EXISTS ...");
}

public 
void SQL_OnFastQuery(Database dbDBResultSet rs, const char[] errorany data)
{
    if(
rs == null)
    {
        
LogError("(SQL_OnFastQuery) %s"error);
    }



eyal282 08-11-2018 09:13

Re: SQLite Helper
 
Quote:

Originally Posted by Ilusion9 (Post 2609590)
Use the new syntax and the database class which is threaded.

PHP Code:


public void OnPluginStart()
{
    
Database.Connect(SQL_ConnectToDatabase"storage-local");
}

public 
void SQL_ConnectToDatabase(Database db, const char[] errorany data)
{
    
db.Query(SQL_OnFastQuery"CREATE TABLE IF NOT EXISTS ...");
}

public 
void SQL_OnFastQuery(Database dbDBResultSet rs, const char[] errorany data)
{
    if(
rs == null)
    {
        
LogError("(SQL_OnFastQuery) %s"error);
    }



Why threaded? This is sqlite.

ddhoward 08-11-2018 11:33

Re: SQLite Helper
 
Quote:

Originally Posted by eyal282 (Post 2609605)
Why threaded? This is sqlite.

Disk reads and writes are not instantaneous.

It's just good practice.

Ilusion9 08-12-2018 10:02

Re: SQLite Helper
 
Quote:

Originally Posted by eyal282 (Post 2609605)
Why threaded? This is sqlite.

maybe someone will use sqlite on a remote server

klippy 08-12-2018 10:56

Re: SQLite Helper
 
Quote:

Originally Posted by Ilusion9 (Post 2609761)
maybe someone will use sqlite on a remote server

SQLite doesn't have any networking ability built into it, it's meant to be used only as a local database.


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

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