From 14fd8a1faaca6bdda389d82e2a2e63c863b09968 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Antonio=20Ca=C3=B1as=20Vargas?= Date: Fri, 26 Oct 2018 11:01:06 +0200 Subject: [PATCH] Version 18.8.7 --- swad_account.c | 19 +- swad_agenda.c | 8 +- swad_assignment.c | 67 +++----- swad_attendance.c | 64 +++---- swad_banner.c | 11 +- swad_centre.c | 104 +++++------ swad_changelog.h | 3 +- swad_connected.c | 9 +- swad_country.c | 139 ++++++--------- swad_course.c | 97 ++++------- swad_database.c | 24 ++- swad_database.h | 1 + swad_degree.c | 102 +++++------ swad_degree_type.c | 19 +- swad_department.c | 38 ++-- swad_duplicate.c | 9 +- swad_file_browser.c | 127 ++++++-------- swad_follow.c | 29 ++-- swad_forum.c | 409 +++++++++++++++++--------------------------- swad_game.c | 79 +++------ 20 files changed, 528 insertions(+), 830 deletions(-) diff --git a/swad_account.c b/swad_account.c index 37a09d67f..d751e0e51 100644 --- a/swad_account.c +++ b/swad_account.c @@ -611,7 +611,6 @@ static bool Acc_GetParamsNewAccount (char NewNicknameWithoutArroba[Nck_MAX_BYTES extern const char *Txt_The_nickname_entered_X_is_not_valid_; extern const char *Txt_The_email_address_X_had_been_registered_by_another_user; extern const char *Txt_The_email_address_entered_X_is_not_valid; - char *Query; char NewNicknameWithArroba[Nck_MAX_BYTES_NICKNAME_FROM_FORM + 1]; char NewPlainPassword[Pwd_MAX_BYTES_PLAIN_PASSWORD + 1]; bool Error = false; @@ -634,11 +633,10 @@ static bool Acc_GetParamsNewAccount (char NewNicknameWithoutArroba[Nck_MAX_BYTES { /* Check if the new nickname matches any of the nicknames of other users */ - if (asprintf (&Query,"SELECT COUNT(*) FROM usr_nicknames" - " WHERE Nickname='%s' AND UsrCod<>%ld", - NewNicknameWithoutArroba,Gbl.Usrs.Me.UsrDat.UsrCod) < 0) - Lay_NotEnoughMemoryExit (); - if (DB_QueryCOUNT_free (Query,"can not check if nickname already existed")) // A nickname of another user is the same that this nickname + DB_BuildQuery ("SELECT COUNT(*) FROM usr_nicknames" + " WHERE Nickname='%s' AND UsrCod<>%ld", + NewNicknameWithoutArroba,Gbl.Usrs.Me.UsrDat.UsrCod); + if (DB_QueryCOUNT_new ("can not check if nickname already existed")) // A nickname of another user is the same that this nickname { Error = true; snprintf (Gbl.Alert.Txt,sizeof (Gbl.Alert.Txt), @@ -665,11 +663,10 @@ static bool Acc_GetParamsNewAccount (char NewNicknameWithoutArroba[Nck_MAX_BYTES { /* Check if the new email matches any of the confirmed emails of other users */ - if (asprintf (&Query,"SELECT COUNT(*) FROM usr_emails" - " WHERE E_mail='%s' AND Confirmed='Y'", - NewEmail) < 0) - Lay_NotEnoughMemoryExit (); - if (DB_QueryCOUNT_free (Query,"can not check if email already existed")) // An email of another user is the same that my email + DB_BuildQuery ("SELECT COUNT(*) FROM usr_emails" + " WHERE E_mail='%s' AND Confirmed='Y'", + NewEmail); + if (DB_QueryCOUNT_new ("can not check if email already existed")) // An email of another user is the same that my email { Error = true; snprintf (Gbl.Alert.Txt,sizeof (Gbl.Alert.Txt), diff --git a/swad_agenda.c b/swad_agenda.c index 8284f46c8..2076fee3b 100644 --- a/swad_agenda.c +++ b/swad_agenda.c @@ -1775,13 +1775,9 @@ void Agd_RemoveUsrEvents (long UsrCod) unsigned Agd_GetNumEventsFromUsr (long UsrCod) { - char *Query; - /***** Get number of events in a course from database *****/ - if (asprintf (&Query,"SELECT COUNT(*) FROM agendas WHERE UsrCod=%ld", - UsrCod) < 0) - Lay_NotEnoughMemoryExit (); - return (unsigned) DB_QueryCOUNT_free (Query,"can not get number of events from user"); + DB_BuildQuery ("SELECT COUNT(*) FROM agendas WHERE UsrCod=%ld",UsrCod); + return (unsigned) DB_QueryCOUNT_new ("can not get number of events from user"); } /*****************************************************************************/ diff --git a/swad_assignment.c b/swad_assignment.c index 11746ab8d..25a4cc4e7 100644 --- a/swad_assignment.c +++ b/swad_assignment.c @@ -1097,14 +1097,11 @@ void Asg_ShowAssignment (void) static bool Asg_CheckIfSimilarAssignmentExists (const char *Field,const char *Value,long AsgCod) { - char *Query; - /***** Get number of assignments with a field value from database *****/ - if (asprintf (&Query,"SELECT COUNT(*) FROM assignments" - " WHERE CrsCod=%ld AND %s='%s' AND AsgCod<>%ld", - Gbl.CurrentCrs.Crs.CrsCod,Field,Value,AsgCod) < 0) - Lay_NotEnoughMemoryExit (); - return (DB_QueryCOUNT_free (Query,"can not get similar assignments") != 0); + DB_BuildQuery ("SELECT COUNT(*) FROM assignments" + " WHERE CrsCod=%ld AND %s='%s' AND AsgCod<>%ld", + Gbl.CurrentCrs.Crs.CrsCod,Field,Value,AsgCod); + return (DB_QueryCOUNT_new ("can not get similar assignments") != 0); } /*****************************************************************************/ @@ -1545,13 +1542,9 @@ static void Asg_UpdateAssignment (struct Assignment *Asg,const char *Txt) static bool Asg_CheckIfAsgIsAssociatedToGrps (long AsgCod) { - char *Query; - /***** Get if an assignment is associated to a group from database *****/ - if (asprintf (&Query,"SELECT COUNT(*) FROM asg_grp WHERE AsgCod=%ld", - AsgCod) < 0) - Lay_NotEnoughMemoryExit (); - return (DB_QueryCOUNT_free (Query,"can not check if an assignment is associated to groups") != 0); + DB_BuildQuery ("SELECT COUNT(*) FROM asg_grp WHERE AsgCod=%ld",AsgCod); + return (DB_QueryCOUNT_new ("can not check if an assignment is associated to groups") != 0); } /*****************************************************************************/ @@ -1560,14 +1553,11 @@ static bool Asg_CheckIfAsgIsAssociatedToGrps (long AsgCod) bool Asg_CheckIfAsgIsAssociatedToGrp (long AsgCod,long GrpCod) { - char *Query; - /***** Get if an assignment is associated to a group from database *****/ - if (asprintf (&Query,"SELECT COUNT(*) FROM asg_grp" - " WHERE AsgCod=%ld AND GrpCod=%ld", - AsgCod,GrpCod) < 0) - Lay_NotEnoughMemoryExit (); - return (DB_QueryCOUNT_free (Query,"can not check if an assignment is associated to a group") != 0); + DB_BuildQuery ("SELECT COUNT(*) FROM asg_grp" + " WHERE AsgCod=%ld AND GrpCod=%ld", + AsgCod,GrpCod); + return (DB_QueryCOUNT_new ("can not check if an assignment is associated to a group") != 0); } /*****************************************************************************/ @@ -1734,8 +1724,6 @@ void Asg_RemoveCrsAssignments (long CrsCod) static bool Asg_CheckIfIBelongToCrsOrGrpsThisAssignment (long AsgCod) { - char *Query; - switch (Gbl.Usrs.Me.Role.Logged) { case Rol_STD: @@ -1743,20 +1731,19 @@ static bool Asg_CheckIfIBelongToCrsOrGrpsThisAssignment (long AsgCod) case Rol_TCH: // Students and teachers can do assignments depending on groups /***** Get if I can do an assignment from database *****/ - if (asprintf (&Query,"SELECT COUNT(*) FROM assignments" - " WHERE AsgCod=%ld" - " AND " - "(" - "AsgCod NOT IN (SELECT AsgCod FROM asg_grp)" // Assignment is for the whole course - " OR " - "AsgCod IN" // Assignment is for specific groups - " (SELECT asg_grp.AsgCod FROM asg_grp,crs_grp_usr" - " WHERE crs_grp_usr.UsrCod=%ld" - " AND asg_grp.GrpCod=crs_grp_usr.GrpCod)" - ")", - AsgCod,Gbl.Usrs.Me.UsrDat.UsrCod) < 0) - Lay_NotEnoughMemoryExit (); - return (DB_QueryCOUNT_free (Query,"can not check if I can do an assignment") != 0); + DB_BuildQuery ("SELECT COUNT(*) FROM assignments" + " WHERE AsgCod=%ld" + " AND " + "(" + "AsgCod NOT IN (SELECT AsgCod FROM asg_grp)" // Assignment is for the whole course + " OR " + "AsgCod IN" // Assignment is for specific groups + " (SELECT asg_grp.AsgCod FROM asg_grp,crs_grp_usr" + " WHERE crs_grp_usr.UsrCod=%ld" + " AND asg_grp.GrpCod=crs_grp_usr.GrpCod)" + ")", + AsgCod,Gbl.Usrs.Me.UsrDat.UsrCod); + return (DB_QueryCOUNT_new ("can not check if I can do an assignment") != 0); case Rol_DEG_ADM: case Rol_CTR_ADM: case Rol_INS_ADM: @@ -1774,13 +1761,9 @@ static bool Asg_CheckIfIBelongToCrsOrGrpsThisAssignment (long AsgCod) unsigned Asg_GetNumAssignmentsInCrs (long CrsCod) { - char *Query; - /***** Get number of assignments in a course from database *****/ - if (asprintf (&Query,"SELECT COUNT(*) FROM assignments WHERE CrsCod=%ld", - CrsCod) < 0) - Lay_NotEnoughMemoryExit (); - return (unsigned) DB_QueryCOUNT_free (Query,"can not get number of assignments in course"); + DB_BuildQuery ("SELECT COUNT(*) FROM assignments WHERE CrsCod=%ld",CrsCod); + return (unsigned) DB_QueryCOUNT_new ("can not get number of assignments in course"); } /*****************************************************************************/ diff --git a/swad_attendance.c b/swad_attendance.c index 8d596e4ef..fa992384c 100644 --- a/swad_attendance.c +++ b/swad_attendance.c @@ -1026,14 +1026,11 @@ void Att_ShowAttEvent (void) static bool Att_CheckIfSimilarAttEventExists (const char *Field,const char *Value,long AttCod) { - char *Query; - /***** Get number of attendance events with a field value from database *****/ - if (asprintf (&Query,"SELECT COUNT(*) FROM att_events" - " WHERE CrsCod=%ld AND %s='%s' AND AttCod<>%ld", - Gbl.CurrentCrs.Crs.CrsCod,Field,Value,AttCod) < 0) - Lay_NotEnoughMemoryExit (); - return (DB_QueryCOUNT_free (Query,"can not get similar attendance events") != 0); + DB_BuildQuery ("SELECT COUNT(*) FROM att_events" + " WHERE CrsCod=%ld AND %s='%s' AND AttCod<>%ld", + Gbl.CurrentCrs.Crs.CrsCod,Field,Value,AttCod); + return (DB_QueryCOUNT_new ("can not get similar attendance events") != 0); } /*****************************************************************************/ @@ -1420,13 +1417,9 @@ void Att_UpdateAttEvent (struct AttendanceEvent *Att,const char *Txt) bool Att_CheckIfAttEventIsAssociatedToGrps (long AttCod) { - char *Query; - /***** Get if an attendance event is associated to a group from database *****/ - if (asprintf (&Query,"SELECT COUNT(*) FROM att_grp WHERE AttCod=%ld", - AttCod) < 0) - Lay_NotEnoughMemoryExit (); - return (DB_QueryCOUNT_free (Query,"can not check if an attendance event is associated to groups") != 0); + DB_BuildQuery ("SELECT COUNT(*) FROM att_grp WHERE AttCod=%ld",AttCod); + return (DB_QueryCOUNT_new ("can not check if an attendance event is associated to groups") != 0); } /*****************************************************************************/ @@ -1435,14 +1428,11 @@ bool Att_CheckIfAttEventIsAssociatedToGrps (long AttCod) bool Att_CheckIfAttEventIsAssociatedToGrp (long AttCod,long GrpCod) { - char *Query; - /***** Get if an attendance event is associated to a group from database *****/ - if (asprintf (&Query,"SELECT COUNT(*) FROM att_grp" - " WHERE AttCod=%ld AND GrpCod=%ld", - AttCod,GrpCod) < 0) - Lay_NotEnoughMemoryExit (); - return (DB_QueryCOUNT_free (Query,"can not check if an attendance event is associated to a group") != 0); + DB_BuildQuery ("SELECT COUNT(*) FROM att_grp" + " WHERE AttCod=%ld AND GrpCod=%ld", + AttCod,GrpCod); + return (DB_QueryCOUNT_new ("can not check if an attendance event is associated to a group") != 0); } /*****************************************************************************/ @@ -1678,13 +1668,9 @@ void Att_RemoveCrsAttEvents (long CrsCod) unsigned Att_GetNumAttEventsInCrs (long CrsCod) { - char *Query; - /***** Get number of attendance events in a course from database *****/ - if (asprintf (&Query,"SELECT COUNT(*) FROM att_events WHERE CrsCod=%ld", - CrsCod) < 0) - Lay_NotEnoughMemoryExit (); - return (unsigned) DB_QueryCOUNT_free (Query,"can not get number of attendance events in course"); + DB_BuildQuery ("SELECT COUNT(*) FROM att_events WHERE CrsCod=%ld",CrsCod); + return (unsigned) DB_QueryCOUNT_new ("can not get number of attendance events in course"); } /*****************************************************************************/ @@ -2451,14 +2437,11 @@ void Att_RegisterStudentsInAttEvent (void) static void Att_GetNumStdsTotalWhoAreInAttEvent (struct AttendanceEvent *Att) { - char *Query; - /***** Count number of students registered in an event in database *****/ - if (asprintf (&Query,"SELECT COUNT(*) FROM att_usr" - " WHERE AttCod=%ld AND Present='Y'", - Att->AttCod) < 0) - Lay_NotEnoughMemoryExit (); - Att->NumStdsTotal = (unsigned) DB_QueryCOUNT_free (Query,"can not get number of students who are registered in an event"); + DB_BuildQuery ("SELECT COUNT(*) FROM att_usr" + " WHERE AttCod=%ld AND Present='Y'", + Att->AttCod); + Att->NumStdsTotal = (unsigned) DB_QueryCOUNT_new ("can not get number of students who are registered in an event"); } /*****************************************************************************/ @@ -2467,7 +2450,6 @@ static void Att_GetNumStdsTotalWhoAreInAttEvent (struct AttendanceEvent *Att) static unsigned Att_GetNumStdsFromAListWhoAreInAttEvent (long AttCod,long LstSelectedUsrCods[],unsigned NumStdsInList) { - char *Query; char SubQuery[1 + 1 + 10 + 1]; unsigned NumStd; unsigned NumStdsInAttEvent = 0; @@ -2477,13 +2459,13 @@ static unsigned Att_GetNumStdsFromAListWhoAreInAttEvent (long AttCod,long LstSel { /***** Allocate space for query *****/ MaxLength = 256 + NumStdsInList * (1 + 1 + 10); - if ((Query = (char *) malloc (MaxLength + 1)) == NULL) + if ((Gbl.DB.QueryPtr = (char *) malloc (MaxLength + 1)) == NULL) Lay_NotEnoughMemoryExit (); /***** Count number of students registered in an event in database *****/ - sprintf (Query,"SELECT COUNT(*) FROM att_usr" - " WHERE AttCod=%ld" - " AND UsrCod IN (", + sprintf (Gbl.DB.QueryPtr,"SELECT COUNT(*) FROM att_usr" + " WHERE AttCod=%ld" + " AND UsrCod IN (", AttCod); for (NumStd = 0; NumStd < NumStdsInList; @@ -2493,13 +2475,13 @@ static unsigned Att_GetNumStdsFromAListWhoAreInAttEvent (long AttCod,long LstSel NumStd ? ",%ld" : "%ld", LstSelectedUsrCods[NumStd]); - Str_Concat (Query,SubQuery, + Str_Concat (Gbl.DB.QueryPtr,SubQuery, MaxLength); } - Str_Concat (Query,") AND Present='Y'", + Str_Concat (Gbl.DB.QueryPtr,") AND Present='Y'", MaxLength); - NumStdsInAttEvent = (unsigned) DB_QueryCOUNT_free (Query,"can not get number of students from a list who are registered in an event"); + NumStdsInAttEvent = (unsigned) DB_QueryCOUNT_new ("can not get number of students from a list who are registered in an event"); } return NumStdsInAttEvent; } diff --git a/swad_banner.c b/swad_banner.c index 79f6ed349..c0b9c9e9f 100644 --- a/swad_banner.c +++ b/swad_banner.c @@ -713,14 +713,11 @@ static void Ban_RenameBanner (Cns_ShrtOrFullName_t ShrtOrFullName) static bool Ban_CheckIfBannerNameExists (const char *FieldName,const char *Name,long BanCod) { - char *Query; - /***** Get number of banners with a name from database *****/ - if (asprintf (&Query,"SELECT COUNT(*) FROM banners" - " WHERE %s='%s' AND BanCod<>%ld", - FieldName,Name,BanCod) < 0) - Lay_NotEnoughMemoryExit (); - return (DB_QueryCOUNT_free (Query,"can not check if the name of a banner already existed") != 0); + DB_BuildQuery ("SELECT COUNT(*) FROM banners" + " WHERE %s='%s' AND BanCod<>%ld", + FieldName,Name,BanCod); + return (DB_QueryCOUNT_new ("can not check if the name of a banner already existed") != 0); } /*****************************************************************************/ diff --git a/swad_centre.c b/swad_centre.c index c741e17f3..73abd87bc 100644 --- a/swad_centre.c +++ b/swad_centre.c @@ -2065,14 +2065,11 @@ static void Ctr_RenameCentre (struct Centre *Ctr,Cns_ShrtOrFullName_t ShrtOrFull static bool Ctr_CheckIfCtrNameExistsInIns (const char *FieldName,const char *Name,long CtrCod,long InsCod) { - char *Query; - /***** Get number of centres with a name from database *****/ - if (asprintf (&Query,"SELECT COUNT(*) FROM centres" - " WHERE InsCod=%ld AND %s='%s' AND CtrCod<>%ld", - InsCod,FieldName,Name,CtrCod) < 0) - Lay_NotEnoughMemoryExit (); - return (DB_QueryCOUNT_free (Query,"can not check if the name of a centre already existed") != 0); + DB_BuildQuery ("SELECT COUNT(*) FROM centres" + " WHERE InsCod=%ld AND %s='%s' AND CtrCod<>%ld", + InsCod,FieldName,Name,CtrCod); + return (DB_QueryCOUNT_new ("can not check if the name of a centre already existed") != 0); } /*****************************************************************************/ @@ -2861,12 +2858,9 @@ static void Ctr_CreateCentre (unsigned Status) unsigned Ctr_GetNumCtrsTotal (void) { - char *Query; - /***** Get total number of centres from database *****/ - if (asprintf (&Query,"SELECT COUNT(*) FROM centres") < 0) - Lay_NotEnoughMemoryExit (); - return (unsigned) DB_QueryCOUNT_free (Query,"can not get total number of centres"); + DB_BuildQuery ("SELECT COUNT(*) FROM centres"); + return (unsigned) DB_QueryCOUNT_new ("can not get total number of centres"); } /*****************************************************************************/ @@ -2875,15 +2869,12 @@ unsigned Ctr_GetNumCtrsTotal (void) unsigned Ctr_GetNumCtrsInCty (long CtyCod) { - char *Query; - /***** Get number of centres of a country from database *****/ - if (asprintf (&Query,"SELECT COUNT(*) FROM institutions,centres" - " WHERE institutions.CtyCod=%ld" - " AND institutions.InsCod=centres.InsCod", - CtyCod) < 0) - Lay_NotEnoughMemoryExit (); - return (unsigned) DB_QueryCOUNT_free (Query,"can not get number of centres in a country"); + DB_BuildQuery ("SELECT COUNT(*) FROM institutions,centres" + " WHERE institutions.CtyCod=%ld" + " AND institutions.InsCod=centres.InsCod", + CtyCod); + return (unsigned) DB_QueryCOUNT_new ("can not get number of centres in a country"); } /*****************************************************************************/ @@ -2892,14 +2883,9 @@ unsigned Ctr_GetNumCtrsInCty (long CtyCod) unsigned Ctr_GetNumCtrsInIns (long InsCod) { - char *Query; - /***** Get number of centres of an institution from database *****/ - if (asprintf (&Query,"SELECT COUNT(*) FROM centres" - " WHERE InsCod=%ld", - InsCod) < 0) - Lay_NotEnoughMemoryExit (); - return (unsigned) DB_QueryCOUNT_free (Query,"can not get number of centres in an institution"); + DB_BuildQuery ("SELECT COUNT(*) FROM centres WHERE InsCod=%ld",InsCod); + return (unsigned) DB_QueryCOUNT_new ("can not get number of centres in an institution"); } /*****************************************************************************/ @@ -2908,14 +2894,11 @@ unsigned Ctr_GetNumCtrsInIns (long InsCod) unsigned Ctr_GetNumCtrsInPlc (long PlcCod) { - char *Query; - /***** Get number of centres (of the current institution) in a place *****/ - if (asprintf (&Query,"SELECT COUNT(*) FROM centres" - " WHERE InsCod=%ld AND PlcCod=%ld", - Gbl.CurrentIns.Ins.InsCod,PlcCod) < 0) - Lay_NotEnoughMemoryExit (); - return (unsigned) DB_QueryCOUNT_free (Query,"can not get the number of centres in a place"); + DB_BuildQuery ("SELECT COUNT(*) FROM centres" + " WHERE InsCod=%ld AND PlcCod=%ld", + Gbl.CurrentIns.Ins.InsCod,PlcCod); + return (unsigned) DB_QueryCOUNT_new ("can not get the number of centres in a place"); } /*****************************************************************************/ @@ -2924,16 +2907,13 @@ unsigned Ctr_GetNumCtrsInPlc (long PlcCod) unsigned Ctr_GetNumCtrsWithDegs (const char *SubQuery) { - char *Query; - /***** Get number of centres with degrees from database *****/ - if (asprintf (&Query,"SELECT COUNT(DISTINCT centres.CtrCod)" - " FROM institutions,centres,degrees" - " WHERE %sinstitutions.InsCod=centres.InsCod" - " AND centres.CtrCod=degrees.CtrCod", - SubQuery) < 0) - Lay_NotEnoughMemoryExit (); - return (unsigned) DB_QueryCOUNT_free (Query,"can not get number of centres with degrees"); + DB_BuildQuery ("SELECT COUNT(DISTINCT centres.CtrCod)" + " FROM institutions,centres,degrees" + " WHERE %sinstitutions.InsCod=centres.InsCod" + " AND centres.CtrCod=degrees.CtrCod", + SubQuery); + return (unsigned) DB_QueryCOUNT_new ("can not get number of centres with degrees"); } /*****************************************************************************/ @@ -2942,17 +2922,14 @@ unsigned Ctr_GetNumCtrsWithDegs (const char *SubQuery) unsigned Ctr_GetNumCtrsWithCrss (const char *SubQuery) { - char *Query; - /***** Get number of centres with courses from database *****/ - if (asprintf (&Query,"SELECT COUNT(DISTINCT centres.CtrCod)" - " FROM institutions,centres,degrees,courses" - " WHERE %sinstitutions.InsCod=centres.InsCod" - " AND centres.CtrCod=degrees.CtrCod" - " AND degrees.DegCod=courses.DegCod", - SubQuery) < 0) - Lay_NotEnoughMemoryExit (); - return (unsigned) DB_QueryCOUNT_free (Query,"can not get number of centres with courses"); + DB_BuildQuery ("SELECT COUNT(DISTINCT centres.CtrCod)" + " FROM institutions,centres,degrees,courses" + " WHERE %sinstitutions.InsCod=centres.InsCod" + " AND centres.CtrCod=degrees.CtrCod" + " AND degrees.DegCod=courses.DegCod", + SubQuery); + return (unsigned) DB_QueryCOUNT_new ("can not get number of centres with courses"); } /*****************************************************************************/ @@ -2961,19 +2938,16 @@ unsigned Ctr_GetNumCtrsWithCrss (const char *SubQuery) unsigned Ctr_GetNumCtrsWithUsrs (Rol_Role_t Role,const char *SubQuery) { - char *Query; - /***** Get number of centres with users from database *****/ - if (asprintf (&Query,"SELECT COUNT(DISTINCT centres.CtrCod)" - " FROM institutions,centres,degrees,courses,crs_usr" - " WHERE %sinstitutions.InsCod=centres.InsCod" - " AND centres.CtrCod=degrees.CtrCod" - " AND degrees.DegCod=courses.DegCod" - " AND courses.CrsCod=crs_usr.CrsCod" - " AND crs_usr.Role=%u", - SubQuery,(unsigned) Role) < 0) - Lay_NotEnoughMemoryExit (); - return (unsigned) DB_QueryCOUNT_free (Query,"can not get number of centres with users"); + DB_BuildQuery ("SELECT COUNT(DISTINCT centres.CtrCod)" + " FROM institutions,centres,degrees,courses,crs_usr" + " WHERE %sinstitutions.InsCod=centres.InsCod" + " AND centres.CtrCod=degrees.CtrCod" + " AND degrees.DegCod=courses.DegCod" + " AND courses.CrsCod=crs_usr.CrsCod" + " AND crs_usr.Role=%u", + SubQuery,(unsigned) Role); + return (unsigned) DB_QueryCOUNT_new ("can not get number of centres with users"); } /*****************************************************************************/ diff --git a/swad_changelog.h b/swad_changelog.h index f345e8ff7..1cd7b965c 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.6 (2018-10-26)" +#define Log_PLATFORM_VERSION "SWAD 18.8.7 (2018-10-26)" #define CSS_FILE "swad18.4.css" #define JS_FILE "swad17.17.1.js" /* + Version 18.8.7: Oct 26, 2018 Some asprintf for database queries changed by internal function. (237172 lines) Version 18.8.6: Oct 26, 2018 Some asprintf for database queries changed by internal function. (237407 lines) Version 18.8.5: Oct 26, 2018 Some asprintf for database queries changed by internal function. (237723 lines) Version 18.8.4: Oct 25, 2018 Some asprintf for database queries changed by internal function. (237916 lines) diff --git a/swad_connected.c b/swad_connected.c index 5953f0a06..31c29aba2 100644 --- a/swad_connected.c +++ b/swad_connected.c @@ -676,16 +676,13 @@ void Con_RemoveOldConnected (void) static unsigned Con_GetConnectedUsrsTotal (Rol_Role_t Role) { - char *Query; - if (!Gbl.DB.DatabaseIsOpen) return 0; /***** Get number of connected users with a role from database *****/ - if (asprintf (&Query,"SELECT COUNT(*) FROM connected WHERE RoleInLastCrs=%u", - (unsigned) Role) < 0) - Lay_NotEnoughMemoryExit (); - return (unsigned) DB_QueryCOUNT_free (Query,"can not get number of connected users"); + DB_BuildQuery ("SELECT COUNT(*) FROM connected WHERE RoleInLastCrs=%u", + (unsigned) Role); + return (unsigned) DB_QueryCOUNT_new ("can not get number of connected users"); } /*****************************************************************************/ diff --git a/swad_country.c b/swad_country.c index b0a4793d7..d84c939f9 100644 --- a/swad_country.c +++ b/swad_country.c @@ -780,16 +780,11 @@ static void Cty_PutIconToEditCountries (void) static unsigned Cty_GetNumUsrsWhoClaimToBelongToCty (long CtyCod) { - char *Query; - /***** Get number of users from database *****/ - if (asprintf (&Query,"SELECT COUNT(*) FROM usr_data" - " WHERE CtyCod=%ld", - CtyCod) < 0) - Lay_NotEnoughMemoryExit (); - return (unsigned) DB_QueryCOUNT_free (Query,"can not get number of users" - " who claim to belong" - " to other countries"); + DB_BuildQuery ("SELECT COUNT(*) FROM usr_data WHERE CtyCod=%ld",CtyCod); + return (unsigned) DB_QueryCOUNT_new ("can not get number of users" + " who claim to belong" + " to other countries"); } /*****************************************************************************/ @@ -1826,14 +1821,10 @@ void Cty_RenameCountry (void) static bool Cty_CheckIfNumericCountryCodeExists (long CtyCod) { - char *Query; - /***** Get number of countries with a name from database *****/ - if (asprintf (&Query,"SELECT COUNT(*) FROM countries WHERE CtyCod='%03ld'", - CtyCod) < 0) - Lay_NotEnoughMemoryExit (); - return (DB_QueryCOUNT_free (Query,"can not check if the numeric code" - " of a country already existed") != 0); + DB_BuildQuery ("SELECT COUNT(*) FROM countries WHERE CtyCod='%03ld'",CtyCod); + return (DB_QueryCOUNT_new ("can not check if the numeric code" + " of a country already existed") != 0); } /*****************************************************************************/ @@ -1842,14 +1833,10 @@ static bool Cty_CheckIfNumericCountryCodeExists (long CtyCod) static bool Cty_CheckIfAlpha2CountryCodeExists (const char Alpha2[2 + 1]) { - char *Query; - /***** Get number of countries with a name from database *****/ - if (asprintf (&Query,"SELECT COUNT(*) FROM countries WHERE Alpha2='%s'", - Alpha2) < 0) - Lay_NotEnoughMemoryExit (); - return (DB_QueryCOUNT_free (Query,"can not check if the alphabetic code" - " of a country already existed") != 0); + DB_BuildQuery ("SELECT COUNT(*) FROM countries WHERE Alpha2='%s'",Alpha2); + return (DB_QueryCOUNT_new ("can not check if the alphabetic code" + " of a country already existed") != 0); } /*****************************************************************************/ @@ -1859,15 +1846,13 @@ static bool Cty_CheckIfAlpha2CountryCodeExists (const char Alpha2[2 + 1]) static bool Cty_CheckIfCountryNameExists (Txt_Language_t Language,const char *Name,long CtyCod) { extern const char *Txt_STR_LANG_ID[1 + Txt_NUM_LANGUAGES]; - char *Query; /***** Get number of countries with a name from database *****/ - if (asprintf (&Query,"SELECT COUNT(*) FROM countries" - " WHERE Name_%s='%s' AND CtyCod<>'%03ld'", - Txt_STR_LANG_ID[Language],Name,CtyCod) < 0) - Lay_NotEnoughMemoryExit (); - return (DB_QueryCOUNT_free (Query,"can not check if the name" - " of a country already existed") != 0); + DB_BuildQuery ("SELECT COUNT(*) FROM countries" + " WHERE Name_%s='%s' AND CtyCod<>'%03ld'", + Txt_STR_LANG_ID[Language],Name,CtyCod); + return (DB_QueryCOUNT_new ("can not check if the name" + " of a country already existed") != 0); } /*****************************************************************************/ @@ -2295,12 +2280,9 @@ static void Cty_CreateCountry (struct Country *Cty) unsigned Cty_GetNumCtysTotal (void) { - char *Query; - /***** Get total number of degrees from database *****/ - if (asprintf (&Query,"SELECT COUNT(*) FROM countries") < 0) - Lay_NotEnoughMemoryExit (); - return (unsigned) DB_QueryCOUNT_free (Query,"can not get the total number of countries"); + DB_BuildQuery ("SELECT COUNT(*) FROM countries"); + return (unsigned) DB_QueryCOUNT_new ("can not get the total number of countries"); } /*****************************************************************************/ @@ -2309,15 +2291,12 @@ unsigned Cty_GetNumCtysTotal (void) unsigned Cty_GetNumCtysWithInss (const char *SubQuery) { - char *Query; - /***** Get number of countries with institutions from database *****/ - if (asprintf (&Query,"SELECT COUNT(DISTINCT countries.CtyCod)" - " FROM countries,institutions" - " WHERE %scountries.CtyCod=institutions.CtyCod", - SubQuery) < 0) - Lay_NotEnoughMemoryExit (); - return (unsigned) DB_QueryCOUNT_free (Query,"can not get number of countries with institutions"); + DB_BuildQuery ("SELECT COUNT(DISTINCT countries.CtyCod)" + " FROM countries,institutions" + " WHERE %scountries.CtyCod=institutions.CtyCod", + SubQuery); + return (unsigned) DB_QueryCOUNT_new ("can not get number of countries with institutions"); } /*****************************************************************************/ @@ -2326,16 +2305,13 @@ unsigned Cty_GetNumCtysWithInss (const char *SubQuery) unsigned Cty_GetNumCtysWithCtrs (const char *SubQuery) { - char *Query; - /***** Get number of countries with centres from database *****/ - if (asprintf (&Query,"SELECT COUNT(DISTINCT countries.CtyCod)" - " FROM countries,institutions,centres" - " WHERE %scountries.CtyCod=institutions.CtyCod" - " AND institutions.InsCod=centres.InsCod", - SubQuery) < 0) - Lay_NotEnoughMemoryExit (); - return (unsigned) DB_QueryCOUNT_free (Query,"can not get number of countries with centres"); + DB_BuildQuery ("SELECT COUNT(DISTINCT countries.CtyCod)" + " FROM countries,institutions,centres" + " WHERE %scountries.CtyCod=institutions.CtyCod" + " AND institutions.InsCod=centres.InsCod", + SubQuery); + return (unsigned) DB_QueryCOUNT_new ("can not get number of countries with centres"); } /*****************************************************************************/ @@ -2344,17 +2320,14 @@ unsigned Cty_GetNumCtysWithCtrs (const char *SubQuery) unsigned Cty_GetNumCtysWithDegs (const char *SubQuery) { - char *Query; - /***** Get number of countries with degrees from database *****/ - if (asprintf (&Query,"SELECT COUNT(DISTINCT countries.CtyCod)" - " FROM countries,institutions,centres,degrees" - " WHERE %scountries.CtyCod=institutions.CtyCod" - " AND institutions.InsCod=centres.InsCod" - " AND centres.CtrCod=degrees.CtrCod", - SubQuery) < 0) - Lay_NotEnoughMemoryExit (); - return (unsigned) DB_QueryCOUNT_free (Query,"can not get number of countries with degrees"); + DB_BuildQuery ("SELECT COUNT(DISTINCT countries.CtyCod)" + " FROM countries,institutions,centres,degrees" + " WHERE %scountries.CtyCod=institutions.CtyCod" + " AND institutions.InsCod=centres.InsCod" + " AND centres.CtrCod=degrees.CtrCod", + SubQuery); + return (unsigned) DB_QueryCOUNT_new ("can not get number of countries with degrees"); } /*****************************************************************************/ @@ -2363,18 +2336,15 @@ unsigned Cty_GetNumCtysWithDegs (const char *SubQuery) unsigned Cty_GetNumCtysWithCrss (const char *SubQuery) { - char *Query; - /***** Get number of countries with courses from database *****/ - if (asprintf (&Query,"SELECT COUNT(DISTINCT countries.CtyCod)" - " FROM countries,institutions,centres,degrees,courses" - " WHERE %scountries.CtyCod=institutions.CtyCod" - " AND institutions.InsCod=centres.InsCod" - " AND centres.CtrCod=degrees.CtrCod" - " AND degrees.DegCod=courses.DegCod", - SubQuery) < 0) - Lay_NotEnoughMemoryExit (); - return (unsigned) DB_QueryCOUNT_free (Query,"can not get number of countries with courses"); + DB_BuildQuery ("SELECT COUNT(DISTINCT countries.CtyCod)" + " FROM countries,institutions,centres,degrees,courses" + " WHERE %scountries.CtyCod=institutions.CtyCod" + " AND institutions.InsCod=centres.InsCod" + " AND centres.CtrCod=degrees.CtrCod" + " AND degrees.DegCod=courses.DegCod", + SubQuery); + return (unsigned) DB_QueryCOUNT_new ("can not get number of countries with courses"); } /*****************************************************************************/ @@ -2383,20 +2353,17 @@ unsigned Cty_GetNumCtysWithCrss (const char *SubQuery) unsigned Cty_GetNumCtysWithUsrs (Rol_Role_t Role,const char *SubQuery) { - char *Query; - /***** Get number of countries with users from database *****/ - if (asprintf (&Query,"SELECT COUNT(DISTINCT countries.CtyCod)" - " FROM countries,institutions,centres,degrees,courses,crs_usr" - " WHERE %scountries.CtyCod=institutions.CtyCod" - " AND institutions.InsCod=centres.InsCod" - " AND centres.CtrCod=degrees.CtrCod" - " AND degrees.DegCod=courses.DegCod" - " AND courses.CrsCod=crs_usr.CrsCod" - " AND crs_usr.Role=%u", - SubQuery,(unsigned) Role) < 0) - Lay_NotEnoughMemoryExit (); - return (unsigned) DB_QueryCOUNT_free (Query,"can not get number of countries with users"); + DB_BuildQuery ("SELECT COUNT(DISTINCT countries.CtyCod)" + " FROM countries,institutions,centres,degrees,courses,crs_usr" + " WHERE %scountries.CtyCod=institutions.CtyCod" + " AND institutions.InsCod=centres.InsCod" + " AND centres.CtrCod=degrees.CtrCod" + " AND degrees.DegCod=courses.DegCod" + " AND courses.CrsCod=crs_usr.CrsCod" + " AND crs_usr.Role=%u", + SubQuery,(unsigned) Role); + return (unsigned) DB_QueryCOUNT_new ("can not get number of countries with users"); } /*****************************************************************************/ diff --git a/swad_course.c b/swad_course.c index e337ec81e..0a144ae95 100644 --- a/swad_course.c +++ b/swad_course.c @@ -793,12 +793,9 @@ static void Crs_WriteListMyCoursesToSelectOne (void) unsigned Crs_GetNumCrssTotal (void) { - char *Query; - /***** Get total number of courses from database *****/ - if (asprintf (&Query,"SELECT COUNT(*) FROM courses") < 0) - Lay_NotEnoughMemoryExit (); - return (unsigned) DB_QueryCOUNT_free (Query,"can not get the total number of courses"); + DB_BuildQuery ("SELECT COUNT(*) FROM courses"); + return (unsigned) DB_QueryCOUNT_new ("can not get the total number of courses"); } /*****************************************************************************/ @@ -807,17 +804,14 @@ unsigned Crs_GetNumCrssTotal (void) unsigned Crs_GetNumCrssInCty (long CtyCod) { - char *Query; - /***** Get number of courses in a country from database *****/ - if (asprintf (&Query,"SELECT COUNT(*) FROM institutions,centres,degrees,courses" - " WHERE institutions.CtyCod=%ld" - " AND institutions.InsCod=centres.InsCod" - " AND centres.CtrCod=degrees.CtrCod" - " AND degrees.DegCod=courses.DegCod", - CtyCod) < 0) - Lay_NotEnoughMemoryExit (); - return (unsigned) DB_QueryCOUNT_free (Query,"can not get the number of courses in a country"); + DB_BuildQuery ("SELECT COUNT(*) FROM institutions,centres,degrees,courses" + " WHERE institutions.CtyCod=%ld" + " AND institutions.InsCod=centres.InsCod" + " AND centres.CtrCod=degrees.CtrCod" + " AND degrees.DegCod=courses.DegCod", + CtyCod); + return (unsigned) DB_QueryCOUNT_new ("can not get the number of courses in a country"); } /*****************************************************************************/ @@ -826,16 +820,13 @@ unsigned Crs_GetNumCrssInCty (long CtyCod) unsigned Crs_GetNumCrssInIns (long InsCod) { - char *Query; - /***** Get number of courses in a degree from database *****/ - if (asprintf (&Query,"SELECT COUNT(*) FROM centres,degrees,courses" - " WHERE centres.InsCod=%ld" - " AND centres.CtrCod=degrees.CtrCod" - " AND degrees.DegCod=courses.DegCod", - InsCod) < 0) - Lay_NotEnoughMemoryExit (); - return (unsigned) DB_QueryCOUNT_free (Query,"can not get the number of courses in an institution"); + DB_BuildQuery ("SELECT COUNT(*) FROM centres,degrees,courses" + " WHERE centres.InsCod=%ld" + " AND centres.CtrCod=degrees.CtrCod" + " AND degrees.DegCod=courses.DegCod", + InsCod); + return (unsigned) DB_QueryCOUNT_new ("can not get the number of courses in an institution"); } /*****************************************************************************/ @@ -844,15 +835,12 @@ unsigned Crs_GetNumCrssInIns (long InsCod) unsigned Crs_GetNumCrssInCtr (long CtrCod) { - char *Query; - /***** Get number of courses in a degree from database *****/ - if (asprintf (&Query,"SELECT COUNT(*) FROM degrees,courses" - " WHERE degrees.CtrCod=%ld" - " AND degrees.DegCod=courses.DegCod", - CtrCod) < 0) - Lay_NotEnoughMemoryExit (); - return (unsigned) DB_QueryCOUNT_free (Query,"can not get the number of courses in a centre"); + DB_BuildQuery ("SELECT COUNT(*) FROM degrees,courses" + " WHERE degrees.CtrCod=%ld" + " AND degrees.DegCod=courses.DegCod", + CtrCod); + return (unsigned) DB_QueryCOUNT_new ("can not get the number of courses in a centre"); } /*****************************************************************************/ @@ -861,14 +849,9 @@ unsigned Crs_GetNumCrssInCtr (long CtrCod) unsigned Crs_GetNumCrssInDeg (long DegCod) { - char *Query; - /***** Get number of courses in a degree from database *****/ - if (asprintf (&Query,"SELECT COUNT(*) FROM courses" - " WHERE DegCod=%ld", - DegCod) < 0) - Lay_NotEnoughMemoryExit (); - return (unsigned) DB_QueryCOUNT_free (Query,"can not get the number of courses in a degree"); + DB_BuildQuery ("SELECT COUNT(*) FROM courses WHERE DegCod=%ld",DegCod); + return (unsigned) DB_QueryCOUNT_new ("can not get the number of courses in a degree"); } /*****************************************************************************/ @@ -877,19 +860,16 @@ unsigned Crs_GetNumCrssInDeg (long DegCod) unsigned Crs_GetNumCrssWithUsrs (Rol_Role_t Role,const char *SubQuery) { - char *Query; - /***** Get number of degrees with users from database *****/ - if (asprintf (&Query,"SELECT COUNT(DISTINCT courses.CrsCod)" - " FROM institutions,centres,degrees,courses,crs_usr" - " WHERE %sinstitutions.InsCod=centres.InsCod" - " AND centres.CtrCod=degrees.CtrCod" - " AND degrees.DegCod=courses.DegCod" - " AND courses.CrsCod=crs_usr.CrsCod" - " AND crs_usr.Role=%u", - SubQuery,(unsigned) Role) < 0) - Lay_NotEnoughMemoryExit (); - return (unsigned) DB_QueryCOUNT_free (Query,"can not get number of courses with users"); + DB_BuildQuery ("SELECT COUNT(DISTINCT courses.CrsCod)" + " FROM institutions,centres,degrees,courses,crs_usr" + " WHERE %sinstitutions.InsCod=centres.InsCod" + " AND centres.CtrCod=degrees.CtrCod" + " AND degrees.DegCod=courses.DegCod" + " AND courses.CrsCod=crs_usr.CrsCod" + " AND crs_usr.Role=%u", + SubQuery,(unsigned) Role); + return (unsigned) DB_QueryCOUNT_new ("can not get number of courses with users"); } /*****************************************************************************/ @@ -2784,16 +2764,13 @@ static void Crs_RenameCourse (struct Course *Crs,Cns_ShrtOrFullName_t ShrtOrFull static bool Crs_CheckIfCrsNameExistsInYearOfDeg (const char *FieldName,const char *Name,long CrsCod, long DegCod,unsigned Year) { - char *Query; - /***** Get number of courses in a year of a degree and with a name from database *****/ - if (asprintf (&Query,"SELECT COUNT(*) FROM courses" - " WHERE DegCod=%ld AND Year=%u" - " AND %s='%s' AND CrsCod<>%ld", - DegCod,Year,FieldName,Name,CrsCod) < 0) - Lay_NotEnoughMemoryExit (); - return (DB_QueryCOUNT_free (Query,"can not check if the name" - " of a course already existed") != 0); + DB_BuildQuery ("SELECT COUNT(*) FROM courses" + " WHERE DegCod=%ld AND Year=%u" + " AND %s='%s' AND CrsCod<>%ld", + DegCod,Year,FieldName,Name,CrsCod); + return (DB_QueryCOUNT_new ("can not check if the name" + " of a course already existed") != 0); } /*****************************************************************************/ diff --git a/swad_database.c b/swad_database.c index f04516eb7..255b85a1f 100644 --- a/swad_database.c +++ b/swad_database.c @@ -3128,10 +3128,6 @@ unsigned long DB_QuerySELECT_new (MYSQL_RES **mysql_res,const char *MsgError) return (unsigned long) mysql_num_rows (*mysql_res); } -/*****************************************************************************/ -/******************** Make a SELECT query from database **********************/ -/*****************************************************************************/ - unsigned long DB_QuerySELECT_free (const char *Query,MYSQL_RES **mysql_res,const char *MsgError) { int Result; @@ -3168,6 +3164,26 @@ unsigned long DB_QuerySELECT (const char *Query,MYSQL_RES **mysql_res,const char /**************** Make a SELECT COUNT query from database ********************/ /*****************************************************************************/ +unsigned long DB_QueryCOUNT_new (const char *MsgError) + { + MYSQL_RES *mysql_res; + MYSQL_ROW row; + unsigned long NumRows; + + /***** Make query "SELECT COUNT(*) FROM..." *****/ + DB_QuerySELECT_new (&mysql_res,MsgError); + + /***** Get number of rows *****/ + row = mysql_fetch_row (mysql_res); + if (sscanf (row[0],"%lu",&NumRows) != 1) + Lay_ShowErrorAndExit ("Error when counting number of rows."); + + /***** Free structure that stores the query result *****/ + DB_FreeMySQLResult (&mysql_res); + + return NumRows; + } + unsigned long DB_QueryCOUNT_free (const char *Query,const char *MsgError) { MYSQL_RES *mysql_res; diff --git a/swad_database.h b/swad_database.h index 364c4057d..33414b187 100644 --- a/swad_database.h +++ b/swad_database.h @@ -43,6 +43,7 @@ unsigned long DB_QuerySELECT_new (MYSQL_RES **mysql_res,const char *MsgError); unsigned long DB_QuerySELECT_free (const char *Query,MYSQL_RES **mysql_res,const char *MsgError); unsigned long DB_QuerySELECT (const char *Query,MYSQL_RES **mysql_res,const char *MsgError); +unsigned long DB_QueryCOUNT_new (const char *MsgError); unsigned long DB_QueryCOUNT_free (const char *Query,const char *MsgError); unsigned long DB_QueryCOUNT (const char *Query,const char *MsgError); diff --git a/swad_degree.c b/swad_degree.c index 0968d7943..c63ac6005 100644 --- a/swad_degree.c +++ b/swad_degree.c @@ -2019,14 +2019,11 @@ static void Deg_RenameDegree (struct Degree *Deg,Cns_ShrtOrFullName_t ShrtOrFull static bool Deg_CheckIfDegNameExistsInCtr (const char *FieldName,const char *Name, long DegCod,long CtrCod) { - char *Query; - /***** Get number of degrees with a type and a name from database *****/ - if (asprintf (&Query,"SELECT COUNT(*) FROM degrees" - " WHERE CtrCod=%ld AND %s='%s' AND DegCod<>%ld", - CtrCod,FieldName,Name,DegCod) < 0) - Lay_NotEnoughMemoryExit (); - return (DB_QueryCOUNT_free (Query,"can not check if the name of a degree already existed") != 0); + DB_BuildQuery ("SELECT COUNT(*) FROM degrees" + " WHERE CtrCod=%ld AND %s='%s' AND DegCod<>%ld", + CtrCod,FieldName,Name,DegCod); + return (DB_QueryCOUNT_new ("can not check if the name of a degree already existed") != 0); } /*****************************************************************************/ @@ -2343,12 +2340,9 @@ void Deg_RemoveLogo (void) unsigned Deg_GetNumDegsTotal (void) { - char *Query; - /***** Get total number of degrees from database *****/ - if (asprintf (&Query,"SELECT COUNT(*) FROM degrees") < 0) - Lay_NotEnoughMemoryExit (); - return (unsigned) DB_QueryCOUNT_free (Query,"can not get the total number of degrees"); + DB_BuildQuery ("SELECT COUNT(*) FROM degrees"); + return (unsigned) DB_QueryCOUNT_new ("can not get the total number of degrees"); } /*****************************************************************************/ @@ -2357,17 +2351,14 @@ unsigned Deg_GetNumDegsTotal (void) unsigned Deg_GetNumDegsInCty (long InsCod) { - char *Query; - /***** Get number of degrees in a country from database *****/ - if (asprintf (&Query,"SELECT COUNT(*) FROM institutions,centres,degrees" - " WHERE institutions.CtyCod=%ld" - " AND institutions.InsCod=centres.InsCod" - " AND centres.CtrCod=degrees.CtrCod", - InsCod) < 0) - Lay_NotEnoughMemoryExit (); - return (unsigned) DB_QueryCOUNT_free (Query,"can not get the number of degrees" - " in a country"); + DB_BuildQuery ("SELECT COUNT(*) FROM institutions,centres,degrees" + " WHERE institutions.CtyCod=%ld" + " AND institutions.InsCod=centres.InsCod" + " AND centres.CtrCod=degrees.CtrCod", + InsCod); + return (unsigned) DB_QueryCOUNT_new ("can not get the number of degrees" + " in a country"); } /*****************************************************************************/ @@ -2376,16 +2367,13 @@ unsigned Deg_GetNumDegsInCty (long InsCod) unsigned Deg_GetNumDegsInIns (long InsCod) { - char *Query; - /***** Get number of degrees in an institution from database *****/ - if (asprintf (&Query,"SELECT COUNT(*) FROM centres,degrees" - " WHERE centres.InsCod=%ld" - " AND centres.CtrCod=degrees.CtrCod", - InsCod) < 0) - Lay_NotEnoughMemoryExit (); - return (unsigned) DB_QueryCOUNT_free (Query,"can not get the number of degrees" - " in an institution"); + DB_BuildQuery ("SELECT COUNT(*) FROM centres,degrees" + " WHERE centres.InsCod=%ld" + " AND centres.CtrCod=degrees.CtrCod", + InsCod); + return (unsigned) DB_QueryCOUNT_new ("can not get the number of degrees" + " in an institution"); } /*****************************************************************************/ @@ -2394,14 +2382,10 @@ unsigned Deg_GetNumDegsInIns (long InsCod) unsigned Deg_GetNumDegsInCtr (long CtrCod) { - char *Query; - /***** Get number of degrees in a centre from database *****/ - if (asprintf (&Query,"SELECT COUNT(*) FROM degrees WHERE CtrCod=%ld", - CtrCod) < 0) - Lay_NotEnoughMemoryExit (); - return (unsigned) DB_QueryCOUNT_free (Query,"can not get the number of degrees" - " in a centre"); + DB_BuildQuery ("SELECT COUNT(*) FROM degrees WHERE CtrCod=%ld",CtrCod); + return (unsigned) DB_QueryCOUNT_new ("can not get the number of degrees" + " in a centre"); } /*****************************************************************************/ @@ -2410,18 +2394,15 @@ unsigned Deg_GetNumDegsInCtr (long CtrCod) unsigned Deg_GetNumDegsWithCrss (const char *SubQuery) { - char *Query; - /***** Get number of degrees with courses from database *****/ - if (asprintf (&Query,"SELECT COUNT(DISTINCT degrees.DegCod)" - " FROM institutions,centres,degrees,courses" - " WHERE %sinstitutions.InsCod=centres.InsCod" - " AND centres.CtrCod=degrees.CtrCod" - " AND degrees.DegCod=courses.DegCod", - SubQuery) < 0) - Lay_NotEnoughMemoryExit (); - return (unsigned) DB_QueryCOUNT_free (Query,"can not get number of degrees" - " with courses"); + DB_BuildQuery ("SELECT COUNT(DISTINCT degrees.DegCod)" + " FROM institutions,centres,degrees,courses" + " WHERE %sinstitutions.InsCod=centres.InsCod" + " AND centres.CtrCod=degrees.CtrCod" + " AND degrees.DegCod=courses.DegCod", + SubQuery); + return (unsigned) DB_QueryCOUNT_new ("can not get number of degrees" + " with courses"); } /*****************************************************************************/ @@ -2430,20 +2411,17 @@ unsigned Deg_GetNumDegsWithCrss (const char *SubQuery) unsigned Deg_GetNumDegsWithUsrs (Rol_Role_t Role,const char *SubQuery) { - char *Query; - /***** Get number of degrees with users from database *****/ - if (asprintf (&Query,"SELECT COUNT(DISTINCT degrees.DegCod)" - " FROM institutions,centres,degrees,courses,crs_usr" - " WHERE %sinstitutions.InsCod=centres.InsCod" - " AND centres.CtrCod=degrees.CtrCod" - " AND degrees.DegCod=courses.DegCod" - " AND courses.CrsCod=crs_usr.CrsCod" - " AND crs_usr.Role=%u", - SubQuery,(unsigned) Role) < 0) - Lay_NotEnoughMemoryExit (); - return (unsigned) DB_QueryCOUNT_free (Query,"can not get number of degrees" - " with users"); + DB_BuildQuery ("SELECT COUNT(DISTINCT degrees.DegCod)" + " FROM institutions,centres,degrees,courses,crs_usr" + " WHERE %sinstitutions.InsCod=centres.InsCod" + " AND centres.CtrCod=degrees.CtrCod" + " AND degrees.DegCod=courses.DegCod" + " AND courses.CrsCod=crs_usr.CrsCod" + " AND crs_usr.Role=%u", + SubQuery,(unsigned) Role); + return (unsigned) DB_QueryCOUNT_new ("can not get number of degrees" + " with users"); } /*****************************************************************************/ diff --git a/swad_degree_type.c b/swad_degree_type.c index 3c8c06c74..03705c30d 100644 --- a/swad_degree_type.c +++ b/swad_degree_type.c @@ -803,13 +803,9 @@ long DT_GetAndCheckParamOtherDegTypCod (long MinCodAllowed) static unsigned DT_CountNumDegsOfType (long DegTypCod) { - char *Query; - /***** Get number of degrees of a type from database *****/ - if (asprintf (&Query,"SELECT COUNT(*) FROM degrees WHERE DegTypCod=%ld", - DegTypCod) < 0) - Lay_NotEnoughMemoryExit (); - return (unsigned) DB_QueryCOUNT_free (Query,"can not get number of degrees of a type"); + DB_BuildQuery ("SELECT COUNT(*) FROM degrees WHERE DegTypCod=%ld",DegTypCod); + return (unsigned) DB_QueryCOUNT_new ("can not get number of degrees of a type"); } /*****************************************************************************/ @@ -995,14 +991,11 @@ void DT_RenameDegreeType (void) static bool DT_CheckIfDegreeTypeNameExists (const char *DegTypName,long DegTypCod) { - char *Query; - /***** Get number of degree types with a name from database *****/ - if (asprintf (&Query,"SELECT COUNT(*) FROM deg_types" - " WHERE DegTypName='%s' AND DegTypCod<>%ld", - DegTypName,DegTypCod) < 0) - Lay_NotEnoughMemoryExit (); - return (DB_QueryCOUNT_free (Query,"can not check if the name of a type of degree already existed") != 0); + DB_BuildQuery ("SELECT COUNT(*) FROM deg_types" + " WHERE DegTypName='%s' AND DegTypCod<>%ld", + DegTypName,DegTypCod); + return (DB_QueryCOUNT_new ("can not check if the name of a type of degree already existed") != 0); } /*****************************************************************************/ diff --git a/swad_department.c b/swad_department.c index 7e25edc93..e748c48c3 100644 --- a/swad_department.c +++ b/swad_department.c @@ -445,14 +445,10 @@ void Dpt_FreeListDepartments (void) unsigned Dpt_GetNumDepartmentsInInstitution (long InsCod) { - char *Query; - /***** Get number of departments in an institution from database *****/ - if (asprintf (&Query,"SELECT COUNT(*) FROM departments WHERE InsCod=%ld", - InsCod) < 0) - Lay_NotEnoughMemoryExit (); - return (unsigned) DB_QueryCOUNT_free (Query,"can not get number of departments" - " in an institution"); + DB_BuildQuery ("SELECT COUNT(*) FROM departments WHERE InsCod=%ld",InsCod); + return (unsigned) DB_QueryCOUNT_new ("can not get number of departments" + " in an institution"); } /*****************************************************************************/ @@ -789,15 +785,12 @@ static void Dpt_RenameDepartment (Cns_ShrtOrFullName_t ShrtOrFullName) static bool Dpt_CheckIfDepartmentNameExists (const char *FieldName,const char *Name,long DptCod) { - char *Query; - /***** Get number of departments with a name from database *****/ - if (asprintf (&Query,"SELECT COUNT(*) FROM departments" - " WHERE %s='%s' AND DptCod<>%ld", - FieldName,Name,DptCod) < 0) - Lay_NotEnoughMemoryExit (); - return (DB_QueryCOUNT_free (Query,"can not check if the name" - " of a department already existed") != 0); + DB_BuildQuery ("SELECT COUNT(*) FROM departments" + " WHERE %s='%s' AND DptCod<>%ld", + FieldName,Name,DptCod); + return (DB_QueryCOUNT_new ("can not check if the name" + " of a department already existed") != 0); } /*****************************************************************************/ @@ -1092,12 +1085,9 @@ static void Dpt_CreateDepartment (struct Department *Dpt) unsigned Dpt_GetTotalNumberOfDepartments (void) { - char *Query; - /***** Get number of departments from database *****/ - if (asprintf (&Query,"SELECT COUNT(*) FROM departments") < 0) - Lay_NotEnoughMemoryExit (); - return (unsigned) DB_QueryCOUNT_free (Query,"can not get number of departments"); + DB_BuildQuery ("SELECT COUNT(*) FROM departments"); + return (unsigned) DB_QueryCOUNT_new ("can not get number of departments"); } /*****************************************************************************/ @@ -1106,13 +1096,9 @@ unsigned Dpt_GetTotalNumberOfDepartments (void) unsigned Dpt_GetNumDptsInIns (long InsCod) { - char *Query; - /***** Get departments in an institution from database *****/ - if (asprintf (&Query,"SELECT COUNT(*) FROM departments WHERE InsCod=%ld", - InsCod) < 0) - Lay_NotEnoughMemoryExit (); - return (unsigned) DB_QueryCOUNT_free (Query,"can not get number of departments in an institution"); + DB_BuildQuery ("SELECT COUNT(*) FROM departments WHERE InsCod=%ld",InsCod); + return (unsigned) DB_QueryCOUNT_new ("can not get number of departments in an institution"); } /*****************************************************************************/ diff --git a/swad_duplicate.c b/swad_duplicate.c index 57662d8b6..7340578d5 100644 --- a/swad_duplicate.c +++ b/swad_duplicate.c @@ -390,12 +390,9 @@ static void Dup_ListSimilarUsrs (void) static bool Dup_CheckIfUsrIsDup (long UsrCod) { - char *Query; - - if (asprintf (&Query,"SELECT COUNT(*) FROM usr_duplicated WHERE UsrCod=%ld", - UsrCod) < 0) - Lay_NotEnoughMemoryExit (); - return (DB_QueryCOUNT_free (Query,"can not if user is in list of possible duplicate users") != 0); + DB_BuildQuery ("SELECT COUNT(*) FROM usr_duplicated WHERE UsrCod=%ld", + UsrCod); + return (DB_QueryCOUNT_new ("can not if user is in list of possible duplicate users") != 0); } /*****************************************************************************/ diff --git a/swad_file_browser.c b/swad_file_browser.c index 1abc0439f..ff2e38058 100644 --- a/swad_file_browser.c +++ b/swad_file_browser.c @@ -8278,48 +8278,38 @@ static bool Brw_GetIfExpandedTree (const char Path[PATH_MAX + 1]) { long Cod = Brw_GetCodForExpandedFolders (); long WorksUsrCod = Brw_GetWorksUsrCodForExpandedFolders (); - char *Query; Brw_FileBrowser_t FileBrowserForExpandedFolders = Brw_FileBrowserForDB_expanded_folders[Gbl.FileBrowser.Type]; /***** Get if a folder is expanded from database *****/ if (Cod > 0) { if (WorksUsrCod > 0) - { - if (asprintf (&Query,"SELECT COUNT(*) FROM expanded_folders" - " WHERE UsrCod=%ld AND FileBrowser=%u" - " AND Cod=%ld AND WorksUsrCod=%ld" - " AND Path='%s/'", - Gbl.Usrs.Me.UsrDat.UsrCod, - (unsigned) FileBrowserForExpandedFolders, - Cod,WorksUsrCod, - Path) < 0) - Lay_NotEnoughMemoryExit (); - } + DB_BuildQuery ("SELECT COUNT(*) FROM expanded_folders" + " WHERE UsrCod=%ld AND FileBrowser=%u" + " AND Cod=%ld AND WorksUsrCod=%ld" + " AND Path='%s/'", + Gbl.Usrs.Me.UsrDat.UsrCod, + (unsigned) FileBrowserForExpandedFolders, + Cod,WorksUsrCod, + Path); else - { - if (asprintf (&Query,"SELECT COUNT(*) FROM expanded_folders" - " WHERE UsrCod=%ld AND FileBrowser=%u" - " AND Cod=%ld" - " AND Path='%s/'", - Gbl.Usrs.Me.UsrDat.UsrCod, - (unsigned) FileBrowserForExpandedFolders, - Cod, - Path) < 0) - Lay_NotEnoughMemoryExit (); - } + DB_BuildQuery ("SELECT COUNT(*) FROM expanded_folders" + " WHERE UsrCod=%ld AND FileBrowser=%u" + " AND Cod=%ld" + " AND Path='%s/'", + Gbl.Usrs.Me.UsrDat.UsrCod, + (unsigned) FileBrowserForExpandedFolders, + Cod, + Path); } else // Briefcase - { - if (asprintf (&Query,"SELECT COUNT(*) FROM expanded_folders" - " WHERE UsrCod=%ld AND FileBrowser=%u" - " AND Path='%s/'", - Gbl.Usrs.Me.UsrDat.UsrCod, - (unsigned) FileBrowserForExpandedFolders, - Path) < 0) - Lay_NotEnoughMemoryExit (); - } - return (DB_QueryCOUNT_free (Query,"can not get check if a folder is expanded") != 0); + DB_BuildQuery ("SELECT COUNT(*) FROM expanded_folders" + " WHERE UsrCod=%ld AND FileBrowser=%u" + " AND Path='%s/'", + Gbl.Usrs.Me.UsrDat.UsrCod, + (unsigned) FileBrowserForExpandedFolders, + Path); + return (DB_QueryCOUNT_new ("can not get check if a folder is expanded") != 0); } /*****************************************************************************/ @@ -10186,8 +10176,6 @@ bool Brw_CheckIfFileOrFolderIsSetAsHiddenInDB (Brw_FileType_t FileType,const cha bool Brw_CheckIfFileOrFolderIsHidden (struct FileMetadata *FileMetadata) { - char *Query; - /***** Get if a file or folder is under a hidden folder from database *****/ /* The argument Path passed to this function is hidden if: @@ -10195,18 +10183,17 @@ bool Brw_CheckIfFileOrFolderIsHidden (struct FileMetadata *FileMetadata) or 2) the argument Path begins by 'x/', where x is a path stored in database */ - if (asprintf (&Query,"SELECT COUNT(*) FROM files" - " WHERE FileBrowser=%u AND Cod=%ld AND ZoneUsrCod=%ld" - " AND Hidden='Y'" - " AND (Path='%s' OR LOCATE(CONCAT(Path,'/'),'%s')=1)", - FileMetadata->FileBrowser, - FileMetadata->Cod, - FileMetadata->ZoneUsrCod, - FileMetadata->FullPathInTree, - FileMetadata->FullPathInTree) < 0) - Lay_NotEnoughMemoryExit (); + DB_BuildQuery ("SELECT COUNT(*) FROM files" + " WHERE FileBrowser=%u AND Cod=%ld AND ZoneUsrCod=%ld" + " AND Hidden='Y'" + " AND (Path='%s' OR LOCATE(CONCAT(Path,'/'),'%s')=1)", + FileMetadata->FileBrowser, + FileMetadata->Cod, + FileMetadata->ZoneUsrCod, + FileMetadata->FullPathInTree, + FileMetadata->FullPathInTree); - return (DB_QueryCOUNT_free (Query,"can not check if a file or folder is hidden") != 0); + return (DB_QueryCOUNT_new ("can not check if a file or folder is hidden") != 0); } /*****************************************************************************/ @@ -11720,17 +11707,15 @@ static bool Brw_GetIfFolderHasPublicFiles (const char Path[PATH_MAX + 1]) { long Cod = Brw_GetCodForFiles (); long ZoneUsrCod = Brw_GetZoneUsrCodForFiles (); - char *Query; /***** Get if a file or folder is public from database *****/ - if (asprintf (&Query,"SELECT COUNT(*) FROM files" - " WHERE FileBrowser=%u AND Cod=%ld AND ZoneUsrCod=%ld" - " AND Path LIKE '%s/%%' AND Public='Y'", - (unsigned) Brw_FileBrowserForDB_files[Gbl.FileBrowser.Type], - Cod,ZoneUsrCod, - Path) < 0) - Lay_NotEnoughMemoryExit (); - return (DB_QueryCOUNT_free (Query,"can not check if a folder contains public files") != 0); + DB_BuildQuery ("SELECT COUNT(*) FROM files" + " WHERE FileBrowser=%u AND Cod=%ld AND ZoneUsrCod=%ld" + " AND Path LIKE '%s/%%' AND Public='Y'", + (unsigned) Brw_FileBrowserForDB_files[Gbl.FileBrowser.Type], + Cod,ZoneUsrCod, + Path); + return (DB_QueryCOUNT_new ("can not check if a folder contains public files") != 0); } /*****************************************************************************/ @@ -11739,16 +11724,13 @@ static bool Brw_GetIfFolderHasPublicFiles (const char Path[PATH_MAX + 1]) unsigned Brw_GetNumFilesUsr (long UsrCod) { - char *Query; - /***** Get current number of files published by a user from database *****/ - if (asprintf (&Query,"SELECT COUNT(*) FROM files" - " WHERE PublisherUsrCod=%ld AND FileType IN (%u,%u)", - UsrCod, - (unsigned) Brw_IS_FILE, - (unsigned) Brw_IS_UNKNOWN) < 0) // Unknown entries are counted as files - Lay_NotEnoughMemoryExit (); - return (unsigned) DB_QueryCOUNT_free (Query,"can not get number of files from a user"); + DB_BuildQuery ("SELECT COUNT(*) FROM files" + " WHERE PublisherUsrCod=%ld AND FileType IN (%u,%u)", + UsrCod, + (unsigned) Brw_IS_FILE, + (unsigned) Brw_IS_UNKNOWN); // Unknown entries are counted as files + return (unsigned) DB_QueryCOUNT_new ("can not get number of files from a user"); } /*****************************************************************************/ @@ -11757,17 +11739,14 @@ unsigned Brw_GetNumFilesUsr (long UsrCod) unsigned Brw_GetNumPublicFilesUsr (long UsrCod) { - char *Query; - /***** Get current number of public files published by a user from database *****/ - if (asprintf (&Query,"SELECT COUNT(*) FROM files" - " WHERE PublisherUsrCod=%ld AND FileType IN (%u,%u)" - " AND Public='Y'", - UsrCod, - (unsigned) Brw_IS_FILE, - (unsigned) Brw_IS_UNKNOWN) < 0) // Unknown entries are counted as files - Lay_NotEnoughMemoryExit (); - return (unsigned) DB_QueryCOUNT_free (Query,"can not get number of public files from a user"); + DB_BuildQuery ("SELECT COUNT(*) FROM files" + " WHERE PublisherUsrCod=%ld AND FileType IN (%u,%u)" + " AND Public='Y'", + UsrCod, + (unsigned) Brw_IS_FILE, + (unsigned) Brw_IS_UNKNOWN); // Unknown entries are counted as files + return (unsigned) DB_QueryCOUNT_new ("can not get number of public files from a user"); } /*****************************************************************************/ diff --git a/swad_follow.c b/swad_follow.c index 04e63ca6b..7cab91ff8 100644 --- a/swad_follow.c +++ b/swad_follow.c @@ -442,17 +442,14 @@ static void Fol_PutIconToUpdateWhoToFollow (void) bool Fol_CheckUsrIsFollowerOf (long FollowerCod,long FollowedCod) { - char *Query; - if (FollowerCod == FollowedCod) return false; /***** Check if a user is a follower of another user *****/ - if (asprintf (&Query,"SELECT COUNT(*) FROM usr_follow" - " WHERE FollowerCod=%ld AND FollowedCod=%ld", - FollowerCod,FollowedCod) < 0) - Lay_NotEnoughMemoryExit (); - return (DB_QueryCOUNT_free (Query,"can not get if a user is a follower of another one") != 0); + DB_BuildQuery ("SELECT COUNT(*) FROM usr_follow" + " WHERE FollowerCod=%ld AND FollowedCod=%ld", + FollowerCod,FollowedCod); + return (DB_QueryCOUNT_new ("can not get if a user is a follower of another one") != 0); } /*****************************************************************************/ @@ -461,13 +458,10 @@ bool Fol_CheckUsrIsFollowerOf (long FollowerCod,long FollowedCod) unsigned Fol_GetNumFollowing (long UsrCod) { - char *Query; - /***** Check if a user is a follower of another user *****/ - if (asprintf (&Query,"SELECT COUNT(*) FROM usr_follow WHERE FollowerCod=%ld", - UsrCod) < 0) - Lay_NotEnoughMemoryExit (); - return DB_QueryCOUNT_free (Query,"can not get number of followed"); + DB_BuildQuery ("SELECT COUNT(*) FROM usr_follow WHERE FollowerCod=%ld", + UsrCod); + return DB_QueryCOUNT_new ("can not get number of followed"); } /*****************************************************************************/ @@ -476,13 +470,10 @@ unsigned Fol_GetNumFollowing (long UsrCod) unsigned Fol_GetNumFollowers (long UsrCod) { - char *Query; - /***** Check if a user is a follower of another user *****/ - if (asprintf (&Query,"SELECT COUNT(*) FROM usr_follow WHERE FollowedCod=%ld", - UsrCod) < 0) - Lay_NotEnoughMemoryExit (); - return DB_QueryCOUNT_free (Query,"can not get number of followers"); + DB_BuildQuery ("SELECT COUNT(*) FROM usr_follow WHERE FollowedCod=%ld", + UsrCod); + return DB_QueryCOUNT_new ("can not get number of followers"); } /*****************************************************************************/ diff --git a/swad_forum.c b/swad_forum.c index 057fb7d9b..a6729ed93 100644 --- a/swad_forum.c +++ b/swad_forum.c @@ -440,13 +440,9 @@ void For_DisablePost (void) static bool For_GetIfForumPstExists (long PstCod) { - char *Query; - /***** Get if a forum post exists from database *****/ - if (asprintf (&Query,"SELECT COUNT(*) FROM forum_post WHERE PstCod=%ld", - PstCod) < 0) - Lay_NotEnoughMemoryExit (); - return (DB_QueryCOUNT_free (Query,"can not check if a post of a forum already existed") != 0); // Post exists if it appears in table of forum posts + DB_BuildQuery ("SELECT COUNT(*) FROM forum_post WHERE PstCod=%ld",PstCod); + return (DB_QueryCOUNT_new ("can not check if a post of a forum already existed") != 0); // Post exists if it appears in table of forum posts } /*****************************************************************************/ @@ -455,16 +451,13 @@ static bool For_GetIfForumPstExists (long PstCod) static bool For_GetIfPstIsEnabled (long PstCod) { - char *Query; - if (PstCod > 0) { /***** Get if post is disabled from database *****/ - if (asprintf (&Query,"SELECT COUNT(*) FROM forum_disabled_post" - " WHERE PstCod=%ld", - PstCod) < 0) - Lay_NotEnoughMemoryExit (); - return (DB_QueryCOUNT_free (Query,"can not check if a post of a forum is disabled") == 0); // Post is enabled if it does not appear in table of disabled posts + DB_BuildQuery ("SELECT COUNT(*) FROM forum_disabled_post" + " WHERE PstCod=%ld", + PstCod); + return (DB_QueryCOUNT_new ("can not check if a post of a forum is disabled") == 0); // Post is enabled if it does not appear in table of disabled posts } else return false; @@ -825,14 +818,11 @@ static void For_UpdateThrReadTime (long ThrCod, static unsigned For_GetNumOfReadersOfThr (long ThrCod) { - char *Query; - /***** Get number of distinct readers of a thread from database *****/ - if (asprintf (&Query,"SELECT COUNT(*) FROM forum_thr_read WHERE ThrCod=%ld", - ThrCod) < 0) - Lay_NotEnoughMemoryExit (); - return (unsigned) DB_QueryCOUNT_free (Query,"can not get the number of readers" - " of a thread of a forum"); + DB_BuildQuery ("SELECT COUNT(*) FROM forum_thr_read WHERE ThrCod=%ld", + ThrCod); + return (unsigned) DB_QueryCOUNT_new ("can not get the number of readers" + " of a thread of a forum"); } /*****************************************************************************/ @@ -871,14 +861,10 @@ static unsigned For_GetNumOfWritersInThr (long ThrCod) static unsigned For_GetNumPstsInThr (long ThrCod) { - char *Query; - /***** Get number of posts in a thread from database *****/ - if (asprintf (&Query,"SELECT COUNT(*) FROM forum_post WHERE ThrCod=%ld", - ThrCod) < 0) - Lay_NotEnoughMemoryExit (); - return (unsigned) DB_QueryCOUNT_free (Query,"can not get the number of posts" - " in a thread of a forum"); + DB_BuildQuery ("SELECT COUNT(*) FROM forum_post WHERE ThrCod=%ld",ThrCod); + return (unsigned) DB_QueryCOUNT_new ("can not get the number of posts" + " in a thread of a forum"); } /*****************************************************************************/ @@ -887,15 +873,12 @@ static unsigned For_GetNumPstsInThr (long ThrCod) static unsigned For_GetNumMyPstInThr (long ThrCod) { - char *Query; - /***** Get if I have write posts in a thread from database *****/ - if (asprintf (&Query,"SELECT COUNT(*) FROM forum_post" - " WHERE ThrCod=%ld AND UsrCod=%ld", - ThrCod,Gbl.Usrs.Me.UsrDat.UsrCod) < 0) - Lay_NotEnoughMemoryExit (); - return (unsigned) DB_QueryCOUNT_free (Query,"can not check if you have written" - " posts in a thead of a forum"); + DB_BuildQuery ("SELECT COUNT(*) FROM forum_post" + " WHERE ThrCod=%ld AND UsrCod=%ld", + ThrCod,Gbl.Usrs.Me.UsrDat.UsrCod); + return (unsigned) DB_QueryCOUNT_new ("can not check if you have written" + " posts in a thead of a forum"); } /*****************************************************************************/ @@ -904,13 +887,9 @@ static unsigned For_GetNumMyPstInThr (long ThrCod) unsigned long For_GetNumPostsUsr (long UsrCod) { - char *Query; - /***** Get number of posts from a user from database *****/ - if (asprintf (&Query,"SELECT COUNT(*) FROM forum_post WHERE UsrCod=%ld", - UsrCod) < 0) - Lay_NotEnoughMemoryExit (); - return DB_QueryCOUNT_free (Query,"can not number of posts from a user"); + DB_BuildQuery ("SELECT COUNT(*) FROM forum_post WHERE UsrCod=%ld",UsrCod); + return DB_QueryCOUNT_new ("can not number of posts from a user"); } /*****************************************************************************/ @@ -1485,7 +1464,6 @@ static void For_WriteNumberOfPosts (long UsrCod) extern const char *Txt_post; extern const char *Txt_posts; char SubQuery[256]; - char *Query; unsigned NumPsts; /***** Star table cell *****/ @@ -1497,13 +1475,12 @@ static void For_WriteNumberOfPosts (long UsrCod) Gbl.Forum.ForumSelected.Location); else SubQuery[0] = '\0'; - if (asprintf (&Query,"SELECT COUNT(*) FROM forum_post,forum_thread" - " WHERE forum_post.UsrCod=%ld" - " AND forum_post.ThrCod=forum_thread.ThrCod" - " AND forum_thread.ForumType=%u%s", - UsrCod,(unsigned) Gbl.Forum.ForumSelected.Type,SubQuery) < 0) - Lay_NotEnoughMemoryExit (); - NumPsts = (unsigned) DB_QueryCOUNT_free (Query,"can not get the number of posts of a user in a forum"); + DB_BuildQuery ("SELECT COUNT(*) FROM forum_post,forum_thread" + " WHERE forum_post.UsrCod=%ld" + " AND forum_post.ThrCod=forum_thread.ThrCod" + " AND forum_thread.ForumType=%u%s", + UsrCod,(unsigned) Gbl.Forum.ForumSelected.Type,SubQuery); + NumPsts = (unsigned) DB_QueryCOUNT_new ("can not get the number of posts of a user in a forum"); /***** Write number of threads and number of posts *****/ if (NumPsts == 1) @@ -2367,7 +2344,6 @@ static unsigned For_GetNumOfThreadsInForumNewerThan (struct Forum *Forum, const char *Time) { char SubQuery[256]; - char *Query; /***** Get number of threads with a last message modify time > specified time from database *****/ @@ -2375,13 +2351,12 @@ static unsigned For_GetNumOfThreadsInForumNewerThan (struct Forum *Forum, sprintf (SubQuery," AND forum_thread.Location=%ld",Forum->Location); else SubQuery[0] = '\0'; - if (asprintf (&Query,"SELECT COUNT(*) FROM forum_thread,forum_post" - " WHERE forum_thread.ForumType=%u%s" - " AND forum_thread.LastPstCod=forum_post.PstCod" - " AND forum_post.ModifTime>'%s'", - (unsigned) Forum->Type,SubQuery,Time) < 0) - Lay_NotEnoughMemoryExit (); - return (unsigned) DB_QueryCOUNT_free (Query,"can not check if there are new posts in a forum"); + DB_BuildQuery ("SELECT COUNT(*) FROM forum_thread,forum_post" + " WHERE forum_thread.ForumType=%u%s" + " AND forum_thread.LastPstCod=forum_post.PstCod" + " AND forum_post.ModifTime>'%s'", + (unsigned) Forum->Type,SubQuery,Time); + return (unsigned) DB_QueryCOUNT_new ("can not check if there are new posts in a forum"); } /*****************************************************************************/ @@ -2421,14 +2396,11 @@ static unsigned For_GetNumOfUnreadPostsInThr (long ThrCod,unsigned NumPostsInThr static unsigned For_GetNumOfPostsInThrNewerThan (long ThrCod,const char *Time) { - char *Query; - /***** Get the number of posts in thread with a modify time > a specified time from database *****/ - if (asprintf (&Query,"SELECT COUNT(*) FROM forum_post" - " WHERE ThrCod=%ld AND ModifTime>'%s'", - ThrCod,Time) < 0) - Lay_NotEnoughMemoryExit (); - return (unsigned) DB_QueryCOUNT_free (Query,"can not check if there are new posts in a thread of a forum"); + DB_BuildQuery ("SELECT COUNT(*) FROM forum_post" + " WHERE ThrCod=%ld AND ModifTime>'%s'", + ThrCod,Time); + return (unsigned) DB_QueryCOUNT_new ("can not check if there are new posts in a thread of a forum"); } /*****************************************************************************/ @@ -2885,8 +2857,6 @@ unsigned For_GetNumTotalForumsOfType (For_ForumType_t ForumType, unsigned For_GetNumTotalThrsInForumsOfType (For_ForumType_t ForumType, long CtyCod,long InsCod,long CtrCod,long DegCod,long CrsCod) { - char *Query; - /***** Get total number of threads in forums of this type from database *****/ switch (ForumType) { @@ -2895,206 +2865,151 @@ unsigned For_GetNumTotalThrsInForumsOfType (For_ForumType_t ForumType, case For_FORUM__SWAD__USRS: case For_FORUM__SWAD__TCHS: // Total number of threads in forums of this type - if (asprintf (&Query,"SELECT COUNT(*)" - " FROM forum_thread" - " WHERE ForumType=%u", - (unsigned) ForumType) < 0) - Lay_NotEnoughMemoryExit (); + DB_BuildQuery ("SELECT COUNT(*)" + " FROM forum_thread" + " WHERE ForumType=%u", + (unsigned) ForumType); break; case For_FORUM_INSTIT_USRS: case For_FORUM_INSTIT_TCHS: if (InsCod > 0) // InsCod > 0 ==> Number of threads in institution forums for an institution - { - if (asprintf (&Query,"SELECT COUNT(*)" - " FROM forum_thread" - " WHERE ForumType=%u" - " AND Location=%ld", - (unsigned) ForumType,InsCod) < 0) - Lay_NotEnoughMemoryExit (); - } + DB_BuildQuery ("SELECT COUNT(*)" + " FROM forum_thread" + " WHERE ForumType=%u" + " AND Location=%ld", + (unsigned) ForumType,InsCod); else if (CtyCod > 0) // InsCod <= 0 && CtyCod > 0 ==> Number of threads in institution forums for a country - { - if (asprintf (&Query,"SELECT COUNT(*)" - " FROM forum_thread,institutions" - " WHERE forum_thread.ForumType=%u" - " AND forum_thread.Location=institutions.InsCod" - " AND institutions.CtyCod=%ld", - (unsigned) ForumType,CtyCod) < 0) - Lay_NotEnoughMemoryExit (); - } + DB_BuildQuery ("SELECT COUNT(*)" + " FROM forum_thread,institutions" + " WHERE forum_thread.ForumType=%u" + " AND forum_thread.Location=institutions.InsCod" + " AND institutions.CtyCod=%ld", + (unsigned) ForumType,CtyCod); else // InsCod <= 0 ==> Number of threads in institution forums for the whole platform - { - if (asprintf (&Query,"SELECT COUNT(*)" - " FROM forum_thread" - " WHERE ForumType=%u", - (unsigned) ForumType) < 0) - Lay_NotEnoughMemoryExit (); - } + DB_BuildQuery ("SELECT COUNT(*)" + " FROM forum_thread" + " WHERE ForumType=%u", + (unsigned) ForumType); break; case For_FORUM_CENTRE_USRS: case For_FORUM_CENTRE_TCHS: if (CtrCod > 0) // CtrCod > 0 ==> 0 <= Number of threads in centre forums for a centre <= 1 - { - if (asprintf (&Query,"SELECT COUNT(*)" - " FROM forum_thread" - " WHERE ForumType=%u" - " AND Location=%ld", - (unsigned) ForumType,CtrCod) < 0) - Lay_NotEnoughMemoryExit (); - } + DB_BuildQuery ("SELECT COUNT(*)" + " FROM forum_thread" + " WHERE ForumType=%u" + " AND Location=%ld", + (unsigned) ForumType,CtrCod); else if (InsCod > 0) // CtrCod <= 0 && InsCod > 0 ==> Number of threads in centre forums for an institution - { - if (asprintf (&Query,"SELECT COUNT(*)" - " FROM forum_thread,centres" - " WHERE forum_thread.ForumType=%u" - " AND forum_thread.Location=centres.CtrCod" - " AND centres.InsCod=%ld", - (unsigned) ForumType,InsCod) < 0) - Lay_NotEnoughMemoryExit (); - } + DB_BuildQuery ("SELECT COUNT(*)" + " FROM forum_thread,centres" + " WHERE forum_thread.ForumType=%u" + " AND forum_thread.Location=centres.CtrCod" + " AND centres.InsCod=%ld", + (unsigned) ForumType,InsCod); else if (CtyCod > 0) // InsCod <= 0 && CtyCod > 0 ==> Number of threads in centre forums for a country - { - if (asprintf (&Query,"SELECT COUNT(*)" - " FROM forum_thread,centres,institutions" - " WHERE forum_thread.ForumType=%u" - " AND forum_thread.Location=centres.CtrCod" - " AND centres.InsCod=institutions.InsCod" - " AND institutions.CtyCod=%ld", - (unsigned) ForumType,CtyCod) < 0) - Lay_NotEnoughMemoryExit (); - } + DB_BuildQuery ("SELECT COUNT(*)" + " FROM forum_thread,centres,institutions" + " WHERE forum_thread.ForumType=%u" + " AND forum_thread.Location=centres.CtrCod" + " AND centres.InsCod=institutions.InsCod" + " AND institutions.CtyCod=%ld", + (unsigned) ForumType,CtyCod); else // InsCod <= 0 ==> Number of threads in centre forums for the whole platform - { - if (asprintf (&Query,"SELECT COUNT(*)" - " FROM forum_thread" - " WHERE ForumType=%u", - (unsigned) ForumType) < 0) - Lay_NotEnoughMemoryExit (); - } + DB_BuildQuery ("SELECT COUNT(*)" + " FROM forum_thread" + " WHERE ForumType=%u", + (unsigned) ForumType); break; case For_FORUM_DEGREE_USRS: case For_FORUM_DEGREE_TCHS: if (DegCod > 0) // DegCod > 0 ==> Number of threads in degree forums for a degree - { - if (asprintf (&Query,"SELECT COUNT(*)" - " FROM forum_thread" - " WHERE ForumType=%u" - " AND Location=%ld", - (unsigned) ForumType,DegCod) < 0) - Lay_NotEnoughMemoryExit (); - } + DB_BuildQuery ("SELECT COUNT(*)" + " FROM forum_thread" + " WHERE ForumType=%u" + " AND Location=%ld", + (unsigned) ForumType,DegCod); else if (CtrCod > 0) // DegCod <= 0 && CtrCod > 0 ==> Number of threads in degree forums for a centre - { - if (asprintf (&Query,"SELECT COUNT(*)" - " FROM forum_thread,degrees" - " WHERE forum_thread.ForumType=%u" - " AND forum_thread.Location=degrees.DegCod" - " AND degrees.CtrCod=%ld", - (unsigned) ForumType,CtrCod) < 0) - Lay_NotEnoughMemoryExit (); - } + DB_BuildQuery ("SELECT COUNT(*)" + " FROM forum_thread,degrees" + " WHERE forum_thread.ForumType=%u" + " AND forum_thread.Location=degrees.DegCod" + " AND degrees.CtrCod=%ld", + (unsigned) ForumType,CtrCod); else if (InsCod > 0) // CtrCod <= 0 && InsCod > 0 ==> Number of threads in degree forums for an institution - { - if (asprintf (&Query,"SELECT COUNT(*)" - " FROM forum_thread,degrees,centres" - " WHERE forum_thread.ForumType=%u" - " AND forum_thread.Location=degrees.DegCod" - " AND degrees.CtrCod=centres.CtrCod" - " AND centres.InsCod=%ld", - (unsigned) ForumType,InsCod) < 0) - Lay_NotEnoughMemoryExit (); - } + DB_BuildQuery ("SELECT COUNT(*)" + " FROM forum_thread,degrees,centres" + " WHERE forum_thread.ForumType=%u" + " AND forum_thread.Location=degrees.DegCod" + " AND degrees.CtrCod=centres.CtrCod" + " AND centres.InsCod=%ld", + (unsigned) ForumType,InsCod); else if (CtyCod > 0) // InsCod <= 0 && CtyCod > 0 ==> Number of threads in degree forums for a country - { - if (asprintf (&Query,"SELECT COUNT(*)" - " FROM forum_thread,degrees,centres,institutions" - " WHERE forum_thread.ForumType=%u" - " AND forum_thread.Location=degrees.DegCod" - " AND degrees.CtrCod=centres.CtrCod" - " AND centres.InsCod=institutions.InsCod" - " AND institutions.CtyCod=%ld", - (unsigned) ForumType,CtyCod) < 0) - Lay_NotEnoughMemoryExit (); - } + DB_BuildQuery ("SELECT COUNT(*)" + " FROM forum_thread,degrees,centres,institutions" + " WHERE forum_thread.ForumType=%u" + " AND forum_thread.Location=degrees.DegCod" + " AND degrees.CtrCod=centres.CtrCod" + " AND centres.InsCod=institutions.InsCod" + " AND institutions.CtyCod=%ld", + (unsigned) ForumType,CtyCod); else // InsCod <= 0 ==> Number of threads in degree forums for the whole platform - { - if (asprintf (&Query,"SELECT COUNT(*)" - " FROM forum_thread" - " WHERE ForumType=%u", - (unsigned) ForumType) < 0) - Lay_NotEnoughMemoryExit (); - } + DB_BuildQuery ("SELECT COUNT(*)" + " FROM forum_thread" + " WHERE ForumType=%u", + (unsigned) ForumType); break; case For_FORUM_COURSE_USRS: case For_FORUM_COURSE_TCHS: if (CrsCod > 0) // CrsCod > 0 ==> 0 <= Number of threads in course forums for a course - { - if (asprintf (&Query,"SELECT COUNT(*)" - " FROM forum_thread" - " WHERE ForumType=%u" - " AND Location=%ld", - (unsigned) ForumType,CrsCod) < 0) - Lay_NotEnoughMemoryExit (); - } + DB_BuildQuery ("SELECT COUNT(*)" + " FROM forum_thread" + " WHERE ForumType=%u" + " AND Location=%ld", + (unsigned) ForumType,CrsCod); else if (DegCod > 0) // CrsCod <= 0 && DegCod > 0 ==> Number of threads in course forums for a degree - { - if (asprintf (&Query,"SELECT COUNT(*)" - " FROM forum_thread,courses" - " WHERE forum_thread.ForumType=%u" - " AND forum_thread.Location=courses.CrsCod" - " AND courses.DegCod=%ld", - (unsigned) ForumType,DegCod) < 0) - Lay_NotEnoughMemoryExit (); - } + DB_BuildQuery ("SELECT COUNT(*)" + " FROM forum_thread,courses" + " WHERE forum_thread.ForumType=%u" + " AND forum_thread.Location=courses.CrsCod" + " AND courses.DegCod=%ld", + (unsigned) ForumType,DegCod); else if (CtrCod > 0) // DegCod <= 0 && CtrCod > 0 ==> Number of threads in course forums for a centre - { - if (asprintf (&Query,"SELECT COUNT(*)" - " FROM forum_thread,courses,degrees" - " WHERE forum_thread.ForumType=%u" - " AND forum_thread.Location=courses.CrsCod" - " AND courses.DegCod=degrees.DegCod" - " AND degrees.CtrCod=%ld", - (unsigned) ForumType,CtrCod) < 0) - Lay_NotEnoughMemoryExit (); - } + DB_BuildQuery ("SELECT COUNT(*)" + " FROM forum_thread,courses,degrees" + " WHERE forum_thread.ForumType=%u" + " AND forum_thread.Location=courses.CrsCod" + " AND courses.DegCod=degrees.DegCod" + " AND degrees.CtrCod=%ld", + (unsigned) ForumType,CtrCod); else if (InsCod > 0) // CtrCod <= 0 && InsCod > 0 ==> Number of threads in course forums for an institution - { - if (asprintf (&Query,"SELECT COUNT(*)" - " FROM forum_thread,courses,degrees,centres" - " WHERE forum_thread.ForumType=%u" - " AND forum_thread.Location=courses.CrsCod" - " AND courses.DegCod=degrees.DegCod" - " AND degrees.CtrCod=centres.CtrCod" - " AND centres.InsCod=%ld", - (unsigned) ForumType,InsCod) < 0) - Lay_NotEnoughMemoryExit (); - } + DB_BuildQuery ("SELECT COUNT(*)" + " FROM forum_thread,courses,degrees,centres" + " WHERE forum_thread.ForumType=%u" + " AND forum_thread.Location=courses.CrsCod" + " AND courses.DegCod=degrees.DegCod" + " AND degrees.CtrCod=centres.CtrCod" + " AND centres.InsCod=%ld", + (unsigned) ForumType,InsCod); else if (CtyCod > 0) // InsCod <= 0 && CtyCod > 0 ==> Number of threads in course forums for a country - { - if (asprintf (&Query,"SELECT COUNT(*)" - " FROM forum_thread,courses,degrees,centres,institutions" - " WHERE forum_thread.ForumType=%u" - " AND forum_thread.Location=courses.CrsCod" - " AND courses.DegCod=degrees.DegCod" - " AND degrees.CtrCod=centres.CtrCod" - " AND centres.InsCod=institutions.InsCod" - " AND institutions.CtyCod=%ld", - (unsigned) ForumType,CtyCod) < 0) - Lay_NotEnoughMemoryExit (); - } + DB_BuildQuery ("SELECT COUNT(*)" + " FROM forum_thread,courses,degrees,centres,institutions" + " WHERE forum_thread.ForumType=%u" + " AND forum_thread.Location=courses.CrsCod" + " AND courses.DegCod=degrees.DegCod" + " AND degrees.CtrCod=centres.CtrCod" + " AND centres.InsCod=institutions.InsCod" + " AND institutions.CtyCod=%ld", + (unsigned) ForumType,CtyCod); else // InsCod <= 0 ==> Number of threads in course forums for the whole platform - { - if (asprintf (&Query,"SELECT COUNT(*)" - " FROM forum_thread" - " WHERE ForumType=%u", - (unsigned) ForumType) < 0) - Lay_NotEnoughMemoryExit (); - } + DB_BuildQuery ("SELECT COUNT(*)" + " FROM forum_thread" + " WHERE ForumType=%u", + (unsigned) ForumType); break; default: return 0; } - return (unsigned) DB_QueryCOUNT_free (Query,"can not get the number of threads in forums of a type"); + return (unsigned) DB_QueryCOUNT_new ("can not get the number of threads in forums of a type"); } /*****************************************************************************/ @@ -3104,17 +3019,15 @@ unsigned For_GetNumTotalThrsInForumsOfType (For_ForumType_t ForumType, static unsigned For_GetNumThrsInForum (struct Forum *Forum) { char SubQuery[256]; - char *Query; /***** Get number of threads in a forum from database *****/ if (Forum->Location > 0) sprintf (SubQuery," AND Location=%ld",Forum->Location); else SubQuery[0] = '\0'; - if (asprintf (&Query,"SELECT COUNT(*) FROM forum_thread WHERE ForumType=%u%s", - (unsigned) Forum->Type,SubQuery) < 0) - Lay_NotEnoughMemoryExit (); - return (unsigned) DB_QueryCOUNT_free (Query,"can not get number of threads in a forum"); + DB_BuildQuery ("SELECT COUNT(*) FROM forum_thread WHERE ForumType=%u%s", + (unsigned) Forum->Type,SubQuery); + return (unsigned) DB_QueryCOUNT_new ("can not get number of threads in a forum"); } /*****************************************************************************/ @@ -3330,19 +3243,17 @@ unsigned For_GetNumTotalPstsInForumsOfType (For_ForumType_t ForumType, static unsigned For_GetNumPstsInForum (struct Forum *Forum) { char SubQuery[256]; - char *Query; /***** Get number of posts in a forum from database *****/ if (Forum->Location > 0) sprintf (SubQuery," AND forum_thread.Location=%ld",Forum->Location); else SubQuery[0] = '\0'; - if (asprintf (&Query,"SELECT COUNT(*) FROM forum_thread,forum_post " - " WHERE forum_thread.ForumType=%u%s" - " AND forum_thread.ThrCod=forum_post.ThrCod", - (unsigned) Forum->Type,SubQuery) < 0) - Lay_NotEnoughMemoryExit (); - return (unsigned) DB_QueryCOUNT_free (Query,"can not get the number of posts in a forum"); + DB_BuildQuery ("SELECT COUNT(*) FROM forum_thread,forum_post " + " WHERE forum_thread.ForumType=%u%s" + " AND forum_thread.ThrCod=forum_post.ThrCod", + (unsigned) Forum->Type,SubQuery); + return (unsigned) DB_QueryCOUNT_new ("can not get the number of posts in a forum"); } /*****************************************************************************/ @@ -4450,18 +4361,16 @@ static long For_GetThrInMyClipboard (void) static bool For_CheckIfThrBelongsToForum (long ThrCod,struct Forum *Forum) { char SubQuery[256]; - char *Query; /***** Get if a thread belong to current forum from database *****/ if (Forum->Location > 0) sprintf (SubQuery," AND forum_thread.Location=%ld",Forum->Location); else SubQuery[0] = '\0'; - if (asprintf (&Query,"SELECT COUNT(*) FROM forum_thread" - " WHERE ThrCod=%ld AND ForumType=%u%s", - ThrCod,(unsigned) Forum->Type,SubQuery) < 0) - Lay_NotEnoughMemoryExit (); - return (DB_QueryCOUNT_free (Query,"can not get if a thread belong to current forum") != 0); + DB_BuildQuery ("SELECT COUNT(*) FROM forum_thread" + " WHERE ThrCod=%ld AND ForumType=%u%s", + ThrCod,(unsigned) Forum->Type,SubQuery); + return (DB_QueryCOUNT_new ("can not get if a thread belong to current forum") != 0); } /*****************************************************************************/ diff --git a/swad_game.c b/swad_game.c index 806fbaeae..522cc338c 100644 --- a/swad_game.c +++ b/swad_game.c @@ -1727,16 +1727,14 @@ void Gam_UnhideGame (void) static bool Gam_CheckIfSimilarGameExists (struct Game *Game) { extern const char *Sco_ScopeDB[Sco_NUM_SCOPES]; - char *Query; /***** Get number of games with a field value from database *****/ - if (asprintf (&Query,"SELECT COUNT(*) FROM games" - " WHERE Scope='%s' AND Cod=%ld" - " AND Title='%s' AND GamCod<>%ld", - Sco_ScopeDB[Game->Scope],Game->Cod, - Game->Title,Game->GamCod) < 0) - Lay_NotEnoughMemoryExit (); - return (DB_QueryCOUNT_free (Query,"can not get similar games") != 0); + DB_BuildQuery ("SELECT COUNT(*) FROM games" + " WHERE Scope='%s' AND Cod=%ld" + " AND Title='%s' AND GamCod<>%ld", + Sco_ScopeDB[Game->Scope],Game->Cod, + Game->Title,Game->GamCod); + return (DB_QueryCOUNT_new ("can not get similar games") != 0); } /*****************************************************************************/ @@ -2272,13 +2270,9 @@ static void Gam_UpdateGame (struct Game *Game,const char *Txt) static bool Gam_CheckIfGamIsAssociatedToGrps (long GamCod) { - char *Query; - /***** Get if a game is associated to a group from database *****/ - if (asprintf (&Query,"SELECT COUNT(*) FROM gam_grp WHERE GamCod=%ld", - GamCod) < 0) - Lay_NotEnoughMemoryExit (); - return (DB_QueryCOUNT_free (Query,"can not check if a game is associated to groups") != 0); + DB_BuildQuery ("SELECT COUNT(*) FROM gam_grp WHERE GamCod=%ld",GamCod); + return (DB_QueryCOUNT_new ("can not check if a game is associated to groups") != 0); } /*****************************************************************************/ @@ -2287,14 +2281,11 @@ static bool Gam_CheckIfGamIsAssociatedToGrps (long GamCod) bool Gam_CheckIfGamIsAssociatedToGrp (long GamCod,long GrpCod) { - char *Query; - /***** Get if a game is associated to a group from database *****/ - if (asprintf (&Query,"SELECT COUNT(*) FROM gam_grp" - " WHERE GamCod=%ld AND GrpCod=%ld", - GamCod,GrpCod) < 0) - Lay_NotEnoughMemoryExit (); - return (DB_QueryCOUNT_free (Query,"can not check if a game is associated to a group") != 0); + DB_BuildQuery ("SELECT COUNT(*) FROM gam_grp" + " WHERE GamCod=%ld AND GrpCod=%ld", + GamCod,GrpCod); + return (DB_QueryCOUNT_new ("can not check if a game is associated to a group") != 0); } /*****************************************************************************/ @@ -2500,18 +2491,15 @@ void Gam_RemoveGames (Sco_Scope_t Scope,long Cod) static bool Gam_CheckIfICanDoThisGameBasedOnGrps (long GamCod) { - char *Query; - /***** Get if I can do a game from database *****/ - if (asprintf (&Query,"SELECT COUNT(*) FROM games" - " WHERE GamCod=%ld" - " AND (GamCod NOT IN (SELECT GamCod FROM gam_grp) OR" - " GamCod IN (SELECT gam_grp.GamCod FROM gam_grp,crs_grp_usr" - " WHERE crs_grp_usr.UsrCod=%ld" - " AND gam_grp.GrpCod=crs_grp_usr.GrpCod))", - GamCod,Gbl.Usrs.Me.UsrDat.UsrCod) < 0) - Lay_NotEnoughMemoryExit (); - return (DB_QueryCOUNT_free (Query,"can not check if I can do a game") != 0); + DB_BuildQuery ("SELECT COUNT(*) FROM games" + " WHERE GamCod=%ld" + " AND (GamCod NOT IN (SELECT GamCod FROM gam_grp) OR" + " GamCod IN (SELECT gam_grp.GamCod FROM gam_grp,crs_grp_usr" + " WHERE crs_grp_usr.UsrCod=%ld" + " AND gam_grp.GrpCod=crs_grp_usr.GrpCod))", + GamCod,Gbl.Usrs.Me.UsrDat.UsrCod); + return (DB_QueryCOUNT_new ("can not check if I can do a game") != 0); } /*****************************************************************************/ @@ -2520,13 +2508,9 @@ static bool Gam_CheckIfICanDoThisGameBasedOnGrps (long GamCod) static unsigned Gam_GetNumQstsGame (long GamCod) { - char *Query; - /***** Get data of questions from database *****/ - if (asprintf (&Query,"SELECT COUNT(*) FROM gam_questions WHERE GamCod=%ld", - GamCod) < 0) - Lay_NotEnoughMemoryExit (); - return (unsigned) DB_QueryCOUNT_free (Query,"can not get number of questions of a game"); + DB_BuildQuery ("SELECT COUNT(*) FROM gam_questions WHERE GamCod=%ld",GamCod); + return (unsigned) DB_QueryCOUNT_new ("can not get number of questions of a game"); } /*****************************************************************************/ @@ -3855,14 +3839,11 @@ static void Gam_RegisterIHaveAnsweredGame (long GamCod) static bool Gam_CheckIfIHaveAnsweredGame (long GamCod) { - char *Query; - /***** Get number of games with a field value from database *****/ - if (asprintf (&Query,"SELECT COUNT(*) FROM gam_users" - " WHERE GamCod=%ld AND UsrCod=%ld", - GamCod,Gbl.Usrs.Me.UsrDat.UsrCod) < 0) - Lay_NotEnoughMemoryExit (); - return (DB_QueryCOUNT_free (Query,"can not check if you have answered a game") != 0); + DB_BuildQuery ("SELECT COUNT(*) FROM gam_users" + " WHERE GamCod=%ld AND UsrCod=%ld", + GamCod,Gbl.Usrs.Me.UsrDat.UsrCod); + return (DB_QueryCOUNT_new ("can not check if you have answered a game") != 0); } /*****************************************************************************/ @@ -3871,13 +3852,9 @@ static bool Gam_CheckIfIHaveAnsweredGame (long GamCod) static unsigned Gam_GetNumUsrsWhoHaveAnsweredGame (long GamCod) { - char *Query; - /***** Get number of games with a field value from database *****/ - if (asprintf (&Query,"SELECT COUNT(*) FROM gam_users WHERE GamCod=%ld", - GamCod) < 0) - Lay_NotEnoughMemoryExit (); - return (unsigned) DB_QueryCOUNT_free (Query,"can not get number of users who have answered a game"); + DB_BuildQuery ("SELECT COUNT(*) FROM gam_users WHERE GamCod=%ld",GamCod); + return (unsigned) DB_QueryCOUNT_new ("can not get number of users who have answered a game"); } /*****************************************************************************/