-
-
Notifications
You must be signed in to change notification settings - Fork 42
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
194c53a
commit 2994f04
Showing
27 changed files
with
1,059 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,6 @@ | ||
/tests export-ignore | ||
.gitattributes export-ignore | ||
.gitignore export-ignore | ||
.scrutinizer.yml export-ignore | ||
.travis.yml export-ignore | ||
phpunit.xml.dist export-ignore |
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,8 @@ | ||
/.idea | ||
/.vagrant | ||
/vendor | ||
after.sh | ||
aliases | ||
composer.lock | ||
Homestead.yaml | ||
Vagrantfile |
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,4 @@ | ||
tools: | ||
external_code_coverage: | ||
runs: 3 | ||
timeout: 600 |
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,55 @@ | ||
language: php | ||
|
||
dist: xenial | ||
|
||
services: | ||
- postgresql | ||
|
||
addons: | ||
mariadb: 10.2 | ||
|
||
env: | ||
global: | ||
- COVERAGE=no | ||
- DB=mysql | ||
- RELEASE=stable | ||
|
||
matrix: | ||
include: | ||
- php: 7.0 | ||
- php: 7.0 | ||
env: RELEASE=lowest | ||
- php: 7.1 | ||
- php: 7.2 | ||
- php: 7.3 | ||
env: COVERAGE=yes | ||
- php: 7.3 | ||
env: COVERAGE=yes DB=pgsql | ||
- php: 7.3 | ||
env: COVERAGE=yes DB=sqlite | ||
|
||
cache: | ||
directories: | ||
- $HOME/.composer/cache | ||
|
||
before_install: | ||
- COMPOSER_FLAGS=$([ $RELEASE == "lowest" ] && echo "--prefer-lowest" || echo "") | ||
- PHPUNIT_FLAGS=$([ $COVERAGE == "yes" ] && echo "--coverage-clover=coverage.xml" || echo "") | ||
|
||
install: | ||
- travis_retry composer update --no-interaction --no-suggest --prefer-dist --prefer-stable $COMPOSER_FLAGS | ||
|
||
before_script: | ||
- cp tests/config/database.travis.php tests/config/database.php | ||
- mysql -e 'create database `test`;' | ||
- psql -c 'create database "test";' -U postgres | ||
|
||
script: | ||
- vendor/bin/phpunit $PHPUNIT_FLAGS | ||
|
||
after_script: | ||
- | | ||
if [ $COVERAGE == "yes" ]; then | ||
travis_retry wget https://scrutinizer-ci.com/ocular.phar | ||
travis_retry php ocular.phar code-coverage:upload --format=php-clover coverage.xml | ||
fi |
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,106 @@ | ||
[![Build Status](https://travis-ci.org/staudenmeir/laravel-cte.svg?branch=master)](https://travis-ci.org/staudenmeir/laravel-cte) | ||
[![Code Coverage](https://scrutinizer-ci.com/g/staudenmeir/laravel-cte/badges/coverage.png?b=master)](https://scrutinizer-ci.com/g/staudenmeir/laravel-cte/?branch=master) | ||
[![Scrutinizer Code Quality](https://scrutinizer-ci.com/g/staudenmeir/laravel-cte/badges/quality-score.png?b=master)](https://scrutinizer-ci.com/g/staudenmeir/laravel-cte/?branch=master) | ||
[![Latest Stable Version](https://poser.pugx.org/staudenmeir/laravel-cte/v/stable)](https://packagist.org/packages/staudenmeir/laravel-cte) | ||
[![Total Downloads](https://poser.pugx.org/staudenmeir/laravel-cte/downloads)](https://packagist.org/packages/staudenmeir/laravel-cte) | ||
[![License](https://poser.pugx.org/staudenmeir/laravel-cte/license)](https://packagist.org/packages/staudenmeir/laravel-cte) | ||
|
||
## Introduction | ||
This Laravel extension adds support for common table expressions (CTE) to the query builder and Eloquent. | ||
Supports Laravel 5.5+. | ||
|
||
## Installation | ||
|
||
composer require staudenmeir/laravel-cte:"^1.0" | ||
|
||
## Usage | ||
|
||
- [SELECT Queries](#select-queries) | ||
- [INSERT/UPDATE/DELETE Queries](#insertupdatedelete-queries) | ||
- [Eloquent](#eloquent) | ||
|
||
### SELECT Queries | ||
|
||
Use `withExpression()` and provide a query builder instance, an SQL string or a closure: | ||
|
||
```php | ||
$posts = DB::table('p') | ||
->select('p.*', 'u.name') | ||
->withExpression('p', DB::table('posts')) | ||
->withExpression('u', function ($query) { | ||
$query->from('users'); | ||
}) | ||
->join('u', 'u.id', '=', 'p.user_id') | ||
->get(); | ||
``` | ||
|
||
Use `withRecursiveExpression()` for recursive expressions: | ||
|
||
```php | ||
$query = DB::table('users') | ||
->whereNull('parent_id') | ||
->unionAll( | ||
DB::table('users') | ||
->select('users.*') | ||
->join('tree', 'tree.id', '=', 'users.parent_id') | ||
); | ||
|
||
$tree = DB::table('tree') | ||
->withRecursiveExpression('tree', $query) | ||
->get(); | ||
``` | ||
|
||
You can provide the expression's columns as the third argument: | ||
|
||
```php | ||
$query = 'select 1 union all select number + 1 from numbers where number < 10'; | ||
|
||
$numbers = DB::table('numbers') | ||
->withRecursiveExpression('numbers', $query, ['number']) | ||
->get(); | ||
``` | ||
|
||
### INSERT/UPDATE/DELETE Queries | ||
|
||
You can use common table expressions in `INSERT`(Laravel 5.7.17+), `UPDATE` and `DELETE` queries: | ||
|
||
```php | ||
DB::table('profiles') | ||
->withExpression('u', DB::table('users')->select('id', 'name')) | ||
->insertUsing(['user_id', 'name'], DB::table('u')); | ||
``` | ||
|
||
```php | ||
DB::table('profiles') | ||
->withExpression('u', DB::table('users')) | ||
->join('u', 'u.id', '=', 'profiles.user_id') | ||
->update(['profiles.name' => DB::raw('u.name')]); | ||
``` | ||
|
||
```php | ||
DB::table('profiles') | ||
->withExpression('u', DB::table('users')->where('active', false)) | ||
->whereIn('user_id', DB::table('u')->select('id')) | ||
->delete(); | ||
``` | ||
|
||
### Eloquent | ||
|
||
You can use common table expressions in Eloquent queries with the `QueriesExpressions` trait: | ||
|
||
```php | ||
class User extends Model | ||
{ | ||
use \Staudenmeir\LaravelCte\Eloquent\QueriesExpressions; | ||
} | ||
|
||
$query = User::whereNull('parent_id') | ||
->unionAll( | ||
User::select('users.*') | ||
->join('tree', 'tree.id', '=', 'users.parent_id') | ||
); | ||
|
||
$tree = User::from('tree') | ||
->withRecursiveExpression('tree', $query) | ||
->get(); | ||
``` |
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,36 @@ | ||
{ | ||
"name": "staudenmeir/laravel-cte", | ||
"description": "Laravel queries with common table expressions", | ||
"license": "MIT", | ||
"authors": [ | ||
{ | ||
"name": "Jonas Staudenmeir", | ||
"email": "mail@jonas-staudenmeir.de" | ||
} | ||
], | ||
"require": { | ||
"php": ">=7.0", | ||
"illuminate/database": "5.5.*|5.6.*|5.7.*" | ||
}, | ||
"require-dev": { | ||
"laravel/homestead": "^7.0", | ||
"orchestra/testbench": "^3.5" | ||
}, | ||
"autoload": { | ||
"psr-4": { | ||
"Staudenmeir\\LaravelCte\\": "src/" | ||
} | ||
}, | ||
"autoload-dev": { | ||
"psr-4": { | ||
"Tests\\": "tests/" | ||
} | ||
}, | ||
"extra": { | ||
"laravel": { | ||
"providers": [ | ||
"Staudenmeir\\LaravelCte\\DatabaseServiceProvider" | ||
] | ||
} | ||
} | ||
} |
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,23 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<phpunit backupGlobals="false" | ||
backupStaticAttributes="false" | ||
colors="true" | ||
convertErrorsToExceptions="true" | ||
convertNoticesToExceptions="true" | ||
convertWarningsToExceptions="true" | ||
processIsolation="false" | ||
stopOnError="false" | ||
stopOnFailure="false" | ||
verbose="true" | ||
> | ||
<testsuites> | ||
<testsuite name="LaravelCte Test Suite"> | ||
<directory suffix="Test.php">./tests</directory> | ||
</testsuite> | ||
</testsuites> | ||
<filter> | ||
<whitelist processUncoveredFilesFromWhitelist="true"> | ||
<directory suffix=".php">./src</directory> | ||
</whitelist> | ||
</filter> | ||
</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,18 @@ | ||
<?php | ||
|
||
namespace Staudenmeir\LaravelCte\Connections; | ||
|
||
use Staudenmeir\LaravelCte\Query\Builder; | ||
|
||
trait CreatesQueryBuilder | ||
{ | ||
/** | ||
* Get a new query builder instance. | ||
* | ||
* @return \Illuminate\Database\Query\Builder | ||
*/ | ||
public function query() | ||
{ | ||
return new Builder($this, $this->getQueryGrammar(), $this->getPostProcessor()); | ||
} | ||
} |
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,21 @@ | ||
<?php | ||
|
||
namespace Staudenmeir\LaravelCte\Connections; | ||
|
||
use Illuminate\Database\MySqlConnection as Base; | ||
use Staudenmeir\LaravelCte\Grammars\MySqlGrammar; | ||
|
||
class MySqlConnection extends Base | ||
{ | ||
use CreatesQueryBuilder; | ||
|
||
/** | ||
* Get the default query grammar instance. | ||
* | ||
* @return \Illuminate\Database\Query\Grammars\MySqlGrammar | ||
*/ | ||
protected function getDefaultQueryGrammar() | ||
{ | ||
return $this->withTablePrefix(new MySqlGrammar); | ||
} | ||
} |
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,21 @@ | ||
<?php | ||
|
||
namespace Staudenmeir\LaravelCte\Connections; | ||
|
||
use Illuminate\Database\PostgresConnection as Base; | ||
use Staudenmeir\LaravelCte\Grammars\PostgresGrammar; | ||
|
||
class PostgresConnection extends Base | ||
{ | ||
use CreatesQueryBuilder; | ||
|
||
/** | ||
* Get the default query grammar instance. | ||
* | ||
* @return \Illuminate\Database\Query\Grammars\PostgresGrammar | ||
*/ | ||
protected function getDefaultQueryGrammar() | ||
{ | ||
return $this->withTablePrefix(new PostgresGrammar); | ||
} | ||
} |
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,21 @@ | ||
<?php | ||
|
||
namespace Staudenmeir\LaravelCte\Connections; | ||
|
||
use Illuminate\Database\SQLiteConnection as Base; | ||
use Staudenmeir\LaravelCte\Grammars\SQLiteGrammar; | ||
|
||
class SQLiteConnection extends Base | ||
{ | ||
use CreatesQueryBuilder; | ||
|
||
/** | ||
* Get the default query grammar instance. | ||
* | ||
* @return \Illuminate\Database\Query\Grammars\SQLiteGrammar | ||
*/ | ||
protected function getDefaultQueryGrammar() | ||
{ | ||
return $this->withTablePrefix(new SQLiteGrammar); | ||
} | ||
} |
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,21 @@ | ||
<?php | ||
|
||
namespace Staudenmeir\LaravelCte\Connections; | ||
|
||
use Illuminate\Database\SqlServerConnection as Base; | ||
use Staudenmeir\LaravelCte\Grammars\SqlServerGrammar; | ||
|
||
class SqlServerConnection extends Base | ||
{ | ||
use CreatesQueryBuilder; | ||
|
||
/** | ||
* Get the default query grammar instance. | ||
* | ||
* @return \Illuminate\Database\Query\Grammars\SqlServerGrammar | ||
*/ | ||
protected function getDefaultQueryGrammar() | ||
{ | ||
return $this->withTablePrefix(new SqlServerGrammar); | ||
} | ||
} |
Oops, something went wrong.