Advertisement
gamer931215

YourSQL Library 2.3

Mar 22nd, 2011
831
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Pawn 4.55 KB | None | 0 0
  1. /*
  2.     YourSQL Library by Gamer931215
  3.     MYSQL Simplified in pawn
  4.     V2.3 Release ©2011
  5.  
  6.     native yoursql_connect(host[],user[],table[],password[])
  7.     native yoursql_insert(table[],type[],{Float,_}...)
  8.     native yoursql_receive(strquery[])
  9.     native yoursql_update(table[],updater[],identifier[])
  10.     native yoursql_delete(table[],identifier[])
  11.     native yoursql_countrows(strquery)
  12.     native yoursql_escape_string(str[]);
  13.     native yoursql_close();
  14. */
  15. #include <a_samp>
  16. #include <a_mysql>
  17. new connection = -1;
  18.  
  19. #if defined FILTERSCRIPT
  20.  
  21.     public OnFilterScriptExit()
  22.     {
  23.         mysql_close(connection);
  24.         return CallLocalFunction("yoursql_OnFilterScriptExit","");
  25.     }
  26.     #if defined _ALS_OnFilterScriptExit
  27.         #undef OnFilterScriptExit
  28.     #else
  29.         #define _ALS_OnFilterScriptExit
  30.     #endif
  31.     #define OnFilterScriptExit yoursql_OnFilterScriptExit
  32.     forward yoursql_OnFilterScriptExit();
  33.  
  34. #else
  35.  
  36.     public OnGameModeExit()
  37.     {
  38.         mysql_close(connection);
  39.         return CallLocalFunction("yoursql_OnGameModeExit","");
  40.     }
  41.     #if defined _ALS_OnGameModeExit
  42.         #undef OnGameModeExit
  43.     #else
  44.         #define _ALS_OnGameModeExit
  45.     #endif
  46.     #define OnGameModeExit yoursql_OnGameModeExit
  47.     forward yoursql_OnGameModeExit();
  48.  
  49. #endif
  50.  
  51. stock yoursql_connect(host[],user[],table[],password[])
  52. {
  53.     printf("yoursql => Connecting to %s...",host);
  54.     connection = mysql_connect(host,user,table,password);
  55.     if(mysql_ping(connection) == 1)
  56.     {
  57.         printf("yoursql => Connection to %s succesfully established! (Connection ID %i).",host,connection);
  58.     } else {
  59.         printf("yoursql => Connection to %s failed!",host);
  60.     }
  61.     return 0;
  62. }
  63.  
  64. stock yoursql_close()
  65. {
  66.     mysql_close(connection);
  67. }
  68.  
  69. stock yoursql_insert(table[],type[],{Float,_}:...)
  70. {
  71.     new query[256],values[128];
  72.     for(new i = 0;i<numargs();i++)
  73.     {
  74.         switch(type[i])
  75.         {
  76.             case 's':
  77.             {
  78.                 new result[32];
  79.                 for(new a= 0;getarg(i +2,a) != 0;a++)
  80.                 {
  81.                     result[a] = getarg(i +2,a);
  82.                 }
  83.                 if(!strlen(values))
  84.                 {
  85.                     format(values,sizeof values,"'%s'",result);
  86.                 } else format(values,sizeof values,"%s, '%s'",values,result);
  87.             }
  88.  
  89.             case 'i':
  90.             {
  91.                 new result = getarg(i +2);
  92.                 if(!strlen(values))
  93.                 {
  94.                     format(values,sizeof values,"%i",result);
  95.                 } else format(values,sizeof values,"%s, %i",values,result);
  96.             }
  97.  
  98.             case 'f':
  99.             {
  100.                 new Float:result = Float:getarg(i +2);
  101.                 if(!strlen(values))
  102.                 {
  103.                     format(values,sizeof values,"%f",result);
  104.                 } else format(values,sizeof values,"%s, %f",values,result);
  105.             }
  106.         }
  107.     }
  108.     format(query,sizeof query,"INSERT INTO %s (%s) VALUES (%s)",table,GetStructure(table),values);
  109.     return mysql_query(query,-1,-1,connection);
  110. }
  111.  
  112. stock yoursql_update(table[],updater[],identifier[])
  113. {
  114.     new query[256];format(query,sizeof query,"UPDATE %s SET %s WHERE %s",table,updater,identifier);
  115.     return mysql_query(query,-1,-1,connection);
  116. }
  117.  
  118. stock yoursql_delete(table[],identifier[])
  119. {
  120.     new query[128];format(query,sizeof query,"DELETE FROM %s WHERE %s",table,identifier);
  121.     return mysql_query(query,-1,-1,connection);
  122. }
  123.  
  124. stock yoursql_receive(table[],identifier[])
  125. {
  126.     new query[128];format(query,sizeof query,"SELECT * FROM %s WHERE %s",table,identifier);
  127.     if(mysql_query(query,-1,-1,connection) == 0) return query;
  128.     mysql_store_result(connection);
  129.  
  130.     if(mysql_fetch_row_format(query,"|",connection))
  131.     {
  132.         mysql_free_result(connection);
  133.         return query;
  134.     }
  135.     return query;
  136. }
  137.  
  138. stock yoursql_escape_string(str[])
  139. {
  140.     new string[256];
  141.     mysql_real_escape_string(str,string);
  142.     return string;
  143. }
  144.  
  145. stock yoursql_query(query[])
  146. {
  147.     return mysql_query(query,-1,-1,connection);
  148. }
  149.  
  150. stock yoursql_countrows(strquery[])
  151. {
  152.     new query[96];format(query,sizeof query,"%s",strquery);
  153.     mysql_query(query,-1,-1,connection);
  154.     mysql_store_result(connection);
  155.     new result = mysql_num_rows(connection);
  156.     mysql_free_result(connection);
  157.     return result;
  158. }
  159.  
  160. stock GetStructure(table[])
  161. {
  162.     new query[96];format(query,sizeof query,"SHOW FIELDS FROM %s",table);
  163.     if(!mysql_query(query,-1,-1,connection))
  164.     {
  165.         printf("yoursql => ERROR! Could not execute query: %s",query);
  166.     }
  167.     mysql_store_result(connection);
  168.  
  169.     new temp[32],result[96];
  170.     while(mysql_retrieve_row(connection))
  171.     {
  172.         mysql_fetch_field_row(temp,"Field");
  173.  
  174.         if(!strlen(result))
  175.         {
  176.             format(result,sizeof result,"`%s`",temp);
  177.         } else {
  178.             format(result,sizeof result,"%s,`%s`",result,temp);
  179.         }
  180.     }
  181.     mysql_free_result(connection);
  182.     return result;
  183. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement