Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /* SA-MP Include MySQL Libary
- *
- * (c) by Kaliber, 2015
- *
- */
- #if !defined _samp_included
- #include <a_samp>
- #endif
- #if !defined mysql_included
- #include <a_mysql>
- #endif
- #if defined _m_Lib
- #endinput
- #endif
- #define _m_Lib
- #if !defined MYSQL_STACK
- #define MYSQL_STACK 1024
- #endif
- /*
- native MySQL_Insert(handle,const table[][],const target_Table[],const form[],{Float, _}:...);
- native MySQL_Update(handle,const table[][],const target_Table[],const where[], const form[],{Float, _}:...);
- */
- stock static count(const table[][]) {
- new i;
- for( ;; i++) {
- switch(table[i][0]) {
- case '`': continue;
- default: break;
- }
- }
- return i-1;
- }
- stock static Get(const string[],bool:x) {
- new buffer[64],p=strfind(string,"|");
- strcat(buffer,string,sizeof buffer);
- if(p != -1) {
- switch(x) {
- case false: strdel(buffer,p,strlen(buffer));
- case true: strdel(buffer,0,p+1);
- }
- }
- return strcat(buffer,"`=",sizeof buffer),buffer;
- }
- stock static c2(const string[]) {
- new i,c;
- for( ;; i++) {
- switch(string[i]) {
- case 'w': c++;
- case EOS: break;
- }
- }
- return c;
- }
- stock MySQL_Insert(MySQL,const table[][],const t_n[],const form[],{Float, _}:...)
- {
- new size=count(table);
- if(size != strlen(form)) return print(!"MySQL Fehler (INSERT): Felder & Spalten haben nicht die selbe Anzahl!");
- new string[MYSQL_STACK] = "INSERT INTO `",tmp[32],i;
- const params = 4;
- strcat(string,t_n,MYSQL_STACK),strcat(string,"` (",MYSQL_STACK);
- for( ; i<size ; i++) {
- strcat(string,table[i],MYSQL_STACK);
- strcat(string,((i==size-1)?(")"):(",")),MYSQL_STACK);
- }
- strcat(string," VALUES (",MYSQL_STACK);
- for(i=0 ;; i++) {
- switch(form[i]) {
- case 'd','i': valstr(tmp,getarg(params+i,0)),strcat(string,tmp,MYSQL_STACK);
- case 'f': format(tmp,sizeof tmp,"'%f'",getarg(params+i,0)),strcat(string,tmp,MYSQL_STACK);
- case 's': {
- tmp[0] = EOS;
- for(new z; z<sizeof tmp; z++) {
- tmp[z] = getarg(params+i,z);
- switch(tmp[z]) {
- case EOS: break;
- }
- }
- mysql_escape_string(tmp,tmp,MySQL,MYSQL_STACK);
- strcat(string,"'",MYSQL_STACK),strcat(string,tmp,MYSQL_STACK),strcat(string,"'",MYSQL_STACK);
- }
- }
- switch(form[i+1]) {
- case EOS: break;
- }
- switch(form[i]) {
- case 'd','i','s','f': strcat(string,",");
- }
- }
- strcat(string,")",MYSQL_STACK);
- mysql_function_query(MySQL,string,false,"","");
- return 1;
- }
- stock MySQL_Update(MySQL,const table[][],const t_n[],const where[],const form[],{Float, _}:...)
- {
- const params = 5;
- new l = c2(form);
- if(!l || !(numargs()-params)) return print(!"MySQL Fehler (UPDATE): Bitte eine WHERE Bedingung angeben!");
- new string[MYSQL_STACK] = "UPDATE `",tmp[32],i,bool:w,t;
- strcat(string,t_n,MYSQL_STACK),strcat(string,"` SET ",MYSQL_STACK);
- for( ;; i++)
- {
- switch(form[i]) {
- case 'w': {}
- default: {
- if(form[i+1] != EOS && !w) strcat(string,table[i],MYSQL_STACK),strcat(string,"=",MYSQL_STACK);
- }
- }
- switch(form[i])
- {
- case 'd','i': valstr(tmp,getarg(params-t+i,0)),strcat(string,tmp,MYSQL_STACK);
- case 'f': format(tmp,sizeof tmp,"'%f'",getarg(params+i,0)),strcat(string,tmp,MYSQL_STACK);
- case 's': {
- tmp[0] = EOS;
- for(new z,o=params-t+i;z<sizeof tmp;z++) {
- tmp[z] = getarg(o,z);
- switch(tmp[z]) {
- case EOS: break;
- }
- }
- mysql_escape_string(tmp,tmp,MySQL,MYSQL_STACK);
- strcat(string,"'",MYSQL_STACK),strcat(string,tmp,MYSQL_STACK),strcat(string,"'",MYSQL_STACK);
- }
- case 'w': {
- switch(w) {
- case false: w=true,strcat(string," WHERE `",MYSQL_STACK),strcat(string,Get(where,false),MYSQL_STACK);
- case true: strcat(string," AND `",MYSQL_STACK),strcat(string,Get(where,true),MYSQL_STACK);
- }
- t++;
- }
- }
- switch(form[i+1]) {
- case 'w': continue;
- case EOS: break;
- }
- switch(form[i]) {
- case 'd','i','s','f': strcat(string,",");
- }
- }
- mysql_function_query(MySQL,string,false,"","");
- return 1;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement