Skip to content

Commit

Permalink
Fix #2921 SQL error on install due to reserved word in MySQL 8 (#3389)
Browse files Browse the repository at this point in the history
* Quote "groups" column name for MySQL

* Quote column names for certain MySQL DB methods

* Use trim() for column names in modified DB methods
  • Loading branch information
dvz authored and euantorano committed Aug 11, 2018
1 parent acde34d commit 7e68ea1
Show file tree
Hide file tree
Showing 8 changed files with 57 additions and 39 deletions.
4 changes: 2 additions & 2 deletions forumdisplay.php
Original file line number Diff line number Diff line change
Expand Up @@ -1366,9 +1366,9 @@
foreach($gids as $gid)
{
$gid = (int)$gid;
$gidswhere .= " OR CONCAT(',',groups,',') LIKE '%,{$gid},%'";
$gidswhere .= " OR CONCAT(',',`groups`,',') LIKE '%,{$gid},%'";
}
$query = $db->simple_select("modtools", 'tid, name', "(CONCAT(',',forums,',') LIKE '%,$fid,%' OR CONCAT(',',forums,',') LIKE '%,-1,%' OR forums='') AND (groups='' OR CONCAT(',',groups,',') LIKE '%,-1,%'{$gidswhere}) AND type = 't'");
$query = $db->simple_select("modtools", 'tid, name', "(CONCAT(',',forums,',') LIKE '%,$fid,%' OR CONCAT(',',forums,',') LIKE '%,-1,%' OR forums='') AND (`groups`='' OR CONCAT(',',`groups`,',') LIKE '%,-1,%'{$gidswhere}) AND type = 't'");
break;
}

