generated from spatie/package-skeleton-laravel
-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #120 from clickbar/geometry-cast-expressions
[2.x] Refactor to remove GeometryType enum
- Loading branch information
Showing
27 changed files
with
499 additions
and
392 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
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
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
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
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
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
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,62 @@ | ||
<?php | ||
|
||
namespace Clickbar\Magellan\Database\Builder; | ||
|
||
use Clickbar\Magellan\Database\MagellanExpressions\ColumnParameter; | ||
use Closure; | ||
use Illuminate\Contracts\Database\Query\Expression; | ||
use Illuminate\Database\Eloquent\Builder as EloquentBuilder; | ||
use Illuminate\Database\Eloquent\Relations\Relation; | ||
use Illuminate\Database\Grammar; | ||
use Illuminate\Database\Query\Builder; | ||
use Illuminate\Support\Facades\DB; | ||
|
||
trait StringifiesQueryParameters | ||
{ | ||
public function stringifyQueryParameter(Grammar $grammar, mixed $param): string | ||
{ | ||
|
||
// 1. Check if param is queryable -> it's a subquery | ||
if ($this->isQueryable($param)) { | ||
// --> Create sub and replace with param array | ||
return $this->createSub($param); | ||
} | ||
|
||
// 2. Basic Binding Value | ||
if ($param instanceof ValueParameter) { | ||
// --> escape and replace | ||
return $grammar->escape($param->getValue()); | ||
} | ||
|
||
// 3. expression | ||
if ($param instanceof Expression) { | ||
return $param->getValue($grammar); | ||
} | ||
|
||
// 4. Column Parameter | ||
if ($param instanceof ColumnParameter) { | ||
return $grammar->wrap($param->getValue()); | ||
} | ||
|
||
// 5. string / default | ||
return $grammar->wrap($param); | ||
} | ||
|
||
private function createSub($query): string | ||
{ | ||
if ($query instanceof Closure) { | ||
$callback = $query; | ||
$callback($query = DB::query()); | ||
} | ||
|
||
return "({$query->toRawSql()})"; | ||
} | ||
|
||
private function isQueryable($value): bool | ||
{ | ||
return $value instanceof Builder || | ||
$value instanceof EloquentBuilder || | ||
$value instanceof Relation || | ||
$value instanceof Closure; | ||
} | ||
} |
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
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,22 @@ | ||
<?php | ||
|
||
namespace Clickbar\Magellan\Database\Expressions; | ||
|
||
use Closure; | ||
use Illuminate\Contracts\Database\Query\Expression as ExpressionContract; | ||
use Illuminate\Database\Grammar; | ||
|
||
class AsGeography extends GeometryWrapperExpression | ||
{ | ||
public function __construct( | ||
public string|ExpressionContract|Closure $expression, | ||
) { | ||
} | ||
|
||
public function getValue(Grammar $grammar): string | ||
{ | ||
$expression = $this->stringifyQueryParameter($grammar, $this->expression); | ||
|
||
return "($expression)::geography"; | ||
} | ||
} |
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,22 @@ | ||
<?php | ||
|
||
namespace Clickbar\Magellan\Database\Expressions; | ||
|
||
use Closure; | ||
use Illuminate\Contracts\Database\Query\Expression as ExpressionContract; | ||
use Illuminate\Database\Grammar; | ||
|
||
class AsGeometry extends GeometryWrapperExpression | ||
{ | ||
public function __construct( | ||
public string|ExpressionContract|Closure $expression, | ||
) { | ||
} | ||
|
||
public function getValue(Grammar $grammar): string | ||
{ | ||
$expression = $this->stringifyQueryParameter($grammar, $this->expression); | ||
|
||
return "($expression)::geometry"; | ||
} | ||
} |
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,11 @@ | ||
<?php | ||
|
||
namespace Clickbar\Magellan\Database\Expressions; | ||
|
||
use Clickbar\Magellan\Database\Builder\StringifiesQueryParameters; | ||
use Illuminate\Contracts\Database\Query\Expression; | ||
|
||
abstract class GeometryWrapperExpression implements Expression | ||
{ | ||
use StringifiesQueryParameters; | ||
} |
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 @@ | ||
<?php | ||
|
||
namespace Clickbar\Magellan\Database\MagellanExpressions; | ||
|
||
/** | ||
* @internal | ||
* | ||
* Wrapper class for the parameters that represents a database column. | ||
* We need this wrapper to distinguish when generating the actual SQL of a MagellanBaseExpression. | ||
*/ | ||
class ColumnParameter | ||
{ | ||
protected function __construct( | ||
protected readonly string $column, | ||
) { | ||
} | ||
|
||
/** | ||
* Only wrap the given value into the ColumnParameter if it is string. | ||
* | ||
* @return self|mixed | ||
*/ | ||
public static function wrap(mixed $value): mixed | ||
{ | ||
if (is_string($value)) { | ||
return new self($value); | ||
} | ||
|
||
return $value; | ||
} | ||
|
||
public function getValue(): string | ||
{ | ||
return $this->column; | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.