Skip to content

Instantly share code, notes, and snippets.

@ADRFranklin
Created April 13, 2019 15:40
Show Gist options
  • Save ADRFranklin/1d73d910eb97a4953ef70e4b152940e8 to your computer and use it in GitHub Desktop.
Save ADRFranklin/1d73d910eb97a4953ef70e4b152940e8 to your computer and use it in GitHub Desktop.
Example of loading data from database with mysql plugin
// Load a houses's data
stock Error: LoadHouseRow(DBID: house_db_id)
{
inline handle()
{
// holds any errors we have
new errorInfo[1024];
// get the number of rows
new rows = cache_num_rows();
// check if any rows were returned
if(rows) {
// create a new house instance
new House: house_id;
new Error: chi = CreateHouseInstance(house_id);
if(IsError(chi)) {
GetErrors(errorInfo);
str_val(errorInfo);
Handled();
@return 1;
}
// get the house's data object
new Map: house_data;
new Error: ghdo = GetHouseDataObject(house_id, house_data);
if(IsError(ghdo)) {
GetErrors(errorInfo);
str_val(errorInfo);
Handled();
@return 1;
}
// store the house's database id
new DBID: db_id;
cache_get_value_int(0, "id", DBID: db_id);
map_set_str(house_data, "db_id", db_id);
// store the house's name
new name[HOUSE_MAX_NAME];
cache_get_value(0, "name", name);
map_str_set_str(house_data, "name", name);
// store the house's type
new HouseType: type;
cache_get_value_int(0, "type", HouseType: type);
map_set_str(house_data, "type", type);
// store the house owner's id
new DBID: owner_id;
cache_get_value_int(0, "owner_id", DBID: owner_id);
map_set_str(house_data, "owner_id", owner_id);
// store the house owner's name
new owner_name[MAX_PLAYERS_NAME];
cache_get_value(0, "owner_name", owner_name);
map_str_set_str(house_data, "owner_name", owner_name);
// store the house's price
new price;
cache_get_value_int(0, "price", price);
map_set_str(house_data, "price", price);
// store the house's lock status
new bool: lock_status;
cache_get_value_bool(0, "lock_status", lock_status);
map_set_str(house_data, "lock_status", lock_status);
// store the house's exterior position
new
Float: exterior_x,
Float: exterior_y,
Float: exterior_z,
Float: exterior_a,
exterior_int,
exterior_vw
;
cache_get_value_float(0, "exterior_x", exterior_x);
cache_get_value_float(0, "exterior_y", exterior_y);
cache_get_value_float(0, "exterior_z", exterior_z);
cache_get_value_float(0, "exterior_a", exterior_a);
cache_get_value_int(0, "exterior_int", exterior_int);
cache_get_value_int(0, "exterior_vw", exterior_vw);
map_set_str(house_data, "exterior_x", exterior_x);
map_set_str(house_data, "exterior_y", exterior_y);
map_set_str(house_data, "exterior_z", exterior_z);
map_set_str(house_data, "exterior_a", exterior_a);
map_set_str(house_data, "exterior_int", exterior_int);
map_set_str(house_data, "exterior_vw", exterior_vw);
// store the house's interior position
new
Float: interior_x,
Float: interior_y,
Float: interior_z,
Float: interior_a,
interior_int,
interior_vw
;
cache_get_value_float(0, "interior_x", interior_x);
cache_get_value_float(0, "interior_y", interior_y);
cache_get_value_float(0, "interior_z", interior_z);
cache_get_value_float(0, "interior_a", interior_a);
cache_get_value_int(0, "interior_int", interior_int);
cache_get_value_int(0, "interior_vw", interior_vw);
map_set_str(house_data, "interior_x", interior_x);
map_set_str(house_data, "interior_y", interior_y);
map_set_str(house_data, "interior_z", interior_z);
map_set_str(house_data, "interior_a", interior_a);
map_set_str(house_data, "interior_int", interior_int);
map_set_str(house_data, "interior_vw", interior_vw);
// update the house
new Error: uh = UpdateHouse(house_id);
if(IsError(uh)) {
GetErrors(errorInfo);
str_val(errorInfo);
Handled();
@return 1;
}
// Register an event call
// creates an event that says that a
// house has been loaded
new result;
if(pawn_try_call_public("OnLoadHouse", result, "i", house_id) != amx_err_notfound) {
if(!result) {
GetErrors(errorInfo);
str_val(errorInfo);
Handled();
@return 1;
}
}
}
@return 1;
}
new ret = MySQL_PQueryInline(mysql_active_handle, using inline handle,
"\
SELECT house.*, player.username \
FROM `"HOUSE_DATABASE_TABLE"` house \
LEFT JOIN `"AUTH_DATABASE_TABLE"` player \
ON house.owner_id = player.id \
WHERE house.id = %d \
LIMIT 1 \
", house_db_id);
if(!ret) {
return Error(1, "Failed to run query");
}
return NoError();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment