root/src/common/sql.h @ 16

Revision 1, 8.4 kB (checked in by jinshiro, 17 years ago)
Line 
1// Copyright (c) Athena Dev Teams - Licensed under GNU GPL
2// For more information, see LICENCE in the main folder
3
4#ifndef _COMMON_SQL_H_
5#define _COMMON_SQL_H_
6
7#ifndef _CBASETYPES_H_
8#include "../common/cbasetypes.h"
9#endif
10#include <stdarg.h>// va_list
11
12
13
14// Return codes
15#define SQL_ERROR -1
16#define SQL_SUCCESS 0
17#define SQL_NO_DATA 100
18
19
20
21/// Data type identifier.
22/// String, enum and blob data types need the buffer length specified.
23enum SqlDataType
24{
25        SQLDT_NULL,
26        // fixed size
27        SQLDT_INT8,
28        SQLDT_INT16,
29        SQLDT_INT32,
30        SQLDT_INT64,
31        SQLDT_UINT8,
32        SQLDT_UINT16,
33        SQLDT_UINT32,
34        SQLDT_UINT64,
35        // platform dependent size
36        SQLDT_CHAR,
37        SQLDT_SHORT,
38        SQLDT_INT,
39        SQLDT_LONG,
40        SQLDT_LONGLONG,
41        SQLDT_UCHAR,
42        SQLDT_USHORT,
43        SQLDT_UINT,
44        SQLDT_ULONG,
45        SQLDT_ULONGLONG,
46        // floating point
47        SQLDT_FLOAT,
48        SQLDT_DOUBLE,
49        // other
50        SQLDT_STRING,
51        SQLDT_ENUM,
52        // Note: An ENUM is a string with restricted values. When an invalid value
53        //       is inserted, it is saved as an empty string (numerical value 0).
54        SQLDT_BLOB,
55        SQLDT_LASTID
56};
57
58struct Sql;// Sql handle (private access)
59struct SqlStmt;// Sql statement (private access)
60
61typedef enum SqlDataType SqlDataType;
62typedef struct Sql Sql;
63typedef struct SqlStmt SqlStmt;
64
65
66/// Allocates and initializes a new Sql handle.
67struct Sql* Sql_Malloc(void);
68
69
70
71/// Establishes a connection.
72///
73/// @return SQL_SUCCESS or SQL_ERROR
74int Sql_Connect(Sql* self, const char* user, const char* passwd, const char* host, uint16 port, const char* db);
75
76
77
78
79/// Retrieves the timeout of the connection.
80///
81/// @return SQL_SUCCESS or SQL_ERROR
82int Sql_GetTimeout(Sql* self, uint32* out_timeout);
83
84
85
86
87/// Retrieves the name of the columns of a table into out_buf, with the separator after each name.
88///
89/// @return SQL_SUCCESS or SQL_ERROR
90int Sql_GetColumnNames(Sql* self, const char* table, char* out_buf, size_t buf_len, char sep);
91
92
93
94
95/// Changes the encoding of the connection.
96///
97/// @return SQL_SUCCESS or SQL_ERROR
98int Sql_SetEncoding(Sql* self, const char* encoding);
99
100
101
102/// Pings the connection.
103///
104/// @return SQL_SUCCESS or SQL_ERROR
105int Sql_Ping(Sql* self);
106
107
108
109/// Escapes a string.
110/// The output buffer must be at least strlen(from)*2+1 in size.
111///
112/// @return The size of the escaped string
113size_t Sql_EscapeString(Sql* self, char* out_to, const char* from);
114
115
116
117/// Escapes a string.
118/// The output buffer must be at least from_len*2+1 in size.
119///
120/// @return The size of the escaped string
121size_t Sql_EscapeStringLen(Sql* self, char* out_to, const char* from, size_t from_len);
122
123
124
125/// Executes a query.
126/// Any previous result is freed.
127/// The query is constructed as if it was sprintf.
128///
129/// @return SQL_SUCCESS or SQL_ERROR
130int Sql_Query(Sql* self, const char* query, ...);
131
132
133
134/// Executes a query.
135/// Any previous result is freed.
136/// The query is constructed as if it was svprintf.
137///
138/// @return SQL_SUCCESS or SQL_ERROR
139int Sql_QueryV(Sql* self, const char* query, va_list args);
140
141
142
143/// Executes a query.
144/// Any previous result is freed.
145/// The query is used directly.
146///
147/// @return SQL_SUCCESS or SQL_ERROR
148int Sql_QueryStr(Sql* self, const char* query);
149
150
151
152/// Returns the number of the AUTO_INCREMENT column of the last INSERT/UPDATE query.
153///
154/// @return Value of the auto-increment column
155uint64 Sql_LastInsertId(Sql* self);
156
157
158
159/// Returns the number of columns in each row of the result.
160///
161/// @return Number of columns
162uint32 Sql_NumColumns(Sql* self);
163
164
165
166/// Returns the number of rows in the result.
167///
168/// @return Number of rows
169uint64 Sql_NumRows(Sql* self);
170
171
172
173/// Fetches the next row.
174/// The data of the previous row is no longer valid.
175///
176/// @return SQL_SUCCESS, SQL_ERROR or SQL_NO_DATA
177int Sql_NextRow(Sql* self);
178
179
180
181/// Gets the data of a column.
182/// The data remains valid until the next row is fetched or the result is freed.
183///
184/// @return SQL_SUCCESS or SQL_ERROR
185int Sql_GetData(Sql* self, size_t col, char** out_buf, size_t* out_len);
186
187
188
189/// Frees the result of the query.
190void Sql_FreeResult(Sql* self);
191
192
193
194#if defined(SQL_REMOVE_SHOWDEBUG)
195#define Sql_ShowDebug(self) (void)0
196#else
197#define Sql_ShowDebug(self) Sql_ShowDebug_(self, __FILE__, __LINE__)
198#endif
199/// Shows debug information (last query).
200void Sql_ShowDebug_(Sql* self, const char* debug_file, const unsigned long debug_line);
201
202
203
204/// Frees a Sql handle returned by Sql_Malloc.
205void Sql_Free(Sql* self);
206
207
208
209///////////////////////////////////////////////////////////////////////////////
210// Prepared Statements
211///////////////////////////////////////////////////////////////////////////////
212// Parameters are placed in the statement by embedding question mark ('?')
213// characters into the query at the appropriate positions.
214// The markers are legal only in places where they represent data.
215// The markers cannot be inside quotes. Quotes will be added automatically
216// when they are required.
217//
218// example queries with parameters:
219// 1) SELECT col FROM table WHERE id=?
220// 2) INSERT INTO table(col1,col2) VALUES(?,?)
221
222
223
224/// Allocates and initializes a new SqlStmt handle.
225/// It uses the connection of the parent Sql handle.
226/// Queries in Sql and SqlStmt are independent and don't affect each other.
227///
228/// @return SqlStmt handle or NULL if an error occured
229struct SqlStmt* SqlStmt_Malloc(Sql* sql);
230
231
232
233/// Prepares the statement.
234/// Any previous result is freed and all parameter bindings are removed.
235/// The query is constructed as if it was sprintf.
236///
237/// @return SQL_SUCCESS or SQL_ERROR
238int SqlStmt_Prepare(SqlStmt* self, const char* query, ...);
239
240
241
242/// Prepares the statement.
243/// Any previous result is freed and all parameter bindings are removed.
244/// The query is constructed as if it was svprintf.
245///
246/// @return SQL_SUCCESS or SQL_ERROR
247int SqlStmt_PrepareV(SqlStmt* self, const char* query, va_list args);
248
249
250
251/// Prepares the statement.
252/// Any previous result is freed and all parameter bindings are removed.
253/// The query is used directly.
254///
255/// @return SQL_SUCCESS or SQL_ERROR
256int SqlStmt_PrepareStr(SqlStmt* self, const char* query);
257
258
259
260/// Returns the number of parameters in the prepared statement.
261///
262/// @return Number or paramenters
263size_t SqlStmt_NumParams(SqlStmt* self);
264
265
266
267/// Binds a parameter to a buffer.
268/// The buffer data will be used when the statement is executed.
269/// All parameters should have bindings.
270///
271/// @return SQL_SUCCESS or SQL_ERROR
272int SqlStmt_BindParam(SqlStmt* self, size_t idx, SqlDataType buffer_type, void* buffer, size_t buffer_len);
273
274
275
276/// Executes the prepared statement.
277/// Any previous result is freed and all column bindings are removed.
278///
279/// @return SQL_SUCCESS or SQL_ERROR
280int SqlStmt_Execute(SqlStmt* self);
281
282
283
284/// Returns the number of the AUTO_INCREMENT column of the last INSERT/UPDATE statement.
285///
286/// @return Value of the auto-increment column
287uint64 SqlStmt_LastInsertId(SqlStmt* self);
288
289
290
291/// Returns the number of columns in each row of the result.
292///
293/// @return Number of columns
294size_t SqlStmt_NumColumns(SqlStmt* self);
295
296
297
298/// Binds the result of a column to a buffer.
299/// The buffer will be filled with data when the next row is fetched.
300/// For string/enum buffer types there has to be enough space for the data
301/// and the nul-terminator (an extra byte).
302///
303/// @return SQL_SUCCESS or SQL_ERROR
304int SqlStmt_BindColumn(SqlStmt* self, size_t idx, SqlDataType buffer_type, void* buffer, size_t buffer_len, uint32* out_length, int8* out_is_null);
305
306
307
308/// Returns the number of rows in the result.
309///
310/// @return Number of rows
311uint64 SqlStmt_NumRows(SqlStmt* self);
312
313
314
315/// Fetches the next row.
316/// All column bindings will be filled with data.
317///
318/// @return SQL_SUCCESS, SQL_ERROR or SQL_NO_DATA
319int SqlStmt_NextRow(SqlStmt* self);
320
321
322
323/// Frees the result of the statement execution.
324void SqlStmt_FreeResult(SqlStmt* self);
325
326
327
328#if defined(SQL_REMOVE_SHOWDEBUG)
329#define SqlStmt_ShowDebug(self) (void)0
330#else
331#define SqlStmt_ShowDebug(self) SqlStmt_ShowDebug_(self, __FILE__, __LINE__)
332#endif
333/// Shows debug information (with statement).
334void SqlStmt_ShowDebug_(SqlStmt* self, const char* debug_file, const unsigned long debug_line);
335
336
337
338/// Frees a SqlStmt returned by SqlStmt_Malloc.
339void SqlStmt_Free(SqlStmt* self);
340
341
342
343#endif /* _COMMON_SQL_H_ */
Note: See TracBrowser for help on using the browser.