-
Notifications
You must be signed in to change notification settings - Fork 1
/
pvo_v1_2023_202306031042.sql
1743 lines (1577 loc) · 67.7 KB
/
pvo_v1_2023_202306031042.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
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
--
-- Script was generated by Devart dbForge Studio for MySQL, Version 9.2.34.0
-- Product home page: http://www.devart.com/dbforge/mysql/studio
-- Script date 03-Jun-23 10:42:00 AM
-- Server version: 10.6.13
-- Client version: 4.1
--
--
-- Disable foreign keys
--
/*!40014 SET @OLD_FOREIGN_KEY_CHECKS=@@FOREIGN_KEY_CHECKS, FOREIGN_KEY_CHECKS=0 */;
--
-- Set SQL mode
--
/*!40101 SET @OLD_SQL_MODE=@@SQL_MODE, SQL_MODE='NO_AUTO_VALUE_ON_ZERO' */;
--
-- Set character set the client will use to send SQL statements to the server
--
SET NAMES 'utf8';
--
-- Set default database
--
USE pvo_v1_2023;
--
-- Drop view `view_example`
--
DROP VIEW IF EXISTS view_example CASCADE;
--
-- Drop procedure `Proc_Account_CreateActivatedAccountData`
--
DROP PROCEDURE IF EXISTS Proc_Account_CreateActivatedAccountData;
--
-- Drop table `tone`
--
DROP TABLE IF EXISTS tone;
--
-- Drop table `sys_tone`
--
DROP TABLE IF EXISTS sys_tone;
--
-- Drop table `mode`
--
DROP TABLE IF EXISTS mode;
--
-- Drop table `sys_mode`
--
DROP TABLE IF EXISTS sys_mode;
--
-- Drop table `register`
--
DROP TABLE IF EXISTS register;
--
-- Drop table `sys_register`
--
DROP TABLE IF EXISTS sys_register;
--
-- Drop table `nuance`
--
DROP TABLE IF EXISTS nuance;
--
-- Drop table `sys_nuance`
--
DROP TABLE IF EXISTS sys_nuance;
--
-- Drop table `dialect`
--
DROP TABLE IF EXISTS dialect;
--
-- Drop table `sys_dialect`
--
DROP TABLE IF EXISTS sys_dialect;
--
-- Drop view `view_concept_relationship`
--
DROP VIEW IF EXISTS view_concept_relationship CASCADE;
--
-- Drop procedure `Proc_Dictionary_TransferDictionaryData`
--
DROP PROCEDURE IF EXISTS Proc_Dictionary_TransferDictionaryData;
--
-- Drop procedure `Proc_Dictionary_CloneDictionaryData`
--
DROP PROCEDURE IF EXISTS Proc_Dictionary_CloneDictionaryData;
--
-- Drop procedure `Proc_Dictionary_DeleteDictionaryData`
--
DROP PROCEDURE IF EXISTS Proc_Dictionary_DeleteDictionaryData;
--
-- Drop table `concept_relationship`
--
DROP TABLE IF EXISTS concept_relationship;
--
-- Drop table `concept_link`
--
DROP TABLE IF EXISTS concept_link;
--
-- Drop table `sys_concept_link`
--
DROP TABLE IF EXISTS sys_concept_link;
--
-- Drop procedure `Proc_Log_GetLogsFilterPaging`
--
DROP PROCEDURE IF EXISTS Proc_Log_GetLogsFilterPaging;
--
-- Drop table `audit_log`
--
DROP TABLE IF EXISTS audit_log;
--
-- Drop view `view_example_relationship`
--
DROP VIEW IF EXISTS view_example_relationship CASCADE;
--
-- Drop table `example_relationship`
--
DROP TABLE IF EXISTS example_relationship;
--
-- Drop procedure `Proc_Dictionary_GetNumberRecord`
--
DROP PROCEDURE IF EXISTS Proc_Dictionary_GetNumberRecord;
--
-- Drop table `concept`
--
DROP TABLE IF EXISTS concept;
--
-- Drop table `example`
--
DROP TABLE IF EXISTS example;
--
-- Drop table `dictionary`
--
DROP TABLE IF EXISTS dictionary;
--
-- Drop table `example_link`
--
DROP TABLE IF EXISTS example_link;
--
-- Drop table `user_setting`
--
DROP TABLE IF EXISTS user_setting;
--
-- Drop table `user`
--
DROP TABLE IF EXISTS user;
--
-- Drop table `sys_example_link`
--
DROP TABLE IF EXISTS sys_example_link;
--
-- Set default database
--
USE pvo_v1_2023;
--
-- Create table `sys_example_link`
--
CREATE TABLE sys_example_link (
sys_example_link_id char(36) NOT NULL COMMENT 'id khóa chính',
example_link_name varchar(255) DEFAULT NULL COMMENT 'Tên loại liên kết',
example_link_type int(11) DEFAULT NULL COMMENT 'Loại liên kết',
sort_order int(11) DEFAULT NULL COMMENT 'Thứ tự sắp xếp',
created_date datetime DEFAULT NULL,
modified_date datetime DEFAULT NULL,
PRIMARY KEY (sys_example_link_id)
)
ENGINE = INNODB,
AVG_ROW_LENGTH = 1820,
CHARACTER SET utf8mb4,
COLLATE utf8mb4_general_ci,
COMMENT = 'Bảng sys loại liên kết giữa example với concept';
--
-- Create table `user`
--
CREATE TABLE user (
user_id char(36) NOT NULL COMMENT 'id khóa chính',
user_name varchar(100) DEFAULT NULL COMMENT 'Tên đăng nhập',
password char(60) DEFAULT NULL COMMENT 'Mã hash mật khẩu',
email varchar(50) DEFAULT NULL,
full_name varchar(255) DEFAULT NULL COMMENT 'Tên đầy đủ',
display_name varchar(100) DEFAULT NULL COMMENT 'Tên hiển thị',
birthday datetime DEFAULT NULL COMMENT 'Ngày sinh',
`position` varchar(255) DEFAULT NULL COMMENT 'Công việc',
avatar text DEFAULT NULL COMMENT 'Link ảnh đại diện',
status int(11) DEFAULT NULL COMMENT 'Trạng thái tài khoản',
created_date datetime DEFAULT NULL,
modified_date datetime DEFAULT NULL,
PRIMARY KEY (user_id)
)
ENGINE = INNODB,
AVG_ROW_LENGTH = 8192,
CHARACTER SET utf8mb4,
COLLATE utf8mb4_general_ci,
COMMENT = 'Bảng chứa thông tin người dùng';
--
-- Create index `email` on table `user`
--
ALTER TABLE user
ADD UNIQUE INDEX email (email);
--
-- Create index `user_name` on table `user`
--
ALTER TABLE user
ADD UNIQUE INDEX user_name (user_name);
--
-- Create table `user_setting`
--
CREATE TABLE user_setting (
user_setting_id char(36) NOT NULL COMMENT 'id khóa chính',
user_id char(36) DEFAULT NULL COMMENT 'id người dùng',
setting_key varchar(255) DEFAULT NULL COMMENT 'key',
setting_value varchar(255) DEFAULT NULL COMMENT 'value',
created_date datetime DEFAULT NULL,
modified_date datetime DEFAULT NULL,
PRIMARY KEY (user_setting_id)
)
ENGINE = INNODB,
CHARACTER SET utf8mb4,
COLLATE utf8mb4_general_ci,
COMMENT = 'Bảng chứa thông tin cấu hình của người dùng';
--
-- Create foreign key
--
ALTER TABLE user_setting
ADD CONSTRAINT FK_user_setting_user_id FOREIGN KEY (user_id)
REFERENCES user (user_id) ON DELETE CASCADE;
--
-- Create table `example_link`
--
CREATE TABLE example_link (
example_link_id char(36) NOT NULL COMMENT 'id khóa chính',
sys_example_link_id char(36) DEFAULT NULL COMMENT 'sys id',
user_id char(36) DEFAULT NULL COMMENT 'id người dùng',
example_link_name varchar(255) DEFAULT NULL COMMENT 'tên loại liên kết',
example_link_type int(11) DEFAULT NULL COMMENT 'loại liên kết',
sort_order int(11) DEFAULT NULL COMMENT 'thứ tự sắp xếp',
created_date datetime DEFAULT NULL,
modified_date datetime DEFAULT NULL,
PRIMARY KEY (example_link_id)
)
ENGINE = INNODB,
AVG_ROW_LENGTH = 1820,
CHARACTER SET utf8mb4,
COLLATE utf8mb4_general_ci,
COMMENT = 'Bảng chứa thông tin loại liên kết example-content';
--
-- Create foreign key
--
ALTER TABLE example_link
ADD CONSTRAINT FK_example_link_sys_example_link_id FOREIGN KEY (sys_example_link_id)
REFERENCES sys_example_link (sys_example_link_id) ON DELETE CASCADE;
--
-- Create foreign key
--
ALTER TABLE example_link
ADD CONSTRAINT FK_example_link_user_id FOREIGN KEY (user_id)
REFERENCES user (user_id) ON DELETE CASCADE;
--
-- Create table `dictionary`
--
CREATE TABLE dictionary (
dictionary_id char(36) NOT NULL COMMENT 'id khóa chính',
user_id char(36) DEFAULT NULL COMMENT 'id người dùng (tham chiếu bảng user)',
dictionary_name varchar(255) DEFAULT NULL COMMENT 'Tên dictionary',
last_view_at datetime DEFAULT NULL COMMENT 'Thời điểm truy cập gần nhất',
created_date datetime DEFAULT CURRENT_TIMESTAMP(),
modified_date datetime DEFAULT NULL,
PRIMARY KEY (dictionary_id)
)
ENGINE = INNODB,
AVG_ROW_LENGTH = 4096,
CHARACTER SET utf8mb4,
COLLATE utf8mb4_general_ci,
COMMENT = 'Bảng chứa thông tin dictionary';
--
-- Create foreign key
--
ALTER TABLE dictionary
ADD CONSTRAINT FK_dictionary_user_id FOREIGN KEY (user_id)
REFERENCES user (user_id) ON DELETE CASCADE;
--
-- Create table `example`
--
CREATE TABLE example (
example_id char(36) NOT NULL,
dictionary_id char(36) DEFAULT NULL,
detail text DEFAULT NULL,
detail_html text DEFAULT NULL,
note varchar(255) DEFAULT NULL,
tone_id char(36) DEFAULT NULL,
register_id char(36) DEFAULT NULL,
dialect_id char(36) DEFAULT NULL,
mode_id char(36) DEFAULT NULL,
nuance_id char(36) DEFAULT NULL,
created_date datetime DEFAULT NULL,
modified_date datetime DEFAULT NULL,
PRIMARY KEY (example_id)
)
ENGINE = INNODB,
AVG_ROW_LENGTH = 1820,
CHARACTER SET utf8mb4,
COLLATE utf8mb4_general_ci,
COMMENT = 'Bảng chứa thông tin example';
--
-- Create foreign key
--
ALTER TABLE example
ADD CONSTRAINT FK_example_dictionary_id FOREIGN KEY (dictionary_id)
REFERENCES dictionary (dictionary_id) ON DELETE CASCADE;
--
-- Create table `concept`
--
CREATE TABLE concept (
concept_id char(36) NOT NULL COMMENT 'id khóa chính',
dictionary_id char(36) DEFAULT NULL COMMENT 'id dictionary (tham chiếu bảng dictionary)',
title varchar(255) DEFAULT NULL COMMENT 'Tên concept',
description varchar(500) DEFAULT NULL COMMENT 'Mô tả concept',
created_date datetime DEFAULT NULL,
modified_date datetime DEFAULT NULL,
PRIMARY KEY (concept_id)
)
ENGINE = INNODB,
AVG_ROW_LENGTH = 744,
CHARACTER SET utf8mb4,
COLLATE utf8mb4_general_ci,
COMMENT = 'Bảng chứa thông tin concept';
--
-- Create foreign key
--
ALTER TABLE concept
ADD CONSTRAINT FK_concept_dictionary_id FOREIGN KEY (dictionary_id)
REFERENCES dictionary (dictionary_id) ON DELETE CASCADE;
DELIMITER $$
--
-- Create procedure `Proc_Dictionary_GetNumberRecord`
--
CREATE
DEFINER = 'root'@'localhost'
PROCEDURE Proc_Dictionary_GetNumberRecord (IN $DictionaryId char(36), OUT $NumberConcept int, OUT $NumberExample int)
BEGIN
SET $NumberConcept = (SELECT
COUNT(1)
FROM concept
WHERE dictionary_id = $DictionaryId);
SET $NumberExample = (SELECT
COUNT(1)
FROM example
WHERE dictionary_id = $DictionaryId);
END
$$
DELIMITER ;
--
-- Create table `example_relationship`
--
CREATE TABLE example_relationship (
example_relationship_id int(11) NOT NULL AUTO_INCREMENT COMMENT 'id khóa chính',
dictionary_id char(36) DEFAULT NULL COMMENT 'id dictionary',
concept_id char(36) DEFAULT NULL,
example_id char(36) DEFAULT NULL,
example_link_id char(36) DEFAULT NULL,
created_date datetime DEFAULT NULL,
modified_date datetime DEFAULT NULL,
PRIMARY KEY (example_relationship_id)
)
ENGINE = INNODB,
AUTO_INCREMENT = 5,
AVG_ROW_LENGTH = 8192,
CHARACTER SET utf8mb4,
COLLATE utf8mb4_general_ci,
COMMENT = 'Bảng chứa liên kết example-content';
--
-- Create foreign key
--
ALTER TABLE example_relationship
ADD CONSTRAINT FK_example_relationship_concept_id FOREIGN KEY (concept_id)
REFERENCES concept (concept_id) ON DELETE CASCADE;
--
-- Create foreign key
--
ALTER TABLE example_relationship
ADD CONSTRAINT FK_example_relationship_dictionary_id FOREIGN KEY (dictionary_id)
REFERENCES dictionary (dictionary_id) ON DELETE CASCADE;
--
-- Create foreign key
--
ALTER TABLE example_relationship
ADD CONSTRAINT FK_example_relationship_example_id FOREIGN KEY (example_id)
REFERENCES example (example_id) ON DELETE CASCADE;
--
-- Create foreign key
--
ALTER TABLE example_relationship
ADD CONSTRAINT FK_example_relationship_example_link_id FOREIGN KEY (example_link_id)
REFERENCES example_link (example_link_id) ON DELETE CASCADE;
--
-- Create view `view_example_relationship`
--
CREATE
DEFINER = 'root'@'localhost'
VIEW view_example_relationship
AS
SELECT
`er`.`dictionary_id` AS `dictionary_id`,
`c`.`concept_id` AS `concept_id`,
`c`.`title` AS `concept`,
`e`.`example_id` AS `example_id`,
`e`.`detail` AS `example`,
`e`.`detail_html` AS `example_html`,
`el`.`example_link_id` AS `example_link_id`,
`el`.`example_link_name` AS `example_link_name`
FROM (((`example_relationship` `er`
JOIN `concept` `c`
ON (`er`.`concept_id` = `c`.`concept_id`))
JOIN `example` `e`
ON (`er`.`example_id` = `e`.`example_id`))
LEFT JOIN `example_link` `el`
ON (`er`.`example_link_id` = `el`.`example_link_id`));
--
-- Create table `audit_log`
--
CREATE TABLE audit_log (
audit_log_id int(11) NOT NULL AUTO_INCREMENT COMMENT 'id khóa chính',
user_id char(36) DEFAULT NULL COMMENT 'id người dùng',
screen_info varchar(255) DEFAULT NULL COMMENT 'Thông tin màn hình',
action_type int(11) DEFAULT NULL COMMENT 'Loại hành động',
reference varchar(255) DEFAULT NULL COMMENT 'Thông tin tham chiếu, vd: id dictionary',
description varchar(500) DEFAULT NULL COMMENT 'Mô tả',
user_agent varchar(255) DEFAULT NULL,
created_date datetime DEFAULT NULL,
modified_date varchar(255) DEFAULT NULL,
PRIMARY KEY (audit_log_id)
)
ENGINE = INNODB,
AUTO_INCREMENT = 14,
AVG_ROW_LENGTH = 3276,
CHARACTER SET utf8mb4,
COLLATE utf8mb4_general_ci,
COMMENT = 'Bảng chứa thông tin lịch sử truy cập';
--
-- Create foreign key
--
ALTER TABLE audit_log
ADD CONSTRAINT FK_audit_log_user_id FOREIGN KEY (user_id)
REFERENCES user (user_id) ON DELETE CASCADE;
DELIMITER $$
--
-- Create procedure `Proc_Log_GetLogsFilterPaging`
--
CREATE
DEFINER = 'root'@'localhost'
PROCEDURE Proc_Log_GetLogsFilterPaging (IN $UserId char(36), IN $SearchFilter varchar(255), IN $PageIndex int, IN $PageSize int, IN $DateFrom datetime, IN $DateTo datetime, OUT $TotalRecord int, OUT $TotalPage int)
COMMENT 'Store lấy dữ liệu filter, phân trang audit log'
BEGIN
IF $PageSize > 0 THEN
SELECT
al.audit_log_id,
al.user_id,
al.screen_info,
al.action_type,
al.reference,
al.description,
al.user_agent,
al.created_date
FROM audit_log al
WHERE al.user_id = $UserId
AND (
$SearchFilter IS NULL
OR $SearchFilter = ""
OR (
al.screen_info LIKE CONCAT('%', $SearchFilter, '%')
OR al.description LIKE CONCAT('%', $SearchFilter, '%')
OR al.user_agent LIKE CONCAT('%', $SearchFilter, '%')
-- action_type ???
)
)
AND (
$DateFrom IS NULL
OR al.created_date >= $DateFrom
)
AND (
$DateTo IS NULL
OR al.created_date <= $DateTo
)
ORDER BY al.created_date DESC
LIMIT $PageSize OFFSET $PageIndex;
-- Tổng số bản ghi
SET $TotalRecord = (SELECT
COUNT(al.audit_log_id)
FROM audit_log al
WHERE al.user_id = $UserId
AND (
$SearchFilter IS NULL
OR $SearchFilter = ""
OR (
al.screen_info LIKE CONCAT('%', $SearchFilter, '%')
OR al.description LIKE CONCAT('%', $SearchFilter, '%')
OR al.user_agent LIKE CONCAT('%', $SearchFilter, '%')
-- action_type ???
)
)
AND (
$DateFrom IS NULL
OR al.created_date >= $DateFrom
)
AND (
$DateTo IS NULL
OR al.created_date <= $DateTo
));
-- Tổng số trang
SET $TotalPage = CEILING(1.0 * $TotalRecord / $PageSize);
END IF;
END
$$
DELIMITER ;
--
-- Create table `sys_concept_link`
--
CREATE TABLE sys_concept_link (
sys_concept_link_id char(36) NOT NULL COMMENT 'id khóa chính',
concept_link_name varchar(255) DEFAULT NULL COMMENT 'Tên loại liên kết',
concept_link_type int(11) DEFAULT NULL COMMENT 'Loại liên kết',
sort_order int(11) DEFAULT NULL COMMENT 'Thứ tự sắp xếp',
created_date datetime DEFAULT NULL,
modified_date datetime DEFAULT NULL,
PRIMARY KEY (sys_concept_link_id)
)
ENGINE = INNODB,
AVG_ROW_LENGTH = 5461,
CHARACTER SET utf8mb4,
COLLATE utf8mb4_general_ci,
COMMENT = 'Bảng sys loại liên kết giữa concept với concept';
--
-- Create table `concept_link`
--
CREATE TABLE concept_link (
concept_link_id char(36) NOT NULL COMMENT 'id khóa chính',
sys_concept_link_id char(36) DEFAULT NULL COMMENT 'id bảng sys',
user_id char(36) DEFAULT NULL COMMENT 'id người dùng',
concept_link_name varchar(255) DEFAULT NULL COMMENT 'Tên loại liên kết',
concept_link_type int(11) DEFAULT NULL COMMENT 'Loại liên kết',
sort_order int(11) DEFAULT NULL COMMENT 'Thứ tự sắp xếp',
created_date datetime DEFAULT NULL,
modified_date datetime DEFAULT NULL,
PRIMARY KEY (concept_link_id)
)
ENGINE = INNODB,
AVG_ROW_LENGTH = 5461,
CHARACTER SET utf8mb4,
COLLATE utf8mb4_general_ci,
COMMENT = 'Bảng chứa thông tin loại liên kết concept-concept';
--
-- Create foreign key
--
ALTER TABLE concept_link
ADD CONSTRAINT FK_concept_link_sys_concept_link_id FOREIGN KEY (sys_concept_link_id)
REFERENCES sys_concept_link (sys_concept_link_id) ON DELETE CASCADE;
--
-- Create foreign key
--
ALTER TABLE concept_link
ADD CONSTRAINT FK_concept_link_user_id FOREIGN KEY (user_id)
REFERENCES user (user_id) ON DELETE CASCADE;
--
-- Create table `concept_relationship`
--
CREATE TABLE concept_relationship (
concept_relationship_id int(11) NOT NULL AUTO_INCREMENT COMMENT 'id khóa chính',
dictionary_id char(36) DEFAULT NULL COMMENT 'id dictionảy',
concept_id char(36) DEFAULT NULL COMMENT 'id concept',
parent_id char(36) DEFAULT NULL COMMENT 'id concept cha',
concept_link_id char(36) DEFAULT NULL COMMENT 'id loại liên kết concept-concept',
created_date datetime DEFAULT NULL,
modified_date datetime DEFAULT NULL,
PRIMARY KEY (concept_relationship_id)
)
ENGINE = INNODB,
AUTO_INCREMENT = 36,
AVG_ROW_LENGTH = 819,
CHARACTER SET utf8mb4,
COLLATE utf8mb4_general_ci,
COMMENT = 'Bảng chứa liên kết concept-concept';
--
-- Create foreign key
--
ALTER TABLE concept_relationship
ADD CONSTRAINT FK_concept_relationship_concept_id FOREIGN KEY (concept_id)
REFERENCES concept (concept_id) ON DELETE CASCADE;
--
-- Create foreign key
--
ALTER TABLE concept_relationship
ADD CONSTRAINT FK_concept_relationship_concept_link_id FOREIGN KEY (concept_link_id)
REFERENCES concept_link (concept_link_id) ON DELETE CASCADE;
--
-- Create foreign key
--
ALTER TABLE concept_relationship
ADD CONSTRAINT FK_concept_relationship_dictionary_id FOREIGN KEY (dictionary_id)
REFERENCES dictionary (dictionary_id) ON DELETE CASCADE;
--
-- Create foreign key
--
ALTER TABLE concept_relationship
ADD CONSTRAINT FK_concept_relationship_parent_id FOREIGN KEY (parent_id)
REFERENCES concept (concept_id) ON DELETE CASCADE;
DELIMITER $$
--
-- Create procedure `Proc_Dictionary_DeleteDictionaryData`
--
CREATE
DEFINER = 'root'@'localhost'
PROCEDURE Proc_Dictionary_DeleteDictionaryData (IN $DictionaryId char(36))
COMMENT 'Store thực hiện xóa dữ liệu của 1 dictionary'
BEGIN
DECLARE EXIT HANDLER FOR SQLEXCEPTION
BEGIN
ROLLBACK;
RESIGNAL;
END;
START TRANSACTION;
-- ==================================================================================================================
-- ====================================== Xóa dữ liệu của dictionary =======================================
-- ==================================================================================================================
DELETE
FROM concept_relationship
WHERE dictionary_id = $DictionaryId;
DELETE
FROM example_relationship
WHERE dictionary_id = $DictionaryId;
DELETE
FROM concept
WHERE dictionary_id = $DictionaryId;
DELETE
FROM example
WHERE dictionary_id = $DictionaryId;
COMMIT;
END
$$
--
-- Create procedure `Proc_Dictionary_CloneDictionaryData`
--
CREATE
DEFINER = 'root'@'localhost'
PROCEDURE Proc_Dictionary_CloneDictionaryData (IN $SourceDictionaryId char(36), IN $DestDictionaryId char(36))
COMMENT 'Store thực hiện clone dữ liệu từ điển (có xóa dữ liệu từ điển đích)'
BEGIN
DECLARE EXIT HANDLER FOR SQLEXCEPTION
BEGIN
ROLLBACK;
RESIGNAL;
END;
START TRANSACTION;
-- ==================================================================================================================
-- ====================================== Xóa dữ liệu của dictionary đích ===================================
-- ==================================================================================================================
DELETE
FROM concept_relationship
WHERE dictionary_id = $DestDictionaryId;
DELETE
FROM example_relationship
WHERE dictionary_id = $DestDictionaryId;
DELETE
FROM concept
WHERE dictionary_id = $DestDictionaryId;
DELETE
FROM example
WHERE dictionary_id = $DestDictionaryId;
-- ==================================================================================================================
-- ====================================== Chuẩn bị bảng tạm map id phần tử cũ và phần tử mới ========================
-- ==================================================================================================================
-- Bảng tạm map id concept cũ và concept mới
DROP TEMPORARY TABLE IF EXISTS tmpMapConcept;
CREATE TEMPORARY TABLE tmpMapConcept (
old_concept_id char(36),
new_concept_id char(36)
);
INSERT INTO tmpMapConcept (old_concept_id, new_concept_id)
SELECT
c.concept_id,
UUID()
FROM concept c
WHERE c.dictionary_id = $SourceDictionaryId;
-- Bảng tạm map id example cũ và example mới
DROP TEMPORARY TABLE IF EXISTS tmpMapExample;
CREATE TEMPORARY TABLE tmpMapExample (
old_example_id char(36),
new_example_id char(36)
);
INSERT INTO tmpMapExample (old_example_id, new_example_id)
SELECT
e.example_id,
UUID()
FROM example e
WHERE e.dictionary_id = $SourceDictionaryId;
-- ==================================================================================================================
-- ====================================== Insert dữ liệu clone ======================================================
-- ==================================================================================================================
INSERT INTO concept (concept_id, dictionary_id, title, description, created_date, modified_date)
SELECT
mc.new_concept_id,
$DestDictionaryId,
c.title,
c.description,
NOW(),
NULL
FROM concept c
JOIN tmpMapConcept mc
ON c.concept_id = mc.old_concept_id
WHERE c.dictionary_id = $SourceDictionaryId;
INSERT INTO concept_relationship (dictionary_id, concept_id, parent_id, concept_link_id, created_date, modified_date)
SELECT
$DestDictionaryId,
mc.new_concept_id,
mc1.new_concept_id,
cr.concept_link_id,
NOW(),
NULL
FROM concept_relationship cr
JOIN tmpMapConcept mc
ON cr.concept_id = mc.old_concept_id
JOIN tmpMapConcept mc1
ON cr.parent_id = mc1.old_concept_id
WHERE cr.dictionary_id = $SourceDictionaryId;
INSERT INTO example (example_id, dictionary_id, detail, detail_html, note, tone_id, register_id, dialect_id, mode_id, nuance_id, created_date, modified_date)
SELECT
me.new_example_id,
$DestDictionaryId,
e.detail,
e.detail_html,
e.note,
e.tone_id,
e.register_id,
e.dialect_id,
e.mode_id,
e.nuance_id,
NOW(),
NULL
FROM example e
JOIN tmpMapExample me
ON e.example_id = me.old_example_id
WHERE e.dictionary_id = $SourceDictionaryId;
INSERT INTO example_relationship (dictionary_id, concept_id, example_id, example_link_id, created_date, modified_date)
SELECT
$DestDictionaryId,
mc.new_concept_id,
me.new_example_id,
er.example_link_id,
NOW(),
NULL
FROM example_relationship er
JOIN tmpMapConcept mc
ON er.concept_id = mc.old_concept_id
JOIN tmpMapExample me
ON er.example_id = me.old_example_id
WHERE er.dictionary_id = $SourceDictionaryId;
COMMIT;
END
$$
--
-- Create procedure `Proc_Dictionary_TransferDictionaryData`
--
CREATE
DEFINER = 'root'@'localhost'
PROCEDURE Proc_Dictionary_TransferDictionaryData (IN $SourceDictionaryId char(36), IN $DestDictionaryId char(36), IN $IsDeleteData bit)
COMMENT 'Store thực hiện copy dữ liệu từ từ điển nguồn và gộp vào dữ liệu ở từ điển đích'
BEGIN
DECLARE EXIT HANDLER FOR SQLEXCEPTION
BEGIN
ROLLBACK;
RESIGNAL;
END;
IF $IsDeleteData = TRUE THEN
CALL Proc_Dictionary_CloneDictionaryData($SourceDictionaryId, $DestDictionaryId);
ELSE
BEGIN
START TRANSACTION;
-- ==================================================================================================================
-- ====================================== Chuẩn bị bảng tạm map id phần tử cũ và phần tử mới ========================
-- ==================================================================================================================
-- Bảng tạm map id concept nguồn và concept đích
DROP TEMPORARY TABLE IF EXISTS tmpMapConcept;
CREATE TEMPORARY TABLE tmpMapConcept (
source_concept_id char(36),
dest_concept_id char(36),
new_concept_id char(36)
);
INSERT INTO tmpMapConcept (source_concept_id, dest_concept_id, new_concept_id)
SELECT
c.concept_id,
c1.concept_id,
IFNULL(c1.concept_id, UUID())
FROM concept c
LEFT JOIN concept c1
ON c.title = c1.title
AND c1.dictionary_id = $DestDictionaryId
WHERE c.dictionary_id = $SourceDictionaryId;
-- Bảng tạm map id example cũ và example mới
DROP TEMPORARY TABLE IF EXISTS tmpMapExample;
CREATE TEMPORARY TABLE tmpMapExample (
source_example_id char(36),
dest_example_id char(36),
new_example_id char(36)
);
INSERT INTO tmpMapExample (source_example_id, dest_example_id, new_example_id)
SELECT
e.example_id,
e1.example_id,
IFNULL(e1.example_id, UUID())
FROM example e
LEFT JOIN example e1
ON e.detail_html = e1.detail_html
AND e1.dictionary_id = $DestDictionaryId
WHERE e.dictionary_id = $SourceDictionaryId;
-- ==================================================================================================================
-- ====================================== Insert dữ liệu copy =======================================================
-- ==================================================================================================================
INSERT INTO concept (concept_id, dictionary_id, title, description, created_date, modified_date)
SELECT
mc.new_concept_id,
$DestDictionaryId,
c.title,
c.description,
NOW(),
NULL
FROM concept c
JOIN tmpMapConcept mc
ON c.concept_id = mc.source_concept_id
AND mc.dest_concept_id IS NULL
WHERE c.dictionary_id = $SourceDictionaryId;
INSERT INTO example (example_id, dictionary_id, detail, detail_html, note, tone_id, register_id, dialect_id, mode_id, nuance_id, created_date, modified_date)
SELECT
me.new_example_id,
$DestDictionaryId,
e.detail,
e.detail_html,
e.note,
e.tone_id,
e.register_id,
e.dialect_id,
e.mode_id,
e.nuance_id,
NOW(),
NULL
FROM example e
JOIN tmpMapExample me
ON e.example_id = me.source_example_id
AND me.dest_example_id IS NULL
WHERE e.dictionary_id = $SourceDictionaryId;
INSERT INTO concept_relationship (dictionary_id, concept_id, parent_id, concept_link_id, created_date, modified_date)
SELECT
$DestDictionaryId,
tmpSrc.id1,
tmpSrc.id2,
tmpSrc.link_id,
NOW(),
NULL
FROM (SELECT
mc.new_concept_id id1,
mc1.new_concept_id id2,
cr.concept_link_id link_id
FROM concept_relationship cr
JOIN tmpMapConcept mc
ON cr.concept_id = mc.source_concept_id
JOIN tmpMapConcept mc1
ON cr.parent_id = mc1.source_concept_id
WHERE cr.dictionary_id = $SourceDictionaryId) tmpSrc
LEFT JOIN (SELECT
cr1.concept_id id1,
cr1.parent_id id2
FROM concept_relationship cr1
WHERE cr1.dictionary_id = $DestDictionaryId
UNION
SELECT
cr1.parent_id id1,
cr1.concept_id id2
FROM concept_relationship cr1
WHERE cr1.dictionary_id = $DestDictionaryId) tmpDest
ON tmpSrc.id1 = tmpDest.id1
AND tmpSrc.id2 = tmpDest.id2
WHERE tmpDest.id1 IS NULL
AND tmpDest.id2 IS NULL;
INSERT INTO example_relationship (dictionary_id, concept_id, example_id, example_link_id, created_date, modified_date)
SELECT
$DestDictionaryId,
tmpSrc.c_id,
tmpSrc.e_id,
tmpSrc.link_id,
NOW(),
NULL
FROM (SELECT
mc.new_concept_id c_id,
me.new_example_id e_id,
er.example_link_id link_id
FROM example_relationship er
JOIN tmpMapConcept mc
ON er.concept_id = mc.source_concept_id
JOIN tmpMapExample me