Expand Down
17 changes: 13 additions & 4 deletions inc/db_mysql.php
Original file line number Diff line number Diff line change
Expand Up @@ -1353,7 +1353,9 @@ function replace_query($table, $replacements=array(), $default_field="", $insert
*/
function drop_column($table, $column)
{
return $this->write_query("ALTER TABLE {$this->table_prefix}{$table} DROP {$column}");
$column = trim($column, '`');

return $this->write_query("ALTER TABLE {$this->table_prefix}{$table} DROP `{$column}`");
}

/**
Expand All @@ -1366,7 +1368,9 @@ function drop_column($table, $column)
*/
function add_column($table, $column, $definition)
{
return $this->write_query("ALTER TABLE {$this->table_prefix}{$table} ADD {$column} {$definition}");
$column = trim($column, '`');

return $this->write_query("ALTER TABLE {$this->table_prefix}{$table} ADD `{$column}` {$definition}");
}

/**
Expand All @@ -1379,7 +1383,9 @@ function add_column($table, $column, $definition)
*/
function modify_column($table, $column, $new_definition)
{
return $this->write_query("ALTER TABLE {$this->table_prefix}{$table} MODIFY {$column} {$new_definition}");
$column = trim($column, '`');

return $this->write_query("ALTER TABLE {$this->table_prefix}{$table} MODIFY `{$column}` {$new_definition}");
}

/**
Expand All @@ -1393,7 +1399,10 @@ function modify_column($table, $column, $new_definition)
*/
function rename_column($table, $old_column, $new_column, $new_definition)
{
return $this->write_query("ALTER TABLE {$this->table_prefix}{$table} CHANGE {$old_column} {$new_column} {$new_definition}");
$old_column = trim($old_column, '`');
$new_column = trim($new_column, '`');

return $this->write_query("ALTER TABLE {$this->table_prefix}{$table} CHANGE `{$old_column}` `{$new_column}` {$new_definition}");
}

/**
Expand Down
17 changes: 13 additions & 4 deletions inc/db_mysqli.php
Original file line number Diff line number Diff line change
Expand Up @@ -1334,7 +1334,9 @@ function replace_query($table, $replacements=array(), $default_field="", $insert
*/
function drop_column($table, $column)
{
return $this->write_query("ALTER TABLE {$this->table_prefix}{$table} DROP {$column}");
$column = trim($column, '`');

return $this->write_query("ALTER TABLE {$this->table_prefix}{$table} DROP `{$column}`");
}

/**
Expand All @@ -1347,7 +1349,9 @@ function drop_column($table, $column)
*/
function add_column($table, $column, $definition)
{
return $this->write_query("ALTER TABLE {$this->table_prefix}{$table} ADD {$column} {$definition}");
$column = trim($column, '`');

return $this->write_query("ALTER TABLE {$this->table_prefix}{$table} ADD `{$column}` {$definition}");
}

/**
Expand All @@ -1360,7 +1364,9 @@ function add_column($table, $column, $definition)
*/
function modify_column($table, $column, $new_definition)
{
return $this->write_query("ALTER TABLE {$this->table_prefix}{$table} MODIFY {$column} {$new_definition}");
$column = trim($column, '`');

return $this->write_query("ALTER TABLE {$this->table_prefix}{$table} MODIFY `{$column}` {$new_definition}");
}

/**
Expand All @@ -1374,7 +1380,10 @@ function modify_column($table, $column, $new_definition)
*/
function rename_column($table, $old_column, $new_column, $new_definition)
{
return $this->write_query("ALTER TABLE {$this->table_prefix}{$table} CHANGE {$old_column} {$new_column} {$new_definition}");
$old_column = trim($old_column, '`');
$new_column = trim($new_column, '`');

return $this->write_query("ALTER TABLE {$this->table_prefix}{$table} CHANGE `{$old_column}` `{$new_column}` {$new_definition}");
}

/**
Expand Down
42 changes: 21 additions & 21 deletions install/resources/mysql_db_inserts.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,27 +8,27 @@
*
*/

$inserts[] = "INSERT INTO mybb_attachtypes (atid, name, mimetype, extension, maxsize, icon, enabled, groups, forums, avatarfile) VALUES (1, 'ZIP File', 'application/zip', 'zip', 1024, 'images/attachtypes/zip.png', 1, '-1', '-1', 0);";
$inserts[] = "INSERT INTO mybb_attachtypes (atid, name, mimetype, extension, maxsize, icon, enabled, groups, forums, avatarfile) VALUES (2, 'JPG Image', 'image/jpeg', 'jpg', 500, 'images/attachtypes/image.png', 1, '-1', '-1', 1);";
$inserts[] = "INSERT INTO mybb_attachtypes (atid, name, mimetype, extension, maxsize, icon, enabled, groups, forums, avatarfile) VALUES (3, 'Text Document', 'text/plain', 'txt', 200, 'images/attachtypes/txt.png', 1, '-1', '-1', 0);";
$inserts[] = "INSERT INTO mybb_attachtypes (atid, name, mimetype, extension, maxsize, icon, enabled, groups, forums, avatarfile) VALUES (4, 'GIF Image', 'image/gif', 'gif', 500, 'images/attachtypes/image.png', 1, '-1', '-1', 1);";
$inserts[] = "INSERT INTO mybb_attachtypes (atid, name, mimetype, extension, maxsize, icon, enabled, groups, forums, avatarfile) VALUES (5, 'Adobe Photoshop File', 'application/x-photoshop', 'psd', 1024, 'images/attachtypes/psd.png', 1, '-1', '-1', 0);";
$inserts[] = "INSERT INTO mybb_attachtypes (atid, name, mimetype, extension, maxsize, icon, enabled, groups, forums, avatarfile) VALUES (6, 'PHP File', 'application/x-httpd-php', 'php', 500, 'images/attachtypes/php.png', 1, '-1', '-1', 0);";
$inserts[] = "INSERT INTO mybb_attachtypes (atid, name, mimetype, extension, maxsize, icon, enabled, groups, forums, avatarfile) VALUES (7, 'PNG Image', 'image/png', 'png', 500, 'images/attachtypes/image.png', 1, '-1', '-1', 1);";
$inserts[] = "INSERT INTO mybb_attachtypes (atid, name, mimetype, extension, maxsize, icon, enabled, groups, forums, avatarfile) VALUES (8, 'Microsoft Word Document', 'application/msword', 'doc', 1024, 'images/attachtypes/doc.png', 1, '-1', '-1', 0);";
$inserts[] = "INSERT INTO mybb_attachtypes (atid, name, mimetype, extension, maxsize, icon, enabled, groups, forums, avatarfile) VALUES (9, 'HTM File', 'text/html', 'htm', 100, 'images/attachtypes/html.png', 1, '-1', '-1', 0);";
$inserts[] = "INSERT INTO mybb_attachtypes (atid, name, mimetype, extension, maxsize, icon, enabled, groups, forums, avatarfile) VALUES (10, 'HTML File', 'text/html', 'html', 100, 'images/attachtypes/html.png', 1, '-1', '-1', 0);";
$inserts[] = "INSERT INTO mybb_attachtypes (atid, name, mimetype, extension, maxsize, icon, enabled, groups, forums, avatarfile) VALUES (11, 'JPEG Image', 'image/jpeg', 'jpeg', 500, 'images/attachtypes/image.png', 1, '-1', '-1', 1);";
$inserts[] = "INSERT INTO mybb_attachtypes (atid, name, mimetype, extension, maxsize, icon, enabled, groups, forums, avatarfile) VALUES (12, 'GZIP Compressed File', 'application/x-gzip', 'gz', 1024, 'images/attachtypes/tar.png', 1, '-1', '-1', 0);";
$inserts[] = "INSERT INTO mybb_attachtypes (atid, name, mimetype, extension, maxsize, icon, enabled, groups, forums, avatarfile) VALUES (13, 'TAR Compressed File', 'application/x-tar', 'tar', 1024, 'images/attachtypes/tar.png', 1, '-1', '-1', 0);";
$inserts[] = "INSERT INTO mybb_attachtypes (atid, name, mimetype, extension, maxsize, icon, enabled, groups, forums, avatarfile) VALUES (14, 'CSS Stylesheet', 'text/css', 'css', 100, 'images/attachtypes/css.png', 1, '-1', '-1', 0);";
$inserts[] = "INSERT INTO mybb_attachtypes (atid, name, mimetype, extension, maxsize, icon, enabled, groups, forums, avatarfile) VALUES (15, 'Adobe Acrobat PDF', 'application/pdf', 'pdf', 2048, 'images/attachtypes/pdf.png', 1, '-1', '-1', 0);";
$inserts[] = "INSERT INTO mybb_attachtypes (atid, name, mimetype, extension, maxsize, icon, enabled, groups, forums, avatarfile) VALUES (16, 'Bitmap Image', 'image/bmp', 'bmp', 500, 'images/attachtypes/image.png', 1, '-1', '-1', 0);";
$inserts[] = "INSERT INTO mybb_attachtypes (atid, name, mimetype, extension, maxsize, icon, enabled, groups, forums, avatarfile) VALUES (17, 'Microsoft Word 2007 Document', 'application/vnd.openxmlformats-officedocument.wordprocessingml.document', 'docx', 1024, 'images/attachtypes/doc.png', 1, '-1', '-1', 0);";
$inserts[] = "INSERT INTO mybb_attachtypes (atid, name, mimetype, extension, maxsize, icon, enabled, groups, forums, avatarfile) VALUES (18, 'Microsoft Excel Document', 'application/vnd.ms-excel', 'xls', 1024, 'images/attachtypes/xls.png', 1, '-1', '-1', 0);";
$inserts[] = "INSERT INTO mybb_attachtypes (atid, name, mimetype, extension, maxsize, icon, enabled, groups, forums, avatarfile) VALUES (19, 'Microsoft Excel 2007 Document', 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet', 'xlsx', 1024, 'images/attachtypes/xls.png', 1, '-1', '-1', 0);";
$inserts[] = "INSERT INTO mybb_attachtypes (atid, name, mimetype, extension, maxsize, icon, enabled, groups, forums, avatarfile) VALUES (20, 'Microsoft PowerPoint Document', 'application/vnd.ms-powerpoint', 'ppt', 1024, 'images/attachtypes/ppt.png', 1, '-1', '-1', 0);";
$inserts[] = "INSERT INTO mybb_attachtypes (atid, name, mimetype, extension, maxsize, icon, enabled, groups, forums, avatarfile) VALUES (21, 'Microsoft PowerPoint 2007 Document', 'application/vnd.openxmlformats-officedocument.presentationml.presentation', 'pptx', 1024, 'images/attachtypes/ppt.png', 1, '-1', '-1', 0);";
$inserts[] = "INSERT INTO mybb_attachtypes (atid, name, mimetype, extension, maxsize, icon, enabled, `groups`,forums, avatarfile) VALUES (1, 'ZIP File', 'application/zip', 'zip', 1024, 'images/attachtypes/zip.png', 1, '-1', '-1', 0);";
$inserts[] = "INSERT INTO mybb_attachtypes (atid, name, mimetype, extension, maxsize, icon, enabled, `groups`,forums, avatarfile) VALUES (2, 'JPG Image', 'image/jpeg', 'jpg', 500, 'images/attachtypes/image.png', 1, '-1', '-1', 1);";
$inserts[] = "INSERT INTO mybb_attachtypes (atid, name, mimetype, extension, maxsize, icon, enabled, `groups`,forums, avatarfile) VALUES (3, 'Text Document', 'text/plain', 'txt', 200, 'images/attachtypes/txt.png', 1, '-1', '-1', 0);";
$inserts[] = "INSERT INTO mybb_attachtypes (atid, name, mimetype, extension, maxsize, icon, enabled, `groups`,forums, avatarfile) VALUES (4, 'GIF Image', 'image/gif', 'gif', 500, 'images/attachtypes/image.png', 1, '-1', '-1', 1);";
$inserts[] = "INSERT INTO mybb_attachtypes (atid, name, mimetype, extension, maxsize, icon, enabled, `groups`,forums, avatarfile) VALUES (5, 'Adobe Photoshop File', 'application/x-photoshop', 'psd', 1024, 'images/attachtypes/psd.png', 1, '-1', '-1', 0);";
$inserts[] = "INSERT INTO mybb_attachtypes (atid, name, mimetype, extension, maxsize, icon, enabled, `groups`,forums, avatarfile) VALUES (6, 'PHP File', 'application/x-httpd-php', 'php', 500, 'images/attachtypes/php.png', 1, '-1', '-1', 0);";
$inserts[] = "INSERT INTO mybb_attachtypes (atid, name, mimetype, extension, maxsize, icon, enabled, `groups`,forums, avatarfile) VALUES (7, 'PNG Image', 'image/png', 'png', 500, 'images/attachtypes/image.png', 1, '-1', '-1', 1);";
$inserts[] = "INSERT INTO mybb_attachtypes (atid, name, mimetype, extension, maxsize, icon, enabled, `groups`,forums, avatarfile) VALUES (8, 'Microsoft Word Document', 'application/msword', 'doc', 1024, 'images/attachtypes/doc.png', 1, '-1', '-1', 0);";
$inserts[] = "INSERT INTO mybb_attachtypes (atid, name, mimetype, extension, maxsize, icon, enabled, `groups`,forums, avatarfile) VALUES (9, 'HTM File', 'text/html', 'htm', 100, 'images/attachtypes/html.png', 1, '-1', '-1', 0);";
$inserts[] = "INSERT INTO mybb_attachtypes (atid, name, mimetype, extension, maxsize, icon, enabled, `groups`,forums, avatarfile) VALUES (10, 'HTML File', 'text/html', 'html', 100, 'images/attachtypes/html.png', 1, '-1', '-1', 0);";
$inserts[] = "INSERT INTO mybb_attachtypes (atid, name, mimetype, extension, maxsize, icon, enabled, `groups`,forums, avatarfile) VALUES (11, 'JPEG Image', 'image/jpeg', 'jpeg', 500, 'images/attachtypes/image.png', 1, '-1', '-1', 1);";
$inserts[] = "INSERT INTO mybb_attachtypes (atid, name, mimetype, extension, maxsize, icon, enabled, `groups`,forums, avatarfile) VALUES (12, 'GZIP Compressed File', 'application/x-gzip', 'gz', 1024, 'images/attachtypes/tar.png', 1, '-1', '-1', 0);";
$inserts[] = "INSERT INTO mybb_attachtypes (atid, name, mimetype, extension, maxsize, icon, enabled, `groups`,forums, avatarfile) VALUES (13, 'TAR Compressed File', 'application/x-tar', 'tar', 1024, 'images/attachtypes/tar.png', 1, '-1', '-1', 0);";
$inserts[] = "INSERT INTO mybb_attachtypes (atid, name, mimetype, extension, maxsize, icon, enabled, `groups`,forums, avatarfile) VALUES (14, 'CSS Stylesheet', 'text/css', 'css', 100, 'images/attachtypes/css.png', 1, '-1', '-1', 0);";
$inserts[] = "INSERT INTO mybb_attachtypes (atid, name, mimetype, extension, maxsize, icon, enabled, `groups`,forums, avatarfile) VALUES (15, 'Adobe Acrobat PDF', 'application/pdf', 'pdf', 2048, 'images/attachtypes/pdf.png', 1, '-1', '-1', 0);";
$inserts[] = "INSERT INTO mybb_attachtypes (atid, name, mimetype, extension, maxsize, icon, enabled, `groups`,forums, avatarfile) VALUES (16, 'Bitmap Image', 'image/bmp', 'bmp', 500, 'images/attachtypes/image.png', 1, '-1', '-1', 0);";
$inserts[] = "INSERT INTO mybb_attachtypes (atid, name, mimetype, extension, maxsize, icon, enabled, `groups`,forums, avatarfile) VALUES (17, 'Microsoft Word 2007 Document', 'application/vnd.openxmlformats-officedocument.wordprocessingml.document', 'docx', 1024, 'images/attachtypes/doc.png', 1, '-1', '-1', 0);";
$inserts[] = "INSERT INTO mybb_attachtypes (atid, name, mimetype, extension, maxsize, icon, enabled, `groups`,forums, avatarfile) VALUES (18, 'Microsoft Excel Document', 'application/vnd.ms-excel', 'xls', 1024, 'images/attachtypes/xls.png', 1, '-1', '-1', 0);";
$inserts[] = "INSERT INTO mybb_attachtypes (atid, name, mimetype, extension, maxsize, icon, enabled, `groups`,forums, avatarfile) VALUES (19, 'Microsoft Excel 2007 Document', 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet', 'xlsx', 1024, 'images/attachtypes/xls.png', 1, '-1', '-1', 0);";
$inserts[] = "INSERT INTO mybb_attachtypes (atid, name, mimetype, extension, maxsize, icon, enabled, `groups`,forums, avatarfile) VALUES (20, 'Microsoft PowerPoint Document', 'application/vnd.ms-powerpoint', 'ppt', 1024, 'images/attachtypes/ppt.png', 1, '-1', '-1', 0);";
$inserts[] = "INSERT INTO mybb_attachtypes (atid, name, mimetype, extension, maxsize, icon, enabled, `groups`,forums, avatarfile) VALUES (21, 'Microsoft PowerPoint 2007 Document', 'application/vnd.openxmlformats-officedocument.presentationml.presentation', 'pptx', 1024, 'images/attachtypes/ppt.png', 1, '-1', '-1', 0);";

$inserts[] = "INSERT INTO mybb_calendars (name,disporder,startofweek,showbirthdays,eventlimit,moderation,allowhtml,allowmycode,allowimgcode,allowvideocode,allowsmilies) VALUES ('Default Calendar',1,0,1,4,0,0,1,1,1,1);";

Expand Down
6 changes: 3 additions & 3 deletions install/resources/mysql_db_tables.php
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@
maxsize int(15) unsigned NOT NULL default '0',
icon varchar(100) NOT NULL default '',
enabled tinyint(1) NOT NULL default '1',
groups TEXT NOT NULL,
`groups` TEXT NOT NULL,
forums TEXT NOT NULL,
avatarfile tinyint(1) NOT NULL default '0',
PRIMARY KEY (atid)
Expand Down Expand Up @@ -482,7 +482,7 @@
name varchar(200) NOT NULL,
description text NOT NULL,
forums text NOT NULL,
groups text NOT NULL,
`groups` text NOT NULL,
type char(1) NOT NULL default '',
postoptions text NOT NULL,
threadoptions text NOT NULL,
Expand Down Expand Up @@ -874,7 +874,7 @@
prefix varchar(120) NOT NULL default '',
displaystyle varchar(200) NOT NULL default '',
forums text NOT NULL,
groups text NOT NULL,
`groups` text NOT NULL,
PRIMARY KEY (pid)
) ENGINE=MyISAM;";

