-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLocks.pgc
189 lines (150 loc) · 4.57 KB
/
Locks.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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
#include <malloc.h>
#include "Locks.h"
#include "IO.h"
EXEC SQL WHENEVER SQLWARNING SQLPRINT;
EXEC SQL WHENEVER SQLERROR SQLPRINT;
EXEC SQL WHENEVER NOT FOUND CONTINUE;
void CreateLocksTableTrigger()
{
EXEC SQL CREATE TABLE Locks
(
Name varchar(50) PRIMARY KEY,
IsLocked boolean NOT NULL,
DialogId int NOT NULL,
FOREIGN KEY (DialogId) REFERENCES Dialogs (DialogId) ON DELETE CASCADE
);
EXEC SQL COMMIT;
CreateLocksTrigger();
}
void DropLocksTableTrigger()
{
EXEC SQL DROP FUNCTION IF EXISTS clear_old_locks() CASCADE;
EXEC SQL DROP TABLE IF EXISTS Locks CASCADE;
EXEC SQL COMMIT;
}
void CreateLocksTrigger()
{
EXEC SQL DROP FUNCTION IF EXISTS clear_old_locks CASCADE;
EXEC SQL CREATE OR REPLACE FUNCTION clear_old_locks() RETURNS trigger AS
$$
BEGIN
DELETE FROM Locks WHERE isLocked = false;
RETURN NULL;
END;
$$ LANGUAGE plpgsql;
EXEC SQL CREATE TRIGGER clear_old_locks
BEFORE INSERT
ON Locks
FOR STATEMENT
EXECUTE PROCEDURE clear_old_locks();
EXEC SQL COMMIT;
}
void Lock(char* lockname, int dialogId)
{
EXEC SQL BEGIN DECLARE SECTION;
char* lockname_ = lockname;
int dialogId_ = dialogId;
bool _;
EXEC SQL END DECLARE SECTION;
EXEC SQL INSERT INTO Locks (Name, IsLocked, DialogId) VALUES (:lockname_, true, :dialogId_);
EXEC SQL COMMIT;
EXEC SQL SELECT isLocked INTO :_ FROM Locks WHERE Name = :lockname_ FOR UPDATE;
//EXEC SQL PREPARE TRANSACTION :lockname_;
char transaction[STRING_SIZE] = "prepare transaction '";
strcat(transaction, lockname_);
strcat(transaction, "'");
ECPGtrans(__LINE__, NULL, transaction);
EXEC SQL COMMIT;
}
void Wait(char* lockname)
{
EXEC SQL BEGIN DECLARE SECTION;
char* lockname_ = lockname;
bool _;
EXEC SQL END DECLARE SECTION;
EXEC SQL SELECT isLocked INTO :_ FROM Locks WHERE Name = :lockname_ FOR UPDATE;
EXEC SQL COMMIT;
}
void Unlock(char* lockname, bool isUpdateNeeded)
{
EXEC SQL BEGIN DECLARE SECTION;
char* lockname_ = lockname;
EXEC SQL END DECLARE SECTION;
EXEC SQL COMMIT;
//EXEC SQL COMMIT PREPARED :lockname_;
char transaction[STRING_SIZE] = "commit prepared '";
strcat(transaction, lockname_);
strcat(transaction, "'");
ECPGtrans(__LINE__, NULL, transaction);
EXEC SQL COMMIT;
if (isUpdateNeeded)
{
EXEC SQL UPDATE Locks SET IsLocked = false WHERE Name = :lockname_;
EXEC SQL COMMIT;
}
}
void UnlockDialog(int dialogId)
{
EXEC SQL BEGIN DECLARE SECTION;
int dialogId_ = dialogId;
int transactionsCount = -1;
int j = 0;
char** transactionNames;
EXEC SQL END DECLARE SECTION;
EXEC SQL SELECT count(*) INTO :transactionsCount FROM locks WHERE DialogId = :dialogId_ AND isLocked = true;
transactionNames = (char**) malloc (sizeof (char*) * transactionsCount);
for (int i = 0; i < transactionsCount; i++)
{
transactionNames[i] = (char*) malloc (STRING_SIZE * sizeof(char));
}
EXEC SQL DECLARE cursor4 CURSOR FOR
SELECT Name FROM locks WHERE DialogId = :dialogId_ AND isLocked = true;
EXEC SQL OPEN cursor4;
EXEC SQL WHENEVER NOT FOUND DO BREAK;
while (true)
{
EXEC SQL FETCH cursor4 INTO :transactionNames[j++];
}
EXEC SQL WHENEVER NOT FOUND CONTINUE;
EXEC SQL CLOSE cursor4;
EXEC SQL COMMIT;
for (int i = 0; i < transactionsCount; i++)
{
Unlock(transactionNames[i], true);
free(transactionNames[i]);
}
free(transactionNames);
}
void UnlockAll()
{
EXEC SQL BEGIN DECLARE SECTION;
int j = 0;
int transactionsCount = -1;
char** transactionNames;
EXEC SQL END DECLARE SECTION;
EXEC SQL SELECT count(*) INTO :transactionsCount FROM pg_prepared_xacts;
transactionNames = (char**) malloc (sizeof (char*) * transactionsCount);
for (int i = 0; i < transactionsCount; i++)
{
transactionNames[i] = (char*) malloc (STRING_SIZE * sizeof(char));
}
EXEC SQL DECLARE cursorA CURSOR FOR
SELECT gid FROM pg_prepared_xacts;
EXEC SQL OPEN cursorA;
EXEC SQL WHENEVER NOT FOUND DO BREAK;
while (true)
{
EXEC SQL FETCH cursorA INTO :transactionNames[j++];
}
EXEC SQL WHENEVER NOT FOUND CONTINUE;
EXEC SQL CLOSE cursorA;
EXEC SQL COMMIT;
for (int i = 0; i < transactionsCount; i++)
{
Unlock(transactionNames[i], false);
free(transactionNames[i]);
}
free(transactionNames);
EXEC SQL UPDATE Locks SET isLocked = false;
EXEC SQL COMMIT;
}