a_mysql.inc 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  1. /**
  2. * MySQL plugin R38
  3. */
  4. #if defined mysql_included
  5. #endinput
  6. #endif
  7. #define mysql_included
  8. /**
  9. * Common error codes
  10. *
  11. * Client: http://dev.mysql.com/doc/refman/5.1/en/error-messages-client.html
  12. * Server: http://dev.mysql.com/doc/refman/5.1/en/error-messages-server.html
  13. */
  14. #define ER_DBACCESS_DENIED_ERROR 1044
  15. #define ER_ACCESS_DENIED_ERROR 1045
  16. #define ER_UNKNOWN_TABLE 1109
  17. #define ER_SYNTAX_ERROR 1149
  18. #define CR_SERVER_GONE_ERROR 2006
  19. #define CR_SERVER_LOST 2013
  20. #define CR_COMMAND_OUT_OF_SYNC 2014
  21. #define CR_SERVER_LOST_EXTENDED 2055
  22. enum E_LOGLEVEL
  23. {
  24. LOG_NONE = 0,
  25. LOG_ERROR = 1,
  26. LOG_WARNING = 2,
  27. LOG_DEBUG = 4,
  28. LOG_ALL = LOG_ERROR | LOG_WARNING | LOG_DEBUG
  29. };
  30. enum E_LOGTYPE
  31. {
  32. LOG_TYPE_TEXT = 1,
  33. LOG_TYPE_HTML = 2
  34. };
  35. enum ORM_Error
  36. {
  37. ERROR_OK,
  38. ERROR_NO_DATA
  39. };
  40. enum E_VAR_DATATYPE
  41. {
  42. DATATYPE_INT,
  43. DATATYPE_FLOAT,
  44. DATATYPE_STRING
  45. };
  46. enum E_MYSQL_OPTION
  47. {
  48. DUPLICATE_CONNECTIONS
  49. };
  50. enum E_EXECTIME_UNIT
  51. {
  52. UNIT_MILLISECONDS,
  53. UNIT_MICROSECONDS
  54. };
  55. #define mysql_real_escape_string mysql_escape_string
  56. #define cache_num_fields cache_get_field_count
  57. #define cache_num_rows cache_get_row_count
  58. #define mysql_function_query(%0,%1,%2,%3,"%4"%5) mysql_tquery(%0,%1,%3,#%4%5)
  59. #define mysql_reload(%0) mysql_tquery(%0,"FLUSH PRIVILEGES")
  60. #define mysql_debug(%0) (%0?mysql_log(LOG_ALL):mysql_log())
  61. #define ismysqlnull(%0) (strcmp(%0,"NULL",false)==0)
  62. // ORM functions
  63. native ORM:orm_create(const table[], connectionHandle = 1);
  64. native orm_destroy(ORM:id);
  65. native ORM_Error:orm_errno(ORM:id);
  66. native orm_apply_cache(ORM:id, row);
  67. native orm_select(ORM:id, callback[] = "", format[] = "", {Float, _}:...);
  68. /*
  69. native orm_select_inline(ORM:id, callback:Callback, format[], {Float,_}:...); //y_inline
  70. */
  71. native orm_update(ORM:id);
  72. native orm_insert(ORM:id, callback[] = "", format[] = "", {Float, _}:...);
  73. /*
  74. native orm_insert_inline(ORM:id, callback:Callback, format[], {Float,_}:...); //y_inline
  75. */
  76. native orm_delete(ORM:id, bool:clearvars=true);
  77. native orm_load(ORM:id, callback[] = "", format[] = "", {Float, _}:...) = orm_select;
  78. native orm_save(ORM:id, callback[] = "", format[] = "", {Float, _}:...);
  79. native orm_addvar_int(ORM:id, &var, varname[]);
  80. native orm_addvar_float(ORM:id, &Float:var, varname[]);
  81. native orm_addvar_string(ORM:id, var[], var_maxlen, varname[]);
  82. native orm_delvar(ORM:id, varname[]);
  83. native orm_setkey(ORM:id, varname[]);
  84. // MySQL functions
  85. native mysql_log(E_LOGLEVEL:loglevel = LOG_ERROR | LOG_WARNING, E_LOGTYPE:logtype = LOG_TYPE_TEXT);
  86. native mysql_connect(const host[], const user[], const database[], const password[], port = 3306, bool:autoreconnect = true, pool_size = 2);
  87. native mysql_close(connectionHandle = 1);
  88. native mysql_reconnect(connectionHandle = 1);
  89. native mysql_unprocessed_queries(connectionHandle = 1);
  90. native mysql_current_handle();
  91. native mysql_option(E_MYSQL_OPTION:type, value);
  92. native mysql_errno(connectionHandle = 1);
  93. native mysql_escape_string(const source[], destination[], connectionHandle = 1, max_len = sizeof(destination));
  94. native mysql_format(connectionHandle, output[], len, format[], {Float,_}:...);
  95. native mysql_pquery(connectionHandle, query[], callback[] = "", format[] = "", {Float,_}:...);
  96. /*
  97. native mysql_pquery_inline(connHandle, query[], callback:Callback, format[], {Float,_}:...); //y_inline
  98. */
  99. native mysql_tquery(connectionHandle, query[], callback[] = "", format[] = "", {Float,_}:...);
  100. /*
  101. native mysql_tquery_inline(connHandle, query[], callback:Callback, format[], {Float,_}:...); //y_inline
  102. */
  103. native Cache:mysql_query(conhandle, query[], bool:use_cache = true);
  104. native mysql_stat(destination[], connectionHandle = 1, max_len = sizeof(destination));
  105. native mysql_get_charset(destination[], connectionHandle = 1, max_len = sizeof(destination));
  106. native mysql_set_charset(charset[], connectionHandle = 1);
  107. // Cache functions
  108. native cache_get_data(&num_rows, &num_fields, connectionHandle = 1);
  109. native cache_get_row_count(connectionHandle = 1);
  110. native cache_get_field_count(connectionHandle = 1);
  111. native cache_get_field_name(field_index, destination[], connectionHandle = 1, max_len = sizeof(destination));
  112. native cache_get_row(row, field_idx, destination[], connectionHandle = 1, max_len = sizeof(destination));
  113. native cache_get_row_int(row, field_idx, connectionHandle = 1);
  114. native Float:cache_get_row_float(row, field_idx, connectionHandle = 1);
  115. native cache_get_field_content(row, const field_name[], destination[], connectionHandle = 1, max_len = sizeof(destination));
  116. native cache_get_field_content_int(row, const field_name[], connectionHandle = 1);
  117. native Float:cache_get_field_content_float(row, const field_name[], connectionHandle = 1);
  118. native Cache:cache_save(connectionHandle = 1);
  119. native cache_delete(Cache:cache_id, connectionHandle = 1);
  120. native cache_set_active(Cache:cache_id, connectionHandle = 1);
  121. native cache_is_valid(Cache:cache_id, connectionHandle = 1);
  122. native cache_affected_rows(connectionHandle = 1);
  123. native cache_insert_id(connectionHandle = 1);
  124. native cache_warning_count(connectionHandle = 1);
  125. native cache_get_query_exec_time(E_EXECTIME_UNIT:unit = UNIT_MICROSECONDS);
  126. native cache_get_query_string(destination[], max_len = sizeof(destination));
  127. // Forward declarations
  128. forward OnQueryError(errorid, error[], callback[], query[], connectionHandle);
  129. #if defined MYSQL_USE_YINLINE || defined E_CALLBACK_DATA
  130. #if !defined E_CALLBACK_DATA
  131. #include <YSI\y_inline>
  132. #endif
  133. static g_MySQL_InlineData[1000][E_CALLBACK_DATA];
  134. static g_MySQL_VarArray[32][YSI_MAX_STRING];
  135. static g_MySQL_AddressArray[32];
  136. stock MySQL_Internal_SaveInline(callback:CB)
  137. {
  138. static bool:g_MySQL_LazyInit = true;
  139. if(g_MySQL_LazyInit == true)
  140. {
  141. //set g_MySQL_InlineData empty
  142. for(new i=0; i < sizeof(g_MySQL_InlineData); ++i)
  143. for(new E_CALLBACK_DATA:e = E_CALLBACK_DATA:0; e < E_CALLBACK_DATA; ++e)
  144. g_MySQL_InlineData[i][e] = 0;
  145. g_MySQL_LazyInit = false;
  146. }
  147. for(new i=0; i < sizeof(g_MySQL_InlineData); ++i)
  148. if(_:g_MySQL_InlineData[i][E_CALLBACK_DATA_POINTER] == 0)
  149. if (Callback_Get(CB, g_MySQL_InlineData[i]))
  150. return i;
  151. return -1;
  152. }
  153. #define mysql_pquery_inline(%0,%1,%2,"%3"%4) \
  154. mysql_pquery(%0,%1,"FJ37DH3JG_MYSQL_INTERNAL","d"#%3,MySQL_Internal_SaveInline(%2)%4)
  155. #define mysql_tquery_inline(%0,%1,%2,"%3"%4) \
  156. mysql_tquery(%0,%1,"FJ37DH3JG_MYSQL_INTERNAL","d"#%3,MySQL_Internal_SaveInline(%2)%4)
  157. #define orm_select_inline(%0,%1,"%2"%3) \
  158. orm_select(%0,"FJ37DH3JG_MYSQL_INTERNAL","d"#%2,MySQL_Internal_SaveInline(%1)%3)
  159. #define orm_insert_inline(%0,%1,"%2"%3) \
  160. orm_insert(%0,"FJ37DH3JG_MYSQL_INTERNAL","d"#%2,MySQL_Internal_SaveInline(%1)%3)
  161. forward FJ37DH3JG_MYSQL_INTERNAL(...);
  162. public FJ37DH3JG_MYSQL_INTERNAL(...)
  163. {
  164. new InlineDataIndex = getarg(0);
  165. if(InlineDataIndex < 0)
  166. return 0;
  167. for(new i=0; i < numargs()-1; ++i)
  168. {
  169. for(new l=0; l < YSI_MAX_STRING; ++l)
  170. {
  171. new TmpVal = getarg(i+1, l);
  172. if(l == 0 || g_MySQL_VarArray[i][l-1] < 256)
  173. g_MySQL_VarArray[i][l] = TmpVal;
  174. else
  175. break;
  176. }
  177. g_MySQL_AddressArray[i] = AMX_GetRelativeAddress(g_MySQL_VarArray[i][0]);
  178. }
  179. Callback_Array(g_MySQL_InlineData[InlineDataIndex], g_MySQL_AddressArray);
  180. Callback_Release(g_MySQL_InlineData[InlineDataIndex]);
  181. for(new E_CALLBACK_DATA:e = E_CALLBACK_DATA:0; e < E_CALLBACK_DATA; ++e)
  182. g_MySQL_InlineData[InlineDataIndex][e] = 0;
  183. for(new i=0; i < numargs()-1; ++i)
  184. {
  185. g_MySQL_AddressArray[i] = 0;
  186. for(new l=0, lmax=strlen(g_MySQL_VarArray[i]); l < lmax; ++l)
  187. g_MySQL_VarArray[i][l] = 0;
  188. }
  189. return 1;
  190. }
  191. #endif