Expand Down
2 changes: 1 addition & 1 deletion install/resources/upgrade17.php
Original file line number Diff line number Diff line change
Expand Up @@ -368,7 +368,7 @@ function upgrade17_dbchanges4()
prefix varchar(120) NOT NULL default '',
displaystyle varchar(200) NOT NULL default '',
forums text NOT NULL,
groups text NOT NULL,
`groups` text NOT NULL,
PRIMARY KEY(pid)
) ENGINE=MyISAM;");
$db->write_query("CREATE TABLE ".TABLE_PREFIX."delayedmoderation (
Expand Down
4 changes: 2 additions & 2 deletions moderation.php
Original file line number Diff line number Diff line change
Expand Up @@ -249,10 +249,10 @@
{
case "pgsql":
case "sqlite":
$query = $db->simple_select("modtools", 'tid, name, groups', "(','||forums||',' LIKE '%,$fid,%' OR ','||forums||',' LIKE '%,-1,%' OR forums='') AND type = 't'");
$query = $db->simple_select("modtools", 'tid, name, `groups`', "(','||forums||',' LIKE '%,$fid,%' OR ','||forums||',' LIKE '%,-1,%' OR forums='') AND type = 't'");
break;
default:
$query = $db->simple_select("modtools", 'tid, name, groups', "(CONCAT(',',forums,',') LIKE '%,$fid,%' OR CONCAT(',',forums,',') LIKE '%,-1,%' OR forums='') AND type = 't'");
$query = $db->simple_select("modtools", 'tid, name, `groups`', "(CONCAT(',',forums,',') LIKE '%,$fid,%' OR CONCAT(',',forums,',') LIKE '%,-1,%' OR forums='') AND type = 't'");
}
while($tool = $db->fetch_array($query))
{
Expand Down
4 changes: 2 additions & 2 deletions showthread.php
Original file line number Diff line number Diff line change
Expand Up @@ -1324,9 +1324,9 @@
foreach($gids as $gid)
{
$gid = (int)$gid;
$gidswhere .= " OR CONCAT(',',groups,',') LIKE '%,{$gid},%'";
$gidswhere .= " OR CONCAT(',',`groups`,',') LIKE '%,{$gid},%'";
}
$query = $db->simple_select("modtools", 'tid, name, type', "(CONCAT(',',forums,',') LIKE '%,$fid,%' OR CONCAT(',',forums,',') LIKE '%,-1,%' OR forums='') AND (groups='' OR CONCAT(',',groups,',') LIKE '%,-1,%'{$gidswhere})");
$query = $db->simple_select("modtools", 'tid, name, type', "(CONCAT(',',forums,',') LIKE '%,$fid,%' OR CONCAT(',',forums,',') LIKE '%,-1,%' OR forums='') AND (`groups`='' OR CONCAT(',',`groups`,',') LIKE '%,-1,%'{$gidswhere})");
break;
}

Expand Down

2 comments on commit 7e68ea1

@fluxtion
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Well done on getting this fixed correctly in the end. How did you find them all? My best efforts using regex searches etc. always came up slightly short.

@dvz
Copy link
Contributor Author

@dvz dvz commented on 7e68ea1 Aug 12, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[^\[$_'\-">a-z](?<![a-z};] )groups, then [^\[$_'\-">a-z`](?<![a-z};] )('|"|)groups\1 to review function arguments

Please sign in to comment.