-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathContext.pgc
98 lines (77 loc) · 2.24 KB
/
Context.pgc
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
#include "IO.h"
#include "Dialogs.h"
#include "Accounts.h"
#include "Context.h"
#include "Messages.h"
#include "Locks.h"
EXEC SQL WHENEVER SQLWARNING SQLPRINT;
EXEC SQL WHENEVER SQLERROR SQLPRINT;
EXEC SQL WHENEVER NOT FOUND CONTINUE;
void EnsureCreated()
{
CreateAccountsTable();
CreateDialogsTable();
CreateMessagesTable();
CreateAccountsDialogsTable();
CreateLocksTableTrigger();
EXEC SQL COMMIT;
}
void FillSomeData()
{
Register("User1", "qwerty");
Register("User2", "qwerty");
Register("User3", "qwerty1");
Register("User4", "qwerty123");
CreateDialog("User1", "User2");
CreateDialog("User1", "User3");
CreateDialog("User2", "User3");
CreateDialog("User4", "User3");
AddMessage(1, 1, "Message1 User1 -> (User1, User2)");
AddMessage(1, 1, "Message2 User1 -> (User1, User2)");
AddMessage(2, 1, "Message3 User2 -> (User1, User2)");
AddMessage(1, 2, "Message4 User1 -> (User1, User3)");
AddMessage(3, 3, "Message5 User3 -> (User2, User3)");
AddMessage(2, 3, "Message6 User2 -> (User2, User3)");
AddMessage(3, 3, "Message7 User3 -> (User2, User3)");
AddMessage(4, 4, "Message8 User4 -> (User4, User3)");
}
void DropTables()
{
DropLocksTableTrigger();
DropAccountsDialogsTable();
DropMessagesTable();
DropDialogsTable();
DropAccountsTable();
EXEC SQL COMMIT;
}
EXEC SQL BEGIN DECLARE SECTION;
const char* connection = "connection";
EXEC SQL END DECLARE SECTION;
bool Connect(char* address, char* port, char* dbname, char* dbUser, char* dbUserPassword)
{
EXEC SQL BEGIN DECLARE SECTION;
char target[STRING_SIZE] = "";
char *user_ = dbUser;
char *password_ = dbUserPassword;
EXEC SQL END DECLARE SECTION;
strcat(target, "tcp:postgresql://");
strcat(target, address);
strcat(target, ":");
strcat(target, port);
strcat(target, "/");
strcat(target, dbname);
EXEC SQL CONNECT TO :target AS :connection USER :user_ IDENTIFIED BY :password_;
return true;
}
void PrintDbVersion()
{
EXEC SQL BEGIN DECLARE SECTION;
char version[STRING_SIZE];
EXEC SQL END DECLARE SECTION;
EXEC SQL SELECT version() INTO :version;
printf("%s\n", version);
}
void Disconnect()
{
EXEC SQL DISCONNECT :connection;
}