Skip to content

Commit

Permalink
Avoid MySQL-specific SQL to cast vote
Browse files Browse the repository at this point in the history
Until now, saving a user's vote was relying on MySQL's non-standard
"UPSERT" statement (INSERT INTO ... ON DUPLICATE KEY UPDATE ...). This
prevents using the plugin with other RDBMS.

To avoid this, we always delete the old vote, and insert a new record
with the user's new stance.

Fixes #4
  • Loading branch information
dregad committed Sep 8, 2019
1 parent 618fc09 commit 0ba4007
Showing 1 changed file with 10 additions and 13 deletions.
23 changes: 10 additions & 13 deletions pages/submit_support.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,30 +23,27 @@
form_security_validate( 'GaugeSupport_submit_vote' );

$f_vote = gpc_get_string( 'vote', '' );
$t_withdraw_vote = $f_vote == 'withdraw';
$t_cast_vote = $f_vote != 'withdraw';

$f_bug_id = gpc_get_int( 'bugid' );
if( !$t_withdraw_vote ) {
$f_stance = gpc_get_int( 'stance' );
}

$t_user_id = auth_get_current_user_id();
$t_table = plugin_table( 'support_data', 'GaugeSupport' );

if( $t_withdraw_vote ) {
# Delete user's current vote
$t_query = "DELETE FROM $t_table
# Delete user's current vote
$t_query = "DELETE FROM $t_table
WHERE bugid = " . db_param() . " AND userid = " . db_param();
$t_param = array( $f_bug_id, $t_user_id );
db_query( $t_query, $t_param );
} else {
$t_param = array( $f_bug_id, $t_user_id );
db_query( $t_query, $t_param );

if( $t_cast_vote ) {
$f_stance = gpc_get_int( 'stance' );

$t_query = "INSERT INTO {$t_table} (bugid, userid, rating)
VALUES (" . db_param() . "," . db_param() . "," . db_param() . ")
ON DUPLICATE KEY UPDATE rating = " . db_param();
VALUES (" . db_param() . "," . db_param() . "," . db_param() . ")";
$t_param = array(
$f_bug_id,
$t_user_id,
$f_stance,
$f_stance
);
$t_result = db_query( $t_query, $t_param );
Expand Down

0 comments on commit 0ba4007

Please sign in to comment.