From 2bfc538f889394c535a270eccf97703783b1a058 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lu=C3=ADs=20Dalmolin?= Date: Thu, 7 Dec 2023 07:20:57 -0300 Subject: [PATCH] wip --- src/Mixins/RelationshipsExtraMethods.php | 6 ++-- src/PowerJoinClause.php | 24 ++++++++++++++ src/PowerJoins.php | 28 +++++++++++++++- tests/Models/Post.php | 5 +++ tests/Models/PostStat.php | 31 ++++++++++++++++++ tests/MultipleConnectionsTest.php | 32 +++++++++++++++++++ tests/TestCase.php | 17 ++++++++++ tests/database/factories/PostStatFactory.php | 11 +++++++ .../2020_03_16_000000_create_tables.php | 7 ++++ 9 files changed, 157 insertions(+), 4 deletions(-) create mode 100644 tests/Models/PostStat.php create mode 100644 tests/MultipleConnectionsTest.php create mode 100644 tests/database/factories/PostStatFactory.php diff --git a/src/Mixins/RelationshipsExtraMethods.php b/src/Mixins/RelationshipsExtraMethods.php index b838c66..552236c 100644 --- a/src/Mixins/RelationshipsExtraMethods.php +++ b/src/Mixins/RelationshipsExtraMethods.php @@ -2,11 +2,9 @@ namespace Kirschbaum\PowerJoins\Mixins; -use Stringable; use Illuminate\Support\Str; use Kirschbaum\PowerJoins\StaticCache; use Kirschbaum\PowerJoins\PowerJoinClause; -use Kirschbaum\PowerJoins\Tests\Models\Post; use Illuminate\Database\Eloquent\SoftDeletes; use Illuminate\Database\Eloquent\Relations\HasOne; use Illuminate\Database\Eloquent\Relations\HasMany; @@ -293,7 +291,9 @@ protected function performJoinForEloquentPowerJoinsForHasMany() $builder->take(1); } - $builder->{$joinType}($this->query->getModel()->getTable(), function ($join) use ($callback, $joinedTable, $parentTable, $alias, $disableExtraConditions) { + $model = $this->query->getModel(); + + $builder->{$joinType}($model->getTable(), function ($join) use ($callback, $joinedTable, $parentTable, $alias, $disableExtraConditions) { if ($alias) { $join->as($alias); } diff --git a/src/PowerJoinClause.php b/src/PowerJoinClause.php index 365cf66..26cb504 100644 --- a/src/PowerJoinClause.php +++ b/src/PowerJoinClause.php @@ -151,6 +151,30 @@ protected function useAliasInWhereColumnType(array $where): array return $where; } + protected function includeDatabaseName(): self + { + $key = $this->model->getKeyName(); + $table = $this->tableName; + + // if it was already replaced, skip + // TODO + // if (Str::startsWith($where['first'] . '.', $this->alias . '.') || Str::startsWith($where['second'] . '.', $this->alias . '.')) { + // return $where; + // } + + $this->wheres = collect($this->wheres)->filter(function ($where) { + return in_array($where['type'] ?? '', ['Column', 'Basic']); + })->map(function ($where) { + $key = $this->model->getKeyName(); + $table = $this->tableName; + $replaceMethod = sprintf('useAliasInWhere%sType', ucfirst($where['type'])); + + return $this->{$replaceMethod}($where); + })->toArray(); + + return $this; + } + protected function useAliasInWhereBasicType(array $where): array { $table = $this->tableName; diff --git a/src/PowerJoins.php b/src/PowerJoins.php index 09e9b81..2212e03 100644 --- a/src/PowerJoins.php +++ b/src/PowerJoins.php @@ -5,5 +5,31 @@ trait PowerJoins { - // + public function powerJoinsGetConnectionName(): string + { + // TODO: This is a hack to get the connection name for in-memory databases. + if ($this->getConnection()->getConfig()['database'] === ':memory:') { + return $this->getConnection()->getName(); + } + + return $this->getConnection()->getConfig()['database']; + } + + public function getQualifiedTableName(): string + { + return sprintf( + '%s.%s', + $this->powerJoinsGetConnectionName(), + $this->getTable() + ); + } + + public function getFullyQualifiedDeletedAtColumn(): string + { + return sprintf( + '%s.%s', + $this->powerJoinsGetConnectionName(), + $this->getQualifiedDeletedAtColumn() + ); + } } diff --git a/tests/Models/Post.php b/tests/Models/Post.php index fdd31fc..6a2e8a2 100644 --- a/tests/Models/Post.php +++ b/tests/Models/Post.php @@ -83,4 +83,9 @@ public function translations(): HasMany { return $this->hasMany(PostTranslation::class); } + + public function stats(): HasMany + { + return $this->hasMany(PostStat::class); + } } diff --git a/tests/Models/PostStat.php b/tests/Models/PostStat.php new file mode 100644 index 0000000..0f77992 --- /dev/null +++ b/tests/Models/PostStat.php @@ -0,0 +1,31 @@ + 'array', + ]; + + public function post(): BelongsTo + { + return $this->belongsTo(Post::class); + } +} diff --git a/tests/MultipleConnectionsTest.php b/tests/MultipleConnectionsTest.php new file mode 100644 index 0000000..ab573b0 --- /dev/null +++ b/tests/MultipleConnectionsTest.php @@ -0,0 +1,32 @@ +create(); + $postStat = factory(PostStat::class)->create(['post_id' => $post->id]); + + $sql = Post::query() + ->joinRelationship('stats', fn ($join) => $join->includeDatabaseName()) + ->toSql(); + + $posts = Post::query() + ->joinRelationship('stats') + ->get(); + + dd($sql); + } +} diff --git a/tests/TestCase.php b/tests/TestCase.php index f78bf70..461f121 100644 --- a/tests/TestCase.php +++ b/tests/TestCase.php @@ -2,6 +2,7 @@ namespace Kirschbaum\PowerJoins\Tests; +use Illuminate\Support\Facades\DB; use Kirschbaum\PowerJoins\PowerJoinsServiceProvider; class TestCase extends \Orchestra\Testbench\TestCase @@ -12,6 +13,16 @@ public function setUp(): void $this->withFactories(__DIR__.'/database/factories'); $this->loadMigrationsFrom(__DIR__.'/database/migrations'); + + $this->attachDatabase(); + } + + protected function attachDatabase(): void + { + $name = DB::connection('testbench-2')->getName(); + $database = DB::connection('testbench-2')->getDatabaseName(); + + DB::connection('testbench')->statement("ATTACH DATABASE '$database' AS '$name'"); } protected function getPackageProviders($app) @@ -34,5 +45,11 @@ protected function getEnvironmentSetUp($app) 'database' => ':memory:', 'prefix' => '', ]); + + $app['config']->set('database.connections.testbench-2', [ + 'driver' => 'sqlite', + 'database' => ':memory:', + 'prefix' => '', + ]); } } diff --git a/tests/database/factories/PostStatFactory.php b/tests/database/factories/PostStatFactory.php new file mode 100644 index 0000000..2beef86 --- /dev/null +++ b/tests/database/factories/PostStatFactory.php @@ -0,0 +1,11 @@ +define(PostStat::class, function (Faker\Generator $faker) { + return [ + 'post_id' => factory(Post::class), + 'data' => [], + ]; +}); diff --git a/tests/database/migrations/2020_03_16_000000_create_tables.php b/tests/database/migrations/2020_03_16_000000_create_tables.php index 68d5e06..f6b505a 100644 --- a/tests/database/migrations/2020_03_16_000000_create_tables.php +++ b/tests/database/migrations/2020_03_16_000000_create_tables.php @@ -57,6 +57,13 @@ public function up() $table->softDeletes(); }); + Schema::connection('testbench-2')->create('post_stats', function (Blueprint $table) { + $table->increments('id'); + $table->unsignedInteger('post_id'); + $table->json('data'); + $table->timestamps(); + }); + Schema::create('post_groups', function (Blueprint $table) { $table->unsignedInteger('group_id'); $table->unsignedInteger('post_id');