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

Commit

Permalink
Added 5.6 changes
Browse files Browse the repository at this point in the history
  • Loading branch information
maguilar92 committed Apr 20, 2018
1 parent 4c38333 commit 51e5414
Show file tree
Hide file tree
Showing 7 changed files with 173 additions and 32 deletions.
7 changes: 7 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,13 @@
# Laravel Cross database subqueries
Eloquent cross database compatibility in subqueries.

| **Laravel** | **laravel-cross-database-subqueries** | **Lifecycle** |
|---|---|---|
| ^5.5 | ^5.5 | January 24, 2017 |
||| Bug fixes until January 2019 |
||| Security fixes until June 2020 |


# Why do I need it?
### To use the following Eloquent methods cross databases:
* has
Expand Down
14 changes: 9 additions & 5 deletions src/Eloquent/Concerns/QueriesRelationships.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ protected function addHasWhere(Builder $hasQuery, Relation $relation, $operator,
$subqueryConnection = $hasQuery->getConnection()->getDatabaseName();
$queryConnection = $this->getConnection()->getDatabaseName();
if ($queryConnection != $subqueryConnection) {
$queryFrom = $hasQuery->getQuery()->from.'<-->'.$subqueryConnection;
$queryFrom = $hasQuery->getConnection()->getTablePrefix().'<-->'.$hasQuery->getQuery()->from.'<-->'.$subqueryConnection;
$hasQuery->from($queryFrom);
}
}
Expand Down Expand Up @@ -78,25 +78,29 @@ public function withCount($relations)

$query->callScope($constraints);

$query->mergeConstraintsFrom($relation->getQuery());

// If connection implements CanCrossDatabaseShazaamInterface we must attach database
// connection name in from to be used by grammar when query compiled
if ($this->getConnection() instanceof CanCrossDatabaseShazaamInterface) {
$subqueryConnection = $query->getConnection()->getDatabaseName();
$queryConnection = $this->getConnection()->getDatabaseName();
if ($queryConnection != $subqueryConnection) {
$queryFrom = $query->getQuery()->from.'<-->'.$subqueryConnection;
$queryFrom = $query->getConnection()->getTablePrefix().'<-->'.$query->getQuery()->from.'<-->'.$subqueryConnection;
$query->from($queryFrom);
}
}

$query = $query->mergeConstraintsFrom($relation->getQuery())->toBase();

if (count($query->columns) > 1) {
$query->columns = [$query->columns[0]];
}

// Finally we will add the proper result column alias to the query and run the subselect
// statement against the query builder. Then we will return the builder instance back
// to the developer for further constraint chaining that needs to take place on it.
$column = $alias ?? Str::snake($name.'_count');

$this->selectSub($query->toBase(), $column);
$this->selectSub($query, $column);
}

return $this;
Expand Down
9 changes: 7 additions & 2 deletions src/Query/Grammars/MySqlGrammar.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,14 @@ protected function compileFrom(Builder $query, $table)
{
// Check for cross database query to attach database name
if (strpos($table, '<-->') !== false) {
list($table, $database) = explode('<-->', $table);
list($prefix, $table, $database) = explode('<-->', $table);
$wrappedTable = $this->wrapTable($table, true);
$wrappedTablePrefixed = $this->wrap($prefix.$table, true);
if ($wrappedTable != $wrappedTablePrefixed) {
return 'from '.$this->wrap($database).'.'.$wrappedTablePrefixed.' as '.$wrappedTable;
}

return 'from '.$this->wrap($database).'.'.$this->wrapTable($table);
return 'from '.$this->wrap($database).'.'.$wrappedTablePrefixed;
}

return 'from '.$this->wrapTable($table);
Expand Down
9 changes: 7 additions & 2 deletions src/Query/Grammars/PostgresGrammar.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,14 @@ protected function compileFrom(Builder $query, $table)
{
// Check for cross database query to attach database name
if (strpos($table, '<-->') !== false) {
list($table, $database) = explode('<-->', $table);
list($prefix, $table, $database) = explode('<-->', $table);
$wrappedTable = $this->wrapTable($table, true);
$wrappedTablePrefixed = $this->wrap($prefix.$table, true);
if ($wrappedTable != $wrappedTablePrefixed) {
return 'from '.$this->wrap($database).'.'.$wrappedTablePrefixed.' as '.$wrappedTable;
}

return 'from '.$this->wrap($database).'.'.$this->wrapTable($table);
return 'from '.$this->wrap($database).'.'.$wrappedTablePrefixed;
}

return 'from '.$this->wrapTable($table);
Expand Down
10 changes: 8 additions & 2 deletions src/Query/Grammars/SqlServerGrammar.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,16 @@ class SqlServerGrammar extends IlluminateSqlServerGrammar
protected function compileFrom(Builder $query, $table)
{
$from = 'from '.$this->wrapTable($table);

// Check for cross database query to attach database name
if (strpos($table, '<-->') !== false) {
list($table, $database) = explode('<-->', $table);
$from = 'from '.$this->wrap($database).'.'.$this->wrapTable($table);
list($prefix, $table, $database) = explode('<-->', $table);
$wrappedTable = $this->wrapTable($table, true);
$wrappedTablePrefixed = $this->wrap($prefix.$table, true);
$from = 'from '.$this->wrap($database).'.'.$wrappedTablePrefixed;
if ($wrappedTable != $wrappedTablePrefixed) {
$from = 'from '.$this->wrap($database).'.'.$wrappedTablePrefixed.' as '.$wrappedTable;
}
}

if (is_string($query->lock)) {
Expand Down
Loading

0 comments on commit 51e5414

Please sign in to comment.