From e7ba3b873ad5ead454bd5e309168fccc45aca42c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Antonio=20Ca=C3=B1as=20Vargas?= Date: Sun, 4 Nov 2018 20:51:38 +0100 Subject: [PATCH] Version 18.13.2 --- swad_ID.c | 5 +++- swad_attendance.c | 45 +++++++++++++++++---------------- swad_centre.c | 3 +-- swad_changelog.h | 4 ++- swad_country.c | 4 +-- swad_course.c | 3 +-- swad_database.c | 63 ++++++++++++++++++---------------------------- swad_database.h | 6 ++--- swad_degree.c | 4 +-- swad_department.c | 4 +-- swad_forum.c | 3 ++- swad_game.c | 3 ++- swad_global.c | 1 - swad_global.h | 1 - swad_group.c | 3 ++- swad_institution.c | 4 +-- swad_message.c | 23 ++++++++--------- swad_password.c | 7 ++++-- swad_profile.c | 9 ++++--- swad_session.c | 4 +-- swad_social.c | 9 ++++--- swad_statistic.c | 57 ++++++++++++----------------------------- swad_survey.c | 3 ++- swad_test.c | 9 ++++--- swad_user.c | 13 +++++++--- swad_web_service.c | 47 +++++++++++++++++++++------------- 26 files changed, 160 insertions(+), 177 deletions(-) diff --git a/swad_ID.c b/swad_ID.c index 9a1eee118..2e87a30e2 100644 --- a/swad_ID.c +++ b/swad_ID.c @@ -234,7 +234,10 @@ unsigned ID_GetListUsrCodsFromUsrID (struct UsrData *UsrDat, Str_Concat (Query," AND Confirmed='Y'", 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) { /***** Allocate space for the list of users' codes *****/ diff --git a/swad_attendance.c b/swad_attendance.c index f4a7253c5..de53b946a 100644 --- a/swad_attendance.c +++ b/swad_attendance.c @@ -2449,41 +2449,44 @@ static void Att_GetNumStdsTotalWhoAreInAttEvent (struct AttendanceEvent *Att) static unsigned Att_GetNumStdsFromAListWhoAreInAttEvent (long AttCod,long LstSelectedUsrCods[],unsigned NumStdsInList) { - char *Query = NULL; - char SubQuery[1 + 1 + 10 + 1]; + char *SubQueryAllUsrs = NULL; + char SubQueryOneUsr[1 + 1 + 10 + 1]; unsigned NumStd; unsigned NumStdsInAttEvent = 0; size_t MaxLength; if (NumStdsInList) { - /***** Allocate space for query *****/ + /***** Allocate space for subquery *****/ MaxLength = 256 + NumStdsInList * (1 + 1 + 10); - if ((Query = (char *) malloc (MaxLength + 1)) == NULL) + if ((SubQueryAllUsrs = (char *) malloc (MaxLength + 1)) == NULL) Lay_NotEnoughMemoryExit (); + SubQueryAllUsrs[0] = '\0'; /***** 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; NumStd < NumStdsInList; NumStd++) - { - snprintf (SubQuery,sizeof (SubQuery), - NumStd ? ",%ld" : - "%ld", - LstSelectedUsrCods[NumStd]); - Str_Concat (Query,SubQuery, - MaxLength); - } - Str_Concat (Query,") AND Present='Y'", - MaxLength); + if (NumStd) + { + snprintf (SubQueryOneUsr,sizeof (SubQueryOneUsr), + ",%ld", + LstSelectedUsrCods[NumStd]); + Str_Concat (SubQueryAllUsrs,SubQueryOneUsr, + MaxLength); + } + else + snprintf (SubQueryAllUsrs,sizeof (SubQueryOneUsr), + "%ld", + LstSelectedUsrCods[NumStd]); - - NumStdsInAttEvent = (unsigned) DB_QueryCOUNT_old (&Query,"can not get number of students from a list who are registered in an event"); + NumStdsInAttEvent = + (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; } diff --git a/swad_centre.c b/swad_centre.c index 4c5db838e..727b73cb3 100644 --- a/swad_centre.c +++ b/swad_centre.c @@ -2850,8 +2850,7 @@ static void Ctr_CreateCentre (unsigned Status) unsigned Ctr_GetNumCtrsTotal (void) { /***** Get total number of centres from database *****/ - return (unsigned) DB_QueryCOUNT ("can not get total number of centres", - "SELECT COUNT(*) FROM centres"); + return (unsigned) DB_GetNumRowsTable ("centres"); } /*****************************************************************************/ diff --git a/swad_changelog.h b/swad_changelog.h index 46ed75689..090f5b725 100644 --- a/swad_changelog.h +++ b/swad_changelog.h @@ -355,10 +355,12 @@ En OpenSWAD: 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 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.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. diff --git a/swad_country.c b/swad_country.c index f3dcaf455..daedf6fc7 100644 --- a/swad_country.c +++ b/swad_country.c @@ -2284,9 +2284,7 @@ static void Cty_CreateCountry (struct Country *Cty) unsigned Cty_GetNumCtysTotal (void) { /***** Get total number of countries from database *****/ - return - (unsigned) DB_QueryCOUNT ("can not get the total number of countries", - "SELECT COUNT(*) FROM countries"); + return (unsigned) DB_GetNumRowsTable ("countries"); } /*****************************************************************************/ diff --git a/swad_course.c b/swad_course.c index eb1760ccc..5bffa4e30 100644 --- a/swad_course.c +++ b/swad_course.c @@ -794,8 +794,7 @@ static void Crs_WriteListMyCoursesToSelectOne (void) unsigned Crs_GetNumCrssTotal (void) { /***** Get total number of courses from database *****/ - return (unsigned) DB_QueryCOUNT ("can not get the total number of courses", - "SELECT COUNT(*) FROM courses"); + return (unsigned) DB_GetNumRowsTable ("courses"); } /*****************************************************************************/ diff --git a/swad_database.c b/swad_database.c index 2e06f6fb9..7f9a3ff78 100644 --- a/swad_database.c +++ b/swad_database.c @@ -3105,10 +3105,10 @@ unsigned long DB_QuerySELECT (MYSQL_RES **mysql_res,const char *MsgError, // vasprintf will return -1 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; @@ -3136,11 +3136,22 @@ unsigned long DB_QuerySELECT_old (char **Query,MYSQL_RES **mysql_res,const char /**************** 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,...) { va_list ap; int NumBytesPrinted; char *Query = NULL; + MYSQL_RES *mysql_res; + MYSQL_ROW row; + unsigned long NumRows; va_start (ap,fmt); NumBytesPrinted = vasprintf (&Query,fmt,ap); @@ -3151,17 +3162,8 @@ unsigned long DB_QueryCOUNT (const char *MsgError,const char *fmt,...) // vasprintf will return -1 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..." *****/ - DB_QuerySELECT_old (Query,&mysql_res,MsgError); + DB_QuerySELECTusingQueryStr (&Query,&mysql_res,MsgError); /***** Get number of rows *****/ row = mysql_fetch_row (mysql_res); @@ -3183,6 +3185,7 @@ void DB_QueryINSERT (const char *MsgError,const char *fmt,...) va_list ap; int NumBytesPrinted; char *Query = NULL; + int Result; va_start (ap,fmt); NumBytesPrinted = vasprintf (&Query,fmt,ap); @@ -3193,26 +3196,15 @@ void DB_QueryINSERT (const char *MsgError,const char *fmt,...) // vasprintf will return -1 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 does point to an allocated string *****/ - if (*Query == NULL) + if (Query == NULL) Lay_ShowErrorAndExit ("Wrong query string."); /***** Query database and free query string pointer *****/ - Result = mysql_query (&Gbl.mysql,*Query); // Returns 0 on success - free ((void *) *Query); - *Query = NULL; + Result = mysql_query (&Gbl.mysql,Query); // Returns 0 on success + free ((void *) Query); + Query = NULL; if (Result) DB_ExitOnMySQLError (MsgError); } @@ -3295,6 +3287,7 @@ void DB_QueryUPDATE (const char *MsgError,const char *fmt,...) va_list ap; int NumBytesPrinted; char *Query = NULL; + int Result; va_start (ap,fmt); NumBytesPrinted = vasprintf (&Query,fmt,ap); @@ -3305,28 +3298,22 @@ void DB_QueryUPDATE (const char *MsgError,const char *fmt,...) // vasprintf will return -1 Lay_NotEnoughMemoryExit (); - DB_QueryUPDATE_old (&Query,MsgError); - } - -void DB_QueryUPDATE_old (char **Query,const char *MsgError) - { - int Result; /***** Check that query string pointer does point to an allocated string *****/ - if (*Query == NULL) + if (Query == NULL) Lay_ShowErrorAndExit ("Wrong query string."); /***** Query database and free query string pointer *****/ - Result = mysql_query (&Gbl.mysql,*Query); // Returns 0 on success - free ((void *) *Query); - *Query = NULL; + Result = mysql_query (&Gbl.mysql,Query); // Returns 0 on success + free ((void *) Query); + Query = NULL; if (Result) DB_ExitOnMySQLError (MsgError); /***** Return number of rows updated *****/ //return (unsigned long) mysql_affected_rows (&Gbl.mysql); - } + } /*****************************************************************************/ /******************** Make a DELETE query from database **********************/ diff --git a/swad_database.h b/swad_database.h index 31704c731..14bbebece 100644 --- a/swad_database.h +++ b/swad_database.h @@ -41,20 +41,18 @@ void DB_BuildQuery_old (char **Query,const char *fmt,...); unsigned long DB_QuerySELECT (MYSQL_RES **mysql_res,const char *MsgError, 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_old (char **Query,const char *MsgError); 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,...); void DB_QueryREPLACE (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,...); diff --git a/swad_degree.c b/swad_degree.c index 5d14d5fc6..7fdd56136 100644 --- a/swad_degree.c +++ b/swad_degree.c @@ -2338,9 +2338,7 @@ void Deg_RemoveLogo (void) unsigned Deg_GetNumDegsTotal (void) { /***** Get total number of degrees from database *****/ - return - (unsigned) DB_QueryCOUNT ("can not get the total number of degrees", - "SELECT COUNT(*) FROM degrees"); + return (unsigned) DB_GetNumRowsTable ("degrees"); } /*****************************************************************************/ diff --git a/swad_department.c b/swad_department.c index 4d2a4580a..7277a571f 100644 --- a/swad_department.c +++ b/swad_department.c @@ -1076,9 +1076,7 @@ static void Dpt_CreateDepartment (struct Department *Dpt) unsigned Dpt_GetTotalNumberOfDepartments (void) { /***** Get number of departments from database *****/ - return - (unsigned) DB_QueryCOUNT ("can not get number of departments", - "SELECT COUNT(*) FROM departments"); + return (unsigned) DB_GetNumRowsTable ("departments"); } /*****************************************************************************/ diff --git a/swad_forum.c b/swad_forum.c index ca6f1bd6a..a7d76e6cd 100644 --- a/swad_forum.c +++ b/swad_forum.c @@ -869,7 +869,8 @@ unsigned long For_GetNumPostsUsr (long UsrCod) { /***** Get number of posts from a user from database *****/ 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); } diff --git a/swad_game.c b/swad_game.c index bbaec90ad..c0dc1485f 100644 --- a/swad_game.c +++ b/swad_game.c @@ -2255,7 +2255,8 @@ static bool Gam_CheckIfGamIsAssociatedToGrps (long GamCod) { /***** Get if a game is associated to a group from database *****/ 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); } diff --git a/swad_global.c b/swad_global.c index 3d1b854c9..763067df7 100644 --- a/swad_global.c +++ b/swad_global.c @@ -126,7 +126,6 @@ void Gbl_InitializeGlobals (void) Gbl.DB.DatabaseIsOpen = false; Gbl.DB.LockedTables = false; - Gbl.DB.QueryPtr = NULL; Gbl.HiddenParamsInsertedIntoDB = false; diff --git a/swad_global.h b/swad_global.h index 48058991f..5b5c5fc8b 100644 --- a/swad_global.h +++ b/swad_global.h @@ -142,7 +142,6 @@ struct Globals { bool DatabaseIsOpen; bool LockedTables; - char *QueryPtr; // Pointer to query string, allocated dinamically } DB; bool HiddenParamsInsertedIntoDB; // If parameters are inserted in the database in this execution diff --git a/swad_group.c b/swad_group.c index 3212daa29..a8aa1916b 100644 --- a/swad_group.c +++ b/swad_group.c @@ -3174,7 +3174,8 @@ bool Grp_CheckIfGroupExists (long GrpCod) { /***** Get if a group exists from database *****/ 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); } diff --git a/swad_institution.c b/swad_institution.c index 4e0ec8945..e95981637 100644 --- a/swad_institution.c +++ b/swad_institution.c @@ -2442,9 +2442,7 @@ static void Ins_CreateInstitution (unsigned Status) unsigned Ins_GetNumInssTotal (void) { /***** Get total number of degrees from database *****/ - return - (unsigned) DB_QueryCOUNT ("can not get the total number of institutions", - "SELECT COUNT(*) FROM institutions"); + return (unsigned) DB_GetNumRowsTable ("institutions"); } /*****************************************************************************/ diff --git a/swad_message.c b/swad_message.c index 530e01ab5..48283f5f2 100644 --- a/swad_message.c +++ b/swad_message.c @@ -2077,9 +2077,11 @@ unsigned long Msg_GetNumMsgsSentByUsr (long UsrCod) /***** 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", "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); } @@ -2109,10 +2111,7 @@ unsigned Msg_GetNumMsgsSent (Sco_Scope_t Scope,Msg_Status_t MsgStatus) switch (Scope) { case Sco_SCOPE_SYS: - NumMsgs = - (unsigned) DB_QueryCOUNT ("can not get number of sent messages", - "SELECT COUNT(*) FROM %s", - Table); + NumMsgs = (unsigned) DB_GetNumRowsTable (Table); break; case Sco_SCOPE_CTY: NumMsgs = @@ -2202,11 +2201,7 @@ unsigned Msg_GetNumMsgsReceived (Sco_Scope_t Scope,Msg_Status_t MsgStatus) switch (Scope) { case Sco_SCOPE_SYS: - NumMsgs = - (unsigned) DB_QueryCOUNT ("can not get number" - " of received messages", - "SELECT COUNT(*) FROM %s", - Table); + NumMsgs = (unsigned) DB_GetNumRowsTable (Table); break; case Sco_SCOPE_CTY: NumMsgs = @@ -3450,9 +3445,11 @@ static void Msg_WriteMsgTo (long MsgCod) NumRecipientsTotal = (unsigned) DB_QueryCOUNT ("can not get number of recipients", "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); /***** Get recipients of a message from database *****/ diff --git a/swad_password.c b/swad_password.c index 42e191d4f..230df4437 100644 --- a/swad_password.c +++ b/swad_password.c @@ -575,7 +575,8 @@ static bool Pwd_CheckIfPasswdIsUsrIDorName (const char *PlainPassword) /***** 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", - "SELECT COUNT(*) FROM usr_IDs WHERE UsrID='%s'", + "SELECT COUNT(*) FROM usr_IDs" + " WHERE UsrID='%s'", PlainPassword) != 0); /***** 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" " a first name or a surname", "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); return Found; diff --git a/swad_profile.c b/swad_profile.c index 0c3c838e3..7580fbe21 100644 --- a/swad_profile.c +++ b/swad_profile.c @@ -697,7 +697,8 @@ static unsigned long Prf_GetNumUsrsWithFigure (const char *FieldName) { /***** Select number of rows with values already calculated *****/ 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); } @@ -887,7 +888,8 @@ static void Prf_GetNumClicksAndStoreAsUsrFigure (long UsrCod) /***** Get number of clicks from database *****/ UsrFigures.NumClicks = (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); /***** Update number of clicks in user's figures *****/ @@ -1067,7 +1069,8 @@ void Prf_RemoveUsrFigures (long UsrCod) static bool Prf_CheckIfUsrFiguresExists (long UsrCod) { 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); } diff --git a/swad_session.c b/swad_session.c index 0b733d4d3..8acd5f44b 100644 --- a/swad_session.c +++ b/swad_session.c @@ -63,9 +63,7 @@ static bool Ses_CheckIfHiddenParIsAlreadyInDB (Act_Action_t NextAction, void Ses_GetNumSessions (void) { /***** Get the number of open sessions from database *****/ - Gbl.Session.NumSessions = - (unsigned) DB_QueryCOUNT ("can not get the number of open sessions", - "SELECT COUNT(*) FROM sessions"); + Gbl.Session.NumSessions = (unsigned) DB_GetNumRowsTable ("sessions"); 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) diff --git a/swad_social.c b/swad_social.c index 1869faca8..4bf9f3ea2 100644 --- a/swad_social.c +++ b/swad_social.c @@ -925,7 +925,8 @@ static void Soc_ShowTimeline (char **Query, bool ItsMe = Usr_ItsMe (Gbl.Usrs.Other.UsrDat.UsrCod); /***** 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 *****/ Box_StartBox (Soc_WIDTH_TIMELINE,Title,Soc_PutIconsTimeline, @@ -1124,7 +1125,8 @@ static void Soc_InsertNewPubsInTimeline (char **Query) struct SocialNote SocNot; /***** 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 *****/ for (NumPub = 0; @@ -1164,7 +1166,8 @@ static void Soc_ShowOldPubsInTimeline (char **Query) struct SocialNote SocNot; /***** 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 *****/ for (NumPub = 0; diff --git a/swad_statistic.c b/swad_statistic.c index e57c6c2c4..704da2055 100644 --- a/swad_statistic.c +++ b/swad_statistic.c @@ -287,8 +287,6 @@ void Sta_GetRemoteAddr (void) void Sta_LogAccess (const char *Comments) { - size_t MaxLength; - char *Query = NULL; long LogCod; long ActCod = Act_GetActCod (Gbl.Action.Act); 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.IP); + /* Log comments */ if (Comments) - { - /* Allocate space for query */ - MaxLength = 512 + strlen (Comments); - if ((Query = (char *) malloc (MaxLength + 1)) == NULL) - Lay_NotEnoughMemoryExit (); - - /* 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)"); - } + DB_QueryINSERT ("can not log access (comments)", + "INSERT INTO log_comments" + " (LogCod,Comments)" + " VALUES" + " (%ld,'%s')", + Comments); + /* Log search string */ if (Gbl.Search.LogSearch && Gbl.Search.Str[0]) - { - /* Allocate space for query */ - MaxLength = 512 + strlen (Gbl.Search.Str); - if ((Query = (char *) malloc (MaxLength + 1)) == NULL) - Lay_NotEnoughMemoryExit (); - - /* 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)"); - } + DB_QueryINSERT ("can not log access (search)", + "INSERT INTO log_search" + " (LogCod,SearchStr)" + " VALUES" + " (%ld,'%s')", + Gbl.Search.Str); if (Gbl.WebService.IsWebService) /* Log web service plugin and function */ @@ -1484,7 +1458,8 @@ static void Sta_ShowHits (Sta_GlobalOrCourseAccesses_t GlobalOrCourse) Ale_ShowAlert (Ale_INFO,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 *****/ if (NumRows == 0) diff --git a/swad_survey.c b/swad_survey.c index 5d61d0271..cbc6247f9 100644 --- a/swad_survey.c +++ b/swad_survey.c @@ -2327,7 +2327,8 @@ static bool Svy_CheckIfSvyIsAssociatedToGrps (long SvyCod) { /***** Get if a survey is associated to a group from database *****/ 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); } diff --git a/swad_test.c b/swad_test.c index 9a48e70c7..963ef5011 100644 --- a/swad_test.c +++ b/swad_test.c @@ -1672,7 +1672,8 @@ static bool Tst_CheckIfCurrentCrsHasTestTags (void) { /***** Get available tags from database *****/ 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); } @@ -2650,7 +2651,8 @@ static unsigned long Tst_GetQuestions (MYSQL_RES **mysql_res) } /* 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) 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); */ /* Make the query */ - return DB_QuerySELECT_old (&Query,mysql_res,"can not get questions"); + return DB_QuerySELECTusingQueryStr (&Query,mysql_res, + "can not get questions"); } /*****************************************************************************/ diff --git a/swad_user.c b/swad_user.c index 705846a44..cb1e51bd3 100644 --- a/swad_user.c +++ b/swad_user.c @@ -965,7 +965,8 @@ unsigned Usr_GetNumCrssOfUsr (long UsrCod) /***** Get the number of courses of a user from database ******/ return (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); } @@ -5242,7 +5243,9 @@ static void Usr_GetListUsrsFromQuery (char **Query,Rol_Role_t Role,Sco_Scope_t S } /***** 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) { @@ -9083,7 +9086,8 @@ float Usr_GetNumUsrsPerCrs (Rol_Role_t Role) bool Usr_CheckIfUsrBanned (long UsrCod) { 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); } @@ -9094,7 +9098,8 @@ bool Usr_CheckIfUsrBanned (long UsrCod) void Usr_RemoveUsrFromUsrBanned (long UsrCod) { DB_QueryDELETE ("can not remove user from users banned", - "DELETE FROM usr_banned WHERE UsrCod=%ld", + "DELETE FROM usr_banned" + " WHERE UsrCod=%ld", UsrCod); } diff --git a/swad_web_service.c b/swad_web_service.c index eca4a29a8..e846e74cb 100644 --- a/swad_web_service.c +++ b/swad_web_service.c @@ -358,7 +358,8 @@ static int Svc_CheckIdSession (const char *IdSession) /***** Query if session identifier already exists in database *****/ if (DB_QueryCOUNT ("can not get session data", - "SELECT COUNT(*) FROM sessions WHERE SessionId='%s'", + "SELECT COUNT(*) FROM sessions" + " WHERE SessionId='%s'", IdSession) != 1) return soap_receiver_fault (Gbl.soap, "Bad session identifier", @@ -413,7 +414,8 @@ static int Svc_CheckCourseAndGroupCodes (long CrsCod,long GrpCod) /***** Query if course code already exists in database *****/ if (DB_QueryCOUNT ("can not get course", - "SELECT COUNT(*) FROM courses WHERE CrsCod=%ld", + "SELECT COUNT(*) FROM courses" + " WHERE CrsCod=%ld", CrsCod) != 1) return soap_sender_fault (Gbl.soap, "Bad course code", @@ -456,7 +458,8 @@ static int Svc_GenerateNewWSKey (long UsrCod, /***** Check that key does not exist in database *****/ 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)) return soap_receiver_fault (Gbl.soap, "Error when generating key", @@ -2734,8 +2737,8 @@ int swad__sendAttendanceUsers (struct soap *soap, char LongStr[1 + 10 + 1]; struct UsrData UsrDat; unsigned NumCodsInList; - char *Query = NULL; - char SubQuery[256]; + char *SubQueryAllUsrs = NULL; + char SubQueryOneUsr[1 + 1 + 10 + 1]; size_t Length = 0; // Initialized to avoid warning /***** Initializations *****/ @@ -2786,15 +2789,13 @@ int swad__sendAttendanceUsers (struct soap *soap, /* Find next string in text until comma (leading and trailing spaces are removed) */ 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; - if ((Query = (char *) malloc (Length + 1)) == NULL) + if ((SubQueryAllUsrs = (char *) malloc (Length + 1)) == NULL) return soap_receiver_fault (Gbl.soap, "Not enough memory", "Not enough memory to store list of users"); - sprintf (Query,"UPDATE att_usr SET Present='N'" - " WHERE AttCod=%ld", - Att.AttCod); + SubQueryAllUsrs[0] = '\0'; } 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 */ if (setOthersAsAbsent) { - sprintf (SubQuery,sendAttendanceUsersOut->numUsers ? ",%ld" : - " AND UsrCod NOT IN (%ld", - UsrDat.UsrCod); - Str_Concat (Query,SubQuery, - Length); + if (sendAttendanceUsersOut->numUsers) + { + snprintf (SubQueryOneUsr,sizeof (SubQueryOneUsr), + ",%ld",UsrDat.UsrCod); + Str_Concat (SubQueryAllUsrs,SubQueryOneUsr, + Length); + } + else + snprintf (SubQueryAllUsrs,Length, + " AND UsrCod NOT IN (%ld", + UsrDat.UsrCod); } sendAttendanceUsersOut->numUsers++; @@ -2830,10 +2837,13 @@ int swad__sendAttendanceUsers (struct soap *soap, { /* Mark not present users as absent in table of users */ if (sendAttendanceUsersOut->numUsers) - Str_Concat (Query,")", + Str_Concat (SubQueryAllUsrs,")", 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 */ 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" { /***** 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->usersArray.__size = (int) NumRows;