Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- *
- * @Author Tr1xy
- * @Date 5th August 2020
- * @Weburl www.vibe-gaming.com
- * @Project V I B E
- *
- * @File mysql.inc
- * @Module server\database
- *
- */
- #if defined _inc_mysql
- #undef _inc_mysql
- #endif
- #if defined __SERVER_DATABASE_MYSQL
- #endinput
- #endif
- #define __SERVER_DATABASE_MYSQL
- #include <env>
- #include <a_mysql>
- #include <YSI_Coding\y_hooks>
- /// <summary>
- /// Stores the last dbName handle
- /// </summary>
- static MySQL: dbHandler;
- // -
- // Api
- // -
- /// <summary>
- /// Returns the query handle
- /// </summary>
- stock MySQL: Database_GetHandle()
- {
- return dbHandler;
- }
- // -
- // Internal
- // -
- /// <summary>
- /// Checks for Env's and then begins to create a dbName
- /// connection from them.
- /// </summary>
- static CreateConnection()
- {
- // Check if the env variables have been set
- // and assign them for later
- new dbHost[86];
- if(Env_Has("MYSQL_HOSTNAME")) {
- Env_Get("MYSQL_HOSTNAME", dbHost, sizeof dbHost);
- }
- new dbUser[86];
- if(Env_Has("MYSQL_USERNAME")) {
- Env_Get("MYSQL_USERNAME", dbUser, sizeof dbUser);
- }
- new dbPass[86];
- if(Env_Has("MYSQL_PASSWORD")) {
- Env_Get("MYSQL_PASSWORD", dbPass, sizeof dbPass);
- }
- new dbName[86];
- if(Env_Has("MYSQL_DATABASE")) {
- Env_Get("MYSQL_DATABASE", dbName, sizeof dbName);
- }
- new port;
- if(Env_Has("MYSQL_PORT")) {
- new tmp_port[7];
- Env_Get("MYSQL_PORT", tmp_port, sizeof tmp_port);
- if(!isnull(tmp_port))
- port = strval(tmp_port);
- }
- // Attempt to establish a connection to the dbName else
- // fail and return
- new MySQLOpt: options = mysql_init_options();
- mysql_set_option(options, AUTO_RECONNECT, true);
- mysql_set_option(options, MULTI_STATEMENTS, true);
- mysql_set_option(options, POOL_SIZE, 2);
- mysql_set_option(options, SERVER_PORT, port);
- mysql_log(WARNING | ERROR);
- dbHandler = mysql_connect(dbHost, dbUser, dbPass, dbName, options);
- if(mysql_errno(dbHandler) != 0) {
- return 0;
- }
- return 1;
- }
- // -
- // Hooks
- // -
- /// <summary>
- /// Attempt to create a database connection or fail with an output
- /// </summary>
- hook OnGameModeInit()
- {
- // Dispatch an event before the database connection has connected
- CallLocalFunction("OnDatabaseConnecting", "");
- if(!CreateConnection()) {
- printf("Failed to establish a connection with the database. Please check credentials.");
- SendRconCommand("exit");
- return 0;
- }
- // Dispatch an event after the database connection has connected
- CallLocalFunction("OnDatabaseConnected", "");
- return continue();
- }
- /// <summary>
- /// Attempt to close the database connection or fail with
- /// an output
- /// </summary>
- hook OnGameModeExit()
- {
- // Dispatch an event before the database has disconnected
- CallLocalFunction("OnDatabaseDisconnecting", "");
- if(!mysql_close(dbHandler)) {
- printf("Failed to terminate the database connection. Please check this.");
- }
- // Dispatch an event after the database has disconnected
- CallLocalFunction("OnDatabaseDisconnected", "");
- return continue();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement