Advertisement
Private200

Easy MySQL

Jun 18th, 2016
256
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Pawn 12.31 KB | None | 0 0
  1. #include <a_samp>
  2. #include <a_mysql>
  3.  
  4. /*
  5. native MySQLConnect(const host[] = MYSQL_SERVER, const user[] = MYSQL_UID, const database[] = MYSQL_DATABASE, const password[] = MYSQL_PASSWORD, port = 3306, bool:autoreconnect = true, pool_size = 2)
  6. native MySQLDisconnect();
  7. native CreateRow(table[], const column[], { _,Float }:...)
  8. native RetrieveKey(table[], key[], keys[] = "", size = sizeof (keys), const column[], { _,Float }:...)
  9. native VerifyTable(table[], key[], bool:ai = true)
  10. native VerifyColumn(table[], column[], DBType:type, len, { _,Float }:...)
  11. native SetFloatEntry(table[], key[], keys, const column[], Float:value)
  12. native SetStringEntry(table[], key[], keys, const column[], const value[])
  13. native SetIntEntry(table[], key[], keys, const column[], value)
  14. */
  15.  
  16. #if ! defined MYSQL_DATABASE
  17.     #define MYSQL_DATABASE "test"
  18. #endif
  19.  
  20. #if ! defined MYSQL_SERVER
  21.     #define MYSQL_SERVER "127.0.0.1"
  22. #endif
  23.  
  24. #if ! defined MYSQL_UID
  25.     #define MYSQL_UID "root"
  26. #endif
  27.  
  28. #if ! defined MYSQL_PASSWORD
  29.     #define MYSQL_PASSWORD ""
  30. #endif
  31.  
  32. #if ! defined MySQL_MAX_NAME
  33.     #define MySQL_MAX_NAME (45)
  34. #endif
  35.  
  36. #if ! defined MySQL_MAX_ENTRY
  37.     #define MySQL_MAX_ENTRY (1024)
  38. #endif
  39.  
  40. #if ! defined MySQL_MAX_QUERY
  41.     #define MySQL_MAX_QUERY (3024)
  42. #endif
  43.  
  44. #define MySQL_INVALID               (0)
  45. #define MySQL_INVALID_KEY           (0)
  46.  
  47. // variables for mysql
  48.  
  49. new mysql;
  50. new g_Query[MySQL_MAX_QUERY];
  51.  
  52. //column types enum
  53. enum DBType
  54. {
  55.     TYPE_NUMBER,
  56.     TYPE_FLOAT,
  57.     TYPE_STRING
  58. }
  59.  
  60. stock MySQLConnect(const host[] = MYSQL_SERVER, const user[] = MYSQL_UID, const database[] = MYSQL_DATABASE, const password[] = MYSQL_PASSWORD, port = 3306, bool:autoreconnect = true, pool_size = 2)
  61. {
  62.     mysql = mysql_connect(host, user, database, password, port, autoreconnect, pool_size);
  63.     if(mysql_errno() == 0 )
  64.     {
  65.         printf("Connection to the host and database has been established. (Host: %s | DB: %s)", host, database);
  66.         print("EasyMySQL build 1.2 has been loaded");
  67.         return 1;
  68.     }
  69.     else
  70.     {
  71.         printf("Connection to the host and database could not be established. (Host: %s | DB: %s)", host, database);
  72.         return 0;
  73.     }
  74. }
  75.  
  76. stock MySQLDisconnect()
  77. {
  78.     mysql_close(mysql);
  79.     return 1;
  80. }
  81.  
  82. stock VerifyTable(table[], key[], bool:ai = true)
  83. {
  84.     g_Query[0] = EOS;
  85.  
  86.     strcat(g_Query, "SELECT * FROM `");
  87.     strcat(g_Query, table);
  88.     strcat(g_Query, "` LIMIT 1");
  89.  
  90.     new Cache:result = mysql_query(mysql, g_Query);
  91.    
  92.     if(!result)
  93.     {
  94.         printf("Table '%s' does not seem to exist; attempting to create it.", table);
  95.        
  96.         g_Query[0] = EOS;
  97.        
  98.         strcat(g_Query, "CREATE TABLE `");
  99.         strcat(g_Query, table);
  100.         strcat(g_Query, "` (`");
  101.         strcat(g_Query, key);
  102.         if(ai == true)
  103.         {
  104.             strcat(g_Query, "` INTEGER PRIMARY KEY AUTO_INCREMENT)");
  105.         }
  106.         else strcat(g_Query, "` INTEGER PRIMARY KEY)");
  107.        
  108.         mysql_query(mysql, g_Query);
  109.        
  110.         g_Query[0] = EOS;
  111.        
  112.         strcat(g_Query, "SELECT * FROM `");
  113.         strcat(g_Query, table);
  114.         strcat(g_Query, "` LIMIT 1");
  115.  
  116.         result = mysql_query(mysql, g_Query);
  117.        
  118.         if(result)
  119.         {
  120.             printf("Table '%s' has been created successfully; key: %s.", table, key);
  121.             cache_delete(result);
  122.         }
  123.         else
  124.         {
  125.             printf("Table '%s' could not be created.", table, key);
  126.             cache_delete(result);
  127.         }
  128.     }
  129.     return 1;
  130. }
  131.  
  132. stock VerifyColumn(table[], column[], DBType:type, len, { _,Float }:...)
  133. {
  134.     g_Query[0] = EOS;
  135.     strcat(g_Query, "SELECT `");
  136.     strcat(g_Query, column);
  137.     strcat(g_Query, "` FROM `");
  138.     strcat(g_Query, table);
  139.     strcat(g_Query, "` LIMIT 1");
  140.  
  141.     new Cache:result = mysql_query(mysql, g_Query);
  142.     if (! result)
  143.     {
  144.         printf("The column \"%s\" doesn't exist; attempting to create it.", column);
  145.  
  146.         switch (type)
  147.         {
  148.             case TYPE_NUMBER:
  149.             {
  150.                 new default_value = 0;
  151.                 if (numargs() == 4)
  152.                 {
  153.                     default_value = getarg(3);
  154.                 }
  155.  
  156.                 format(g_Query, MySQL_MAX_QUERY, "ALTER TABLE `%s` ADD COLUMN `%s` INTEGER(%d) DEFAULT %d", table, column, len,  default_value);
  157.             }
  158.  
  159.             case TYPE_FLOAT:
  160.             {
  161.                 new Float:default_value = 0.0;
  162.                 if (numargs() != 4)
  163.                 {
  164.                     default_value = Float:getarg(3);
  165.                 }
  166.  
  167.                 format(g_Query, MySQL_MAX_QUERY, "ALTER TABLE `%s` ADD COLUMN `%s` FLOAT(%d) DEFAULT %f", table, column, len, default_value);
  168.             }
  169.  
  170.             case TYPE_STRING:
  171.             {
  172.                 new default_value[1024 * 2] = "";
  173.                 if (numargs() == 4)
  174.                 {
  175.                     getstringarg(default_value, 3);
  176.                 }
  177.  
  178.                 format(g_Query, MySQL_MAX_QUERY, "ALTER TABLE `%s` ADD COLUMN `%s` VARCHAR(%d) DEFAULT '%s'", table, column, len,  default_value);
  179.             }
  180.         }
  181.  
  182.         result = mysql_query(mysql, g_Query);
  183.  
  184.         if (result)
  185.         {
  186.             cache_delete(result);
  187.             return 1;
  188.         }
  189.         else
  190.         {
  191.             cache_delete(result);
  192.             printf("Failed to create the column \"%s\"; this could be because of an invalid column name.", column);
  193.             return MySQL_INVALID;
  194.         }
  195.     }
  196.     return 1;
  197. }
  198.  
  199. stock RetrieveKey(table[], key[], keys[] = "", size = sizeof (keys), const column[], { _,Float }:...)
  200. {
  201.     if (numargs() != 6)
  202.     {
  203.         return MySQL_INVALID_KEY;
  204.     }
  205.  
  206.     new args[MySQL_MAX_ENTRY * 2];
  207.     getstringarg(args, 5);
  208.  
  209.     mysql_format(mysql, g_Query, MySQL_MAX_QUERY, "SELECT `%s` FROM `%s` WHERE `%s` = '%e'", key, table, column, args);
  210.  
  211.     new Cache:result = mysql_query(mysql, g_Query);
  212.    
  213.     if (result)
  214.     {
  215.         if (size == 1)
  216.         {
  217.             new keyx = cache_get_field_content_int(0, key);
  218.             if(keyx != MySQL_INVALID_KEY)
  219.             {
  220.                 return keyx;
  221.             }
  222.         }
  223.         else
  224.         {
  225.             new rows = cache_num_rows();
  226.  
  227.             for (new i; i < size; i++)
  228.             {
  229.                 if (i < rows)
  230.                 {
  231.                     keys[i] = cache_get_field_content_int(i, key);
  232.                 }
  233.                 else
  234.                 {
  235.                     keys[i] = MySQL_INVALID_KEY;
  236.                 }
  237.             }
  238.            
  239.             return keys[0];
  240.         }
  241.     }
  242.  
  243.     cache_delete(result);
  244.    
  245.     return MySQL_INVALID_KEY;
  246. }
  247.  
  248. stock CreateRow(table[], const column[], { _,Float }:...)
  249. {
  250.     if (numargs() != 3)
  251.     {
  252.         return false;
  253.     }
  254.  
  255.     new args[MySQL_MAX_ENTRY * 2];
  256.     getstringarg(args, 2);
  257.  
  258.     mysql_format(mysql, g_Query, MySQL_MAX_QUERY, "INSERT INTO `%s`(`%s`) VALUES('%e')", table, column, args);
  259.     new Cache:result = mysql_query(mysql, g_Query);
  260.     new id = cache_insert_id();
  261.     cache_delete(result);
  262.     return id;
  263. }
  264.  
  265. stock SetIntEntry(table[], key[], keys, const column[], value)
  266. {
  267.     g_Query[0] = EOS;
  268.  
  269.     format(g_Query, MySQL_MAX_QUERY, "UPDATE `%s` SET `%s` = '%d' WHERE `%s` = '%d'", table, column, value, key, keys);
  270.  
  271.     mysql_tquery(mysql, g_Query, "", "");
  272.     if(mysql_errno() == 0 )
  273.     {
  274.         return 1;
  275.     }
  276.     else
  277.     {
  278.         return 0;
  279.     }
  280. }
  281.  
  282. stock SetFloatEntry(table[], key[], keys, const column[], Float:value)
  283. {
  284.     format(g_Query, MySQL_MAX_QUERY, "UPDATE `%s` SET `%s` = '%f' WHERE `%s` = '%d'", table, column, value, key, keys);
  285.     mysql_tquery(mysql, g_Query, "", "");
  286.     if(mysql_errno() == 0 )
  287.     {
  288.         return 1;
  289.     }
  290.     else
  291.     {
  292.         return 0;
  293.     }
  294. }
  295.  
  296. stock SetStringEntry(table[], key[], keys, const column[], const value[])
  297. {
  298.     mysql_format(mysql, g_Query, MySQL_MAX_QUERY, "UPDATE `%s` SET `%s` = '%q' WHERE `%s` = '%d'", table, column, value, key, keys);
  299.  
  300.     mysql_tquery(mysql, g_Query, "", "");
  301.     if(mysql_errno() == 0 )
  302.     {
  303.         return 1;
  304.     }
  305.     else
  306.     {
  307.         return 0;
  308.     }
  309. }
  310.  
  311. stock bool:MultiSet(table[], key[], keys, const fmat[], {_, Float}:...)
  312. {
  313.     g_Query[0] = EOS;
  314.  
  315.     for (new i, j = strlen(fmat); i < j; i++)
  316.     {
  317.         switch (fmat[i])
  318.         {
  319.             case 'i', 'd', 'f', 's':
  320.             {
  321.             }
  322.             default:
  323.             {
  324.                 return false;
  325.             }
  326.         }
  327.     }
  328.  
  329.     new column[MySQL_MAX_NAME];
  330.     new column_arg;
  331.     new value[MySQL_MAX_ENTRY * 2];
  332.  
  333.     g_Query = "UPDATE `";
  334.     strcat(g_Query, table);
  335.     strcat(g_Query, "` SET ");
  336.  
  337.     for (new i, j = strlen(fmat); i < j; i++)
  338.     {
  339.         column_arg = 4 + i * 2;
  340.  
  341.         getstringarg(column, column_arg);
  342.  
  343.         ++column_arg;
  344.  
  345.         strcat(g_Query, "`");
  346.         strcat(g_Query, column);
  347.         strcat(g_Query, "` = '");
  348.  
  349.         switch (fmat[i])
  350.         {
  351.             case 'i', 'd':
  352.             {
  353.                 valstr(value, getarg(column_arg));
  354.             }
  355.             case 'f':
  356.             {
  357.                 format(value, sizeof(value), "%f", Float:getarg(column_arg));
  358.             }
  359.             case 's':
  360.             {
  361.                 getstringarg(value, column_arg, MySQL_MAX_ENTRY);
  362.             }
  363.         }
  364.  
  365.         strcat(g_Query, value);
  366.         strcat(g_Query, "'");
  367.  
  368.         if (i < j - 1)
  369.         {
  370.             strcat(g_Query, ", ");
  371.         }
  372.     }
  373.  
  374.     valstr(value, keys);
  375.  
  376.     strcat(g_Query, " WHERE `");
  377.     strcat(g_Query, key);
  378.     strcat(g_Query, "` = '");
  379.     strcat(g_Query, value);
  380.     strcat(g_Query, "'");
  381.  
  382.     mysql_tquery(mysql, g_Query, "", "");
  383.     return true;
  384. }
  385.  
  386. stock bool:MultiGet(table[], key[], keys, const fmat[], {_, Float}:...)
  387. {
  388.     new column[MySQL_MAX_NAME];
  389.     new column_arg;
  390.  
  391.     g_Query = "SELECT `";
  392.  
  393.     for (new i, j = strlen(fmat); i < j; i++)
  394.     {
  395.         column_arg = 4 + i * 2;
  396.  
  397.         getstringarg(column, column_arg);
  398.  
  399.         strcat(g_Query, column);
  400.  
  401.         if (i < j - 1)
  402.         {
  403.             strcat(g_Query, "`, `");
  404.         }
  405.     }
  406.  
  407.     strcat(g_Query, "` FROM `");
  408.     strcat(g_Query, table);
  409.     strcat(g_Query, "` WHERE `");
  410.     strcat(g_Query, key);
  411.     strcat(g_Query, "` = '");
  412.    
  413.     new valuex[11];
  414.     valstr(valuex,keys);
  415.    
  416.     strcat(g_Query, valuex);
  417.     strcat(g_Query, "'");
  418.  
  419.     new Cache:result = mysql_query(mysql, g_Query);
  420.    
  421.     if (result)
  422.     {
  423.         if (! cache_num_rows())
  424.         {
  425.             cache_delete(result);
  426.             return false;
  427.         }
  428.  
  429.         new str[MySQL_MAX_ENTRY];
  430.         for (new i, j = cache_num_rows(); i < j; i++)
  431.         {
  432.             new arg[50];
  433.             format(arg, sizeof arg, column);
  434.             switch (fmat[i])
  435.             {
  436.                 case 'i', 'd':
  437.                 {
  438.                     setarg((4 + i * 2) + 1, .value = cache_get_field_content_int(i, arg));
  439.                 }
  440.                 case 'f':
  441.                 {
  442.                     setarg((4 + i * 2) + 1, .value = _:cache_get_field_content_float(i, arg));
  443.                 }
  444.                 case 's':
  445.                 {
  446.                     cache_get_field_content(i, arg, str);
  447.                     setstringarg((4 + i * 2) + 1, str);
  448.                 }
  449.             }
  450.         }
  451.  
  452.         cache_delete(result);
  453.         return true;
  454.     }
  455.     return false;
  456. }
  457.  
  458. forward OnNewRowIsCreated();
  459. public OnNewRowIsCreated()
  460. {
  461.     return cache_insert_id();
  462. }
  463.  
  464. stock static setstringarg(arg, const value[], len = sizeof (value))
  465. {
  466.     new address;
  467.  
  468.     // Get the address of the previous function's stack.  First get the index of
  469.     // the argument required.
  470.     #emit LOAD.S.pri arg
  471.  
  472.     // Then convert that number to bytes from cells.
  473.     #emit SMUL.C     4
  474.  
  475.     // Get the previous function's frame.
  476.     #emit LOAD.S.alt 0
  477.  
  478.     // Add the frame pointer to the argument offset in bytes.
  479.     #emit ADD
  480.  
  481.     // Add 12 to skip over the function header.
  482.     #emit ADD.C      12
  483.  
  484.     // Load the address stored in the specified address.
  485.     #emit LOAD.I
  486.     #emit STOR.S.PRI address
  487.  
  488.     // Push the length (last argument first)
  489.     #emit PUSH.S     len
  490.  
  491.     // Push the new value (source) value
  492.     #emit PUSH.S     value
  493.  
  494.     // Blank out the first cell of the argument
  495.     #emit CONST.pri  0
  496.     #emit SREF.S.pri address
  497.  
  498.     // Push the destination
  499.     #emit PUSH.S     address
  500.  
  501.     // Push the number of parameters passed (in bytes) to the function.
  502.     #emit PUSH.C     12
  503.  
  504.     // Call the function.
  505.     #emit SYSREQ.C   strcat
  506.  
  507.     // Restore the stack to its level before we called this native.
  508.     #emit STACK      16
  509.     return 1;
  510. }
  511.  
  512. stock static getstringarg(dest[], arg, len = sizeof (dest))
  513. {
  514.     // Get the address of the previous function's stack.  First get the index of
  515.     // the argument required.
  516.     #emit LOAD.S.pri arg
  517.     // Then convert that number to bytes from cells.
  518.     #emit SMUL.C     4
  519.     // Get the previous function's frame.  Stored in variable 0 (in the current
  520.     // frame).  Parameters are FRM+n+12, locals are FRM-n, previous frame is
  521.     // FRM+0, return address is FRM+4, parameter count is FRM+8.  We could add
  522.     // checks that "arg * 4 < *(*(FRM + 0) + 8)", for the previous frame parameter
  523.     // count (in C pointer speak).
  524.     #emit LOAD.S.alt 0
  525.     // Add the frame pointer to the argument offset in bytes.
  526.     #emit ADD
  527.     // Add 12 to skip over the function header.
  528.     #emit ADD.C      12
  529.     // Load the address stored in the specified address.
  530.     #emit LOAD.I
  531.     // Push the length for "strcat".
  532.     #emit PUSH.S     len
  533.     // Push the address we just determined was the source.
  534.     #emit PUSH.pri
  535.     // Load the address of the destination.
  536.     #emit LOAD.S.alt dest
  537.     // Blank the first cell so "strcat" behaves like "strcpy".
  538.     #emit CONST.pri  0
  539.     // Store the loaded number 0 to the loaded address.
  540.     #emit STOR.I
  541.     // Push the loaded address.
  542.     #emit PUSH.alt
  543.     // Push the number of parameters passed (in bytes) to the function.
  544.     #emit PUSH.C     12
  545.     // Call the function.
  546.     #emit SYSREQ.C   strcat
  547.     // Restore the stack to its level before we called this native.
  548.     #emit STACK      16
  549.     return 1;
  550. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement