/
comment.sql
executable file
·162 lines (117 loc) · 2.92 KB
/
comment.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
-- Comments
-- get all comment
CREATE PROCEDURE getAll(
-- variables
IN language_id INT,
IN site_id INT,
IN post_id INT,
IN user_id INT,
IN language_id INT,
IN status INT,
IN post_title INT,
-- pagination
IN start INT,
IN limit INT,
IN order CHAR,
-- return
OUT fetch_all, -- orders
OUT fetch_one -- count
)
BEGIN
SELECT user.*, comment.*, comment_id as array_key
@IF isset(:post_title) AND :post_title
THEN
,post_content.name
END @IF
FROM comment AS comment
LEFT JOIN user ON (comment.user_id = comment.user_id)
@IF isset(:post_title) AND :post_title
THEN
LEFT JOIN post_content ON (post_content.post_id = comment.post_id)
END @IF
WHERE 1 = 1
-- post
@IF isset(:post_id)
THEN
AND comment.post_id = :post_id
END @IF
-- post slug
@IF isset(:slug)
THEN
AND comment.post_id = (SELECT post_id FROM post_content WHERE slug = :slug LIMIT 1)
END @IF
-- user
@IF isset(:user_id)
THEN
AND comment.user_id = :user_id
END @IF
-- user
@IF isset(:language_id) AND isset(:post_title)
THEN
AND post_content.language_id = :language_id
END @IF
-- user
@IF isset(:status)
THEN
AND comment.status = :status
END @IF
ORDER BY parent_id, comment_id
@IF isset(:order) AND :order == "desc"
THEN
DESC
END @IF
@SQL_LIMIT(:start, :limit);
SELECT count(*) FROM (
@SQL_COUNT(comment_id, comment) -- this takes previous query removes limit and replaces select columns with parameter comment_id
) as count;
END
-- get one comment
CREATE PROCEDURE get(
IN comment_id INT,
OUT fetch_row,
)
BEGIN
SELECT *
FROM comment AS _
WHERE 1 = 1
@IF isset(:comment_id)
THEN
AND _.comment_id = :comment_id
END @IF
LIMIT 1;
-- SELECT `key` as array_key,value as array_value FROM comment_meta as _
-- WHERE _.comment_id = @result.comment_id
END
-- Add new comment
CREATE PROCEDURE add(
IN comment ARRAY,
OUT insert_id
)
BEGIN
-- allow only table fields and set defaults for missing values
@FILTER(:comment, comment)
INSERT INTO comment
( @KEYS(:comment) )
VALUES ( :comment )
END
-- Edit comment
CREATE PROCEDURE edit(
IN comment ARRAY,
IN id_comment INT,
OUT affected_rows
)
BEGIN
-- allow only table fields and set defaults for missing values
@FILTER(:comment, comment)
UPDATE comment
SET @LIST(:comment)
WHERE comment_id = :comment_id
END
-- Delete comment
CREATE PROCEDURE delete(
IN comment_id ARRAY,
OUT affected_rows
)
BEGIN
DELETE FROM comment WHERE comment_id IN (:comment_id)
END