Skip to content
This repository was archived by the owner on Sep 9, 2021. It is now read-only.

Commit 51e5414

Browse files
committed
Added 5.6 changes
1 parent 4c38333 commit 51e5414

File tree

7 files changed

+173
-32
lines changed

7 files changed

+173
-32
lines changed

README.md

+7
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,13 @@
33
# Laravel Cross database subqueries
44
Eloquent cross database compatibility in subqueries.
55

6+
| **Laravel** | **laravel-cross-database-subqueries** | **Lifecycle** |
7+
|---|---|---|
8+
| ^5.5 | ^5.5 | January 24, 2017 |
9+
||| Bug fixes until January 2019 |
10+
||| Security fixes until June 2020 |
11+
12+
613
# Why do I need it?
714
### To use the following Eloquent methods cross databases:
815
* has

src/Eloquent/Concerns/QueriesRelationships.php

+9-5
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ protected function addHasWhere(Builder $hasQuery, Relation $relation, $operator,
2828
$subqueryConnection = $hasQuery->getConnection()->getDatabaseName();
2929
$queryConnection = $this->getConnection()->getDatabaseName();
3030
if ($queryConnection != $subqueryConnection) {
31-
$queryFrom = $hasQuery->getQuery()->from.'<-->'.$subqueryConnection;
31+
$queryFrom = $hasQuery->getConnection()->getTablePrefix().'<-->'.$hasQuery->getQuery()->from.'<-->'.$subqueryConnection;
3232
$hasQuery->from($queryFrom);
3333
}
3434
}
@@ -78,25 +78,29 @@ public function withCount($relations)
7878

7979
$query->callScope($constraints);
8080

81-
$query->mergeConstraintsFrom($relation->getQuery());
82-
8381
// If connection implements CanCrossDatabaseShazaamInterface we must attach database
8482
// connection name in from to be used by grammar when query compiled
8583
if ($this->getConnection() instanceof CanCrossDatabaseShazaamInterface) {
8684
$subqueryConnection = $query->getConnection()->getDatabaseName();
8785
$queryConnection = $this->getConnection()->getDatabaseName();
8886
if ($queryConnection != $subqueryConnection) {
89-
$queryFrom = $query->getQuery()->from.'<-->'.$subqueryConnection;
87+
$queryFrom = $query->getConnection()->getTablePrefix().'<-->'.$query->getQuery()->from.'<-->'.$subqueryConnection;
9088
$query->from($queryFrom);
9189
}
9290
}
9391

92+
$query = $query->mergeConstraintsFrom($relation->getQuery())->toBase();
93+
94+
if (count($query->columns) > 1) {
95+
$query->columns = [$query->columns[0]];
96+
}
97+
9498
// Finally we will add the proper result column alias to the query and run the subselect
9599
// statement against the query builder. Then we will return the builder instance back
96100
// to the developer for further constraint chaining that needs to take place on it.
97101
$column = $alias ?? Str::snake($name.'_count');
98102

99-
$this->selectSub($query->toBase(), $column);
103+
$this->selectSub($query, $column);
100104
}
101105

102106
return $this;

src/Query/Grammars/MySqlGrammar.php

+7-2
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,14 @@ protected function compileFrom(Builder $query, $table)
1919
{
2020
// Check for cross database query to attach database name
2121
if (strpos($table, '<-->') !== false) {
22-
list($table, $database) = explode('<-->', $table);
22+
list($prefix, $table, $database) = explode('<-->', $table);
23+
$wrappedTable = $this->wrapTable($table, true);
24+
$wrappedTablePrefixed = $this->wrap($prefix.$table, true);
25+
if ($wrappedTable != $wrappedTablePrefixed) {
26+
return 'from '.$this->wrap($database).'.'.$wrappedTablePrefixed.' as '.$wrappedTable;
27+
}
2328

24-
return 'from '.$this->wrap($database).'.'.$this->wrapTable($table);
29+
return 'from '.$this->wrap($database).'.'.$wrappedTablePrefixed;
2530
}
2631

2732
return 'from '.$this->wrapTable($table);

src/Query/Grammars/PostgresGrammar.php

+7-2
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,14 @@ protected function compileFrom(Builder $query, $table)
1919
{
2020
// Check for cross database query to attach database name
2121
if (strpos($table, '<-->') !== false) {
22-
list($table, $database) = explode('<-->', $table);
22+
list($prefix, $table, $database) = explode('<-->', $table);
23+
$wrappedTable = $this->wrapTable($table, true);
24+
$wrappedTablePrefixed = $this->wrap($prefix.$table, true);
25+
if ($wrappedTable != $wrappedTablePrefixed) {
26+
return 'from '.$this->wrap($database).'.'.$wrappedTablePrefixed.' as '.$wrappedTable;
27+
}
2328

24-
return 'from '.$this->wrap($database).'.'.$this->wrapTable($table);
29+
return 'from '.$this->wrap($database).'.'.$wrappedTablePrefixed;
2530
}
2631

2732
return 'from '.$this->wrapTable($table);

src/Query/Grammars/SqlServerGrammar.php

+8-2
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,16 @@ class SqlServerGrammar extends IlluminateSqlServerGrammar
1818
protected function compileFrom(Builder $query, $table)
1919
{
2020
$from = 'from '.$this->wrapTable($table);
21+
2122
// Check for cross database query to attach database name
2223
if (strpos($table, '<-->') !== false) {
23-
list($table, $database) = explode('<-->', $table);
24-
$from = 'from '.$this->wrap($database).'.'.$this->wrapTable($table);
24+
list($prefix, $table, $database) = explode('<-->', $table);
25+
$wrappedTable = $this->wrapTable($table, true);
26+
$wrappedTablePrefixed = $this->wrap($prefix.$table, true);
27+
$from = 'from '.$this->wrap($database).'.'.$wrappedTablePrefixed;
28+
if ($wrappedTable != $wrappedTablePrefixed) {
29+
$from = 'from '.$this->wrap($database).'.'.$wrappedTablePrefixed.' as '.$wrappedTable;
30+
}
2531
}
2632

2733
if (is_string($query->lock)) {

0 commit comments

Comments
 (0)