Parent Directory | Revision Log
Revision 1 - (view) (download)
1 : | ian | 1 | /* SQL Database API |
2 : | * By the AMX Mod X Development Team | ||
3 : | * Notes - Read the comments! Make sure your plugins use | ||
4 : | * nice ANSI SQL and don't use database column names like "key" | ||
5 : | * otherwise this API will be a nightmare | ||
6 : | * Never do error checking with the not operator! This is bad: | ||
7 : | * if (!dbi_query()) | ||
8 : | * You should do: | ||
9 : | * ret = dbi_query() | ||
10 : | * if (ret < 0) | ||
11 : | * This is because DBI functions can and will return negative numbers | ||
12 : | * Negative numbers evaluate to "true" in AMX. | ||
13 : | */ | ||
14 : | |||
15 : | #if defined _dbi_included | ||
16 : | #endinput | ||
17 : | #endif | ||
18 : | #define _dbi_included | ||
19 : | |||
20 : | // You can't include SQLX first! | ||
21 : | // there's really no reason to anyway. | ||
22 : | #assert !defined _sqlx_included | ||
23 : | |||
24 : | #if AMXX_VERSION_NUM >= 175 | ||
25 : | #pragma reqclass dbi | ||
26 : | #else | ||
27 : | #pragma library dbi | ||
28 : | #endif | ||
29 : | |||
30 : | enum Sql | ||
31 : | { | ||
32 : | SQL_FAILED=0, | ||
33 : | SQL_OK | ||
34 : | } | ||
35 : | |||
36 : | enum Result | ||
37 : | { | ||
38 : | RESULT_FAILED=-1, | ||
39 : | RESULT_NONE, | ||
40 : | RESULT_OK | ||
41 : | } | ||
42 : | |||
43 : | /* This will return a number equal to or below 0 on failure. | ||
44 : | * If it does fail, the error will be mirrored in dbi_error() | ||
45 : | * The return value will otherwise be a resource handle, not an | ||
46 : | * OK code or cell pointer. | ||
47 : | */ | ||
48 : | native Sql:dbi_connect(_host[], _user[], _pass[], _dbname[], _error[]="", _maxlength=0); | ||
49 : | |||
50 : | /* This will do a simple query execution on the SQL server. | ||
51 : | * If it fails, it will return a number BELOW ZERO (0) | ||
52 : | * If zero, it succeeded with NO RETURN RESULT. | ||
53 : | * If greater than zero, make sure to call dbi_free_result() on it! | ||
54 : | * The return is a handle to the result set | ||
55 : | */ | ||
56 : | native Result:dbi_query(Sql:_sql, _query[], {Float,_}:...); | ||
57 : | |||
58 : | /* Has the same usage as dbi_query, but this native returns by | ||
59 : | * reference the number of rows affected in the query. If the | ||
60 : | * query fails rows will be equal to -1. | ||
61 : | */ | ||
62 : | native Result:dbi_query2(Sql:_sql, &rows, _query[], {Float,_}:...); | ||
63 : | |||
64 : | /* Returns 0 on failure or End of Results. | ||
65 : | * Advances result pointer by one row. | ||
66 : | */ | ||
67 : | native dbi_nextrow(Result:_result); | ||
68 : | |||
69 : | /* Gets a field by number. Returns 0 on failure. | ||
70 : | * Although internally fields always start from 0, | ||
71 : | * This function takes fieldnum starting from 1. | ||
72 : | * No extra params: returns int | ||
73 : | * One extra param: returns Float: byref | ||
74 : | * Two extra param: Stores string with length | ||
75 : | */ | ||
76 : | native dbi_field(Result:_result, _fieldnum, {Float,_}:... ); | ||
77 : | |||
78 : | /* Gets a field by name. Returns 0 on failure. | ||
79 : | * One extra param: returns Float: byref | ||
80 : | * Two extra param: Stores string with length | ||
81 : | */ | ||
82 : | native dbi_result(Result:_result, _field[], {Float,_}:... ); | ||
83 : | |||
84 : | /* Returns the number of rows returned from a query | ||
85 : | */ | ||
86 : | native dbi_num_rows(Result:_result); | ||
87 : | |||
88 : | /* Frees memory used by a result handle. Do this or get memory leaks. | ||
89 : | */ | ||
90 : | native dbi_free_result(&Result:result); | ||
91 : | |||
92 : | /* Closes a database handle. Internally, it will also | ||
93 : | * mark the handle as free, so this particular handle may | ||
94 : | * be re-used in the future to save time. | ||
95 : | */ | ||
96 : | native dbi_close(&Sql:_sql); | ||
97 : | |||
98 : | /* Returns an error message set. For PGSQL and MySQL, | ||
99 : | * this is a direct error return from the database handle/API. | ||
100 : | * For MSSQL, it returns the last error message found from a | ||
101 : | * thrown exception. | ||
102 : | */ | ||
103 : | native dbi_error(Sql:_sql, _error[], _len); | ||
104 : | |||
105 : | /* Returns the type of database being used. So far: | ||
106 : | * "mysql", "pgsql", "mssql", "sqlite" | ||
107 : | */ | ||
108 : | native dbi_type(_type[], _len); | ||
109 : | |||
110 : | /* Returns the number of fields/colums in a result set. | ||
111 : | * Unlike dbi_nextrow, you must pass a valid result handle. | ||
112 : | */ | ||
113 : | native dbi_num_fields(Result:result); | ||
114 : | |||
115 : | /* Retrieves the name of a field/column in a result set. | ||
116 : | * Requires a valid result handle, and columns are numbered 1 to n. | ||
117 : | */ | ||
118 : | native dbi_field_name(Result:result, field, name[], maxLength); | ||
119 : | |||
120 : | /* This function can be used to find out if a table in a Sqlite database exists. | ||
121 : | */ | ||
122 : | stock bool:sqlite_table_exists(Sql:sql, table[]) | ||
123 : | { | ||
124 : | new bool:exists; | ||
125 : | new query[128]; | ||
126 : | format(query, 127, "SELECT name FROM sqlite_master WHERE type='table' AND name='%s' LIMIT 1;", table); | ||
127 : | |||
128 : | new Result:result = dbi_query(sql, query); | ||
129 : | |||
130 : | if (dbi_nextrow(result)) | ||
131 : | { | ||
132 : | exists = true; | ||
133 : | } | ||
134 : | else | ||
135 : | { | ||
136 : | exists = false; | ||
137 : | } | ||
138 : | |||
139 : | if (result > RESULT_NONE) | ||
140 : | { | ||
141 : | dbi_free_result(result); | ||
142 : | } | ||
143 : | |||
144 : | return exists; | ||
145 : | } |
Contact | ViewVC Help |
Powered by ViewVC 1.0.4 |