From 05c8e1276141029b842a139e8ba2a9a566fa9620 Mon Sep 17 00:00:00 2001 From: Toby Zerner Date: Sat, 21 Jul 2018 22:02:54 +0930 Subject: [PATCH] Wrap column names; use whereColumn where possible --- ..._change_access_tokens_add_foreign_keys.php | 2 +- ...00_change_discussions_add_foreign_keys.php | 2 +- ...hange_discussion_user_add_foreign_keys.php | 4 ++-- ...0_change_email_tokens_add_foreign_keys.php | 2 +- ...ange_group_permission_add_foreign_keys.php | 2 +- ...700_change_group_user_add_foreign_keys.php | 4 ++-- ..._change_notifications_add_foreign_keys.php | 4 ++-- ...hange_password_tokens_add_foreign_keys.php | 2 +- ...8_135100_change_posts_add_foreign_keys.php | 2 +- .../src/Discussion/DiscussionRepository.php | 2 +- .../Search/Gambit/FulltextGambit.php | 19 +++++++++++++------ framework/core/src/Post/Post.php | 6 +----- framework/core/src/Post/PostPolicy.php | 2 +- 13 files changed, 28 insertions(+), 25 deletions(-) diff --git a/framework/core/migrations/2018_01_11_094000_change_access_tokens_add_foreign_keys.php b/framework/core/migrations/2018_01_11_094000_change_access_tokens_add_foreign_keys.php index b6125506d..3103a963e 100644 --- a/framework/core/migrations/2018_01_11_094000_change_access_tokens_add_foreign_keys.php +++ b/framework/core/migrations/2018_01_11_094000_change_access_tokens_add_foreign_keys.php @@ -19,7 +19,7 @@ return [ $schema->getConnection() ->table('access_tokens') ->whereNotExists(function ($query) { - $query->selectRaw(1)->from('users')->whereRaw('id = user_id'); + $query->selectRaw(1)->from('users')->whereColumn('id', 'user_id'); }) ->delete(); diff --git a/framework/core/migrations/2018_01_11_155300_change_discussions_add_foreign_keys.php b/framework/core/migrations/2018_01_11_155300_change_discussions_add_foreign_keys.php index 3e33b765c..23d6b338c 100644 --- a/framework/core/migrations/2018_01_11_155300_change_discussions_add_foreign_keys.php +++ b/framework/core/migrations/2018_01_11_155300_change_discussions_add_foreign_keys.php @@ -21,7 +21,7 @@ return [ $selectId = function ($table, $column) use ($connection) { return new Expression( - '('.$connection->table($table)->whereRaw("id = $column")->select('id')->toSql().')' + '('.$connection->table($table)->whereColumn('id', $column)->select('id')->toSql().')' ); }; diff --git a/framework/core/migrations/2018_01_15_071900_change_discussion_user_add_foreign_keys.php b/framework/core/migrations/2018_01_15_071900_change_discussion_user_add_foreign_keys.php index 4a9d69a07..c3bf1d9fd 100644 --- a/framework/core/migrations/2018_01_15_071900_change_discussion_user_add_foreign_keys.php +++ b/framework/core/migrations/2018_01_15_071900_change_discussion_user_add_foreign_keys.php @@ -19,10 +19,10 @@ return [ $connection = $schema->getConnection(); $connection->table('discussion_user') ->whereNotExists(function ($query) { - $query->selectRaw(1)->from('users')->whereRaw('id = user_id'); + $query->selectRaw(1)->from('users')->whereColumn('id', 'user_id'); }) ->orWhereNotExists(function ($query) { - $query->selectRaw(1)->from('discussions')->whereRaw('id = discussion_id'); + $query->selectRaw(1)->from('discussions')->whereColumn('id', 'discussion_id'); }) ->delete(); diff --git a/framework/core/migrations/2018_01_15_072700_change_email_tokens_add_foreign_keys.php b/framework/core/migrations/2018_01_15_072700_change_email_tokens_add_foreign_keys.php index c840045e6..243b706f3 100644 --- a/framework/core/migrations/2018_01_15_072700_change_email_tokens_add_foreign_keys.php +++ b/framework/core/migrations/2018_01_15_072700_change_email_tokens_add_foreign_keys.php @@ -19,7 +19,7 @@ return [ $schema->getConnection() ->table('email_tokens') ->whereNotExists(function ($query) { - $query->selectRaw(1)->from('users')->whereRaw('id = user_id'); + $query->selectRaw(1)->from('users')->whereColumn('id', 'user_id'); }) ->delete(); diff --git a/framework/core/migrations/2018_01_18_130500_change_group_permission_add_foreign_keys.php b/framework/core/migrations/2018_01_18_130500_change_group_permission_add_foreign_keys.php index f783547a6..267a09dc0 100644 --- a/framework/core/migrations/2018_01_18_130500_change_group_permission_add_foreign_keys.php +++ b/framework/core/migrations/2018_01_18_130500_change_group_permission_add_foreign_keys.php @@ -19,7 +19,7 @@ return [ $schema->getConnection() ->table('group_permission') ->whereNotExists(function ($query) { - $query->selectRaw(1)->from('groups')->whereRaw('id = group_id'); + $query->selectRaw(1)->from('groups')->whereColumn('id', 'group_id'); }) ->delete(); diff --git a/framework/core/migrations/2018_01_18_130700_change_group_user_add_foreign_keys.php b/framework/core/migrations/2018_01_18_130700_change_group_user_add_foreign_keys.php index 028b35e94..79a5ef2a5 100644 --- a/framework/core/migrations/2018_01_18_130700_change_group_user_add_foreign_keys.php +++ b/framework/core/migrations/2018_01_18_130700_change_group_user_add_foreign_keys.php @@ -19,10 +19,10 @@ return [ $schema->getConnection() ->table('group_user') ->whereNotExists(function ($query) { - $query->selectRaw(1)->from('users')->whereRaw('id = user_id'); + $query->selectRaw(1)->from('users')->whereColumn('id', 'user_id'); }) ->orWhereNotExists(function ($query) { - $query->selectRaw(1)->from('groups')->whereRaw('id = group_id'); + $query->selectRaw(1)->from('groups')->whereColumn('id', 'group_id'); }) ->delete(); diff --git a/framework/core/migrations/2018_01_18_133100_change_notifications_add_foreign_keys.php b/framework/core/migrations/2018_01_18_133100_change_notifications_add_foreign_keys.php index 442ae1dbd..4845222cb 100644 --- a/framework/core/migrations/2018_01_18_133100_change_notifications_add_foreign_keys.php +++ b/framework/core/migrations/2018_01_18_133100_change_notifications_add_foreign_keys.php @@ -19,14 +19,14 @@ return [ $schema->getConnection() ->table('notifications') ->whereNotExists(function ($query) { - $query->selectRaw(1)->from('users')->whereRaw('id = user_id'); + $query->selectRaw(1)->from('users')->whereColumn('id', 'user_id'); }) ->delete(); $schema->getConnection() ->table('notifications') ->whereNotExists(function ($query) { - $query->selectRaw(1)->from('users')->whereRaw('id = from_user_id'); + $query->selectRaw(1)->from('users')->whereColumn('id', 'from_user_id'); }) ->update(['from_user_id' => null]); diff --git a/framework/core/migrations/2018_01_18_134500_change_password_tokens_add_foreign_keys.php b/framework/core/migrations/2018_01_18_134500_change_password_tokens_add_foreign_keys.php index 7e3c3c6d4..cb7303e71 100644 --- a/framework/core/migrations/2018_01_18_134500_change_password_tokens_add_foreign_keys.php +++ b/framework/core/migrations/2018_01_18_134500_change_password_tokens_add_foreign_keys.php @@ -19,7 +19,7 @@ return [ $connection = $schema->getConnection(); $connection->table('password_tokens') ->whereNotExists(function ($query) { - $query->selectRaw(1)->from('users')->whereRaw('id = user_id'); + $query->selectRaw(1)->from('users')->whereColumn('id', 'user_id'); }) ->delete(); diff --git a/framework/core/migrations/2018_01_18_135100_change_posts_add_foreign_keys.php b/framework/core/migrations/2018_01_18_135100_change_posts_add_foreign_keys.php index 2a0623052..3ac447273 100644 --- a/framework/core/migrations/2018_01_18_135100_change_posts_add_foreign_keys.php +++ b/framework/core/migrations/2018_01_18_135100_change_posts_add_foreign_keys.php @@ -21,7 +21,7 @@ return [ $selectId = function ($table, $column) use ($connection) { return new Expression( - '('.$connection->table($table)->whereRaw("id = $column")->select('id')->toSql().')' + '('.$connection->table($table)->whereColumn('id', $column)->select('id')->toSql().')' ); }; diff --git a/framework/core/src/Discussion/DiscussionRepository.php b/framework/core/src/Discussion/DiscussionRepository.php index de0b7ee36..588132d7b 100644 --- a/framework/core/src/Discussion/DiscussionRepository.php +++ b/framework/core/src/Discussion/DiscussionRepository.php @@ -51,7 +51,7 @@ class DiscussionRepository { return Discussion::leftJoin('discussions_users', 'discussions_users.discussion_id', '=', 'discussions.id') ->where('user_id', $user->id) - ->whereRaw('last_read_post_number >= last_post_number') + ->whereColumn('last_read_post_number', '>=', 'last_post_number') ->pluck('id') ->all(); } diff --git a/framework/core/src/Discussion/Search/Gambit/FulltextGambit.php b/framework/core/src/Discussion/Search/Gambit/FulltextGambit.php index 81f5e5976..d925e0ec8 100644 --- a/framework/core/src/Discussion/Search/Gambit/FulltextGambit.php +++ b/framework/core/src/Discussion/Search/Gambit/FulltextGambit.php @@ -33,22 +33,29 @@ class FulltextGambit implements GambitInterface // See https://bugs.mysql.com/bug.php?id=74042 $bit = str_replace('@', '*', $bit); - $search->getQuery() - ->selectRaw('SUBSTRING_INDEX(GROUP_CONCAT(posts.id ORDER BY MATCH(posts.content) AGAINST (?) DESC), \',\', 1) as most_relevant_post_id', [$bit]) + $query = $search->getQuery(); + $grammar = $query->getGrammar(); + + $query + ->selectRaw('SUBSTRING_INDEX(GROUP_CONCAT('.$grammar->wrap('posts.id').' ORDER BY MATCH('.$grammar->wrap('posts.content').') AGAINST (?) DESC), \',\', 1) as most_relevant_post_id', [$bit]) ->leftJoin('posts', 'posts.discussion_id', '=', 'discussions.id') ->where('posts.type', 'comment') ->where(function ($query) use ($search) { event(new ScopeModelVisibility(Post::query()->setQuery($query), $search->getActor(), 'view')); }) ->where(function ($query) use ($bit) { - $query->whereRaw('MATCH(discussions.title) AGAINST (? IN BOOLEAN MODE)', [$bit]) - ->orWhereRaw('MATCH(posts.content) AGAINST (? IN BOOLEAN MODE)', [$bit]); + $grammar = $query->getGrammar(); + + $query->whereRaw('MATCH('.$grammar->wrap('discussions.title').') AGAINST (? IN BOOLEAN MODE)', [$bit]) + ->orWhereRaw('MATCH('.$grammar->wrap('posts.content').') AGAINST (? IN BOOLEAN MODE)', [$bit]); }) ->groupBy('posts.discussion_id'); $search->setDefaultSort(function ($query) use ($bit) { - $query->orderByRaw('MATCH(discussions.title) AGAINST (?) desc', [$bit]); - $query->orderByRaw('MATCH(posts.content) AGAINST (?) desc', [$bit]); + $grammar = $query->getGrammar(); + + $query->orderByRaw('MATCH('.$grammar->wrap('discussions.title').') AGAINST (?) desc', [$bit]); + $query->orderByRaw('MATCH('.$grammar->wrap('posts.content').') AGAINST (?) desc', [$bit]); }); } } diff --git a/framework/core/src/Post/Post.php b/framework/core/src/Post/Post.php index 137e9657d..899949a6d 100644 --- a/framework/core/src/Post/Post.php +++ b/framework/core/src/Post/Post.php @@ -121,13 +121,9 @@ class Post extends AbstractModel // Make sure the post's discussion is visible as well $query->whereExists(function ($query) use ($actor) { - $grammar = $query->getGrammar(); - $column1 = $grammar->wrap('discussions.id'); - $column2 = $grammar->wrap('posts.discussion_id'); - $query->selectRaw('1') ->from('discussions') - ->whereRaw("$column1 = $column2"); + ->whereColumn('discussions.id', 'posts.discussion_id'); static::$dispatcher->dispatch( new ScopeModelVisibility(Discussion::query()->setQuery($query), $actor, 'view') diff --git a/framework/core/src/Post/PostPolicy.php b/framework/core/src/Post/PostPolicy.php index 34df19747..0ed52e9d1 100644 --- a/framework/core/src/Post/PostPolicy.php +++ b/framework/core/src/Post/PostPolicy.php @@ -86,7 +86,7 @@ class PostPolicy extends AbstractPolicy ->orWhereExists(function ($query) use ($actor) { $query->selectRaw('1') ->from('discussions') - ->whereRaw('discussions.id = posts.discussion_id') + ->whereColumn('discussions.id', 'posts.discussion_id') ->where(function ($query) use ($actor) { $this->events->dispatch( new ScopeModelVisibility(Discussion::query()->setQuery($query), $actor, 'hidePosts')