Advertisement
Guest User

arukotofiy

a guest
Aug 6th, 2020
361
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Pawn 3.05 KB | None | 0 0
  1. /*
  2.  *
  3.  *  @Author     Tr1xy
  4.  *  @Date       5th August 2020
  5.  *  @Weburl     www.vibe-gaming.com
  6.  *  @Project    V I B E
  7.  *
  8.  *  @File       mysql.inc
  9.  *  @Module     server\database
  10.  *
  11.  */
  12.  
  13. #if defined _inc_mysql
  14.     #undef _inc_mysql
  15. #endif
  16.  
  17. #if defined __SERVER_DATABASE_MYSQL
  18.     #endinput
  19. #endif
  20. #define __SERVER_DATABASE_MYSQL
  21.  
  22. #include <env>
  23. #include <a_mysql>
  24. #include <YSI_Coding\y_hooks>
  25.  
  26. /// <summary>
  27. /// Stores the last dbName handle
  28. /// </summary>
  29. static MySQL: dbHandler;
  30.  
  31. // -
  32. // Api
  33. // -
  34.  
  35. /// <summary>
  36. /// Returns the query handle
  37. /// </summary>
  38. stock MySQL: Database_GetHandle()
  39. {
  40.     return dbHandler;
  41. }
  42.  
  43. // -
  44. // Internal
  45. // -
  46.  
  47. /// <summary>
  48. /// Checks for Env's and then begins to create a dbName
  49. /// connection from them.
  50. /// </summary>
  51. static CreateConnection()
  52. {
  53.     // Check if the env variables have been set
  54.     // and assign them for later
  55.    
  56.     new dbHost[86];
  57.     if(Env_Has("MYSQL_HOSTNAME")) {
  58.         Env_Get("MYSQL_HOSTNAME", dbHost, sizeof dbHost);
  59.     }
  60.    
  61.     new dbUser[86];
  62.     if(Env_Has("MYSQL_USERNAME")) {
  63.         Env_Get("MYSQL_USERNAME", dbUser, sizeof dbUser);
  64.     }
  65.    
  66.     new dbPass[86];
  67.     if(Env_Has("MYSQL_PASSWORD")) {
  68.         Env_Get("MYSQL_PASSWORD", dbPass, sizeof dbPass);
  69.     }
  70.    
  71.     new dbName[86];
  72.     if(Env_Has("MYSQL_DATABASE")) {
  73.         Env_Get("MYSQL_DATABASE", dbName, sizeof dbName);
  74.     }
  75.    
  76.     new port;
  77.     if(Env_Has("MYSQL_PORT")) {
  78.         new tmp_port[7];
  79.         Env_Get("MYSQL_PORT", tmp_port, sizeof tmp_port);
  80.        
  81.         if(!isnull(tmp_port))
  82.             port = strval(tmp_port);
  83.     }
  84.    
  85.     // Attempt to establish a connection to the dbName else
  86.     // fail and return
  87.    
  88.     new MySQLOpt: options = mysql_init_options();
  89.     mysql_set_option(options, AUTO_RECONNECT, true);
  90.     mysql_set_option(options, MULTI_STATEMENTS, true);
  91.     mysql_set_option(options, POOL_SIZE, 2);
  92.     mysql_set_option(options, SERVER_PORT, port);
  93.    
  94.     mysql_log(WARNING | ERROR);
  95.    
  96.     dbHandler = mysql_connect(dbHost, dbUser, dbPass, dbName, options);
  97.     if(mysql_errno(dbHandler) != 0) {
  98.         return 0;
  99.     }
  100.    
  101.     return 1;
  102. }
  103.  
  104. // -
  105. // Hooks
  106. // -
  107.  
  108. /// <summary>
  109. /// Attempt to create a database connection or fail with an output
  110. /// </summary>
  111. hook OnGameModeInit()
  112. {
  113.     // Dispatch an event before the database connection has connected
  114.     CallLocalFunction("OnDatabaseConnecting", "");
  115.    
  116.     if(!CreateConnection()) {
  117.         printf("Failed to establish a connection with the database. Please check credentials.");
  118.         SendRconCommand("exit");
  119.         return 0;
  120.     }
  121.    
  122.     // Dispatch an event after the database connection has connected
  123.     CallLocalFunction("OnDatabaseConnected", "");
  124.    
  125.     return continue();
  126. }
  127.  
  128. /// <summary>
  129. /// Attempt to close the database connection or fail with
  130. /// an output
  131. /// </summary>
  132. hook OnGameModeExit()
  133. {
  134.     // Dispatch an event before the database has disconnected
  135.     CallLocalFunction("OnDatabaseDisconnecting", "");
  136.    
  137.     if(!mysql_close(dbHandler)) {
  138.         printf("Failed to terminate the database connection. Please check this.");
  139.     }
  140.    
  141.     // Dispatch an event after the database has disconnected
  142.     CallLocalFunction("OnDatabaseDisconnected", "");
  143.    
  144.     return continue();
  145. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement