sql.h 8.0 KB

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