forked from hoyvoy/laravel-cross-database-subqueries
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
183c60a
commit 0935048
Showing
19 changed files
with
869 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
vendor/ | ||
build/ | ||
composer.lock |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
language: php | ||
php: | ||
- "7.0.21" | ||
- "7.1" | ||
- "7.2" | ||
|
||
before_script: | ||
- curl -sS https://getcomposer.org/installer | php -- --filename=composer | ||
- chmod +x composer | ||
- composer install -n | ||
|
||
script: | ||
- php vendor/bin/phpunit | ||
|
||
after_script: | ||
- php vendor/bin/codacycoverage clover build/logs/clover.xml | ||
|
||
branches: | ||
only: | ||
- master |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
{ | ||
"name": "hoyvoy/laravel-cross-database-subqueries", | ||
"description": "Eloquent cross database compatibility in subqueries", | ||
"type": "library", | ||
"require": { | ||
"php": ">=7.0.0", | ||
"illuminate/support": "^5.5", | ||
"illuminate/container": "^5.5", | ||
"illuminate/database": "^5.5", | ||
"illuminate/events": "^5.5" | ||
}, | ||
"require-dev": { | ||
"phpunit/phpunit": "~6.0", | ||
"orchestra/testbench": "3.5.x", | ||
"codacy/coverage": "dev-master" | ||
}, | ||
"autoload": { | ||
"psr-4": { | ||
"Hoyvoy\\CrossDatabase\\": "src/" | ||
} | ||
}, | ||
"autoload-dev": { | ||
"psr-4": { | ||
"Hoyvoy\\Tests\\" : "tests" | ||
} | ||
}, | ||
"extra": { | ||
"laravel": { | ||
"providers": [ | ||
"Hoyvoy\\CrossDatabase\\CrossDatabaseServiceProvider" | ||
] | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<phpunit backupGlobals="false" | ||
backupStaticAttributes="false" | ||
beStrictAboutTestsThatDoNotTestAnything="false" | ||
colors="true" | ||
convertErrorsToExceptions="true" | ||
convertNoticesToExceptions="true" | ||
convertWarningsToExceptions="true" | ||
stopOnError="false" | ||
stopOnFailure="false" | ||
stopOnIncomplete="false" | ||
stopOnSkipped="false" | ||
syntaxCheck="true" | ||
verbose="true"> | ||
<testsuites> | ||
<testsuite name="Application Test Suite"> | ||
<directory>./tests/</directory> | ||
</testsuite> | ||
</testsuites> | ||
|
||
<logging> | ||
<log type="coverage-html" target="build/coverage"/> | ||
<log type="coverage-clover" target="build/logs/clover.xml"/> | ||
<log type="junit" target="build/logs/junit.xml" logIncompleteSkipped="false"/> | ||
</logging> | ||
|
||
<filter> | ||
<whitelist> | ||
<directory>./src/</directory> | ||
<exclude> | ||
<directory>./vendor/</directory> | ||
</exclude> | ||
</whitelist> | ||
</filter> | ||
<php> | ||
</php> | ||
</phpunit> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
<?php | ||
|
||
namespace Hoyvoy\CrossDatabase; | ||
|
||
interface CanCrossDatabaseShazaamInterface | ||
{ | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
<?php | ||
|
||
namespace Hoyvoy\CrossDatabase\Connectors; | ||
|
||
use Illuminate\Database\Connection; | ||
use Hoyvoy\CrossDatabase\MySqlConnection; | ||
use Hoyvoy\CrossDatabase\PostgresConnection; | ||
use Hoyvoy\CrossDatabase\SqlServerConnection; | ||
use Illuminate\Database\Connectors\ConnectionFactory as IlluminateConnectionFactory; | ||
use PDO; | ||
|
||
class ConnectionFactory extends IlluminateConnectionFactory | ||
{ | ||
/** | ||
* @param string $driver | ||
* @param \Closure|PDO $connection | ||
* @param string $database | ||
* @param string $prefix | ||
* @param array $config | ||
* | ||
* @return \Illuminate\Database\ConnectionInterface | ||
*/ | ||
protected function createConnection($driver, $connection, $database, $prefix = '', array $config = []) | ||
{ | ||
if ($resolver = Connection::getResolver($driver)) { | ||
return $resolver($connection, $database, $prefix, $config); | ||
} | ||
|
||
switch ($driver) { | ||
case 'mysql': | ||
return new MySqlConnection($connection, $database, $prefix, $config); | ||
case 'pgsql': | ||
return new PostgresConnection($connection, $database, $prefix, $config); | ||
case 'sqlsrv': | ||
return new SqlServerConnection($connection, $database, $prefix, $config); | ||
} | ||
|
||
return parent::createConnection($driver, $connection, $database, $prefix, $config); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
<?php | ||
|
||
namespace Hoyvoy\CrossDatabase; | ||
|
||
use Hoyvoy\CrossDatabase\Connectors\ConnectionFactory; | ||
use Illuminate\Database\Eloquent\Model; | ||
use Illuminate\Support\ServiceProvider; | ||
|
||
class CrossDatabaseServiceProvider extends ServiceProvider | ||
{ | ||
/** | ||
* Bootstrap the application events. | ||
*/ | ||
public function boot() | ||
{ | ||
Model::setConnectionResolver($this->app['db']); | ||
|
||
Model::setEventDispatcher($this->app['events']); | ||
} | ||
|
||
/** | ||
* Register the service provider. | ||
*/ | ||
public function register() | ||
{ | ||
// The connection factory is used to create the actual connection instances on | ||
// the database. We will inject the factory into the manager so that it may | ||
// make the connections while they are actually needed and not of before. | ||
$this->app->singleton('db.factory', function ($app) { | ||
return new ConnectionFactory($app); | ||
}); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
<?php | ||
|
||
namespace Hoyvoy\CrossDatabase\Eloquent; | ||
|
||
use Illuminate\Database\Eloquent\Builder as IlluminateEloquentBuilder; | ||
use Illuminate\Database\Eloquent\Concerns\QueriesRelationships as IlluminateEloquentQueriesRelationships; | ||
use Hoyvoy\CrossDatabase\Eloquent\Concerns\QueriesRelationships as CrossDatabaseQueriesRelationships; | ||
|
||
class Builder extends IlluminateEloquentBuilder | ||
{ | ||
use IlluminateEloquentQueriesRelationships, CrossDatabaseQueriesRelationships { | ||
CrossDatabaseQueriesRelationships::addHasWhere insteadof IlluminateEloquentQueriesRelationships; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
<?php | ||
|
||
namespace Hoyvoy\CrossDatabase\Eloquent\Concerns; | ||
|
||
use Closure; | ||
use Illuminate\Database\Eloquent\Builder; | ||
use Illuminate\Database\Eloquent\Relations\Relation; | ||
use Hoyvoy\CrossDatabase\CanCrossDatabaseShazaamInterface; | ||
|
||
trait QueriesRelationships | ||
{ | ||
/** | ||
* Add the "has" condition where clause to the query. | ||
* | ||
* @param \Illuminate\Database\Eloquent\Builder $hasQuery | ||
* @param \Illuminate\Database\Eloquent\Relations\Relation $relation | ||
* @param string $operator | ||
* @param int $count | ||
* @param string $boolean | ||
* @return \Illuminate\Database\Eloquent\Builder|static | ||
*/ | ||
protected function addHasWhere(Builder $hasQuery, Relation $relation, $operator, $count, $boolean) | ||
{ | ||
// 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 = $hasQuery->getConnection()->getDatabaseName(); | ||
$queryConnection = $this->getConnection()->getDatabaseName(); | ||
if ($queryConnection != $subqueryConnection) { | ||
$queryFrom = $hasQuery->getQuery()->from.'<-->'.$subqueryConnection; | ||
$hasQuery->from($queryFrom); | ||
} | ||
} | ||
|
||
return parent::addHasWhere($hasQuery, $relation, $operator, $count, $boolean); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
<?php | ||
|
||
namespace Hoyvoy\CrossDatabase\Eloquent; | ||
|
||
use Hoyvoy\CrossDatabase\Eloquent\Builder; | ||
use Illuminate\Database\Eloquent\Model as IlluminateModel; | ||
|
||
abstract class Model extends IlluminateModel | ||
{ | ||
/** | ||
* Create a new Eloquent query builder for the model. | ||
* | ||
* @param \Illuminate\Database\Query\Builder $query | ||
* @return \oyvoy\Support\CrossDatabase\Eloquent\Builder|static | ||
*/ | ||
public function newEloquentBuilder($query) | ||
{ | ||
return new Builder($query); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
<?php | ||
|
||
namespace Hoyvoy\CrossDatabase; | ||
|
||
use Illuminate\Database\Eloquent\Builder; | ||
use Illuminate\Database\Eloquent\Concerns\QueriesRelationships; | ||
use Hoyvoy\CrossDatabase\Concerns\QueriesRelationships as CrossDatabaseQueriesRelationships; | ||
|
||
class EloquentBuilder extends Builder | ||
{ | ||
use QueriesRelationships, CrossDatabaseQueriesRelationships { | ||
CrossDatabaseQueriesRelationships::has insteadof QueriesRelationships; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
<?php | ||
|
||
namespace Hoyvoy\CrossDatabase; | ||
|
||
use Hoyvoy\CrossDatabase\CanCrossDatabaseShazaamInterface; | ||
use Illuminate\Database\MySqlConnection as IlluminateMySqlConnection; | ||
use Hoyvoy\CrossDatabase\Query\Grammars\MySqlGrammar as MySqlQueryGrammar; | ||
|
||
class MySqlConnection extends IlluminateMySqlConnection implements CanCrossDatabaseShazaamInterface | ||
{ | ||
/** | ||
* Get the default query grammar instance. | ||
* | ||
* @return \Illuminate\Database\Query\Grammars\MySqlGrammar | ||
*/ | ||
protected function getDefaultQueryGrammar() | ||
{ | ||
return $this->withTablePrefix(new MySqlQueryGrammar); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
<?php | ||
|
||
namespace Hoyvoy\CrossDatabase; | ||
|
||
use Hoyvoy\CrossDatabase\CanCrossDatabaseShazaamInterface; | ||
use Illuminate\Database\PostgresConnection as IlluminatePostgresConnection; | ||
use Hoyvoy\CrossDatabase\Query\Grammars\PostgresGrammar as PostgresQueryGrammar; | ||
|
||
class PostgresConnection extends IlluminatePostgresConnection implements CanCrossDatabaseShazaamInterface | ||
{ | ||
/** | ||
* Get the default query grammar instance. | ||
* | ||
* @return \Illuminate\Database\Query\Grammars\PostgresGrammar | ||
*/ | ||
protected function getDefaultQueryGrammar() | ||
{ | ||
return $this->withTablePrefix(new PostgresQueryGrammar); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
<?php | ||
|
||
namespace Hoyvoy\CrossDatabase\Query\Grammars; | ||
|
||
use Illuminate\Database\Query\Builder; | ||
use Illuminate\Database\Query\Grammars\MySqlGrammar as IlluminateMySqlGrammar; | ||
|
||
class MySqlGrammar extends IlluminateMySqlGrammar | ||
{ | ||
/** | ||
* Compile the "from" portion of the query. | ||
* | ||
* @param \Illuminate\Database\Query\Builder $query | ||
* @param string $table | ||
* @return string | ||
*/ | ||
protected function compileFrom(Builder $query, $table) | ||
{ | ||
// Check for cross database query to attach database name | ||
if (strpos($table, '<-->') !== false) { | ||
list($table, $database) = explode('<-->', $table); | ||
return 'from '.$this->wrap($database).'.'.$this->wrapTable($table); | ||
} | ||
return 'from '.$this->wrapTable($table); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
<?php | ||
|
||
namespace Hoyvoy\CrossDatabase\Query\Grammars; | ||
|
||
use Illuminate\Database\Query\Builder; | ||
use Illuminate\Database\Query\Grammars\PostgresGrammar as IlluminatePostgresGrammar; | ||
|
||
class PostgresGrammar extends IlluminatePostgresGrammar | ||
{ | ||
/** | ||
* Compile the "from" portion of the query. | ||
* | ||
* @param \Illuminate\Database\Query\Builder $query | ||
* @param string $table | ||
* @return string | ||
*/ | ||
protected function compileFrom(Builder $query, $table) | ||
{ | ||
// Check for cross database query to attach database name | ||
if (strpos($table, '<-->') !== false) { | ||
list($table, $database) = explode('<-->', $table); | ||
return 'from '.$this->wrap($database).'.'.$this->wrapTable($table); | ||
} | ||
return 'from '.$this->wrapTable($table); | ||
} | ||
} |
Oops, something went wrong.