Skip to content

Commit

Permalink
First commit
Browse files Browse the repository at this point in the history
  • Loading branch information
maguilar92 committed Feb 7, 2018
1 parent 183c60a commit 0935048
Show file tree
Hide file tree
Showing 19 changed files with 869 additions and 0 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
vendor/
build/
composer.lock
20 changes: 20 additions & 0 deletions .travis.yml
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
34 changes: 34 additions & 0 deletions composer.json
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"
]
}
}
}
37 changes: 37 additions & 0 deletions phpunit.xml
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>
7 changes: 7 additions & 0 deletions src/CanCrossDatabaseShazaamInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?php

namespace Hoyvoy\CrossDatabase;

interface CanCrossDatabaseShazaamInterface
{
}
40 changes: 40 additions & 0 deletions src/Connectors/ConnectionFactory.php
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);
}
}
33 changes: 33 additions & 0 deletions src/CrossDatabaseServiceProvider.php
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);
});
}
}
14 changes: 14 additions & 0 deletions src/Eloquent/Builder.php
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;
}
}
37 changes: 37 additions & 0 deletions src/Eloquent/Concerns/QueriesRelationships.php
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);
}
}
20 changes: 20 additions & 0 deletions src/Eloquent/Model.php
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);
}
}
14 changes: 14 additions & 0 deletions src/EloquentBuilder.php
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;
}
}
20 changes: 20 additions & 0 deletions src/MySqlConnection.php
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);
}
}
20 changes: 20 additions & 0 deletions src/PostgresConnection.php
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);
}
}
26 changes: 26 additions & 0 deletions src/Query/Grammars/MysqlGrammar.php
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);
}
}
26 changes: 26 additions & 0 deletions src/Query/Grammars/PostgresGrammar.php
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);
}
}
Loading

0 comments on commit 0935048

Please sign in to comment.