a_mysql2.inc 7.5 KB

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