Advertisement
Guest User

Local Database 1.0.0.1

a guest
Mar 16th, 2014
327
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.68 KB | None | 0 0
  1. // The C Plus Plus headers.
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. #include <string.h>
  5. #include <math.h>
  6. #include <ctype.h>
  7. #include <stdarg.h>
  8.  
  9. // The other further headers.
  10. #include "Header.h"
  11. #include "amxxmodule.h"
  12. #include "sqlite3.h"
  13.  
  14. // The definitions.
  15. #define MAXIMUM_CHARACTERS_FOR_STRING   4096U
  16. #define MAXIMUM_CHARACTERS_FOR_COLUMN   128U
  17. #define MAXIMUM_ROWS_TO_SELECT          2048U
  18. #define MAXIMUM_COLUMNS_TO_HAVE         64U
  19.  
  20. // The Database_Row class. Each row will contain 64 columns as long as 128 maximum characters.
  21. class Database_Row
  22. {
  23. public:
  24.     char Column[MAXIMUM_COLUMNS_TO_HAVE][MAXIMUM_CHARACTERS_FOR_COLUMN];
  25. };
  26.  
  27. // The data used into the extension.
  28. Database_Row g_Row[MAXIMUM_ROWS_TO_SELECT];
  29.  
  30. sqlite3 * g_Handle = NULL;
  31.  
  32. char g_String[MAXIMUM_CHARACTERS_FOR_STRING];
  33.  
  34. int g_Rows = 0U;
  35. int g_Iterator = 0U;
  36. int g_Length = 0U;
  37.  
  38. // The SQLite handlers.
  39. int SQLite_SelectQueryHandler(void *, int Count, char ** Data, char **)
  40. {
  41.     if (g_Rows < MAXIMUM_ROWS_TO_SELECT)
  42.     {
  43.         for (g_Iterator = 0; g_Iterator < Count && g_Iterator < MAXIMUM_COLUMNS_TO_HAVE; g_Iterator++)
  44.         {
  45.             _snprintf(g_Row[g_Rows].Column[g_Iterator], MAXIMUM_CHARACTERS_FOR_COLUMN, "%s", \
  46.                 Data[g_Iterator] ? Data[g_Iterator] : "NULL");
  47.         }
  48.     }
  49.  
  50.     // Let this one to increment everytime. It will be usefull for getting the rows count from any table.
  51.     g_Rows++;
  52.  
  53.     return 0U;
  54. }
  55.  
  56. int SQLite_UpdateQueryHandler(void *, int, char **, char **)
  57. {
  58.     // Do nothing.
  59.     return 0U;
  60. }
  61.  
  62. // The AMX Mod X functions.
  63. static cell AMX_NATIVE_CALL Query(AMX * Handle, cell * Parameters)
  64. {
  65.     g_Rows = 0U;
  66.  
  67.     sqlite3_exec(g_Handle, MF_GetAmxString(Handle, Parameters[1U], 0U, 0U), Parameters[2U] ? \
  68.         SQLite_SelectQueryHandler : SQLite_UpdateQueryHandler, NULL, NULL);
  69.  
  70.     return g_Rows;
  71. }
  72.  
  73. static cell AMX_NATIVE_CALL Get(AMX * Handle, cell * Parameters)
  74. {
  75.     // If the coder will fail, this won't log any error. Will just set the buffer as "NULL" and will return zero.
  76.     if (Parameters[1U] >= 0U && Parameters[1U] < MAXIMUM_ROWS_TO_SELECT && Parameters[2U] >= 0U && \
  77.         Parameters[2U] < MAXIMUM_COLUMNS_TO_HAVE)
  78.     {
  79.         MF_SetAmxString(Handle, Parameters[3U], g_Row[Parameters[1U]].Column[Parameters[2U]], Parameters[4U]);
  80.  
  81.         return (cell)atoi(g_Row[Parameters[1U]].Column[Parameters[2U]]);
  82.     }
  83.  
  84.     MF_SetAmxString(Handle, Parameters[3U], "NULL", Parameters[4U]);
  85.  
  86.     return 0U;
  87. }
  88.  
  89. static cell AMX_NATIVE_CALL Safe(AMX * Handle, cell * Parameters)
  90. {
  91.     // Run into an error whether coder wants to replace a bad character with another bad character.
  92.     if (Parameters[2] == '\'' || Parameters[2] == '"' || Parameters[2] == '\\' || Parameters[2] == '`')
  93.     {
  94.         MF_LogError(Handle, AMX_ERR_NATIVE, \
  95.             "Unable to replace a bad character with another bad character. Bad characters are \\, ', \" and `.");
  96.  
  97.         return 0U;
  98.     }
  99.  
  100.     g_Length = _snprintf(g_String, MAXIMUM_CHARACTERS_FOR_STRING, "%s", MF_GetAmxString(Handle, Parameters[1U], 0U, 0U));
  101.  
  102.     // Check for each character.
  103.     for (g_Iterator = 0; g_Iterator < g_Length; g_Iterator++)
  104.     {
  105.         if (g_String[g_Iterator] == '\'' || g_String[g_Iterator] == '"' || g_String[g_Iterator] == '\\' \
  106.             || g_String[g_Iterator] == '`')
  107.         {
  108.             // Replace bad character with the given one.
  109.             g_String[g_Iterator] = Parameters[2U];
  110.         }
  111.     }
  112.  
  113.     MF_SetAmxString(Handle, Parameters[1U], g_String, g_Length);
  114.  
  115.     return 1U;
  116. }
  117.  
  118. // Functions bundle.
  119. const AMX_NATIVE_INFO g_Functions[] =
  120. {
  121.     { "Database_Query", Query },
  122.     { "Database_Get", Get },
  123.     { "Database_Safe", Safe },
  124.  
  125.     { NULL, NULL }
  126. };
  127.  
  128. // AMX Mod X forwards.
  129. void OnAmxxAttach(void)
  130. {
  131.     MF_AddNatives(g_Functions);
  132.  
  133.     sqlite3_open("Database.SQ3", &g_Handle);
  134. }
  135.  
  136. void OnAmxxDetach(void)
  137. {
  138.     sqlite3_close(g_Handle);
  139. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement