[Half-Life AMXX] / amxmod_compat / mysql.sma Repository:
ViewVC logotype

Annotation of /amxmod_compat/mysql.sma

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1 - (view) (download)

1 : ian 1 /**
2 :     * AMX Mod Compatibility engine
3 :     * by the AMX Mod X Development Team
4 :     */
5 :    
6 :     #define MAX_CONNECTIONS 64
7 :    
8 :     new Connections[MAX_CONNECTIONS+1] = {0}
9 :     new ConnectionTracker[MAX_CONNECTIONS+1] = {0}
10 :     new ConnectionErrors[MAX_CONNECTIONS+1][255]
11 :     new ConnectionQueries[MAX_CONNECTIONS+1] = {0}
12 :     new QueryPositions[MAX_CONNECTIONS+1]
13 :    
14 :     MySQL_Natives()
15 :     {
16 :     register_native("mysql_connect", "__mysql_connect")
17 :     register_native("mysql_query", "__mysql_query")
18 :     register_native("mysql_error", "__mysql_error")
19 :     register_native("mysql_close", "__mysql_close")
20 :     register_native("mysql_nextrow", "__mysql_nextrow")
21 :     register_native("mysql_getfield", "__mysql_getfield")
22 :     register_native("mysql_getresult", "__mysql_getresult")
23 :     register_native("mysql_affected_rows", "__mysql_affected_rows")
24 :     register_native("mysql_num_fields", "__mysql_num_fields")
25 :     register_native("mysql_num_rows", "__mysql_num_rows")
26 :     register_native("mysql_field_name", "__mysql_field_name")
27 :     register_native("mysql_insert_id", "__mysql_insert_id")
28 :     }
29 :    
30 :     MakeConnectionIndex(Handle:cn)
31 :     {
32 :     if (ConnectionTracker[0])
33 :     {
34 :     new idx = ConnectionTracker[ConnectionTracker[0]]
35 :     ConnectionTracker[0]--
36 :     Connections[idx] = _:cn
37 :     return idx
38 :     } else {
39 :     Connections[0]++
40 :     if (Connections[0] > MAX_CONNECTIONS)
41 :     {
42 :     return 0
43 :     }
44 :    
45 :     Connections[Connections[0]] = _:cn
46 :     return Connections[0]
47 :     }
48 :    
49 :     return 0
50 :     }
51 :    
52 :     Handle:GetConnectionIndex(idx)
53 :     {
54 :     if (idx < 1 || idx > MAX_CONNECTIONS || !Connections[idx])
55 :     {
56 :     return Empty_Handle
57 :     }
58 :    
59 :     return Handle:Connections[idx]
60 :     }
61 :    
62 :     FreeConnectionIndex(idx)
63 :     {
64 :     Connections[idx] = 0
65 :     ConnectionTracker[0]++
66 :     ConnectionTracker[ConnectionTracker[0]] = idx
67 :     ConnectionErrors[idx][0] = 0
68 :     ConnectionQueries[idx] = 0
69 :     QueryPositions[idx] = 0
70 :     }
71 :    
72 :     /*
73 :     * Unlike the previous this does not check for a matching connection.
74 :     * Unless a plugin breaks I'm not going to take that step.
75 :     */
76 :    
77 :     public __mysql_connect(plid, num)
78 :     {
79 :     static host[255], user[128], pass[128], dbname[128], error[512]
80 :     new errcode
81 :    
82 :     get_string(1, host, 254)
83 :     get_string(2, user, 127)
84 :     get_string(3, pass, 127)
85 :     get_string(4, dbname, 127)
86 :    
87 :     new Handle:info = SQL_MakeDbTuple(host, user, pass, dbname)
88 :     new Handle:cn = SQL_Connect(info, errcode, error, 511)
89 :    
90 :     if (cn == Empty_Handle)
91 :     {
92 :     set_string(5, error, get_param(6))
93 :     return 0
94 :     }
95 :    
96 :     SQL_FreeHandle(info)
97 :    
98 :     new idx = MakeConnectionIndex(cn)
99 :     if (idx == 0)
100 :     {
101 :     set_string(5, "Reached max unclosed connections", get_param(6))
102 :     return 0
103 :     }
104 :    
105 :     ConnectionQueries[idx] = 0
106 :    
107 :     return idx
108 :     }
109 :    
110 :     public __mysql_query(plid, num)
111 :     {
112 :     static queryString[4096]
113 :     new cn_idx = get_param(1)
114 :     new Handle:cn
115 :    
116 :     if ((cn=GetConnectionIndex(cn_idx)) == Empty_Handle)
117 :     {
118 :     return 0
119 :     }
120 :    
121 :     vdformat(queryString, 4095, 2, 3)
122 :    
123 :     new Handle:query = SQL_PrepareQuery(cn, "%s", queryString)
124 :    
125 :     if (!SQL_Execute(query))
126 :     {
127 :     SQL_QueryError(query, ConnectionErrors[cn_idx], 254)
128 :     SQL_FreeHandle(query)
129 :     return 0
130 :     }
131 :    
132 :     if (ConnectionQueries[cn_idx])
133 :     {
134 :     SQL_FreeHandle(Handle:ConnectionQueries[cn_idx])
135 :     }
136 :    
137 :     ConnectionQueries[cn_idx] = _:query
138 :     QueryPositions[cn_idx] = 0
139 :    
140 :     return 1
141 :     }
142 :    
143 :     public __mysql_error(plid, num)
144 :     {
145 :     new cn_idx = get_param(1)
146 :    
147 :     if (Connections[cn_idx] < 1)
148 :     {
149 :     static error[255]
150 :     format(error, 254, "Invalid connection index: %d", cn_idx)
151 :     set_string(2, error, get_param(3))
152 :     return 1
153 :     }
154 :    
155 :     set_string(2, ConnectionErrors[cn_idx], get_param(3))
156 :    
157 :     return 1
158 :     }
159 :    
160 :     public __mysql_close(plid, num)
161 :     {
162 :     new cn_idx = get_param(1)
163 :    
164 :     new Handle:cn = GetConnectionIndex(cn_idx)
165 :    
166 :     if (cn == Empty_Handle)
167 :     {
168 :     return 0
169 :     }
170 :    
171 :     new Handle:query = Handle:ConnectionQueries[cn_idx]
172 :    
173 :     if (query != Empty_Handle)
174 :     {
175 :     SQL_FreeHandle(query)
176 :     }
177 :    
178 :     SQL_FreeHandle(cn)
179 :    
180 :     FreeConnectionIndex(cn_idx)
181 :    
182 :     return 1
183 :     }
184 :    
185 :     public __mysql_nextrow(plid, num)
186 :     {
187 :     new cn_idx = get_param(1)
188 :    
189 :     new Handle:cn = GetConnectionIndex(cn_idx)
190 :     if (cn == Empty_Handle)
191 :     {
192 :     return 0
193 :     }
194 :    
195 :     new Handle:query = Handle:ConnectionQueries[cn_idx]
196 :     if (query == Empty_Handle)
197 :     {
198 :     return 0
199 :     }
200 :    
201 :     if (QueryPositions[cn_idx] != 0)
202 :     {
203 :     SQL_NextRow(query)
204 :     }
205 :    
206 :     if (SQL_MoreResults(query))
207 :     {
208 :     return ++QueryPositions[cn_idx]
209 :     }
210 :    
211 :     return 0
212 :     }
213 :    
214 :     public __mysql_getresult(plid, num)
215 :     {
216 :     new cn_idx = get_param(1)
217 :    
218 :     new Handle:cn = GetConnectionIndex(cn_idx)
219 :     if (cn == Empty_Handle)
220 :     {
221 :     return 0
222 :     }
223 :    
224 :     new Handle:query = Handle:ConnectionQueries[cn_idx]
225 :     if (query == Empty_Handle)
226 :     {
227 :     return 0
228 :     }
229 :    
230 :     if (!SQL_MoreResults(query))
231 :     {
232 :     return 0
233 :     }
234 :    
235 :     static name[64]
236 :     get_string(2, name, 63)
237 :     new column = SQL_FieldNameToNum(query, name)
238 :     if (column == -1)
239 :     {
240 :     log_error(AMX_ERR_NATIVE, "Invalid column name: %s", name)
241 :     return 0
242 :     }
243 :    
244 :     switch (num)
245 :     {
246 :     case 2:
247 :     {
248 :     return SQL_ReadResult(query, column)
249 :     }
250 :     case 3:
251 :     {
252 :     new Float:fma
253 :     SQL_ReadResult(query, column, fma)
254 :     set_param_byref(3, _:fma)
255 :     }
256 :     case 4:
257 :     {
258 :     static str[2048]
259 :     SQL_ReadResult(query, column, str, 2047)
260 :     set_string(3, str, get_param_byref(4))
261 :     }
262 :     }
263 :    
264 :     return 1
265 :     }
266 :    
267 :     public __mysql_getfield(plid, num)
268 :     {
269 :     new cn_idx = get_param(1)
270 :    
271 :     new Handle:cn = GetConnectionIndex(cn_idx)
272 :     if (cn == Empty_Handle)
273 :     {
274 :     return 0
275 :     }
276 :    
277 :     new Handle:query = Handle:ConnectionQueries[cn_idx]
278 :     if (query == Empty_Handle)
279 :     {
280 :     return 0
281 :     }
282 :    
283 :     if (!SQL_MoreResults(query))
284 :     {
285 :     return 0
286 :     }
287 :    
288 :     switch (num)
289 :     {
290 :     case 2:
291 :     {
292 :     return SQL_ReadResult(query, get_param(2)-1)
293 :     }
294 :     case 3:
295 :     {
296 :     new Float:fma
297 :     SQL_ReadResult(query, get_param(2)-1, fma)
298 :     set_param_byref(3, _:fma)
299 :     }
300 :     case 4:
301 :     {
302 :     static str[2048]
303 :     SQL_ReadResult(query, get_param(2)-1, str, 2047)
304 :     set_string(3, str, get_param_byref(4))
305 :     }
306 :     }
307 :    
308 :     return 1
309 :     }
310 :    
311 :     public __mysql_affected_rows(plid, num)
312 :     {
313 :     new cn_idx = get_param(1)
314 :    
315 :     new Handle:cn = GetConnectionIndex(cn_idx)
316 :     if (cn == Empty_Handle)
317 :     {
318 :     return 0
319 :     }
320 :    
321 :     new Handle:query = Handle:ConnectionQueries[cn_idx]
322 :     if (query == Empty_Handle)
323 :     {
324 :     return 0
325 :     }
326 :    
327 :     return SQL_AffectedRows(query)
328 :     }
329 :    
330 :     public __mysql_num_fields(plid, num)
331 :     {
332 :     new cn_idx = get_param(1)
333 :    
334 :     new Handle:cn = GetConnectionIndex(cn_idx)
335 :     if (cn == Empty_Handle)
336 :     {
337 :     return 0
338 :     }
339 :    
340 :     new Handle:query = Handle:ConnectionQueries[cn_idx]
341 :     if (query == Empty_Handle)
342 :     {
343 :     return 0
344 :     }
345 :    
346 :     return SQL_NumColumns(query)
347 :     }
348 :    
349 :     public __mysql_insert_id(plid, num)
350 :     {
351 :     new cn_idx = get_param(1)
352 :    
353 :     new Handle:cn = GetConnectionIndex(cn_idx)
354 :     if (cn == Empty_Handle)
355 :     {
356 :     return 0
357 :     }
358 :    
359 :     new Handle:query = Handle:ConnectionQueries[cn_idx]
360 :     if (query == Empty_Handle)
361 :     {
362 :     return 0
363 :     }
364 :    
365 :     return SQL_GetInsertId(query)
366 :     }
367 :    
368 :     public __mysql_num_rows(plid, num)
369 :     {
370 :     new cn_idx = get_param(1)
371 :    
372 :     new Handle:cn = GetConnectionIndex(cn_idx)
373 :     if (cn == Empty_Handle)
374 :     {
375 :     return 0
376 :     }
377 :    
378 :     new Handle:query = Handle:ConnectionQueries[cn_idx]
379 :     if (query == Empty_Handle)
380 :     {
381 :     return 0
382 :     }
383 :    
384 :     return SQL_NumResults(query)
385 :     }
386 :    
387 :     public __mysql_field_name(plid, num)
388 :     {
389 :     new cn_idx = get_param(1)
390 :    
391 :     new Handle:cn = GetConnectionIndex(cn_idx)
392 :     if (cn == Empty_Handle)
393 :     {
394 :     return 0
395 :     }
396 :    
397 :     new Handle:query = Handle:ConnectionQueries[cn_idx]
398 :     if (query == Empty_Handle)
399 :     {
400 :     return 0
401 :     }
402 :    
403 :     new column = get_param(2) - 1
404 :     if (column < 0 || column >= SQL_NumColumns(query))
405 :     {
406 :     return 0
407 :     }
408 :    
409 :     new field[64]
410 :     SQL_FieldNumToName(query, column, field, 63)
411 :    
412 :     set_string(3, field, get_param(4))
413 :    
414 :     return 1
415 :     }

Contact
ViewVC Help
Powered by ViewVC 1.0.4