-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdatabaseQueries.cpp
More file actions
118 lines (104 loc) · 2.62 KB
/
databaseQueries.cpp
File metadata and controls
118 lines (104 loc) · 2.62 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
//NOTE: These will only get
#include <iostream>
#include <string>
#include <pqxx/pqxx>
namespace Query {
pqxx::result getUser(double user_id){//Gets all information on a user given a user id
pqxx::result r;
try{
pqxx::connection c;
pqxx::work w(c);
r = w.exec("SELECT * FROM users WHERE user_id = " + to_string(user_id) + ";");
w.commit();
}
catch (const std::exception &e){
}
return r;
}
pqxx::result getUserFromEmail(std::string email){//Gets user from email address
pqxx::result r;
try{
pqxx::connection c;
pqxx::work w(c);
r = w.exec("SELECT * FROM users WHERE email = " + email + ";");
w.commit();
}
catch (const std::exception &e){
}
return r;
}
pqxx::result getGroupsFromUser(double user_id){ //Gets all groups a user is a member of
pqxx::result r;
try{
pqxx::connection c;
pqxx::work w(c);
r = w.exec("SELECT * FROM groups natural join membership WHERE user_id = " + to_string(user_id) + ";");
w.commit();
}
catch (const std::exception &e){
}
return r;
}
pqxx::result getGroupInfo(double group_id){//Gets all of a groups info
pqxx::result r;
try{
pqxx::connection c;
pqxx::work w(c);
r = w.exec("SELECT * FROM groups WHERE group_id = " + to_string(group_id) + ";");
w.commit();
}
catch (const std::exception &e){
}
return r;
}
bool createGroup(std::string group_name){//Creates a new group called "group_name" Returns true if it suceeds
pqxx::result r;
try{
pqxx::connection c;
pqxx::work w(c);
r = w.exec("INSERT INTO groups (group_name) VALUES (" + group_name + ");");
w.commit();
}
catch (const std::exception &e){
return false;
}
return true;
}
pqxx::result getUsersFromGroup(double group_id){
pqxx::result r;
try{
pqxx::connection c;
pqxx::work w(c);
r = w.exec("SELECT user_id FROM membership WHERE group_id = " + to_string(group_id) + ";");
w.commit();
}
catch (const std::exception &e){
}
return r;
}
pqxx::result getMessages(double group_id){
pqxx::result r;
try{
pqxx::connection c;
pqxx::work w(c);
r = w.exec("SELECT * FROM messages WHERE group_id = " + to_string(group_id) + " ORDER BY message_time;");
w.commit();
}
catch (const std::exception &e){
}
return r;
}
bool addMessage(double group_id, double user_id, std::string message_text){
pqxx::result r;
try{
pqxx::connection c;
pqxx::work w(c);
r = w.exec("INSERT INTO messages (message_time, user_id, group_id) VALUES (CURRENT_TIMESTAMP, " + to_string(user_id) + ", " + to_string(group_id) + ");");
w.commit();
}
catch (const std::exception &e){
return false;
}
return true;
}
}