Aperture-Development / MSync-2

Version 2 of MSync - Fully working and extra steak
https://www.Aperture-Development.de
GNU General Public License v3.0
12 stars 1 forks source link

SQL Syntax incompatible with MySQL #75

Closed ApertureDevelopment closed 2 years ago

ApertureDevelopment commented 2 years ago

A bug was just discovered within the nested SQL statements. It appears that the current nested selects for the insert queries are all not working with a regular mysql database.

This syntax:

INSERT INTO `tbl_mrsync` (user_id, rank, server_group)
VALUES (
    (SELECT p_user_id FROM tbl_users WHERE steamid=? AND steamid64=?), 
?, 
    (SELECT p_group_id FROM tbl_server_grp WHERE group_name=?)
)
ON DUPLICATE KEY UPDATE rank=VALUES(rank);

is only MariaDB specific and does not work with MySQL 8. The proper syntax to use for the example given above would be

INSERT INTO `tbl_mrsync` (user_id, rank, server_group) 
SELECT tbl_users.p_user_id, ?, tbl_server_grp.p_group_id
FROM tbl_users, tbl_server_grp
WHERE
  tbl_users.steamid=? AND tbl_users.steamid64=?
AND
  tbl_server_grp.group_name=?
ON DUPLICATE KEY UPDATE rank=VALUES(rank);
ApertureDevelopment commented 2 years ago

https://github.com/Aperture-Development/MSync-2/blob/master/lua/msync/server/modules/sv_mrsync.lua#L55

INSERT INTO `tbl_mrsync` (user_id, rank, server_group) 
SELECT tbl_users.p_user_id, ?, tbl_server_grp.p_group_id
FROM tbl_users, tbl_server_grp
WHERE
  tbl_users.steamid=? AND tbl_users.steamid64=?
AND
  tbl_server_grp.group_name=?
ON DUPLICATE KEY UPDATE rank=VALUES(rank);
ApertureDevelopment commented 2 years ago

https://github.com/Aperture-Development/MSync-2/blob/master/lua/msync/server/modules/sv_mbsync.lua#L123 and https://github.com/Aperture-Development/MSync-2/blob/master/lua/msync/server/modules/sv_mbsync.lua#L203 ( compare UserTbl id in an OR notation instead ) and https://github.com/Aperture-Development/MSync-2/blob/master/lua/msync/server/modules/sv_mbsync.lua#L686 ( no check for steamid64 )

INSERT INTO `tbl_mbsync` (user_id, admin_id, reason, date_unix, length_unix, server_group)
SELECT UserTbl.p_user_id, AdminTbl.p_user_id, ?, ?, ?, tbl_server_grp.p_group_id
FROM tbl_users AS UserTbl, tbl_users AS AdminTbl
WHERE
    UserTbl.steamid=? AND
    UserTbl.steamid64=?
AND
    AdminTbl.steamid=? AND
    AdminTbl.steamid64=?
AND
    tbl_server_grp.group_name=?;
ApertureDevelopment commented 2 years ago

https://github.com/Aperture-Development/MSync-2/blob/master/lua/msync/server/sv_mysql.lua#L208

INSERT INTO `tbl_msync_servers` (server_name, ip, port, server_group)
SELECT ?, ?, ?, tbl_server_grp.p_group_id
FROM tbl_server_grp
WHERE
    group_name=?
ON DUPLICATE KEY UPDATE server_name=VALUES(server_name), server_group=VALUES(server_group);