Version 18.13.2

This commit is contained in:
Antonio Cañas Vargas 2018-11-04 20:51:38 +01:00
parent 845d77a8a5
commit e7ba3b873a
26 changed files with 160 additions and 177 deletions

View File

@ -234,7 +234,10 @@ unsigned ID_GetListUsrCodsFromUsrID (struct UsrData *UsrDat,
Str_Concat (Query," AND Confirmed='Y'", Str_Concat (Query," AND Confirmed='Y'",
MaxLength); MaxLength);
ListUsrCods->NumUsrs = (unsigned) DB_QuerySELECT_old (&Query,&mysql_res,"can not get user's codes"); ListUsrCods->NumUsrs =
(unsigned) DB_QuerySELECTusingQueryStr (&Query,&mysql_res,
"can not get user's codes");
if (ListUsrCods->NumUsrs) if (ListUsrCods->NumUsrs)
{ {
/***** Allocate space for the list of users' codes *****/ /***** Allocate space for the list of users' codes *****/

View File

@ -2449,41 +2449,44 @@ static void Att_GetNumStdsTotalWhoAreInAttEvent (struct AttendanceEvent *Att)
static unsigned Att_GetNumStdsFromAListWhoAreInAttEvent (long AttCod,long LstSelectedUsrCods[],unsigned NumStdsInList) static unsigned Att_GetNumStdsFromAListWhoAreInAttEvent (long AttCod,long LstSelectedUsrCods[],unsigned NumStdsInList)
{ {
char *Query = NULL; char *SubQueryAllUsrs = NULL;
char SubQuery[1 + 1 + 10 + 1]; char SubQueryOneUsr[1 + 1 + 10 + 1];
unsigned NumStd; unsigned NumStd;
unsigned NumStdsInAttEvent = 0; unsigned NumStdsInAttEvent = 0;
size_t MaxLength; size_t MaxLength;
if (NumStdsInList) if (NumStdsInList)
{ {
/***** Allocate space for query *****/ /***** Allocate space for subquery *****/
MaxLength = 256 + NumStdsInList * (1 + 1 + 10); MaxLength = 256 + NumStdsInList * (1 + 1 + 10);
if ((Query = (char *) malloc (MaxLength + 1)) == NULL) if ((SubQueryAllUsrs = (char *) malloc (MaxLength + 1)) == NULL)
Lay_NotEnoughMemoryExit (); Lay_NotEnoughMemoryExit ();
SubQueryAllUsrs[0] = '\0';
/***** Count number of students registered in an event in database *****/ /***** Count number of students registered in an event in database *****/
snprintf (Query,MaxLength + 1,
"SELECT COUNT(*) FROM att_usr"
" WHERE AttCod=%ld"
" AND UsrCod IN (",
AttCod);
for (NumStd = 0; for (NumStd = 0;
NumStd < NumStdsInList; NumStd < NumStdsInList;
NumStd++) NumStd++)
{ if (NumStd)
snprintf (SubQuery,sizeof (SubQuery), {
NumStd ? ",%ld" : snprintf (SubQueryOneUsr,sizeof (SubQueryOneUsr),
"%ld", ",%ld",
LstSelectedUsrCods[NumStd]); LstSelectedUsrCods[NumStd]);
Str_Concat (Query,SubQuery, Str_Concat (SubQueryAllUsrs,SubQueryOneUsr,
MaxLength); MaxLength);
} }
Str_Concat (Query,") AND Present='Y'", else
MaxLength); snprintf (SubQueryAllUsrs,sizeof (SubQueryOneUsr),
"%ld",
LstSelectedUsrCods[NumStd]);
NumStdsInAttEvent =
NumStdsInAttEvent = (unsigned) DB_QueryCOUNT_old (&Query,"can not get number of students from a list who are registered in an event"); (unsigned) DB_QueryCOUNT ("can not get number of students"
" from a list who are registered in an event",
"SELECT COUNT(*) FROM att_usr"
" WHERE AttCod=%ld"
" AND UsrCod IN (%s) AND Present='Y'",
AttCod,SubQueryAllUsrs);
} }
return NumStdsInAttEvent; return NumStdsInAttEvent;
} }

View File

@ -2850,8 +2850,7 @@ static void Ctr_CreateCentre (unsigned Status)
unsigned Ctr_GetNumCtrsTotal (void) unsigned Ctr_GetNumCtrsTotal (void)
{ {
/***** Get total number of centres from database *****/ /***** Get total number of centres from database *****/
return (unsigned) DB_QueryCOUNT ("can not get total number of centres", return (unsigned) DB_GetNumRowsTable ("centres");
"SELECT COUNT(*) FROM centres");
} }
/*****************************************************************************/ /*****************************************************************************/

View File

@ -355,10 +355,12 @@ En OpenSWAD:
ps2pdf source.ps destination.pdf ps2pdf source.ps destination.pdf
*/ */
#define Log_PLATFORM_VERSION "SWAD 18.13 (2018-11-03)" #define Log_PLATFORM_VERSION "SWAD 18.13.2 (2018-11-04)"
#define CSS_FILE "swad18.4.css" #define CSS_FILE "swad18.4.css"
#define JS_FILE "swad17.17.1.js" #define JS_FILE "swad17.17.1.js"
/* /*
Version 18.13.2: Nov 04, 2018 Code refactoring related to databse queries. (236555 lines)
Version 18.13.1: Nov 04, 2018 New database funtion to get number of rows in a table. (236572 lines)
Version 18.13: Nov 03, 2018 Joining building and performing query into one function. (236563 lines) Version 18.13: Nov 03, 2018 Joining building and performing query into one function. (236563 lines)
Version 18.12: Nov 03, 2018 Code refactoring in statistics. (236471 lines) Version 18.12: Nov 03, 2018 Code refactoring in statistics. (236471 lines)
Version 18.11.27: Nov 03, 2018 Joining building and performing query into one function. Version 18.11.27: Nov 03, 2018 Joining building and performing query into one function.

View File

@ -2284,9 +2284,7 @@ static void Cty_CreateCountry (struct Country *Cty)
unsigned Cty_GetNumCtysTotal (void) unsigned Cty_GetNumCtysTotal (void)
{ {
/***** Get total number of countries from database *****/ /***** Get total number of countries from database *****/
return return (unsigned) DB_GetNumRowsTable ("countries");
(unsigned) DB_QueryCOUNT ("can not get the total number of countries",
"SELECT COUNT(*) FROM countries");
} }
/*****************************************************************************/ /*****************************************************************************/

View File

@ -794,8 +794,7 @@ static void Crs_WriteListMyCoursesToSelectOne (void)
unsigned Crs_GetNumCrssTotal (void) unsigned Crs_GetNumCrssTotal (void)
{ {
/***** Get total number of courses from database *****/ /***** Get total number of courses from database *****/
return (unsigned) DB_QueryCOUNT ("can not get the total number of courses", return (unsigned) DB_GetNumRowsTable ("courses");
"SELECT COUNT(*) FROM courses");
} }
/*****************************************************************************/ /*****************************************************************************/

View File

@ -3105,10 +3105,10 @@ unsigned long DB_QuerySELECT (MYSQL_RES **mysql_res,const char *MsgError,
// vasprintf will return -1 // vasprintf will return -1
Lay_NotEnoughMemoryExit (); Lay_NotEnoughMemoryExit ();
return DB_QuerySELECT_old (&Query,mysql_res,MsgError); return DB_QuerySELECTusingQueryStr (&Query,mysql_res,MsgError);
} }
unsigned long DB_QuerySELECT_old (char **Query,MYSQL_RES **mysql_res,const char *MsgError) unsigned long DB_QuerySELECTusingQueryStr (char **Query,MYSQL_RES **mysql_res,const char *MsgError)
{ {
int Result; int Result;
@ -3136,11 +3136,22 @@ unsigned long DB_QuerySELECT_old (char **Query,MYSQL_RES **mysql_res,const char
/**************** Make a SELECT COUNT query from database ********************/ /**************** Make a SELECT COUNT query from database ********************/
/*****************************************************************************/ /*****************************************************************************/
unsigned long DB_GetNumRowsTable (const char *Table)
{
/***** Get total number of centres from database *****/
return DB_QueryCOUNT ("can not get number of rows in table",
"SELECT COUNT(*) FROM %s",
Table);
}
unsigned long DB_QueryCOUNT (const char *MsgError,const char *fmt,...) unsigned long DB_QueryCOUNT (const char *MsgError,const char *fmt,...)
{ {
va_list ap; va_list ap;
int NumBytesPrinted; int NumBytesPrinted;
char *Query = NULL; char *Query = NULL;
MYSQL_RES *mysql_res;
MYSQL_ROW row;
unsigned long NumRows;
va_start (ap,fmt); va_start (ap,fmt);
NumBytesPrinted = vasprintf (&Query,fmt,ap); NumBytesPrinted = vasprintf (&Query,fmt,ap);
@ -3151,17 +3162,8 @@ unsigned long DB_QueryCOUNT (const char *MsgError,const char *fmt,...)
// vasprintf will return -1 // vasprintf will return -1
Lay_NotEnoughMemoryExit (); Lay_NotEnoughMemoryExit ();
return DB_QueryCOUNT_old (&Query,MsgError);
}
unsigned long DB_QueryCOUNT_old (char **Query,const char *MsgError)
{
MYSQL_RES *mysql_res;
MYSQL_ROW row;
unsigned long NumRows;
/***** Make query "SELECT COUNT(*) FROM..." *****/ /***** Make query "SELECT COUNT(*) FROM..." *****/
DB_QuerySELECT_old (Query,&mysql_res,MsgError); DB_QuerySELECTusingQueryStr (&Query,&mysql_res,MsgError);
/***** Get number of rows *****/ /***** Get number of rows *****/
row = mysql_fetch_row (mysql_res); row = mysql_fetch_row (mysql_res);
@ -3183,6 +3185,7 @@ void DB_QueryINSERT (const char *MsgError,const char *fmt,...)
va_list ap; va_list ap;
int NumBytesPrinted; int NumBytesPrinted;
char *Query = NULL; char *Query = NULL;
int Result;
va_start (ap,fmt); va_start (ap,fmt);
NumBytesPrinted = vasprintf (&Query,fmt,ap); NumBytesPrinted = vasprintf (&Query,fmt,ap);
@ -3193,26 +3196,15 @@ void DB_QueryINSERT (const char *MsgError,const char *fmt,...)
// vasprintf will return -1 // vasprintf will return -1
Lay_NotEnoughMemoryExit (); Lay_NotEnoughMemoryExit ();
DB_QueryINSERT_old (&Query,MsgError);
}
/*****************************************************************************/
/******************** Make an INSERT query in database ***********************/
/*****************************************************************************/
void DB_QueryINSERT_old (char **Query,const char *MsgError)
{
int Result;
/***** Check that query string pointer /***** Check that query string pointer
does point to an allocated string *****/ does point to an allocated string *****/
if (*Query == NULL) if (Query == NULL)
Lay_ShowErrorAndExit ("Wrong query string."); Lay_ShowErrorAndExit ("Wrong query string.");
/***** Query database and free query string pointer *****/ /***** Query database and free query string pointer *****/
Result = mysql_query (&Gbl.mysql,*Query); // Returns 0 on success Result = mysql_query (&Gbl.mysql,Query); // Returns 0 on success
free ((void *) *Query); free ((void *) Query);
*Query = NULL; Query = NULL;
if (Result) if (Result)
DB_ExitOnMySQLError (MsgError); DB_ExitOnMySQLError (MsgError);
} }
@ -3295,6 +3287,7 @@ void DB_QueryUPDATE (const char *MsgError,const char *fmt,...)
va_list ap; va_list ap;
int NumBytesPrinted; int NumBytesPrinted;
char *Query = NULL; char *Query = NULL;
int Result;
va_start (ap,fmt); va_start (ap,fmt);
NumBytesPrinted = vasprintf (&Query,fmt,ap); NumBytesPrinted = vasprintf (&Query,fmt,ap);
@ -3305,28 +3298,22 @@ void DB_QueryUPDATE (const char *MsgError,const char *fmt,...)
// vasprintf will return -1 // vasprintf will return -1
Lay_NotEnoughMemoryExit (); Lay_NotEnoughMemoryExit ();
DB_QueryUPDATE_old (&Query,MsgError);
}
void DB_QueryUPDATE_old (char **Query,const char *MsgError)
{
int Result;
/***** Check that query string pointer /***** Check that query string pointer
does point to an allocated string *****/ does point to an allocated string *****/
if (*Query == NULL) if (Query == NULL)
Lay_ShowErrorAndExit ("Wrong query string."); Lay_ShowErrorAndExit ("Wrong query string.");
/***** Query database and free query string pointer *****/ /***** Query database and free query string pointer *****/
Result = mysql_query (&Gbl.mysql,*Query); // Returns 0 on success Result = mysql_query (&Gbl.mysql,Query); // Returns 0 on success
free ((void *) *Query); free ((void *) Query);
*Query = NULL; Query = NULL;
if (Result) if (Result)
DB_ExitOnMySQLError (MsgError); DB_ExitOnMySQLError (MsgError);
/***** Return number of rows updated *****/ /***** Return number of rows updated *****/
//return (unsigned long) mysql_affected_rows (&Gbl.mysql); //return (unsigned long) mysql_affected_rows (&Gbl.mysql);
} }
/*****************************************************************************/ /*****************************************************************************/
/******************** Make a DELETE query from database **********************/ /******************** Make a DELETE query from database **********************/

View File

@ -41,20 +41,18 @@ void DB_BuildQuery_old (char **Query,const char *fmt,...);
unsigned long DB_QuerySELECT (MYSQL_RES **mysql_res,const char *MsgError, unsigned long DB_QuerySELECT (MYSQL_RES **mysql_res,const char *MsgError,
const char *fmt,...); const char *fmt,...);
unsigned long DB_QuerySELECT_old (char **Query,MYSQL_RES **mysql_res,const char *MsgError); unsigned long DB_QuerySELECTusingQueryStr (char **Query,MYSQL_RES **mysql_res,const char *MsgError);
unsigned long DB_GetNumRowsTable (const char *Table);
unsigned long DB_QueryCOUNT (const char *MsgError,const char *fmt,...); unsigned long DB_QueryCOUNT (const char *MsgError,const char *fmt,...);
unsigned long DB_QueryCOUNT_old (char **Query,const char *MsgError);
void DB_QueryINSERT (const char *MsgError,const char *fmt,...); void DB_QueryINSERT (const char *MsgError,const char *fmt,...);
void DB_QueryINSERT_old (char **Query,const char *MsgError);
long DB_QueryINSERTandReturnCode (const char *MsgError,const char *fmt,...); long DB_QueryINSERTandReturnCode (const char *MsgError,const char *fmt,...);
void DB_QueryREPLACE (const char *MsgError,const char *fmt,...); void DB_QueryREPLACE (const char *MsgError,const char *fmt,...);
void DB_QueryUPDATE (const char *MsgError,const char *fmt,...); void DB_QueryUPDATE (const char *MsgError,const char *fmt,...);
void DB_QueryUPDATE_old (char **Query,const char *MsgError);
void DB_QueryDELETE (const char *MsgError,const char *fmt,...); void DB_QueryDELETE (const char *MsgError,const char *fmt,...);

View File

@ -2338,9 +2338,7 @@ void Deg_RemoveLogo (void)
unsigned Deg_GetNumDegsTotal (void) unsigned Deg_GetNumDegsTotal (void)
{ {
/***** Get total number of degrees from database *****/ /***** Get total number of degrees from database *****/
return return (unsigned) DB_GetNumRowsTable ("degrees");
(unsigned) DB_QueryCOUNT ("can not get the total number of degrees",
"SELECT COUNT(*) FROM degrees");
} }
/*****************************************************************************/ /*****************************************************************************/

View File

@ -1076,9 +1076,7 @@ static void Dpt_CreateDepartment (struct Department *Dpt)
unsigned Dpt_GetTotalNumberOfDepartments (void) unsigned Dpt_GetTotalNumberOfDepartments (void)
{ {
/***** Get number of departments from database *****/ /***** Get number of departments from database *****/
return return (unsigned) DB_GetNumRowsTable ("departments");
(unsigned) DB_QueryCOUNT ("can not get number of departments",
"SELECT COUNT(*) FROM departments");
} }
/*****************************************************************************/ /*****************************************************************************/

View File

@ -869,7 +869,8 @@ unsigned long For_GetNumPostsUsr (long UsrCod)
{ {
/***** Get number of posts from a user from database *****/ /***** Get number of posts from a user from database *****/
return DB_QueryCOUNT ("can not number of posts from a user", return DB_QueryCOUNT ("can not number of posts from a user",
"SELECT COUNT(*) FROM forum_post WHERE UsrCod=%ld", "SELECT COUNT(*) FROM forum_post"
" WHERE UsrCod=%ld",
UsrCod); UsrCod);
} }

View File

@ -2255,7 +2255,8 @@ static bool Gam_CheckIfGamIsAssociatedToGrps (long GamCod)
{ {
/***** Get if a game is associated to a group from database *****/ /***** Get if a game is associated to a group from database *****/
return (DB_QueryCOUNT ("can not check if a game is associated to groups", return (DB_QueryCOUNT ("can not check if a game is associated to groups",
"SELECT COUNT(*) FROM gam_grp WHERE GamCod=%ld", "SELECT COUNT(*) FROM gam_grp"
" WHERE GamCod=%ld",
GamCod) != 0); GamCod) != 0);
} }

View File

@ -126,7 +126,6 @@ void Gbl_InitializeGlobals (void)
Gbl.DB.DatabaseIsOpen = false; Gbl.DB.DatabaseIsOpen = false;
Gbl.DB.LockedTables = false; Gbl.DB.LockedTables = false;
Gbl.DB.QueryPtr = NULL;
Gbl.HiddenParamsInsertedIntoDB = false; Gbl.HiddenParamsInsertedIntoDB = false;

View File

@ -142,7 +142,6 @@ struct Globals
{ {
bool DatabaseIsOpen; bool DatabaseIsOpen;
bool LockedTables; bool LockedTables;
char *QueryPtr; // Pointer to query string, allocated dinamically
} DB; } DB;
bool HiddenParamsInsertedIntoDB; // If parameters are inserted in the database in this execution bool HiddenParamsInsertedIntoDB; // If parameters are inserted in the database in this execution

View File

@ -3174,7 +3174,8 @@ bool Grp_CheckIfGroupExists (long GrpCod)
{ {
/***** Get if a group exists from database *****/ /***** Get if a group exists from database *****/
return (DB_QueryCOUNT ("can not check if a group exists", return (DB_QueryCOUNT ("can not check if a group exists",
"SELECT COUNT(*) FROM crs_grp WHERE GrpCod=%ld", "SELECT COUNT(*) FROM crs_grp"
" WHERE GrpCod=%ld",
GrpCod) != 0); GrpCod) != 0);
} }

View File

@ -2442,9 +2442,7 @@ static void Ins_CreateInstitution (unsigned Status)
unsigned Ins_GetNumInssTotal (void) unsigned Ins_GetNumInssTotal (void)
{ {
/***** Get total number of degrees from database *****/ /***** Get total number of degrees from database *****/
return return (unsigned) DB_GetNumRowsTable ("institutions");
(unsigned) DB_QueryCOUNT ("can not get the total number of institutions",
"SELECT COUNT(*) FROM institutions");
} }
/*****************************************************************************/ /*****************************************************************************/

View File

@ -2077,9 +2077,11 @@ unsigned long Msg_GetNumMsgsSentByUsr (long UsrCod)
/***** Get the number of unique messages sent by any teacher from this course *****/ /***** Get the number of unique messages sent by any teacher from this course *****/
return DB_QueryCOUNT ("can not get the number of messages sent by a user", return DB_QueryCOUNT ("can not get the number of messages sent by a user",
"SELECT" "SELECT"
" (SELECT COUNT(*) FROM msg_snt WHERE UsrCod=%ld)" " (SELECT COUNT(*) FROM msg_snt"
" WHERE UsrCod=%ld)"
" +" " +"
" (SELECT COUNT(*) FROM msg_snt_deleted WHERE UsrCod=%ld)", " (SELECT COUNT(*) FROM msg_snt_deleted"
" WHERE UsrCod=%ld)",
UsrCod, UsrCod,
UsrCod); UsrCod);
} }
@ -2109,10 +2111,7 @@ unsigned Msg_GetNumMsgsSent (Sco_Scope_t Scope,Msg_Status_t MsgStatus)
switch (Scope) switch (Scope)
{ {
case Sco_SCOPE_SYS: case Sco_SCOPE_SYS:
NumMsgs = NumMsgs = (unsigned) DB_GetNumRowsTable (Table);
(unsigned) DB_QueryCOUNT ("can not get number of sent messages",
"SELECT COUNT(*) FROM %s",
Table);
break; break;
case Sco_SCOPE_CTY: case Sco_SCOPE_CTY:
NumMsgs = NumMsgs =
@ -2202,11 +2201,7 @@ unsigned Msg_GetNumMsgsReceived (Sco_Scope_t Scope,Msg_Status_t MsgStatus)
switch (Scope) switch (Scope)
{ {
case Sco_SCOPE_SYS: case Sco_SCOPE_SYS:
NumMsgs = NumMsgs = (unsigned) DB_GetNumRowsTable (Table);
(unsigned) DB_QueryCOUNT ("can not get number"
" of received messages",
"SELECT COUNT(*) FROM %s",
Table);
break; break;
case Sco_SCOPE_CTY: case Sco_SCOPE_CTY:
NumMsgs = NumMsgs =
@ -3450,9 +3445,11 @@ static void Msg_WriteMsgTo (long MsgCod)
NumRecipientsTotal = NumRecipientsTotal =
(unsigned) DB_QueryCOUNT ("can not get number of recipients", (unsigned) DB_QueryCOUNT ("can not get number of recipients",
"SELECT " "SELECT "
"(SELECT COUNT(*) FROM msg_rcv WHERE MsgCod=%ld)" "(SELECT COUNT(*) FROM msg_rcv"
" WHERE MsgCod=%ld)"
" + " " + "
"(SELECT COUNT(*) FROM msg_rcv_deleted WHERE MsgCod=%ld)", "(SELECT COUNT(*) FROM msg_rcv_deleted"
" WHERE MsgCod=%ld)",
MsgCod,MsgCod); MsgCod,MsgCod);
/***** Get recipients of a message from database *****/ /***** Get recipients of a message from database *****/

View File

@ -575,7 +575,8 @@ static bool Pwd_CheckIfPasswdIsUsrIDorName (const char *PlainPassword)
/***** Get if password is found in user's ID from database *****/ /***** Get if password is found in user's ID from database *****/
Found = (DB_QueryCOUNT ("can not check if a password matches a user's ID", Found = (DB_QueryCOUNT ("can not check if a password matches a user's ID",
"SELECT COUNT(*) FROM usr_IDs WHERE UsrID='%s'", "SELECT COUNT(*) FROM usr_IDs"
" WHERE UsrID='%s'",
PlainPassword) != 0); PlainPassword) != 0);
/***** Get if password is found in first name or surnames of anybody, from database *****/ /***** Get if password is found in first name or surnames of anybody, from database *****/
@ -583,7 +584,9 @@ static bool Pwd_CheckIfPasswdIsUsrIDorName (const char *PlainPassword)
Found = (DB_QueryCOUNT ("can not check if a password matches" Found = (DB_QueryCOUNT ("can not check if a password matches"
" a first name or a surname", " a first name or a surname",
"SELECT COUNT(*) FROM usr_data" "SELECT COUNT(*) FROM usr_data"
" WHERE FirstName='%s' OR Surname1='%s' OR Surname2='%s'", " WHERE FirstName='%s'"
" OR Surname1='%s'"
" OR Surname2='%s'",
PlainPassword,PlainPassword,PlainPassword) != 0); PlainPassword,PlainPassword,PlainPassword) != 0);
return Found; return Found;

View File

@ -697,7 +697,8 @@ static unsigned long Prf_GetNumUsrsWithFigure (const char *FieldName)
{ {
/***** Select number of rows with values already calculated *****/ /***** Select number of rows with values already calculated *****/
return DB_QueryCOUNT ("can not get number of users with a figure", return DB_QueryCOUNT ("can not get number of users with a figure",
"SELECT COUNT(*) FROM usr_figures WHERE %s>=0", "SELECT COUNT(*) FROM usr_figures"
" WHERE %s>=0",
FieldName); FieldName);
} }
@ -887,7 +888,8 @@ static void Prf_GetNumClicksAndStoreAsUsrFigure (long UsrCod)
/***** Get number of clicks from database *****/ /***** Get number of clicks from database *****/
UsrFigures.NumClicks = UsrFigures.NumClicks =
(long) DB_QueryCOUNT ("can not get number of clicks", (long) DB_QueryCOUNT ("can not get number of clicks",
"SELECT COUNT(*) FROM log_full WHERE UsrCod=%ld", "SELECT COUNT(*) FROM log_full"
" WHERE UsrCod=%ld",
UsrCod); UsrCod);
/***** Update number of clicks in user's figures *****/ /***** Update number of clicks in user's figures *****/
@ -1067,7 +1069,8 @@ void Prf_RemoveUsrFigures (long UsrCod)
static bool Prf_CheckIfUsrFiguresExists (long UsrCod) static bool Prf_CheckIfUsrFiguresExists (long UsrCod)
{ {
return (DB_QueryCOUNT ("can not get user's first click", return (DB_QueryCOUNT ("can not get user's first click",
"SELECT COUNT(*) FROM usr_figures WHERE UsrCod=%ld", "SELECT COUNT(*) FROM usr_figures"
" WHERE UsrCod=%ld",
UsrCod) != 0); UsrCod) != 0);
} }

View File

@ -63,9 +63,7 @@ static bool Ses_CheckIfHiddenParIsAlreadyInDB (Act_Action_t NextAction,
void Ses_GetNumSessions (void) void Ses_GetNumSessions (void)
{ {
/***** Get the number of open sessions from database *****/ /***** Get the number of open sessions from database *****/
Gbl.Session.NumSessions = Gbl.Session.NumSessions = (unsigned) DB_GetNumRowsTable ("sessions");
(unsigned) DB_QueryCOUNT ("can not get the number of open sessions",
"SELECT COUNT(*) FROM sessions");
Gbl.Usrs.Connected.TimeToRefreshInMs = (unsigned long) (Gbl.Session.NumSessions/Cfg_TIMES_PER_SECOND_REFRESH_CONNECTED) * 1000UL; Gbl.Usrs.Connected.TimeToRefreshInMs = (unsigned long) (Gbl.Session.NumSessions/Cfg_TIMES_PER_SECOND_REFRESH_CONNECTED) * 1000UL;
if (Gbl.Usrs.Connected.TimeToRefreshInMs < Con_MIN_TIME_TO_REFRESH_CONNECTED_IN_MS) if (Gbl.Usrs.Connected.TimeToRefreshInMs < Con_MIN_TIME_TO_REFRESH_CONNECTED_IN_MS)

View File

@ -925,7 +925,8 @@ static void Soc_ShowTimeline (char **Query,
bool ItsMe = Usr_ItsMe (Gbl.Usrs.Other.UsrDat.UsrCod); bool ItsMe = Usr_ItsMe (Gbl.Usrs.Other.UsrDat.UsrCod);
/***** Get publishings from database *****/ /***** Get publishings from database *****/
NumPubsGot = DB_QuerySELECT_old (Query,&mysql_res,"can not get timeline"); NumPubsGot = DB_QuerySELECTusingQueryStr (Query,&mysql_res,
"can not get timeline");
/***** Start box *****/ /***** Start box *****/
Box_StartBox (Soc_WIDTH_TIMELINE,Title,Soc_PutIconsTimeline, Box_StartBox (Soc_WIDTH_TIMELINE,Title,Soc_PutIconsTimeline,
@ -1124,7 +1125,8 @@ static void Soc_InsertNewPubsInTimeline (char **Query)
struct SocialNote SocNot; struct SocialNote SocNot;
/***** Get new publishings timeline from database *****/ /***** Get new publishings timeline from database *****/
NumPubsGot = DB_QuerySELECT_old (Query,&mysql_res,"can not get timeline"); NumPubsGot = DB_QuerySELECTusingQueryStr (Query,&mysql_res,
"can not get timeline");
/***** List new publishings timeline *****/ /***** List new publishings timeline *****/
for (NumPub = 0; for (NumPub = 0;
@ -1164,7 +1166,8 @@ static void Soc_ShowOldPubsInTimeline (char **Query)
struct SocialNote SocNot; struct SocialNote SocNot;
/***** Get old publishings timeline from database *****/ /***** Get old publishings timeline from database *****/
NumPubsGot = DB_QuerySELECT_old (Query,&mysql_res,"can not get timeline"); NumPubsGot = DB_QuerySELECTusingQueryStr (Query,&mysql_res,
"can not get timeline");
/***** List old publishings in timeline *****/ /***** List old publishings in timeline *****/
for (NumPub = 0; for (NumPub = 0;

View File

@ -287,8 +287,6 @@ void Sta_GetRemoteAddr (void)
void Sta_LogAccess (const char *Comments) void Sta_LogAccess (const char *Comments)
{ {
size_t MaxLength;
char *Query = NULL;
long LogCod; long LogCod;
long ActCod = Act_GetActCod (Gbl.Action.Act); long ActCod = Act_GetActCod (Gbl.Action.Act);
Rol_Role_t RoleToStore = (Gbl.Action.Act == ActLogOut) ? Gbl.Usrs.Me.Role.LoggedBeforeCloseSession : Rol_Role_t RoleToStore = (Gbl.Action.Act == ActLogOut) ? Gbl.Usrs.Me.Role.LoggedBeforeCloseSession :
@ -336,47 +334,23 @@ void Sta_LogAccess (const char *Comments)
Gbl.TimeSendInMicroseconds, Gbl.TimeSendInMicroseconds,
Gbl.IP); Gbl.IP);
/* Log comments */
if (Comments) if (Comments)
{ DB_QueryINSERT ("can not log access (comments)",
/* Allocate space for query */ "INSERT INTO log_comments"
MaxLength = 512 + strlen (Comments); " (LogCod,Comments)"
if ((Query = (char *) malloc (MaxLength + 1)) == NULL) " VALUES"
Lay_NotEnoughMemoryExit (); " (%ld,'%s')",
Comments);
/* Log comments */
snprintf (Query,MaxLength,
"INSERT INTO log_comments"
" (LogCod,Comments)"
" VALUES"
" (%ld,'",
LogCod);
Str_AddStrToQuery (Query,Comments,MaxLength);
Str_Concat (Query,"')",
MaxLength);
DB_QueryINSERT_old (&Query,"can not log access (comments)");
}
/* Log search string */
if (Gbl.Search.LogSearch && Gbl.Search.Str[0]) if (Gbl.Search.LogSearch && Gbl.Search.Str[0])
{ DB_QueryINSERT ("can not log access (search)",
/* Allocate space for query */ "INSERT INTO log_search"
MaxLength = 512 + strlen (Gbl.Search.Str); " (LogCod,SearchStr)"
if ((Query = (char *) malloc (MaxLength + 1)) == NULL) " VALUES"
Lay_NotEnoughMemoryExit (); " (%ld,'%s')",
Gbl.Search.Str);
/* Log search string */
snprintf (Query,MaxLength,
"INSERT INTO log_search"
" (LogCod,SearchStr)"
" VALUES"
" (%ld,'",
LogCod);
Str_AddStrToQuery (Query,Gbl.Search.Str,MaxLength);
Str_Concat (Query,"')",
MaxLength);
DB_QueryINSERT_old (&Query,"can not log access (search)");
}
if (Gbl.WebService.IsWebService) if (Gbl.WebService.IsWebService)
/* Log web service plugin and function */ /* Log web service plugin and function */
@ -1484,7 +1458,8 @@ static void Sta_ShowHits (Sta_GlobalOrCourseAccesses_t GlobalOrCourse)
Ale_ShowAlert (Ale_INFO,Query); Ale_ShowAlert (Ale_INFO,Query);
*/ */
/***** Make the query *****/ /***** Make the query *****/
NumRows = DB_QuerySELECT_old (&Query,&mysql_res,"can not get clicks"); NumRows = DB_QuerySELECTusingQueryStr (&Query,&mysql_res,
"can not get clicks");
/***** Count the number of rows in result *****/ /***** Count the number of rows in result *****/
if (NumRows == 0) if (NumRows == 0)

View File

@ -2327,7 +2327,8 @@ static bool Svy_CheckIfSvyIsAssociatedToGrps (long SvyCod)
{ {
/***** Get if a survey is associated to a group from database *****/ /***** Get if a survey is associated to a group from database *****/
return (DB_QueryCOUNT ("can not check if a survey is associated to groups", return (DB_QueryCOUNT ("can not check if a survey is associated to groups",
"SELECT COUNT(*) FROM svy_grp WHERE SvyCod=%ld", "SELECT COUNT(*) FROM svy_grp"
" WHERE SvyCod=%ld",
SvyCod) != 0); SvyCod) != 0);
} }

View File

@ -1672,7 +1672,8 @@ static bool Tst_CheckIfCurrentCrsHasTestTags (void)
{ {
/***** Get available tags from database *****/ /***** Get available tags from database *****/
return (DB_QueryCOUNT ("can not check if course has tags", return (DB_QueryCOUNT ("can not check if course has tags",
"SELECT COUNT(*) FROM tst_tags WHERE CrsCod=%ld", "SELECT COUNT(*) FROM tst_tags"
" WHERE CrsCod=%ld",
Gbl.CurrentCrs.Crs.CrsCod) != 0); Gbl.CurrentCrs.Crs.CrsCod) != 0);
} }
@ -2650,7 +2651,8 @@ static unsigned long Tst_GetQuestions (MYSQL_RES **mysql_res)
} }
/* Make the query */ /* Make the query */
NumRows = DB_QuerySELECT_old (&Query,mysql_res,"can not get questions"); NumRows = DB_QuerySELECTusingQueryStr (&Query,mysql_res,
"can not get questions");
if (NumRows == 0) if (NumRows == 0)
Ale_ShowAlert (Ale_INFO,Txt_No_questions_found_matching_your_search_criteria); Ale_ShowAlert (Ale_INFO,Txt_No_questions_found_matching_your_search_criteria);
@ -2786,7 +2788,8 @@ static unsigned long Tst_GetQuestionsForTest (MYSQL_RES **mysql_res)
Lay_ShowAlert (Lay_INFO,Query); Lay_ShowAlert (Lay_INFO,Query);
*/ */
/* Make the query */ /* Make the query */
return DB_QuerySELECT_old (&Query,mysql_res,"can not get questions"); return DB_QuerySELECTusingQueryStr (&Query,mysql_res,
"can not get questions");
} }
/*****************************************************************************/ /*****************************************************************************/

View File

@ -965,7 +965,8 @@ unsigned Usr_GetNumCrssOfUsr (long UsrCod)
/***** Get the number of courses of a user from database ******/ /***** Get the number of courses of a user from database ******/
return return
(unsigned) DB_QueryCOUNT ("can not get the number of courses of a user", (unsigned) DB_QueryCOUNT ("can not get the number of courses of a user",
"SELECT COUNT(*) FROM crs_usr WHERE UsrCod=%ld", "SELECT COUNT(*) FROM crs_usr"
" WHERE UsrCod=%ld",
UsrCod); UsrCod);
} }
@ -5242,7 +5243,9 @@ static void Usr_GetListUsrsFromQuery (char **Query,Rol_Role_t Role,Sco_Scope_t S
} }
/***** Query database *****/ /***** Query database *****/
if ((Gbl.Usrs.LstUsrs[Role].NumUsrs = (unsigned) DB_QuerySELECT_old (Query,&mysql_res,"can not get list of users"))) if ((Gbl.Usrs.LstUsrs[Role].NumUsrs =
(unsigned) DB_QuerySELECTusingQueryStr (Query,&mysql_res,
"can not get list of users")))
{ {
if (Gbl.Usrs.LstUsrs[Role].NumUsrs > Cfg_MAX_USRS_IN_LIST) if (Gbl.Usrs.LstUsrs[Role].NumUsrs > Cfg_MAX_USRS_IN_LIST)
{ {
@ -9083,7 +9086,8 @@ float Usr_GetNumUsrsPerCrs (Rol_Role_t Role)
bool Usr_CheckIfUsrBanned (long UsrCod) bool Usr_CheckIfUsrBanned (long UsrCod)
{ {
return (DB_QueryCOUNT ("can not check if user is banned", return (DB_QueryCOUNT ("can not check if user is banned",
"SELECT COUNT(*) FROM usr_banned WHERE UsrCod=%ld", "SELECT COUNT(*) FROM usr_banned"
" WHERE UsrCod=%ld",
UsrCod) != 0); UsrCod) != 0);
} }
@ -9094,7 +9098,8 @@ bool Usr_CheckIfUsrBanned (long UsrCod)
void Usr_RemoveUsrFromUsrBanned (long UsrCod) void Usr_RemoveUsrFromUsrBanned (long UsrCod)
{ {
DB_QueryDELETE ("can not remove user from users banned", DB_QueryDELETE ("can not remove user from users banned",
"DELETE FROM usr_banned WHERE UsrCod=%ld", "DELETE FROM usr_banned"
" WHERE UsrCod=%ld",
UsrCod); UsrCod);
} }

View File

@ -358,7 +358,8 @@ static int Svc_CheckIdSession (const char *IdSession)
/***** Query if session identifier already exists in database *****/ /***** Query if session identifier already exists in database *****/
if (DB_QueryCOUNT ("can not get session data", if (DB_QueryCOUNT ("can not get session data",
"SELECT COUNT(*) FROM sessions WHERE SessionId='%s'", "SELECT COUNT(*) FROM sessions"
" WHERE SessionId='%s'",
IdSession) != 1) IdSession) != 1)
return soap_receiver_fault (Gbl.soap, return soap_receiver_fault (Gbl.soap,
"Bad session identifier", "Bad session identifier",
@ -413,7 +414,8 @@ static int Svc_CheckCourseAndGroupCodes (long CrsCod,long GrpCod)
/***** Query if course code already exists in database *****/ /***** Query if course code already exists in database *****/
if (DB_QueryCOUNT ("can not get course", if (DB_QueryCOUNT ("can not get course",
"SELECT COUNT(*) FROM courses WHERE CrsCod=%ld", "SELECT COUNT(*) FROM courses"
" WHERE CrsCod=%ld",
CrsCod) != 1) CrsCod) != 1)
return soap_sender_fault (Gbl.soap, return soap_sender_fault (Gbl.soap,
"Bad course code", "Bad course code",
@ -456,7 +458,8 @@ static int Svc_GenerateNewWSKey (long UsrCod,
/***** Check that key does not exist in database *****/ /***** Check that key does not exist in database *****/
if (DB_QueryCOUNT ("can not get existence of key", if (DB_QueryCOUNT ("can not get existence of key",
"SELECT COUNT(*) FROM ws_keys WHERE WSKey='%s'", "SELECT COUNT(*) FROM ws_keys"
" WHERE WSKey='%s'",
WSKey)) WSKey))
return soap_receiver_fault (Gbl.soap, return soap_receiver_fault (Gbl.soap,
"Error when generating key", "Error when generating key",
@ -2734,8 +2737,8 @@ int swad__sendAttendanceUsers (struct soap *soap,
char LongStr[1 + 10 + 1]; char LongStr[1 + 10 + 1];
struct UsrData UsrDat; struct UsrData UsrDat;
unsigned NumCodsInList; unsigned NumCodsInList;
char *Query = NULL; char *SubQueryAllUsrs = NULL;
char SubQuery[256]; char SubQueryOneUsr[1 + 1 + 10 + 1];
size_t Length = 0; // Initialized to avoid warning size_t Length = 0; // Initialized to avoid warning
/***** Initializations *****/ /***** Initializations *****/
@ -2786,15 +2789,13 @@ int swad__sendAttendanceUsers (struct soap *soap,
/* Find next string in text until comma (leading and trailing spaces are removed) */ /* Find next string in text until comma (leading and trailing spaces are removed) */
Str_GetNextStringUntilComma (&Ptr,LongStr,1 + 10); Str_GetNextStringUntilComma (&Ptr,LongStr,1 + 10);
/* Start query used to mark not present users as absent */ /* Allocate subquery used to mark not present users as absent */
Length = 256 + NumCodsInList * (1 + 1 + 10 + 1) - 1; Length = 256 + NumCodsInList * (1 + 1 + 10 + 1) - 1;
if ((Query = (char *) malloc (Length + 1)) == NULL) if ((SubQueryAllUsrs = (char *) malloc (Length + 1)) == NULL)
return soap_receiver_fault (Gbl.soap, return soap_receiver_fault (Gbl.soap,
"Not enough memory", "Not enough memory",
"Not enough memory to store list of users"); "Not enough memory to store list of users");
sprintf (Query,"UPDATE att_usr SET Present='N'" SubQueryAllUsrs[0] = '\0';
" WHERE AttCod=%ld",
Att.AttCod);
} }
for (Ptr = users; for (Ptr = users;
@ -2815,11 +2816,17 @@ int swad__sendAttendanceUsers (struct soap *soap,
/* Add this user to query used to mark not present users as absent */ /* Add this user to query used to mark not present users as absent */
if (setOthersAsAbsent) if (setOthersAsAbsent)
{ {
sprintf (SubQuery,sendAttendanceUsersOut->numUsers ? ",%ld" : if (sendAttendanceUsersOut->numUsers)
" AND UsrCod NOT IN (%ld", {
UsrDat.UsrCod); snprintf (SubQueryOneUsr,sizeof (SubQueryOneUsr),
Str_Concat (Query,SubQuery, ",%ld",UsrDat.UsrCod);
Length); Str_Concat (SubQueryAllUsrs,SubQueryOneUsr,
Length);
}
else
snprintf (SubQueryAllUsrs,Length,
" AND UsrCod NOT IN (%ld",
UsrDat.UsrCod);
} }
sendAttendanceUsersOut->numUsers++; sendAttendanceUsersOut->numUsers++;
@ -2830,10 +2837,13 @@ int swad__sendAttendanceUsers (struct soap *soap,
{ {
/* Mark not present users as absent in table of users */ /* Mark not present users as absent in table of users */
if (sendAttendanceUsersOut->numUsers) if (sendAttendanceUsersOut->numUsers)
Str_Concat (Query,")", Str_Concat (SubQueryAllUsrs,")",
Length); Length);
DB_QueryUPDATE_old (&Query,"can not set other users as absent"); DB_QueryUPDATE ("can not set other users as absent",
"UPDATE att_usr SET Present='N'"
" WHERE AttCod=%ld%s",
Att.AttCod,SubQueryAllUsrs);
/* Clean table att_usr */ /* Clean table att_usr */
Att_RemoveUsrsAbsentWithoutCommentsFromAttEvent (Att.AttCod); Att_RemoveUsrsAbsentWithoutCommentsFromAttEvent (Att.AttCod);
@ -3362,7 +3372,8 @@ int swad__sendMessage (struct soap *soap,
if (ReplyUsrCod > 0 || ThereAreNicknames) // There are a recipient to reply or nicknames in "to" if (ReplyUsrCod > 0 || ThereAreNicknames) // There are a recipient to reply or nicknames in "to"
{ {
/***** Get users *****/ /***** Get users *****/
NumRows = DB_QuerySELECT_old (&Query,&mysql_res,"can not get users"); NumRows = DB_QuerySELECTusingQueryStr (&Query,&mysql_res,
"can not get users");
sendMessageOut->numUsers = (int) NumRows; sendMessageOut->numUsers = (int) NumRows;
sendMessageOut->usersArray.__size = (int) NumRows; sendMessageOut->usersArray.__size = (int) NumRows;