* NEW: Add a driver API that handles debugging messages.
* NEW: Print the current date/time before any debugging message.

[GB.DB.MYSQL]
* NEW: Use the new driver API to print debugging messages.

[GB.DB.ODBC]
* NEW: Use the new driver API to print debugging messages.

[GB.DB.POSTGRESQL]
* NEW: Use the new driver API to print debugging messages.

[GB.DB.SQLITE2]
* NEW: Use the new driver API to print debugging messages.

[GB.DB.SQLITE3]
* NEW: Use the new driver API to print debugging messages.
This commit is contained in:
gambas 2019-08-30 09:09:16 +02:00
parent 17d57a712a
commit 234c2c690a
9 changed files with 55 additions and 55 deletions

View file

@ -481,8 +481,7 @@ static void check_connection(MYSQL *conn)
if (mysql_thread_id(conn) != thread_id)
{
if (DB.IsDebug())
fprintf(stderr, "gb.db.mysql: connection lost\n");
DB.Debug("gb.db.mysql", "connection lost\n");
// Connection has been reestablished, set utf8 again
mysql_query(conn, "set names 'utf8'");
}
@ -512,8 +511,7 @@ static int do_query(DB_DATABASE *db, const char *error, MYSQL_RES **pres, const
else
query = qtemp;
if (DB.IsDebug())
fprintf(stderr, "gb.db.mysql: %p: %s\n", conn, query);
DB.Debug("gb.db.mysql", "%p: %s", conn, query);
check_connection(conn);

View file

@ -136,15 +136,15 @@ void reportODBCError(const char *fn,
SQLSMALLINT len;
SQLRETURN ret;
if(DB.IsDebug())
if (DB.IsDebug())
{
fprintf(stderr, "gb.db.odbc: %s\n", fn);
DB.Debug("gb.db.odbc", fn);
do
{
ret = SQLGetDiagRec(type, handle, ++i, state, &native, text, sizeof(text), &len);
if (SQL_SUCCEEDED(ret))
fprintf(stderr, "gb.db.odbc: %d:%s:%d:%s\n", (int)i, (char *)state, (int)native, (char *)text);
DB.Debug("gb.db.odbc", "%d:%s:%d:%s", (int)i, (char *)state, (int)native, (char *)text);
}
while (ret == SQL_SUCCESS);
}
@ -221,10 +221,7 @@ int GetRecordCount(SQLHANDLE stmtHandle, SQLINTEGER cursorScrollable)
//Make sure the statement has a cursor
if (!(stmtHandle && (cursorScrollable == SQL_TRUE)))
{
if (DB.IsDebug())
{
fprintf(stderr, "gb.db.odbc: Cannot do GetRecordCount()!\n");
}
DB.Debug("gb.db.odbc", "cannot do GetRecordCount()!");
return ((int) myRecCnt);
}
@ -250,10 +247,7 @@ int GetRecordCount(SQLHANDLE stmtHandle, SQLINTEGER cursorScrollable)
//Make sure the statement has a cursor
if (formerRecIdx < 0)
{
if (DB.IsDebug())
{
fprintf(stderr, "gb.db.odbc.GetRecordCount: Current record returned %d, returning -1 as count.\n", formerRecIdx);
}
DB.Debug("gb.db.odbc", "GetRecordCount: Current record returned %d, returning -1 as count", formerRecIdx);
return ((int) myRecCnt);
}
@ -270,10 +264,7 @@ int GetRecordCount(SQLHANDLE stmtHandle, SQLINTEGER cursorScrollable)
if (SQL_SUCCEEDED(retcode))
{
//Inform first recno if in Debug mode and carry on
if (DB.IsDebug())
{
fprintf(stderr, "gb.db.odbc.GetRecordCount: First recno=%d\n", (int) firstRecNo);
}
DB.Debug("gb.db.odbc", "GetRecordCount: First recno=%d", (int) firstRecNo);
} else {
//Could not fetch the first recno: Abort!
reportODBCError("SQLFetchScroll SQL_ATTR_ROW_NUMBER (first recno)", stmtHandle, SQL_HANDLE_STMT);
@ -292,11 +283,7 @@ int GetRecordCount(SQLHANDLE stmtHandle, SQLINTEGER cursorScrollable)
if (SQL_SUCCEEDED(retcode))
{
//Set ret value
if (DB.IsDebug())
{
fprintf(stderr, "gb.db.odbc.GetRecordCount: Last recno=%d\n", (int) lastRecNo);
}
DB.Debug("gb.db.odbc", "GetRecordCount: Last recno=%d", (int) lastRecNo);
} else {
reportODBCError("SQLGetStmtAttr SQL_ATTR_ROW_NUMBER (last recno)", stmtHandle, SQL_HANDLE_STMT);
}
@ -325,10 +312,7 @@ int GetRecordCount(SQLHANDLE stmtHandle, SQLINTEGER cursorScrollable)
}
myRecCnt = (lastRecNo - firstRecNo + 1);
if (DB.IsDebug())
{
fprintf(stderr, "gb.db.odbc.GetRecordCount: Record count=%d\n", (int) myRecCnt);
}
DB.Debug("gb.db.odbc", "GetRecordCount: Record count=%d", (int) myRecCnt);
return ((int) myRecCnt);
@ -704,15 +688,10 @@ void GetConnectedDBName(DB_DESC *desc, ODBC_CONN *odbc)
free(dbName);
}
if (DB.IsDebug())
{
if (desc->name)
{
fprintf(stderr, "gb.db.odbc.GetConnectedDBName: desc->name (%d chars):'%s'.\n", (int)charsNeeded, desc->name);
} else {
fprintf(stderr, "gb.db.odbc.GetConnectedDBName: desc->name: NULL.\n");
}
}
if (desc->name)
DB.Debug("gb.db.odbc", "GetConnectedDBName: desc->name (%d chars): '%s'", (int)charsNeeded, desc->name);
else
DB.Debug("gb.db.odbc", "GetConnectedDBName: desc->name: NULL");
}
@ -1091,8 +1070,8 @@ fflush(stderr);
else
query = qtemp;
if (DB.IsDebug())
fprintf(stderr, "gb.db.odbc.do_query: res %p, dbc handle %p, query '%s'\n", res, handle, query);
//DB.Debug("gb.db.odbc", "do_query: res %p, dbc handle %p, query '%s'", res, handle, query);
DB.Debug("gb.db.odbc", "%p: %s", handle, query);
GB.AllocZero(POINTER(&odbcres), sizeof(ODBC_RESULT));
@ -1118,8 +1097,7 @@ fflush(stderr);
retcode = SQLExecDirect(odbcres->odbcStatHandle, (SQLCHAR *) query, SQL_NTS);
if ((retcode != SQL_SUCCESS) && (retcode != SQL_SUCCESS_WITH_INFO) && (retcode != SQL_NO_DATA))
{
if (DB.IsDebug())
fprintf(stderr, "gb.db.odbc.do_query: SQLExecDirect() returned code %d\n", (int)retcode);
DB.Debug("gb.db.odbc", "do_query: SQLExecDirect() returned code %d", (int)retcode);
throwODBCError("SQLExecDirect", odbcres->odbcStatHandle, SQL_HANDLE_STMT);
SQLFreeHandle(SQL_HANDLE_STMT, odbcres->odbcStatHandle);
//GB.Error("Error while executing the statement");

View file

@ -551,11 +551,7 @@ static int do_query(DB_DATABASE *db, const char *error, PGresult **pres, const c
else
query = qtemp;
if (DB.IsDebug())
{
fprintf(stderr, "gb.db.postgresql: %p: %s\n", conn, query);
fflush(stderr);
}
DB.Debug("gb.db.postgresql", "%p: %s", conn, query);
res = PQexec(conn, query);
ret = check_result(res, error);

View file

@ -284,8 +284,7 @@ static int do_query(DB_DATABASE *db, const char *error, Dataset **pres,
_print_query = FALSE;
}
if (DB.IsDebug())
fprintf(stderr, "sqlite2: %p: %s\n", conn, query);
DB.Debug("sqlite2","%p: %s", conn, query);
if (strncasecmp("select",query,6) == 0){

View file

@ -265,8 +265,7 @@ static int do_query(DB_DATABASE *db, const char *error, SQLITE_RESULT **pres, co
_print_query = FALSE;
}
if (DB.IsDebug())
fprintf(stderr, "gb.db.sqlite3: %p: %s\n", conn, query);
DB.Debug("gb.db.sqlite3","%p: %s", conn, query);
if (db->timeout > 0)
max_retry = db->timeout * 5;
@ -2343,8 +2342,7 @@ static int database_create(DB_DATABASE *db, const char *name)
_CREATE_DATABASE:
if (DB.IsDebug())
fprintf(stderr, "sqlite3: create database: %s\n", fullpath);
DB.Debug("gb.db.sqlite3", "create database: %s", fullpath);
conn = sqlite_open_database(fullpath, host);
GB.FreeString(&fullpath);

View file

@ -1762,4 +1762,3 @@ void LOCAL_set_first_day_of_week(char day)
if (day >= -1 && day <= 6)
LOCAL_first_day_of_week = day;
}

View file

@ -42,7 +42,7 @@ typedef
/* LIMIT position */
#define DB_LIMIT_NONE 0
#define DB_LIMIT_NONE 0
#define DB_LIMIT_AT_BEGIN 1
#define DB_LIMIT_AT_END 2
@ -225,7 +225,8 @@ typedef
void (*Register)(DB_DRIVER *);
void (*Format)(DB_DRIVER *, GB_VALUE *, DB_FORMAT_CALLBACK);
void (*FormatVariant)(DB_DRIVER *, GB_VARIANT_VALUE *, DB_FORMAT_CALLBACK);
int (*IsDebug)(void);
bool (*IsDebug)();
void (*Debug)(const char *, const char *, ...);
void (*TryAnother)(const char *);
char *(*SubstString)(const char *, int, DB_SUBST_CALLBACK);
char *(*QuoteString)(const char *, int, char);

View file

@ -28,6 +28,7 @@
#include <stdarg.h>
#include <string.h>
#include <ctype.h>
#include <sys/time.h>
#include "gb_common.h"
@ -466,6 +467,34 @@ int DB_IsDebug(void)
return _debug;
}
void DB_Debug(const char *prefix, const char *msg, ...)
{
va_list args;
struct timeval tv;
GB_DATE_SERIAL *date;
GB_DATE val;
if (!_debug)
return;
if (gettimeofday(&tv, NULL) == 0)
{
GB.MakeDateFromTime((time_t)tv.tv_sec, tv.tv_usec, &val);
date = GB.SplitDate(&val);
fprintf(stderr, "%04d-%02d-%02d %02d:%02d:%02d.%03d ", date->year, date->month, date->day, date->hour, date->min, date->sec, date->msec);
}
fprintf(stderr, "%s: ", prefix);
va_start(args, msg);
vfprintf(stderr, msg, args);
va_end(args);
fputc('\n', stderr);
fflush(stderr);
}
static char *_quote;
DB_SUBST_CALLBACK _quote_cb;
@ -660,6 +689,7 @@ void *GB_DB_1[] EXPORT = {
(void *)DB_Format,
(void *)DB_FormatVariant,
(void *)DB_IsDebug,
(void *)DB_Debug,
(void *)DB_TryAnother,
(void *)DB_SubstString,
(void *)DB_QuoteString,

View file

@ -51,6 +51,7 @@ GB_ARRAY DB_StringArrayToGambasArray(char **array);
int DB_FindStringArray(char **array, const char *elt);
void DB_SetDebug(int debug);
int DB_IsDebug(void);
void DB_Debug(const char *prefix, const char *msg, ...);
void DB_TryAnother(const char *);
void q_init(void);