2021-09-30 00:04:41 +02:00
|
|
|
|
// swad_nickname_database.c: Users' nicknames operations with database
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
SWAD (Shared Workspace At a Distance),
|
|
|
|
|
is a web platform developed at the University of Granada (Spain),
|
|
|
|
|
and used to support university teaching.
|
|
|
|
|
|
|
|
|
|
This file is part of SWAD core.
|
2024-02-07 00:40:28 +01:00
|
|
|
|
Copyright (C) 1999-2024 Antonio Ca<EFBFBD>as Vargas
|
2021-09-30 00:04:41 +02:00
|
|
|
|
|
|
|
|
|
This program is free software: you can redistribute it and/or modify
|
|
|
|
|
it under the terms of the GNU Affero General Public License as
|
|
|
|
|
published by the Free Software Foundation, either version 3 of the
|
|
|
|
|
License, or (at your option) any later version.
|
|
|
|
|
|
|
|
|
|
This program is distributed in the hope that it will be useful,
|
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
|
GNU Affero General Public License for more details.
|
|
|
|
|
|
|
|
|
|
You should have received a copy of the GNU Affero General Public License
|
|
|
|
|
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
*/
|
|
|
|
|
/*****************************************************************************/
|
|
|
|
|
/********************************* Headers ***********************************/
|
|
|
|
|
/*****************************************************************************/
|
|
|
|
|
|
|
|
|
|
#include "swad_database.h"
|
|
|
|
|
#include "swad_nickname_database.h"
|
|
|
|
|
|
|
|
|
|
/*****************************************************************************/
|
|
|
|
|
/******************* Update user's nickname in database **********************/
|
|
|
|
|
/*****************************************************************************/
|
|
|
|
|
|
|
|
|
|
void Nck_DB_UpdateNick (long UsrCod,const char *NewNickname)
|
|
|
|
|
{
|
|
|
|
|
DB_QueryREPLACE ("can not update nickname",
|
|
|
|
|
"REPLACE INTO usr_nicknames"
|
|
|
|
|
" (UsrCod,Nickname,CreatTime)"
|
|
|
|
|
" VALUES"
|
|
|
|
|
" (%ld,'%s',NOW())",
|
|
|
|
|
UsrCod,
|
|
|
|
|
NewNickname);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/*****************************************************************************/
|
|
|
|
|
/************** Get user's code of a user from his/her nickname **************/
|
|
|
|
|
/*****************************************************************************/
|
|
|
|
|
|
2021-10-21 20:33:55 +02:00
|
|
|
|
long Nck_DB_GetUsrCodFromNickname (const char *NickWithoutArr)
|
2021-09-30 00:04:41 +02:00
|
|
|
|
{
|
|
|
|
|
return DB_QuerySELECTCode ("can not get user's code",
|
|
|
|
|
"SELECT usr_nicknames.UsrCod"
|
|
|
|
|
" FROM usr_nicknames,"
|
|
|
|
|
"usr_data"
|
|
|
|
|
" WHERE usr_nicknames.Nickname='%s'"
|
|
|
|
|
" AND usr_nicknames.UsrCod=usr_data.UsrCod",
|
2021-09-30 20:11:00 +02:00
|
|
|
|
NickWithoutArr);
|
2021-09-30 00:04:41 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/*****************************************************************************/
|
|
|
|
|
/************* Get nickname of a user from his/her user's code ***************/
|
|
|
|
|
/*****************************************************************************/
|
|
|
|
|
|
|
|
|
|
void Nck_DB_GetNicknameFromUsrCod (long UsrCod,
|
2021-09-30 20:11:00 +02:00
|
|
|
|
char NickWithoutArr[Nck_MAX_BYTES_NICK_WITHOUT_ARROBA + 1])
|
2021-09-30 00:04:41 +02:00
|
|
|
|
{
|
|
|
|
|
/***** Get current (last updated) user's nickname from database *****/
|
2021-09-30 20:11:00 +02:00
|
|
|
|
DB_QuerySELECTString (NickWithoutArr,Nck_MAX_BYTES_NICK_WITHOUT_ARROBA,
|
2021-09-30 00:04:41 +02:00
|
|
|
|
"can not get nickname",
|
|
|
|
|
"SELECT Nickname"
|
|
|
|
|
" FROM usr_nicknames"
|
|
|
|
|
" WHERE UsrCod=%ld"
|
|
|
|
|
" ORDER BY CreatTime DESC"
|
|
|
|
|
" LIMIT 1",
|
|
|
|
|
UsrCod);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/*****************************************************************************/
|
|
|
|
|
/***************************** Get my nicknames ******************************/
|
|
|
|
|
/*****************************************************************************/
|
|
|
|
|
|
|
|
|
|
unsigned Nck_DB_GetUsrNicknames (MYSQL_RES **mysql_res,long UsrCod)
|
|
|
|
|
{
|
|
|
|
|
return (unsigned)
|
|
|
|
|
DB_QuerySELECT (mysql_res,"can not get nicknames of a user",
|
|
|
|
|
"SELECT Nickname" // row[0]
|
|
|
|
|
" FROM usr_nicknames"
|
|
|
|
|
" WHERE UsrCod=%ld"
|
|
|
|
|
" ORDER BY CreatTime DESC",
|
|
|
|
|
UsrCod);
|
|
|
|
|
}
|
|
|
|
|
|
2021-11-08 17:18:36 +01:00
|
|
|
|
/*****************************************************************************/
|
|
|
|
|
/*************** Check if nickname matches any of the nicknames **************/
|
|
|
|
|
/*****************************************************************************/
|
|
|
|
|
|
|
|
|
|
bool Nck_DB_CheckIfNickMatchesAnyNick (const char *NickWithoutArr)
|
|
|
|
|
{
|
|
|
|
|
return
|
|
|
|
|
DB_QueryEXISTS ("can not check if nickname already existed",
|
|
|
|
|
"SELECT EXISTS"
|
|
|
|
|
"(SELECT *"
|
|
|
|
|
" FROM usr_nicknames"
|
|
|
|
|
" WHERE Nickname='%s')",
|
|
|
|
|
NickWithoutArr);
|
|
|
|
|
}
|
|
|
|
|
|
2021-09-30 00:04:41 +02:00
|
|
|
|
/*****************************************************************************/
|
|
|
|
|
/************ Check if nickname matches any of a user's nicknames ************/
|
|
|
|
|
/*****************************************************************************/
|
|
|
|
|
|
2021-10-21 20:33:55 +02:00
|
|
|
|
bool Nck_DB_CheckIfNickMatchesAnyUsrNick (long UsrCod,const char *NickWithoutArr)
|
2021-09-30 00:04:41 +02:00
|
|
|
|
{
|
2021-11-05 23:41:26 +01:00
|
|
|
|
return
|
|
|
|
|
DB_QueryEXISTS ("can not check if nickname already existed",
|
|
|
|
|
"SELECT EXISTS"
|
|
|
|
|
"(SELECT *"
|
|
|
|
|
" FROM usr_nicknames"
|
|
|
|
|
" WHERE UsrCod=%ld"
|
|
|
|
|
" AND Nickname='%s')",
|
|
|
|
|
UsrCod,
|
|
|
|
|
NickWithoutArr);
|
2021-09-30 00:04:41 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/*****************************************************************************/
|
|
|
|
|
/********* Check if nickname matches any of other user's nicknames ***********/
|
|
|
|
|
/*****************************************************************************/
|
|
|
|
|
|
2021-10-21 20:33:55 +02:00
|
|
|
|
bool Nck_DB_CheckIfNickMatchesAnyOtherUsrsNicks (long UsrCod,const char *NickWithoutArr)
|
2021-09-30 00:04:41 +02:00
|
|
|
|
{
|
2021-11-05 23:41:26 +01:00
|
|
|
|
return
|
|
|
|
|
DB_QueryEXISTS ("can not check if nickname already existed",
|
|
|
|
|
"SELECT EXISTS"
|
|
|
|
|
"(SELECT *"
|
|
|
|
|
" FROM usr_nicknames"
|
|
|
|
|
" WHERE Nickname='%s'"
|
|
|
|
|
" AND UsrCod<>%ld)",
|
|
|
|
|
NickWithoutArr,
|
|
|
|
|
UsrCod); // A nickname of another user is the same that user's nickname
|
2021-09-30 00:04:41 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/*****************************************************************************/
|
|
|
|
|
/********************** Remove a nickname from database **********************/
|
|
|
|
|
/*****************************************************************************/
|
|
|
|
|
|
|
|
|
|
void Nck_DB_RemoveNickname (long UsrCod,const char *Nickname)
|
|
|
|
|
{
|
|
|
|
|
DB_QueryREPLACE ("can not remove a nickname",
|
|
|
|
|
"DELETE FROM usr_nicknames"
|
|
|
|
|
" WHERE UsrCod=%ld"
|
|
|
|
|
" AND Nickname='%s'",
|
|
|
|
|
UsrCod,
|
|
|
|
|
Nickname);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/*****************************************************************************/
|
|
|
|
|
/************************** Remove user's nicknames **************************/
|
|
|
|
|
/*****************************************************************************/
|
|
|
|
|
|
|
|
|
|
void Nck_DB_RemoveUsrNicknames (long UsrCod)
|
|
|
|
|
{
|
|
|
|
|
DB_QueryDELETE ("can not remove user's nicknames",
|
|
|
|
|
"DELETE FROM usr_nicknames"
|
|
|
|
|
" WHERE UsrCod=%ld",
|
|
|
|
|
UsrCod);
|
|
|
|
|
}
|