sql.h 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345
  1. // Copyright (c) Athena Dev Teams - Licensed under GNU GPL
  2. // For more information, see LICENCE in the main folder
  3. #ifndef _COMMON_SQL_H_
  4. #define _COMMON_SQL_H_
  5. #include "../common/cbasetypes.h"
  6. #include <stdarg.h>// va_list
  7. // Return codes
  8. #define SQL_ERROR -1
  9. #define SQL_SUCCESS 0
  10. #define SQL_NO_DATA 100
  11. // macro definition to determine whether the mySQL engine is running on InnoDB (rather than MyISAM)
  12. // uncomment this line if the your mySQL tables have been changed to run on InnoDB
  13. // this macro will adjust how logs are recorded in the database to accommodate the change
  14. //#define SQL_INNODB
  15. /// Data type identifier.
  16. /// String, enum and blob data types need the buffer length specified.
  17. enum SqlDataType
  18. {
  19. SQLDT_NULL,
  20. // fixed size
  21. SQLDT_INT8,
  22. SQLDT_INT16,
  23. SQLDT_INT32,
  24. SQLDT_INT64,
  25. SQLDT_UINT8,
  26. SQLDT_UINT16,
  27. SQLDT_UINT32,
  28. SQLDT_UINT64,
  29. // platform dependent size
  30. SQLDT_CHAR,
  31. SQLDT_SHORT,
  32. SQLDT_INT,
  33. SQLDT_LONG,
  34. SQLDT_LONGLONG,
  35. SQLDT_UCHAR,
  36. SQLDT_USHORT,
  37. SQLDT_UINT,
  38. SQLDT_ULONG,
  39. SQLDT_ULONGLONG,
  40. // floating point
  41. SQLDT_FLOAT,
  42. SQLDT_DOUBLE,
  43. // other
  44. SQLDT_STRING,
  45. SQLDT_ENUM,
  46. // Note: An ENUM is a string with restricted values. When an invalid value
  47. // is inserted, it is saved as an empty string (numerical value 0).
  48. SQLDT_BLOB,
  49. SQLDT_LASTID
  50. };
  51. struct Sql;// Sql handle (private access)
  52. struct SqlStmt;// Sql statement (private access)
  53. typedef enum SqlDataType SqlDataType;
  54. typedef struct Sql Sql;
  55. typedef struct SqlStmt SqlStmt;
  56. /// Allocates and initializes a new Sql handle.
  57. struct Sql* Sql_Malloc(void);
  58. /// Establishes a connection.
  59. ///
  60. /// @return SQL_SUCCESS or SQL_ERROR
  61. int Sql_Connect(Sql* self, const char* user, const char* passwd, const char* host, uint16 port, const char* db);
  62. /// Retrieves the timeout of the connection.
  63. ///
  64. /// @return SQL_SUCCESS or SQL_ERROR
  65. int Sql_GetTimeout(Sql* self, uint32* out_timeout);
  66. /// Retrieves the name of the columns of a table into out_buf, with the separator after each name.
  67. ///
  68. /// @return SQL_SUCCESS or SQL_ERROR
  69. int Sql_GetColumnNames(Sql* self, const char* table, char* out_buf, size_t buf_len, char sep);
  70. /// Changes the encoding of the connection.
  71. ///
  72. /// @return SQL_SUCCESS or SQL_ERROR
  73. int Sql_SetEncoding(Sql* self, const char* encoding);
  74. /// Pings the connection.
  75. ///
  76. /// @return SQL_SUCCESS or SQL_ERROR
  77. int Sql_Ping(Sql* self);
  78. /// Escapes a string.
  79. /// The output buffer must be at least strlen(from)*2+1 in size.
  80. ///
  81. /// @return The size of the escaped string
  82. size_t Sql_EscapeString(Sql* self, char* out_to, const char* from);
  83. /// Escapes a string.
  84. /// The output buffer must be at least from_len*2+1 in size.
  85. ///
  86. /// @return The size of the escaped string
  87. size_t Sql_EscapeStringLen(Sql* self, char* out_to, const char* from, size_t from_len);
  88. /// Executes a query.
  89. /// Any previous result is freed.
  90. /// The query is constructed as if it was sprintf.
  91. ///
  92. /// @return SQL_SUCCESS or SQL_ERROR
  93. int Sql_Query(Sql* self, const char* query, ...);
  94. /// Executes a query.
  95. /// Any previous result is freed.
  96. /// The query is constructed as if it was svprintf.
  97. ///
  98. /// @return SQL_SUCCESS or SQL_ERROR
  99. int Sql_QueryV(Sql* self, const char* query, va_list args);
  100. /// Executes a query.
  101. /// Any previous result is freed.
  102. /// The query is used directly.
  103. ///
  104. /// @return SQL_SUCCESS or SQL_ERROR
  105. int Sql_QueryStr(Sql* self, const char* query);
  106. /// Returns the number of the AUTO_INCREMENT column of the last INSERT/UPDATE query.
  107. ///
  108. /// @return Value of the auto-increment column
  109. uint64 Sql_LastInsertId(Sql* self);
  110. /// Returns the number of columns in each row of the result.
  111. ///
  112. /// @return Number of columns
  113. uint32 Sql_NumColumns(Sql* self);
  114. /// Returns the number of rows in the result.
  115. ///
  116. /// @return Number of rows
  117. uint64 Sql_NumRows(Sql* self);
  118. /// Fetches the next row.
  119. /// The data of the previous row is no longer valid.
  120. ///
  121. /// @return SQL_SUCCESS, SQL_ERROR or SQL_NO_DATA
  122. int Sql_NextRow(Sql* self);
  123. /// Gets the data of a column.
  124. /// The data remains valid until the next row is fetched or the result is freed.
  125. ///
  126. /// @return SQL_SUCCESS or SQL_ERROR
  127. int Sql_GetData(Sql* self, size_t col, char** out_buf, size_t* out_len);
  128. /// Frees the result of the query.
  129. void Sql_FreeResult(Sql* self);
  130. #if defined(SQL_REMOVE_SHOWDEBUG)
  131. #define Sql_ShowDebug(self) (void)0
  132. #else
  133. #define Sql_ShowDebug(self) Sql_ShowDebug_(self, __FILE__, __LINE__)
  134. #endif
  135. /// Shows debug information (last query).
  136. void Sql_ShowDebug_(Sql* self, const char* debug_file, const unsigned long debug_line);
  137. /// Frees a Sql handle returned by Sql_Malloc.
  138. void Sql_Free(Sql* self);
  139. ///////////////////////////////////////////////////////////////////////////////
  140. // Prepared Statements
  141. ///////////////////////////////////////////////////////////////////////////////
  142. // Parameters are placed in the statement by embedding question mark ('?')
  143. // characters into the query at the appropriate positions.
  144. // The markers are legal only in places where they represent data.
  145. // The markers cannot be inside quotes. Quotes will be added automatically
  146. // when they are required.
  147. //
  148. // example queries with parameters:
  149. // 1) SELECT col FROM table WHERE id=?
  150. // 2) INSERT INTO table(col1,col2) VALUES(?,?)
  151. /// Allocates and initializes a new SqlStmt handle.
  152. /// It uses the connection of the parent Sql handle.
  153. /// Queries in Sql and SqlStmt are independent and don't affect each other.
  154. ///
  155. /// @return SqlStmt handle or NULL if an error occured
  156. struct SqlStmt* SqlStmt_Malloc(Sql* sql);
  157. /// Prepares the statement.
  158. /// Any previous result is freed and all parameter bindings are removed.
  159. /// The query is constructed as if it was sprintf.
  160. ///
  161. /// @return SQL_SUCCESS or SQL_ERROR
  162. int SqlStmt_Prepare(SqlStmt* self, const char* query, ...);
  163. /// Prepares the statement.
  164. /// Any previous result is freed and all parameter bindings are removed.
  165. /// The query is constructed as if it was svprintf.
  166. ///
  167. /// @return SQL_SUCCESS or SQL_ERROR
  168. int SqlStmt_PrepareV(SqlStmt* self, const char* query, va_list args);
  169. /// Prepares the statement.
  170. /// Any previous result is freed and all parameter bindings are removed.
  171. /// The query is used directly.
  172. ///
  173. /// @return SQL_SUCCESS or SQL_ERROR
  174. int SqlStmt_PrepareStr(SqlStmt* self, const char* query);
  175. /// Returns the number of parameters in the prepared statement.
  176. ///
  177. /// @return Number or paramenters
  178. size_t SqlStmt_NumParams(SqlStmt* self);
  179. /// Binds a parameter to a buffer.
  180. /// The buffer data will be used when the statement is executed.
  181. /// All parameters should have bindings.
  182. ///
  183. /// @return SQL_SUCCESS or SQL_ERROR
  184. int SqlStmt_BindParam(SqlStmt* self, size_t idx, SqlDataType buffer_type, void* buffer, size_t buffer_len);
  185. /// Executes the prepared statement.
  186. /// Any previous result is freed and all column bindings are removed.
  187. ///
  188. /// @return SQL_SUCCESS or SQL_ERROR
  189. int SqlStmt_Execute(SqlStmt* self);
  190. /// Returns the number of the AUTO_INCREMENT column of the last INSERT/UPDATE statement.
  191. ///
  192. /// @return Value of the auto-increment column
  193. uint64 SqlStmt_LastInsertId(SqlStmt* self);
  194. /// Returns the number of columns in each row of the result.
  195. ///
  196. /// @return Number of columns
  197. size_t SqlStmt_NumColumns(SqlStmt* self);
  198. /// Binds the result of a column to a buffer.
  199. /// The buffer will be filled with data when the next row is fetched.
  200. /// For string/enum buffer types there has to be enough space for the data
  201. /// and the nul-terminator (an extra byte).
  202. ///
  203. /// @return SQL_SUCCESS or SQL_ERROR
  204. int SqlStmt_BindColumn(SqlStmt* self, size_t idx, SqlDataType buffer_type, void* buffer, size_t buffer_len, uint32* out_length, int8* out_is_null);
  205. /// Returns the number of rows in the result.
  206. ///
  207. /// @return Number of rows
  208. uint64 SqlStmt_NumRows(SqlStmt* self);
  209. /// Fetches the next row.
  210. /// All column bindings will be filled with data.
  211. ///
  212. /// @return SQL_SUCCESS, SQL_ERROR or SQL_NO_DATA
  213. int SqlStmt_NextRow(SqlStmt* self);
  214. /// Frees the result of the statement execution.
  215. void SqlStmt_FreeResult(SqlStmt* self);
  216. #if defined(SQL_REMOVE_SHOWDEBUG)
  217. #define SqlStmt_ShowDebug(self) (void)0
  218. #else
  219. #define SqlStmt_ShowDebug(self) SqlStmt_ShowDebug_(self, __FILE__, __LINE__)
  220. #endif
  221. /// Shows debug information (with statement).
  222. void SqlStmt_ShowDebug_(SqlStmt* self, const char* debug_file, const unsigned long debug_line);
  223. /// Frees a SqlStmt returned by SqlStmt_Malloc.
  224. void SqlStmt_Free(SqlStmt* self);
  225. void Sql_Init(void);
  226. #endif /* _COMMON_SQL_H_ */