* 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 (mysql_thread_id(conn) != thread_id)
{ {
if (DB.IsDebug()) DB.Debug("gb.db.mysql", "connection lost\n");
fprintf(stderr, "gb.db.mysql: connection lost\n");
// Connection has been reestablished, set utf8 again // Connection has been reestablished, set utf8 again
mysql_query(conn, "set names 'utf8'"); 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 else
query = qtemp; query = qtemp;
if (DB.IsDebug()) DB.Debug("gb.db.mysql", "%p: %s", conn, query);
fprintf(stderr, "gb.db.mysql: %p: %s\n", conn, query);
check_connection(conn); check_connection(conn);

View file

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

View file

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

View file

@ -42,7 +42,7 @@ typedef
/* LIMIT position */ /* LIMIT position */
#define DB_LIMIT_NONE 0 #define DB_LIMIT_NONE 0
#define DB_LIMIT_AT_BEGIN 1 #define DB_LIMIT_AT_BEGIN 1
#define DB_LIMIT_AT_END 2 #define DB_LIMIT_AT_END 2
@ -225,7 +225,8 @@ typedef
void (*Register)(DB_DRIVER *); void (*Register)(DB_DRIVER *);
void (*Format)(DB_DRIVER *, GB_VALUE *, DB_FORMAT_CALLBACK); void (*Format)(DB_DRIVER *, GB_VALUE *, DB_FORMAT_CALLBACK);
void (*FormatVariant)(DB_DRIVER *, GB_VARIANT_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 *); void (*TryAnother)(const char *);
char *(*SubstString)(const char *, int, DB_SUBST_CALLBACK); char *(*SubstString)(const char *, int, DB_SUBST_CALLBACK);
char *(*QuoteString)(const char *, int, char); char *(*QuoteString)(const char *, int, char);

View file

@ -28,6 +28,7 @@
#include <stdarg.h> #include <stdarg.h>
#include <string.h> #include <string.h>
#include <ctype.h> #include <ctype.h>
#include <sys/time.h>
#include "gb_common.h" #include "gb_common.h"
@ -466,6 +467,34 @@ int DB_IsDebug(void)
return _debug; 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; static char *_quote;
DB_SUBST_CALLBACK _quote_cb; DB_SUBST_CALLBACK _quote_cb;
@ -660,6 +689,7 @@ void *GB_DB_1[] EXPORT = {
(void *)DB_Format, (void *)DB_Format,
(void *)DB_FormatVariant, (void *)DB_FormatVariant,
(void *)DB_IsDebug, (void *)DB_IsDebug,
(void *)DB_Debug,
(void *)DB_TryAnother, (void *)DB_TryAnother,
(void *)DB_SubstString, (void *)DB_SubstString,
(void *)DB_QuoteString, (void *)DB_QuoteString,

View file

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