forked from neha19066/Suitor
-
Notifications
You must be signed in to change notification settings - Fork 0
/
queriesIndex.sql
365 lines (269 loc) · 12.8 KB
/
queriesIndex.sql
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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
use suitor;
-- QUERIES
-- PARALEGAL
-- 'A21s9n5a5A', 'Giselle', 'Astra', 'Hanson', '1989-10-22', 'Male', '19792', '14', '57', '19', '20', 'vestibulum.lorem@ornarelibero.co.uk', '9900412440', 'Paralegal', '38', 'P.O. Box 759, 6311 Arcu Avenue', 'Cirebon', '6444871126', 'West Java', 'LGBTQ Law', '4'
CREATE USER ParalegalU@localhost IDENTIFIED BY 'pass';
CREATE ROLE Paralegal;
GRANT SELECT ON suitor.myDetails TO Paralegal;
GRANT SELECT ON suitor.allLegalDocs TO Paralegal;
GRANT SELECT, UPDATE, DELETE, INSERT ON suitor.myEvents TO Paralegal;
GRANT SELECT ON suitor.allCases TO Paralegal;
GRANT SELECT ON suitor.myCases TO Paralegal;
GRANT Paralegal TO ParalegalU@localhost;
-- view their personal details
CREATE OR REPLACE VIEW myDetails AS
SELECT * FROM lawyer
WHERE userID = "A21s9n5a5A";
select * from myDetails;
-- view/add events
CREATE OR REPLACE VIEW myEvents AS
select * from calendar
where userID = "A21s9n5a5A";
select * from myEvents;
-- add new meeting
insert into myEvents values("A21s9n5a5A", '2021-05-01 02:00:00',"Settlement Meeting");
-- reschedule meeting
update myEvents
set
myEvents.when = '2021-05-01 04:30:00'
where userID = "A21s9n5a5A" and myEvents.when = '2021-05-01 02:00:00';
-- delete details of cancelled meting
delete from myEvents
where userID = "A21s9n5a5A" and myEvents.when = '2021-05-01 04:30:00';
-- view all case details for all cases in the firm
create or replace view allCases as
select distinct h.caseID, c.plaintiff, c.lastDateOfActivity, c.flair, c.dateOfFiling, c.duration, c.status, ic.userID as ClientID, ic.firstName as CFirstName, ic.lastName as CLastName, ic.emailID as CEmailID, ic.isClient, ic.city as CCity, l.userID as LawyerID, l.firstName as LFirstName, l.lastName as LLastName, l.emailID as LEmailID, l.positionAtFirm, l.specialization, l.city as LCity, o.oppositionID, o.firstName as OFirstName, o.lastName as OLastName from lawyer l, handles h, legalcases c, hasa ch, individualclients ic, opposition o, against a
where l.userID = h.userID and h.caseID = c.caseID and ch.userID = ic.userID and a.oppositionID = o.oppositionID and a.caseID = c.caseID;
select * from allCases;
-- view/update allowed legal documents for all cases in the firm
create or replace view allLegalDocs as
select distinct d.docID, d.createdOn, d.dateLastModified, d.type, c.caseID, c.lastdateofactivity, c.flair, c.status, c.plaintiff from legaldocuments d, legalcases c
where d.caseID = c.caseID and d.visibility = 1;
select * from allLegalDocs;
-- view their own cases
create or replace view myCases as
select distinct caseID, plaintiff, lastDateOfActivity, flair, dateOfFiling, duration, status, ClientID, CFirstName, CLastName, CEmailID, isClient, CCity from allCases
where LawyerID = "A21s9n5a5A";
select * from myCases;
-- search for cases using flair, client details, etc.
-- search using flair
select * from allCases
where flair = "Tenant Law";
-- search using client's last name
select * from allCases
where CLastName = "Brady";
-- sort all cases by client's city
select * from allCases
order by CCity;
-- search for all cases of a particular lawyer
select * from allCases
where LLastName = "Griffin";
-- CUSTOMER/CLIENT
-- 'I21p5a6t2C', 'MacKensie', 'Trevor', 'Crosby', '1987-04-27', '13540', 'arcu@necmetus.edu', '9729651309', 'Ap #335-2862 Curae; St.', 'Stargard Szczeciński', '80461', 'ZP', '1'
CREATE USER CustomerU@localhost IDENTIFIED BY 'pass';
CREATE ROLE Customer;
GRANT SELECT ON suitor.myDetailsClient TO Customer;
GRANT SELECT, INSERT, UPDATE, DELETE ON suitor.myEventsClient TO Customer;
GRANT SELECT ON suitor.allmycasesclient TO Customer;
GRANT SELECT ON suitor.mybillsClient TO Customer;
GRANT SELECT ON suitor.bestsuitedlawyer TO Customer;
GRANT Customer TO CustomerU@localhost;
drop role Customer;
drop user CustomerU@localhost;
select user, host from MySQL.user;
-- login using \connect --mysql CustomerU@localhost on MySQL Shell
-- change to sql using \sql
-- do set role all;
-- use whatever commands you want now
-- View their personal details.
create or replace view myDetailsClient as
select * from individualclients
where userID = "I21p5a6t2C";
select * from myDetailsClient;
-- view/ add events
CREATE OR REPLACE VIEW myEventsClient AS
select * from calendar
where userID = "I21p5a6t2C";
select * from myEventsClient;
-- add new meeting
insert into myEventsClient values("I21p5a6t2C", '2021-05-01 02:00:00',"Settlement Meeting");
-- reschedule meeting
update myEventsClient
set
myEventsClient.when = '2021-05-01 04:30:00'
where userID = "I21p5a6t2C" and myEventsClient.when = '2021-05-01 02:00:00';
-- delete details of cancelled meting
delete from myEventsClient
where userID = "I21p5a6t2C" and myEventsCLient.when = '2021-05-01 04:30:00';
-- View case details of all their cases
create or replace view allMyCasesClient as
select distinct h.caseID, c.plaintiff, c.lastDateOfActivity, c.flair, c.dateOfFiling, c.duration, c.status, l.userID as LawyerID, l.firstName as LFirstName, l.lastName as LLastName, l.emailID as LEmailID, l.positionAtFirm, l.specialization, l.city as LCity, o.oppositionID, o.firstName as OFirstName, o.lastName as OLastName from lawyer l, handles h, legalcases c, hasa ch, individualclients ic, opposition o, against a
where l.userID = h.userID and h.caseID = c.caseID and ch.userID = ic.userID and a.oppositionID = o.oppositionID and a.caseID = c.caseID;
select * from allMyCasesClient;
-- View all bills
create or replace view myBillsClient as
select f.transactionID, f.dateOfPayment, f.description, f.amount, c.caseID, c.flair, c.status from financialtransactions f, invest i, hasa h, legalCases c
where f.transactionID = i.transactionid and i.caseID = h.caseID and h.caseID = c.caseID and h.userID = "I21p5a6t2C";
select * from myBillsClient;
-- Best suited lawyer from customer.
create or replace view BestSuitedLawyer as
select lawyer.firstname, lawyer.lastname, lawyer.userID from lawyer
where specialization="Civil" and experience >= 0 and avgTimePerCase <= 89 and charges <= 30000 and clientRating >= 3 and casesWon div casesLost >= 0;
select * from BestSuitedLawyer;
-- LAWYER
CREATE USER LawyerU@localhost IDENTIFIED BY 'pass';
CREATE ROLE Lawyer;
GRANT SELECT, INSERT, UPDATE, DELETE ON suitor.LawyerEvents TO Lawyer;
GRANT SELECT ON suitor.LawyerDeets TO Lawyer;
GRANT SELECT, UPDATE ON suitor.LawyerCases TO Lawyer;
GRANT SELECT ON suitor.otherLawyers TO Lawyer;
GRANT SELECT ON visibleDocs TO Lawyer;
GRANT SELECT ON individualsasclients TO Lawyer;
GRANT SELECT ON companyclients TO Lawyer;
GRANT Lawyer TO LawyerU@localhost;
-- View their personal details (financial history, track record).
create or replace view LawyerDeets as
select * from Lawyer where userId="A21a0f5x5m";
select * from LawyerDeets;
-- View events (such as current court hearings related to ongoing cases).
CREATE OR REPLACE VIEW LawyerEvents AS
select * from calendar
where userID = "A21a0f5x5m";
select * from LawyerEvents;
-- add new meeting
insert into LawyerEvents values("A21a0f5x5m", '2021-05-01 02:00:00',"Settlement Meeting");
-- reschedule meeting
update LawyerEvents
set
LawyerEvents.when = '2021-05-01 04:30:00'
where userID = "A21a0f5x5m" and LawyerEvents.when = '2021-05-01 02:00:00';
-- delete details of cancelled meting
delete from LawyerEvents
where userID = "A21a0f5x5m" and LawyerEvents.when = '2021-05-01 04:30:00';
-- View case details of all their cases. **** Take userID INPUT. ****
CREATE OR REPLACE VIEW LawyerCases AS
select legalcases.caseID, plaintiff, lastDateOfActivity, flair, dateOfFiling, duration, legalcases.status
from handles inner join legalcases
on legalcases.caseID=handles.caseID and handles.userID="A21a0f5x5m";
select * from LawyerCases;
-- update cases won/ increment by 1
update lawyerdeets
set casesWon = casesWon+1
where userID = "A21a0f5x5M";
-- View basic details of peers (not salary).
create or replace view otherlawyers as
select firstname, lastname, emailId, specialization, experience, casesLost, casesSettled, avgTimePerCase, clientRating from lawyer;
select * from otherlawyers;
-- View allowed documents related to any case of the law firm.
create or replace view visibleDocs as
select d.docID, d.createdOn, d.dateLastModified, d.type, c.caseID, c.lastdateofactivity, c.flair, c.status, c.plaintiff from legaldocuments d, legalcases c
where d.caseID = c.caseID and d.visibility = 1;
select * from visibleDocs;
-- View data of all their clients.
CREATE OR REPLACE VIEW IndividualsAsClients AS
select * from individualclients where userID in (
select hasa.userID
from handles inner join Lawyer
on handles.userID=lawyer.userID and lawyer.userID="A21a0f5x5m"
inner join hasa
on hasa.caseID=handles.caseID);
select * from individualsasclients;
CREATE OR REPLACE VIEW CompanyClients AS
select * from clientcompanies where userID in (
select hasa.userID
from handles inner join Lawyer
on handles.userID=lawyer.userID and lawyer.userID="A21a0f5x5m"
inner join hasa
on hasa.caseID=handles.caseID);
select * from companyclients;
-- MANAGING PARTNER
-- O21a0b2d6K Keely Duncan Moody 1986-05-01 Female 18547 20 dictum.magna.Ut@Nulla.net 9490378505 Support Staff P.O. Box 631, 8421 In Road Itagüí 76642 Antioquia
CREATE USER MPU@localhost IDENTIFIED BY 'pass';
CREATE ROLE MP;
GRANT SELECT, INSERT, UPDATE, DELETE ON suitor.myeventsmanagement TO MP;
GRANT SELECT ON suitor.mymanagingpartner TO MP;
GRANT SELECT, UPDATE, INSERT, DELETE ON suitor.allFinancialTrans TO MP;
GRANT SELECT ON suitor.ChooseLawyerRatio TO MP;
GRANT SELECT ON suitor.ChooseLawyerRating TO MP;
GRANT ALL PRIVILEGES ON suitor.* TO MP;
GRANT MP TO MPU@localhost;
-- 1. View their personal details.
create or replace view myManagingPartner as
select * from OtherStaff
where userID = "O21a0b2d6K";
select * from myManagingPartner;
-- 2. view/ add events
CREATE OR REPLACE VIEW myEventsManagement AS
select * from Calendar
where userID = "O21a0b2d6K";
select * from myEventsManagement;
-- add new meeting
insert into myEventsManagement values("O21a0b2d6K", '2021-05-01 02:00:00',"Settlement Meeting");
-- reschedule meeting
update myEventsManagement
set
myEventsManagement.when = '2021-05-01 04:30:00'
where userID = "O21a0b2d6K" and myEventsManagement.when = '2021-05-01 02:00:00';
-- delete details of cancelled meting
delete from myEventsManagement
where userID = "O21a0b2d6K" and myEventsManagement.when = '2021-05-01 04:30:00';
-- 3. view/update all financial transactions in the firm
CREATE OR REPLACE VIEW allFinancialTrans AS
select * from FinancialTransactions ;
select * from allFinancialTrans;
-- 4. Choose Lawyer on the basis of win/loss ratio
CREATE OR REPLACE VIEW ChooseLawyerRatio AS
Select * From Lawyer where round(casesWon/casesLost) in
(Select max(Ratio) from
(select userID, round(casesWon/casesLost) as Ratio from Lawyer) as latest);
select * from ChooseLawyerRatio;
-- 5. Choose lawyer on the basis of rating
CREATE OR REPLACE VIEW ChooseLawyerRating AS
Select Distinct userID, firstName, lastName, clientRating From Lawyer where clientRating in
(Select max(clientRating) from Lawyer) limit 1;
select * From ChooseLawyerRating;
-- OTHER STAFF
-- O21a0b2d6K Keely Duncan Moody 1986-05-01 Female 18547 20 dictum.magna.Ut@Nulla.net 9490378505 Support Staff P.O. Box 631, 8421 In Road Itagüí 76642 Antioquia
CREATE USER StaffU@localhost IDENTIFIED BY 'pass';
CREATE ROLE Staff;
GRANT SELECT, INSERT, UPDATE, DELETE ON suitor.myEventsStaff TO Staff;
GRANT SELECT ON suitor.myDetailsStaff TO Staff;
GRANT SELECT, UPDATE, INSERT, DELETE ON suitor.allFinancialTrans TO Staff;
GRANT Staff TO StaffU@localhost;
-- 1. View their personal details.
create or replace view myDetailsStaff as
select * from OtherStaff
where userID = "O21a0b2d6K";
select * from myDetailsStaff;
-- 2. view/ add events
CREATE OR REPLACE VIEW myEventsStaff AS
select * from Calendar
where userID = "O21a0b2d6K";
select * from myeventsStaff;
-- add new meeting
insert into myEventsStaff values("O21a0b2d6K", '2021-05-01 02:00:00',"Settlement Meeting");
-- reschedule meeting
update myEventsStaff
set
myEventsStaff.when = '2021-05-01 04:30:00'
where userID = "O21a0b2d6K" and myEventsStaff.when = '2021-05-01 02:00:00';
-- delete details of cancelled meting
delete from myEventsStaff
where userID = "O21a0b2d6K" and myEventsStaff.when = '2021-05-01 04:30:00';
select * from myEventsStaff;
-- 3. view/update all financial transactions in the firm
CREATE OR REPLACE VIEW allFinancialTrans AS
select * from FinancialTransactions ;
select * from allFinancialTrans;
-- INDEXING
-- -----------------------------------------
create index lawyer_city on Lawyer(city);
create index lawyer_lastName on Lawyer(LastName);
create index lawyer_charges on Lawyer(cHARGES);
create index lawyer_time on Lawyer(avgTimePerCase);
create index lawyer_exp on Lawyer(experience);
create index lawyer_rating on Lawyer(clientRating);
show index from lawyer;
create index doc_visibility on LegalDocuments(visibility);
show index from legaldocuments;
show index from legalcases;