-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathall codes.txt
More file actions
372 lines (247 loc) · 9.77 KB
/
all codes.txt
File metadata and controls
372 lines (247 loc) · 9.77 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
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
366
367
368
369
370
371
372
create table user_account(
userid int not null PRIMARY KEY AUTO_INCREMENT,
username VARCHAR(50) BINARY NOT NULL UNIQUE,
password VARCHAR(255) NOT NULL,
role VARCHAR(50) NOT NULL,
date_created TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
money integer default 0,
delay integer default 0
);
#
delimiter $$
CREATE TRIGGER usernamegsix BEFORE insert ON user_account
FOR EACH row
BEGIN
DECLARE numLength INT;
SET numLength = (SELECT LENGTH(NEW.username));
IF (numLength < 6) THEN
SIGNAL SQLSTATE '45000' set message_text='Username must consist of 6 characters at least!!!';
END IF;
end;
#
insert into user_account(username, password)
values('234234234234', 'asd');
#
# SELECT * FROM user_account WHERE BINARY username = 'amirmohammad'
#
SELECT * FROM user_account WHERE username REGEXP '^[A-Za-z0-9]+$';
#
delimiter $$
CREATE TRIGGER usernamecomplexity BEFORE INSERT ON user_account
FOR EACH ROW
BEGIN
IF NOT (SELECT NEW.username REGEXP '[A-Za-z]' and NEW.username REGEXP '[0-9]') THEN
SIGNAL sqlstate '45000' set message_text = 'Username must have both alphabets an numbers!!';
END IF;
END;
SELECT '234234234234' REGEXP '[A-Za-z]' and '234234234234' REGEXP '[0-9]'
######################
delimiter $$
CREATE TRIGGER passwordg8 BEFORE insert ON user_account
FOR EACH row
BEGIN
DECLARE numLength INT;
SET numLength = (SELECT LENGTH(NEW.password));
IF (numLength < 8) THEN
SIGNAL SQLSTATE '45000' set message_text='Password must consist of 8 characters at least!!!';
END IF;
end;
##########################################
#
delimiter $$
CREATE TRIGGER passwordcomplexity BEFORE INSERT ON user_account
FOR EACH ROW
BEGIN
IF NOT (SELECT NEW.password REGEXP '[A-Za-z]' and NEW.password REGEXP '[0-9]') THEN
SIGNAL sqlstate '45000' set message_text = 'Password must have both alphabets an numbers!!';
else
set new.password = SHA2(new.password, 224);
END IF;
END;
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
CREATE TABLE child (
parent_id INT,
INDEX par_ind (parent_id),
FOREIGN KEY (parent_id)
REFERENCES user_account(userid)
ON DELETE CASCADE
);
(((((((((((((((((((())))))))))))))))))))
INSERT INTO user_account (username, password)
VALUES ('tom37482394stills', 'asdkjahsdkj7387928388');
INSERT INTO child (parent_id)
VALUES (LAST_INSERT_ID());
################################################### create table user_informatio
create table user_information (
address varchar(512) not null,
fname varchar(50) not null,
surname varchar(50) not null,
role varchar(50) not null,
userid int not null,
FOREIGN KEY (`userid`) REFERENCES `user_account` (`userid`) ON DELETE CASCADE
);
##################################### join user account and user informations
SELECT user_account.userid, user_account.username, user_information.fname, user_information.surname
FROM user_account
INNER JOIN user_information ON user_information.userid = user_account.userid
where user_account.id = 1;
################################### see table fields
DESCRIBE user_account;
DESCRIBE user_information;
$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$ book table
create table book(
bookid int not null PRIMARY KEY AUTO_INCREMENT,
types varchar(40) not null,
name VARCHAR(50) BINARY NOT NULL UNIQUE,
writer VARCHAR(50) NOT NULL,
date date not null,
verion int,
count int,
price int
);
insert into book(name, writer, date, verion, count, types)
values ('100', 'amir', '2020-12-12', 0, 3, 'amoozeshi');
##################################################check money
delimiter $$
CREATE TRIGGER checkvalue BEFORE insert ON user_account
FOR EACH row
BEGIN
IF (select sign(new.money)) < 1 THEN
SIGNAL SQLSTATE '45000' SET MESSAGE_TEXT = 'The money that you insert must be over 0 toman.';
END IF;
end;
delimiter $$
CREATE TRIGGER checkbvalue BEFORE update ON user_account
FOR EACH row
BEGIN
IF (select sign(new.money)) < 1 THEN
SIGNAL SQLSTATE '45000' SET MESSAGE_TEXT = 'The money that you insert must be over 0 toman.';
END IF;
end;
$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$ count of book
delimiter $$
CREATE TRIGGER bookcount BEFORE update ON book
FOR EACH row
BEGIN
IF (select sign(new.count) = -1) and (select sign(old.count) = 0) THEN
SIGNAL SQLSTATE '45000' SET MESSAGE_TEXT = 'This book is not currently available.';
END IF;
end;
#################################### check role to borrow
select * from user_account u
join book b where u.role = 'student' and (b.types = '' or b.types = 'amoozeshi') and u.userid = 1;
########################################################## kam kardane poool
delimiter $$
CREATE TRIGGER reduce_money BEFORE update ON user_account
FOR EACH row
BEGIN
IF (select sign(new.money) = -1) THEN
SIGNAL SQLSTATE '45000' SET MESSAGE_TEXT = 'You do not have enough money.';
END IF;
end;
delimiter $$
CREATE TRIGGER reduce_money BEFORE update ON user_account
FOR EACH row
BEGIN
IF (select sign(old.money - new.money) = -1) THEN
SIGNAL SQLSTATE '45000' SET MESSAGE_TEXT = 'This book is not currently available.';
END IF;
end;
$$$$$$$$$$$$$$$$$$$$ test kam shodan az hesab
update user_account u
join book b
set u.money = u.money - ( b.price * 5 ) / 100
where u.userid = 1 and b.bookid = 1;
$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$ operations in get book successfull or not
create table getbook_opt (
message varchar(512) not null,
operation BOOLEAN not null,
userid int not null,
FOREIGN KEY (`userid`) REFERENCES `user_account` (`userid`) ON DELETE CASCADE
);
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% successful request to admin inbox
create table inbox (
inboxid int not null PRIMARY KEY AUTO_INCREMENT,
message varchar(512) not null,
operation BOOLEAN not null default True,
userid int not null,
bookid int not null,
FOREIGN KEY (`userid`) REFERENCES `user_account` (`userid`) ON DELETE CASCADE,
FOREIGN KEY (`bookid`) REFERENCES `book` (`bookid`) ON DELETE CASCADE,
date_created TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);
$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$ join bara inbox
select book.name, inbox.inboxid from book
join inbox where book.bookid = inbox.bookid;
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% list darkhast ha
select * from inbox order by date_created DESC;
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% ezafe kardan be zaman
UPDATE `user_account` SET `date_created` = DATE_ADD(`date_created` , INTERVAL 1 DAY) WHERE `userid` = 1;
delimiter $$
CREATE TRIGGER deliver_date BEFORE INSERT ON `inbox`
FOR EACH ROW SET
NEW.deliver_date = TIMESTAMPADD(DAY, 14, NEW.deliver_date);
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% add delivered or not in inbox
alter table `inbox`
add column delivered boolean default False;
alter table `inbox`
add column delays boolean default False;
************************************************************* compare date
select '2000-12-12' < CURDATE();
########################################################### trigger to check delay in deliver
delimiter $$
CREATE TRIGGER check_delay BEFORE update ON `inbox`
FOR EACH row
BEGIN
IF old.deliver_date > CURDATE() THEN
set new.delays = False;
END IF;
END;
delimiter $$
CREATE TRIGGER check_delay BEFORE update ON `inbox`
FOR EACH row
BEGIN
IF deliver_date > CURDATE() THEN
update inbox set delays = False;
END IF;
END;
********************************************************************* hash password
delimiter $$
CREATE TRIGGER `hash_password`
AFTER INSERT ON `user_account` FOR EACH ROW
BEGIN
UPDATE user_account
SET password = SHA2(new.password, 224);
END
&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&& deliver book table
create table deliver_book (
deliver_id int not null PRIMARY KEY AUTO_INCREMENT,
message varchar(512) not null,
userid int not null,
bookid int not null,
FOREIGN KEY (`userid`) REFERENCES `user_account` (`userid`) ON DELETE CASCADE,
FOREIGN KEY (`bookid`) REFERENCES `book` (`bookid`) ON DELETE CASCADE,
date_delivered TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);
((((((((((((((((((((((((((((((((((((((((((())))))))))))))))))))))))))))))))))))))))))) check count and version of book in add book
delimiter $$
CREATE TRIGGER count_version_book BEFORE insert ON book
FOR EACH row
BEGIN
IF (select sign(new.count) = -1) or (select sign(new.count) = 0) or (select sign(new.verion) = -1) or (select sign(new.verion) = 0) THEN
SIGNAL SQLSTATE '45000' SET MESSAGE_TEXT = 'نسخه و تعداد کتاب وارد شده باید بزرگتر از صفر باشد';
END IF;
end;
&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&& chech count in update
delimiter $$
CREATE TRIGGER count_version_book1 BEFORE update ON book
FOR EACH row
BEGIN
IF (select sign(new.count) = -1) or (select sign(new.count) = 0) or (select sign(new.verion) = -1) or (select sign(new.verion) = 0) THEN
SIGNAL SQLSTATE '45000' SET MESSAGE_TEXT = 'نسخه و تعداد کتاب وارد شده باید بزرگتر از صفر باشد';
END IF;
end;
%%%%%%%%%%%%%%%%%%%%%%%%%%%%
$ export FLASK_APP=main
$ export FLASK_ENV=development
$ flask run