Advertisement
Guest User

a_mysql

a guest
Dec 24th, 2016
45
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Pawn 10.19 KB | None | 0 0
  1. /**
  2.  * MySQL plugin R34
  3.  */
  4.  
  5. #if defined mysql_included
  6.     #endinput
  7. #endif
  8. #define mysql_included
  9.  
  10. /**
  11.  * Common error codes
  12.  *
  13.  * Client: http://dev.mysql.com/doc/refman/5.1/en/error-messages-client.html
  14.  * Server: http://dev.mysql.com/doc/refman/5.1/en/error-messages-server.html
  15.  */
  16.  
  17. #define ER_DBACCESS_DENIED_ERROR        1044
  18. #define ER_ACCESS_DENIED_ERROR          1045
  19. #define ER_UNKNOWN_TABLE                1109
  20. #define ER_SYNTAX_ERROR                 1149
  21. #define CR_SERVER_GONE_ERROR            2006
  22. #define CR_SERVER_LOST                  2013
  23. #define CR_COMMAND_OUT_OF_SYNC          2014
  24. #define CR_SERVER_LOST_EXTENDED         2055
  25.  
  26. enum { //log levels
  27.     LOG_NONE = 0,
  28.     LOG_ERROR = 1,
  29.     LOG_WARNING = 2,
  30.     LOG_DEBUG = 4
  31. };
  32.  
  33. enum { //log types
  34.     LOG_TYPE_TEXT = 1,
  35.     LOG_TYPE_HTML = 2
  36. };
  37.  
  38. enum ORM_Error {
  39.     ERROR_OK,
  40.     ERROR_NO_DATA
  41. };
  42.  
  43. enum VarDatatype {
  44.     DATATYPE_INT,
  45.     DATATYPE_FLOAT,
  46.     DATATYPE_STRING
  47. };
  48.  
  49.  
  50. #define mysql_insert_id cache_insert_id
  51. #define mysql_affected_rows cache_affected_rows
  52. #define mysql_warning_count cache_warning_count
  53. #define mysql_real_escape_string mysql_escape_string
  54.  
  55. // cache_num_fields
  56. #define cache_num_fields cache_get_field_count
  57.  
  58. // cache_num_rows
  59. #define cache_num_rows cache_get_row_count
  60.  
  61. // cache_get_field
  62. #define cache_get_field(%0) cache_get_field_name(%0)
  63.  
  64. // mysql_function_query(conHandle, query[], bool:cache, callback[], format[], {Float,_}:...);
  65. #define mysql_function_query(%0,%1,%2,%3,"%4"%5) mysql_tquery(%0,%1,%3,#%4%5)
  66.  
  67. // mysql_fetch_row(destination)
  68. #define mysql_fetch_row(%1) mysql_fetch_row_format(%1,"|")
  69.  
  70. // mysql_next_row()
  71. #define mysql_next_row() mysql_retrieve_row()
  72.  
  73. // mysql_get_field(fieldname, destination)
  74. #define mysql_get_field(%1,%2) mysql_fetch_field_row(%2,%1)
  75.  
  76. // ismysqlnull(string[])
  77. #define ismysqlnull(%1) (strcmp(%1,"NULL",false)==0)
  78.  
  79.  
  80. // ORM natives
  81. native ORM:orm_create(const table[], connectionHandle = 1);
  82. native orm_destroy(ORM:id);
  83.  
  84. native ORM_Error:orm_errno(ORM:id);
  85.  
  86. native orm_apply_cache(ORM:id, row);
  87. native orm_select(ORM:id, callback[] = "", format[] = "", {Float, _}:...);
  88. /*
  89. native orm_select_inline(ORM:id, callback:Callback, format[], {Float,_}:...); //y_inline
  90. */
  91. native orm_update(ORM:id);
  92. native orm_insert(ORM:id, callback[] = "", format[] = "", {Float, _}:...);
  93. /*
  94. native orm_insert_inline(ORM:id, callback:Callback, format[], {Float,_}:...); //y_inline
  95. */
  96. native orm_delete(ORM:id, bool:clearvars=true);
  97.  
  98. native orm_load(ORM:id, callback[] = "", format[] = "", {Float, _}:...) = orm_select;
  99. native orm_save(ORM:id, callback[] = "", format[] = "", {Float, _}:...);
  100.  
  101. native orm_addvar(ORM:id, &{Float, _}:var, VarDatatype:datatype, var_maxlen, varname[]);
  102. /*
  103. native orm_addvar_int(ORM:id, &var, varname[]);
  104. native orm_addvar_float(ORM:id, &Float:var, varname[]);
  105. native orm_addvar_string(ORM:id, &var, var_maxlen, varname[]);
  106. */
  107. #define orm_addvar_int(%0,%1,%2) orm_addvar(%0,%1,DATATYPE_INT,1,%2)
  108. #define orm_addvar_float(%0,%1,%2) orm_addvar(%0,%1,DATATYPE_FLOAT,1,%2)
  109. #define orm_addvar_string(%0,%1,%2,%3) orm_addvar(%0,%1,DATATYPE_STRING,%2,%3)
  110.  
  111. native orm_setkey(ORM:id, varname[]);
  112.  
  113.  
  114. // MySQL natives
  115. native mysql_log(loglevel = LOG_ERROR | LOG_WARNING, logtype = LOG_TYPE_TEXT);
  116. native mysql_connect(const host[], const user[], const database[], const password[], port = 3306, bool:autoreconnect = true);
  117. native mysql_close(connectionHandle = 1, bool:wait = true);
  118. native mysql_reconnect(connectionHandle = 1);
  119. native mysql_unprocessed_queries(connectionHandle = 1);
  120. native mysql_ping(connectionHandle = 1);
  121. native mysql_errno(connectionHandle = 1);
  122. native mysql_escape_string(const source[], destination[], connectionHandle = 1, max_len = sizeof(destination));
  123. native mysql_format(connectionHandle, output[], len, format[], {Float,_}:...);
  124. native mysql_tquery(connectionHandle, query[], callback[], format[], {Float,_}:...);
  125. /*
  126. native mysql_tquery_inline(connHandle, query[], callback:Callback, format[], {Float,_}:...); //y_inline
  127. */
  128. native Cache:mysql_query(conhandle, query[], bool:use_cache = true);
  129.  
  130. native mysql_stat(destination[], connectionHandle = 1, max_len = sizeof(destination));
  131. native mysql_get_charset(destination[], connectionHandle = 1, max_len = sizeof(destination));
  132. native mysql_set_charset(charset[], connectionHandle = 1);
  133.  
  134.  
  135. // Cache functions.
  136. native cache_get_data(&num_rows, &num_fields, connectionHandle = 1);
  137. native cache_get_row_count(connectionHandle = 1);
  138. native cache_get_field_count(connectionHandle = 1);
  139. native cache_get_field_name(field_index, destination[], connectionHandle = 1, max_len = sizeof(destination));
  140.  
  141. native cache_get_row(row, field_idx, destination[], connectionHandle = 1, max_len = sizeof(destination));
  142. native cache_get_row_int(row, field_idx, connectionHandle = 1);
  143. native Float:cache_get_row_float(row, field_idx, connectionHandle = 1);
  144.  
  145. native cache_get_field_content(row, const field_name[], destination[], connectionHandle = 1, max_len = sizeof(destination));
  146. native cache_get_field_content_int(row, const field_name[], connectionHandle = 1);
  147. native Float:cache_get_field_content_float(row, const field_name[], connectionHandle = 1);
  148.  
  149. native Cache:cache_save(connectionHandle = 1);
  150. native cache_delete(Cache:cache_id, connectionHandle = 1);
  151. native cache_set_active(Cache:cache_id, connectionHandle = 1);
  152.  
  153. native cache_affected_rows(connectionHandle = 1);
  154. native cache_insert_id(connectionHandle = 1);
  155. native cache_warning_count(connectionHandle = 1);
  156.  
  157.  
  158. // Forward declarations.
  159. forward OnQueryError(errorid, error[], callback[], query[], connectionHandle);
  160.  
  161.  
  162. #if defined MYSQL_USE_YINLINE || defined E_CALLBACK_DATA
  163.     #if !defined E_CALLBACK_DATA
  164.         #include <YSI\y_inline>
  165.     #endif
  166.    
  167.     new stock g_MySQL_InlineData[1000][E_CALLBACK_DATA];
  168.     new stock g_MySQL_VarArray[32][YSI_MAX_STRING];
  169.     new stock g_MySQL_AddressArray[32];
  170.  
  171.     stock MySQL_Internal_SaveInline(callback:CB) {
  172.         static bool:g_MySQL_LazyInit = true;
  173.         if(g_MySQL_LazyInit == true) {
  174.             //set g_MySQL_InlineData empty
  175.             for(new i=0; i < sizeof(g_MySQL_InlineData); ++i)
  176.                 for(new E_CALLBACK_DATA:e = E_CALLBACK_DATA:0; e < E_CALLBACK_DATA; ++e)
  177.                     g_MySQL_InlineData[i][e] = 0;
  178.             g_MySQL_LazyInit = false;
  179.         }
  180.        
  181.         new InlineData[E_CALLBACK_DATA];
  182.         if (Callback_Get(CB, InlineData))
  183.         {
  184.             for(new i=0; i < sizeof(g_MySQL_InlineData); ++i) {
  185.                 if(g_MySQL_InlineData[i][E_CALLBACK_DATA_POINTER] == 0) {
  186.                     g_MySQL_InlineData[i] = InlineData;
  187.                     return i;
  188.                 }
  189.             }
  190.         }
  191.         return -1;
  192.     }
  193.    
  194.     #define mysql_tquery_inline(%0,%1,%2,"%3"%4) \
  195.         mysql_tquery(%0,%1,"FJ37DH3JG_MYSQL_INTERNAL","d"#%3,MySQL_Internal_SaveInline(%2)%4)
  196.    
  197.     #define orm_select_inline(%0,%1,"%2"%3) \
  198.         orm_select(%0,"FJ37DH3JG_MYSQL_INTERNAL","d"#%2,MySQL_Internal_SaveInline(%1)%3)
  199.    
  200.     #define orm_insert_inline(%0,%1,"%2"%3) \
  201.         orm_insert(%0,"FJ37DH3JG_MYSQL_INTERNAL","d"#%2,MySQL_Internal_SaveInline(%1)%3)
  202.  
  203.  
  204.     forward FJ37DH3JG_MYSQL_INTERNAL(...);
  205.     public FJ37DH3JG_MYSQL_INTERNAL(...) {
  206.         new InlineDataIndex = getarg(0);
  207.         if(InlineDataIndex < 0)
  208.             return 0;
  209.        
  210.         for(new i=0; i < numargs()-1; ++i) {
  211.             for(new l=0; l < YSI_MAX_STRING; ++l) {
  212.                 new TmpVal = getarg(i+1, l);
  213.                 if(l == 0 || g_MySQL_VarArray[i][l-1] < 256)
  214.                     g_MySQL_VarArray[i][l] = TmpVal;
  215.                 else
  216.                     break;
  217.             }
  218.            
  219.             g_MySQL_AddressArray[i] = AMX_GetRelativeAddress(g_MySQL_VarArray[i][0]);
  220.         }
  221.        
  222.         Callback_Array(g_MySQL_InlineData[InlineDataIndex], g_MySQL_AddressArray);
  223.        
  224.         Callback_Release(g_MySQL_InlineData[InlineDataIndex]);
  225.         for(new E_CALLBACK_DATA:e = E_CALLBACK_DATA:0; e < E_CALLBACK_DATA; ++e)
  226.             g_MySQL_InlineData[InlineDataIndex][e] = 0;
  227.        
  228.         for(new i=0; i < numargs()-1; ++i) {
  229.             g_MySQL_AddressArray[i] = 0;
  230.             for(new l=0, lmax=strlen(g_MySQL_VarArray[i]); l < lmax; ++l)
  231.                 g_MySQL_VarArray[i][l] = 0;
  232.         }
  233.         return 1;
  234.     }
  235. #endif
  236.  
  237.  
  238. // General wrappers for backward-compatibility
  239. stock mysql_debug(enabled) {
  240.     if(enabled)
  241.         mysql_log(LOG_WARNING | LOG_ERROR | LOG_DEBUG);
  242.     else
  243.         mysql_log();
  244.     return 1;
  245. }
  246. #pragma deprecated mysql_ping
  247. stock mysql_reload(connectionHandle = 1) {
  248.     mysql_tquery(connectionHandle, "FLUSH PRIVILEGES", "", "");
  249.     return 1;
  250. }
  251.  
  252. static stock RowIndex[20];
  253. static stock bool:RetrieveRow[20];
  254. stock mysql_store_result(connectionHandle = 1) {
  255.     RowIndex[connectionHandle] = 0;
  256.     RetrieveRow[connectionHandle] = false;
  257.    
  258. }
  259. stock mysql_free_result(connectionHandle = 1) {
  260.     #pragma unused connectionHandle
  261. }
  262. stock mysql_num_rows(connectionHandle = 1) {
  263.     return cache_get_row_count(connectionHandle);
  264. }
  265. stock mysql_num_fields(connectionHandle = 1) {
  266.     return cache_get_field_count(connectionHandle);
  267. }
  268. stock mysql_retrieve_row(connectionHandle = 1) {
  269.     if(RetrieveRow[connectionHandle] != false)
  270.         RowIndex[connectionHandle]++;
  271.     else
  272.         RetrieveRow[connectionHandle] = true;
  273.    
  274.     if(RowIndex[connectionHandle] >= cache_get_row_count(connectionHandle))
  275.         return 0;
  276.     return 1;
  277. }
  278. stock mysql_data_seek(offset, connectionHandle = 1) {
  279.     new Rows = cache_get_row_count(connectionHandle);
  280.     if(offset < 0)
  281.         offset = 0;
  282.     else if(offset >= Rows)
  283.         offset = Rows-1;
  284.     RowIndex[connectionHandle] = offset;
  285. }
  286. stock mysql_fetch_field(number, destination[], connectionHandle = 1, max_len=sizeof(destination)) {
  287.     strdel(destination, 0, max_len-1);
  288.     return cache_get_field(number, destination, connectionHandle, max_len);
  289. }
  290. stock mysql_fetch_field_row(destination[], const fieldname[], connectionHandle = 1, max_len=sizeof(destination)) {
  291.     strdel(destination, 0, max_len-1);
  292.     new RIDX = RowIndex[connectionHandle];
  293.     cache_get_field_content(RIDX < 0 ? 0 : RIDX, fieldname, destination, connectionHandle, max_len);
  294. }
  295. stock mysql_fetch_row_format(destination[], const delimiter[] = "|", connectionHandle = 1, max_len=sizeof(destination)) {
  296.     new retrieve = mysql_retrieve_row(connectionHandle);
  297.     strdel(destination, 0, max_len-1);
  298.     new Fields = cache_get_field_count(connectionHandle);
  299.     for(new f=0; f < Fields; f++) {
  300.         if(f != 0) {
  301.             strcat(destination, delimiter, max_len);
  302.            
  303.         }
  304.         new row_val_tmp[512];
  305.         new RIDX = RowIndex[connectionHandle];
  306.         cache_get_row(RIDX < 0 ? 0 : RIDX, f, row_val_tmp, connectionHandle);
  307.         if(f == 0)
  308.             strins(destination, row_val_tmp, 0, max_len);
  309.         else
  310.             strcat(destination, row_val_tmp, max_len);
  311.     }
  312.     return retrieve;
  313. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement