Advertisement
Guest User

Untitled

a guest
Oct 22nd, 2014
144
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 21.17 KB | None | 0 0
  1. /**
  2.  * Copyright (c) 2013, Dan
  3.  * All rights reserved.
  4.  *
  5.  * Redistribution and use in source and binary forms, with or without
  6.  * modification, are permitted provided that the following conditions are met:
  7.  *
  8.  * 1. Redistributions of source code must retain the above copyright notice, this
  9.  *    list of conditions and the following disclaimer.
  10.  * 2. Redistributions in binary form must reproduce the above copyright notice,
  11.  *    this list of conditions and the following disclaimer in the documentation
  12.  *    and/or other materials provided with the distribution.
  13.  *
  14.  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
  15.  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  16.  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  17.  * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
  18.  * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  19.  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  20.  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  21.  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  22.  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  23.  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  24.  */
  25.  
  26. /**
  27.  * <version>2.6</version>
  28.  * <remarks>
  29.  * Data types:
  30.  *      SQL - a SQL database handle
  31.  *      Result  - the result of a query, whether it returns or not rows
  32.  * </remarks>
  33.  */
  34.  
  35. /**
  36.  * <summary>MySQL client error codes.</summary>
  37.  */
  38. #define MYSQL_CR_UNKNOWN_ERROR                              2000
  39. #define MYSQL_CR_SOCKET_CREATE_ERROR                        2001
  40. #define MYSQL_CR_CONNECTION_ERROR                           2002
  41. #define MYSQL_CR_CONN_HOST_ERROR                            2003
  42. #define MYSQL_CR_IPSOCK_ERROR                               2004
  43. #define MYSQL_CR_UNKNOWN_HOST                               2005
  44. #define MYSQL_CR_SERVER_GONE_ERROR                          2006
  45. #define MYSQL_CR_VERSION_ERROR                              2007
  46. #define MYSQL_CR_OUT_OF_MEMORY                              2008
  47. #define MYSQL_CR_WRONG_HOST_INFO                            2009
  48. #define MYSQL_CR_LOCALHOST_CONNECTION                       2010
  49. #define MYSQL_CR_TCP_CONNECTION                             2011
  50. #define MYSQL_CR_SERVER_HANDSHAKE_ERR                       2012
  51. #define MYSQL_CR_SERVER_LOST                                2013
  52. #define MYSQL_CR_COMMANDS_OUT_OF_SYNC                       2014
  53. #define MYSQL_CR_NAMEDPIPE_CONNECTION                       2015
  54. #define MYSQL_CR_NAMEDPIPEWAIT_ERROR                        2016
  55. #define MYSQL_CR_NAMEDPIPEOPEN_ERROR                        2017
  56. #define MYSQL_CR_NAMEDPIPESETSTATE_ERROR                    2018
  57. #define MYSQL_CR_CANT_READ_CHARSET                          2019
  58. #define MYSQL_CR_NET_PACKET_TOO_LARGE                       2020
  59. #define MYSQL_CR_EMBEDDED_CONNECTION                        2021
  60. #define MYSQL_CR_PROBE_SLAVE_STATUS                         2022
  61. #define MYSQL_CR_PROBE_SLAVE_HOSTS                          2023
  62. #define MYSQL_CR_PROBE_SLAVE_CONNECT                        2024
  63. #define MYSQL_CR_PROBE_MASTER_CONNECT                       2025
  64. #define MYSQL_CR_SSL_CONNECTION_ERROR                       2026
  65. #define MYSQL_CR_MALFORMED_PACKET                           2027
  66. #define MYSQL_CR_WRONG_LICENSE                              2028
  67. #define MYSQL_CR_NULL_POINTER                               2029
  68. #define MYSQL_CR_NO_PREPARE_STMT                            2030
  69. #define MYSQL_CR_PARAMS_NOT_BOUND                           2031
  70. #define MYSQL_CR_DATA_TRUNCATED                             2032
  71. #define MYSQL_CR_NO_PARAMETERS_EXISTS                       2033
  72. #define MYSQL_CR_INVALID_PARAMETER_NO                       2034
  73. #define MYSQL_CR_INVALID_BUFFER_USE                         2035
  74. #define MYSQL_CR_UNSUPPORTED_PARAM_TYPE                     2036
  75. #define MYSQL_CR_SHARED_MEMORY_CONNECTION                   2037
  76. #define MYSQL_CR_SHARED_MEMORY_CONNECT_REQUEST_ERROR        2038
  77. #define MYSQL_CR_SHARED_MEMORY_CONNECT_ANSWER_ERROR         2039
  78. #define MYSQL_CR_SHARED_MEMORY_CONNECT_FILE_MAP_ERROR       2040
  79. #define MYSQL_CR_SHARED_MEMORY_CONNECT_MAP_ERROR            2041
  80. #define MYSQL_CR_SHARED_MEMORY_FILE_MAP_ERROR               2042
  81. #define MYSQL_CR_SHARED_MEMORY_MAP_ERROR                    2043
  82. #define MYSQL_CR_SHARED_MEMORY_EVENT_ERROR                  2044
  83. #define MYSQL_CR_SHARED_MEMORY_CONNECT_ABANDONED_ERROR      2045
  84. #define MYSQL_CR_SHARED_MEMORY_CONNECT_SET_ERROR            2046
  85. #define MYSQL_CR_CONN_UNKNOW_PROTOCOL                       2047
  86. #define MYSQL_CR_INVALID_CONN_HANDLE                        2048
  87. #define MYSQL_CR_SECURE_AUTH                                2049
  88. #define MYSQL_CR_FETCH_CANCELED                             2050
  89. #define MYSQL_CR_NO_DATA                                    2051
  90. #define MYSQL_CR_NO_STMT_METADATA                           2052
  91.  
  92. /**
  93.  * <summary>MySQL</summary>
  94.  */
  95. #define SQL_HANDLER_MYSQL               1
  96.  
  97. /**
  98.  * <summary>Postgre SQL</summary>
  99.  */
  100. #define SQL_HANDLER_POSTGRESQL          2
  101.  
  102. /**
  103.  * <summary>The query will be executed in server's thread and the result is fetched on demand.</summary>
  104.  */
  105. #define QUERY_NONE                      0
  106.  
  107. /**
  108.  * <summary>Runs the query in a different thread (other than main server's thread).</summary>
  109.  */
  110. #define QUERY_THREADED                  1
  111.  
  112. /**
  113.  * <summary>Stores (temporarily) rows returned by a query in cache.</summary>
  114.  */
  115. #define QUERY_CACHED                    2
  116.  
  117. /**
  118.  * <summary>Log levels. (@see sql_debug)</summary>
  119.  */
  120. #define LOG_ALL                         0
  121. #define LOG_DEBUG                       1
  122. #define LOG_INFO                        2
  123. #define LOG_WARNING                     3
  124. #define LOG_ERROR                       4
  125. #define LOG_NONE                        5
  126.  
  127. /**
  128.  * <summary>Checks if a string is null.</summary>
  129.  */
  130. #if !defined isnull
  131.     #define isnull(%1) \
  132.         ((!(%1[0])) || (((%1[0]) == '\1') && (!(%1[1]))))
  133. #endif
  134.  
  135. /**
  136.  * <summary>Checks if a cell from a SQL table is null.</summary>
  137.  */
  138. #if !defined issqlnull
  139.     #define issqlnull(%1) \
  140.         ((isnull(%1)) || (strcmp(%1, "NULL", false) == 0))
  141. #endif
  142.    
  143. /**
  144.  * <summary>Aliases of natives defined below.</summary>
  145.  */
  146. #define sql_open                        sql_connect
  147. #define sql_close                       sql_disconnect
  148. #define mysql_connect(%0)               sql_connect(SQL_HANDLER_MYSQL,%0)
  149. #define pgsql_connect(%0)               sql_connect(SQL_HANDLER_POSTGRESQL,%0)
  150.  
  151. /**
  152.  * <summary>Called when a SQL error occurs during the execution of a query.</summary>
  153.  * <param name="handle">The SQL handle used for execution of the query.</param>
  154.  * <param name="errorid">The ID of the error (@see "C.4. Client Error Codes and Messages" from "SQL Reference Manual").</param>
  155.  * <param name="error">The message of the error.</param>
  156.  * <param name="query">Query which caused the error.</param>
  157.  * <param name="callback">Callback which should have been called normally.</param>
  158.  * <returns>No return value is expected.</returns>
  159.  */
  160. forward OnSQLError(SQL:handle, errorid, error[], query[], callback[]);
  161.  
  162. /**
  163.  * <summary>Sets the minimum level of logged messages.</summary>
  164.  * <param name="file">The minimum level of the file (sql_log.txt) logs.</param>
  165.  * <param name="console">The minimum level of the console logs.</param>
  166.  * <returns>No return value.</returns>
  167.  */
  168. native sql_debug(file = LOG_ALL, console = LOG_WARNING);
  169.  
  170. /**
  171.  * <summary>Creates a new handle and connects to a SQL server.</summary>
  172.  * <param name="host">The SQL hostname.</param>
  173.  * <param name="user">The SQL username.</param>
  174.  * <param name="pass">The SQL password assigned to the user used.</param>
  175.  * <param name="db">The name of the targeted database.</param>
  176.  * <param name="port">The port on which the SQL server listens.</param>
  177.  * <returns>The ID of the handle.</returns>
  178.  */
  179. native SQL:sql_connect(sql_type, host[], user[], pass[], db[], port = 0);
  180.  
  181. /**
  182.  * <summary>Destroys the handle and disconnects from the SQL server.</summary>
  183.  * <param name="handle">The SQL handle which has to be disconnected.</param>
  184.  * <returns>No return value.</returns>
  185.  */
  186. native sql_disconnect(SQL:handle);
  187.  
  188. /**
  189.  * <summary>Waits for a handle to finish its activity (all queries to be executed).</summary>
  190.  * <param name="handle">The SQL handle.</param>
  191.  * <returns>No return value.</returns>
  192.  */
  193. native sql_wait(SQL:handle);
  194.  
  195. /**
  196.  * <summary>Sets default character set.</summary>
  197.  * <param name="handle">The SQL handle.</param>
  198.  * <param name="charset">New default character set.</param>
  199.  * <returns>True if succesful.</returns>
  200.  */
  201. native sql_set_charset(SQL:handle, charset[]);
  202.  
  203. /**
  204.  * <summary>Gets default character set.</summary>
  205.  * <param name="dest">The destination where the character set will be stored.</param>
  206.  * <param name="dest_len">The capacity of the destination.</param>
  207.  * <returns>True if succesful.</returns>
  208.  */
  209. native sql_get_charset(SQL:handle, dest[], dest_len = sizeof(dest));
  210.  
  211. /**
  212.  * <summary>Pings the SQL server.</summary>
  213.  * <param name="handle">The SQL handle which has to be disconnected.</param>
  214.  * <returns>Returns a SQL client error (if any) or 0 if the connection is alive.</returns>
  215.  */
  216. native sql_ping(SQL:handle);
  217.  
  218. /**
  219.  * <summary>Gets statistics from SQL server.</summary>
  220.  * <param name="dest">The destination where the information will be stored.</param>
  221.  * <param name="dest_len">The capacity of the destination.</param>
  222.  * <returns>True if succesful.</returns>
  223.  */
  224. native sql_get_stat(SQL:handle, dest[], dest_len = sizeof(dest));
  225.  
  226. /**
  227.  * <summary>Escapes a string to be used further in queries.</summary>
  228.  * <param name="handle">The SQL handle used for escaping the string.</param>
  229.  * <param name="src">The source of the unescaped string.</param>
  230.  * <param name="dest">The destination where the escaped string will be stored.</param>
  231.  * <param name="dest_len">The capacity of the destination.</param>
  232.  * <returns>The length of the escaped string.</returns>
  233.  */
  234. native sql_escape_string(SQL:handle, src[], dest[], dest_len = sizeof(dest));
  235.  
  236. /**
  237.  * <summary>Executes a SQL query and returns the result.</summary>
  238.  * <param name="handle">The SQL handle used for execution of the query.</param>
  239.  * <param name="query">The query.</param>
  240.  * <param name="flag">Query's flags.</param>
  241.  * <param name="callback">The callback which has to be called after the query was sucesfully executed.</param>
  242.  * <param name="format">The format of the callback.
  243.  *      a, A = arrays (must be followed by an integer: array's szie);
  244.  *      b, B = boolean; c, C = character; d, D, i, I = integer;
  245.  *      r, R = result; s, S = string
  246.  * </param>
  247.  * <returns>The ID of the result.</returns>
  248.  */
  249. native Result:sql_query(SQL:handle, query[], flag = QUERY_NONE, callback[] = "", format[] = "", {Float,_}:...);
  250.  
  251. /**
  252.  * <summary>Stores the result for later use (if query is threaded).</summary>
  253.  * <param name="result">The ID of the result which has to be stored.</param>
  254.  * <returns>No return value.</returns>
  255.  */
  256. native sql_store_result(Result:result);
  257.  
  258. /**
  259.  * <summary>Frees the result for later use (if the query was stored or is not threaded).</summary>
  260.  * <param name="result">The ID of the result which has to be freed.</param>
  261.  * <returns>No return value.</returns>
  262.  */
  263. native sql_free_result(Result:result);
  264.  
  265. /**
  266.  * <summary>Gets the count of affected rows.</summary>
  267.  * <param name="result">The ID of the result.</param>
  268.  * <returns>The count of affected rows.</returns>
  269.  */
  270. native sql_affected_rows(Result:result);
  271.  
  272. /**
  273.  * <summary>Gets the ID of the last insert query.</summary>
  274.  * <param name="result">The ID of the result.</param>
  275.  * <returns>The ID of the latest inserted row.</returns>
  276.  */
  277. native sql_insert_id(Result:result);
  278.  
  279. /**
  280.  * <summary>Gets the ID of the error returned by this result.</summary>
  281.  * <param name="result">The ID of the result.</param>
  282.  * <returns>The ID of the error.</returns>
  283.  */
  284. native sql_error(Result:result);
  285.  
  286. /**
  287.  * <summary>Gets the message of the error returned by this result.</summary>
  288.  * <param name="result">The ID of the result.</param>
  289.  * <param name="dest">The destination where the error message will be stored.</param>
  290.  * <param name="dest_len">The capacity of the destination.</param>
  291.  * <returns>The ID of the error.</returns>
  292.  */
  293. native sql_error_string(Result:result, dest[], dest_len = sizeof(dest));
  294.  
  295. /**
  296.  * <summary>Gets the count of rows contained in a result.</summary>
  297.  * <param name="result">The ID of the result.</param>
  298.  * <returns>The count of rows contained in the result.</returns>
  299.  */
  300. native sql_num_rows(Result:result);
  301.  
  302. /**
  303.  * <summary>Gets the count of fields contained in a result.</summary>
  304.  * <param name="result">The ID of the result.</param>
  305.  * <returns>The count of fields contained in the result.</returns>
  306.  */
  307. native sql_num_fields(Result:result);
  308.  
  309. /**
  310.  * <summary>Jumps to a specific result set.</summary>
  311.  * <param name="result">The ID of the result.</param>
  312.  * <param name="idx">The index of the result set. If -1 is specified, next result set will be retrieved.</param>
  313.  * <returns>`true` if there are any results left, `false` otherwise.</returns>
  314.  */
  315. native sql_next_result(Result:result, idx = -1);
  316.  
  317. /**
  318.  * <summary>Fetches the name of a field.</summary>
  319.  * <param name="result">The ID of the result.</param>
  320.  * <param name="field">The index of the field.</param>
  321.  * <param name="dest">The destination where the field will be stored.</param>
  322.  * <param name="dest_len">The capacity of the destination.</param>
  323.  * <returns>The length of field's name.</returns>
  324.  */
  325. native sql_field_name(Result:result, field, dest[], dest_len = sizeof(dest));
  326.  
  327. /**
  328.  * <summary>Fetches an entire row inserting the separator between each cell.</summary>
  329.  * <param name="result"></param>
  330.  * <param name="separator"></param>
  331.  * <param name="dest">The destination where the field will be stored.</param>
  332.  * <param name="dest_len">The capacity of the destination.</param>
  333.   * <returns>The length of the row.</returns>
  334.  */
  335. native sql_fetch_row(Result:result, sep[], dest[], dest_len = sizeof(dest));
  336.  
  337. /**
  338.  * <summary>Jumps to a specific row.</summary>
  339.  * <param name="result">The ID of the result.</param>
  340.  * <param name="row">The index of the row. If -1 is specified, next row will be retrieved.</param>
  341.  * <returns>`true` if there are any rows left, `false` otherwise.</returns>
  342.  */
  343. native sql_next_row(Result:result, row = -1);
  344.  
  345. // ----------------------------------------------------------------------------
  346.  
  347. /**
  348.  * <summary>Fetches a cell containing a string by field index.</summary>
  349.  * <param name="result">The ID of the result.</param>
  350.  * <param name="field">The index of the field.</param>
  351.  * <param name="dest">The destination where the string will be stored.</param>
  352.  * <param name="dest_len">The capacity of the destination.</param>
  353.  * <returns>The length of the string.</returns>
  354.  */
  355. native sql_get_field(Result:result, field, dest[], dest_len = sizeof(dest));
  356.  
  357. /**
  358.  * <summary>Fetches a cell containing a string by field name.</summary>
  359.  * <param name="result">The ID of the result.</param>
  360.  * <param name="field">The name of the field.</param>
  361.  * <param name="dest">The destination where the string will be stored.</param>
  362.  * <param name="dest_len">The capacity of the destination.</param>
  363.  * <returns>The length of the string.</returns>
  364.  */
  365. native sql_get_field_assoc(Result:result, field[], dest[], dest_len = sizeof(dest));
  366.  
  367. /**
  368.  * <summary>Fetches a cell containing an integer value by field index.</summary>
  369.  * <param name="result">The ID of the result.</param>
  370.  * <param name="field">The index of the field.</param>
  371.  * <returns>The value of the cell.</returns>
  372.  */
  373. native sql_get_field_int(Result:result, field);
  374.  
  375. /**
  376.  * <summary>Fetches a cell containing an integer value by field name.</summary>
  377.  * <param name="result">The ID of the result.</param>
  378.  * <param name="field">The name of the field.</param>
  379.  * <returns>The value of the cell.</returns>
  380.  */
  381. native sql_get_field_assoc_int(Result:result, field[]);
  382.  
  383. /**
  384.  * <summary>Fetches a cell containing a float value by field index.</summary>
  385.  * <param name="result">The ID of the result.</param>
  386.  * <param name="field">The index of the field.</param>
  387.  * <returns>The value of the cell.</returns>
  388.  */
  389. native Float:sql_get_field_float(Result:result, field);
  390.  
  391. /**
  392.  * <summary>Fetches a cell containing a float value by field name.</summary>
  393.  * <param name="result">The ID of the result.</param>
  394.  * <param name="field">The name of the field.</param>
  395.  * <returns>The value of the cell.</returns>
  396.  */
  397. native Float:sql_get_field_assoc_float(Result:result, field[]);
  398.  
  399. // ----------------------------------------------------------------------------
  400.  
  401. /**
  402.  * <summary>Fetches a cell containing a string by field index.</summary>
  403.  * <param name="result">The ID of the result.</param>
  404.  * <param name="row">The index of the row.</param>
  405.  * <param name="field">The index of the field.</param>
  406.  * <param name="dest">The destination where the string will be stored.</param>
  407.  * <param name="dest_len">The capacity of the destination.</param>
  408.  * <returns>The length of the string.</returns>
  409.  */
  410. native sql_get_field_ex(Result:result, row, field, dest[], dest_len = sizeof(dest));
  411.  
  412. /**
  413.  * <summary>Fetches a cell containing a string by field name.</summary>
  414.  * <param name="result">The ID of the result.</param>
  415.  * <param name="row">The index of the row.</param>
  416.  * <param name="field">The name of the field.</param>
  417.  * <param name="dest">The destination where the string will be stored.</param>
  418.  * <param name="dest_len">The capacity of the destination.</param>
  419.  * <returns>The length of the string.</returns>
  420.  */
  421. native sql_get_field_assoc_ex(Result:result, row, field[], dest[], dest_len = sizeof(dest));
  422.  
  423. /**
  424.  * <summary>Fetches a cell containing an integer value by field index.</summary>
  425.  * <param name="result">The ID of the result.</param>
  426.  * <param name="row">The index of the row.</param>
  427.  * <param name="field">The index of the field.</param>
  428.  * <returns>The value of the cell.</returns>
  429.  */
  430. native sql_get_field_int_ex(Result:result, row, field);
  431.  
  432. /**
  433.  * <summary>Fetches a cell containing an integer value by field name.</summary>
  434.  * <param name="result">The ID of the result.</param>
  435.  * <param name="row">The index of the row.</param>
  436.  * <param name="field">The name of the field.</param>
  437.  * <returns>The value of the cell.</returns>
  438.  */
  439. native sql_get_field_assoc_int_ex(Result:result, row, field[]);
  440.  
  441. /**
  442.  * <summary>Fetches a cell containing a float value by field index.</summary>
  443.  * <param name="result">The ID of the result.</param>
  444.  * <param name="row">The index of the row.</param>
  445.  * <param name="field">The index of the field.</param>
  446.  * <returns>The value of the cell.</returns>
  447.  */
  448. native Float:sql_get_field_float_ex(Result:result, row, field);
  449.  
  450. /**
  451.  * <summary>Fetches a cell containing a float value by field name.</summary>
  452.  * <param name="result">The ID of the result.</param>
  453.  * <param name="row">The index of the row.</param>
  454.  * <param name="field">The name of the field.</param>
  455.  * <returns>The value of the cell.</returns>
  456.  */
  457. native Float:sql_get_field_assoc_float_ex(Result:result, row, field[]);
  458.  
  459. // ----------------------------------------------------------------------------
  460.  
  461. /**
  462.  * y_inline interface
  463.  */
  464.  
  465. #if !defined SQL_USE_Y_INLINE
  466.     // NOTE: #emit ignores pre-processor's directives.
  467.     // So, if we disable y_inline support, the #emit instructions below still
  468.     // requirea variable to store the address of the first passed parameter.
  469.     new SQL_addr;
  470.     #pragma unused SQL_addr
  471. #endif
  472.  
  473. #if defined SQL_USE_Y_INLINE
  474.  
  475.     #if !defined SQL_MAX_INLINE_CALLBACKS
  476.    
  477.         /**
  478.          * <summary>The maximum number of inline callbacks scheduled simultaneously.</summary>
  479.          */
  480.         #define SQL_MAX_INLINE_CALLBACKS    512
  481.     #endif
  482.    
  483.     #if !defined SQL_MAX_INLINE_PARAMETERS
  484.        
  485.         /**
  486.          * <summary>The maximum number of parameters an inline function can have.</summary>
  487.          */
  488.         #define SQL_MAX_INLINE_PARAMETERS   16
  489.     #endif
  490.  
  491.     /**
  492.      * <summary>Executes a SQL query and calls an inline callback on return.</summary>
  493.      * <remarks>The "format" parameter must be static.</remarks>
  494.      */
  495.     //native Result:sql_query_inline(SQL:handle, query[], flag, callback:callback, format[] = "", {Float,_}:...);
  496.     #define sql_query_inline(%0,%1,%2,%3,"%4"%5) \
  497.         sql_query(%0,%1,%2,"SQL_OnInlineCallback","i"#%4,SQL_SaveCallback(%3)%5)
  498.  
  499.     /**
  500.      * <summary>Stores information about callbacks that are scheduled to be executed.</summary>
  501.      * <remarks>This data is recycled. Once `SQL_LoadCallback` was called, that index is marked as free.</remarks>
  502.      *
  503.      * TODO: If the query fails, the `SQL_OnInlineCallback` won't  be called
  504.      * and the data won't be recycled. Fix it.
  505.      */
  506.     stock SQL_callbacks[SQL_MAX_INLINE_CALLBACKS][E_CALLBACK_DATA];
  507.  
  508.     /**
  509.      * <summary>Saves the callback data of a scheduled statement.</summary>
  510.      * <param name="callback">Callback's ID.</param>
  511.      * <returns>The index from `SQL_callbacks` where the callback was saved.</returns>
  512.      */
  513.     stock SQL_SaveCallback(callback:callback) {
  514.         for (new i = 0; i != sizeof(SQL_callbacks); ++i) {
  515.             if (SQL_callbacks[i][E_CALLBACK_DATA_POINTER] != 0) {
  516.                 continue;
  517.             }
  518.             if (!Callback_Get(callback, SQL_callbacks[i])) {
  519.                 SQL_callbacks[i][E_CALLBACK_DATA_POINTER] = 0;
  520.                 return -1;
  521.             }
  522.             return i;
  523.         }
  524.         print("[plugin.sql][warning] No more free space in `SQL_callbacks`.");
  525.         return -1;
  526.     }
  527.  
  528.     /**
  529.      * <summary>The Interface between the plugin and y_inline, which loads the callback data and executes it.</summary>
  530.      * <remarks>This function also recycles data.</remarks>
  531.      * <param name="idx">Callback's index from `SQL_callbacks`.</param>
  532.      */
  533.     forward SQL_OnInlineCallback(idx, {Float,_}:...);
  534.     public SQL_OnInlineCallback(idx, {Float,_}:...) {
  535.         if (idx == -1) {
  536.             return;
  537.         }
  538.         new numArgs = numargs(), SQL_addr[SQL_MAX_INLINE_PARAMETERS];
  539.         if (numArgs > 1) {
  540.             #emit CONST.alt         16
  541.             #emit LCTRL             5
  542.             #emit ADD
  543.             #emit STOR.S.pri        SQL_addr
  544.             --numArgs;
  545.             for (new i = 1; i != numArgs; ++i) {
  546.                 SQL_addr[i] = SQL_addr[0] + (i * 4); // BYTES_PER_CELL = 4
  547.             }
  548.         }
  549.         Callback_Array(SQL_callbacks[idx], SQL_addr);
  550.         SQL_callbacks[idx][E_CALLBACK_DATA_POINTER] = 0;
  551.     }
  552.    
  553. #endif
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement