From 733ae0e86d791717d9cf3f999b4c7366b2db1385 Mon Sep 17 00:00:00 2001 From: acanas Date: Sun, 21 Nov 2021 01:22:59 +0100 Subject: [PATCH] Version 21.58: Nov 21, 2021 Code refactoring in timeline. --- js/{swad21.57.js => swad21.58.js} | 31 ++++++-- sql/cambios.sql | 39 ++++++++++- swad_browser_database.c | 72 +++++++++---------- swad_changelog.h | 5 +- swad_database.c | 32 ++++++++- swad_database.h | 3 + swad_enrolment_database.c | 85 +++++++++++----------- swad_follow_database.c | 26 ++++--- swad_mail_database.c | 23 +++--- swad_tag_database.c | 16 ++--- swad_timeline.c | 54 +++++++------- swad_timeline_database.c | 113 +++++++++++------------------- swad_timeline_database.h | 13 ++-- swad_timeline_note.c | 3 +- swad_timeline_publication.c | 27 ++----- swad_timeline_share.c | 8 +-- swad_user_database.c | 68 +++++++++--------- 17 files changed, 319 insertions(+), 299 deletions(-) rename js/{swad21.57.js => swad21.58.js} (98%) diff --git a/js/swad21.57.js b/js/swad21.58.js similarity index 98% rename from js/swad21.57.js rename to js/swad21.58.js index 4ce8f051..510e1e92 100644 --- a/js/swad21.57.js +++ b/js/swad21.58.js @@ -937,8 +937,30 @@ function readNewTimelineData () { // (move all the LI elements (notes) in UL 'just_now_timeline_list'... // ...to the top of UL 'new_timeline_list') var newTimeline = document.getElementById('new_timeline_list'); // Access to UL with the new timeline - for (var i=0; iPublishers.Table, + SubQueries->RangeBottom, + SubQueries->RangeTop, + SubQueries->Publishers.SubQuery, + SubQueries->AlreadyExists); + diff --git a/swad_browser_database.c b/swad_browser_database.c index 4265b063..742a2aff 100644 --- a/swad_browser_database.c +++ b/swad_browser_database.c @@ -1722,43 +1722,40 @@ unsigned Brw_DB_SearchFilesInMyCrss (MYSQL_RES **mysql_res, /***** Create temporary tables with codes of files in documents and shared areas accessible by me. It is necessary to speed up the second query *****/ - DB_Query ("can not remove temporary tables", - "DROP TEMPORARY TABLE IF EXISTS my_files_crs," - "my_files_grp"); + DB_DropTmpTable ("my_files_crs"); + DB_DropTmpTable ("my_files_grp"); - DB_Query ("can not create temporary table", - "CREATE TEMPORARY TABLE my_files_crs" - " (FilCod INT NOT NULL," - "UNIQUE INDEX(FilCod))" - " ENGINE=MEMORY" - " SELECT brw_files.FilCod" - " FROM crs_users," - "brw_files" - " WHERE crs_users.UsrCod=%ld" - " AND crs_users.CrsCod=brw_files.Cod" - " AND brw_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_CreateTmpTable ("CREATE TEMPORARY TABLE my_files_crs" + " (FilCod INT NOT NULL," + "UNIQUE INDEX(FilCod))" + " ENGINE=MEMORY" + " SELECT brw_files.FilCod" + " FROM crs_users," + "brw_files" + " WHERE crs_users.UsrCod=%ld" + " AND crs_users.CrsCod=brw_files.Cod" + " AND brw_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 ("can not create temporary table", - "CREATE TEMPORARY TABLE my_files_grp" - " (FilCod INT NOT NULL," - "UNIQUE INDEX(FilCod))" - " ENGINE=MEMORY" - " SELECT brw_files.FilCod" - " FROM grp_users," - "brw_files" - " WHERE grp_users.UsrCod=%ld" - " AND grp_users.GrpCod=brw_files.Cod" - " AND brw_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); + DB_CreateTmpTable ("CREATE TEMPORARY TABLE my_files_grp" + " (FilCod INT NOT NULL," + "UNIQUE INDEX(FilCod))" + " ENGINE=MEMORY" + " SELECT brw_files.FilCod" + " FROM grp_users," + "brw_files" + " WHERE grp_users.UsrCod=%ld" + " AND grp_users.GrpCod=brw_files.Cod" + " AND brw_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 *****/ NumFiles = (unsigned) @@ -1853,9 +1850,8 @@ unsigned Brw_DB_SearchFilesInMyCrss (MYSQL_RES **mysql_res, RangeQuery); /***** Drop temporary tables *****/ - DB_Query ("can not remove temporary table", - "DROP TEMPORARY TABLE IF EXISTS my_files_crs," - "my_files_grp"); + DB_DropTmpTable ("my_files_crs"); + DB_DropTmpTable ("my_files_grp"); return NumFiles; } diff --git a/swad_changelog.h b/swad_changelog.h index 5f8b1200..80682b3e 100644 --- a/swad_changelog.h +++ b/swad_changelog.h @@ -602,13 +602,14 @@ TODO: FIX BUG, URGENT! En las fechas como par TODO: En las encuestas, que los estudiantes no puedan ver los resultados hasta que no finalice el plazo. */ -#define Log_PLATFORM_VERSION "SWAD 21.57.5 (2021-11-18)" +#define Log_PLATFORM_VERSION "SWAD 21.58 (2021-11-21)" #define CSS_FILE "swad21.57.css" -#define JS_FILE "swad21.57.js" +#define JS_FILE "swad21.58.js" /* TODO: Rename CENTRE to CENTER in help wiki. TODO: Rename ASSESSMENT.Announcements to ASSESSMENT.Calls_for_exams + Version 21.58: Nov 21, 2021 Code refactoring in timeline. (319157 lines) Version 21.57.5: Nov 18, 2021 Code refactoring related to check if user logged is me. (319174 lines) Version 21.57.4: Nov 17, 2021 Code refactoring in timeline. (319235 lines) Version 21.57.3: Nov 17, 2021 Code refactoring in timeline. (319225 lines) diff --git a/swad_database.c b/swad_database.c index 5d27c71f..ce3d937e 100644 --- a/swad_database.c +++ b/swad_database.c @@ -3724,7 +3724,7 @@ mysql> DESCRIBE usr_webs; static void DB_CreateTable (const char *Query) { HTM_LI_Begin ("class=\"DAT\""); - HTM_Txt (Query); + HTM_Txt (Query); HTM_LI_End (); if (mysql_query (&Gbl.mysql,Query)) @@ -4224,6 +4224,36 @@ void DB_QueryDELETE (const char *MsgError,const char *fmt,...) DB_ExitOnMySQLError (MsgError); } +/*****************************************************************************/ +/**************************** Create temporary table *************************/ +/*****************************************************************************/ + +void DB_CreateTmpTable (const char *fmt,...) + { + va_list ap; + int NumBytesPrinted; + char *Query; + int Result; + + va_start (ap,fmt); + NumBytesPrinted = vasprintf (&Query,fmt,ap); + va_end (ap); + if (NumBytesPrinted < 0) // -1 if no memory or any other error + Err_NotEnoughMemoryExit (); + + /***** Query database and free query string pointer *****/ + Result = mysql_query (&Gbl.mysql,Query); // Returns 0 on success + free (Query); + if (Result) + DB_ExitOnMySQLError ("can not create temporary table"); + } + +void DB_DropTmpTable (const char *Table) + { + DB_Query ("can not remove temporary table", + "DROP TEMPORARY TABLE IF EXISTS %s",Table); + } + /*****************************************************************************/ /**************** Make other kind of query from database *********************/ /*****************************************************************************/ diff --git a/swad_database.h b/swad_database.h index d37da0f7..2d927614 100644 --- a/swad_database.h +++ b/swad_database.h @@ -69,6 +69,9 @@ void DB_QueryUPDATE (const char *MsgError,const char *fmt,...); void DB_QueryDELETE (const char *MsgError,const char *fmt,...); +void DB_CreateTmpTable (const char *fmt,...); +void DB_DropTmpTable (const char *Table); + void DB_Query (const char *MsgError,const char *fmt,...); void DB_FreeMySQLResult (MYSQL_RES **mysql_res); diff --git a/swad_enrolment_database.c b/swad_enrolment_database.c index ca9a55ba..b60d3e22 100644 --- a/swad_enrolment_database.c +++ b/swad_enrolment_database.c @@ -78,24 +78,23 @@ void Enr_DB_AcceptUsrInCrs (long UsrCod,long CrsCod) void Enr_DB_CreateTmpTableMyCourses (void) { - 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_users.CrsCod," - "crs_users.Role," - "crs_courses.DegCod" - " FROM crs_users," - "crs_courses," - "deg_degrees" - " WHERE crs_users.UsrCod=%ld" - " AND crs_users.CrsCod=crs_courses.CrsCod" - " AND crs_courses.DegCod=deg_degrees.DegCod" - " ORDER BY deg_degrees.ShortName," - "crs_courses.ShortName", - Gbl.Usrs.Me.UsrDat.UsrCod); + DB_CreateTmpTable ("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_users.CrsCod," + "crs_users.Role," + "crs_courses.DegCod" + " FROM crs_users," + "crs_courses," + "deg_degrees" + " WHERE crs_users.UsrCod=%ld" + " AND crs_users.CrsCod=crs_courses.CrsCod" + " AND crs_courses.DegCod=deg_degrees.DegCod" + " ORDER BY deg_degrees.ShortName," + "crs_courses.ShortName", + Gbl.Usrs.Me.UsrDat.UsrCod); } /*****************************************************************************/ @@ -118,8 +117,7 @@ unsigned Enr_DB_GetMyCourses (MYSQL_RES **mysql_res) void Enr_DB_DropTmpTableMyCourses (void) { - DB_Query ("can not remove temporary table", - "DROP TEMPORARY TABLE IF EXISTS my_courses_tmp"); + DB_DropTmpTable ("my_courses_tmp"); } /*****************************************************************************/ @@ -205,19 +203,17 @@ bool Enr_DB_CheckIfUsrSharesAnyOfMyCrsWithDifferentRole (long UsrCod) Enr_GetMyCourses (); /* Remove temporary table if exists */ - DB_Query ("can not remove temporary tables", - "DROP TEMPORARY TABLE IF EXISTS usr_courses_tmp"); + DB_DropTmpTable ("usr_courses_tmp"); /* Create temporary table with all user's courses for a role */ - 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_users" - " WHERE UsrCod=%ld", - UsrCod); + DB_CreateTmpTable ("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_users" + " WHERE UsrCod=%ld", + UsrCod); /* Get if a user shares any course with me from database */ UsrSharesAnyOfMyCrsWithDifferentRole = @@ -230,8 +226,7 @@ bool Enr_DB_CheckIfUsrSharesAnyOfMyCrsWithDifferentRole (long UsrCod) " AND my_courses_tmp.Role<>usr_courses_tmp.Role)"); /* Remove temporary table if exists */ - DB_Query ("can not remove temporary tables", - "DROP TEMPORARY TABLE IF EXISTS usr_courses_tmp"); + DB_DropTmpTable ("usr_courses_tmp"); return UsrSharesAnyOfMyCrsWithDifferentRole; } @@ -381,8 +376,7 @@ unsigned Enr_DB_GetNumUsrsInCrssOfAUsr (long UsrCod,Rol_Role_t UsrRole, // The temporary table achieves speedup from ~2s to few ms /***** Remove temporary table if exists *****/ - DB_Query ("can not remove temporary tables", - "DROP TEMPORARY TABLE IF EXISTS usr_courses_tmp"); + DB_DropTmpTable ("usr_courses_tmp"); /***** Create temporary table with all user's courses as student/non-editing teacher/teacher *****/ @@ -405,15 +399,15 @@ unsigned Enr_DB_GetNumUsrsInCrssOfAUsr (long UsrCod,Rol_Role_t UsrRole, Err_WrongRoleExit (); break; } - 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_users" - " WHERE UsrCod=%ld" - "%s", - UsrCod,SubQueryRole); + DB_CreateTmpTable ("CREATE TEMPORARY TABLE IF NOT EXISTS usr_courses_tmp" + " (CrsCod INT NOT NULL,UNIQUE INDEX (CrsCod))" + " ENGINE=MEMORY" + " SELECT CrsCod" + " FROM crs_users" + " WHERE UsrCod=%ld" + "%s", + UsrCod, + SubQueryRole); /***** Get the number of students/teachers in a course from database ******/ OthersRolesStr[0] = '\0'; @@ -437,8 +431,7 @@ unsigned Enr_DB_GetNumUsrsInCrssOfAUsr (long UsrCod,Rol_Role_t UsrRole, OthersRolesStr); /***** Remove temporary table *****/ - DB_Query ("can not remove temporary tables", - "DROP TEMPORARY TABLE IF EXISTS usr_courses_tmp"); + DB_DropTmpTable ("usr_courses_tmp"); return NumUsrs; } diff --git a/swad_follow_database.c b/swad_follow_database.c index 873b7ebe..11e71d91 100644 --- a/swad_follow_database.c +++ b/swad_follow_database.c @@ -693,22 +693,20 @@ void Fol_DB_RemoveUsrFromUsrFollow (long UsrCod) void Fol_DB_CreateTmpTableMeAndUsrsIFollow (void) { - DB_Query ("can not create temporary table", - "CREATE TEMPORARY TABLE fol_tmp_me_and_followed " - "(UsrCod INT NOT NULL," - "UNIQUE INDEX(UsrCod))" - " ENGINE=MEMORY" - " SELECT %ld AS UsrCod" // Me - " UNION" - " SELECT FollowedCod AS UsrCod" // Users I follow - " FROM usr_follow" - " WHERE FollowerCod=%ld", - Gbl.Usrs.Me.UsrDat.UsrCod, - Gbl.Usrs.Me.UsrDat.UsrCod); + DB_CreateTmpTable ("CREATE TEMPORARY TABLE fol_tmp_me_and_followed " + "(UsrCod INT NOT NULL," + "UNIQUE INDEX(UsrCod))" + " ENGINE=MEMORY" + " SELECT %ld AS UsrCod" // Me + " UNION" + " SELECT FollowedCod AS UsrCod" // Users I follow + " FROM usr_follow" + " WHERE FollowerCod=%ld", + Gbl.Usrs.Me.UsrDat.UsrCod, + Gbl.Usrs.Me.UsrDat.UsrCod); } void Fol_DB_DropTmpTableMeAndUsrsIFollow (void) { - DB_Query ("can not remove temporary table", - "DROP TEMPORARY TABLE IF EXISTS fol_tmp_me_and_followed"); + DB_DropTmpTable ("fol_tmp_me_and_followed"); } diff --git a/swad_mail_database.c b/swad_mail_database.c index b0f53cd9..af830a4f 100644 --- a/swad_mail_database.c +++ b/swad_mail_database.c @@ -307,17 +307,15 @@ void Mai_DB_RemoveExpiredPendingEmails (void) void Mai_DB_CreateTmpTables (void) { - 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_CreateTmpTable ("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 ("can not create temporary table", - "CREATE TEMPORARY TABLE T2 ENGINE=MEMORY" - " SELECT *" - " FROM T1"); + DB_CreateTmpTable ("CREATE TEMPORARY TABLE T2 ENGINE=MEMORY" + " SELECT *" + " FROM T1"); } /*****************************************************************************/ @@ -457,7 +455,6 @@ void Mai_DB_RemoveMailDomain (long MaiCod) void Mai_DB_RemoveTmpTables (void) { - DB_Query ("can not remove temporary tables", - "DROP TEMPORARY TABLE IF EXISTS T1," - "T2"); + DB_DropTmpTable ("T1"); + DB_DropTmpTable ("T2"); } diff --git a/swad_tag_database.c b/swad_tag_database.c index 12063a5a..8b5c3401 100644 --- a/swad_tag_database.c +++ b/swad_tag_database.c @@ -76,13 +76,12 @@ void Tag_DB_AddTagToQst (long QstCod,long TagCod,unsigned TagInd) void Tag_DB_CreateTmpTableQuestionsWithTag (long TagCod) { - 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", - TagCod); + DB_CreateTmpTable ("CREATE TEMPORARY TABLE tst_question_tags_tmp" + " ENGINE=MEMORY" + " SELECT QstCod" + " FROM tst_question_tags" + " WHERE TagCod=%ld", + TagCod); } /*****************************************************************************/ @@ -92,8 +91,7 @@ void Tag_DB_CreateTmpTableQuestionsWithTag (long TagCod) void Tag_DB_DropTmpTableQuestionsWithTag (void) { - DB_Query ("can not remove temporary table", - "DROP TEMPORARY TABLE IF EXISTS tst_question_tags_tmp"); + DB_DropTmpTable ("tst_question_tags_tmp"); } /*****************************************************************************/ diff --git a/swad_timeline.c b/swad_timeline.c index 8590c712..ebb24247 100644 --- a/swad_timeline.c +++ b/swad_timeline.c @@ -45,7 +45,7 @@ mysql> SHOW TABLES LIKE 'tml_%'; *Numbers are got from swad.ugr.es on may 2021 - ____tml_pubs___ _tml_comments + _tml_pubs______ _tml_comments | | | | | Publication p |---------->| Comment c |-----+ | (comment) | | (to note 2) | | @@ -61,35 +61,35 @@ mysql> SHOW TABLES LIKE 'tml_%'; | | (4855) | | |Publication i+3|-- | | |(original note)| \ | | - |_______________| \ ___tml_notes___ | | _calls_for_exams + |_______________| \ _tml_notes_____ | | _cfe_exams_____ | | \ | | | | | | |Publication i+2|-- ---->| Note n |<-+ | | Call for exam | (5581) |(original note)| \ |(exam announc.)|-(2622)->|_______________| - |_______________| \ |_______________| 11% ____files____ - | | \ | | | | | - |Publication i+1|-- ---->| Note n-1 |-(64)--->| Public file | (1497132) - |(original note)| \ | (public file) | <1% |_____________| - |_______________| \ |_______________| | _notices_ - | | \ | | | | | - | Publication i |-- ---->| Note n-2 |-(17078)>| Notice | (14984) - |(original note)| \ | (notice) | 72% |_________| - |_______________| \ |_______________| | __tml_posts__ - | | \ | | | | | - · ... · ---->| Note n-3 |-(3533)->| Post s | - · ... · | (tl. post) | 15% | | - |_______________| |_______________| | |_____________| - | | | | | | | - | Publication 3 | · ... · | · ... · (3533) - | (shared note) |--- · ... · | · ... · - |_______________| \ |_______________| | |_____________| - | | \ | | | | | - | Publication 2 | ---->| Note 2 |<---+ | Post 1 | - |(original note)|--------->| (tl. post) |-------->| | - |_______________| |_______________| |_____________| - | | | | _forum_post_ - | Publication 1 |--------->| Note 1 | | | - |(original note)| | (forum post) |-(276)-->| Forum post | (66226) - |_______________| |_______________| 1% |____________| + |_______________| \ |_______________| 11% _brw_files_____ + | | \ | | | | | + |Publication i+1|-- ---->| Note n-1 |-(64)--->| Public file | (1497132) + |(original note)| \ | (public file) | <1% |_______________| + |_______________| \ |_______________| | _not_notices___ + | | \ | | | | | + | Publication i |-- ---->| Note n-2 |-(17078)>| Notice | (14984) + |(original note)| \ | (notice) | 72% |_______________| + |_______________| \ |_______________| | _tml_posts_____ + | | \ | | | | | + · ... · ---->| Note n-3 |-(3533)->| Post s | + · ... · | (tl. post) | 15% | | + |_______________| |_______________| | |_______________| + | | | | | | | + | Publication 3 | · ... · | · ... · (3533) + | (shared note) |--- · ... · | · ... · + |_______________| \ |_______________| | |_______________| + | | \ | | | | | + | Publication 2 | ---->| Note 2 |<---+ | Post 1 | + |(original note)|--------->| (tl. post) |-------->| | + |_______________| |_______________| |_______________| + | | | | _for_posts_____ + | Publication 1 |--------->| Note 1 | | | + |(original note)| | (forum post) |-(276)-->| Forum post | (66226) + |_______________| |_______________| 1% |_______________| (29435) (23573) A note can be: diff --git a/swad_timeline_database.c b/swad_timeline_database.c index d93ee22e..85921a69 100644 --- a/swad_timeline_database.c +++ b/swad_timeline_database.c @@ -397,55 +397,34 @@ unsigned Tml_DB_GetNumNotesAndUsrsTotal (MYSQL_RES **mysql_res) /******* Create temporary tables used to not get notes already shown *********/ /*****************************************************************************/ -void Tml_DB_CreateTmpTableJustRetrievedNotes (void) +void Tml_DB_CreateTmpTableTimeline (Tml_WhatToGet_t WhatToGet) { - DB_Query ("can not create temporary table", - "CREATE TEMPORARY TABLE tml_tmp_just_retrieved_notes " - "(NotCod BIGINT NOT NULL,UNIQUE INDEX(NotCod))" - " ENGINE=MEMORY"); - } - -void Tml_DB_CreateTmpTableVisibleTimeline (void) - { - DB_Query ("can not create temporary table", - "CREATE TEMPORARY TABLE tml_tmp_visible_timeline " - "(NotCod BIGINT NOT NULL,UNIQUE INDEX(NotCod))" - " ENGINE=MEMORY" - " SELECT NotCod" - " FROM tml_timelines" - " WHERE SessionId='%s'", - Gbl.Session.Id); + switch (WhatToGet) + { + case Tml_GET_OLD_PUBS: + DB_CreateTmpTable ("CREATE TEMPORARY TABLE tml_tmp_timeline " + "(NotCod BIGINT NOT NULL,UNIQUE INDEX(NotCod))" + " ENGINE=MEMORY" + " SELECT NotCod" + " FROM tml_timelines" + " WHERE SessionId='%s'", + Gbl.Session.Id); + break; + case Tml_GET_NEW_PUBS: + case Tml_GET_REC_PUBS: + default: + DB_CreateTmpTable ("CREATE TEMPORARY TABLE tml_tmp_timeline " + "(NotCod BIGINT NOT NULL,UNIQUE INDEX(NotCod))" + " ENGINE=MEMORY"); + break; + } } /*****************************************************************************/ -/**** Insert note in temporary tables used to not get notes already shown ****/ +/**** Insert note in temporary table used to not get notes already shown *****/ /*****************************************************************************/ -void Tml_DB_InsertNoteInJustRetrievedNotes (long NotCod) - { - /* Insert note in temporary table with just retrieved notes. - This table will be used to not get notes already shown */ - DB_QueryINSERT ("can not store note code", - "INSERT IGNORE INTO tml_tmp_just_retrieved_notes" - " SET NotCod=%ld", - NotCod); - } - -void Tml_DB_InsertNoteInVisibleTimeline (long NotCod) - { - /* Insert note in temporary table with visible timeline. - This table will be used to not get notes already shown */ - DB_QueryINSERT ("can not store note code", - "INSERT IGNORE INTO tml_tmp_visible_timeline" - " SET NotCod=%ld", - NotCod); - } - -/*****************************************************************************/ -/****** Add just retrieved notes to current timeline for this session ********/ -/*****************************************************************************/ - -void Tml_DB_AddNotesJustRetrievedToVisibleTimelineOfSession (void) +void Tml_DB_InsertNoteInTimeline (long NotCod) { /* tml_timelines contains the distinct notes in timeline of each open session: mysql> SELECT SessionId,COUNT(*) FROM tml_timelines GROUP BY SessionId; @@ -465,31 +444,29 @@ mysql> SELECT SessionId,COUNT(*) FROM tml_timelines GROUP BY SessionId; +---------------------------------------------+----------+ 10 rows in set (0,01 sec) */ - DB_QueryINSERT ("can not insert notes in timeline", + /* Insert note in temporary table with visible timeline. + This table will be used to not get notes already shown */ + DB_QueryINSERT ("can not insert note in timeline", + "INSERT IGNORE INTO tml_tmp_timeline" + " SET NotCod=%ld", + NotCod); + + DB_QueryINSERT ("can not insert note in timeline", "INSERT IGNORE INTO tml_timelines" " (SessionId,NotCod)" - " SELECT '%s'," - "NotCod" - " FROM tml_tmp_just_retrieved_notes", - Gbl.Session.Id); + " VALUES" + " ('%s',%ld)", + Gbl.Session.Id, + NotCod); } /*****************************************************************************/ -/******** Drop temporary tables used to not get notes already shown **********/ +/********** Drop temporary table with all notes visible in timeline **********/ /*****************************************************************************/ -void Tml_DB_DropTmpTableJustRetrievedNotes (void) +void Tml_DB_DropTmpTableTimeline (void) { - /***** Drop temporary table with notes just retrieved *****/ - DB_Query ("can not remove temporary table", - "DROP TEMPORARY TABLE IF EXISTS tml_tmp_just_retrieved_notes"); - } - -void Tml_DB_DropTmpTableVisibleTimeline (void) - { - /***** Drop temporary table with all notes visible in timeline *****/ - DB_Query ("can not remove temporary table", - "DROP TEMPORARY TABLE IF EXISTS tml_tmp_visible_timeline"); + DB_DropTmpTable ("tml_tmp_timeline"); } /*****************************************************************************/ @@ -918,21 +895,13 @@ void Tml_DB_CreateSubQueryPublishers (Tml_Usr_UsrOrGbl_t UsrOrGbl,Usr_Who_t Who, /********* Create subquery to get only notes not present in timeline *********/ /*****************************************************************************/ -void Tml_DB_CreateSubQueryAlreadyExists (Tml_WhatToGet_t WhatToGet, - char AlreadyExists[Tml_Pub_MAX_BYTES_SUBQUERY + 1]) +void Tml_DB_CreateSubQueryAlreadyExists (char AlreadyExists[Tml_Pub_MAX_BYTES_SUBQUERY + 1]) { - static const char *Table[Tml_NUM_WHAT_TO_GET] = - { - [Tml_GET_NEW_PUBS] = "tml_tmp_just_retrieved_notes", // Avoid notes just retrieved - [Tml_GET_REC_PUBS] = "tml_tmp_just_retrieved_notes", // Avoid notes just retrieved - [Tml_GET_OLD_PUBS] = "tml_tmp_visible_timeline", // Avoid notes already shown - }; - - snprintf (AlreadyExists,Tml_Pub_MAX_BYTES_SUBQUERY + 1, + Str_Copy (AlreadyExists, " tml_pubs.NotCod NOT IN" " (SELECT NotCod" - " FROM %s)", - Table[WhatToGet]); + " FROM tml_tmp_timeline)", + Tml_Pub_MAX_BYTES_SUBQUERY); } /*****************************************************************************/ diff --git a/swad_timeline_database.h b/swad_timeline_database.h index 8652be4d..918819f3 100644 --- a/swad_timeline_database.h +++ b/swad_timeline_database.h @@ -57,13 +57,9 @@ unsigned Tml_DB_GetNumNotesAndUsrsByType (MYSQL_RES **mysql_res, Tml_Not_Type_t NoteType); unsigned Tml_DB_GetNumNotesAndUsrsTotal (MYSQL_RES **mysql_res); -void Tml_DB_CreateTmpTableJustRetrievedNotes (void); -void Tml_DB_CreateTmpTableVisibleTimeline (void); -void Tml_DB_InsertNoteInJustRetrievedNotes (long NotCod); -void Tml_DB_InsertNoteInVisibleTimeline (long NotCod); -void Tml_DB_AddNotesJustRetrievedToVisibleTimelineOfSession (void); -void Tml_DB_DropTmpTableJustRetrievedNotes (void); -void Tml_DB_DropTmpTableVisibleTimeline (void); +void Tml_DB_CreateTmpTableTimeline (Tml_WhatToGet_t WhatToGet); +void Tml_DB_InsertNoteInTimeline (long NotCod); +void Tml_DB_DropTmpTableTimeline (void); void Tml_DB_ClearOldTimelinesNotesFromDB (void); void Tml_DB_ClearTimelineNotesOfSessionFromDB (void); @@ -100,8 +96,7 @@ void Tml_DB_RemoveAllCommsMadeBy (long UsrCod); void Tml_DB_CreateSubQueryPublishers (Tml_Usr_UsrOrGbl_t UsrOrGbl,Usr_Who_t Who, char **Table, char SubQuery[Tml_Pub_MAX_BYTES_SUBQUERY + 1]); -void Tml_DB_CreateSubQueryAlreadyExists (Tml_WhatToGet_t WhatToGet, - char AlreadyExists[Tml_Pub_MAX_BYTES_SUBQUERY + 1]); +void Tml_DB_CreateSubQueryAlreadyExists (char AlreadyExists[Tml_Pub_MAX_BYTES_SUBQUERY + 1]); void Tml_DB_CreateSubQueryRangeBottom (long Bottom, char SubQuery[Tml_Pub_MAX_BYTES_SUBQUERY + 1]); void Tml_DB_CreateSubQueryRangeTop (long Top, diff --git a/swad_timeline_note.c b/swad_timeline_note.c index e0a8bdbd..9293c255 100644 --- a/swad_timeline_note.c +++ b/swad_timeline_note.c @@ -1172,8 +1172,7 @@ static void Tml_Not_RemoveNoteMediaAndDBEntries (struct Tml_Not_Note *Not) /***** Mark possible notifications on the publications of this note as removed *****/ - PubCod = Tml_DB_GetPubCodOfOriginalNote (Not->NotCod); - if (PubCod > 0) + if ((PubCod = Tml_DB_GetPubCodOfOriginalNote (Not->NotCod)) > 0) { Ntf_DB_MarkNotifAsRemoved (Ntf_EVENT_TML_FAV ,PubCod); Ntf_DB_MarkNotifAsRemoved (Ntf_EVENT_TML_SHARE ,PubCod); diff --git a/swad_timeline_publication.c b/swad_timeline_publication.c index b325fda6..09cac089 100644 --- a/swad_timeline_publication.c +++ b/swad_timeline_publication.c @@ -96,13 +96,8 @@ void Tml_Pub_GetListPubsToShowInTimeline (struct Tml_Timeline *Timeline) if (Timeline->WhatToGet == Tml_GET_REC_PUBS) Tml_DB_ClearTimelineNotesOfSessionFromDB (); - /***** Create temporary tables *****/ - /* Create temporary table with notes just retrieved */ - Tml_DB_CreateTmpTableJustRetrievedNotes (); - - /* Create temporary table with all notes visible in timeline */ - if (Timeline->WhatToGet == Tml_GET_OLD_PUBS) - Tml_DB_CreateTmpTableVisibleTimeline (); + /***** Create temporary table with all notes visible in timeline *****/ + Tml_DB_CreateTmpTableTimeline (Timeline->WhatToGet); /***** Create subqueries *****/ /* Create subquery with potential publishers */ @@ -111,8 +106,7 @@ void Tml_Pub_GetListPubsToShowInTimeline (struct Tml_Timeline *Timeline) SubQueries.Publishers.SubQuery); /* Create subquery to get only notes not present in timeline */ - Tml_DB_CreateSubQueryAlreadyExists (Timeline->WhatToGet, - SubQueries.AlreadyExists); + Tml_DB_CreateSubQueryAlreadyExists (SubQueries.AlreadyExists); /* Create subquery with bottom range of publications to get from tml_pubs. Bottom pub. code remains unchanged in all iterations of the loop. */ @@ -179,11 +173,9 @@ void Tml_Pub_GetListPubsToShowInTimeline (struct Tml_Timeline *Timeline) if (Pub == NULL) // Nothing got ==> abort loop break; // Last publication - /* Insert note in temporary tables with just retrieved notes. + /* Insert note in temporary tables with visible notes. These tables will be used to not get notes already shown */ - Tml_DB_InsertNoteInJustRetrievedNotes (Pub->NotCod); - if (Timeline->WhatToGet == Tml_GET_OLD_PUBS) // Get only old publications - Tml_DB_InsertNoteInVisibleTimeline (Pub->NotCod); + Tml_DB_InsertNoteInTimeline (Pub->NotCod); /* Narrow the range for the next iteration */ RangePubsToGet.Top = Pub->PubCod; @@ -193,14 +185,9 @@ void Tml_Pub_GetListPubsToShowInTimeline (struct Tml_Timeline *Timeline) into session for next refresh *****/ Tml_Pub_UpdateFirstLastPubCodesIntoSession (Timeline); - /***** Add notes just retrieved to visible timeline for this session *****/ - Tml_DB_AddNotesJustRetrievedToVisibleTimelineOfSession (); - /***** Drop temporary tables *****/ - /* Drop temporary tables with notes already retrieved */ - Tml_DB_DropTmpTableJustRetrievedNotes (); - if (Timeline->WhatToGet == Tml_GET_OLD_PUBS) // Get only old publications - Tml_DB_DropTmpTableVisibleTimeline (); + /* Drop temporary table with visible notes in timeline */ + Tml_DB_DropTmpTableTimeline (); /* Drop temporary table with me and users I follow */ if (Timeline->UsrOrGbl == Tml_Usr_TIMELINE_GBL && // Show the global timeline diff --git a/swad_timeline_share.c b/swad_timeline_share.c index 024b73fc..088e7201 100644 --- a/swad_timeline_share.c +++ b/swad_timeline_share.c @@ -121,7 +121,7 @@ static void Tml_Sha_ShaNote (struct Tml_Not_Note *Not) /***** Share (publish note in timeline) *****/ Pub.NotCod = Not->NotCod; Pub.PublisherCod = Gbl.Usrs.Me.UsrDat.UsrCod; - Pub.Type = Tml_Pub_SHARED_NOTE; + Pub.Type = Tml_Pub_SHARED_NOTE; Tml_Pub_PublishPubInTimeline (&Pub); // Set Pub.PubCod /***** Update number of times this note is shared *****/ @@ -129,8 +129,7 @@ static void Tml_Sha_ShaNote (struct Tml_Not_Note *Not) /***** Create notification about shared post for the author of the post *****/ - OriginalPubCod = Tml_DB_GetPubCodOfOriginalNote (Not->NotCod); - if (OriginalPubCod > 0) + if ((OriginalPubCod = Tml_DB_GetPubCodOfOriginalNote (Not->NotCod)) > 0) Tml_Ntf_CreateNotifToAuthor (Not->UsrCod,OriginalPubCod,Ntf_EVENT_TML_SHARE); } @@ -179,7 +178,6 @@ static void Tml_Sha_UnsNote (struct Tml_Not_Note *Not) Not->NumShared = Tml_DB_GetNumSharers (Not->NotCod,Not->UsrCod); /***** Mark possible notifications on this note as removed *****/ - OriginalPubCod = Tml_DB_GetPubCodOfOriginalNote (Not->NotCod); - if (OriginalPubCod > 0) + if ((OriginalPubCod = Tml_DB_GetPubCodOfOriginalNote (Not->NotCod)) > 0) Ntf_DB_MarkNotifAsRemoved (Ntf_EVENT_TML_SHARE,OriginalPubCod); } diff --git a/swad_user_database.c b/swad_user_database.c index e43f5bac..623e9e92 100644 --- a/swad_user_database.c +++ b/swad_user_database.c @@ -961,7 +961,7 @@ void Usr_DB_BuildQueryToSearchListUsrs (Rol_Role_t Role,char **Query) "usr_data.CtyCod," // row[ 9] "usr_data.InsCod"; // row[10] static const char *OrderBySubQuery = - "candidate_users.UsrCod=usr_data.UsrCod" + "usr_candidate_users.UsrCod=usr_data.UsrCod" " ORDER BY usr_data.Surname1," "usr_data.Surname2," "usr_data.FirstName," @@ -980,7 +980,7 @@ void Usr_DB_BuildQueryToSearchListUsrs (Rol_Role_t Role,char **Query) /* Search users from the whole platform */ DB_BuildQuery (Query, "SELECT %s" - " FROM candidate_users,usr_data" + " FROM usr_candidate_users,usr_data" " WHERE %s", QueryFields, OrderBySubQuery); @@ -989,14 +989,14 @@ void Usr_DB_BuildQueryToSearchListUsrs (Rol_Role_t Role,char **Query) /* Search users in courses from the current country */ DB_BuildQuery (Query, "SELECT %s" - " FROM candidate_users," + " FROM usr_candidate_users," "crs_users," "crs_courses," "deg_degrees," "ctr_centers," "ins_instits," "usr_data" - " WHERE candidate_users.UsrCod=crs_users.UsrCod" + " WHERE usr_candidate_users.UsrCod=crs_users.UsrCod" " AND crs_users.CrsCod=crs_courses.CrsCod" " AND crs_courses.DegCod=deg_degrees.DegCod" " AND deg_degrees.CtrCod=ctr_centers.CtrCod" @@ -1011,13 +1011,13 @@ void Usr_DB_BuildQueryToSearchListUsrs (Rol_Role_t Role,char **Query) /* Search users in courses from the current institution */ DB_BuildQuery (Query, "SELECT %s" - " FROM candidate_users," + " FROM usr_candidate_users," "crs_users," "crs_courses," "deg_degrees," "ctr_centers," "usr_data" - " WHERE candidate_users.UsrCod=crs_users.UsrCod" + " WHERE usr_candidate_users.UsrCod=crs_users.UsrCod" " AND crs_users.CrsCod=crs_courses.CrsCod" " AND crs_courses.DegCod=deg_degrees.DegCod" " AND deg_degrees.CtrCod=ctr_centers.CtrCod" @@ -1031,12 +1031,12 @@ void Usr_DB_BuildQueryToSearchListUsrs (Rol_Role_t Role,char **Query) /* Search users in courses from the current center */ DB_BuildQuery (Query, "SELECT %s" - " FROM candidate_users," + " FROM usr_candidate_users," "crs_users," "crs_courses," "deg_degrees," " usr_data" - " WHERE candidate_users.UsrCod=crs_users.UsrCod" + " WHERE usr_candidate_users.UsrCod=crs_users.UsrCod" " AND crs_users.CrsCod=crs_courses.CrsCod" " AND crs_courses.DegCod=deg_degrees.DegCod" " AND deg_degrees.CtrCod=%ld" @@ -1049,11 +1049,11 @@ void Usr_DB_BuildQueryToSearchListUsrs (Rol_Role_t Role,char **Query) /* Search users in courses from the current degree */ DB_BuildQuery (Query, "SELECT %s" - " FROM candidate_users," + " FROM usr_candidate_users," "crs_users," "crs_courses," "usr_data" - " WHERE candidate_users.UsrCod=crs_users.UsrCod" + " WHERE usr_candidate_users.UsrCod=crs_users.UsrCod" " AND crs_users.CrsCod=crs_courses.CrsCod" " AND crs_courses.DegCod=%ld" " AND %s", @@ -1067,10 +1067,10 @@ void Usr_DB_BuildQueryToSearchListUsrs (Rol_Role_t Role,char **Query) "SELECT %s," "crs_users.Role," // row[11] "crs_users.Accepted" // row[12] - " FROM candidate_users," + " FROM usr_candidate_users," "crs_users," "usr_data" - " WHERE candidate_users.UsrCod=crs_users.UsrCod" + " WHERE usr_candidate_users.UsrCod=crs_users.UsrCod" " AND crs_users.CrsCod=%ld" " AND %s", QueryFields, @@ -1086,9 +1086,9 @@ void Usr_DB_BuildQueryToSearchListUsrs (Rol_Role_t Role,char **Query) /* Search users with no courses */ DB_BuildQuery (Query, "SELECT %s" - " FROM candidate_users," + " FROM usr_candidate_users," "usr_data" - " WHERE candidate_users.UsrCod NOT IN" + " WHERE usr_candidate_users.UsrCod NOT IN" " (SELECT UsrCod" " FROM crs_users)" " AND %s", @@ -1125,10 +1125,10 @@ void Usr_DB_BuildQueryToSearchListUsrs (Rol_Role_t Role,char **Query) /* Search users in courses from the whole platform */ DB_BuildQuery (Query, "SELECT %s" - " FROM candidate_users," + " FROM usr_candidate_users," "crs_users," "usr_data" - " WHERE candidate_users.UsrCod=crs_users.UsrCod" + " WHERE usr_candidate_users.UsrCod=crs_users.UsrCod" "%s" " AND %s", QueryFields, @@ -1139,14 +1139,14 @@ void Usr_DB_BuildQueryToSearchListUsrs (Rol_Role_t Role,char **Query) /* Search users in courses from the current country */ DB_BuildQuery (Query, "SELECT %s" - " FROM candidate_users," + " FROM usr_candidate_users," "crs_users," "crs_courses," "deg_degrees," "ctr_centers," "ins_instits," "usr_data" - " WHERE candidate_users.UsrCod=crs_users.UsrCod" + " WHERE usr_candidate_users.UsrCod=crs_users.UsrCod" "%s" " AND crs_users.CrsCod=crs_courses.CrsCod" " AND crs_courses.DegCod=deg_degrees.DegCod" @@ -1163,13 +1163,13 @@ void Usr_DB_BuildQueryToSearchListUsrs (Rol_Role_t Role,char **Query) /* Search users in courses from the current institution */ DB_BuildQuery (Query, "SELECT %s" - " FROM candidate_users," + " FROM usr_candidate_users," "crs_users," "crs_courses," "deg_degrees," "ctr_centers," "usr_data" - " WHERE candidate_users.UsrCod=crs_users.UsrCod" + " WHERE usr_candidate_users.UsrCod=crs_users.UsrCod" "%s" " AND crs_users.CrsCod=crs_courses.CrsCod" " AND crs_courses.DegCod=deg_degrees.DegCod" @@ -1185,12 +1185,12 @@ void Usr_DB_BuildQueryToSearchListUsrs (Rol_Role_t Role,char **Query) /* Search users in courses from the current center */ DB_BuildQuery (Query, "SELECT %s" - " FROM candidate_users," + " FROM usr_candidate_users," "crs_users," "crs_courses," "deg_degrees," "usr_data" - " WHERE candidate_users.UsrCod=crs_users.UsrCod" + " WHERE usr_candidate_users.UsrCod=crs_users.UsrCod" "%s" " AND crs_users.CrsCod=crs_courses.CrsCod" " AND crs_courses.DegCod=deg_degrees.DegCod" @@ -1205,11 +1205,11 @@ void Usr_DB_BuildQueryToSearchListUsrs (Rol_Role_t Role,char **Query) /* Search users in courses from the current degree */ DB_BuildQuery (Query, "SELECT %s" - " FROM candidate_users," + " FROM usr_candidate_users," "crs_users," "crs_courses," "usr_data" - " WHERE candidate_users.UsrCod=crs_users.UsrCod" + " WHERE usr_candidate_users.UsrCod=crs_users.UsrCod" "%s" " AND crs_users.CrsCod=crs_courses.CrsCod" " AND crs_courses.DegCod=%ld" @@ -1225,10 +1225,10 @@ void Usr_DB_BuildQueryToSearchListUsrs (Rol_Role_t Role,char **Query) "SELECT %s," "crs_users.Role," "crs_users.Accepted" - " FROM candidate_users," + " FROM usr_candidate_users," "crs_users," "usr_data" - " WHERE candidate_users.UsrCod=crs_users.UsrCod" + " WHERE usr_candidate_users.UsrCod=crs_users.UsrCod" "%s" " AND crs_users.CrsCod=%ld" " AND %s", @@ -1410,13 +1410,12 @@ void Usr_DB_CreateTmpTableAndSearchCandidateUsrs (const char SearchQuery[Sch_MAX - Searching for names is made in the whole platform and stored in this table. */ - DB_Query ("can not create temporary table", - "CREATE TEMPORARY TABLE candidate_users" - " (UsrCod INT NOT NULL,UNIQUE INDEX(UsrCod)) ENGINE=MEMORY" - " SELECT UsrCod" - " FROM usr_data" - " WHERE %s", - SearchQuery); + DB_CreateTmpTable ("CREATE TEMPORARY TABLE usr_candidate_users" + " (UsrCod INT NOT NULL,UNIQUE INDEX(UsrCod)) ENGINE=MEMORY" + " SELECT UsrCod" + " FROM usr_data" + " WHERE %s", + SearchQuery); } /*****************************************************************************/ @@ -1425,8 +1424,7 @@ void Usr_DB_CreateTmpTableAndSearchCandidateUsrs (const char SearchQuery[Sch_MAX void Usr_DB_DropTmpTableWithCandidateUsrs (void) { - DB_Query ("can not remove temporary table", - "DROP TEMPORARY TABLE IF EXISTS candidate_users"); + DB_DropTmpTable ("usr_candidate_users"); } /*****************************************************************************/