1
0

a_mysql.inc 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  1. /**
  2. * MySQL plugin R39-6
  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_MYSQL_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_MYSQL_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_MYSQL_OPTION
  41. {
  42. DUPLICATE_CONNECTIONS,
  43. LOG_TRUNCATE_DATA
  44. };
  45. enum E_EXECTIME_UNIT
  46. {
  47. UNIT_MILLISECONDS,
  48. UNIT_MICROSECONDS
  49. };
  50. #define mysql_real_escape_string mysql_escape_string
  51. #define cache_num_fields cache_get_field_count
  52. #define cache_num_rows cache_get_row_count
  53. #define mysql_function_query(%0,%1,%2,%3,"%4"%5) mysql_tquery(%0,%1,%3,#%4%5)
  54. #define mysql_reload(%0) mysql_tquery(%0,"FLUSH PRIVILEGES")
  55. #define mysql_debug(%0) (%0?mysql_log(LOG_ALL):mysql_log())
  56. #define ismysqlnull(%0) (strcmp(%0,"NULL",false)==0)
  57. // ORM functions
  58. native ORM:orm_create(const table[], connectionHandle = 1);
  59. native orm_destroy(ORM:id);
  60. native ORM_Error:orm_errno(ORM:id);
  61. native orm_apply_cache(ORM:id, row);
  62. native orm_select(ORM:id, callback[] = "", format[] = "", {Float, _}:...);
  63. /*
  64. native orm_select_inline(ORM:id, callback:Callback, format[], {Float,_}:...); //y_inline
  65. */
  66. native orm_update(ORM:id);
  67. native orm_insert(ORM:id, callback[] = "", format[] = "", {Float, _}:...);
  68. /*
  69. native orm_insert_inline(ORM:id, callback:Callback, format[], {Float,_}:...); //y_inline
  70. */
  71. native orm_delete(ORM:id, bool:clearvars=true);
  72. native orm_load(ORM:id, callback[] = "", format[] = "", {Float, _}:...) = orm_select;
  73. native orm_save(ORM:id, callback[] = "", format[] = "", {Float, _}:...);
  74. native orm_addvar_int(ORM:id, &var, varname[]);
  75. native orm_addvar_float(ORM:id, &Float:var, varname[]);
  76. native orm_addvar_string(ORM:id, var[], var_maxlen, varname[]);
  77. native orm_delvar(ORM:id, varname[]);
  78. native orm_setkey(ORM:id, varname[]);
  79. // MySQL functions
  80. native mysql_log(E_MYSQL_LOGLEVEL:loglevel = LOG_ERROR | LOG_WARNING, E_MYSQL_LOGTYPE:logtype = LOG_TYPE_TEXT);
  81. native mysql_connect(const host[], const user[], const database[], const password[], port = 3306, bool:autoreconnect = true, pool_size = 2);
  82. native mysql_close(connectionHandle = 1);
  83. native mysql_reconnect(connectionHandle = 1);
  84. native mysql_unprocessed_queries(connectionHandle = 1);
  85. native mysql_current_handle();
  86. native mysql_option(E_MYSQL_OPTION:type, value);
  87. native mysql_errno(connectionHandle = 1);
  88. native mysql_escape_string(const source[], destination[], connectionHandle = 1, max_len = sizeof(destination));
  89. native mysql_format(connectionHandle, output[], len, format[], {Float,_}:...);
  90. native mysql_pquery(connectionHandle, query[], callback[] = "", format[] = "", {Float,_}:...);
  91. /*
  92. native mysql_pquery_inline(connHandle, query[], callback:Callback, format[], {Float,_}:...); //y_inline
  93. */
  94. native mysql_tquery(connectionHandle, query[], callback[] = "", format[] = "", {Float,_}:...);
  95. /*
  96. native mysql_tquery_inline(connHandle, query[], callback:Callback, format[], {Float,_}:...); //y_inline
  97. */
  98. native Cache:mysql_query(conhandle, query[], bool:use_cache = true);
  99. native mysql_stat(destination[], connectionHandle = 1, max_len = sizeof(destination));
  100. native mysql_get_charset(destination[], connectionHandle = 1, max_len = sizeof(destination));
  101. native mysql_set_charset(charset[], connectionHandle = 1);
  102. // Cache functions
  103. native cache_get_data(&num_rows, &num_fields, connectionHandle = 1);
  104. native cache_get_row_count(connectionHandle = 1);
  105. native cache_get_field_count(connectionHandle = 1);
  106. native cache_get_field_name(field_index, destination[], connectionHandle = 1, max_len = sizeof(destination));
  107. native cache_get_row(row, field_idx, destination[], connectionHandle = 1, max_len = sizeof(destination));
  108. native cache_get_row_int(row, field_idx, connectionHandle = 1);
  109. native Float:cache_get_row_float(row, field_idx, connectionHandle = 1);
  110. native cache_get_field_content(row, const field_name[], destination[], connectionHandle = 1, max_len = sizeof(destination));
  111. native cache_get_field_content_int(row, const field_name[], connectionHandle = 1);
  112. native Float:cache_get_field_content_float(row, const field_name[], connectionHandle = 1);
  113. native Cache:cache_save(connectionHandle = 1);
  114. native cache_delete(Cache:cache_id, connectionHandle = 1);
  115. native cache_set_active(Cache:cache_id, connectionHandle = 1);
  116. native cache_is_valid(Cache:cache_id, connectionHandle = 1);
  117. native cache_affected_rows(connectionHandle = 1);
  118. native cache_insert_id(connectionHandle = 1);
  119. native cache_warning_count(connectionHandle = 1);
  120. native cache_get_query_exec_time(E_EXECTIME_UNIT:unit = UNIT_MICROSECONDS);
  121. native cache_get_query_string(destination[], max_len = sizeof(destination));
  122. // Forward declarations
  123. forward OnQueryError(errorid, error[], callback[], query[], connectionHandle);
  124. #if defined MYSQL_USE_YINLINE || defined E_CALLBACK_DATA
  125. #if !defined E_CALLBACK_DATA
  126. #include <YSI\y_inline>
  127. #endif
  128. static g_MySQL_InlineData[1000][E_CALLBACK_DATA];
  129. static g_MySQL_VarArray[32][YSI_MAX_STRING];
  130. static g_MySQL_AddressArray[32];
  131. stock MySQL_Internal_SaveInline(callback:CB)
  132. {
  133. static bool:g_MySQL_LazyInit = true;
  134. if(g_MySQL_LazyInit == true)
  135. {
  136. //set g_MySQL_InlineData empty
  137. for(new i=0; i < sizeof(g_MySQL_InlineData); ++i)
  138. for(new E_CALLBACK_DATA:e = E_CALLBACK_DATA:0; e < E_CALLBACK_DATA; ++e)
  139. g_MySQL_InlineData[i][e] = 0;
  140. g_MySQL_LazyInit = false;
  141. }
  142. for(new i=0; i < sizeof(g_MySQL_InlineData); ++i)
  143. if(_:g_MySQL_InlineData[i][E_CALLBACK_DATA_POINTER] == 0)
  144. if (Callback_Get(CB, g_MySQL_InlineData[i]))
  145. return i;
  146. return -1;
  147. }
  148. #define mysql_pquery_inline(%0,%1,%2,"%3"%4) \
  149. mysql_pquery(%0,%1,"FJ37DH3JG_MYSQL_INTERNAL","d"#%3,MySQL_Internal_SaveInline(%2)%4)
  150. #define mysql_tquery_inline(%0,%1,%2,"%3"%4) \
  151. mysql_tquery(%0,%1,"FJ37DH3JG_MYSQL_INTERNAL","d"#%3,MySQL_Internal_SaveInline(%2)%4)
  152. #define orm_select_inline(%0,%1,"%2"%3) \
  153. orm_select(%0,"FJ37DH3JG_MYSQL_INTERNAL","d"#%2,MySQL_Internal_SaveInline(%1)%3)
  154. #define orm_insert_inline(%0,%1,"%2"%3) \
  155. orm_insert(%0,"FJ37DH3JG_MYSQL_INTERNAL","d"#%2,MySQL_Internal_SaveInline(%1)%3)
  156. forward FJ37DH3JG_MYSQL_INTERNAL(...);
  157. public FJ37DH3JG_MYSQL_INTERNAL(...)
  158. {
  159. new InlineDataIndex = getarg(0);
  160. if(InlineDataIndex < 0)
  161. return 0;
  162. for(new i=0; i < numargs()-1; ++i)
  163. {
  164. for(new l=0; l < YSI_MAX_STRING; ++l)
  165. {
  166. new TmpVal = getarg(i+1, l);
  167. if(l == 0 || g_MySQL_VarArray[i][l-1] < 256)
  168. g_MySQL_VarArray[i][l] = TmpVal;
  169. else
  170. break;
  171. }
  172. g_MySQL_AddressArray[i] = AMX_GetRelativeAddress(g_MySQL_VarArray[i][0]);
  173. }
  174. Callback_Array(g_MySQL_InlineData[InlineDataIndex], g_MySQL_AddressArray);
  175. Callback_Release(g_MySQL_InlineData[InlineDataIndex]);
  176. for(new E_CALLBACK_DATA:e = E_CALLBACK_DATA:0; e < E_CALLBACK_DATA; ++e)
  177. g_MySQL_InlineData[InlineDataIndex][e] = 0;
  178. for(new i=0; i < numargs()-1; ++i)
  179. {
  180. g_MySQL_AddressArray[i] = 0;
  181. for(new l=0, lmax=strlen(g_MySQL_VarArray[i]); l < lmax; ++l)
  182. g_MySQL_VarArray[i][l] = 0;
  183. }
  184. return 1;
  185. }
  186. #endif