1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Staudenmeir\LaravelAdjacencyList\Eloquent; |
4
|
|
|
|
5
|
|
|
use Illuminate\Database\Eloquent\Builder as Base; |
6
|
|
|
use Illuminate\Database\PostgresConnection; |
7
|
|
|
use RuntimeException; |
8
|
|
|
use Staudenmeir\LaravelAdjacencyList\Query\Grammars\MySqlGrammar; |
9
|
|
|
use Staudenmeir\LaravelAdjacencyList\Query\Grammars\PostgresGrammar; |
10
|
|
|
use Staudenmeir\LaravelAdjacencyList\Query\Grammars\SQLiteGrammar; |
11
|
|
|
use Staudenmeir\LaravelAdjacencyList\Query\Grammars\SqlServerGrammar; |
12
|
|
|
|
13
|
|
|
class Builder extends Base |
14
|
|
|
{ |
15
|
|
|
/** |
16
|
|
|
* Register all passed global scopes. |
17
|
|
|
* |
18
|
|
|
* @param array|null $scopes |
19
|
|
|
* @return $this |
20
|
|
|
*/ |
21
|
48 |
|
public function withGlobalScopes(array $scopes) |
22
|
|
|
{ |
23
|
48 |
|
foreach ($scopes as $identifier => $scope) { |
24
|
|
|
$this->withGlobalScope($identifier, $scope); |
25
|
|
|
} |
26
|
|
|
|
27
|
48 |
|
return $this; |
28
|
|
|
} |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* Get the hydrated models without eager loading. |
32
|
|
|
* |
33
|
|
|
* @param array $columns |
34
|
|
|
* @return \Illuminate\Database\Eloquent\Model[] |
35
|
|
|
*/ |
36
|
290 |
|
public function getModels($columns = ['*']) |
37
|
|
|
{ |
38
|
290 |
|
$items = $this->query->get($columns)->all(); |
39
|
|
|
|
40
|
290 |
|
if ($this->getConnection() instanceof PostgresConnection) { |
|
|
|
|
41
|
76 |
|
$path = $this->model->getPathName(); |
42
|
|
|
|
43
|
76 |
|
if (isset($items[0]->$path)) { |
44
|
37 |
|
$this->replacePathSeparator( |
45
|
37 |
|
$items, |
46
|
|
|
$path, |
|
|
|
|
47
|
37 |
|
$this->model->getPathSeparator() |
|
|
|
|
48
|
|
|
); |
49
|
|
|
|
50
|
37 |
|
foreach ($this->model->getCustomPaths() as $path) { |
51
|
37 |
|
$this->replacePathSeparator( |
52
|
37 |
|
$items, |
53
|
37 |
|
$path['name'], |
54
|
37 |
|
$path['separator'] |
55
|
|
|
); |
56
|
|
|
} |
57
|
|
|
} |
58
|
|
|
} |
59
|
|
|
|
60
|
290 |
|
$table = (new $this->model())->getTable(); |
61
|
|
|
|
62
|
290 |
|
$models = $this->model->hydrate($items)->each->setTable($table); |
63
|
|
|
|
64
|
290 |
|
return $models->all(); |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* Replace the separator in a PostgreSQL path column. |
69
|
|
|
* |
70
|
|
|
* @param array $items |
71
|
|
|
* @param string $path |
72
|
|
|
* @param string $separator |
73
|
|
|
* @return void |
74
|
|
|
*/ |
75
|
37 |
|
protected function replacePathSeparator(array $items, $path, $separator) |
76
|
|
|
{ |
77
|
37 |
|
foreach ($items as $item) { |
78
|
37 |
|
$item->$path = str_replace( |
79
|
37 |
|
',', |
80
|
|
|
$separator, |
81
|
37 |
|
substr($item->$path, 1, -1) |
82
|
|
|
); |
83
|
|
|
} |
84
|
37 |
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* Get the expression grammar. |
88
|
|
|
* |
89
|
|
|
* @return \Staudenmeir\LaravelAdjacencyList\Query\Grammars\ExpressionGrammar |
90
|
|
|
*/ |
91
|
226 |
|
public function getExpressionGrammar() |
92
|
|
|
{ |
93
|
226 |
|
$driver = $this->query->getConnection()->getDriverName(); |
94
|
|
|
|
95
|
226 |
|
switch ($driver) { |
96
|
226 |
|
case 'mysql': |
97
|
60 |
|
return $this->query->getConnection()->withTablePrefix(new MySqlGrammar()); |
98
|
166 |
|
case 'pgsql': |
99
|
60 |
|
return $this->query->getConnection()->withTablePrefix(new PostgresGrammar()); |
100
|
106 |
|
case 'sqlite': |
101
|
60 |
|
return $this->query->getConnection()->withTablePrefix(new SQLiteGrammar()); |
102
|
46 |
|
case 'sqlsrv': |
103
|
46 |
|
return $this->query->getConnection()->withTablePrefix(new SqlServerGrammar()); |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
throw new RuntimeException('This database is not supported.'); // @codeCoverageIgnore |
107
|
|
|
} |
108
|
|
|
} |
109
|
|
|
|