diff options
author | Yorhel <git@yorhel.nl> | 2019-12-16 11:45:30 +0100 |
---|---|---|
committer | Yorhel <git@yorhel.nl> | 2019-12-16 11:49:24 +0100 |
commit | cf7680cbf2d1088ed2ff087c9b35c3ccc52225f9 (patch) | |
tree | f30a6ec055f5e92d9d03f9cba9c9885e1317a06e /lib/VNWeb/Discussions | |
parent | 2c4203b57652e0c3fdc9bd10973754e911f43b36 (diff) |
Minor refactor: Make better use of SQL::Interp for INSERT INTO clauses
It's like I forgot halfway through that SQL::Interp has special, and
very useful, syntax for inserts
This also reduces the number of queries in Discussions::Edit a bit, as
there's no more need for the INSERT-followed-by-UPDATE hack.
Diffstat (limited to 'lib/VNWeb/Discussions')
-rw-r--r-- | lib/VNWeb/Discussions/Edit.pm | 50 | ||||
-rw-r--r-- | lib/VNWeb/Discussions/Thread.pm | 4 |
2 files changed, 29 insertions, 25 deletions
diff --git a/lib/VNWeb/Discussions/Edit.pm b/lib/VNWeb/Discussions/Edit.pm index ab1783a5..f552d5e4 100644 --- a/lib/VNWeb/Discussions/Edit.pm +++ b/lib/VNWeb/Discussions/Edit.pm @@ -120,38 +120,42 @@ json_api qr{/t/edit\.json}, $FORM_IN, sub { ) } - $tid = tuwf->dbVali('INSERT INTO threads (count) VALUES (1) RETURNING id') if !$tid; - tuwf->dbExeci('UPDATE threads SET', { - title => $data->{title}, - poll_question => $data->{poll} ? $data->{poll}{question} : undef, - poll_max_options => $data->{poll} ? $data->{poll}{max_options} : 1, - auth->permBoardmod ? ( - hidden => $data->{hidden}, - locked => $data->{locked}, - ) : (), - auth->permBoardmod || auth->permDbmod || auth->permUsermod ? ( - private => $data->{private} - ) : (), - }, 'WHERE id =', \$tid - ) if $num == 1; + my $thread = { + title => $data->{title}, + poll_question => $data->{poll} ? $data->{poll}{question} : undef, + poll_max_options => $data->{poll} ? $data->{poll}{max_options} : 1, + $tid ? () : (count => 1), + auth->permBoardmod ? ( + hidden => $data->{hidden}, + locked => $data->{locked}, + ) : (), + auth->permBoardmod || auth->permDbmod || auth->permUsermod ? ( + private => $data->{private} + ) : (), + }; + tuwf->dbExeci('UPDATE threads SET', $thread, 'WHERE id =', \$tid) if $tid && $num == 1; + $tid = tuwf->dbVali('INSERT INTO threads', $thread, 'RETURNING id') if !$tid; if($num == 1) { tuwf->dbExeci('DELETE FROM threads_boards WHERE tid =', \$tid); - tuwf->dbExeci('INSERT INTO threads_boards (tid, type, iid) VALUES (', sql_comma(\$tid, \$_->{btype}, \($_->{iid}//0)), ')') for $data->{boards}->@*; + tuwf->dbExeci('INSERT INTO threads_boards', { tid => $tid, type => $_->{btype}, iid => $_->{iid}//0 }) for $data->{boards}->@*; } if($pollchanged) { tuwf->dbExeci('DELETE FROM threads_poll_options WHERE tid =', \$tid); - tuwf->dbExeci('INSERT INTO threads_poll_options (tid, option) VALUES (', sql_comma(\$tid, \"$_"), ')') for $data->{poll}{options}->@*; + tuwf->dbExeci('INSERT INTO threads_poll_options', { tid => $tid, option => $_ }) for $data->{poll}{options}->@*; } - tuwf->dbExeci('INSERT INTO threads_posts (tid, num, uid) VALUES (', sql_comma(\$tid, 1, \auth->uid), ')') if !$data->{tid}; - tuwf->dbExeci('UPDATE threads_posts SET', sql_comma( - sql('msg =', \bb_subst_links $data->{msg}), - auth->permBoardmod ? sql('hidden =', \$data->{hidden}) : (), - auth->permBoardmod && $data->{nolastmod} ? () : 'edited = NOW()', - ), 'WHERE tid =', \$tid, 'AND num =', \$num - ); + my $post = { + tid => $tid, + num => $num, + msg => bb_subst_links($data->{msg}), + $data->{tid} ? () : (uid => auth->uid), + auth->permBoardmod && $num != 1 ? (hidden => $data->{hidden}) : (), + auth->permBoardmod && $data->{nolastmod} ? () : (edited => sql 'NOW()') + }; + tuwf->dbExeci('INSERT INTO threads_posts', $post) if !$data->{tid}; + tuwf->dbExeci('UPDATE threads_posts SET', $post, 'WHERE', { tid => $tid, num => $num }) if $data->{tid}; elm_Redirect post_url $tid, $num, $num; }; diff --git a/lib/VNWeb/Discussions/Thread.pm b/lib/VNWeb/Discussions/Thread.pm index 4d4bdeec..1ddaed78 100644 --- a/lib/VNWeb/Discussions/Thread.pm +++ b/lib/VNWeb/Discussions/Thread.pm @@ -185,7 +185,7 @@ json_api qr{/t/pollvote\.json}, $POLL_IN, sub { validate_dbid sql('SELECT id FROM threads_poll_options WHERE tid =', \$data->{tid}, 'AND id IN'), $data->{options}->@*; tuwf->dbExeci('DELETE FROM threads_poll_votes WHERE tid =', \$data->{tid}, 'AND uid =', \auth->uid); - tuwf->dbExeci('INSERT INTO threads_poll_votes (tid, uid, optid) VALUES(', \$data->{tid}, ',', \auth->uid, ',', \$_, ')') for $data->{options}->@*; + tuwf->dbExeci('INSERT INTO threads_poll_votes', { tid => $data->{tid}, uid => auth->uid, optid => $_ }) for $data->{options}->@*; elm_Success }; @@ -198,7 +198,7 @@ json_api qr{/t/reply\.json}, $REPLY_IN, sub { my $num = $t->{count}+1; my $msg = bb_subst_links $data->{msg}; - tuwf->dbExeci('INSERT INTO threads_posts (tid, num, uid, msg) VALUES (', sql_comma(\$t->{id}, \$num, \auth->uid, \$msg), ')'); + tuwf->dbExeci('INSERT INTO threads_posts', { tid => $t->{id}, num => $num, uid => auth->uid, msg => $msg }); tuwf->dbExeci('UPDATE threads SET count =', \$num, 'WHERE id =', \$t->{id}); elm_Redirect post_url $t->{id}, $num, 'last'; }; |