diff --git a/swad_changelog.h b/swad_changelog.h index 6f7346e78..09b173087 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.11.21 (2018-11-02)" +#define Log_PLATFORM_VERSION "SWAD 18.11.22 (2018-11-02)" #define CSS_FILE "swad18.4.css" #define JS_FILE "swad17.17.1.js" /* + Version 18.11.22: Nov 02, 2018 Joining building and performing query into one function. (236589 lines) Version 18.11.21: Nov 02, 2018 Joining building and performing query into one function. (236580 lines) Version 18.11.20: Nov 02, 2018 Joining building and performing query into one function. (236585 lines) Version 18.11.19: Nov 02, 2018 Joining building and performing query into one function. (236465 lines) diff --git a/swad_database.c b/swad_database.c index 5e8596abd..6570e3d3c 100644 --- a/swad_database.c +++ b/swad_database.c @@ -3363,19 +3363,31 @@ void DB_QueryDELETE (const char *MsgError,const char *fmt,...) /**************** Make other kind of query from database *********************/ /*****************************************************************************/ -void DB_Query_new (const char *MsgError) +void DB_Query (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); + va_end (ap); + + if (NumBytesPrinted < 0) // If memory allocation wasn't possible, + // or some other error occurs, + // vasprintf will return -1 + Lay_NotEnoughMemoryExit (); + /***** Check that query string pointer does point to an allocated string *****/ - if (Gbl.DB.QueryPtr == NULL) + if (Query == 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; + Result = mysql_query (&Gbl.mysql,Query); // Returns 0 on success + free ((void *) Query); + Query = NULL; if (Result) DB_ExitOnMySQLError (MsgError); } diff --git a/swad_database.h b/swad_database.h index 6e458b8f9..bcf2207b4 100644 --- a/swad_database.h +++ b/swad_database.h @@ -60,7 +60,7 @@ void DB_QueryUPDATE_old (char **Query,const char *MsgError); void DB_QueryDELETE (const char *MsgError,const char *fmt,...); -void DB_Query_new (const char *MsgError); +void DB_Query (const char *MsgError,const char *fmt,...); void DB_FreeMySQLResult (MYSQL_RES **mysql_res); void DB_ExitOnMySQLError (const char *Message); diff --git a/swad_game.c b/swad_game.c index 2f6c1d9b9..95063ed28 100644 --- a/swad_game.c +++ b/swad_game.c @@ -3411,8 +3411,8 @@ static void Gam_ExchangeQuestions (long GamCod, long QstCodBottom; /***** Lock table to make the inscription atomic *****/ - DB_BuildQuery ("LOCK TABLES gam_questions WRITE"); - DB_Query_new ("can not lock tables to move game question"); + DB_Query ("can not lock tables to move game question", + "LOCK TABLES gam_questions WRITE"); Gbl.DB.LockedTables = true; /***** Get question code of the questions to be moved *****/ @@ -3447,8 +3447,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 - DB_BuildQuery ("UNLOCK TABLES"); - DB_Query_new ("can not unlock tables after moving game questions"); + DB_Query ("can not unlock tables after moving game questions", + "UNLOCK TABLES"); } /*****************************************************************************/ diff --git a/swad_group.c b/swad_group.c index a3dc39c29..679d10559 100644 --- a/swad_group.c +++ b/swad_group.c @@ -957,9 +957,9 @@ void Grp_ChangeGrpsOtherUsrAtomically (struct ListCodGrps *LstGrpsUsrWants) static void Grp_LockTables (void) { - 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"); + DB_Query ("can not lock tables to change user's groups", + "LOCK TABLES crs_grp_types WRITE,crs_grp WRITE," + "crs_grp_usr WRITE,crs_usr READ"); Gbl.DB.LockedTables = true; } @@ -971,8 +971,8 @@ static void Grp_UnlockTables (void) { Gbl.DB.LockedTables = false; // Set to false before the following unlock... // ...to not retry the unlock if error in unlocking - DB_BuildQuery ("UNLOCK TABLES"); - DB_Query_new ("can not unlock tables after changing user's groups"); + DB_Query ("can not unlock tables after changing user's groups", + "UNLOCK TABLES"); } /*****************************************************************************/ diff --git a/swad_mail.c b/swad_mail.c index 13f022a45..69e1d2ce0 100644 --- a/swad_mail.c +++ b/swad_mail.c @@ -229,16 +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 *****/ - DB_BuildQuery ("DROP TEMPORARY TABLE IF EXISTS T1,T2"); - DB_Query_new ("can not remove temporary tables"); + DB_Query ("can not remove temporary tables", + "DROP TEMPORARY TABLE IF EXISTS T1,T2"); - 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"); + DB_Query ("can not create temporary table", + "CREATE TEMPORARY TABLE T1 ENGINE=MEMORY" + " SELECT SUBSTRING_INDEX(E_mail,'@',-1) AS Domain,COUNT(*) as N" + " FROM usr_emails GROUP BY Domain"); - DB_BuildQuery ("CREATE TEMPORARY TABLE T2 ENGINE=MEMORY SELECT * FROM T1"); - DB_Query_new ("can not create temporary table"); + DB_Query ("can not create temporary table", + "CREATE TEMPORARY TABLE T2 ENGINE=MEMORY SELECT * FROM T1"); /***** Get mail domains from database *****/ switch (Gbl.Mails.SelectedOrder) @@ -313,8 +313,8 @@ static void Mai_GetListMailDomainsAllowedForNotif (void) DB_FreeMySQLResult (&mysql_res); /***** Drop temporary table *****/ - DB_BuildQuery ("DROP TEMPORARY TABLE IF EXISTS T1,T2"); - DB_Query_new ("can not remove temporary tables"); + DB_Query ("can not remove temporary tables", + "DROP TEMPORARY TABLE IF EXISTS T1,T2"); } /*****************************************************************************/ diff --git a/swad_search.c b/swad_search.c index 0c80ec1d4..e21b8fb03 100644 --- a/swad_search.c +++ b/swad_search.c @@ -959,38 +959,36 @@ 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 *****/ - DB_BuildQuery ("DROP TEMPORARY TABLE IF EXISTS my_files_crs,my_files_grp"); - DB_Query_new ("can not remove temporary table"); + DB_Query ("can not remove temporary table", + "DROP TEMPORARY TABLE IF EXISTS my_files_crs,my_files_grp"); - 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"); + DB_Query ("can not create temporary table", + "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_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_new ("can not create temporary table"); + DB_Query ("can not create temporary table", + "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); /***** Build the query *****/ NumDocs = DB_QuerySELECT (&mysql_res,"can not get files", @@ -1053,8 +1051,8 @@ static unsigned Sch_SearchDocumentsInMyCoursesInDB (const char *RangeQuery) Txt_documents_in_my_courses); /***** Drop temporary table *****/ - DB_BuildQuery ("DROP TEMPORARY TABLE IF EXISTS my_files_crs,my_files_grp"); - DB_Query_new ("can not remove temporary table"); + DB_Query ("can not remove temporary table", + "DROP TEMPORARY TABLE IF EXISTS my_files_crs,my_files_grp"); return (unsigned) NumDocs; } diff --git a/swad_social.c b/swad_social.c index 1487d11f2..a0f7a610d 100644 --- a/swad_social.c +++ b/swad_social.c @@ -537,21 +537,21 @@ static void Soc_BuildQueryToGetTimeline (char **Query, Soc_DropTemporaryTablesUsedToQueryTimeline (); /***** Create temporary table with publishing codes *****/ - DB_BuildQuery ("CREATE TEMPORARY TABLE pub_codes " - "(PubCod BIGINT NOT NULL,UNIQUE INDEX(PubCod)) ENGINE=MEMORY"); - DB_Query_new ("can not create temporary table"); + DB_Query ("can not create temporary table", + "CREATE TEMPORARY TABLE pub_codes " + "(PubCod BIGINT NOT NULL,UNIQUE INDEX(PubCod)) ENGINE=MEMORY"); /***** Create temporary table with notes got in this execution *****/ - DB_BuildQuery ("CREATE TEMPORARY TABLE not_codes " - "(NotCod BIGINT NOT NULL,INDEX(NotCod)) ENGINE=MEMORY"); - DB_Query_new ("can not create temporary table"); + DB_Query ("can not create temporary table", + "CREATE TEMPORARY TABLE not_codes " + "(NotCod BIGINT NOT NULL,INDEX(NotCod)) ENGINE=MEMORY"); /***** Create temporary table with notes already present in timeline for this session *****/ - DB_BuildQuery ("CREATE TEMPORARY TABLE current_timeline " - "(NotCod BIGINT NOT NULL,INDEX(NotCod)) ENGINE=MEMORY" - " SELECT NotCod FROM social_timelines WHERE SessionId='%s'", - Gbl.Session.Id); - DB_Query_new ("can not create temporary table"); + DB_Query ("can not create temporary table", + "CREATE TEMPORARY TABLE current_timeline " + "(NotCod BIGINT NOT NULL,INDEX(NotCod)) ENGINE=MEMORY" + " SELECT NotCod FROM social_timelines WHERE SessionId='%s'", + Gbl.Session.Id); /***** Create temporary table and subquery with potential publishers *****/ switch (TimelineUsrOrGbl) @@ -564,15 +564,15 @@ static void Soc_BuildQueryToGetTimeline (char **Query, switch (Gbl.Social.WhichUsrs) { case Soc_FOLLOWED: // Show the timeline of the users I follow - DB_BuildQuery ("CREATE TEMPORARY TABLE publishers " - "(UsrCod INT NOT NULL,UNIQUE INDEX(UsrCod)) ENGINE=MEMORY" - " SELECT %ld AS UsrCod" - " UNION" - " SELECT FollowedCod AS UsrCod" - " FROM usr_follow WHERE FollowerCod=%ld", - Gbl.Usrs.Me.UsrDat.UsrCod, - Gbl.Usrs.Me.UsrDat.UsrCod); - DB_Query_new ("can not create temporary table"); + DB_Query ("can not create temporary table", + "CREATE TEMPORARY TABLE publishers " + "(UsrCod INT NOT NULL,UNIQUE INDEX(UsrCod)) ENGINE=MEMORY" + " SELECT %ld AS UsrCod" + " UNION" + " SELECT FollowedCod AS UsrCod" + " FROM usr_follow WHERE FollowerCod=%ld", + Gbl.Usrs.Me.UsrDat.UsrCod, + Gbl.Usrs.Me.UsrDat.UsrCod); sprintf (SubQueryPublishers,"social_pubs.PublisherCod=publishers.UsrCod AND "); break; @@ -873,9 +873,9 @@ static void Soc_UpdateFirstPubCodIntoSession (long FirstPubCod) static void Soc_DropTemporaryTablesUsedToQueryTimeline (void) { - DB_BuildQuery ("DROP TEMPORARY TABLE IF EXISTS" - " pub_codes,not_codes,publishers,current_timeline"); - DB_Query_new ("can not remove temporary tables"); + DB_Query ("can not remove temporary tables", + "DROP TEMPORARY TABLE IF EXISTS" + " pub_codes,not_codes,publishers,current_timeline"); } /*****************************************************************************/ diff --git a/swad_test.c b/swad_test.c index 732a3138d..f32aa4127 100644 --- a/swad_test.c +++ b/swad_test.c @@ -1600,15 +1600,15 @@ void Tst_RenameTag (void) /* Create a temporary table with all the question codes that had the new tag as one of their tags */ - DB_BuildQuery ("DROP TEMPORARY TABLE IF EXISTS tst_question_tags_tmp"); - DB_Query_new ("can not remove temporary table"); + DB_Query ("can not remove temporary table", + "DROP TEMPORARY TABLE IF EXISTS tst_question_tags_tmp"); - DB_BuildQuery ("CREATE TEMPORARY TABLE tst_question_tags_tmp" - " ENGINE=MEMORY" - " SELECT QstCod FROM tst_question_tags" - " WHERE TagCod=%ld", - ExistingTagCod); - DB_Query_new ("can not create temporary table"); + DB_Query ("can not create temporary table", + "CREATE TEMPORARY TABLE tst_question_tags_tmp" + " ENGINE=MEMORY" + " SELECT QstCod FROM tst_question_tags" + " WHERE TagCod=%ld", + ExistingTagCod); /* Remove old tag in questions where it would be repeated */ // New tag existed for a question ==> delete old tag @@ -1631,8 +1631,8 @@ void Tst_RenameTag (void) DB_QueryUPDATE_new ("can not update a tag in some questions"); /* Drop temporary table, no longer necessary */ - DB_BuildQuery ("DROP TEMPORARY TABLE IF EXISTS tst_question_tags_tmp"); - DB_Query_new ("can not remove temporary table"); + DB_Query ("can not remove temporary table", + "DROP TEMPORARY TABLE IF EXISTS tst_question_tags_tmp"); /***** Delete old tag from tst_tags because it is not longer used *****/ diff --git a/swad_user.c b/swad_user.c index e319176a5..8fd23e327 100644 --- a/swad_user.c +++ b/swad_user.c @@ -1018,8 +1018,8 @@ unsigned Usr_GetNumUsrsInCrssOfAUsr (long UsrCod,Rol_Role_t UsrRole, // The temporary table achieves speedup from ~2s to few ms /***** Remove temporary table if exists *****/ - DB_BuildQuery ("DROP TEMPORARY TABLE IF EXISTS usr_courses_tmp"); - DB_Query_new ("can not remove temporary tables"); + DB_Query ("can not remove temporary tables", + "DROP TEMPORARY TABLE IF EXISTS usr_courses_tmp"); /***** Create temporary table with all user's courses as student/teacher *****/ switch (UsrRole) @@ -1038,14 +1038,14 @@ unsigned Usr_GetNumUsrsInCrssOfAUsr (long UsrCod,Rol_Role_t UsrRole, Lay_ShowErrorAndExit ("Wrong role."); break; } - DB_BuildQuery ("CREATE TEMPORARY TABLE IF NOT EXISTS usr_courses_tmp" - " (CrsCod INT NOT NULL,UNIQUE INDEX (CrsCod))" - " ENGINE=MEMORY" - " SELECT CrsCod FROM crs_usr" - " WHERE UsrCod=%ld" - "%s", - UsrCod,SubQueryRole); - DB_Query_new ("can not create temporary table"); + DB_Query ("can not create temporary table", + "CREATE TEMPORARY TABLE IF NOT EXISTS usr_courses_tmp" + " (CrsCod INT NOT NULL,UNIQUE INDEX (CrsCod))" + " ENGINE=MEMORY" + " SELECT CrsCod FROM crs_usr" + " WHERE UsrCod=%ld" + "%s", + UsrCod,SubQueryRole); /***** Get the number of students/teachers in a course from database ******/ switch (OthersRole) @@ -1072,8 +1072,8 @@ unsigned Usr_GetNumUsrsInCrssOfAUsr (long UsrCod,Rol_Role_t UsrRole, NumUsrs = (unsigned) DB_QueryCOUNT_new ("can not get the number of users"); /***** Remove temporary table *****/ - DB_BuildQuery ("DROP TEMPORARY TABLE IF EXISTS usr_courses_tmp"); - DB_Query_new ("can not remove temporary tables"); + DB_Query ("can not remove temporary tables", + "DROP TEMPORARY TABLE IF EXISTS usr_courses_tmp"); return NumUsrs; } @@ -1432,16 +1432,16 @@ bool Usr_CheckIfUsrSharesAnyOfMyCrsWithDifferentRole (long UsrCod) Usr_GetMyCourses (); /* Remove temporary table if exists */ - DB_BuildQuery ("DROP TEMPORARY TABLE IF EXISTS usr_courses_tmp"); - DB_Query_new ("can not remove temporary tables"); + DB_Query ("can not remove temporary tables", + "DROP TEMPORARY TABLE IF EXISTS usr_courses_tmp"); /* Create temporary table with all user's courses for a role */ - DB_BuildQuery ("CREATE TEMPORARY TABLE IF NOT EXISTS usr_courses_tmp " - "(CrsCod INT NOT NULL,Role TINYINT NOT NULL," - "UNIQUE INDEX(CrsCod,Role)) ENGINE=MEMORY" - " SELECT CrsCod,Role FROM crs_usr WHERE UsrCod=%ld", - UsrCod); - DB_Query_new ("can not create temporary table"); + DB_Query ("can not create temporary table", + "CREATE TEMPORARY TABLE IF NOT EXISTS usr_courses_tmp " + "(CrsCod INT NOT NULL,Role TINYINT NOT NULL," + "UNIQUE INDEX(CrsCod,Role)) ENGINE=MEMORY" + " SELECT CrsCod,Role FROM crs_usr WHERE UsrCod=%ld", + UsrCod); /* Get if a user shares any course with me from database */ DB_BuildQuery ("SELECT COUNT(*) FROM my_courses_tmp,usr_courses_tmp" @@ -1450,8 +1450,8 @@ bool Usr_CheckIfUsrSharesAnyOfMyCrsWithDifferentRole (long UsrCod) UsrSharesAnyOfMyCrsWithDifferentRole = (DB_QueryCOUNT_new ("can not check if a user shares any course with you") != 0); /* Remove temporary table if exists */ - DB_BuildQuery ("DROP TEMPORARY TABLE IF EXISTS usr_courses_tmp"); - DB_Query_new ("can not remove temporary tables"); + DB_Query ("can not remove temporary tables", + "DROP TEMPORARY TABLE IF EXISTS usr_courses_tmp"); return UsrSharesAnyOfMyCrsWithDifferentRole; } @@ -1667,19 +1667,19 @@ void Usr_GetMyCourses (void) Usr_RemoveTemporaryTableMyCourses (); /***** Create temporary table with my courses *****/ - DB_BuildQuery ("CREATE TEMPORARY TABLE IF NOT EXISTS my_courses_tmp " - "(CrsCod INT NOT NULL," - "Role TINYINT NOT NULL," - "DegCod INT NOT NULL," - "UNIQUE INDEX(CrsCod,Role,DegCod)) ENGINE=MEMORY" - " SELECT crs_usr.CrsCod,crs_usr.Role,courses.DegCod" - " FROM crs_usr,courses,degrees" - " WHERE crs_usr.UsrCod=%ld" - " AND crs_usr.CrsCod=courses.CrsCod" - " AND courses.DegCod=degrees.DegCod" - " ORDER BY degrees.ShortName,courses.ShortName", - Gbl.Usrs.Me.UsrDat.UsrCod); - DB_Query_new ("can not create temporary table"); + DB_Query ("can not create temporary table", + "CREATE TEMPORARY TABLE IF NOT EXISTS my_courses_tmp " + "(CrsCod INT NOT NULL," + "Role TINYINT NOT NULL," + "DegCod INT NOT NULL," + "UNIQUE INDEX(CrsCod,Role,DegCod)) ENGINE=MEMORY" + " SELECT crs_usr.CrsCod,crs_usr.Role,courses.DegCod" + " FROM crs_usr,courses,degrees" + " WHERE crs_usr.UsrCod=%ld" + " AND crs_usr.CrsCod=courses.CrsCod" + " AND courses.DegCod=degrees.DegCod" + " ORDER BY degrees.ShortName,courses.ShortName", + Gbl.Usrs.Me.UsrDat.UsrCod); /***** Get my courses from database *****/ NumCrss =