[Half-Life AMXX] / include / sqlx.inc Repository:
ViewVC logotype

Annotation of /include/sqlx.inc

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1 - (view) (download)

1 : ian 1 /**
2 :     * SQLX - Newer version of SQL stuff
3 :     */
4 :    
5 :     #if defined _sqlx_included
6 :     #endinput
7 :     #endif
8 :     #define _sqlx_included
9 :    
10 :     //eh..
11 :     #define SQL_NumRows SQL_NumResults
12 :    
13 :     #if AMXX_VERSION_NUM >= 175
14 :     #pragma reqclass sqlx
15 :     #if !defined AMXMODX_NOAUTOLOAD
16 :     #pragma defclasslib sqlx mysql
17 :     #endif //!defined AMXMODX_NOAUTOLOAD
18 :     #endif //AMXX_VERSION_NUM
19 :    
20 :     enum Handle
21 :     {
22 :     Empty_Handle
23 :     };
24 :    
25 :     /**
26 :     * Creates a connection information tuple.
27 :     * This tuple must be passed into connection routines.
28 :     * Freeing the tuple is not necessary, but is a good idea if you
29 :     * create many of them. You can cache these handles globally.
30 :     * !!NOTE!! I have seen most people think that this connects to the DB.
31 :     * Nowhere does it say this, and in fact it does not. It only caches
32 :     * the connection information, the host/user/pass/etc.
33 :     *
34 :     * The optional timeout parameter specifies how long connections should wait before
35 :     * giving up. If 0, the default (which is undefined) is used.
36 :     *
37 :     */
38 :     native Handle:SQL_MakeDbTuple(const host[], const user[], const pass[], const db[], timeout=0);
39 :    
40 :    
41 :     /**
42 :     * Frees an SQL handle.
43 :     * The handle can be to anything (tuple, connection, query, results, etc).
44 :     * If you free a database connection, it closes the connection as well.
45 :     */
46 :     native SQL_FreeHandle(Handle:h);
47 :    
48 :    
49 :     /**
50 :     * Opens a database connection.
51 :     * Returns an SQL handle, which must be freed.
52 :     * Returns Empty_Handle on failure.
53 :     */
54 :     native Handle:SQL_Connect(Handle:cn_tuple, &errcode, error[], maxlength);
55 :    
56 :    
57 :     /**
58 :     * Prepares a query.
59 :     * The query must always be freed.
60 :     * This does not actually do the query!
61 :     */
62 :     native Handle:SQL_PrepareQuery(Handle:db, const fmt[], {Float,_}:...);
63 :    
64 :     #define TQUERY_CONNECT_FAILED -2
65 :     #define TQUERY_QUERY_FAILED -1
66 :     #define TQUERY_SUCCESS 0
67 :     /**
68 :     * Prepares and executes a threaded query.
69 :     * This will not interrupt gameplay in the event of a poor/lossed
70 :     * connection, however, the interface is more complicated and
71 :     * asynchronous. Furthermore, a new connection/disconnection is
72 :     * made for each query to simplify driver support.
73 :     * The handler should look like:
74 :     *
75 :     * @param failstate - One of the three TQUERY_ defines.
76 :     * @param query - Handle to the query, do not free it.
77 :     * @param error - An error message, if any.
78 :     * @param errnum - An error code, if any.
79 :     * @param data - Data array you passed in.
80 :     * @param size - Size of the data array you passed in.
81 :     * @param queuetime - Amount of gametime that passed while the query was resolving.
82 :     *
83 :     * public QueryHandler(failstate, Handle:query, error[], errnum, data[], size, Float:queuetime)
84 :     *
85 :     * Note! The handle you pass in is a DB Tuple, NOT an active connection!
86 :     * Note! The handle does not need to be freed.
87 :     * Also note: This function is not guaranteed to be in another thread
88 :     * (in fact - it's not). You're seeing data "after the fact",
89 :     * and as such to execute another query you should run
90 :     * SQL_ThreadQuery again with new data.
91 :     */
92 :     native SQL_ThreadQuery(Handle:db_tuple, const handler[], const query[], const data[]="", dataSize=0);
93 :    
94 :    
95 :     /**
96 :     * Executes a query.
97 :     * Returns 1 if the query succeeded.
98 :     * Returns 0 if the query failed.
99 :     * NOTE: You can call this multiple times as long as its parent
100 :     * connection is kept open. Each time the result set will be freed
101 :     * from the previous call.
102 :     */
103 :     native SQL_Execute(Handle:query);
104 :    
105 :     /**
106 :     * Gets information about a failed query error.
107 :     * Returns the errorcode.
108 :     */
109 :     native SQL_QueryError(Handle:query, error[], maxlength);
110 :    
111 :    
112 :     /**
113 :     * Returns 1 if there are more results to be read,
114 :     * 0 otherwise.
115 :     */
116 :     native SQL_MoreResults(Handle:query);
117 :    
118 :    
119 :     /**
120 :     * Tells whether a specific column in the current row
121 :     * is NULL or not.
122 :     */
123 :     native SQL_IsNull(Handle:query, column);
124 :    
125 :     /**
126 :     * Retrieves the current result.
127 :     * A successful query starts at the first result,
128 :     * so you should not call SQL_NextRow() first.
129 :     * Passing no extra params - return int
130 :     * Passing one extra param - return float in 1st extra arg
131 :     * Passing two extra params - return string in 1st arg, max length in 2nd
132 :     * Example:
133 :     * new num = SQL_ReadResult(query, 0)
134 :     * new Float:num2
135 :     * new str[32]
136 :     * SQL_ReadResult(query, 1, num2)
137 :     * SQL_ReadResult(query, 2, str, 31)
138 :     */
139 :     native SQL_ReadResult(Handle:query, column, {Float,_}:...);
140 :    
141 :    
142 :     /**
143 :     * Advances to the next result (return value should be ignored).
144 :     */
145 :     native SQL_NextRow(Handle:query);
146 :    
147 :    
148 :     /**
149 :     * Returns the number of affected rows.
150 :     */
151 :     native SQL_AffectedRows(Handle:query);
152 :    
153 :    
154 :     /**
155 :     * Returns the number of rows total.
156 :     */
157 :     native SQL_NumResults(Handle:query);
158 :    
159 :    
160 :     /**
161 :     * Returns the number of columns total.
162 :     */
163 :     native SQL_NumColumns(Handle:query);
164 :    
165 :    
166 :     /**
167 :     * Returns the name of a column.
168 :     * Errors on a bad field number.
169 :     */
170 :     native SQL_FieldNumToName(Handle:query, num, name[], maxlength);
171 :    
172 :    
173 :     /**
174 :     * Returns the number of a named column, or -1 if not found.
175 :     */
176 :     native SQL_FieldNameToNum(Handle:query, const name[]);
177 :    
178 :    
179 :     /**
180 :     * Returns the insert id of the last INSERT query.
181 :     * Returns 0 otherwise.
182 :     */
183 :     native SQL_GetInsertId(Handle:query);
184 :    
185 :    
186 :     /**
187 :     * Returns which driver this plugin is currently bound to.
188 :     */
189 :     native SQL_GetAffinity(driver[], maxlen);
190 :    
191 :     /**
192 :     * Sets driver affinity. You can use this to force a particular
193 :     * driver implementation. This will automatically change all SQL
194 :     * natives in your plugin to be "bound" to the module in question.
195 :     * If no such module is found, it will return 0. This isn't necessarily bad -
196 :     * the user might have typed the wrong driver. Unless your plugin is built
197 :     * to handle different driver types at once, you should let this error pass.
198 :     * Note, that using this while you have open handles to another database
199 :     * type will cause problems. I.e., you cannot open a handle, switch
200 :     * affinity, then close the handle with a different driver.
201 :     * Switching affinity is an O(n*m) operation, where n is the number of
202 :     * SQL natives and m is the number of used natives in total.
203 :     * Intuitive programmers will note that this causes problems for threaded queries.
204 :     * You will have to either force your script to work under one affinity, or to
205 :     * pack the affinity type into the query data, check it against the current, then
206 :     * set the new affinity if necessary. Then, restore the old for safety.
207 :     */
208 :     native SQL_SetAffinity(const driver[]);
209 :    
210 :     /**
211 :     * Returns the original query string that a query handle used.
212 :     */
213 :     native SQL_GetQueryString(Handle:query, buffer[], maxlength);
214 :    
215 :     /**
216 :     * This function can be used to find out if a table in a Sqlite database exists.
217 :     * (updated for newer API)
218 :     */
219 :     stock bool:sqlite_TableExists(Handle:db, const table[])
220 :     {
221 :     new Handle:query = SQL_PrepareQuery(
222 :     db,
223 :     "SELECT name FROM sqlite_master WHERE type='table' AND name='%s' LIMIT 1;",
224 :     table);
225 :    
226 :     if (!SQL_Execute(query) || !SQL_NumResults(query))
227 :     {
228 :     SQL_FreeHandle(query);
229 :     return false;
230 :     }
231 :    
232 :     SQL_FreeHandle(query);
233 :    
234 :     return true;
235 :     }
236 :    
237 :     /**
238 :     * Use this for executing a query where you don't care about the result.
239 :     * Returns 0 on failure, 1 on success
240 :     */
241 :     stock SQL_SimpleQuery(Handle:db, const query[], error[]="", maxlength=0, &rows=0)
242 :     {
243 :     new Handle:hQuery = SQL_PrepareQuery(db, "%s", query);
244 :    
245 :     if (!SQL_Execute(hQuery))
246 :     {
247 :     SQL_QueryError(hQuery, error, maxlength);
248 :     SQL_FreeHandle(hQuery);
249 :     return 0;
250 :     }
251 :    
252 :     rows = SQL_NumResults(hQuery);
253 :    
254 :     SQL_FreeHandle(hQuery);
255 :    
256 :     return 1;
257 :     }
258 :    
259 :     /**
260 :     * Use this for executing a query where you don't care about the result.
261 :     * Returns 0 on failure, 1 on success
262 :     */
263 :     stock SQL_SimpleQueryFmt(Handle:db, error[]="", maxlength=0, &rows=0, const fmt[], ...)
264 :     {
265 :     static query_buf[2048];
266 :     vformat(query_buf, 2047, fmt, 6);
267 :    
268 :     new Handle:hQuery = SQL_PrepareQuery(db, "%s", query_buf);
269 :    
270 :     if (!SQL_Execute(hQuery))
271 :     {
272 :     SQL_QueryError(hQuery, error, maxlength);
273 :     SQL_FreeHandle(hQuery);
274 :     return 0;
275 :     }
276 :    
277 :     rows = SQL_NumResults(hQuery);
278 :    
279 :     SQL_FreeHandle(hQuery);
280 :    
281 :     return 1;
282 :     }
283 :    
284 :     /**
285 :     * Use this for executing a query and not caring about the error.
286 :     * Returns -1 on error, >=0 on success (with number of affected rows)
287 :     */
288 :     stock SQL_QueryAndIgnore(Handle:db, const queryfmt[], {Float,_}:...)
289 :     {
290 :     static query[4096];
291 :     new Handle:hQuery;
292 :     new ret;
293 :    
294 :     vformat(query, sizeof(query)-1, queryfmt, 3);
295 :    
296 :     hQuery = SQL_PrepareQuery(db, "%s", query);
297 :    
298 :     if (SQL_Execute(hQuery))
299 :     {
300 :     ret = SQL_AffectedRows(hQuery);
301 :     } else {
302 :     ret = -1;
303 :     }
304 :    
305 :     SQL_FreeHandle(hQuery);
306 :    
307 :     return ret;
308 :     }
309 :    
310 :     stock Handle:SQL_MakeStdTuple()
311 :     {
312 :     static host[64], user[32], pass[32], db[128];
313 :     static get_type[12], set_type[12];
314 :    
315 :     get_cvar_string("amx_sql_host", host, 63);
316 :     get_cvar_string("amx_sql_user", user, 31);
317 :     get_cvar_string("amx_sql_pass", pass, 31);
318 :     get_cvar_string("amx_sql_type", set_type, 11);
319 :     get_cvar_string("amx_sql_db", db, 127);
320 :    
321 :     SQL_GetAffinity(get_type, 12);
322 :    
323 :     if (!equali(get_type, set_type))
324 :     {
325 :     if (!SQL_SetAffinity(set_type))
326 :     {
327 :     log_amx("Failed to set affinity from %s to %s.", get_type, set_type);
328 :     }
329 :     }
330 :    
331 :     return SQL_MakeDbTuple(host, user, pass, db);
332 :     }

Contact
ViewVC Help
Powered by ViewVC 1.0.4