diff --git a/swad_changelog.h b/swad_changelog.h index 9c9a42fc0..011a4640c 100644 --- a/swad_changelog.h +++ b/swad_changelog.h @@ -355,10 +355,11 @@ En OpenSWAD: ps2pdf source.ps destination.pdf */ -#define Log_PLATFORM_VERSION "SWAD 18.8.17 (2018-10-28)" +#define Log_PLATFORM_VERSION "SWAD 18.8.18 (2018-10-28)" #define CSS_FILE "swad18.4.css" #define JS_FILE "swad17.17.1.js" /* + Version 18.8.18: Oct 28, 2018 Some asprintf for database queries changed by internal function. (236105 lines) Version 18.8.17: Oct 28, 2018 Some asprintf for database queries changed by internal function. (236116 lines) Version 18.8.16: Oct 28, 2018 Some asprintf for database queries changed by internal function. (236263 lines) Version 18.8.15: Oct 28, 2018 Some asprintf for database queries changed by internal function. (236425 lines) diff --git a/swad_database.c b/swad_database.c index 3bc79ccdc..0f0ea0236 100644 --- a/swad_database.c +++ b/swad_database.c @@ -3342,13 +3342,19 @@ void DB_QueryDELETE (const char *Query,const char *MsgError) /**************** Make other kind of query from database *********************/ /*****************************************************************************/ -void DB_Query_free (const char *Query,const char *MsgError) +void DB_Query_new (const char *MsgError) { int Result; - /***** Query database *****/ - Result = mysql_query (&Gbl.mysql,Query); // Returns 0 on success - free ((void *) Query); + /***** Check that query string pointer + does not point to an allocated string *****/ + if (Gbl.DB.QueryPtr == NULL) + Lay_ShowErrorAndExit ("Wrong query string."); + + /***** Query database and free query string pointer *****/ + Result = mysql_query (&Gbl.mysql,Gbl.DB.QueryPtr); // Returns 0 on success + free ((void *) Gbl.DB.QueryPtr); + Gbl.DB.QueryPtr = NULL; if (Result) DB_ExitOnMySQLError (MsgError); } diff --git a/swad_database.h b/swad_database.h index 52f7854b7..8d2956477 100644 --- a/swad_database.h +++ b/swad_database.h @@ -60,7 +60,7 @@ void DB_QueryUPDATE (const char *Query,const char *MsgError); void DB_QueryDELETE_new (const char *MsgError); void DB_QueryDELETE (const char *Query,const char *MsgError); -void DB_Query_free (const char *Query,const char *MsgError); +void DB_Query_new (const char *MsgError); void DB_Query (const char *Query,const char *MsgError); void DB_FreeMySQLResult (MYSQL_RES **mysql_res); void DB_ExitOnMySQLError (const char *Message); diff --git a/swad_game.c b/swad_game.c index 0457a3ebb..5db0f3cdd 100644 --- a/swad_game.c +++ b/swad_game.c @@ -3403,14 +3403,12 @@ void Gam_MoveDownQst (void) static void Gam_ExchangeQuestions (long GamCod, unsigned QstIndTop,unsigned QstIndBottom) { - char *Query; long QstCodTop; long QstCodBottom; /***** Lock table to make the inscription atomic *****/ - if (asprintf (&Query,"LOCK TABLES gam_questions WRITE") < 0) - Lay_NotEnoughMemoryExit (); - DB_Query_free (Query,"can not lock tables to move game question"); + DB_BuildQuery ("LOCK TABLES gam_questions WRITE"); + DB_Query_new ("can not lock tables to move game question"); Gbl.DB.LockedTables = true; /***** Get question code of the questions to be moved *****/ @@ -3445,9 +3443,8 @@ static void Gam_ExchangeQuestions (long GamCod, /***** Unlock table *****/ Gbl.DB.LockedTables = false; // Set to false before the following unlock... // ...to not retry the unlock if error in unlocking - if (asprintf (&Query,"UNLOCK TABLES") < 0) - Lay_NotEnoughMemoryExit (); - DB_Query_free (Query,"can not unlock tables after moving game questions"); + DB_BuildQuery ("UNLOCK TABLES"); + DB_Query_new ("can not unlock tables after moving game questions"); } /*****************************************************************************/ diff --git a/swad_group.c b/swad_group.c index 42398fbfb..a874f5ddc 100644 --- a/swad_group.c +++ b/swad_group.c @@ -957,12 +957,9 @@ void Grp_ChangeGrpsOtherUsrAtomically (struct ListCodGrps *LstGrpsUsrWants) static void Grp_LockTables (void) { - char *Query; - - if (asprintf (&Query,"LOCK TABLES crs_grp_types WRITE,crs_grp WRITE," - "crs_grp_usr WRITE,crs_usr READ") < 0) - Lay_NotEnoughMemoryExit (); - DB_Query_free (Query,"can not lock tables to change user's groups"); + DB_BuildQuery ("LOCK TABLES crs_grp_types WRITE,crs_grp WRITE," + "crs_grp_usr WRITE,crs_usr READ"); + DB_Query_new ("can not lock tables to change user's groups"); Gbl.DB.LockedTables = true; } @@ -972,13 +969,10 @@ static void Grp_LockTables (void) static void Grp_UnlockTables (void) { - char *Query; - Gbl.DB.LockedTables = false; // Set to false before the following unlock... // ...to not retry the unlock if error in unlocking - if (asprintf (&Query,"UNLOCK TABLES") < 0) - Lay_NotEnoughMemoryExit (); - DB_Query_free (Query,"can not unlock tables after changing user's groups"); + DB_BuildQuery ("UNLOCK TABLES"); + DB_Query_new ("can not unlock tables after changing user's groups"); } /*****************************************************************************/ diff --git a/swad_mail.c b/swad_mail.c index 028e43464..9fc6284e9 100644 --- a/swad_mail.c +++ b/swad_mail.c @@ -218,7 +218,6 @@ void Mai_EditMailDomains (void) static void Mai_GetListMailDomainsAllowedForNotif (void) { char OrderBySubQuery[256]; - char *Query; MYSQL_RES *mysql_res; MYSQL_ROW row; unsigned long NumRows; @@ -230,19 +229,16 @@ static void Mai_GetListMailDomainsAllowedForNotif (void) // ...because a unique temporary table can not be used twice in the same query /***** Create temporary table with all the mail domains present in users' emails table *****/ - if (asprintf (&Query,"DROP TEMPORARY TABLE IF EXISTS T1,T2") < 0) - Lay_NotEnoughMemoryExit (); - DB_Query_free (Query,"can not remove temporary tables"); + DB_BuildQuery ("DROP TEMPORARY TABLE IF EXISTS T1,T2"); + DB_Query_new ("can not remove temporary tables"); - if (asprintf (&Query,"CREATE TEMPORARY TABLE T1 ENGINE=MEMORY" - " SELECT SUBSTRING_INDEX(E_mail,'@',-1) AS Domain,COUNT(*) as N" - " FROM usr_emails GROUP BY Domain") < 0) - Lay_NotEnoughMemoryExit (); - DB_Query_free (Query,"can not create temporary table"); + DB_BuildQuery ("CREATE TEMPORARY TABLE T1 ENGINE=MEMORY" + " SELECT SUBSTRING_INDEX(E_mail,'@',-1) AS Domain,COUNT(*) as N" + " FROM usr_emails GROUP BY Domain"); + DB_Query_new ("can not create temporary table"); - if (asprintf (&Query,"CREATE TEMPORARY TABLE T2 ENGINE=MEMORY SELECT * FROM T1") < 0) - Lay_NotEnoughMemoryExit (); - DB_Query_free (Query,"can not create temporary table"); + DB_BuildQuery ("CREATE TEMPORARY TABLE T2 ENGINE=MEMORY SELECT * FROM T1"); + DB_Query_new ("can not create temporary table"); /***** Get mail domains from database *****/ switch (Gbl.Mails.SelectedOrder) @@ -309,9 +305,8 @@ static void Mai_GetListMailDomainsAllowedForNotif (void) DB_FreeMySQLResult (&mysql_res); /***** Drop temporary table *****/ - if (asprintf (&Query,"DROP TEMPORARY TABLE IF EXISTS T1,T2") < 0) - Lay_NotEnoughMemoryExit (); - DB_Query_free (Query,"can not remove temporary tables"); + DB_BuildQuery ("DROP TEMPORARY TABLE IF EXISTS T1,T2"); + DB_Query_new ("can not remove temporary tables"); } /*****************************************************************************/ diff --git a/swad_search.c b/swad_search.c index 0b49d7ad2..c5a548f16 100644 --- a/swad_search.c +++ b/swad_search.c @@ -924,7 +924,6 @@ static unsigned Sch_SearchDocumentsInMyCoursesInDB (const char *RangeQuery) { extern const char *Txt_document_in_my_courses; extern const char *Txt_documents_in_my_courses; - char *Query; char SearchQuery[Sch_MAX_BYTES_SEARCH_QUERY + 1]; unsigned NumDocs; @@ -936,41 +935,38 @@ static unsigned Sch_SearchDocumentsInMyCoursesInDB (const char *RangeQuery) { /***** Create temporary table with codes of files in documents and shared areas accessible by me. It is necessary to speed up the second query *****/ - if (asprintf (&Query,"DROP TEMPORARY TABLE IF EXISTS my_files_crs,my_files_grp") < 0) - Lay_NotEnoughMemoryExit (); - DB_Query_free (Query,"can not remove temporary table"); + DB_BuildQuery ("DROP TEMPORARY TABLE IF EXISTS my_files_crs,my_files_grp"); + DB_Query_new ("can not remove temporary table"); - if (asprintf (&Query,"CREATE TEMPORARY TABLE my_files_crs" - " (FilCod INT NOT NULL,UNIQUE INDEX(FilCod))" - " ENGINE=MEMORY" - " SELECT files.FilCod FROM crs_usr,files" - " WHERE crs_usr.UsrCod=%ld" - " AND crs_usr.CrsCod=files.Cod" - " AND files.FileBrowser IN (%u,%u,%u,%u)", - Gbl.Usrs.Me.UsrDat.UsrCod, - (unsigned) Brw_ADMI_DOC_CRS, - (unsigned) Brw_ADMI_TCH_CRS, - (unsigned) Brw_ADMI_SHR_CRS, - (unsigned) Brw_ADMI_MRK_CRS) < 0) - Lay_NotEnoughMemoryExit (); - DB_Query_free (Query,"can not create temporary table"); + DB_BuildQuery ("CREATE TEMPORARY TABLE my_files_crs" + " (FilCod INT NOT NULL,UNIQUE INDEX(FilCod))" + " ENGINE=MEMORY" + " SELECT files.FilCod FROM crs_usr,files" + " WHERE crs_usr.UsrCod=%ld" + " AND crs_usr.CrsCod=files.Cod" + " AND files.FileBrowser IN (%u,%u,%u,%u)", + Gbl.Usrs.Me.UsrDat.UsrCod, + (unsigned) Brw_ADMI_DOC_CRS, + (unsigned) Brw_ADMI_TCH_CRS, + (unsigned) Brw_ADMI_SHR_CRS, + (unsigned) Brw_ADMI_MRK_CRS); + DB_Query_new ("can not create temporary table"); - if (asprintf (&Query,"CREATE TEMPORARY TABLE my_files_grp" - " (FilCod INT NOT NULL,UNIQUE INDEX(FilCod))" - " ENGINE=MEMORY" - " SELECT files.FilCod FROM crs_grp_usr,files" - " WHERE crs_grp_usr.UsrCod=%ld" - " AND crs_grp_usr.GrpCod=files.Cod" - " AND files.FileBrowser IN (%u,%u,%u,%u)", - Gbl.Usrs.Me.UsrDat.UsrCod, - (unsigned) Brw_ADMI_DOC_GRP, - (unsigned) Brw_ADMI_TCH_GRP, - (unsigned) Brw_ADMI_SHR_GRP, - (unsigned) Brw_ADMI_MRK_GRP) < 0) - Lay_NotEnoughMemoryExit (); + DB_BuildQuery ("CREATE TEMPORARY TABLE my_files_grp" + " (FilCod INT NOT NULL,UNIQUE INDEX(FilCod))" + " ENGINE=MEMORY" + " SELECT files.FilCod FROM crs_grp_usr,files" + " WHERE crs_grp_usr.UsrCod=%ld" + " AND crs_grp_usr.GrpCod=files.Cod" + " AND files.FileBrowser IN (%u,%u,%u,%u)", + Gbl.Usrs.Me.UsrDat.UsrCod, + (unsigned) Brw_ADMI_DOC_GRP, + (unsigned) Brw_ADMI_TCH_GRP, + (unsigned) Brw_ADMI_SHR_GRP, + (unsigned) Brw_ADMI_MRK_GRP); /* if (Gbl.Usrs.Me.Roles.LoggedRole == Rol_SYS_ADM) Lay_ShowAlert (Lay_INFO,Query); */ - DB_Query_free (Query,"can not create temporary table"); + DB_Query_new ("can not create temporary table"); /***** Build the query *****/ DB_BuildQuery ("SELECT * FROM " @@ -1033,9 +1029,8 @@ static unsigned Sch_SearchDocumentsInMyCoursesInDB (const char *RangeQuery) Txt_documents_in_my_courses); /***** Drop temporary table *****/ - if (asprintf (&Query,"DROP TEMPORARY TABLE IF EXISTS my_files_crs,my_files_grp") < 0) - Lay_NotEnoughMemoryExit (); - DB_Query_free (Query,"can not remove temporary table"); + DB_BuildQuery ("DROP TEMPORARY TABLE IF EXISTS my_files_crs,my_files_grp"); + DB_Query_new ("can not remove temporary table"); return NumDocs; }