Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
luisdalmolin committed Dec 7, 2023
1 parent e55fa70 commit 2bfc538
Show file tree
Hide file tree
Showing 9 changed files with 157 additions and 4 deletions.
6 changes: 3 additions & 3 deletions src/Mixins/RelationshipsExtraMethods.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
}
Expand Down
24 changes: 24 additions & 0 deletions src/PowerJoinClause.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
28 changes: 27 additions & 1 deletion src/PowerJoins.php
Original file line number Diff line number Diff line change
Expand Up @@ -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()
);
}
}
5 changes: 5 additions & 0 deletions tests/Models/Post.php
Original file line number Diff line number Diff line change
Expand Up @@ -83,4 +83,9 @@ public function translations(): HasMany
{
return $this->hasMany(PostTranslation::class);
}

public function stats(): HasMany
{
return $this->hasMany(PostStat::class);
}
}
31 changes: 31 additions & 0 deletions tests/Models/PostStat.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php

namespace Kirschbaum\PowerJoins\Tests\Models;

use Kirschbaum\PowerJoins\PowerJoins;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Relations\HasOne;
use Illuminate\Database\Eloquent\Relations\HasMany;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\Relations\MorphMany;
use Illuminate\Database\Eloquent\Relations\MorphToMany;
use Illuminate\Database\Eloquent\SoftDeletes;

class PostStat extends Model
{
use PowerJoins;

protected $connection = 'testbench-2';

protected $table = 'post_stats';

protected $casts = [
'data' => 'array',
];

public function post(): BelongsTo
{
return $this->belongsTo(Post::class);
}
}
32 changes: 32 additions & 0 deletions tests/MultipleConnectionsTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php

namespace Kirschbaum\PowerJoins\Tests;

use Kirschbaum\PowerJoins\PowerJoins;
use Kirschbaum\PowerJoins\Tests\Models\Post;
use Kirschbaum\PowerJoins\Tests\Models\User;
use Kirschbaum\PowerJoins\Tests\Models\Group;
use Kirschbaum\PowerJoins\Tests\Models\Image;
use Kirschbaum\PowerJoins\Tests\Models\Comment;
use Kirschbaum\PowerJoins\Tests\Models\PostStat;
use Kirschbaum\PowerJoins\Tests\Models\UserProfile;

class MultipleConnectionsTest extends TestCase
{
/** @test */
public function test_join_with_multiple_connections()
{
$post = factory(Post::class)->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);
}
}
17 changes: 17 additions & 0 deletions tests/TestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Kirschbaum\PowerJoins\Tests;

use Illuminate\Support\Facades\DB;
use Kirschbaum\PowerJoins\PowerJoinsServiceProvider;

class TestCase extends \Orchestra\Testbench\TestCase
Expand All @@ -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)
Expand All @@ -34,5 +45,11 @@ protected function getEnvironmentSetUp($app)
'database' => ':memory:',
'prefix' => '',
]);

$app['config']->set('database.connections.testbench-2', [
'driver' => 'sqlite',
'database' => ':memory:',
'prefix' => '',
]);
}
}
11 changes: 11 additions & 0 deletions tests/database/factories/PostStatFactory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php

use Kirschbaum\PowerJoins\Tests\Models\Post;
use Kirschbaum\PowerJoins\Tests\Models\PostStat;

$factory->define(PostStat::class, function (Faker\Generator $faker) {
return [
'post_id' => factory(Post::class),
'data' => [],
];
});
7 changes: 7 additions & 0 deletions tests/database/migrations/2020_03_16_000000_create_tables.php
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down

0 comments on commit 2bfc538

Please sign in to comment.