Advertisement
Guest User

MySQL Library

a guest
Jul 7th, 2015
283
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.16 KB | None | 0 0
  1. /* SA-MP Include MySQL Libary
  2.  *
  3.  * (c) by Kaliber, 2015
  4.  *
  5.  */
  6. #if !defined _samp_included
  7.     #include <a_samp>
  8. #endif
  9. #if !defined mysql_included
  10.     #include <a_mysql>
  11. #endif
  12. #if defined _m_Lib
  13.     #endinput
  14. #endif
  15. #define _m_Lib
  16. #if !defined MYSQL_STACK
  17.     #define MYSQL_STACK 1024
  18. #endif
  19. /*
  20.     native MySQL_Insert(handle,const table[][],const target_Table[],const form[],{Float, _}:...);
  21.     native MySQL_Update(handle,const table[][],const target_Table[],const where[], const form[],{Float, _}:...);
  22. */
  23. stock static count(const table[][]) {
  24.     new i;
  25.     for( ;; i++) {
  26.         switch(table[i][0]) {
  27.         case '`': continue;
  28.         default: break;
  29.         }
  30.     }
  31.     return i-1;
  32. }
  33. stock static Get(const string[],bool:x) {
  34.     new buffer[64],p=strfind(string,"|");
  35.     strcat(buffer,string,sizeof buffer);
  36.    
  37.     if(p != -1) {
  38.         switch(x) {
  39.         case false: strdel(buffer,p,strlen(buffer));
  40.         case true: strdel(buffer,0,p+1);
  41.     }
  42.     }
  43.     return strcat(buffer,"`=",sizeof buffer),buffer;   
  44. }
  45. stock static c2(const string[]) {
  46.     new i,c;
  47.     for( ;; i++) {
  48.         switch(string[i]) {
  49.         case 'w': c++;
  50.         case EOS: break;
  51.         }
  52.     }
  53.     return c;
  54. }
  55. stock MySQL_Insert(MySQL,const table[][],const t_n[],const form[],{Float, _}:...)
  56. {
  57.     new size=count(table);
  58.     if(size != strlen(form)) return print(!"MySQL Fehler (INSERT): Felder & Spalten haben nicht die selbe Anzahl!");
  59.     new string[MYSQL_STACK] = "INSERT INTO `",tmp[32],i;
  60.     const params = 4;
  61.     strcat(string,t_n,MYSQL_STACK),strcat(string,"` (",MYSQL_STACK);
  62.     for( ; i<size ; i++) {
  63.         strcat(string,table[i],MYSQL_STACK);
  64.         strcat(string,((i==size-1)?(")"):(",")),MYSQL_STACK);
  65.     }
  66.     strcat(string," VALUES (",MYSQL_STACK);
  67.     for(i=0 ;; i++) {
  68.         switch(form[i]) {
  69.             case 'd','i': valstr(tmp,getarg(params+i,0)),strcat(string,tmp,MYSQL_STACK);
  70.             case 'f': format(tmp,sizeof tmp,"'%f'",getarg(params+i,0)),strcat(string,tmp,MYSQL_STACK);
  71.             case 's': {
  72.                 tmp[0] = EOS;
  73.                 for(new z; z<sizeof tmp; z++) {
  74.                     tmp[z] = getarg(params+i,z);
  75.                     switch(tmp[z]) {
  76.                         case EOS: break;
  77.                     }
  78.                 }
  79.                 mysql_escape_string(tmp,tmp,MySQL,MYSQL_STACK);
  80.                 strcat(string,"'",MYSQL_STACK),strcat(string,tmp,MYSQL_STACK),strcat(string,"'",MYSQL_STACK);
  81.             }
  82.         }
  83.         switch(form[i+1]) {
  84.             case EOS: break;
  85.         }
  86.         switch(form[i]) {
  87.             case 'd','i','s','f': strcat(string,",");
  88.         }
  89.     }
  90.     strcat(string,")",MYSQL_STACK);
  91.     mysql_function_query(MySQL,string,false,"","");
  92.     return 1;
  93. }
  94. stock MySQL_Update(MySQL,const table[][],const t_n[],const where[],const form[],{Float, _}:...)
  95. {
  96.     const params = 5;
  97.     new l = c2(form);
  98.     if(!l || !(numargs()-params)) return print(!"MySQL Fehler (UPDATE): Bitte eine WHERE Bedingung angeben!");
  99.     new string[MYSQL_STACK] = "UPDATE `",tmp[32],i,bool:w,t;
  100.     strcat(string,t_n,MYSQL_STACK),strcat(string,"` SET ",MYSQL_STACK);
  101.     for( ;; i++)
  102.     {
  103.         switch(form[i]) {
  104.             case 'w': {}
  105.             default: {
  106.                 if(form[i+1] != EOS && !w) strcat(string,table[i],MYSQL_STACK),strcat(string,"=",MYSQL_STACK);
  107.             }
  108.         }
  109.         switch(form[i])
  110.         {
  111.             case 'd','i': valstr(tmp,getarg(params-t+i,0)),strcat(string,tmp,MYSQL_STACK);
  112.             case 'f': format(tmp,sizeof tmp,"'%f'",getarg(params+i,0)),strcat(string,tmp,MYSQL_STACK);
  113.             case 's': {
  114.                 tmp[0] = EOS;
  115.                 for(new z,o=params-t+i;z<sizeof tmp;z++) {
  116.                     tmp[z] = getarg(o,z);
  117.                     switch(tmp[z]) {
  118.                         case EOS: break;
  119.                     }
  120.                 }
  121.                 mysql_escape_string(tmp,tmp,MySQL,MYSQL_STACK);
  122.                 strcat(string,"'",MYSQL_STACK),strcat(string,tmp,MYSQL_STACK),strcat(string,"'",MYSQL_STACK);
  123.             }
  124.             case 'w': {
  125.                 switch(w) {
  126.                     case false: w=true,strcat(string," WHERE `",MYSQL_STACK),strcat(string,Get(where,false),MYSQL_STACK);
  127.                     case true: strcat(string," AND `",MYSQL_STACK),strcat(string,Get(where,true),MYSQL_STACK);
  128.                 }
  129.                 t++;
  130.             }
  131.         }
  132.         switch(form[i+1]) {
  133.             case 'w': continue;
  134.             case EOS: break;
  135.         }
  136.         switch(form[i]) {
  137.             case 'd','i','s','f': strcat(string,",");
  138.         }
  139.     }
  140.     mysql_function_query(MySQL,string,false,"","");
  141.     return 1;
  142. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement