From 9627b4803d731f997c40adbadc23cc5b9435625d Mon Sep 17 00:00:00 2001 From: David Anderson Date: Fri, 28 May 2004 09:21:11 +0000 Subject: [PATCH] Added dbi_type() --- dlls/mssql/mssql.cpp | 7 ++++++ dlls/pgsql/pgsql_amx.cpp | 6 +++++ plugins/include/dbi.inc | 51 +++++++++++++++++++++++++++++++++++----- 3 files changed, 58 insertions(+), 6 deletions(-) diff --git a/dlls/mssql/mssql.cpp b/dlls/mssql/mssql.cpp index cc0b9fa1..4506ba27 100755 --- a/dlls/mssql/mssql.cpp +++ b/dlls/mssql/mssql.cpp @@ -296,6 +296,12 @@ static cell AMX_NATIVE_CALL mssql_error(AMX *amx, cell *params) return lastError; } +static cell AMX_NATIVE_CALL dbi_type(AMX *amx, cell *params) +{ + return MF_SetAmxString(amx, params[1], "mssql", params[2]); +} + + void OnAmxxAttach() { MF_AddNatives(mssql_Natives); @@ -326,6 +332,7 @@ AMX_NATIVE_INFO mssql_Natives[] = { {"dbi_nextrow", mssql_nextrow}, {"mssql_error", mssql_error}, {"dbi_error", mssql_error}, + {"dbi_type", dbi_type}, {NULL, NULL}, /////////////////// diff --git a/dlls/pgsql/pgsql_amx.cpp b/dlls/pgsql/pgsql_amx.cpp index 1eb69ac0..f433086e 100755 --- a/dlls/pgsql/pgsql_amx.cpp +++ b/dlls/pgsql/pgsql_amx.cpp @@ -250,6 +250,11 @@ static cell AMX_NATIVE_CALL pgsql_close(AMX *amx, cell *params) return 1; } +static cell AMX_NATIVE_CALL dbi_type(AMX *amx, cell *params) +{ + return MF_SetAmxString(amx, params[1], "pgsql", params[2]); +} + void OnAmxxAttach() { MF_AddNatives(pgsql_exports); @@ -262,6 +267,7 @@ AMX_NATIVE_INFO pgsql_exports[] = { {"dbi_nextrow", pgsql_nextrow}, {"dbi_close", pgsql_close}, {"dbi_getfield", pgsql_getfield}, + {"dbi_type", dbi_type}, {"pgsql_connect", pgsql_connect}, {"pgsql_error", pgsql_error}, {"pgsql_query", pgsql_query}, diff --git a/plugins/include/dbi.inc b/plugins/include/dbi.inc index 04acab34..b673fa87 100755 --- a/plugins/include/dbi.inc +++ b/plugins/include/dbi.inc @@ -1,5 +1,15 @@ /* SQL Database API * By the AMX Mod X Development Team + * Notes - Read the comments! Make sure your plugins use + * nice ANSI SQL and don't use database column names like "key" + * otherwise this API will be a nightmare + * Never do error checking with the not operator! This is bad: + * if (!dbi_query()) + * You should do: + * ret = dbi_query() + * if (ret <= 0) + * This is because DBI functions can and will return negative numbers + * Negative numbers evaluate to "true" in AMX. */ #if defined _dbi_included @@ -7,14 +17,43 @@ #endif #define _dbi_included -native dbi_connect(host[], user[], pass[], dbname[], error[], maxlength); +/* This will return a number equal to or below 0 on failure. + * If it does fail, the error will be mirrored in dbi_error() + * The return value will otherwise be a resource handle, not an + * OK code or cell pointer. + */ +native dbi_connect(_host[], _user[], _pass[], _dbname[], _error[]="", _maxlength=0); -native dbi_query(sql, query[], {Float,_}:...); +/* This will do a simple query execution on the SQL server. + * If it fails, it will return a number equal to or below 0 + */ +native dbi_query(_sql, _query[], {Float,_}:...); -native dbi_nextrow(sql); +/* Returns 0 on failure or End of Results. + * Advances result pointer by one row. + */ +native dbi_nextrow(_sql); -native dbi_getfield(sql, fieldnum, dest[], maxlen); +/* Gets a field by number. Returns 0 on failure. + * Although internally fields always start from 0, + * This function takes fieldnum starting from 1. + */ +native dbi_getfield(_sql, _fieldnum, _dest[], _maxlen); -native dbi_close(sql); +/* Closes a database handle. Internally, it will also + * mark the handle as free, so this particular handle may + * be re-used in the future to save time. + */ +native dbi_close(_sql); -native dbi_error(sql, error[], len); \ No newline at end of file +/* Returns an error message set. For PGSQL and MySQL, + * this is a direct error return from the database handle/API. + * For MSSQL, it returns the last error message found from a + * thrown exception. + */ +native dbi_error(_sql, _error[], _len); + +/* Returns the type of database being used. So far: + * "mysql", "pgsql", "mssql" + */ +native dbi_type(_type[], _len);