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